• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GrBackendSurface_DEFINED
9 #define GrBackendSurface_DEFINED
10 
11 #include "include/gpu/GrTypes.h"
12 #include "include/gpu/gl/GrGLTypes.h"
13 #include "include/gpu/mock/GrMockTypes.h"
14 #include "include/gpu/vk/GrVkTypes.h"
15 #include "include/private/GrGLTypesPriv.h"
16 #include "include/private/GrVkTypesPriv.h"
17 
18 #ifdef SK_DAWN
19 #include "include/gpu/dawn/GrDawnTypes.h"
20 #endif
21 
22 class GrVkImageLayout;
23 class GrGLTextureParameters;
24 
25 #ifdef SK_DAWN
26 #include "dawn/dawncpp.h"
27 #endif
28 
29 #ifdef SK_METAL
30 #include "include/gpu/mtl/GrMtlTypes.h"
31 #endif
32 
33 #if GR_TEST_UTILS
34 class SkString;
35 #endif
36 
37 #if !SK_SUPPORT_GPU
38 
39 // SkSurface and SkImage rely on a minimal version of these always being available
40 class SK_API GrBackendTexture {
41 public:
GrBackendTexture()42     GrBackendTexture() {}
43 
isValid()44     bool isValid() const { return false; }
45 };
46 
47 class SK_API GrBackendRenderTarget {
48 public:
GrBackendRenderTarget()49     GrBackendRenderTarget() {}
50 
isValid()51     bool isValid() const { return false; }
52 };
53 #else
54 
55 enum class GrGLFormat;
56 
57 class SK_API GrBackendFormat {
58 public:
59     // Creates an invalid backend format.
GrBackendFormat()60     GrBackendFormat() {}
61 
62     GrBackendFormat(const GrBackendFormat& src);
63 
MakeGL(GrGLenum format,GrGLenum target)64     static GrBackendFormat MakeGL(GrGLenum format, GrGLenum target) {
65         return GrBackendFormat(format, target);
66     }
67 
MakeVk(VkFormat format)68     static GrBackendFormat MakeVk(VkFormat format) {
69         return GrBackendFormat(format, GrVkYcbcrConversionInfo());
70     }
71 
72     static GrBackendFormat MakeVk(const GrVkYcbcrConversionInfo& ycbcrInfo);
73 
74 #ifdef SK_DAWN
MakeDawn(dawn::TextureFormat format)75     static GrBackendFormat MakeDawn(dawn::TextureFormat format) {
76         return GrBackendFormat(format);
77     }
78 #endif
79 
80 #ifdef SK_METAL
MakeMtl(GrMTLPixelFormat format)81     static GrBackendFormat MakeMtl(GrMTLPixelFormat format) {
82         return GrBackendFormat(format);
83     }
84 #endif
85 
MakeMock(GrColorType colorType)86     static GrBackendFormat MakeMock(GrColorType colorType) {
87         return GrBackendFormat(colorType);
88     }
89 
90     bool operator==(const GrBackendFormat& that) const;
91     bool operator!=(const GrBackendFormat& that) const { return !(*this == that); }
92 
backend()93     GrBackendApi backend() const { return fBackend; }
textureType()94     GrTextureType textureType() const { return fTextureType; }
95 
96     /**
97      * If the backend API is GL this gets the format as a GrGLFormat. Otherwise, returns
98      * GrGLFormat::kUnknown.
99      */
100     GrGLFormat asGLFormat() const;
101 
102     /**
103      * If the backend API is Vulkan this gets the format as a VkFormat and returns true. Otherwise,
104      * returns false.
105      */
106     bool asVkFormat(VkFormat*) const;
107 
108     const GrVkYcbcrConversionInfo* getVkYcbcrConversionInfo() const;
109 
110 #ifdef SK_DAWN
111     /**
112      * If the backend API is Dawn this gets the format as a dawn::TextureFormat and returns true.
113      * Otherwise, returns false.
114      */
115     bool asDawnFormat(dawn::TextureFormat*) const;
116 #endif
117 
118 #ifdef SK_METAL
119     /**
120      * If the backend API is Metal this gets the format as a GrMtlPixelFormat. Otherwise,
121      * Otherwise, returns MTLPixelFormatInvalid.
122      */
123     GrMTLPixelFormat asMtlFormat() const;
124 #endif
125 
126     /**
127      * If the backend API is Mock this gets the format as a GrColorType. Otherwise, returns
128      * GrColorType::kUnknown.
129      */
130     GrColorType asMockColorType() const;
131 
132     // If possible, copies the GrBackendFormat and forces the texture type to be Texture2D. If the
133     // GrBackendFormat was for Vulkan and it originally had a GrVkYcbcrConversionInfo, we will
134     // remove the conversion and set the format to be VK_FORMAT_R8G8B8A8_UNORM.
135     GrBackendFormat makeTexture2D() const;
136 
137     // Returns true if the backend format has been initialized.
isValid()138     bool isValid() const { return fValid; }
139 
140 #if GR_TEST_UTILS
141     SkString toStr() const;
142 #endif
143 
144 private:
145     GrBackendFormat(GrGLenum format, GrGLenum target);
146 
147     GrBackendFormat(const VkFormat vkFormat, const GrVkYcbcrConversionInfo&);
148 
149 #ifdef SK_DAWN
150     GrBackendFormat(dawn::TextureFormat format);
151 #endif
152 
153 #ifdef SK_METAL
154     GrBackendFormat(const GrMTLPixelFormat mtlFormat);
155 #endif
156 
157     GrBackendFormat(GrColorType colorType);
158 
159     GrBackendApi fBackend = GrBackendApi::kMock;
160     bool         fValid = false;
161 
162     union {
163         GrGLenum         fGLFormat; // the sized, internal format of the GL resource
164         struct {
165             VkFormat                 fFormat;
166             GrVkYcbcrConversionInfo  fYcbcrConversionInfo;
167         }                fVk;
168 #ifdef SK_DAWN
169         dawn::TextureFormat fDawnFormat;
170 #endif
171 
172 #ifdef SK_METAL
173         GrMTLPixelFormat fMtlFormat;
174 #endif
175         GrColorType      fMockColorType;
176     };
177     GrTextureType fTextureType = GrTextureType::kNone;
178 };
179 
180 class SK_API GrBackendTexture {
181 public:
182     // Creates an invalid backend texture.
GrBackendTexture()183     GrBackendTexture() : fIsValid(false) {}
184 
185     // The GrGLTextureInfo must have a valid fFormat.
186     GrBackendTexture(int width,
187                      int height,
188                      GrMipMapped,
189                      const GrGLTextureInfo& glInfo);
190 
191     GrBackendTexture(int width,
192                      int height,
193                      const GrVkImageInfo& vkInfo);
194 
195 #ifdef SK_METAL
196     GrBackendTexture(int width,
197                      int height,
198                      GrMipMapped,
199                      const GrMtlTextureInfo& mtlInfo);
200 #endif
201 
202 #ifdef SK_DAWN
203     GrBackendTexture(int width,
204                      int height,
205                      const GrDawnImageInfo& dawnInfo);
206 #endif
207 
208     GrBackendTexture(int width,
209                      int height,
210                      GrMipMapped,
211                      const GrMockTextureInfo& mockInfo);
212 
213     GrBackendTexture(const GrBackendTexture& that);
214 
215     ~GrBackendTexture();
216 
217     GrBackendTexture& operator=(const GrBackendTexture& that);
218 
width()219     int width() const { return fWidth; }
height()220     int height() const { return fHeight; }
hasMipMaps()221     bool hasMipMaps() const { return GrMipMapped::kYes == fMipMapped; }
backend()222     GrBackendApi backend() const {return fBackend; }
223 
224     // If the backend API is GL, copies a snapshot of the GrGLTextureInfo struct into the passed in
225     // pointer and returns true. Otherwise returns false if the backend API is not GL.
226     bool getGLTextureInfo(GrGLTextureInfo*) const;
227 
228     // Call this to indicate that the texture parameters have been modified in the GL context
229     // externally to GrContext.
230     void glTextureParametersModified();
231 
232 #ifdef SK_DAWN
233     // If the backend API is Dawn, copies a snapshot of the GrDawnImageInfo struct into the passed
234     // in pointer and returns true. Otherwise returns false if the backend API is not Dawn.
235     bool getDawnImageInfo(GrDawnImageInfo*) const;
236 #endif
237 
238     // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
239     // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
240     // state. Otherwise returns false if the backend API is not Vulkan.
241     bool getVkImageInfo(GrVkImageInfo*) const;
242 
243     // Anytime the client changes the VkImageLayout of the VkImage captured by this
244     // GrBackendTexture, they must call this function to notify Skia of the changed layout.
245     void setVkImageLayout(VkImageLayout);
246 
247 #ifdef SK_METAL
248     // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
249     // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
250     bool getMtlTextureInfo(GrMtlTextureInfo*) const;
251 #endif
252 
253     // Get the GrBackendFormat for this texture (or an invalid format if this is not valid).
254     GrBackendFormat getBackendFormat() const;
255 
256     // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
257     // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
258     bool getMockTextureInfo(GrMockTextureInfo*) const;
259 
260     // Returns true if we are working with protected content.
261     bool isProtected() const;
262 
263     // Returns true if the backend texture has been initialized.
isValid()264     bool isValid() const { return fIsValid; }
265 
266     // Returns true if both textures are valid and refer to the same API texture.
267     bool isSameTexture(const GrBackendTexture&);
268 
269 #if GR_TEST_UTILS
270     static bool TestingOnly_Equals(const GrBackendTexture& , const GrBackendTexture&);
271 #endif
272 
273 private:
274 
275 #ifdef SK_GL
276     friend class GrGLTexture;
277     friend class GrGLGpu;    // for getGLTextureParams
278     GrBackendTexture(int width,
279                      int height,
280                      GrMipMapped,
281                      const GrGLTextureInfo,
282                      sk_sp<GrGLTextureParameters>);
283     sk_sp<GrGLTextureParameters> getGLTextureParams() const;
284 #endif
285 
286 #ifdef SK_VULKAN
287     friend class GrVkTexture;
288     friend class GrVkGpu;    // for getGrVkImageLayout
289     GrBackendTexture(int width,
290                      int height,
291                      const GrVkImageInfo& vkInfo,
292                      sk_sp<GrVkImageLayout> layout);
293     sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
294 #endif
295 
296     // Free and release and resources being held by the GrBackendTexture.
297     void cleanup();
298 
299     bool fIsValid;
300     int fWidth;         //<! width in pixels
301     int fHeight;        //<! height in pixels
302     GrMipMapped fMipMapped;
303     GrBackendApi fBackend;
304 
305     union {
306 #ifdef SK_GL
307         GrGLBackendTextureInfo fGLInfo;
308 #endif
309         GrVkBackendSurfaceInfo fVkInfo;
310         GrMockTextureInfo fMockInfo;
311     };
312 #ifdef SK_METAL
313     GrMtlTextureInfo fMtlInfo;
314 #endif
315 #ifdef SK_DAWN
316     GrDawnImageInfo  fDawnInfo;
317 #endif
318 };
319 
320 class SK_API GrBackendRenderTarget {
321 public:
322     // Creates an invalid backend texture.
GrBackendRenderTarget()323     GrBackendRenderTarget() : fIsValid(false) {}
324 
325     // The GrGLTextureInfo must have a valid fFormat.
326     GrBackendRenderTarget(int width,
327                           int height,
328                           int sampleCnt,
329                           int stencilBits,
330                           const GrGLFramebufferInfo& glInfo);
331 
332 #ifdef SK_DAWN
333     GrBackendRenderTarget(int width,
334                           int height,
335                           int sampleCnt,
336                           int stencilBits,
337                           const GrDawnImageInfo& dawnInfo);
338 #endif
339 
340     /** Deprecated, use version that does not take stencil bits. */
341     GrBackendRenderTarget(int width,
342                           int height,
343                           int sampleCnt,
344                           int stencilBits,
345                           const GrVkImageInfo& vkInfo);
346     GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
347 
348 #ifdef SK_METAL
349     GrBackendRenderTarget(int width,
350                           int height,
351                           int sampleCnt,
352                           const GrMtlTextureInfo& mtlInfo);
353 #endif
354 
355     GrBackendRenderTarget(int width,
356                           int height,
357                           int sampleCnt,
358                           int stencilBits,
359                           const GrMockRenderTargetInfo& mockInfo);
360 
361     ~GrBackendRenderTarget();
362 
363     GrBackendRenderTarget(const GrBackendRenderTarget& that);
364     GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
365 
width()366     int width() const { return fWidth; }
height()367     int height() const { return fHeight; }
sampleCnt()368     int sampleCnt() const { return fSampleCnt; }
stencilBits()369     int stencilBits() const { return fStencilBits; }
backend()370     GrBackendApi backend() const {return fBackend; }
371 
372     // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
373     // in pointer and returns true. Otherwise returns false if the backend API is not GL.
374     bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
375 
376 #ifdef SK_DAWN
377     // If the backend API is Dawn, copies a snapshot of the GrDawnImageInfo struct into the passed
378     // in pointer and returns true. Otherwise returns false if the backend API is not Dawn.
379     bool getDawnImageInfo(GrDawnImageInfo*) const;
380 #endif
381 
382     // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
383     // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
384     // state. Otherwise returns false if the backend API is not Vulkan.
385     bool getVkImageInfo(GrVkImageInfo*) const;
386 
387     // Anytime the client changes the VkImageLayout of the VkImage captured by this
388     // GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
389     void setVkImageLayout(VkImageLayout);
390 
391 #ifdef SK_METAL
392     // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
393     // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
394     bool getMtlTextureInfo(GrMtlTextureInfo*) const;
395 #endif
396 
397     // Get the GrBackendFormat for this render target (or an invalid format if this is not valid).
398     GrBackendFormat getBackendFormat() const;
399 
400     // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
401     // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
402     bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
403 
404     // Returns true if we are working with protected content.
405     bool isProtected() const;
406 
407     // Returns true if the backend texture has been initialized.
isValid()408     bool isValid() const { return fIsValid; }
409 
410 
411 #if GR_TEST_UTILS
412     static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
413 #endif
414 
415 private:
416     friend class GrVkGpu; // for getGrVkImageLayout
417     sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
418 
419     friend class GrVkRenderTarget;
420     GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
421                           sk_sp<GrVkImageLayout> layout);
422 
423     // Free and release and resources being held by the GrBackendTexture.
424     void cleanup();
425 
426     bool fIsValid;
427     int fWidth;         //<! width in pixels
428     int fHeight;        //<! height in pixels
429 
430     int fSampleCnt;
431     int fStencilBits;
432 
433     GrBackendApi fBackend;
434 
435     union {
436 #ifdef SK_GL
437         GrGLFramebufferInfo fGLInfo;
438 #endif
439         GrVkBackendSurfaceInfo fVkInfo;
440         GrMockRenderTargetInfo fMockInfo;
441     };
442 #ifdef SK_METAL
443     GrMtlTextureInfo fMtlInfo;
444 #endif
445 #ifdef SK_DAWN
446     GrDawnImageInfo  fDawnInfo;
447 #endif
448 };
449 
450 #endif
451 
452 #endif
453 
454