• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SkYUVAInfo&,
41                            SkSpan<const TextureInfo>,
42                            Mipmapped);
43     // DEPRECATED: No more need for a Recorder to construct YUVABackendTextureInfo
YUVABackendTextureInfo(Recorder *,const SkYUVAInfo & yuvaInfo,SkSpan<const TextureInfo> textures,Mipmapped mipmapped)44     YUVABackendTextureInfo(Recorder*,
45                            const SkYUVAInfo& yuvaInfo,
46                            SkSpan<const TextureInfo> textures,
47                            Mipmapped mipmapped)
48             : YUVABackendTextureInfo(yuvaInfo, textures, mipmapped) {}
49 
50     bool operator==(const YUVABackendTextureInfo&) const;
51     bool operator!=(const YUVABackendTextureInfo& that) const { return !(*this == that); }
52 
53     /** TextureInfo for the ith plane, or invalid if i >= numPlanes() */
planeTextureInfo(int i)54     const TextureInfo& planeTextureInfo(int i) const {
55         SkASSERT(i >= 0);
56         return fPlaneTextureInfos[static_cast<size_t>(i)];
57     }
58 
yuvaInfo()59     const SkYUVAInfo& yuvaInfo() const { return fYUVAInfo; }
60 
yuvColorSpace()61     SkYUVColorSpace yuvColorSpace() const { return fYUVAInfo.yuvColorSpace(); }
62 
mipmapped()63     Mipmapped mipmapped() const { return fMipmapped; }
64 
65     /** The number of planes, 0 if this YUVABackendTextureInfo is invalid. */
numPlanes()66     int numPlanes() const { return fYUVAInfo.numPlanes(); }
67 
68     /**
69      * Returns true if this has been configured with a valid SkYUVAInfo with compatible texture
70      * formats.
71      */
isValid()72     bool isValid() const { return fYUVAInfo.isValid(); }
73 
74     /**
75      * Computes a YUVALocations representation of the planar layout. The result is guaranteed to be
76      * valid if this->isValid().
77      */
78     SkYUVAInfo::YUVALocations toYUVALocations() const;
79 
80 private:
81     SkYUVAInfo fYUVAInfo;
82     std::array<TextureInfo, kMaxPlanes> fPlaneTextureInfos;
83     std::array<uint32_t, kMaxPlanes> fPlaneChannelMasks;
84     Mipmapped fMipmapped = Mipmapped::kNo;
85 };
86 
87 /**
88  * A set of BackendTextures that hold the planar data for an image described a SkYUVAInfo.
89  */
90 class SK_API YUVABackendTextures {
91 public:
92     static constexpr auto kMaxPlanes = SkYUVAInfo::kMaxPlanes;
93 
94     YUVABackendTextures() = default;
95     YUVABackendTextures(const YUVABackendTextures&) = delete;
96     YUVABackendTextures& operator=(const YUVABackendTextures&) = delete;
97 
98     /**
99      * Initializes a YUVABackendTextures object from a set of textures that store the planes
100      * indicated by the SkYUVAInfo. This will produce an invalid result (return false from
101      * isValid()) if the passed texture formats' channels don't agree with SkYUVAInfo.
102      */
103     YUVABackendTextures(const SkYUVAInfo&,
104                         SkSpan<const BackendTexture>);
105     // DEPRECATED: No more need for a Recorder to construct YUVABackendTextureInfo
YUVABackendTextures(Recorder *,const SkYUVAInfo & yuvaInfo,SkSpan<const BackendTexture> textures)106     YUVABackendTextures(Recorder*,
107                         const SkYUVAInfo& yuvaInfo,
108                         SkSpan<const BackendTexture> textures)
109             : YUVABackendTextures(yuvaInfo, textures) {}
110 
planeTextures()111     SkSpan<const BackendTexture> planeTextures() const {
112         return SkSpan<const BackendTexture>(fPlaneTextures);
113     }
114 
115     /** BackendTexture for the ith plane, or invalid if i >= numPlanes() */
planeTexture(int i)116     BackendTexture planeTexture(int i) const {
117         SkASSERT(i >= 0);
118         return fPlaneTextures[static_cast<size_t>(i)];
119     }
120 
yuvaInfo()121     const SkYUVAInfo& yuvaInfo() const { return fYUVAInfo; }
122 
yuvColorSpace()123     SkYUVColorSpace yuvColorSpace() const { return fYUVAInfo.yuvColorSpace(); }
124 
125     /** The number of planes, 0 if this YUVABackendTextureInfo is invalid. */
numPlanes()126     int numPlanes() const { return fYUVAInfo.numPlanes(); }
127 
128     /**
129      * Returns true if this has been configured with a valid SkYUVAInfo with compatible texture
130      * formats.
131      */
isValid()132     bool isValid() const { return fYUVAInfo.isValid(); }
133 
134     /**
135      * Computes a YUVALocations representation of the planar layout. The result is guaranteed to be
136      * valid if this->isValid().
137      */
138     SkYUVAInfo::YUVALocations toYUVALocations() const;
139 
140 private:
141     SkYUVAInfo fYUVAInfo;
142     std::array<BackendTexture, kMaxPlanes> fPlaneTextures;
143     std::array<uint32_t, kMaxPlanes> fPlaneChannelMasks;
144 };
145 
146 }  // End of namespace skgpu::graphite
147 
148 #endif  // skgpu_graphite_YUVABackendTextures_DEFINED
149