OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "core/css/MediaValuesCached.h" |
| 7 |
| 8 #include "core/css/CSSHelper.h" |
| 9 #include "core/css/CSSPrimitiveValue.h" |
| 10 #include "core/dom/Document.h" |
| 11 #include "core/rendering/RenderObject.h" |
| 12 |
| 13 namespace WebCore { |
| 14 |
| 15 PassRefPtr<MediaValues> MediaValuesCached::create(MediaValuesCachedData& data) |
| 16 { |
| 17 return adoptRef(new MediaValuesCached(data)); |
| 18 } |
| 19 |
| 20 PassRefPtr<MediaValues> MediaValuesCached::create(Document& document) |
| 21 { |
| 22 Document* executingDocument = getExecutingDocument(document); |
| 23 return MediaValuesCached::create(executingDocument->frame(), executingDocume
nt->renderer()->style()); |
| 24 } |
| 25 |
| 26 PassRefPtr<MediaValues> MediaValuesCached::create(LocalFrame* frame, RenderStyle
* style) |
| 27 { |
| 28 return adoptRef(new MediaValuesCached(frame, style)); |
| 29 } |
| 30 |
| 31 MediaValuesCached::MediaValuesCached(LocalFrame* frame, RenderStyle* style) |
| 32 { |
| 33 ASSERT(frame && style); |
| 34 ASSERT(isMainThread()); |
| 35 m_data.viewportWidth = calculateViewportWidth(frame); |
| 36 m_data.viewportHeight = calculateViewportHeight(frame); |
| 37 m_data.deviceWidth = calculateDeviceWidth(frame); |
| 38 m_data.deviceHeight = calculateDeviceHeight(frame); |
| 39 m_data.devicePixelRatio = calculateDevicePixelRatio(frame); |
| 40 m_data.colorBitsPerComponent = calculateColorBitsPerComponent(frame); |
| 41 m_data.monochromeBitsPerComponent = calculateMonochromeBitsPerComponent(fram
e); |
| 42 m_data.pointer = calculateLeastCapablePrimaryPointerDeviceType(frame); |
| 43 m_data.defaultFontSize = calculateDefaultFontSize(style); |
| 44 m_data.computedFontSize = calculateComputedFontSize(style); |
| 45 m_data.threeDEnabled = calculateThreeDEnabled(frame); |
| 46 m_data.scanMediaType = calculateScanMediaType(frame); |
| 47 m_data.screenMediaType = calculateScreenMediaType(frame); |
| 48 m_data.printMediaType = calculatePrintMediaType(frame); |
| 49 m_data.strictMode = calculateStrictMode(frame); |
| 50 } |
| 51 |
| 52 MediaValuesCached::MediaValuesCached(const MediaValuesCachedData& data) |
| 53 : m_data(data) |
| 54 { |
| 55 } |
| 56 |
| 57 PassRefPtr<MediaValues> MediaValuesCached::copy() const |
| 58 { |
| 59 return adoptRef(new MediaValuesCached(m_data)); |
| 60 } |
| 61 |
| 62 bool MediaValuesCached::computeLength(double value, unsigned short type, int& re
sult) const |
| 63 { |
| 64 // We're running in a background thread, so RenderStyle is not available. |
| 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 } |
| 127 |
| 128 bool MediaValuesCached::isSafeToSendToAnotherThread() const |
| 129 { |
| 130 return hasOneRef(); |
| 131 } |
| 132 |
| 133 int MediaValuesCached::viewportWidth() const |
| 134 { |
| 135 return m_data.viewportWidth; |
| 136 } |
| 137 |
| 138 int MediaValuesCached::viewportHeight() const |
| 139 { |
| 140 return m_data.viewportHeight; |
| 141 } |
| 142 |
| 143 int MediaValuesCached::deviceWidth() const |
| 144 { |
| 145 return m_data.deviceWidth; |
| 146 } |
| 147 |
| 148 int MediaValuesCached::deviceHeight() const |
| 149 { |
| 150 return m_data.deviceHeight; |
| 151 } |
| 152 |
| 153 float MediaValuesCached::devicePixelRatio() const |
| 154 { |
| 155 return m_data.devicePixelRatio; |
| 156 } |
| 157 |
| 158 int MediaValuesCached::colorBitsPerComponent() const |
| 159 { |
| 160 return m_data.colorBitsPerComponent; |
| 161 } |
| 162 |
| 163 int MediaValuesCached::monochromeBitsPerComponent() const |
| 164 { |
| 165 return m_data.monochromeBitsPerComponent; |
| 166 } |
| 167 |
| 168 MediaValues::PointerDeviceType MediaValuesCached::pointer() const |
| 169 { |
| 170 return m_data.pointer; |
| 171 } |
| 172 |
| 173 bool MediaValuesCached::threeDEnabled() const |
| 174 { |
| 175 return m_data.threeDEnabled; |
| 176 } |
| 177 |
| 178 bool MediaValuesCached::scanMediaType() const |
| 179 { |
| 180 return m_data.scanMediaType; |
| 181 } |
| 182 |
| 183 bool MediaValuesCached::screenMediaType() const |
| 184 { |
| 185 return m_data.screenMediaType; |
| 186 } |
| 187 |
| 188 bool MediaValuesCached::printMediaType() const |
| 189 { |
| 190 return m_data.printMediaType; |
| 191 } |
| 192 |
| 193 bool MediaValuesCached::strictMode() const |
| 194 { |
| 195 return m_data.strictMode; |
| 196 } |
| 197 |
| 198 Document* MediaValuesCached::document() const |
| 199 { |
| 200 return 0; |
| 201 } |
| 202 |
| 203 bool MediaValuesCached::hasValues() const |
| 204 { |
| 205 return true; |
| 206 } |
| 207 |
| 208 } // namespace |
OLD | NEW |