summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/graphics/canvas_resource_params.h
blob: 42550e5e4a40d7d33405b3cbb353e59821504017 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_CANVAS_RESOURCE_PARAMS_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_CANVAS_RESOURCE_PARAMS_H_

#include "components/viz/common/resources/resource_format.h"
#include "third_party/blink/renderer/platform/graphics/canvas_color_params.h"
#include "third_party/blink/renderer/platform/graphics/graphics_types.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "ui/gfx/buffer_types.h"

class SkSurfaceProps;

namespace gfx {
class ColorSpace;
}

namespace blink {

// TODO(https://crbug.com/1157747): This class is a copy-paste of
// CanvasColorParams. This should be changed to specify a SkColorType,
// SkAlphaType, SkColorSpace, and SkFilterMode.
class PLATFORM_EXPORT CanvasResourceParams {
  DISALLOW_NEW();

 public:
  // The default constructor will create an output-blended 8-bit surface.
  CanvasResourceParams();
  CanvasResourceParams(CanvasColorSpace, SkColorType, SkAlphaType);
  explicit CanvasResourceParams(const SkImageInfo&);

  CanvasColorSpace ColorSpace() const { return color_space_; }

  void SetCanvasColorSpace(CanvasColorSpace c) { color_space_ = c; }
  void SetSkColorType(SkColorType color_type) { color_type_ = color_type; }

  // The pixel format to use for allocating SkSurfaces.
  SkColorType GetSkColorType() const { return color_type_; }
  uint8_t BytesPerPixel() const;

  // The color space in which pixels read from the canvas via a shader will be
  // returned. Note that for canvases with linear pixel math, these will be
  // converted from their storage space into a linear space.
  gfx::ColorSpace GetSamplerGfxColorSpace() const;

  // Return the color space of the underlying data for the canvas.
  gfx::ColorSpace GetStorageGfxColorSpace() const;
  sk_sp<SkColorSpace> GetSkColorSpace() const;
  SkAlphaType GetSkAlphaType() const { return alpha_type_; }
  SkSurfaceProps GetSkSurfaceProps() const;
  bool CanUseLcdText() const {
    // LCD text is safe when canvas is guaranteed not to be semi-transparent.
    return alpha_type_ == kOpaque_SkAlphaType;
  }

  // Gpu memory buffer parameters
  gfx::BufferFormat GetBufferFormat() const;
  uint32_t GLSizedInternalFormat() const;  // For GLES2, use Unsized
  uint32_t GLUnsizedInternalFormat() const;
  uint32_t GLType() const;

  viz::ResourceFormat TransferableResourceFormat() const;

 private:
  CanvasResourceParams(const sk_sp<SkColorSpace> color_space,
                       SkColorType color_type);

  CanvasColorSpace color_space_ = CanvasColorSpace::kSRGB;
  SkColorType color_type_ = kN32_SkColorType;
  SkAlphaType alpha_type_ = kPremul_SkAlphaType;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_CANVAS_RESOURCE_PARAMS_H_