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