xref: /aosp_15_r20/external/skia/include/gpu/ganesh/GrYUVABackendTextures.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2020 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef GrYUVABackendTextures_DEFINED
9 #define GrYUVABackendTextures_DEFINED
10 
11 #include "include/core/SkTypes.h"
12 #include "include/core/SkYUVAInfo.h"
13 #include "include/gpu/GpuTypes.h"
14 #include "include/gpu/ganesh/GrBackendSurface.h"
15 #include "include/gpu/ganesh/GrTypes.h"
16 
17 #include <array>
18 #include <cstddef>
19 
20 enum SkYUVColorSpace : int;
21 
22 /**
23  * A description of a set GrBackendTextures that hold the planar data described by a SkYUVAInfo.
24  */
25 class SK_API GrYUVABackendTextureInfo {
26 public:
27     static constexpr auto kMaxPlanes = SkYUVAInfo::kMaxPlanes;
28 
29     /** Default GrYUVABackendTextureInfo is invalid. */
30     GrYUVABackendTextureInfo() = default;
31 
32     /**
33      * Initializes a GrYUVABackendTextureInfo to describe a set of textures that can store the
34      * planes indicated by the SkYUVAInfo. The texture dimensions are taken from the SkYUVAInfo's
35      * plane dimensions. All the described textures share a common origin. The planar image this
36      * describes will be mip mapped if all the textures are individually mip mapped as indicated
37      * by skgpu::Mipmapped. This will produce an invalid result (return false from isValid()) if the
38      * passed formats' channels don't agree with SkYUVAInfo.
39      */
40     GrYUVABackendTextureInfo(const SkYUVAInfo&,
41                              const GrBackendFormat[kMaxPlanes],
42                              skgpu::Mipmapped,
43                              GrSurfaceOrigin);
44 
45     GrYUVABackendTextureInfo(const GrYUVABackendTextureInfo&) = default;
46 
47     GrYUVABackendTextureInfo& operator=(const GrYUVABackendTextureInfo&) = default;
48 
49     bool operator==(const GrYUVABackendTextureInfo&) const;
50     bool operator!=(const GrYUVABackendTextureInfo& that) const { return !(*this == that); }
51 
yuvaInfo()52     const SkYUVAInfo& yuvaInfo() const { return fYUVAInfo; }
53 
yuvColorSpace()54     SkYUVColorSpace yuvColorSpace() const { return fYUVAInfo.yuvColorSpace(); }
55 
mipmapped()56     skgpu::Mipmapped mipmapped() const { return fMipmapped; }
57 
textureOrigin()58     GrSurfaceOrigin textureOrigin() const { return fTextureOrigin; }
59 
60     /** The number of SkPixmap planes, 0 if this GrYUVABackendTextureInfo is invalid. */
numPlanes()61     int numPlanes() const { return fYUVAInfo.numPlanes(); }
62 
63     /** Format of the ith plane, or invalid format if i >= numPlanes() */
planeFormat(int i)64     const GrBackendFormat& planeFormat(int i) const { return fPlaneFormats[i]; }
65 
66     /**
67      * Returns true if this has been configured with a valid SkYUVAInfo with compatible texture
68      * formats.
69      */
isValid()70     bool isValid() const { return fYUVAInfo.isValid(); }
71 
72     /**
73      * Computes a YUVALocations representation of the planar layout. The result is guaranteed to be
74      * valid if this->isValid().
75      */
76     SkYUVAInfo::YUVALocations toYUVALocations() const;
77 
78 private:
79     SkYUVAInfo fYUVAInfo;
80     GrBackendFormat fPlaneFormats[kMaxPlanes];
81     skgpu::Mipmapped fMipmapped = skgpu::Mipmapped::kNo;
82     GrSurfaceOrigin fTextureOrigin = kTopLeft_GrSurfaceOrigin;
83 };
84 
85 /**
86  * A set of GrBackendTextures that hold the planar data for an image described a SkYUVAInfo.
87  */
88 class SK_API GrYUVABackendTextures {
89 public:
90     GrYUVABackendTextures() = default;
91     GrYUVABackendTextures(const GrYUVABackendTextures&) = delete;
92     GrYUVABackendTextures(GrYUVABackendTextures&&) = default;
93 
94     GrYUVABackendTextures& operator=(const GrYUVABackendTextures&) = delete;
95     GrYUVABackendTextures& operator=(GrYUVABackendTextures&&) = default;
96 
97     GrYUVABackendTextures(const SkYUVAInfo&,
98                           const GrBackendTexture[SkYUVAInfo::kMaxPlanes],
99                           GrSurfaceOrigin textureOrigin);
100 
textures()101     const std::array<GrBackendTexture, SkYUVAInfo::kMaxPlanes>& textures() const {
102         return fTextures;
103     }
104 
texture(int i)105     GrBackendTexture texture(int i) const {
106         SkASSERT(i >= 0 && i < SkYUVAInfo::kMaxPlanes);
107         return fTextures[static_cast<size_t>(i)];
108     }
109 
yuvaInfo()110     const SkYUVAInfo& yuvaInfo() const { return fYUVAInfo; }
111 
numPlanes()112     int numPlanes() const { return fYUVAInfo.numPlanes(); }
113 
textureOrigin()114     GrSurfaceOrigin textureOrigin() const { return fTextureOrigin; }
115 
isValid()116     bool isValid() const { return fYUVAInfo.isValid(); }
117 
118     /**
119      * Computes a YUVALocations representation of the planar layout. The result is guaranteed to be
120      * valid if this->isValid().
121      */
122     SkYUVAInfo::YUVALocations toYUVALocations() const;
123 
124 private:
125     SkYUVAInfo fYUVAInfo;
126     std::array<GrBackendTexture, SkYUVAInfo::kMaxPlanes> fTextures;
127     GrSurfaceOrigin fTextureOrigin = kTopLeft_GrSurfaceOrigin;
128 };
129 
130 #endif
131