Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: third_party/WebKit/Source/core/html/canvas/CanvasRenderingContext.cpp

Issue 2708403003: Implement canvas color space IDL format for 2D canvas (Closed)
Patch Set: Fixing layout test failures Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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()) {
58 // Set the color space.
52 if (m_creationAttributes.colorSpace() == kSRGBCanvasColorSpaceName) 59 if (m_creationAttributes.colorSpace() == kSRGBCanvasColorSpaceName)
53 m_colorSpace = kSRGBCanvasColorSpace; 60 m_colorSpace = kSRGBCanvasColorSpace;
54 else if (m_creationAttributes.colorSpace() ==
55 kLinearRGBCanvasColorSpaceName)
56 m_colorSpace = kLinearRGBCanvasColorSpace;
57 else if (m_creationAttributes.colorSpace() == kRec2020CanvasColorSpaceName) 61 else if (m_creationAttributes.colorSpace() == kRec2020CanvasColorSpaceName)
58 m_colorSpace = kRec2020CanvasColorSpace; 62 m_colorSpace = kRec2020CanvasColorSpace;
59 else if (m_creationAttributes.colorSpace() == kP3CanvasColorSpaceName) 63 else if (m_creationAttributes.colorSpace() == kP3CanvasColorSpaceName)
60 m_colorSpace = kP3CanvasColorSpace; 64 m_colorSpace = kP3CanvasColorSpace;
65
66 // For now, we only support RGBA8 (for SRGB) and F16 (for all). Everything
67 // else falls back to SRGB + RGBA8.
68 if (m_creationAttributes.pixelFormat() == kF16CanvasPixelFormatName) {
69 m_pixelFormat = kF16CanvasPixelFormat;
70 m_linearPixelMath = true;
71 } else {
72 m_colorSpace = kSRGBCanvasColorSpace;
73 m_pixelFormat = kRGBA8CanvasPixelFormat;
74 }
61 } 75 }
62 // Make m_creationAttributes reflect the effective colorSpace rather than the 76
63 // requested one 77 // Make m_creationAttributes reflect the effective colorSpace, pixelFormat and
78 // linearPixelMath rather than the requested one.
64 m_creationAttributes.setColorSpace(colorSpaceAsString()); 79 m_creationAttributes.setColorSpace(colorSpaceAsString());
80 m_creationAttributes.setPixelFormat(pixelFormatAsString());
81 m_creationAttributes.setLinearPixelMath(linearPixelMath());
65 } 82 }
66 83
67 WTF::String CanvasRenderingContext::colorSpaceAsString() const { 84 WTF::String CanvasRenderingContext::colorSpaceAsString() const {
68 switch (m_colorSpace) { 85 switch (m_colorSpace) {
69 case kLegacyCanvasColorSpace: 86 case kLegacyCanvasColorSpace:
70 return kLegacyCanvasColorSpaceName; 87 return kLegacyCanvasColorSpaceName;
71 case kSRGBCanvasColorSpace: 88 case kSRGBCanvasColorSpace:
72 return kSRGBCanvasColorSpaceName; 89 return kSRGBCanvasColorSpaceName;
73 case kLinearRGBCanvasColorSpace:
74 return kLinearRGBCanvasColorSpaceName;
75 case kRec2020CanvasColorSpace: 90 case kRec2020CanvasColorSpace:
76 return kRec2020CanvasColorSpaceName; 91 return kRec2020CanvasColorSpaceName;
77 case kP3CanvasColorSpace: 92 case kP3CanvasColorSpace:
78 return kP3CanvasColorSpaceName; 93 return kP3CanvasColorSpaceName;
79 }; 94 };
80 CHECK(false); 95 CHECK(false);
81 return ""; 96 return "";
82 } 97 }
83 98
99 WTF::String CanvasRenderingContext::pixelFormatAsString() const {
100 switch (m_pixelFormat) {
101 case kRGBA8CanvasPixelFormat:
102 return kRGBA8CanvasPixelFormatName;
103 case kRGB10A2CanvasPixelFormat:
104 return kRGB10A2CanvasPixelFormatName;
105 case kRGBA12CanvasPixelFormat:
106 return kRGBA12CanvasPixelFormatName;
107 case kF16CanvasPixelFormat:
108 return kF16CanvasPixelFormatName;
109 };
110 CHECK(false);
111 return "";
112 }
113
84 gfx::ColorSpace CanvasRenderingContext::gfxColorSpace() const { 114 gfx::ColorSpace CanvasRenderingContext::gfxColorSpace() const {
85 switch (m_colorSpace) { 115 switch (m_colorSpace) {
86 case kLegacyCanvasColorSpace: 116 case kLegacyCanvasColorSpace:
117 return gfx::ColorSpace::CreateSRGB();
87 case kSRGBCanvasColorSpace: 118 case kSRGBCanvasColorSpace:
119 if (m_pixelFormat == kF16CanvasPixelFormat)
120 return gfx::ColorSpace::CreateSCRGBLinear();
88 return gfx::ColorSpace::CreateSRGB(); 121 return gfx::ColorSpace::CreateSRGB();
89 case kLinearRGBCanvasColorSpace:
90 return gfx::ColorSpace::CreateSCRGBLinear();
91 case kRec2020CanvasColorSpace: 122 case kRec2020CanvasColorSpace:
92 return gfx::ColorSpace(gfx::ColorSpace::PrimaryID::BT2020, 123 return gfx::ColorSpace(gfx::ColorSpace::PrimaryID::BT2020,
93 gfx::ColorSpace::TransferID::IEC61966_2_1); 124 gfx::ColorSpace::TransferID::IEC61966_2_1);
94 case kP3CanvasColorSpace: 125 case kP3CanvasColorSpace:
95 return gfx::ColorSpace(gfx::ColorSpace::PrimaryID::SMPTEST432_1, 126 return gfx::ColorSpace(gfx::ColorSpace::PrimaryID::SMPTEST432_1,
96 gfx::ColorSpace::TransferID::IEC61966_2_1); 127 gfx::ColorSpace::TransferID::IEC61966_2_1);
97 } 128 }
98 NOTREACHED(); 129 NOTREACHED();
99 return gfx::ColorSpace(); 130 return gfx::ColorSpace();
100 } 131 }
101 132
102 sk_sp<SkColorSpace> CanvasRenderingContext::skSurfaceColorSpace() const { 133 sk_sp<SkColorSpace> CanvasRenderingContext::skSurfaceColorSpace() const {
103 if (skSurfacesUseColorSpace()) 134 if (skSurfacesUseColorSpace())
104 return gfxColorSpace().ToSkColorSpace(); 135 return gfxColorSpace().ToSkColorSpace();
105 return nullptr; 136 return nullptr;
106 } 137 }
107 138
108 bool CanvasRenderingContext::skSurfacesUseColorSpace() const { 139 bool CanvasRenderingContext::skSurfacesUseColorSpace() const {
109 return m_colorSpace != kLegacyCanvasColorSpace; 140 return m_colorSpace != kLegacyCanvasColorSpace;
110 } 141 }
111 142
112 ColorBehavior CanvasRenderingContext::colorBehaviorForMediaDrawnToCanvas() 143 ColorBehavior CanvasRenderingContext::colorBehaviorForMediaDrawnToCanvas()
113 const { 144 const {
114 if (RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) 145 if (RuntimeEnabledFeatures::colorCorrectRenderingEnabled())
115 return ColorBehavior::transformTo(gfxColorSpace()); 146 return ColorBehavior::transformTo(gfxColorSpace());
116 return ColorBehavior::transformToGlobalTarget(); 147 return ColorBehavior::transformToGlobalTarget();
117 } 148 }
118 149
119 SkColorType CanvasRenderingContext::colorType() const { 150 SkColorType CanvasRenderingContext::colorType() const {
120 switch (m_colorSpace) { 151 if (m_pixelFormat == kF16CanvasPixelFormat)
121 case kLinearRGBCanvasColorSpace: 152 return kRGBA_F16_SkColorType;
122 case kRec2020CanvasColorSpace: 153 return kN32_SkColorType;
123 case kP3CanvasColorSpace:
124 return kRGBA_F16_SkColorType;
125 default:
126 return kN32_SkColorType;
127 }
128 } 154 }
129 155
130 void CanvasRenderingContext::dispose() { 156 void CanvasRenderingContext::dispose() {
131 if (m_finalizeFrameScheduled) { 157 if (m_finalizeFrameScheduled) {
132 Platform::current()->currentThread()->removeTaskObserver(this); 158 Platform::current()->currentThread()->removeTaskObserver(this);
133 } 159 }
134 160
135 // HTMLCanvasElement and CanvasRenderingContext have a circular reference. 161 // HTMLCanvasElement and CanvasRenderingContext have a circular reference.
136 // When the pair is no longer reachable, their destruction order is non- 162 // 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 163 // 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
220 } 246 }
221 return taintOrigin; 247 return taintOrigin;
222 } 248 }
223 249
224 DEFINE_TRACE(CanvasRenderingContext) { 250 DEFINE_TRACE(CanvasRenderingContext) {
225 visitor->trace(m_canvas); 251 visitor->trace(m_canvas);
226 visitor->trace(m_offscreenCanvas); 252 visitor->trace(m_offscreenCanvas);
227 } 253 }
228 254
229 } // namespace blink 255 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698