blob: dc1379543564544550f19a9f4ef568109154df74 [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;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500208 friend class SkSurface;
Greg Daniel108bb232018-07-03 16:18:29 -0400209 friend class GrAHardwareBufferImageGenerator;
Greg Daniele728f672018-01-17 10:52:04 -0500210 friend class GrBackendTextureImageGenerator;
Greg Danielf2336e42018-01-23 16:38:14 -0500211 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500212 friend class GrGpu;
213 friend class GrGLGpu;
214 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400215 friend class GrMtlGpu;
Greg Daniel057627f2018-03-14 15:51:58 -0400216 friend class PromiseImageHelper;
Robert Phillipsc5509952018-04-04 15:54:55 -0400217
Greg Daniel5254ccc2017-11-13 11:05:52 -0500218 GrPixelConfig config() const { return fConfig; }
219
Greg Daniel52e16d92018-04-10 09:34:07 -0400220#ifdef SK_VULKAN
221 // Requires friending of GrVkGpu (done above already)
222 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
223
224 friend class GrVkTexture;
225 GrBackendTexture(int width,
226 int height,
227 const GrVkImageInfo& vkInfo,
228 sk_sp<GrVkImageLayout> layout);
229#endif
230
231 // Free and release and resources being held by the GrBackendTexture.
232 void cleanup();
233
Greg Daniel9ca30652018-04-06 09:27:20 -0400234 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400235 int fWidth; //<! width in pixels
236 int fHeight; //<! height in pixels
237 GrPixelConfig fConfig;
Greg Daniel177e6952017-10-12 12:27:11 -0400238 GrMipMapped fMipMapped;
Greg Daniel94403452017-04-18 15:52:36 -0400239 GrBackend fBackend;
240
241 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000242 GrGLTextureInfo fGLInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000243#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400244 GrVkBackendSurfaceInfo fVkInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000245#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400246#ifdef SK_METAL
247 GrMtlTextureInfo fMtlInfo;
248#endif
Brian Salomon8fe24272017-07-07 12:56:11 -0400249 GrMockTextureInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400250 };
251};
252
Brian Salomonec045b42017-07-07 10:34:40 -0400253class SK_API GrBackendRenderTarget {
Greg Daniel94403452017-04-18 15:52:36 -0400254public:
Robert Phillips57e08282017-11-16 14:59:48 -0500255 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400256 GrBackendRenderTarget() : fIsValid(false) {}
Robert Phillips57e08282017-11-16 14:59:48 -0500257
Greg Danielfaa095e2017-12-19 13:15:02 -0500258 // The GrGLTextureInfo must have a valid fFormat.
259 GrBackendRenderTarget(int width,
260 int height,
261 int sampleCnt,
262 int stencilBits,
263 const GrGLFramebufferInfo& glInfo);
264
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000265#ifdef SK_VULKAN
Brian Salomonafdc6b12018-03-09 12:02:32 -0500266 /** Deprecated, use version that does not take stencil bits. */
Greg Daniel94403452017-04-18 15:52:36 -0400267 GrBackendRenderTarget(int width,
268 int height,
269 int sampleCnt,
270 int stencilBits,
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000271 const GrVkImageInfo& vkInfo);
Brian Salomonafdc6b12018-03-09 12:02:32 -0500272 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000273#endif
Greg Daniel94403452017-04-18 15:52:36 -0400274
Timothy Liang4e85e802018-06-28 16:37:18 -0400275#ifdef SK_METAL
276 GrBackendRenderTarget(int width,
277 int height,
278 int sampleCnt,
279 const GrMtlTextureInfo& mtlInfo);
280#endif
281
Brian Salomon0c51eea2018-03-09 17:02:09 -0500282 GrBackendRenderTarget(int width,
283 int height,
284 int sampleCnt,
285 int stencilBits,
286 const GrMockRenderTargetInfo& mockInfo);
287
Greg Daniel323fbcf2018-04-10 13:46:30 -0400288 ~GrBackendRenderTarget();
289
290 GrBackendRenderTarget(const GrBackendRenderTarget& that);
291 GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
292
Greg Daniel94403452017-04-18 15:52:36 -0400293 int width() const { return fWidth; }
294 int height() const { return fHeight; }
295 int sampleCnt() const { return fSampleCnt; }
296 int stencilBits() const { return fStencilBits; }
Greg Daniel94403452017-04-18 15:52:36 -0400297 GrBackend backend() const {return fBackend; }
298
Greg Daniel323fbcf2018-04-10 13:46:30 -0400299 // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
300 // in pointer and returns true. Otherwise returns false if the backend API is not GL.
301 bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400302
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000303#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400304 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
305 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
306 // state. Otherwise returns false if the backend API is not Vulkan.
307 bool getVkImageInfo(GrVkImageInfo*) const;
308
309 // Anytime the client changes the VkImageLayout of the VkImage captured by this
310 // GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
311 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000312#endif
313
Timothy Liang4e85e802018-06-28 16:37:18 -0400314#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
Greg Daniel323fbcf2018-04-10 13:46:30 -0400320 // 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;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500323
Robert Phillips57e08282017-11-16 14:59:48 -0500324 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400325 bool isValid() const { return fIsValid; }
Robert Phillips57e08282017-11-16 14:59:48 -0500326
Robert Phillips8caf85f2018-04-05 09:30:38 -0400327
328#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -0400329 // 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).
332 GrPixelConfig pixelConfig() const { return fConfig; }
333 void setPixelConfig(GrPixelConfig config) { fConfig = config; }
334
Robert Phillips8caf85f2018-04-05 09:30:38 -0400335 static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
336#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500337
Greg Daniel94403452017-04-18 15:52:36 -0400338private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500339 // Friending for access to the GrPixelConfig
340 friend class SkSurface;
Greg Danielfaa095e2017-12-19 13:15:02 -0500341 friend class SkSurface_Gpu;
342 friend class SkImage_Gpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500343 friend class GrGpu;
344 friend class GrGLGpu;
Greg Daniel2a303902018-02-20 10:25:54 -0500345 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500346 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400347 friend class GrMtlGpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500348 GrPixelConfig config() const { return fConfig; }
349
Greg Daniel323fbcf2018-04-10 13:46:30 -0400350#ifdef SK_VULKAN
351 // Requires friending of GrVkGpu (done above already)
352 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
353
354 friend class GrVkRenderTarget;
355 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
356 sk_sp<GrVkImageLayout> layout);
357#endif
358
359 // Free and release and resources being held by the GrBackendTexture.
360 void cleanup();
361
Greg Daniel9ca30652018-04-06 09:27:20 -0400362 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400363 int fWidth; //<! width in pixels
364 int fHeight; //<! height in pixels
365
366 int fSampleCnt;
367 int fStencilBits;
368 GrPixelConfig fConfig;
369
370 GrBackend fBackend;
371
372 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000373 GrGLFramebufferInfo fGLInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000374#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400375 GrVkBackendSurfaceInfo fVkInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000376#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400377#ifdef SK_METAL
378 GrMtlTextureInfo fMtlInfo;
379#endif
Brian Salomon0c51eea2018-03-09 17:02:09 -0500380 GrMockRenderTargetInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400381 };
382};
383
384#endif
385
Robert Phillips8caf85f2018-04-05 09:30:38 -0400386#endif
387