blob: 7f05bb50eea04b2097e10a42c9a4abb735dfe837 [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
Ethan Nicholas34aa0592018-07-03 16:38:32 +0000131#if GR_TEST_UTILS
132 // GrGLTextureInfo::fFormat is ignored
133 // Deprecated: Should use version that does not take a GrPixelConfig instead
134 GrBackendTexture(int width,
135 int height,
136 GrPixelConfig config,
137 const GrGLTextureInfo& glInfo);
138
139 // GrGLTextureInfo::fFormat is ignored
140 // Deprecated: Should use version that does not take a GrPixelConfig instead
141 GrBackendTexture(int width,
142 int height,
143 GrPixelConfig config,
144 GrMipMapped,
145 const GrGLTextureInfo& glInfo);
146#endif
147
Greg Daniele7d8da42017-12-04 11:23:19 -0500148 // The GrGLTextureInfo must have a valid fFormat.
149 GrBackendTexture(int width,
150 int height,
151 GrMipMapped,
152 const GrGLTextureInfo& glInfo);
153
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000154#ifdef SK_VULKAN
155 GrBackendTexture(int width,
156 int height,
157 const GrVkImageInfo& vkInfo);
158#endif
159
Timothy Liang4e85e802018-06-28 16:37:18 -0400160#ifdef SK_METAL
161 GrBackendTexture(int width,
162 int height,
163 GrMipMapped,
164 const GrMtlTextureInfo& mtlInfo);
165#endif
166
Brian Salomon8fe24272017-07-07 12:56:11 -0400167 GrBackendTexture(int width,
168 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400169 GrMipMapped,
170 const GrMockTextureInfo& mockInfo);
171
Greg Daniel52e16d92018-04-10 09:34:07 -0400172 GrBackendTexture(const GrBackendTexture& that);
173
174 ~GrBackendTexture();
175
176 GrBackendTexture& operator=(const GrBackendTexture& that);
177
Greg Daniel94403452017-04-18 15:52:36 -0400178 int width() const { return fWidth; }
179 int height() const { return fHeight; }
Greg Daniel177e6952017-10-12 12:27:11 -0400180 bool hasMipMaps() const { return GrMipMapped::kYes == fMipMapped; }
Greg Daniel94403452017-04-18 15:52:36 -0400181 GrBackend backend() const {return fBackend; }
182
Greg Daniel52e16d92018-04-10 09:34:07 -0400183 // If the backend API is GL, copies a snapshot of the GrGLTextureInfo struct into the passed in
184 // pointer and returns true. Otherwise returns false if the backend API is not GL.
185 bool getGLTextureInfo(GrGLTextureInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400186
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000187#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400188 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
Greg Daniel52e16d92018-04-10 09:34:07 -0400189 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
190 // state. Otherwise returns false if the backend API is not Vulkan.
191 bool getVkImageInfo(GrVkImageInfo*) const;
192
Greg Daniel323fbcf2018-04-10 13:46:30 -0400193 // Anytime the client changes the VkImageLayout of the VkImage captured by this
194 // GrBackendTexture, they must call this function to notify Skia of the changed layout.
Greg Daniel52e16d92018-04-10 09:34:07 -0400195 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000196#endif
197
Timothy Liang4e85e802018-06-28 16:37:18 -0400198#ifdef SK_METAL
199 // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
200 // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
201 bool getMtlTextureInfo(GrMtlTextureInfo*) const;
202#endif
203
Greg Daniel52e16d92018-04-10 09:34:07 -0400204 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
205 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
206 bool getMockTextureInfo(GrMockTextureInfo*) const;
Brian Salomon8fe24272017-07-07 12:56:11 -0400207
Eric Karl914a36b2017-10-12 12:44:50 -0700208 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400209 bool isValid() const { return fIsValid; }
Brian Salomon8fe24272017-07-07 12:56:11 -0400210
Robert Phillipsc5509952018-04-04 15:54:55 -0400211#if GR_TEST_UTILS
Ethan Nicholas34aa0592018-07-03 16:38:32 +0000212 GrPixelConfig testingOnly_getPixelConfig() const;
Robert Phillipsc5509952018-04-04 15:54:55 -0400213 static bool TestingOnly_Equals(const GrBackendTexture& , const GrBackendTexture&);
214#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500215
Eric Karl914a36b2017-10-12 12:44:50 -0700216private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500217 // Friending for access to the GrPixelConfig
Greg Danielfaa095e2017-12-19 13:15:02 -0500218 friend class SkImage;
Brian Salomon6a426c12018-03-15 12:16:02 -0400219 friend class SkImage_Gpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500220 friend class SkSurface;
Greg Daniele728f672018-01-17 10:52:04 -0500221 friend class GrBackendTextureImageGenerator;
Greg Danielf2336e42018-01-23 16:38:14 -0500222 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500223 friend class GrGpu;
224 friend class GrGLGpu;
225 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400226 friend class GrMtlGpu;
Greg Daniel057627f2018-03-14 15:51:58 -0400227 friend class PromiseImageHelper;
Robert Phillipsc5509952018-04-04 15:54:55 -0400228
Greg Daniel5254ccc2017-11-13 11:05:52 -0500229 GrPixelConfig config() const { return fConfig; }
230
Greg Daniel52e16d92018-04-10 09:34:07 -0400231#ifdef SK_VULKAN
232 // Requires friending of GrVkGpu (done above already)
233 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
234
235 friend class GrVkTexture;
236 GrBackendTexture(int width,
237 int height,
238 const GrVkImageInfo& vkInfo,
239 sk_sp<GrVkImageLayout> layout);
240#endif
241
242 // Free and release and resources being held by the GrBackendTexture.
243 void cleanup();
244
Greg Daniel9ca30652018-04-06 09:27:20 -0400245 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400246 int fWidth; //<! width in pixels
247 int fHeight; //<! height in pixels
248 GrPixelConfig fConfig;
Greg Daniel177e6952017-10-12 12:27:11 -0400249 GrMipMapped fMipMapped;
Greg Daniel94403452017-04-18 15:52:36 -0400250 GrBackend fBackend;
251
252 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000253 GrGLTextureInfo fGLInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000254#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400255 GrVkBackendSurfaceInfo fVkInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000256#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400257#ifdef SK_METAL
258 GrMtlTextureInfo fMtlInfo;
259#endif
Brian Salomon8fe24272017-07-07 12:56:11 -0400260 GrMockTextureInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400261 };
262};
263
Brian Salomonec045b42017-07-07 10:34:40 -0400264class SK_API GrBackendRenderTarget {
Greg Daniel94403452017-04-18 15:52:36 -0400265public:
Robert Phillips57e08282017-11-16 14:59:48 -0500266 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400267 GrBackendRenderTarget() : fIsValid(false) {}
Robert Phillips57e08282017-11-16 14:59:48 -0500268
Ethan Nicholas34aa0592018-07-03 16:38:32 +0000269#if GR_TEST_UTILS
270 // GrGLTextureInfo::fFormat is ignored
271 // Deprecated: Should use version that does not take a GrPixelConfig instead
272 GrBackendRenderTarget(int width,
273 int height,
274 int sampleCnt,
275 int stencilBits,
276 GrPixelConfig config,
277 const GrGLFramebufferInfo& glInfo);
278#endif
279
Greg Danielfaa095e2017-12-19 13:15:02 -0500280 // The GrGLTextureInfo must have a valid fFormat.
281 GrBackendRenderTarget(int width,
282 int height,
283 int sampleCnt,
284 int stencilBits,
285 const GrGLFramebufferInfo& glInfo);
286
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000287#ifdef SK_VULKAN
Brian Salomonafdc6b12018-03-09 12:02:32 -0500288 /** Deprecated, use version that does not take stencil bits. */
Greg Daniel94403452017-04-18 15:52:36 -0400289 GrBackendRenderTarget(int width,
290 int height,
291 int sampleCnt,
292 int stencilBits,
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000293 const GrVkImageInfo& vkInfo);
Brian Salomonafdc6b12018-03-09 12:02:32 -0500294 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000295#endif
Greg Daniel94403452017-04-18 15:52:36 -0400296
Timothy Liang4e85e802018-06-28 16:37:18 -0400297#ifdef SK_METAL
298 GrBackendRenderTarget(int width,
299 int height,
300 int sampleCnt,
301 const GrMtlTextureInfo& mtlInfo);
302#endif
303
Brian Salomon0c51eea2018-03-09 17:02:09 -0500304 GrBackendRenderTarget(int width,
305 int height,
306 int sampleCnt,
307 int stencilBits,
308 const GrMockRenderTargetInfo& mockInfo);
309
Greg Daniel323fbcf2018-04-10 13:46:30 -0400310 ~GrBackendRenderTarget();
311
312 GrBackendRenderTarget(const GrBackendRenderTarget& that);
313 GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
314
Greg Daniel94403452017-04-18 15:52:36 -0400315 int width() const { return fWidth; }
316 int height() const { return fHeight; }
317 int sampleCnt() const { return fSampleCnt; }
318 int stencilBits() const { return fStencilBits; }
Greg Daniel94403452017-04-18 15:52:36 -0400319 GrBackend backend() const {return fBackend; }
320
Greg Daniel323fbcf2018-04-10 13:46:30 -0400321 // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
322 // in pointer and returns true. Otherwise returns false if the backend API is not GL.
323 bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400324
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000325#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400326 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
327 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
328 // state. Otherwise returns false if the backend API is not Vulkan.
329 bool getVkImageInfo(GrVkImageInfo*) const;
330
331 // Anytime the client changes the VkImageLayout of the VkImage captured by this
332 // GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
333 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000334#endif
335
Timothy Liang4e85e802018-06-28 16:37:18 -0400336#ifdef SK_METAL
337 // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
338 // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
339 bool getMtlTextureInfo(GrMtlTextureInfo*) const;
340#endif
341
Greg Daniel323fbcf2018-04-10 13:46:30 -0400342 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
343 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
344 bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500345
Robert Phillips57e08282017-11-16 14:59:48 -0500346 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400347 bool isValid() const { return fIsValid; }
Robert Phillips57e08282017-11-16 14:59:48 -0500348
Robert Phillips8caf85f2018-04-05 09:30:38 -0400349
350#if GR_TEST_UTILS
Ethan Nicholas34aa0592018-07-03 16:38:32 +0000351 GrPixelConfig testingOnly_getPixelConfig() const;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400352 static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
353#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500354
Greg Daniel94403452017-04-18 15:52:36 -0400355private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500356 // Friending for access to the GrPixelConfig
357 friend class SkSurface;
Greg Danielfaa095e2017-12-19 13:15:02 -0500358 friend class SkSurface_Gpu;
359 friend class SkImage_Gpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500360 friend class GrGpu;
361 friend class GrGLGpu;
Greg Daniel2a303902018-02-20 10:25:54 -0500362 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500363 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400364 friend class GrMtlGpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500365 GrPixelConfig config() const { return fConfig; }
366
Greg Daniel323fbcf2018-04-10 13:46:30 -0400367#ifdef SK_VULKAN
368 // Requires friending of GrVkGpu (done above already)
369 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
370
371 friend class GrVkRenderTarget;
372 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
373 sk_sp<GrVkImageLayout> layout);
374#endif
375
376 // Free and release and resources being held by the GrBackendTexture.
377 void cleanup();
378
Greg Daniel9ca30652018-04-06 09:27:20 -0400379 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400380 int fWidth; //<! width in pixels
381 int fHeight; //<! height in pixels
382
383 int fSampleCnt;
384 int fStencilBits;
385 GrPixelConfig fConfig;
386
387 GrBackend fBackend;
388
389 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000390 GrGLFramebufferInfo fGLInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000391#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400392 GrVkBackendSurfaceInfo fVkInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000393#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400394#ifdef SK_METAL
395 GrMtlTextureInfo fMtlInfo;
396#endif
Brian Salomon0c51eea2018-03-09 17:02:09 -0500397 GrMockRenderTargetInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400398 };
399};
400
401#endif
402
Robert Phillips8caf85f2018-04-05 09:30:38 -0400403#endif
404