• 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 "GrTypes.h"
12 #include "gl/GrGLTypes.h"
13 #include "mock/GrMockTypes.h"
14 #include "vk/GrVkTypes.h"
15 #include "../private/GrVkTypesPriv.h"
16 
17 class GrVkImageLayout;
18 
19 #ifdef SK_METAL
20 #include "mtl/GrMtlTypes.h"
21 #endif
22 
23 #if !SK_SUPPORT_GPU
24 
25 // SkSurface and SkImage rely on a minimal version of these always being available
26 class SK_API GrBackendTexture {
27 public:
GrBackendTexture()28     GrBackendTexture() {}
29 
isValid()30     bool isValid() const { return false; }
31 };
32 
33 class SK_API GrBackendRenderTarget {
34 public:
GrBackendRenderTarget()35     GrBackendRenderTarget() {}
36 
isValid()37     bool isValid() const { return false; }
38 };
39 #else
40 
41 class SK_API GrBackendFormat {
42 public:
43     // Creates an invalid backend format.
GrBackendFormat()44     GrBackendFormat() : fValid(false) {}
45 
MakeGL(GrGLenum format,GrGLenum target)46     static GrBackendFormat MakeGL(GrGLenum format, GrGLenum target) {
47         return GrBackendFormat(format, target);
48     }
49 
MakeVk(VkFormat format)50     static GrBackendFormat MakeVk(VkFormat format) {
51         return GrBackendFormat(format, GrVkYcbcrConversionInfo());
52     }
53 
54     // This is used for external textures and the VkFormat is assumed to be VK_FORMAT_UNDEFINED.
55     // This call is only supported on Android since the GrVkYcbcrConvesionInfo contains an android
56     // external format.
57     static GrBackendFormat MakeVk(const GrVkYcbcrConversionInfo& ycbcrInfo);
58 
59 #ifdef SK_METAL
MakeMtl(GrMTLPixelFormat format)60     static GrBackendFormat MakeMtl(GrMTLPixelFormat format) {
61         return GrBackendFormat(format);
62     }
63 #endif
64 
MakeMock(GrPixelConfig config)65     static GrBackendFormat MakeMock(GrPixelConfig config) {
66         return GrBackendFormat(config);
67     }
68 
69     bool operator==(const GrBackendFormat& that) const;
70     bool operator!=(const GrBackendFormat& that) const { return !(*this == that); }
71 
backend()72     GrBackendApi backend() const { return fBackend; }
textureType()73     GrTextureType textureType() const { return fTextureType; }
74 
75     // If the backend API is GL, these return a pointer to the format and target. Otherwise
76     // it returns nullptr.
77     const GrGLenum* getGLFormat() const;
78     const GrGLenum* getGLTarget() const;
79 
80     // If the backend API is Vulkan, this returns a pointer to a VkFormat. Otherwise
81     // it returns nullptr
82     const VkFormat* getVkFormat() const;
83 
84     const GrVkYcbcrConversionInfo* getVkYcbcrConversionInfo() const;
85 
86 #ifdef SK_METAL
87     // If the backend API is Metal, this returns a pointer to a GrMTLPixelFormat. Otherwise
88     // it returns nullptr
89     const GrMTLPixelFormat* getMtlFormat() const;
90 #endif
91 
92     // If the backend API is Mock, this returns a pointer to a GrPixelConfig. Otherwise
93     // it returns nullptr.
94     const GrPixelConfig* getMockFormat() const;
95 
96     // If possible, copies the GrBackendFormat and forces the texture type to be Texture2D
97     GrBackendFormat makeTexture2D() const;
98 
99     // Returns true if the backend format has been initialized.
isValid()100     bool isValid() const { return fValid; }
101 
102 private:
103     GrBackendFormat(GrGLenum format, GrGLenum target);
104 
105     GrBackendFormat(const VkFormat vkFormat, const GrVkYcbcrConversionInfo&);
106 
107 #ifdef SK_METAL
108     GrBackendFormat(const GrMTLPixelFormat mtlFormat);
109 #endif
110 
111     GrBackendFormat(const GrPixelConfig config);
112 
113     GrBackendApi fBackend;
114     bool      fValid;
115 
116     union {
117         GrGLenum         fGLFormat; // the sized, internal format of the GL resource
118         struct {
119             VkFormat                 fFormat;
120             GrVkYcbcrConversionInfo  fYcbcrConversionInfo;
121         } fVk;
122 #ifdef SK_METAL
123         GrMTLPixelFormat fMtlFormat;
124 #endif
125         GrPixelConfig    fMockFormat;
126     };
127     GrTextureType fTextureType;
128 };
129 
130 class SK_API GrBackendTexture {
131 public:
132     // Creates an invalid backend texture.
GrBackendTexture()133     GrBackendTexture() : fIsValid(false) {}
134 
135     // The GrGLTextureInfo must have a valid fFormat.
136     GrBackendTexture(int width,
137                      int height,
138                      GrMipMapped,
139                      const GrGLTextureInfo& glInfo);
140 
141     GrBackendTexture(int width,
142                      int height,
143                      const GrVkImageInfo& vkInfo);
144 
145 #ifdef SK_METAL
146     GrBackendTexture(int width,
147                      int height,
148                      GrMipMapped,
149                      const GrMtlTextureInfo& mtlInfo);
150 #endif
151 
152     GrBackendTexture(int width,
153                      int height,
154                      GrMipMapped,
155                      const GrMockTextureInfo& mockInfo);
156 
157     GrBackendTexture(const GrBackendTexture& that);
158 
159     ~GrBackendTexture();
160 
161     GrBackendTexture& operator=(const GrBackendTexture& that);
162 
width()163     int width() const { return fWidth; }
height()164     int height() const { return fHeight; }
hasMipMaps()165     bool hasMipMaps() const { return GrMipMapped::kYes == fMipMapped; }
backend()166     GrBackendApi backend() const {return fBackend; }
167 
168     // If the backend API is GL, copies a snapshot of the GrGLTextureInfo struct into the passed in
169     // pointer and returns true. Otherwise returns false if the backend API is not GL.
170     bool getGLTextureInfo(GrGLTextureInfo*) const;
171 
172     // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
173     // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
174     // state. Otherwise returns false if the backend API is not Vulkan.
175     bool getVkImageInfo(GrVkImageInfo*) const;
176 
177     // Anytime the client changes the VkImageLayout of the VkImage captured by this
178     // GrBackendTexture, they must call this function to notify Skia of the changed layout.
179     void setVkImageLayout(VkImageLayout);
180 
181 #ifdef SK_METAL
182     // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
183     // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
184     bool getMtlTextureInfo(GrMtlTextureInfo*) const;
185 #endif
186 
187     // Get the GrBackendFormat for this texture (or an invalid format if this is not valid).
188     GrBackendFormat getBackendFormat() const;
189 
190     // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
191     // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
192     bool getMockTextureInfo(GrMockTextureInfo*) const;
193 
194     // Returns true if the backend texture has been initialized.
isValid()195     bool isValid() const { return fIsValid; }
196 
197 #if GR_TEST_UTILS
198     // We can remove the pixelConfig getter and setter once we remove the GrPixelConfig from the
199     // GrBackendTexture and plumb the GrPixelconfig manually throughout our code (or remove all use
200     // of GrPixelConfig in general).
pixelConfig()201     GrPixelConfig pixelConfig() const { return fConfig; }
setPixelConfig(GrPixelConfig config)202     void setPixelConfig(GrPixelConfig config) { fConfig = config; }
203 
204     static bool TestingOnly_Equals(const GrBackendTexture& , const GrBackendTexture&);
205 #endif
206 
207 private:
208     // Friending for access to the GrPixelConfig
209     friend class SkImage;
210     friend class SkImage_Gpu;
211     friend class SkImage_GpuBase;
212     friend class SkImage_GpuYUVA;
213     friend class SkPromiseImageHelper;
214     friend class SkSurface;
215     friend class GrAHardwareBufferImageGenerator;
216     friend class GrBackendTextureImageGenerator;
217     friend class GrProxyProvider;
218     friend class GrGpu;
219     friend class GrGLGpu;
220     friend class GrVkGpu;
221     friend class GrMtlGpu;
222     friend class PromiseImageHelper;
223 
config()224     GrPixelConfig config() const { return fConfig; }
225 
226    // Requires friending of GrVkGpu (done above already)
227    sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
228 
229    friend class GrVkTexture;
230 #ifdef SK_VULKAN
231    GrBackendTexture(int width,
232                     int height,
233                     const GrVkImageInfo& vkInfo,
234                     sk_sp<GrVkImageLayout> layout);
235 #endif
236 
237     // Free and release and resources being held by the GrBackendTexture.
238     void cleanup();
239 
240     bool fIsValid;
241     int fWidth;         //<! width in pixels
242     int fHeight;        //<! height in pixels
243     GrPixelConfig fConfig;
244     GrMipMapped fMipMapped;
245     GrBackendApi fBackend;
246 
247     union {
248         GrGLTextureInfo fGLInfo;
249         GrVkBackendSurfaceInfo fVkInfo;
250 #ifdef SK_METAL
251         GrMtlTextureInfo fMtlInfo;
252 #endif
253         GrMockTextureInfo fMockInfo;
254     };
255 };
256 
257 class SK_API GrBackendRenderTarget {
258 public:
259     // Creates an invalid backend texture.
GrBackendRenderTarget()260     GrBackendRenderTarget() : fIsValid(false) {}
261 
262     // The GrGLTextureInfo must have a valid fFormat.
263     GrBackendRenderTarget(int width,
264                           int height,
265                           int sampleCnt,
266                           int stencilBits,
267                           const GrGLFramebufferInfo& glInfo);
268 
269     /** Deprecated, use version that does not take stencil bits. */
270     GrBackendRenderTarget(int width,
271                           int height,
272                           int sampleCnt,
273                           int stencilBits,
274                           const GrVkImageInfo& vkInfo);
275     GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
276 
277 #ifdef SK_METAL
278     GrBackendRenderTarget(int width,
279                           int height,
280                           int sampleCnt,
281                           const GrMtlTextureInfo& mtlInfo);
282 #endif
283 
284     GrBackendRenderTarget(int width,
285                           int height,
286                           int sampleCnt,
287                           int stencilBits,
288                           const GrMockRenderTargetInfo& mockInfo);
289 
290     ~GrBackendRenderTarget();
291 
292     GrBackendRenderTarget(const GrBackendRenderTarget& that);
293     GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
294 
width()295     int width() const { return fWidth; }
height()296     int height() const { return fHeight; }
sampleCnt()297     int sampleCnt() const { return fSampleCnt; }
stencilBits()298     int stencilBits() const { return fStencilBits; }
backend()299     GrBackendApi backend() const {return fBackend; }
300 
301     // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
302     // in pointer and returns true. Otherwise returns false if the backend API is not GL.
303     bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
304 
305     // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
306     // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
307     // state. Otherwise returns false if the backend API is not Vulkan.
308     bool getVkImageInfo(GrVkImageInfo*) const;
309 
310     // Anytime the client changes the VkImageLayout of the VkImage captured by this
311     // GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
312     void setVkImageLayout(VkImageLayout);
313 
314 #ifdef SK_METAL
315     // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
316     // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
317     bool getMtlTextureInfo(GrMtlTextureInfo*) const;
318 #endif
319 
320     // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
321     // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
322     bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
323 
324     // Returns true if the backend texture has been initialized.
isValid()325     bool isValid() const { return fIsValid; }
326 
327 
328 #if GR_TEST_UTILS
329     // We can remove the pixelConfig getter and setter once we remove the pixel config from the
330     // GrBackendRenderTarget and plumb the pixel config manually throughout our code (or remove all
331     // use of GrPixelConfig in general).
pixelConfig()332     GrPixelConfig pixelConfig() const { return fConfig; }
setPixelConfig(GrPixelConfig config)333     void setPixelConfig(GrPixelConfig config) { fConfig = config; }
334 
335     static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
336 #endif
337 
338 private:
339     // Friending for access to the GrPixelConfig
340     friend class SkSurface;
341     friend class SkSurface_Gpu;
342     friend class SkImage_Gpu;
343     friend class GrGpu;
344     friend class GrGLGpu;
345     friend class GrProxyProvider;
346     friend class GrVkGpu;
347     friend class GrMtlGpu;
config()348     GrPixelConfig config() const { return fConfig; }
349 
350    // Requires friending of GrVkGpu (done above already)
351    sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
352 
353    friend class GrVkRenderTarget;
354    GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
355                          sk_sp<GrVkImageLayout> layout);
356 
357     // Free and release and resources being held by the GrBackendTexture.
358     void cleanup();
359 
360     bool fIsValid;
361     int fWidth;         //<! width in pixels
362     int fHeight;        //<! height in pixels
363 
364     int fSampleCnt;
365     int fStencilBits;
366     GrPixelConfig fConfig;
367 
368     GrBackendApi fBackend;
369 
370     union {
371         GrGLFramebufferInfo fGLInfo;
372         GrVkBackendSurfaceInfo fVkInfo;
373 #ifdef SK_METAL
374         GrMtlTextureInfo fMtlInfo;
375 #endif
376         GrMockRenderTargetInfo fMockInfo;
377     };
378 };
379 
380 #endif
381 
382 #endif
383 
384