| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "core/html/canvas/CanvasRenderingContext.h" | 26 #include "core/html/canvas/CanvasRenderingContext.h" |
| 27 | 27 |
| 28 #include "core/html/canvas/CanvasContextCreationAttributes.h" | 28 #include "core/html/canvas/CanvasContextCreationAttributes.h" |
| 29 #include "core/html/canvas/CanvasImageSource.h" | 29 #include "core/html/canvas/CanvasImageSource.h" |
| 30 #include "platform/RuntimeEnabledFeatures.h" | 30 #include "platform/RuntimeEnabledFeatures.h" |
| 31 #include "platform/weborigin/SecurityOrigin.h" | 31 #include "platform/weborigin/SecurityOrigin.h" |
| 32 | 32 |
| 33 const char* const kLinearRGBCanvasColorSpaceName = "linear-rgb"; | 33 constexpr const char* kLegacyCanvasColorSpaceName = "legacy-srgb"; |
| 34 const char* const kSRGBCanvasColorSpaceName = "srgb"; | 34 constexpr const char* kSRGBCanvasColorSpaceName = "srgb"; |
| 35 const char* const kLegacyCanvasColorSpaceName = "legacy-srgb"; | 35 constexpr const char* kLinearRGBCanvasColorSpaceName = "linear-rgb"; |
| 36 constexpr const char* kRec2020CanvasColorSpaceName = "rec-2020"; |
| 37 constexpr const char* kP3CanvasColorSpaceName = "p3"; |
| 36 | 38 |
| 37 namespace blink { | 39 namespace blink { |
| 38 | 40 |
| 39 CanvasRenderingContext::CanvasRenderingContext( | 41 CanvasRenderingContext::CanvasRenderingContext( |
| 40 HTMLCanvasElement* canvas, | 42 HTMLCanvasElement* canvas, |
| 41 OffscreenCanvas* offscreenCanvas, | 43 OffscreenCanvas* offscreenCanvas, |
| 42 const CanvasContextCreationAttributes& attrs) | 44 const CanvasContextCreationAttributes& attrs) |
| 43 : m_canvas(canvas), | 45 : m_canvas(canvas), |
| 44 m_offscreenCanvas(offscreenCanvas), | 46 m_offscreenCanvas(offscreenCanvas), |
| 45 m_colorSpace(kLegacyCanvasColorSpace), | 47 m_colorSpace(kLegacyCanvasColorSpace), |
| 46 m_creationAttributes(attrs) { | 48 m_creationAttributes(attrs) { |
| 47 if (RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled() && | 49 if (RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled() && |
| 48 RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) { | 50 RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) { |
| 49 if (m_creationAttributes.colorSpace() == kSRGBCanvasColorSpaceName) | 51 if (m_creationAttributes.colorSpace() == kSRGBCanvasColorSpaceName) |
| 50 m_colorSpace = kSRGBCanvasColorSpace; | 52 m_colorSpace = kSRGBCanvasColorSpace; |
| 51 else if (m_creationAttributes.colorSpace() == | 53 else if (m_creationAttributes.colorSpace() == |
| 52 kLinearRGBCanvasColorSpaceName) | 54 kLinearRGBCanvasColorSpaceName) |
| 53 m_colorSpace = kLinearRGBCanvasColorSpace; | 55 m_colorSpace = kLinearRGBCanvasColorSpace; |
| 56 else if (m_creationAttributes.colorSpace() == kRec2020CanvasColorSpaceName) |
| 57 m_colorSpace = kRec2020CanvasColorSpace; |
| 58 else if (m_creationAttributes.colorSpace() == kP3CanvasColorSpaceName) |
| 59 m_colorSpace = kP3CanvasColorSpace; |
| 54 } | 60 } |
| 55 // Make m_creationAttributes reflect the effective colorSpace rather than the | 61 // Make m_creationAttributes reflect the effective colorSpace rather than the |
| 56 // requested one | 62 // requested one |
| 57 m_creationAttributes.setColorSpace(colorSpaceAsString()); | 63 m_creationAttributes.setColorSpace(colorSpaceAsString()); |
| 58 } | 64 } |
| 59 | 65 |
| 60 WTF::String CanvasRenderingContext::colorSpaceAsString() const { | 66 WTF::String CanvasRenderingContext::colorSpaceAsString() const { |
| 61 switch (m_colorSpace) { | 67 switch (m_colorSpace) { |
| 68 case kLegacyCanvasColorSpace: |
| 69 return kLegacyCanvasColorSpaceName; |
| 62 case kSRGBCanvasColorSpace: | 70 case kSRGBCanvasColorSpace: |
| 63 return kSRGBCanvasColorSpaceName; | 71 return kSRGBCanvasColorSpaceName; |
| 64 case kLinearRGBCanvasColorSpace: | 72 case kLinearRGBCanvasColorSpace: |
| 65 return kLinearRGBCanvasColorSpaceName; | 73 return kLinearRGBCanvasColorSpaceName; |
| 66 case kLegacyCanvasColorSpace: | 74 case kRec2020CanvasColorSpace: |
| 67 return kLegacyCanvasColorSpaceName; | 75 return kRec2020CanvasColorSpaceName; |
| 76 case kP3CanvasColorSpace: |
| 77 return kP3CanvasColorSpaceName; |
| 68 }; | 78 }; |
| 69 CHECK(false); | 79 CHECK(false); |
| 70 return ""; | 80 return ""; |
| 71 } | 81 } |
| 72 | 82 |
| 73 sk_sp<SkColorSpace> CanvasRenderingContext::skColorSpace() const { | 83 sk_sp<SkColorSpace> CanvasRenderingContext::skColorSpace() const { |
| 84 if (!RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled() || |
| 85 !RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) { |
| 86 return nullptr; |
| 87 } |
| 74 switch (m_colorSpace) { | 88 switch (m_colorSpace) { |
| 89 case kLegacyCanvasColorSpace: |
| 90 // Legacy colorspace ensures color matching with CSS is preserved. |
| 91 // So if CSS is color corrected from sRGB to display space, then |
| 92 // canvas must do the same |
| 93 return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
| 75 case kSRGBCanvasColorSpace: | 94 case kSRGBCanvasColorSpace: |
| 76 return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); | 95 return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
| 77 case kLinearRGBCanvasColorSpace: | 96 case kLinearRGBCanvasColorSpace: |
| 78 return SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named); | 97 return SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named); |
| 79 case kLegacyCanvasColorSpace: | 98 case kRec2020CanvasColorSpace: { |
| 80 if (RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) { | 99 // TODO(zakerinasab): Replace this with proper constructor from Skia |
| 81 // Legacy colorspace ensures color matching with CSS is preserved. | 100 // when it is provided. |
| 82 // So if CSS is color corrected from sRGB to display space, then | 101 // https://en.wikipedia.org/wiki/Rec._2020 |
| 83 // canvas must do the same | 102 SkColorSpacePrimaries kPrimaries = {0.708, 0.292, 0.170, 0.797, |
| 84 return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); | 103 0.131, 0.046, 0.3127, 0.3290}; |
| 85 } | 104 SkMatrix44 kToXYZD50; |
| 86 return nullptr; | 105 if (!kPrimaries.toXYZD50(&kToXYZD50)) |
| 106 return nullptr; |
| 107 return SkColorSpace::MakeRGB( |
| 108 SkColorSpace::RenderTargetGamma::kLinear_RenderTargetGamma, |
| 109 kToXYZD50); |
| 110 } |
| 111 case kP3CanvasColorSpace: { |
| 112 // TODO(zakerinasab): Replace this with proper constructor from Skia |
| 113 // when it is provided. |
| 114 // https://en.wikipedia.org/wiki/DCI-P3 |
| 115 SkColorSpacePrimaries kPrimaries = {0.680, 0.320, 0.265, 0.690, |
| 116 0.150, 0.060, 0.3127, 0.3290}; |
| 117 SkMatrix44 kToXYZD50; |
| 118 if (!kPrimaries.toXYZD50(&kToXYZD50)) |
| 119 return nullptr; |
| 120 return SkColorSpace::MakeRGB( |
| 121 SkColorSpace::RenderTargetGamma::kLinear_RenderTargetGamma, |
| 122 kToXYZD50); |
| 123 } |
| 87 }; | 124 }; |
| 88 CHECK(false); | 125 CHECK(false); |
| 89 return nullptr; | 126 return nullptr; |
| 90 } | 127 } |
| 91 | 128 |
| 92 ColorBehavior CanvasRenderingContext::colorBehaviorForMediaDrawnToCanvas() | 129 ColorBehavior CanvasRenderingContext::colorBehaviorForMediaDrawnToCanvas() |
| 93 const { | 130 const { |
| 94 sk_sp<SkColorSpace> colorSpace = skColorSpace(); | 131 sk_sp<SkColorSpace> colorSpace = skColorSpace(); |
| 95 if (colorSpace) { | 132 if (colorSpace) { |
| 96 return ColorBehavior::transformTo(std::move(colorSpace)); | 133 return ColorBehavior::transformTo(std::move(colorSpace)); |
| 97 } | 134 } |
| 98 return ColorBehavior::transformToGlobalTarget(); | 135 return ColorBehavior::transformToGlobalTarget(); |
| 99 } | 136 } |
| 100 | 137 |
| 101 SkColorType CanvasRenderingContext::colorType() const { | 138 SkColorType CanvasRenderingContext::colorType() const { |
| 102 switch (m_colorSpace) { | 139 switch (m_colorSpace) { |
| 103 case kLinearRGBCanvasColorSpace: | 140 case kLinearRGBCanvasColorSpace: |
| 141 case kRec2020CanvasColorSpace: |
| 142 case kP3CanvasColorSpace: |
| 104 return kRGBA_F16_SkColorType; | 143 return kRGBA_F16_SkColorType; |
| 105 default: | 144 default: |
| 106 return kN32_SkColorType; | 145 return kN32_SkColorType; |
| 107 } | 146 } |
| 108 } | 147 } |
| 109 | 148 |
| 110 void CanvasRenderingContext::dispose() { | 149 void CanvasRenderingContext::dispose() { |
| 111 // HTMLCanvasElement and CanvasRenderingContext have a circular reference. | 150 // HTMLCanvasElement and CanvasRenderingContext have a circular reference. |
| 112 // When the pair is no longer reachable, their destruction order is non- | 151 // When the pair is no longer reachable, their destruction order is non- |
| 113 // deterministic, so the first of the two to be destroyed needs to notify | 152 // deterministic, so the first of the two to be destroyed needs to notify |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 } | 216 } |
| 178 return taintOrigin; | 217 return taintOrigin; |
| 179 } | 218 } |
| 180 | 219 |
| 181 DEFINE_TRACE(CanvasRenderingContext) { | 220 DEFINE_TRACE(CanvasRenderingContext) { |
| 182 visitor->trace(m_canvas); | 221 visitor->trace(m_canvas); |
| 183 visitor->trace(m_offscreenCanvas); | 222 visitor->trace(m_offscreenCanvas); |
| 184 } | 223 } |
| 185 | 224 |
| 186 } // namespace blink | 225 } // namespace blink |
| OLD | NEW |