1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_CANVAS_COLOR_PARAMS_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_CANVAS_COLOR_PARAMS_H_
7 
8 #include "components/viz/common/resources/resource_format.h"
9 #include "third_party/blink/renderer/platform/graphics/graphics_types.h"
10 #include "third_party/blink/renderer/platform/platform_export.h"
11 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
12 #include "third_party/skia/include/core/SkColorSpace.h"
13 #include "third_party/skia/include/core/SkImageInfo.h"
14 #include "ui/gfx/buffer_types.h"
15 
16 class SkSurfaceProps;
17 
18 namespace cc {
19 class PaintCanvas;
20 }
21 
22 namespace gfx {
23 class ColorSpace;
24 }
25 
26 namespace blink {
27 
28 enum class CanvasColorSpace {
29   kSRGB,
30   kLinearRGB,
31   kRec2020,
32   kP3,
33 };
34 
35 enum class CanvasPixelFormat {
36   kRGBA8,
37   kBGRA8,
38   kF16,
39 };
40 
41 class PLATFORM_EXPORT CanvasColorParams {
42   DISALLOW_NEW();
43 
44  public:
45   // The default constructor will create an output-blended 8-bit surface.
46   CanvasColorParams();
47   CanvasColorParams(CanvasColorSpace, CanvasPixelFormat, OpacityMode);
48   explicit CanvasColorParams(const SkImageInfo&);
49 
GetNativeCanvasPixelFormat()50   static CanvasPixelFormat GetNativeCanvasPixelFormat() {
51     if (kN32_SkColorType == kRGBA_8888_SkColorType)
52       return CanvasPixelFormat::kRGBA8;
53     else if (kN32_SkColorType == kBGRA_8888_SkColorType)
54       return CanvasPixelFormat::kBGRA8;
55   }
56 
ColorSpace()57   CanvasColorSpace ColorSpace() const { return color_space_; }
PixelFormat()58   CanvasPixelFormat PixelFormat() const { return pixel_format_; }
GetOpacityMode()59   OpacityMode GetOpacityMode() const { return opacity_mode_; }
60 
SetCanvasColorSpace(CanvasColorSpace c)61   void SetCanvasColorSpace(CanvasColorSpace c) { color_space_ = c; }
SetCanvasPixelFormat(CanvasPixelFormat f)62   void SetCanvasPixelFormat(CanvasPixelFormat f) { pixel_format_ = f; }
SetOpacityMode(OpacityMode m)63   void SetOpacityMode(OpacityMode m) { opacity_mode_ = m; }
64 
65   // Indicates if pixels in this canvas color settings require any color
66   // conversion to be used in the passed canvas color settings.
67   bool NeedsColorConversion(const CanvasColorParams&) const;
68 
69   // The SkColorSpace to use in the SkImageInfo for allocated SkSurfaces. This
70   // is nullptr in legacy rendering mode and when the surface is supposed to be
71   // in sRGB (for which we wrap the canvas into a PaintCanvas along with an
72   // SkColorSpaceXformCanvas).
73   sk_sp<SkColorSpace> GetSkColorSpaceForSkSurfaces() const;
74 
75   // The pixel format to use for allocating SkSurfaces.
76   SkColorType GetSkColorType() const;
77   uint8_t BytesPerPixel() const;
78 
79   // The color space in which pixels read from the canvas via a shader will be
80   // returned. Note that for canvases with linear pixel math, these will be
81   // converted from their storage space into a linear space.
82   gfx::ColorSpace GetSamplerGfxColorSpace() const;
83 
84   // Return the color space of the underlying data for the canvas.
85   gfx::ColorSpace GetStorageGfxColorSpace() const;
86   sk_sp<SkColorSpace> GetSkColorSpace() const;
87   SkAlphaType GetSkAlphaType() const;
88   const SkSurfaceProps* GetSkSurfaceProps() const;
89 
90   // Gpu memory buffer parameters
91   gfx::BufferFormat GetBufferFormat() const;
92   uint32_t GLSizedInternalFormat() const;  // For GLES2, use Unsized
93   uint32_t GLUnsizedInternalFormat() const;
94   uint32_t GLType() const;
95 
96   viz::ResourceFormat TransferableResourceFormat() const;
97 
98  private:
99   CanvasColorParams(const sk_sp<SkColorSpace> color_space,
100                     SkColorType color_type);
101 
102   CanvasColorSpace color_space_ = CanvasColorSpace::kSRGB;
103   CanvasPixelFormat pixel_format_ = GetNativeCanvasPixelFormat();
104   OpacityMode opacity_mode_ = kNonOpaque;
105 };
106 
107 }  // namespace blink
108 
109 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_CANVAS_COLOR_PARAMS_H_
110