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 "include/gpu/GrTypes.h"
12
13#include "include/core/SkColorSpace.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkRefCnt.h"
16#include "include/core/SkSurfaceProps.h"
17
18class SkColorSpace;
19
20#include "include/gpu/GrBackendSurface.h"
21
22#if SK_SUPPORT_GPU
23// TODO: remove the GrContext.h include once Flutter is updated
24#include "include/gpu/GrContext.h"
25#include "include/gpu/GrContextThreadSafeProxy.h"
26
27/** \class SkSurfaceCharacterization
28 A surface characterization contains all the information Ganesh requires to makes its internal
29 rendering decisions. When passed into a SkDeferredDisplayListRecorder it will copy the
30 data and pass it on to the SkDeferredDisplayList if/when it is created. Note that both of
31 those objects (the Recorder and the DisplayList) will take a ref on the
32 GrContextThreadSafeProxy and SkColorSpace objects.
33*/
34class SK_API SkSurfaceCharacterization {
35public:
36 enum class Textureable : bool { kNo = false, kYes = true };
37 enum class MipMapped : bool { kNo = false, kYes = true };
38 enum class UsesGLFBO0 : bool { kNo = false, kYes = true };
39 // This flag indicates if the surface is wrapping a raw Vulkan secondary command buffer.
40 enum class VulkanSecondaryCBCompatible : bool { kNo = false, kYes = true };
41
42 SkSurfaceCharacterization()
43 : fCacheMaxResourceBytes(0)
44 , fOrigin(kBottomLeft_GrSurfaceOrigin)
45 , fSampleCnt(0)
46 , fIsTextureable(Textureable::kYes)
47 , fIsMipMapped(MipMapped::kYes)
48 , fUsesGLFBO0(UsesGLFBO0::kNo)
49 , fVulkanSecondaryCBCompatible(VulkanSecondaryCBCompatible::kNo)
50 , fIsProtected(GrProtected::kNo)
51 , fSurfaceProps(0, kUnknown_SkPixelGeometry) {
52 }
53
54 SkSurfaceCharacterization(SkSurfaceCharacterization&&) = default;
55 SkSurfaceCharacterization& operator=(SkSurfaceCharacterization&&) = default;
56
57 SkSurfaceCharacterization(const SkSurfaceCharacterization&) = default;
58 SkSurfaceCharacterization& operator=(const SkSurfaceCharacterization& other) = default;
59 bool operator==(const SkSurfaceCharacterization& other) const;
60 bool operator!=(const SkSurfaceCharacterization& other) const {
61 return !(*this == other);
62 }
63
64 /*
65 * Return a new surface characterization with the only difference being a different width
66 * and height
67 */
68 SkSurfaceCharacterization createResized(int width, int height) const;
69
70 /*
71 * Return a new surface characterization with only a replaced color space
72 */
73 SkSurfaceCharacterization createColorSpace(sk_sp<SkColorSpace>) const;
74
75 /*
76 * Return a new surface characterization with the backend format replaced. A colorType
77 * must also be supplied to indicate the interpretation of the new format.
78 */
79 SkSurfaceCharacterization createBackendFormat(SkColorType colorType,
80 const GrBackendFormat& backendFormat) const;
81
82 /*
83 * Return a new surface characterization with just a different use of FBO0 (in GL)
84 */
85 SkSurfaceCharacterization createFBO0(bool usesGLFBO0) const;
86
87 GrContextThreadSafeProxy* contextInfo() const { return fContextInfo.get(); }
88 sk_sp<GrContextThreadSafeProxy> refContextInfo() const { return fContextInfo; }
89 size_t cacheMaxResourceBytes() const { return fCacheMaxResourceBytes; }
90
91 bool isValid() const { return kUnknown_SkColorType != fImageInfo.colorType(); }
92
93 const SkImageInfo& imageInfo() const { return fImageInfo; }
94 const GrBackendFormat& backendFormat() const { return fBackendFormat; }
95 GrSurfaceOrigin origin() const { return fOrigin; }
96 SkISize dimensions() const { return fImageInfo.dimensions(); }
97 int width() const { return fImageInfo.width(); }
98 int height() const { return fImageInfo.height(); }
99 SkColorType colorType() const { return fImageInfo.colorType(); }
100 int sampleCount() const { return fSampleCnt; }
101 bool isTextureable() const { return Textureable::kYes == fIsTextureable; }
102 bool isMipMapped() const { return MipMapped::kYes == fIsMipMapped; }
103 bool usesGLFBO0() const { return UsesGLFBO0::kYes == fUsesGLFBO0; }
104 bool vulkanSecondaryCBCompatible() const {
105 return VulkanSecondaryCBCompatible::kYes == fVulkanSecondaryCBCompatible;
106 }
107 GrProtected isProtected() const { return fIsProtected; }
108 SkColorSpace* colorSpace() const { return fImageInfo.colorSpace(); }
109 sk_sp<SkColorSpace> refColorSpace() const { return fImageInfo.refColorSpace(); }
110 const SkSurfaceProps& surfaceProps()const { return fSurfaceProps; }
111
112 // Is the provided backend texture compatible with this surface characterization?
113 bool isCompatible(const GrBackendTexture&) const;
114
115private:
116 friend class SkSurface_Gpu; // for 'set' & 'config'
117 friend class GrVkSecondaryCBDrawContext; // for 'set' & 'config'
118 friend class GrContextThreadSafeProxy; // for private ctor
119 friend class SkDeferredDisplayListRecorder; // for 'config'
120 friend class SkSurface; // for 'config'
121
122 SkDEBUGCODE(void validate() const;)
123
124 SkSurfaceCharacterization(sk_sp<GrContextThreadSafeProxy> contextInfo,
125 size_t cacheMaxResourceBytes,
126 const SkImageInfo& ii,
127 const GrBackendFormat& backendFormat,
128 GrSurfaceOrigin origin,
129 int sampleCnt,
130 Textureable isTextureable,
131 MipMapped isMipMapped,
132 UsesGLFBO0 usesGLFBO0,
133 VulkanSecondaryCBCompatible vulkanSecondaryCBCompatible,
134 GrProtected isProtected,
135 const SkSurfaceProps& surfaceProps)
136 : fContextInfo(std::move(contextInfo))
137 , fCacheMaxResourceBytes(cacheMaxResourceBytes)
138 , fImageInfo(ii)
139 , fBackendFormat(backendFormat)
140 , fOrigin(origin)
141 , fSampleCnt(sampleCnt)
142 , fIsTextureable(isTextureable)
143 , fIsMipMapped(isMipMapped)
144 , fUsesGLFBO0(usesGLFBO0)
145 , fVulkanSecondaryCBCompatible(vulkanSecondaryCBCompatible)
146 , fIsProtected(isProtected)
147 , fSurfaceProps(surfaceProps) {
148 SkDEBUGCODE(this->validate());
149 }
150
151 void set(sk_sp<GrContextThreadSafeProxy> contextInfo,
152 size_t cacheMaxResourceBytes,
153 const SkImageInfo& ii,
154 const GrBackendFormat& backendFormat,
155 GrSurfaceOrigin origin,
156 int sampleCnt,
157 Textureable isTextureable,
158 MipMapped isMipMapped,
159 UsesGLFBO0 usesGLFBO0,
160 VulkanSecondaryCBCompatible vulkanSecondaryCBCompatible,
161 GrProtected isProtected,
162 const SkSurfaceProps& surfaceProps) {
163 SkASSERT(MipMapped::kNo == isMipMapped || Textureable::kYes == isTextureable);
164 SkASSERT(Textureable::kNo == isTextureable || UsesGLFBO0::kNo == usesGLFBO0);
165
166 SkASSERT(VulkanSecondaryCBCompatible::kNo == vulkanSecondaryCBCompatible ||
167 UsesGLFBO0::kNo == usesGLFBO0);
168 SkASSERT(Textureable::kNo == isTextureable ||
169 VulkanSecondaryCBCompatible::kNo == vulkanSecondaryCBCompatible);
170
171 fContextInfo = contextInfo;
172 fCacheMaxResourceBytes = cacheMaxResourceBytes;
173
174 fImageInfo = ii;
175 fBackendFormat = backendFormat;
176 fOrigin = origin;
177 fSampleCnt = sampleCnt;
178 fIsTextureable = isTextureable;
179 fIsMipMapped = isMipMapped;
180 fUsesGLFBO0 = usesGLFBO0;
181 fVulkanSecondaryCBCompatible = vulkanSecondaryCBCompatible;
182 fIsProtected = isProtected;
183 fSurfaceProps = surfaceProps;
184
185 SkDEBUGCODE(this->validate());
186 }
187
188 sk_sp<GrContextThreadSafeProxy> fContextInfo;
189 size_t fCacheMaxResourceBytes;
190
191 SkImageInfo fImageInfo;
192 GrBackendFormat fBackendFormat;
193 GrSurfaceOrigin fOrigin;
194 int fSampleCnt;
195 Textureable fIsTextureable;
196 MipMapped fIsMipMapped;
197 UsesGLFBO0 fUsesGLFBO0;
198 VulkanSecondaryCBCompatible fVulkanSecondaryCBCompatible;
199 GrProtected fIsProtected;
200 SkSurfaceProps fSurfaceProps;
201};
202
203#else// !SK_SUPPORT_GPU
204
205class SK_API SkSurfaceCharacterization {
206public:
207 SkSurfaceCharacterization() : fSurfaceProps(0, kUnknown_SkPixelGeometry) { }
208
209 SkSurfaceCharacterization createResized(int width, int height) const {
210 return *this;
211 }
212
213 SkSurfaceCharacterization createColorSpace(sk_sp<SkColorSpace>) const {
214 return *this;
215 }
216
217 SkSurfaceCharacterization createBackendFormat(SkColorType, const GrBackendFormat&) const {
218 return *this;
219 }
220
221 SkSurfaceCharacterization createFBO0(bool usesGLFBO0) const {
222 return *this;
223 }
224
225 bool operator==(const SkSurfaceCharacterization& other) const { return false; }
226 bool operator!=(const SkSurfaceCharacterization& other) const {
227 return !(*this == other);
228 }
229
230 size_t cacheMaxResourceBytes() const { return 0; }
231
232 bool isValid() const { return false; }
233
234 int width() const { return 0; }
235 int height() const { return 0; }
236 int stencilCount() const { return 0; }
237 bool isTextureable() const { return false; }
238 bool isMipMapped() const { return false; }
239 bool usesGLFBO0() const { return false; }
240 bool vulkanSecondaryCBCompatible() const { return false; }
241 SkColorSpace* colorSpace() const { return nullptr; }
242 sk_sp<SkColorSpace> refColorSpace() const { return nullptr; }
243 const SkSurfaceProps& surfaceProps()const { return fSurfaceProps; }
244
245private:
246 SkSurfaceProps fSurfaceProps;
247};
248
249#endif
250
251#endif
252