| 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 kLegacyCanvasColorSpaceName = "legacy-srgb"; |
| 34 const char* const kSRGBCanvasColorSpaceName = "srgb"; |
| 33 const char* const kLinearRGBCanvasColorSpaceName = "linear-rgb"; | 35 const char* const kLinearRGBCanvasColorSpaceName = "linear-rgb"; |
| 34 const char* const kSRGBCanvasColorSpaceName = "srgb"; | 36 const char* const kLinearRec2020CanvasColorSpaceName = "linear-rec-2020"; |
| 35 const char* const kLegacyCanvasColorSpaceName = "legacy-srgb"; | 37 const char* const kLinearP3CanvasColorSpaceName = "linear-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() == |
| 57 kLinearRec2020CanvasColorSpaceName) |
| 58 m_colorSpace = kLinearRec2020CanvasColorSpace; |
| 59 else if (m_creationAttributes.colorSpace() == kLinearP3CanvasColorSpaceName) |
| 60 m_colorSpace = kLinearP3CanvasColorSpace; |
| 54 } | 61 } |
| 55 // Make m_creationAttributes reflect the effective colorSpace rather than the | 62 // Make m_creationAttributes reflect the effective colorSpace rather than the |
| 56 // requested one | 63 // requested one |
| 57 m_creationAttributes.setColorSpace(colorSpaceAsString()); | 64 m_creationAttributes.setColorSpace(colorSpaceAsString()); |
| 58 } | 65 } |
| 59 | 66 |
| 60 WTF::String CanvasRenderingContext::colorSpaceAsString() const { | 67 WTF::String CanvasRenderingContext::colorSpaceAsString() const { |
| 61 switch (m_colorSpace) { | 68 switch (m_colorSpace) { |
| 69 case kLegacyCanvasColorSpace: |
| 70 return kLegacyCanvasColorSpaceName; |
| 62 case kSRGBCanvasColorSpace: | 71 case kSRGBCanvasColorSpace: |
| 63 return kSRGBCanvasColorSpaceName; | 72 return kSRGBCanvasColorSpaceName; |
| 64 case kLinearRGBCanvasColorSpace: | 73 case kLinearRGBCanvasColorSpace: |
| 65 return kLinearRGBCanvasColorSpaceName; | 74 return kLinearRGBCanvasColorSpaceName; |
| 66 case kLegacyCanvasColorSpace: | 75 case kLinearRec2020CanvasColorSpace: |
| 67 return kLegacyCanvasColorSpaceName; | 76 return kLinearRec2020CanvasColorSpaceName; |
| 77 case kLinearP3CanvasColorSpace: |
| 78 return kLinearP3CanvasColorSpaceName; |
| 68 }; | 79 }; |
| 69 CHECK(false); | 80 CHECK(false); |
| 70 return ""; | 81 return ""; |
| 71 } | 82 } |
| 72 | 83 |
| 73 sk_sp<SkColorSpace> CanvasRenderingContext::skColorSpace() const { | 84 sk_sp<SkColorSpace> CanvasRenderingContext::skColorSpace() const { |
| 85 if (!RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled() || |
| 86 !RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) { |
| 87 return nullptr; |
| 88 } |
| 74 switch (m_colorSpace) { | 89 switch (m_colorSpace) { |
| 90 case kLegacyCanvasColorSpace: |
| 91 // Legacy colorspace ensures color matching with CSS is preserved. |
| 92 // So if CSS is color corrected from sRGB to display space, then |
| 93 // canvas must do the same |
| 94 return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
| 75 case kSRGBCanvasColorSpace: | 95 case kSRGBCanvasColorSpace: |
| 76 return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); | 96 return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
| 77 case kLinearRGBCanvasColorSpace: | 97 case kLinearRGBCanvasColorSpace: |
| 78 return SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named); | 98 return SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named); |
| 79 case kLegacyCanvasColorSpace: | 99 case kLinearRec2020CanvasColorSpace: { |
| 80 if (RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) { | 100 // TODO(zakerinasab): Replace this with proper constructor from Skia |
| 81 // Legacy colorspace ensures color matching with CSS is preserved. | 101 // when it is provided. |
| 82 // So if CSS is color corrected from sRGB to display space, then | 102 // https://en.wikipedia.org/wiki/Rec._2020 |
| 83 // canvas must do the same | 103 SkColorSpacePrimaries kPrimaries = {0.708, 0.292, 0.170, 0.797, |
| 84 return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); | 104 0.131, 0.046, 0.3127, 0.3290}; |
| 85 } | 105 SkMatrix44 kToXYZD50; |
| 86 return nullptr; | 106 if (!kPrimaries.toXYZD50(&kToXYZD50)) |
| 107 return nullptr; |
| 108 return SkColorSpace::MakeRGB( |
| 109 SkColorSpace::RenderTargetGamma::kLinear_RenderTargetGamma, |
| 110 kToXYZD50); |
| 111 } |
| 112 case kLinearP3CanvasColorSpace: { |
| 113 // TODO(zakerinasab): Replace this with proper constructor from Skia |
| 114 // when it is provided. |
| 115 // https://en.wikipedia.org/wiki/DCI-P3 |
| 116 SkColorSpacePrimaries kPrimaries = {0.680, 0.320, 0.265, 0.690, |
| 117 0.150, 0.060, 0.3127, 0.3290}; |
| 118 SkMatrix44 kToXYZD50; |
| 119 if (!kPrimaries.toXYZD50(&kToXYZD50)) |
| 120 return nullptr; |
| 121 return SkColorSpace::MakeRGB( |
| 122 SkColorSpace::RenderTargetGamma::kLinear_RenderTargetGamma, |
| 123 kToXYZD50); |
| 124 } |
| 87 }; | 125 }; |
| 88 CHECK(false); | 126 CHECK(false); |
| 89 return nullptr; | 127 return nullptr; |
| 90 } | 128 } |
| 91 | 129 |
| 92 ColorBehavior CanvasRenderingContext::colorBehaviorForMediaDrawnToCanvas() | 130 ColorBehavior CanvasRenderingContext::colorBehaviorForMediaDrawnToCanvas() |
| 93 const { | 131 const { |
| 94 sk_sp<SkColorSpace> colorSpace = skColorSpace(); | 132 sk_sp<SkColorSpace> colorSpace = skColorSpace(); |
| 95 if (colorSpace) { | 133 if (colorSpace) { |
| 96 return ColorBehavior::transformTo(std::move(colorSpace)); | 134 return ColorBehavior::transformTo(std::move(colorSpace)); |
| 97 } | 135 } |
| 98 return ColorBehavior::transformToGlobalTarget(); | 136 return ColorBehavior::transformToGlobalTarget(); |
| 99 } | 137 } |
| 100 | 138 |
| 101 SkColorType CanvasRenderingContext::colorType() const { | 139 SkColorType CanvasRenderingContext::colorType() const { |
| 102 switch (m_colorSpace) { | 140 switch (m_colorSpace) { |
| 103 case kLinearRGBCanvasColorSpace: | 141 case kLinearRGBCanvasColorSpace: |
| 142 case kLinearRec2020CanvasColorSpace: |
| 143 case kLinearP3CanvasColorSpace: |
| 104 return kRGBA_F16_SkColorType; | 144 return kRGBA_F16_SkColorType; |
| 105 default: | 145 default: |
| 106 return kN32_SkColorType; | 146 return kN32_SkColorType; |
| 107 } | 147 } |
| 108 } | 148 } |
| 109 | 149 |
| 110 void CanvasRenderingContext::dispose() { | 150 void CanvasRenderingContext::dispose() { |
| 111 // HTMLCanvasElement and CanvasRenderingContext have a circular reference. | 151 // HTMLCanvasElement and CanvasRenderingContext have a circular reference. |
| 112 // When the pair is no longer reachable, their destruction order is non- | 152 // 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 | 153 // 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 } | 217 } |
| 178 return taintOrigin; | 218 return taintOrigin; |
| 179 } | 219 } |
| 180 | 220 |
| 181 DEFINE_TRACE(CanvasRenderingContext) { | 221 DEFINE_TRACE(CanvasRenderingContext) { |
| 182 visitor->trace(m_canvas); | 222 visitor->trace(m_canvas); |
| 183 visitor->trace(m_offscreenCanvas); | 223 visitor->trace(m_offscreenCanvas); |
| 184 } | 224 } |
| 185 | 225 |
| 186 } // namespace blink | 226 } // namespace blink |
| OLD | NEW |