| 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 21 matching lines...) Expand all Loading... |
| 32 #include "public/platform/Platform.h" | 32 #include "public/platform/Platform.h" |
| 33 | 33 |
| 34 namespace blink { | 34 namespace blink { |
| 35 | 35 |
| 36 CanvasRenderingContext::CanvasRenderingContext( | 36 CanvasRenderingContext::CanvasRenderingContext( |
| 37 HTMLCanvasElement* canvas, | 37 HTMLCanvasElement* canvas, |
| 38 OffscreenCanvas* offscreen_canvas, | 38 OffscreenCanvas* offscreen_canvas, |
| 39 const CanvasContextCreationAttributes& attrs) | 39 const CanvasContextCreationAttributes& attrs) |
| 40 : canvas_(canvas), | 40 : canvas_(canvas), |
| 41 offscreen_canvas_(offscreen_canvas), | 41 offscreen_canvas_(offscreen_canvas), |
| 42 color_space_(kLegacyCanvasColorSpace), | 42 color_params_(kLegacyCanvasColorSpace, kRGBA8CanvasPixelFormat), |
| 43 pixel_format_(kRGBA8CanvasPixelFormat), | |
| 44 linear_pixel_math_(false), | |
| 45 creation_attributes_(attrs) { | 43 creation_attributes_(attrs) { |
| 46 if (RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled() && | 44 if (RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled() && |
| 47 RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) { | 45 RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) { |
| 48 // Set the default color space to SRGB and continue | 46 // Set the default color space to SRGB and continue |
| 49 color_space_ = kSRGBCanvasColorSpace; | 47 CanvasColorSpace color_space = kSRGBCanvasColorSpace; |
| 50 if (creation_attributes_.colorSpace() == kRec2020CanvasColorSpaceName) | 48 if (creation_attributes_.colorSpace() == kRec2020CanvasColorSpaceName) |
| 51 color_space_ = kRec2020CanvasColorSpace; | 49 color_space = kRec2020CanvasColorSpace; |
| 52 else if (creation_attributes_.colorSpace() == kP3CanvasColorSpaceName) | 50 else if (creation_attributes_.colorSpace() == kP3CanvasColorSpaceName) |
| 53 color_space_ = kP3CanvasColorSpace; | 51 color_space = kP3CanvasColorSpace; |
| 54 | 52 |
| 55 // For now, we only support RGBA8 (for SRGB) and F16 (for all). Everything | 53 // For now, we only support RGBA8 (for SRGB) and F16 (for all). Everything |
| 56 // else falls back to SRGB + RGBA8. | 54 // else falls back to SRGB + RGBA8. |
| 55 CanvasPixelFormat pixel_format = kRGBA8CanvasPixelFormat; |
| 57 if (creation_attributes_.pixelFormat() == kF16CanvasPixelFormatName) { | 56 if (creation_attributes_.pixelFormat() == kF16CanvasPixelFormatName) { |
| 58 pixel_format_ = kF16CanvasPixelFormat; | 57 pixel_format = kF16CanvasPixelFormat; |
| 59 linear_pixel_math_ = true; | |
| 60 } else { | 58 } else { |
| 61 color_space_ = kSRGBCanvasColorSpace; | 59 color_space = kSRGBCanvasColorSpace; |
| 62 pixel_format_ = kRGBA8CanvasPixelFormat; | 60 pixel_format = kRGBA8CanvasPixelFormat; |
| 63 } | 61 } |
| 62 |
| 63 color_params_ = CanvasColorParams(color_space, pixel_format); |
| 64 } | 64 } |
| 65 | 65 |
| 66 // Make m_creationAttributes reflect the effective colorSpace, pixelFormat and | 66 // Make m_creationAttributes reflect the effective colorSpace, pixelFormat and |
| 67 // linearPixelMath rather than the requested one. | 67 // linearPixelMath rather than the requested one. |
| 68 creation_attributes_.setColorSpace(ColorSpaceAsString()); | 68 creation_attributes_.setColorSpace(ColorSpaceAsString()); |
| 69 creation_attributes_.setPixelFormat(PixelFormatAsString()); | 69 creation_attributes_.setPixelFormat(PixelFormatAsString()); |
| 70 creation_attributes_.setLinearPixelMath(LinearPixelMath()); | 70 creation_attributes_.setLinearPixelMath(LinearPixelMath()); |
| 71 } | 71 } |
| 72 | 72 |
| 73 WTF::String CanvasRenderingContext::ColorSpaceAsString() const { | 73 WTF::String CanvasRenderingContext::ColorSpaceAsString() const { |
| 74 switch (color_space_) { | 74 switch (color_params_.color_space()) { |
| 75 case kLegacyCanvasColorSpace: | 75 case kLegacyCanvasColorSpace: |
| 76 return kLegacyCanvasColorSpaceName; | 76 return kLegacyCanvasColorSpaceName; |
| 77 case kSRGBCanvasColorSpace: | 77 case kSRGBCanvasColorSpace: |
| 78 return kSRGBCanvasColorSpaceName; | 78 return kSRGBCanvasColorSpaceName; |
| 79 case kRec2020CanvasColorSpace: | 79 case kRec2020CanvasColorSpace: |
| 80 return kRec2020CanvasColorSpaceName; | 80 return kRec2020CanvasColorSpaceName; |
| 81 case kP3CanvasColorSpace: | 81 case kP3CanvasColorSpace: |
| 82 return kP3CanvasColorSpaceName; | 82 return kP3CanvasColorSpaceName; |
| 83 }; | 83 }; |
| 84 CHECK(false); | 84 CHECK(false); |
| 85 return ""; | 85 return ""; |
| 86 } | 86 } |
| 87 | 87 |
| 88 WTF::String CanvasRenderingContext::PixelFormatAsString() const { | 88 WTF::String CanvasRenderingContext::PixelFormatAsString() const { |
| 89 switch (pixel_format_) { | 89 switch (color_params_.pixel_format()) { |
| 90 case kRGBA8CanvasPixelFormat: | 90 case kRGBA8CanvasPixelFormat: |
| 91 return kRGBA8CanvasPixelFormatName; | 91 return kRGBA8CanvasPixelFormatName; |
| 92 case kRGB10A2CanvasPixelFormat: | 92 case kRGB10A2CanvasPixelFormat: |
| 93 return kRGB10A2CanvasPixelFormatName; | 93 return kRGB10A2CanvasPixelFormatName; |
| 94 case kRGBA12CanvasPixelFormat: | 94 case kRGBA12CanvasPixelFormat: |
| 95 return kRGBA12CanvasPixelFormatName; | 95 return kRGBA12CanvasPixelFormatName; |
| 96 case kF16CanvasPixelFormat: | 96 case kF16CanvasPixelFormat: |
| 97 return kF16CanvasPixelFormatName; | 97 return kF16CanvasPixelFormatName; |
| 98 }; | 98 }; |
| 99 CHECK(false); | 99 CHECK(false); |
| 100 return ""; | 100 return ""; |
| 101 } | 101 } |
| 102 | 102 |
| 103 gfx::ColorSpace CanvasRenderingContext::GfxColorSpace() const { | 103 gfx::ColorSpace CanvasRenderingContext::GfxColorSpace() const { |
| 104 switch (color_space_) { | 104 return color_params_.GetGfxColorSpace(); |
| 105 case kLegacyCanvasColorSpace: | |
| 106 return gfx::ColorSpace::CreateSRGB(); | |
| 107 case kSRGBCanvasColorSpace: | |
| 108 if (pixel_format_ == kF16CanvasPixelFormat) | |
| 109 return gfx::ColorSpace::CreateSCRGBLinear(); | |
| 110 return gfx::ColorSpace::CreateSRGB(); | |
| 111 case kRec2020CanvasColorSpace: | |
| 112 return gfx::ColorSpace(gfx::ColorSpace::PrimaryID::BT2020, | |
| 113 gfx::ColorSpace::TransferID::IEC61966_2_1); | |
| 114 case kP3CanvasColorSpace: | |
| 115 return gfx::ColorSpace(gfx::ColorSpace::PrimaryID::SMPTEST432_1, | |
| 116 gfx::ColorSpace::TransferID::IEC61966_2_1); | |
| 117 } | |
| 118 NOTREACHED(); | |
| 119 return gfx::ColorSpace(); | |
| 120 } | 105 } |
| 121 | 106 |
| 122 sk_sp<SkColorSpace> CanvasRenderingContext::SkSurfaceColorSpace() const { | 107 sk_sp<SkColorSpace> CanvasRenderingContext::SkSurfaceColorSpace() const { |
| 123 if (SkSurfacesUseColorSpace()) | 108 return color_params_.GetSkColorSpaceForSkSurfaces(); |
| 124 return GfxColorSpace().ToSkColorSpace(); | |
| 125 return nullptr; | |
| 126 } | 109 } |
| 127 | 110 |
| 128 bool CanvasRenderingContext::SkSurfacesUseColorSpace() const { | 111 bool CanvasRenderingContext::SkSurfacesUseColorSpace() const { |
| 129 return color_space_ != kLegacyCanvasColorSpace; | 112 return color_params_.GetSkColorSpaceForSkSurfaces(); |
| 113 } |
| 114 |
| 115 bool CanvasRenderingContext::LinearPixelMath() const { |
| 116 return color_params_.LinearPixelMath(); |
| 130 } | 117 } |
| 131 | 118 |
| 132 ColorBehavior CanvasRenderingContext::ColorBehaviorForMediaDrawnToCanvas() | 119 ColorBehavior CanvasRenderingContext::ColorBehaviorForMediaDrawnToCanvas() |
| 133 const { | 120 const { |
| 134 if (RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) | 121 if (RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) |
| 135 return ColorBehavior::TransformTo(GfxColorSpace()); | 122 return ColorBehavior::TransformTo(GfxColorSpace()); |
| 136 return ColorBehavior::TransformToGlobalTarget(); | 123 return ColorBehavior::TransformToGlobalTarget(); |
| 137 } | 124 } |
| 138 | 125 |
| 126 CanvasColorSpace CanvasRenderingContext::ColorSpace() const { |
| 127 return color_params_.color_space(); |
| 128 } |
| 129 |
| 139 SkColorType CanvasRenderingContext::ColorType() const { | 130 SkColorType CanvasRenderingContext::ColorType() const { |
| 140 if (pixel_format_ == kF16CanvasPixelFormat) | 131 return color_params_.GetSkColorType(); |
| 141 return kRGBA_F16_SkColorType; | |
| 142 return kN32_SkColorType; | |
| 143 } | 132 } |
| 144 | 133 |
| 145 void CanvasRenderingContext::Dispose() { | 134 void CanvasRenderingContext::Dispose() { |
| 146 if (finalize_frame_scheduled_) { | 135 if (finalize_frame_scheduled_) { |
| 147 Platform::Current()->CurrentThread()->RemoveTaskObserver(this); | 136 Platform::Current()->CurrentThread()->RemoveTaskObserver(this); |
| 148 } | 137 } |
| 149 | 138 |
| 150 // HTMLCanvasElement and CanvasRenderingContext have a circular reference. | 139 // HTMLCanvasElement and CanvasRenderingContext have a circular reference. |
| 151 // When the pair is no longer reachable, their destruction order is non- | 140 // When the pair is no longer reachable, their destruction order is non- |
| 152 // deterministic, so the first of the two to be destroyed needs to notify | 141 // deterministic, so the first of the two to be destroyed needs to notify |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 } | 230 } |
| 242 return taint_origin; | 231 return taint_origin; |
| 243 } | 232 } |
| 244 | 233 |
| 245 DEFINE_TRACE(CanvasRenderingContext) { | 234 DEFINE_TRACE(CanvasRenderingContext) { |
| 246 visitor->Trace(canvas_); | 235 visitor->Trace(canvas_); |
| 247 visitor->Trace(offscreen_canvas_); | 236 visitor->Trace(offscreen_canvas_); |
| 248 } | 237 } |
| 249 | 238 |
| 250 } // namespace blink | 239 } // namespace blink |
| OLD | NEW |