Chromium Code Reviews| Index: Source/core/css/MediaValuesCached.cpp |
| diff --git a/Source/core/css/MediaValuesCached.cpp b/Source/core/css/MediaValuesCached.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3e8435134dfe76a71ef42e7d73e602fe003d38ce |
| --- /dev/null |
| +++ b/Source/core/css/MediaValuesCached.cpp |
| @@ -0,0 +1,238 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "core/css/MediaValuesCached.h" |
| + |
| +#include "core/css/CSSHelper.h" |
| +#include "core/css/CSSPrimitiveValue.h" |
| + |
| +namespace WebCore { |
| + |
| +PassRefPtr<MediaValues> MediaValuesCached::create(MediaValuesInitializer& initializer) |
| +{ |
| + return adoptRef(new MediaValuesCached(initializer)); |
| +} |
| + |
| +PassRefPtr<MediaValues> MediaValuesCached::create(LocalFrame* frame, RenderStyle* style) |
| +{ |
| + return adoptRef(new MediaValuesCached(frame, style)); |
| +} |
| + |
| +MediaValuesCached::MediaValuesCached(LocalFrame* frame, RenderStyle* style) |
| +{ |
| + ASSERT(frame && style); |
| + m_viewportWidth = calculateViewportWidth(frame, style); |
| + m_viewportHeight = calculateViewportHeight(frame, style); |
| + m_deviceWidth = calculateDeviceWidth(frame); |
| + m_deviceHeight = calculateDeviceHeight(frame); |
| + m_devicePixelRatio = calculateDevicePixelRatio(frame); |
| + m_colorBitsPerComponent = calculateColorBitsPerComponent(frame); |
| + m_monochromeBitsPerComponent = calculateMonochromeBitsPerComponent(frame); |
| + m_pointer = calculateLeastCapablePrimaryPointerDeviceType(frame); |
| + m_defaultFontSize = calculateDefaultFontSize(style); |
| + m_computedFontSize = calculateComputedFontSize(style); |
| + m_hasXHeight = calculateHasXHeight(style); |
| + m_xHeight = calculateXHeight(style); |
| + m_zeroWidth = calculateZeroWidth(style); |
| + m_threeDEnabled = calculateThreeDEnabled(frame); |
| + m_scanMediaType = calculateScanMediaType(frame); |
| + m_screenMediaType = calculateScreenMediaType(frame); |
| + m_printMediaType = calculatePrintMediaType(frame); |
| + m_strictMode = calculateStrictMode(frame); |
| +} |
| + |
| +MediaValuesCached::MediaValuesCached(MediaValuesInitializer& initializer) |
| + : m_viewportWidth(initializer.viewportWidth) |
| + , m_viewportHeight(initializer.viewportHeight) |
| + , m_deviceWidth(initializer.deviceWidth) |
| + , m_deviceHeight(initializer.deviceHeight) |
| + , m_devicePixelRatio(initializer.devicePixelRatio) |
| + , m_colorBitsPerComponent(initializer.colorBitsPerComponent) |
| + , m_monochromeBitsPerComponent(initializer.monochromeBitsPerComponent) |
| + , m_pointer(initializer.pointer) |
| + , m_defaultFontSize(initializer.defaultFontSize) |
| + , m_computedFontSize(initializer.computedFontSize) |
| + , m_hasXHeight(initializer.hasXHeight) |
| + , m_xHeight(initializer.xHeight) |
| + , m_zeroWidth(initializer.zeroWidth) |
| + , m_threeDEnabled(initializer.threeDEnabled) |
| + , m_scanMediaType(initializer.scanMediaType) |
| + , m_screenMediaType(initializer.screenMediaType) |
| + , m_printMediaType(initializer.printMediaType) |
| + , m_strictMode(initializer.strictMode) |
| +{ |
| +} |
| + |
| +PassRefPtr<MediaValues> MediaValuesCached::copy() const |
| +{ |
| + MediaValuesInitializer initializer; |
| + initializer.viewportWidth = m_viewportWidth; |
| + initializer.viewportHeight = m_viewportHeight; |
| + initializer.deviceWidth = m_deviceWidth; |
| + initializer.deviceHeight = m_deviceHeight; |
| + initializer.devicePixelRatio = m_devicePixelRatio; |
| + initializer.colorBitsPerComponent = m_colorBitsPerComponent; |
| + initializer.monochromeBitsPerComponent = m_monochromeBitsPerComponent; |
| + initializer.pointer = m_pointer; |
| + initializer.defaultFontSize = m_defaultFontSize; |
| + initializer.computedFontSize = m_computedFontSize; |
| + initializer.hasXHeight = m_hasXHeight; |
| + initializer.xHeight = m_xHeight; |
| + initializer.zeroWidth = m_zeroWidth; |
| + initializer.threeDEnabled = m_threeDEnabled; |
| + initializer.scanMediaType = m_scanMediaType; |
| + initializer.screenMediaType = m_screenMediaType; |
| + initializer.printMediaType = m_printMediaType; |
| + initializer.strictMode = m_strictMode; |
| + |
| + return adoptRef(new MediaValuesCached(initializer)); |
| +} |
| + |
| +bool MediaValuesCached::computeLength(CSSPrimitiveValue& primitiveValue, int& result) const |
| +{ |
| + unsigned short type = primitiveValue.primitiveType(); |
| + double value = primitiveValue.getDoubleValue(); |
| + |
| + // We're running in a background thread, so RenderStyle is not available. |
| + // Nevertheless, we can evaluate lengths using cached values. |
| + // The logic here is duplicated from CSSPrimitiveValue::computeLengthDouble for performance reasons. |
|
eseidel
2014/04/14 23:10:13
Why? Can't we share code? Have them both call a
|
| + int factor = 0; |
| + switch (type) { |
| + case CSSPrimitiveValue::CSS_EMS: |
| + case CSSPrimitiveValue::CSS_REMS: |
| + factor = m_defaultFontSize; |
| + break; |
| + case CSSPrimitiveValue::CSS_PX: |
| + factor = 1; |
| + break; |
| + case CSSPrimitiveValue::CSS_EXS: |
| + // FIXME: We have a bug right now where the zoom will be applied twice to EX units. |
| + // We really need to compute EX using fontMetrics for the original specifiedSize and not use |
| + // our actual constructed rendering font. |
| + if (m_hasXHeight) |
| + factor = m_xHeight; |
| + else |
| + factor = m_defaultFontSize / 2.0; |
| + break; |
| + case CSSPrimitiveValue::CSS_CHS: |
| + factor = m_zeroWidth; |
| + break; |
| + case CSSPrimitiveValue::CSS_VW: |
| + factor = m_viewportWidth / 100; |
| + break; |
| + case CSSPrimitiveValue::CSS_VH: |
| + factor = m_viewportHeight / 100; |
| + break; |
| + case CSSPrimitiveValue::CSS_VMIN: |
| + factor = std::min(m_viewportWidth, m_viewportHeight) / 100; |
| + break; |
| + case CSSPrimitiveValue::CSS_VMAX: |
| + factor = std::max(m_viewportWidth, m_viewportHeight) / 100; |
| + break; |
| + case CSSPrimitiveValue::CSS_CM: |
| + factor = cssPixelsPerCentimeter; |
| + break; |
| + case CSSPrimitiveValue::CSS_MM: |
| + factor = cssPixelsPerMillimeter; |
| + break; |
| + case CSSPrimitiveValue::CSS_IN: |
| + factor = cssPixelsPerInch; |
| + break; |
| + case CSSPrimitiveValue::CSS_PT: |
| + factor = cssPixelsPerPoint; |
| + break; |
| + case CSSPrimitiveValue::CSS_PC: |
| + factor = cssPixelsPerPica; |
| + break; |
| + default: |
| + return false; |
| + } |
| + |
| + ASSERT(factor > 0); |
| + result = roundForImpreciseConversion<int>(value*factor); |
| + return true; |
| +} |
| + |
| +bool MediaValuesCached::isSafeToSendToAnotherThread() const |
| +{ |
| + return hasOneRef(); |
| +} |
| + |
| +int MediaValuesCached::viewportWidth() const |
| +{ |
| + return m_viewportWidth; |
| +} |
| + |
| +int MediaValuesCached::viewportHeight() const |
| +{ |
| + return m_viewportHeight; |
| +} |
| + |
| +int MediaValuesCached::deviceWidth() const |
| +{ |
| + return m_deviceWidth; |
| +} |
| + |
| +int MediaValuesCached::deviceHeight() const |
| +{ |
| + return m_deviceHeight; |
| +} |
| + |
| +float MediaValuesCached::devicePixelRatio() const |
| +{ |
| + return m_devicePixelRatio; |
| +} |
| + |
| +int MediaValuesCached::colorBitsPerComponent() const |
| +{ |
| + return m_colorBitsPerComponent; |
| +} |
| + |
| +int MediaValuesCached::monochromeBitsPerComponent() const |
| +{ |
| + return m_monochromeBitsPerComponent; |
| +} |
| + |
| +MediaValues::PointerDeviceType MediaValuesCached::pointer() const |
| +{ |
| + return m_pointer; |
| +} |
| + |
| +bool MediaValuesCached::threeDEnabled() const |
| +{ |
| + return m_threeDEnabled; |
| +} |
| + |
| +bool MediaValuesCached::scanMediaType() const |
| +{ |
| + return m_scanMediaType; |
| +} |
| + |
| +bool MediaValuesCached::screenMediaType() const |
| +{ |
| + return m_screenMediaType; |
| +} |
| + |
| +bool MediaValuesCached::printMediaType() const |
| +{ |
| + return m_printMediaType; |
| +} |
| + |
| +bool MediaValuesCached::strictMode() const |
| +{ |
| + return m_strictMode; |
| +} |
| + |
| +Document* MediaValuesCached::document() const |
| +{ |
| + return 0; |
| +} |
| + |
| +bool MediaValuesCached::hasValues() const |
| +{ |
| + return true; |
| +} |
| + |
| +} // namespace |