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