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 15 matching lines...) Expand all Loading... |
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 #include "public/platform/Platform.h" | 32 #include "public/platform/Platform.h" |
33 | 33 |
34 constexpr const char* kLegacyCanvasColorSpaceName = "legacy-srgb"; | 34 constexpr const char* kLegacyCanvasColorSpaceName = "legacy-srgb"; |
35 constexpr const char* kSRGBCanvasColorSpaceName = "srgb"; | 35 constexpr const char* kSRGBCanvasColorSpaceName = "srgb"; |
36 constexpr const char* kLinearRGBCanvasColorSpaceName = "linear-rgb"; | 36 constexpr const char* kRec2020CanvasColorSpaceName = "rec2020"; |
37 constexpr const char* kRec2020CanvasColorSpaceName = "rec-2020"; | |
38 constexpr const char* kP3CanvasColorSpaceName = "p3"; | 37 constexpr const char* kP3CanvasColorSpaceName = "p3"; |
39 | 38 |
| 39 constexpr const char* kRGBA8CanvasPixelFormatName = "8-8-8-8"; |
| 40 constexpr const char* kRGB10A2CanvasPixelFormatName = "10-10-10-2"; |
| 41 constexpr const char* kRGBA12CanvasPixelFormatName = "12-12-12-12"; |
| 42 constexpr const char* kF16CanvasPixelFormatName = "float16"; |
| 43 |
40 namespace blink { | 44 namespace blink { |
41 | 45 |
42 CanvasRenderingContext::CanvasRenderingContext( | 46 CanvasRenderingContext::CanvasRenderingContext( |
43 HTMLCanvasElement* canvas, | 47 HTMLCanvasElement* canvas, |
44 OffscreenCanvas* offscreenCanvas, | 48 OffscreenCanvas* offscreenCanvas, |
45 const CanvasContextCreationAttributes& attrs) | 49 const CanvasContextCreationAttributes& attrs) |
46 : m_canvas(canvas), | 50 : m_canvas(canvas), |
47 m_offscreenCanvas(offscreenCanvas), | 51 m_offscreenCanvas(offscreenCanvas), |
48 m_colorSpace(kLegacyCanvasColorSpace), | 52 m_colorSpace(kLegacyCanvasColorSpace), |
| 53 m_pixelFormat(kRGBA8CanvasPixelFormat), |
| 54 m_linearPixelMath(false), |
49 m_creationAttributes(attrs) { | 55 m_creationAttributes(attrs) { |
50 if (RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled() && | 56 if (RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled() && |
51 RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) { | 57 RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) { |
52 if (m_creationAttributes.colorSpace() == kSRGBCanvasColorSpaceName) | 58 // Set the default color space to SRGB and continue |
53 m_colorSpace = kSRGBCanvasColorSpace; | 59 m_colorSpace = kSRGBCanvasColorSpace; |
54 else if (m_creationAttributes.colorSpace() == | 60 if (m_creationAttributes.colorSpace() == kRec2020CanvasColorSpaceName) |
55 kLinearRGBCanvasColorSpaceName) | |
56 m_colorSpace = kLinearRGBCanvasColorSpace; | |
57 else if (m_creationAttributes.colorSpace() == kRec2020CanvasColorSpaceName) | |
58 m_colorSpace = kRec2020CanvasColorSpace; | 61 m_colorSpace = kRec2020CanvasColorSpace; |
59 else if (m_creationAttributes.colorSpace() == kP3CanvasColorSpaceName) | 62 else if (m_creationAttributes.colorSpace() == kP3CanvasColorSpaceName) |
60 m_colorSpace = kP3CanvasColorSpace; | 63 m_colorSpace = kP3CanvasColorSpace; |
| 64 |
| 65 // For now, we only support RGBA8 (for SRGB) and F16 (for all). Everything |
| 66 // else falls back to SRGB + RGBA8. |
| 67 if (m_creationAttributes.pixelFormat() == kF16CanvasPixelFormatName) { |
| 68 m_pixelFormat = kF16CanvasPixelFormat; |
| 69 m_linearPixelMath = true; |
| 70 } else { |
| 71 m_colorSpace = kSRGBCanvasColorSpace; |
| 72 m_pixelFormat = kRGBA8CanvasPixelFormat; |
| 73 } |
61 } | 74 } |
62 // Make m_creationAttributes reflect the effective colorSpace rather than the | 75 |
63 // requested one | 76 // Make m_creationAttributes reflect the effective colorSpace, pixelFormat and |
| 77 // linearPixelMath rather than the requested one. |
64 m_creationAttributes.setColorSpace(colorSpaceAsString()); | 78 m_creationAttributes.setColorSpace(colorSpaceAsString()); |
| 79 m_creationAttributes.setPixelFormat(pixelFormatAsString()); |
| 80 m_creationAttributes.setLinearPixelMath(linearPixelMath()); |
65 } | 81 } |
66 | 82 |
67 WTF::String CanvasRenderingContext::colorSpaceAsString() const { | 83 WTF::String CanvasRenderingContext::colorSpaceAsString() const { |
68 switch (m_colorSpace) { | 84 switch (m_colorSpace) { |
69 case kLegacyCanvasColorSpace: | 85 case kLegacyCanvasColorSpace: |
70 return kLegacyCanvasColorSpaceName; | 86 return kLegacyCanvasColorSpaceName; |
71 case kSRGBCanvasColorSpace: | 87 case kSRGBCanvasColorSpace: |
72 return kSRGBCanvasColorSpaceName; | 88 return kSRGBCanvasColorSpaceName; |
73 case kLinearRGBCanvasColorSpace: | |
74 return kLinearRGBCanvasColorSpaceName; | |
75 case kRec2020CanvasColorSpace: | 89 case kRec2020CanvasColorSpace: |
76 return kRec2020CanvasColorSpaceName; | 90 return kRec2020CanvasColorSpaceName; |
77 case kP3CanvasColorSpace: | 91 case kP3CanvasColorSpace: |
78 return kP3CanvasColorSpaceName; | 92 return kP3CanvasColorSpaceName; |
79 }; | 93 }; |
80 CHECK(false); | 94 CHECK(false); |
81 return ""; | 95 return ""; |
82 } | 96 } |
83 | 97 |
| 98 WTF::String CanvasRenderingContext::pixelFormatAsString() const { |
| 99 switch (m_pixelFormat) { |
| 100 case kRGBA8CanvasPixelFormat: |
| 101 return kRGBA8CanvasPixelFormatName; |
| 102 case kRGB10A2CanvasPixelFormat: |
| 103 return kRGB10A2CanvasPixelFormatName; |
| 104 case kRGBA12CanvasPixelFormat: |
| 105 return kRGBA12CanvasPixelFormatName; |
| 106 case kF16CanvasPixelFormat: |
| 107 return kF16CanvasPixelFormatName; |
| 108 }; |
| 109 CHECK(false); |
| 110 return ""; |
| 111 } |
| 112 |
84 gfx::ColorSpace CanvasRenderingContext::gfxColorSpace() const { | 113 gfx::ColorSpace CanvasRenderingContext::gfxColorSpace() const { |
85 switch (m_colorSpace) { | 114 switch (m_colorSpace) { |
86 case kLegacyCanvasColorSpace: | 115 case kLegacyCanvasColorSpace: |
| 116 return gfx::ColorSpace::CreateSRGB(); |
87 case kSRGBCanvasColorSpace: | 117 case kSRGBCanvasColorSpace: |
| 118 if (m_pixelFormat == kF16CanvasPixelFormat) |
| 119 return gfx::ColorSpace::CreateSCRGBLinear(); |
88 return gfx::ColorSpace::CreateSRGB(); | 120 return gfx::ColorSpace::CreateSRGB(); |
89 case kLinearRGBCanvasColorSpace: | |
90 return gfx::ColorSpace::CreateSCRGBLinear(); | |
91 case kRec2020CanvasColorSpace: | 121 case kRec2020CanvasColorSpace: |
92 return gfx::ColorSpace(gfx::ColorSpace::PrimaryID::BT2020, | 122 return gfx::ColorSpace(gfx::ColorSpace::PrimaryID::BT2020, |
93 gfx::ColorSpace::TransferID::IEC61966_2_1); | 123 gfx::ColorSpace::TransferID::IEC61966_2_1); |
94 case kP3CanvasColorSpace: | 124 case kP3CanvasColorSpace: |
95 return gfx::ColorSpace(gfx::ColorSpace::PrimaryID::SMPTEST432_1, | 125 return gfx::ColorSpace(gfx::ColorSpace::PrimaryID::SMPTEST432_1, |
96 gfx::ColorSpace::TransferID::IEC61966_2_1); | 126 gfx::ColorSpace::TransferID::IEC61966_2_1); |
97 } | 127 } |
98 NOTREACHED(); | 128 NOTREACHED(); |
99 return gfx::ColorSpace(); | 129 return gfx::ColorSpace(); |
100 } | 130 } |
101 | 131 |
102 sk_sp<SkColorSpace> CanvasRenderingContext::skSurfaceColorSpace() const { | 132 sk_sp<SkColorSpace> CanvasRenderingContext::skSurfaceColorSpace() const { |
103 if (skSurfacesUseColorSpace()) | 133 if (skSurfacesUseColorSpace()) |
104 return gfxColorSpace().ToSkColorSpace(); | 134 return gfxColorSpace().ToSkColorSpace(); |
105 return nullptr; | 135 return nullptr; |
106 } | 136 } |
107 | 137 |
108 bool CanvasRenderingContext::skSurfacesUseColorSpace() const { | 138 bool CanvasRenderingContext::skSurfacesUseColorSpace() const { |
109 return m_colorSpace != kLegacyCanvasColorSpace; | 139 return m_colorSpace != kLegacyCanvasColorSpace; |
110 } | 140 } |
111 | 141 |
112 ColorBehavior CanvasRenderingContext::colorBehaviorForMediaDrawnToCanvas() | 142 ColorBehavior CanvasRenderingContext::colorBehaviorForMediaDrawnToCanvas() |
113 const { | 143 const { |
114 if (RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) | 144 if (RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) |
115 return ColorBehavior::transformTo(gfxColorSpace()); | 145 return ColorBehavior::transformTo(gfxColorSpace()); |
116 return ColorBehavior::transformToGlobalTarget(); | 146 return ColorBehavior::transformToGlobalTarget(); |
117 } | 147 } |
118 | 148 |
119 SkColorType CanvasRenderingContext::colorType() const { | 149 SkColorType CanvasRenderingContext::colorType() const { |
120 switch (m_colorSpace) { | 150 if (m_pixelFormat == kF16CanvasPixelFormat) |
121 case kLinearRGBCanvasColorSpace: | 151 return kRGBA_F16_SkColorType; |
122 case kRec2020CanvasColorSpace: | 152 return kN32_SkColorType; |
123 case kP3CanvasColorSpace: | |
124 return kRGBA_F16_SkColorType; | |
125 default: | |
126 return kN32_SkColorType; | |
127 } | |
128 } | 153 } |
129 | 154 |
130 void CanvasRenderingContext::dispose() { | 155 void CanvasRenderingContext::dispose() { |
131 if (m_finalizeFrameScheduled) { | 156 if (m_finalizeFrameScheduled) { |
132 Platform::current()->currentThread()->removeTaskObserver(this); | 157 Platform::current()->currentThread()->removeTaskObserver(this); |
133 } | 158 } |
134 | 159 |
135 // HTMLCanvasElement and CanvasRenderingContext have a circular reference. | 160 // HTMLCanvasElement and CanvasRenderingContext have a circular reference. |
136 // When the pair is no longer reachable, their destruction order is non- | 161 // When the pair is no longer reachable, their destruction order is non- |
137 // deterministic, so the first of the two to be destroyed needs to notify | 162 // deterministic, so the first of the two to be destroyed needs to notify |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 } | 245 } |
221 return taintOrigin; | 246 return taintOrigin; |
222 } | 247 } |
223 | 248 |
224 DEFINE_TRACE(CanvasRenderingContext) { | 249 DEFINE_TRACE(CanvasRenderingContext) { |
225 visitor->trace(m_canvas); | 250 visitor->trace(m_canvas); |
226 visitor->trace(m_offscreenCanvas); | 251 visitor->trace(m_offscreenCanvas); |
227 } | 252 } |
228 | 253 |
229 } // namespace blink | 254 } // namespace blink |
OLD | NEW |