blob: 8caebb8f1e232d1b659a5982186871b9adcdc63d [file] [log] [blame]
Greg Daniel94403452017-04-18 15:52:36 -04001/*
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"
Greg Danielbcf612b2017-05-01 13:50:58 +000012#include "gl/GrGLTypes.h"
Brian Salomon8fe24272017-07-07 12:56:11 -040013#include "mock/GrMockTypes.h"
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +000014
15#ifdef SK_VULKAN
Greg Danielbcf612b2017-05-01 13:50:58 +000016#include "vk/GrVkTypes.h"
Greg Daniel52e16d92018-04-10 09:34:07 -040017#include "../private/GrVkTypesPriv.h"
18
19class GrVkImageLayout;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +000020#endif
Greg Daniel94403452017-04-18 15:52:36 -040021
Timothy Liang4e85e802018-06-28 16:37:18 -040022#ifdef SK_METAL
23#include "mtl/GrMtlTypes.h"
24#endif
25
Robert Phillips8caf85f2018-04-05 09:30:38 -040026#if !SK_SUPPORT_GPU
27
28// SkSurface and SkImage rely on a minimal version of these always being available
29class SK_API GrBackendTexture {
30public:
31 GrBackendTexture() {}
32
33 bool isValid() const { return false; }
34};
35
36class SK_API GrBackendRenderTarget {
37public:
38 GrBackendRenderTarget() {}
39
40 bool isValid() const { return false; }
41};
42#else
43
Robert Phillipsfc711a22018-02-13 17:03:00 -050044class SK_API GrBackendFormat {
45public:
46 // Creates an invalid backend format.
47 GrBackendFormat() : fValid(false) {}
48
49 static GrBackendFormat MakeGL(GrGLenum format, GrGLenum target) {
50 return GrBackendFormat(format, target);
51 }
52
53#ifdef SK_VULKAN
Greg Daniela8d92112018-03-09 12:05:04 -050054 static GrBackendFormat MakeVk(VkFormat format) {
Robert Phillipsfc711a22018-02-13 17:03:00 -050055 return GrBackendFormat(format);
56 }
57#endif
58
Timothy Liang4e85e802018-06-28 16:37:18 -040059#ifdef SK_METAL
60 static GrBackendFormat MakeMtl(GrMTLPixelFormat format) {
61 return GrBackendFormat(format);
62 }
63#endif
64
Robert Phillipsfc711a22018-02-13 17:03:00 -050065 static GrBackendFormat MakeMock(GrPixelConfig config) {
66 return GrBackendFormat(config);
67 }
68
69 GrBackend backend() const {return fBackend; }
70
71 // If the backend API is GL, these return a pointer to the format and target. Otherwise
72 // it returns nullptr.
73 const GrGLenum* getGLFormat() const;
74 const GrGLenum* getGLTarget() const;
75
76#ifdef SK_VULKAN
77 // If the backend API is Vulkan, this returns a pointer to a VkFormat. Otherwise
78 // it returns nullptr
79 const VkFormat* getVkFormat() const;
80#endif
81
Timothy Liang4e85e802018-06-28 16:37:18 -040082#ifdef SK_METAL
83 // If the backend API is Metal, this returns a pointer to a GrMTLPixelFormat. Otherwise
84 // it returns nullptr
85 const GrMTLPixelFormat* getMtlFormat() const;
86#endif
87
Robert Phillipsfc711a22018-02-13 17:03:00 -050088 // If the backend API is Mock, this returns a pointer to a GrPixelConfig. Otherwise
89 // it returns nullptr.
90 const GrPixelConfig* getMockFormat() const;
91
92 // Returns true if the backend format has been initialized.
93 bool isValid() const { return fValid; }
94
95private:
96 GrBackendFormat(GrGLenum format, GrGLenum target);
97
98#ifdef SK_VULKAN
99 GrBackendFormat(const VkFormat vkFormat);
100#endif
101
Timothy Liang4e85e802018-06-28 16:37:18 -0400102#ifdef SK_METAL
103 GrBackendFormat(const GrMTLPixelFormat mtlFormat);
104#endif
105
Robert Phillipsfc711a22018-02-13 17:03:00 -0500106 GrBackendFormat(const GrPixelConfig config);
107
108 GrBackend fBackend;
109 bool fValid;
110
111 union {
112 struct {
113 GrGLenum fTarget; // GL_TEXTURE_2D, GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE
114 GrGLenum fFormat; // the sized, internal format of the GL resource
115 } fGL;
116#ifdef SK_VULKAN
Timothy Liang4e85e802018-06-28 16:37:18 -0400117 VkFormat fVkFormat;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500118#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400119#ifdef SK_METAL
120 GrMTLPixelFormat fMtlFormat;
121#endif
122 GrPixelConfig fMockFormat;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500123 };
124};
125
Brian Salomonec045b42017-07-07 10:34:40 -0400126class SK_API GrBackendTexture {
Greg Daniel94403452017-04-18 15:52:36 -0400127public:
Brian Salomon8fe24272017-07-07 12:56:11 -0400128 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400129 GrBackendTexture() : fIsValid(false) {}
Brian Salomon8fe24272017-07-07 12:56:11 -0400130
Greg Daniele7d8da42017-12-04 11:23:19 -0500131 // The GrGLTextureInfo must have a valid fFormat.
132 GrBackendTexture(int width,
133 int height,
134 GrMipMapped,
135 const GrGLTextureInfo& glInfo);
136
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000137#ifdef SK_VULKAN
138 GrBackendTexture(int width,
139 int height,
140 const GrVkImageInfo& vkInfo);
141#endif
142
Timothy Liang4e85e802018-06-28 16:37:18 -0400143#ifdef SK_METAL
144 GrBackendTexture(int width,
145 int height,
146 GrMipMapped,
147 const GrMtlTextureInfo& mtlInfo);
148#endif
149
Brian Salomon8fe24272017-07-07 12:56:11 -0400150 GrBackendTexture(int width,
151 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400152 GrMipMapped,
153 const GrMockTextureInfo& mockInfo);
154
Greg Daniel52e16d92018-04-10 09:34:07 -0400155 GrBackendTexture(const GrBackendTexture& that);
156
157 ~GrBackendTexture();
158
159 GrBackendTexture& operator=(const GrBackendTexture& that);
160
Greg Daniel94403452017-04-18 15:52:36 -0400161 int width() const { return fWidth; }
162 int height() const { return fHeight; }
Greg Daniel177e6952017-10-12 12:27:11 -0400163 bool hasMipMaps() const { return GrMipMapped::kYes == fMipMapped; }
Greg Daniel94403452017-04-18 15:52:36 -0400164 GrBackend backend() const {return fBackend; }
165
Greg Daniel52e16d92018-04-10 09:34:07 -0400166 // If the backend API is GL, copies a snapshot of the GrGLTextureInfo struct into the passed in
167 // pointer and returns true. Otherwise returns false if the backend API is not GL.
168 bool getGLTextureInfo(GrGLTextureInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400169
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000170#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400171 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
Greg Daniel52e16d92018-04-10 09:34:07 -0400172 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
173 // state. Otherwise returns false if the backend API is not Vulkan.
174 bool getVkImageInfo(GrVkImageInfo*) const;
175
Greg Daniel323fbcf2018-04-10 13:46:30 -0400176 // Anytime the client changes the VkImageLayout of the VkImage captured by this
177 // GrBackendTexture, they must call this function to notify Skia of the changed layout.
Greg Daniel52e16d92018-04-10 09:34:07 -0400178 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000179#endif
180
Timothy Liang4e85e802018-06-28 16:37:18 -0400181#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
Greg Daniel52e16d92018-04-10 09:34:07 -0400187 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
188 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
189 bool getMockTextureInfo(GrMockTextureInfo*) const;
Brian Salomon8fe24272017-07-07 12:56:11 -0400190
Eric Karl914a36b2017-10-12 12:44:50 -0700191 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400192 bool isValid() const { return fIsValid; }
Brian Salomon8fe24272017-07-07 12:56:11 -0400193
Robert Phillipsc5509952018-04-04 15:54:55 -0400194#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -0400195 // We can remove the pixelConfig getter and setter once we remove the GrPixelConfig from the
196 // GrBackendTexture and plumb the GrPixelconfig manually throughout our code (or remove all use
197 // of GrPixelConfig in general).
198 GrPixelConfig pixelConfig() const { return fConfig; }
199 void setPixelConfig(GrPixelConfig config) { fConfig = config; }
200
Robert Phillipsc5509952018-04-04 15:54:55 -0400201 static bool TestingOnly_Equals(const GrBackendTexture& , const GrBackendTexture&);
202#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500203
Eric Karl914a36b2017-10-12 12:44:50 -0700204private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500205 // Friending for access to the GrPixelConfig
Greg Danielfaa095e2017-12-19 13:15:02 -0500206 friend class SkImage;
Brian Salomon6a426c12018-03-15 12:16:02 -0400207 friend class SkImage_Gpu;
Jim Van Verthf49262d2018-10-02 12:07:20 -0400208 friend class SkImage_GpuYUVA;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500209 friend class SkSurface;
Greg Daniel108bb232018-07-03 16:18:29 -0400210 friend class GrAHardwareBufferImageGenerator;
Greg Daniele728f672018-01-17 10:52:04 -0500211 friend class GrBackendTextureImageGenerator;
Greg Danielf2336e42018-01-23 16:38:14 -0500212 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500213 friend class GrGpu;
214 friend class GrGLGpu;
215 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400216 friend class GrMtlGpu;
Greg Daniel057627f2018-03-14 15:51:58 -0400217 friend class PromiseImageHelper;
Robert Phillipsc5509952018-04-04 15:54:55 -0400218
Greg Daniel5254ccc2017-11-13 11:05:52 -0500219 GrPixelConfig config() const { return fConfig; }
220
Greg Daniel52e16d92018-04-10 09:34:07 -0400221#ifdef SK_VULKAN
222 // Requires friending of GrVkGpu (done above already)
223 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
224
225 friend class GrVkTexture;
226 GrBackendTexture(int width,
227 int height,
228 const GrVkImageInfo& vkInfo,
229 sk_sp<GrVkImageLayout> layout);
230#endif
231
232 // Free and release and resources being held by the GrBackendTexture.
233 void cleanup();
234
Greg Daniel9ca30652018-04-06 09:27:20 -0400235 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400236 int fWidth; //<! width in pixels
237 int fHeight; //<! height in pixels
238 GrPixelConfig fConfig;
Greg Daniel177e6952017-10-12 12:27:11 -0400239 GrMipMapped fMipMapped;
Greg Daniel94403452017-04-18 15:52:36 -0400240 GrBackend fBackend;
241
242 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000243 GrGLTextureInfo fGLInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000244#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400245 GrVkBackendSurfaceInfo fVkInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000246#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400247#ifdef SK_METAL
248 GrMtlTextureInfo fMtlInfo;
249#endif
Brian Salomon8fe24272017-07-07 12:56:11 -0400250 GrMockTextureInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400251 };
252};
253
Brian Salomonec045b42017-07-07 10:34:40 -0400254class SK_API GrBackendRenderTarget {
Greg Daniel94403452017-04-18 15:52:36 -0400255public:
Robert Phillips57e08282017-11-16 14:59:48 -0500256 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400257 GrBackendRenderTarget() : fIsValid(false) {}
Robert Phillips57e08282017-11-16 14:59:48 -0500258
Greg Danielfaa095e2017-12-19 13:15:02 -0500259 // The GrGLTextureInfo must have a valid fFormat.
260 GrBackendRenderTarget(int width,
261 int height,
262 int sampleCnt,
263 int stencilBits,
264 const GrGLFramebufferInfo& glInfo);
265
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000266#ifdef SK_VULKAN
Brian Salomonafdc6b12018-03-09 12:02:32 -0500267 /** Deprecated, use version that does not take stencil bits. */
Greg Daniel94403452017-04-18 15:52:36 -0400268 GrBackendRenderTarget(int width,
269 int height,
270 int sampleCnt,
271 int stencilBits,
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000272 const GrVkImageInfo& vkInfo);
Brian Salomonafdc6b12018-03-09 12:02:32 -0500273 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000274#endif
Greg Daniel94403452017-04-18 15:52:36 -0400275
Timothy Liang4e85e802018-06-28 16:37:18 -0400276#ifdef SK_METAL
277 GrBackendRenderTarget(int width,
278 int height,
279 int sampleCnt,
280 const GrMtlTextureInfo& mtlInfo);
281#endif
282
Brian Salomon0c51eea2018-03-09 17:02:09 -0500283 GrBackendRenderTarget(int width,
284 int height,
285 int sampleCnt,
286 int stencilBits,
287 const GrMockRenderTargetInfo& mockInfo);
288
Greg Daniel323fbcf2018-04-10 13:46:30 -0400289 ~GrBackendRenderTarget();
290
291 GrBackendRenderTarget(const GrBackendRenderTarget& that);
292 GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
293
Greg Daniel94403452017-04-18 15:52:36 -0400294 int width() const { return fWidth; }
295 int height() const { return fHeight; }
296 int sampleCnt() const { return fSampleCnt; }
297 int stencilBits() const { return fStencilBits; }
Greg Daniel94403452017-04-18 15:52:36 -0400298 GrBackend backend() const {return fBackend; }
299
Greg Daniel323fbcf2018-04-10 13:46:30 -0400300 // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
301 // in pointer and returns true. Otherwise returns false if the backend API is not GL.
302 bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400303
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000304#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400305 // 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);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000313#endif
314
Timothy Liang4e85e802018-06-28 16:37:18 -0400315#ifdef SK_METAL
316 // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
317 // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
318 bool getMtlTextureInfo(GrMtlTextureInfo*) const;
319#endif
320
Greg Daniel323fbcf2018-04-10 13:46:30 -0400321 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
322 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
323 bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500324
Robert Phillips57e08282017-11-16 14:59:48 -0500325 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400326 bool isValid() const { return fIsValid; }
Robert Phillips57e08282017-11-16 14:59:48 -0500327
Robert Phillips8caf85f2018-04-05 09:30:38 -0400328
329#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -0400330 // We can remove the pixelConfig getter and setter once we remove the pixel config from the
331 // GrBackendRenderTarget and plumb the pixel config manually throughout our code (or remove all
332 // use of GrPixelConfig in general).
333 GrPixelConfig pixelConfig() const { return fConfig; }
334 void setPixelConfig(GrPixelConfig config) { fConfig = config; }
335
Robert Phillips8caf85f2018-04-05 09:30:38 -0400336 static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
337#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500338
Greg Daniel94403452017-04-18 15:52:36 -0400339private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500340 // Friending for access to the GrPixelConfig
341 friend class SkSurface;
Greg Danielfaa095e2017-12-19 13:15:02 -0500342 friend class SkSurface_Gpu;
343 friend class SkImage_Gpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500344 friend class GrGpu;
345 friend class GrGLGpu;
Greg Daniel2a303902018-02-20 10:25:54 -0500346 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500347 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400348 friend class GrMtlGpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500349 GrPixelConfig config() const { return fConfig; }
350
Greg Daniel323fbcf2018-04-10 13:46:30 -0400351#ifdef SK_VULKAN
352 // Requires friending of GrVkGpu (done above already)
353 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
354
355 friend class GrVkRenderTarget;
356 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
357 sk_sp<GrVkImageLayout> layout);
358#endif
359
360 // Free and release and resources being held by the GrBackendTexture.
361 void cleanup();
362
Greg Daniel9ca30652018-04-06 09:27:20 -0400363 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400364 int fWidth; //<! width in pixels
365 int fHeight; //<! height in pixels
366
367 int fSampleCnt;
368 int fStencilBits;
369 GrPixelConfig fConfig;
370
371 GrBackend fBackend;
372
373 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000374 GrGLFramebufferInfo fGLInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000375#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400376 GrVkBackendSurfaceInfo fVkInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000377#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400378#ifdef SK_METAL
379 GrMtlTextureInfo fMtlInfo;
380#endif
Brian Salomon0c51eea2018-03-09 17:02:09 -0500381 GrMockRenderTargetInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400382 };
383};
384
385#endif
386
Robert Phillips8caf85f2018-04-05 09:30:38 -0400387#endif
388