1 /*
2  * Copyright 2017 Google Inc.
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 SkSurfaceCharacterization_DEFINED
9 #define SkSurfaceCharacterization_DEFINED
10 
11 #include "GrTypes.h"
12 
13 #include "SkColorSpace.h"
14 #include "SkRefCnt.h"
15 #include "SkSurfaceProps.h"
16 
17 class SkColorSpace;
18 
19 #if SK_SUPPORT_GPU
20 // TODO: remove the GrContext.h include once Flutter is updated
21 #include "GrContext.h"
22 #include "GrContextThreadSafeProxy.h"
23 
24 /** \class SkSurfaceCharacterization
25     A surface characterization contains all the information Ganesh requires to makes its internal
26     rendering decisions. When passed into a SkDeferredDisplayListRecorder it will copy the
27     data and pass it on to the SkDeferredDisplayList if/when it is created. Note that both of
28     those objects (the Recorder and the DisplayList) will take a ref on the
29     GrContextThreadSafeProxy and SkColorSpace objects.
30 */
31 class SK_API SkSurfaceCharacterization {
32 public:
33     enum class Textureable : bool { kNo = false, kYes = true };
34     enum class MipMapped : bool { kNo = false, kYes = true };
35     enum class UsesGLFBO0 : bool { kNo = false, kYes = true };
36     // This flag indicates if the surface is wrapping a raw Vulkan secondary command buffer.
37     enum class VulkanSecondaryCBCompatible : bool { kNo = false, kYes = true };
38 
SkSurfaceCharacterization()39     SkSurfaceCharacterization()
40             : fCacheMaxResourceBytes(0)
41             , fOrigin(kBottomLeft_GrSurfaceOrigin)
42             , fConfig(kUnknown_GrPixelConfig)
43             , fFSAAType(GrFSAAType::kNone)
44             , fStencilCnt(0)
45             , fIsTextureable(Textureable::kYes)
46             , fIsMipMapped(MipMapped::kYes)
47             , fUsesGLFBO0(UsesGLFBO0::kNo)
48             , fVulkanSecondaryCBCompatible(VulkanSecondaryCBCompatible::kNo)
49             , fSurfaceProps(0, kUnknown_SkPixelGeometry) {
50     }
51 
52     SkSurfaceCharacterization(SkSurfaceCharacterization&&) = default;
53     SkSurfaceCharacterization& operator=(SkSurfaceCharacterization&&) = default;
54 
55     SkSurfaceCharacterization(const SkSurfaceCharacterization&) = default;
56     SkSurfaceCharacterization& operator=(const SkSurfaceCharacterization& other) = default;
57     bool operator==(const SkSurfaceCharacterization& other) const;
58     bool operator!=(const SkSurfaceCharacterization& other) const {
59         return !(*this == other);
60     }
61 
62     SkSurfaceCharacterization createResized(int width, int height) const;
63 
contextInfo()64     GrContextThreadSafeProxy* contextInfo() const { return fContextInfo.get(); }
refContextInfo()65     sk_sp<GrContextThreadSafeProxy> refContextInfo() const { return fContextInfo; }
cacheMaxResourceBytes()66     size_t cacheMaxResourceBytes() const { return fCacheMaxResourceBytes; }
67 
isValid()68     bool isValid() const { return kUnknown_SkColorType != fImageInfo.colorType(); }
69 
imageInfo()70     const SkImageInfo& imageInfo() const { return fImageInfo; }
origin()71     GrSurfaceOrigin origin() const { return fOrigin; }
width()72     int width() const { return fImageInfo.width(); }
height()73     int height() const { return fImageInfo.height(); }
colorType()74     SkColorType colorType() const { return fImageInfo.colorType(); }
fsaaType()75     GrFSAAType fsaaType() const { return fFSAAType; }
stencilCount()76     int stencilCount() const { return fStencilCnt; }
isTextureable()77     bool isTextureable() const { return Textureable::kYes == fIsTextureable; }
isMipMapped()78     bool isMipMapped() const { return MipMapped::kYes == fIsMipMapped; }
usesGLFBO0()79     bool usesGLFBO0() const { return UsesGLFBO0::kYes == fUsesGLFBO0; }
vulkanSecondaryCBCompatible()80     bool vulkanSecondaryCBCompatible() const {
81         return VulkanSecondaryCBCompatible::kYes == fVulkanSecondaryCBCompatible;
82     }
colorSpace()83     SkColorSpace* colorSpace() const { return fImageInfo.colorSpace(); }
refColorSpace()84     sk_sp<SkColorSpace> refColorSpace() const { return fImageInfo.refColorSpace(); }
surfaceProps()85     const SkSurfaceProps& surfaceProps()const { return fSurfaceProps; }
86 
87 private:
88     friend class SkSurface_Gpu; // for 'set' & 'config'
89     friend class GrContextThreadSafeProxy; // for private ctor
90     friend class SkDeferredDisplayListRecorder; // for 'config'
91     friend class SkSurface; // for 'config'
92 
config()93     GrPixelConfig config() const { return fConfig; }
94 
SkSurfaceCharacterization(sk_sp<GrContextThreadSafeProxy> contextInfo,size_t cacheMaxResourceBytes,const SkImageInfo & ii,GrSurfaceOrigin origin,GrPixelConfig config,GrFSAAType FSAAType,int stencilCnt,Textureable isTextureable,MipMapped isMipMapped,UsesGLFBO0 usesGLFBO0,VulkanSecondaryCBCompatible vulkanSecondaryCBCompatible,const SkSurfaceProps & surfaceProps)95     SkSurfaceCharacterization(sk_sp<GrContextThreadSafeProxy> contextInfo,
96                               size_t cacheMaxResourceBytes,
97                               const SkImageInfo& ii,
98                               GrSurfaceOrigin origin,
99                               GrPixelConfig config,
100                               GrFSAAType FSAAType, int stencilCnt,
101                               Textureable isTextureable, MipMapped isMipMapped,
102                               UsesGLFBO0 usesGLFBO0,
103                               VulkanSecondaryCBCompatible vulkanSecondaryCBCompatible,
104                               const SkSurfaceProps& surfaceProps)
105             : fContextInfo(std::move(contextInfo))
106             , fCacheMaxResourceBytes(cacheMaxResourceBytes)
107             , fImageInfo(ii)
108             , fOrigin(origin)
109             , fConfig(config)
110             , fFSAAType(FSAAType)
111             , fStencilCnt(stencilCnt)
112             , fIsTextureable(isTextureable)
113             , fIsMipMapped(isMipMapped)
114             , fUsesGLFBO0(usesGLFBO0)
115             , fVulkanSecondaryCBCompatible(vulkanSecondaryCBCompatible)
116             , fSurfaceProps(surfaceProps) {
117     }
118 
set(sk_sp<GrContextThreadSafeProxy> contextInfo,size_t cacheMaxResourceBytes,const SkImageInfo & ii,GrSurfaceOrigin origin,GrPixelConfig config,GrFSAAType fsaaType,int stencilCnt,Textureable isTextureable,MipMapped isMipMapped,UsesGLFBO0 usesGLFBO0,VulkanSecondaryCBCompatible vulkanSecondaryCBCompatible,const SkSurfaceProps & surfaceProps)119     void set(sk_sp<GrContextThreadSafeProxy> contextInfo,
120              size_t cacheMaxResourceBytes,
121              const SkImageInfo& ii,
122              GrSurfaceOrigin origin,
123              GrPixelConfig config,
124              GrFSAAType fsaaType,
125              int stencilCnt,
126              Textureable isTextureable,
127              MipMapped isMipMapped,
128              UsesGLFBO0 usesGLFBO0,
129              VulkanSecondaryCBCompatible vulkanSecondaryCBCompatible,
130              const SkSurfaceProps& surfaceProps) {
131         SkASSERT(MipMapped::kNo == isMipMapped || Textureable::kYes == isTextureable);
132         SkASSERT(Textureable::kNo == isTextureable || UsesGLFBO0::kNo == usesGLFBO0);
133 
134         SkASSERT(VulkanSecondaryCBCompatible::kNo == vulkanSecondaryCBCompatible ||
135                  UsesGLFBO0::kNo == usesGLFBO0);
136         SkASSERT(Textureable::kNo == isTextureable ||
137                  VulkanSecondaryCBCompatible::kNo == vulkanSecondaryCBCompatible);
138 
139         fContextInfo = contextInfo;
140         fCacheMaxResourceBytes = cacheMaxResourceBytes;
141 
142         fImageInfo = ii;
143         fOrigin = origin;
144         fConfig = config;
145         fFSAAType = fsaaType;
146         fStencilCnt = stencilCnt;
147         fIsTextureable = isTextureable;
148         fIsMipMapped = isMipMapped;
149         fUsesGLFBO0 = usesGLFBO0;
150         fVulkanSecondaryCBCompatible = vulkanSecondaryCBCompatible;
151         fSurfaceProps = surfaceProps;
152     }
153 
154     sk_sp<GrContextThreadSafeProxy> fContextInfo;
155     size_t                          fCacheMaxResourceBytes;
156 
157     SkImageInfo                     fImageInfo;
158     GrSurfaceOrigin                 fOrigin;
159     GrPixelConfig                   fConfig;
160     GrFSAAType                      fFSAAType;
161     int                             fStencilCnt;
162     Textureable                     fIsTextureable;
163     MipMapped                       fIsMipMapped;
164     UsesGLFBO0                      fUsesGLFBO0;
165     VulkanSecondaryCBCompatible     fVulkanSecondaryCBCompatible;
166     SkSurfaceProps                  fSurfaceProps;
167 };
168 
169 #else// !SK_SUPPORT_GPU
170 
171 class SK_API SkSurfaceCharacterization {
172 public:
SkSurfaceCharacterization()173     SkSurfaceCharacterization() : fSurfaceProps(0, kUnknown_SkPixelGeometry) { }
174 
createResized(int width,int height)175     SkSurfaceCharacterization createResized(int width, int height) const {
176         return *this;
177     }
178 
179     bool operator==(const SkSurfaceCharacterization& other) const { return false; }
180     bool operator!=(const SkSurfaceCharacterization& other) const {
181         return !(*this == other);
182     }
183 
cacheMaxResourceBytes()184     size_t cacheMaxResourceBytes() const { return 0; }
185 
isValid()186     bool isValid() const { return false; }
187 
width()188     int width() const { return 0; }
height()189     int height() const { return 0; }
stencilCount()190     int stencilCount() const { return 0; }
isTextureable()191     bool isTextureable() const { return false; }
isMipMapped()192     bool isMipMapped() const { return false; }
usesGLFBO0()193     bool usesGLFBO0() const { return false; }
vulkanSecondaryCBCompatible()194     bool vulkanSecondaryCBCompatible() const { return false; }
colorSpace()195     SkColorSpace* colorSpace() const { return nullptr; }
refColorSpace()196     sk_sp<SkColorSpace> refColorSpace() const { return nullptr; }
surfaceProps()197     const SkSurfaceProps& surfaceProps()const { return fSurfaceProps; }
198 
199 private:
200     SkSurfaceProps fSurfaceProps;
201 };
202 
203 #endif
204 
205 #endif
206