OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "core/css/MediaValuesCached.h" | 6 #include "core/css/MediaValuesCached.h" |
7 | 7 |
8 #include "core/css/CSSHelper.h" | |
9 #include "core/css/CSSPrimitiveValue.h" | 8 #include "core/css/CSSPrimitiveValue.h" |
10 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
| 10 #include "core/html/imports/HTMLImportsController.h" |
11 #include "core/rendering/RenderObject.h" | 11 #include "core/rendering/RenderObject.h" |
12 | 12 |
13 namespace WebCore { | 13 namespace WebCore { |
14 | 14 |
| 15 PassRefPtr<MediaValues> MediaValuesCached::create() |
| 16 { |
| 17 return adoptRef(new MediaValuesCached()); |
| 18 } |
| 19 |
15 PassRefPtr<MediaValues> MediaValuesCached::create(MediaValuesCachedData& data) | 20 PassRefPtr<MediaValues> MediaValuesCached::create(MediaValuesCachedData& data) |
16 { | 21 { |
17 return adoptRef(new MediaValuesCached(data)); | 22 return adoptRef(new MediaValuesCached(data)); |
18 } | 23 } |
19 | 24 |
20 PassRefPtr<MediaValues> MediaValuesCached::create(Document& document) | 25 PassRefPtr<MediaValues> MediaValuesCached::create(Document& document) |
21 { | 26 { |
22 Document* executingDocument = getExecutingDocument(document); | 27 Document* executingDocument = document.importsController() ? document.import
sController()->master() : &document; |
23 return MediaValuesCached::create(executingDocument->frame(), executingDocume
nt->renderer()->style()); | 28 ASSERT(executingDocument); |
| 29 return MediaValuesCached::create(executingDocument->frame()); |
24 } | 30 } |
25 | 31 |
26 PassRefPtr<MediaValues> MediaValuesCached::create(LocalFrame* frame, RenderStyle
* style) | 32 PassRefPtr<MediaValues> MediaValuesCached::create(LocalFrame* frame) |
27 { | 33 { |
28 return adoptRef(new MediaValuesCached(frame, style)); | 34 if (!frame) |
| 35 return adoptRef(new MediaValuesCached()); |
| 36 return adoptRef(new MediaValuesCached(frame)); |
29 } | 37 } |
30 | 38 |
31 MediaValuesCached::MediaValuesCached(LocalFrame* frame, RenderStyle* style) | 39 MediaValuesCached::MediaValuesCached() |
32 { | 40 { |
33 ASSERT(frame && style); | 41 } |
| 42 |
| 43 MediaValuesCached::MediaValuesCached(LocalFrame* frame) |
| 44 { |
34 ASSERT(isMainThread()); | 45 ASSERT(isMainThread()); |
| 46 ASSERT(frame); |
| 47 // In case that frame is missing (e.g. for images that their document does n
ot have a frame) |
| 48 // We simply leave the MediaValues object with the default MediaValuesCached
Data values. |
35 m_data.viewportWidth = calculateViewportWidth(frame); | 49 m_data.viewportWidth = calculateViewportWidth(frame); |
36 m_data.viewportHeight = calculateViewportHeight(frame); | 50 m_data.viewportHeight = calculateViewportHeight(frame); |
37 m_data.deviceWidth = calculateDeviceWidth(frame); | 51 m_data.deviceWidth = calculateDeviceWidth(frame); |
38 m_data.deviceHeight = calculateDeviceHeight(frame); | 52 m_data.deviceHeight = calculateDeviceHeight(frame); |
39 m_data.devicePixelRatio = calculateDevicePixelRatio(frame); | 53 m_data.devicePixelRatio = calculateDevicePixelRatio(frame); |
40 m_data.colorBitsPerComponent = calculateColorBitsPerComponent(frame); | 54 m_data.colorBitsPerComponent = calculateColorBitsPerComponent(frame); |
41 m_data.monochromeBitsPerComponent = calculateMonochromeBitsPerComponent(fram
e); | 55 m_data.monochromeBitsPerComponent = calculateMonochromeBitsPerComponent(fram
e); |
42 m_data.pointer = calculateLeastCapablePrimaryPointerDeviceType(frame); | 56 m_data.pointer = calculateLeastCapablePrimaryPointerDeviceType(frame); |
43 m_data.defaultFontSize = calculateDefaultFontSize(style); | 57 m_data.defaultFontSize = calculateDefaultFontSize(frame); |
44 m_data.computedFontSize = calculateComputedFontSize(style); | |
45 m_data.threeDEnabled = calculateThreeDEnabled(frame); | 58 m_data.threeDEnabled = calculateThreeDEnabled(frame); |
46 m_data.scanMediaType = calculateScanMediaType(frame); | 59 m_data.scanMediaType = calculateScanMediaType(frame); |
47 m_data.screenMediaType = calculateScreenMediaType(frame); | 60 m_data.screenMediaType = calculateScreenMediaType(frame); |
48 m_data.printMediaType = calculatePrintMediaType(frame); | 61 m_data.printMediaType = calculatePrintMediaType(frame); |
49 m_data.strictMode = calculateStrictMode(frame); | 62 m_data.strictMode = calculateStrictMode(frame); |
50 } | 63 } |
51 | 64 |
52 MediaValuesCached::MediaValuesCached(const MediaValuesCachedData& data) | 65 MediaValuesCached::MediaValuesCached(const MediaValuesCachedData& data) |
53 : m_data(data) | 66 : m_data(data) |
54 { | 67 { |
55 } | 68 } |
56 | 69 |
57 PassRefPtr<MediaValues> MediaValuesCached::copy() const | 70 PassRefPtr<MediaValues> MediaValuesCached::copy() const |
58 { | 71 { |
59 return adoptRef(new MediaValuesCached(m_data)); | 72 return adoptRef(new MediaValuesCached(m_data)); |
60 } | 73 } |
61 | 74 |
62 bool MediaValuesCached::computeLength(double value, unsigned short type, int& re
sult) const | 75 bool MediaValuesCached::computeLength(double value, unsigned short type, int& re
sult) const |
63 { | 76 { |
64 // We're running in a background thread, so RenderStyle is not available. | 77 return MediaValues::computeLength(value, type, m_data.defaultFontSize, m_dat
a.viewportWidth, m_data.viewportHeight, result); |
65 // Nevertheless, we can evaluate lengths using cached values. | |
66 // | |
67 // The logic in this function is duplicated from CSSPrimitiveValue::computeL
engthDouble | |
68 // because MediaValues::computeLength needs nearly identical logic, but we h
aven't found a way to make | |
69 // CSSPrimitiveValue::computeLengthDouble more generic (to solve both cases)
without hurting performance. | |
70 | |
71 // FIXME - Unite the logic here with CSSPrimitiveValue is a performant way. | |
72 int factor = 0; | |
73 switch (type) { | |
74 case CSSPrimitiveValue::CSS_EMS: | |
75 case CSSPrimitiveValue::CSS_REMS: | |
76 factor = m_data.defaultFontSize; | |
77 break; | |
78 case CSSPrimitiveValue::CSS_PX: | |
79 factor = 1; | |
80 break; | |
81 case CSSPrimitiveValue::CSS_EXS: | |
82 // FIXME: We have a bug right now where the zoom will be applied twice t
o EX units. | |
83 // FIXME: We don't seem to be able to cache fontMetrics related values. | |
84 // Trying to access them is triggering some sort of microtask. Serving t
he spec's default instead. | |
85 factor = m_data.defaultFontSize / 2.0; | |
86 break; | |
87 case CSSPrimitiveValue::CSS_CHS: | |
88 // FIXME: We don't seem to be able to cache fontMetrics related values. | |
89 // Trying to access them is triggering some sort of microtask. Serving t
he (future) spec default instead. | |
90 factor = m_data.defaultFontSize / 2.0; | |
91 break; | |
92 case CSSPrimitiveValue::CSS_VW: | |
93 factor = m_data.viewportWidth / 100; | |
94 break; | |
95 case CSSPrimitiveValue::CSS_VH: | |
96 factor = m_data.viewportHeight / 100; | |
97 break; | |
98 case CSSPrimitiveValue::CSS_VMIN: | |
99 factor = std::min(m_data.viewportWidth, m_data.viewportHeight) / 100; | |
100 break; | |
101 case CSSPrimitiveValue::CSS_VMAX: | |
102 factor = std::max(m_data.viewportWidth, m_data.viewportHeight) / 100; | |
103 break; | |
104 case CSSPrimitiveValue::CSS_CM: | |
105 factor = cssPixelsPerCentimeter; | |
106 break; | |
107 case CSSPrimitiveValue::CSS_MM: | |
108 factor = cssPixelsPerMillimeter; | |
109 break; | |
110 case CSSPrimitiveValue::CSS_IN: | |
111 factor = cssPixelsPerInch; | |
112 break; | |
113 case CSSPrimitiveValue::CSS_PT: | |
114 factor = cssPixelsPerPoint; | |
115 break; | |
116 case CSSPrimitiveValue::CSS_PC: | |
117 factor = cssPixelsPerPica; | |
118 break; | |
119 default: | |
120 return false; | |
121 } | |
122 | |
123 ASSERT(factor > 0); | |
124 result = roundForImpreciseConversion<int>(value*factor); | |
125 return true; | |
126 } | 78 } |
127 | 79 |
128 bool MediaValuesCached::isSafeToSendToAnotherThread() const | 80 bool MediaValuesCached::isSafeToSendToAnotherThread() const |
129 { | 81 { |
130 return hasOneRef(); | 82 return hasOneRef(); |
131 } | 83 } |
132 | 84 |
133 int MediaValuesCached::viewportWidth() const | 85 int MediaValuesCached::viewportWidth() const |
134 { | 86 { |
135 return m_data.viewportWidth; | 87 return m_data.viewportWidth; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 { | 151 { |
200 return 0; | 152 return 0; |
201 } | 153 } |
202 | 154 |
203 bool MediaValuesCached::hasValues() const | 155 bool MediaValuesCached::hasValues() const |
204 { | 156 { |
205 return true; | 157 return true; |
206 } | 158 } |
207 | 159 |
208 } // namespace | 160 } // namespace |
OLD | NEW |