xref: /aosp_15_r20/external/skia/include/gpu/graphite/YUVABackendTextures.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 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 skgpu_graphite_YUVABackendTextures_DEFINED
9 #define skgpu_graphite_YUVABackendTextures_DEFINED
10 
11 #include "include/core/SkSpan.h"
12 #include "include/core/SkYUVAInfo.h"
13 #include "include/gpu/graphite/BackendTexture.h"
14 
15 #include <tuple>
16 
17 namespace skgpu::graphite {
18 class Recorder;
19 
20 /**
21  * A description of a set of BackendTextures that hold the planar data described by a SkYUVAInfo.
22  */
23 class SK_API YUVABackendTextureInfo {
24 public:
25     static constexpr auto kMaxPlanes = SkYUVAInfo::kMaxPlanes;
26 
27     /** Default YUVABackendTextureInfo is invalid. */
28     YUVABackendTextureInfo() = default;
29     YUVABackendTextureInfo(const YUVABackendTextureInfo&) = default;
30     YUVABackendTextureInfo& operator=(const YUVABackendTextureInfo&) = default;
31 
32     /**
33      * Initializes a YUVABackendTextureInfo 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 Mipmapped. This will produce an invalid result (return false from isValid()) if the
38      * passed formats' channels don't agree with SkYUVAInfo.
39      */
40     YUVABackendTextureInfo(const Recorder*,
41                            const SkYUVAInfo&,
42                            SkSpan<const TextureInfo>,
43                            Mipmapped);
44 
45     bool operator==(const YUVABackendTextureInfo&) const;
46     bool operator!=(const YUVABackendTextureInfo& that) const { return !(*this == that); }
47 
48     /** TextureInfo for the ith plane, or invalid if i >= numPlanes() */
planeTextureInfo(int i)49     const TextureInfo& planeTextureInfo(int i) const {
50         SkASSERT(i >= 0);
51         return fPlaneTextureInfos[static_cast<size_t>(i)];
52     }
53 
yuvaInfo()54     const SkYUVAInfo& yuvaInfo() const { return fYUVAInfo; }
55 
yuvColorSpace()56     SkYUVColorSpace yuvColorSpace() const { return fYUVAInfo.yuvColorSpace(); }
57 
mipmapped()58     Mipmapped mipmapped() const { return fMipmapped; }
59 
60     /** The number of planes, 0 if this YUVABackendTextureInfo is invalid. */
numPlanes()61     int numPlanes() const { return fYUVAInfo.numPlanes(); }
62 
63     /**
64      * Returns true if this has been configured with a valid SkYUVAInfo with compatible texture
65      * formats.
66      */
isValid()67     bool isValid() const { return fYUVAInfo.isValid(); }
68 
69     /**
70      * Computes a YUVALocations representation of the planar layout. The result is guaranteed to be
71      * valid if this->isValid().
72      */
73     SkYUVAInfo::YUVALocations toYUVALocations() const;
74 
75 private:
76     SkYUVAInfo fYUVAInfo;
77     std::array<TextureInfo, kMaxPlanes> fPlaneTextureInfos;
78     std::array<uint32_t, kMaxPlanes> fPlaneChannelMasks;
79     Mipmapped fMipmapped = Mipmapped::kNo;
80 };
81 
82 /**
83  * A set of BackendTextures that hold the planar data for an image described a SkYUVAInfo.
84  */
85 class SK_API YUVABackendTextures {
86 public:
87     static constexpr auto kMaxPlanes = SkYUVAInfo::kMaxPlanes;
88 
89     YUVABackendTextures() = default;
90     YUVABackendTextures(const YUVABackendTextures&) = delete;
91     YUVABackendTextures& operator=(const YUVABackendTextures&) = delete;
92 
93     /**
94      * Initializes a YUVABackendTextures object from a set of textures that store the planes
95      * indicated by the SkYUVAInfo. This will produce an invalid result (return false from
96      * isValid()) if the passed texture formats' channels don't agree with SkYUVAInfo.
97      */
98     YUVABackendTextures(const Recorder*,
99                         const SkYUVAInfo&,
100                         SkSpan<const BackendTexture>);
101 
planeTextures()102     SkSpan<const BackendTexture> planeTextures() const {
103         return SkSpan<const BackendTexture>(fPlaneTextures);
104     }
105 
106     /** BackendTexture for the ith plane, or invalid if i >= numPlanes() */
planeTexture(int i)107     BackendTexture planeTexture(int i) const {
108         SkASSERT(i >= 0);
109         return fPlaneTextures[static_cast<size_t>(i)];
110     }
111 
yuvaInfo()112     const SkYUVAInfo& yuvaInfo() const { return fYUVAInfo; }
113 
yuvColorSpace()114     SkYUVColorSpace yuvColorSpace() const { return fYUVAInfo.yuvColorSpace(); }
115 
116     /** The number of planes, 0 if this YUVABackendTextureInfo is invalid. */
numPlanes()117     int numPlanes() const { return fYUVAInfo.numPlanes(); }
118 
119     /**
120      * Returns true if this has been configured with a valid SkYUVAInfo with compatible texture
121      * formats.
122      */
isValid()123     bool isValid() const { return fYUVAInfo.isValid(); }
124 
125     /**
126      * Computes a YUVALocations representation of the planar layout. The result is guaranteed to be
127      * valid if this->isValid().
128      */
129     SkYUVAInfo::YUVALocations toYUVALocations() const;
130 
131 private:
132     SkYUVAInfo fYUVAInfo;
133     std::array<BackendTexture, kMaxPlanes> fPlaneTextures;
134     std::array<uint32_t, kMaxPlanes> fPlaneChannelMasks;
135 };
136 
137 }  // End of namespace skgpu::graphite
138 
139 #endif  // skgpu_graphite_YUVABackendTextures_DEFINED
140