blob: 28543c3f2d208a34c0c7435f4bffd6cbf096ab57 [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"
Greg Danielbcf612b2017-05-01 13:50:58 +000014#include "vk/GrVkTypes.h"
Greg Daniel52e16d92018-04-10 09:34:07 -040015#include "../private/GrVkTypesPriv.h"
16
17class GrVkImageLayout;
Greg Daniel94403452017-04-18 15:52:36 -040018
Timothy Liang4e85e802018-06-28 16:37:18 -040019#ifdef SK_METAL
20#include "mtl/GrMtlTypes.h"
21#endif
22
Robert Phillips8caf85f2018-04-05 09:30:38 -040023#if !SK_SUPPORT_GPU
24
25// SkSurface and SkImage rely on a minimal version of these always being available
26class SK_API GrBackendTexture {
27public:
28 GrBackendTexture() {}
29
30 bool isValid() const { return false; }
31};
32
33class SK_API GrBackendRenderTarget {
34public:
35 GrBackendRenderTarget() {}
36
37 bool isValid() const { return false; }
38};
39#else
40
Robert Phillipsfc711a22018-02-13 17:03:00 -050041class SK_API GrBackendFormat {
42public:
43 // Creates an invalid backend format.
44 GrBackendFormat() : fValid(false) {}
45
46 static GrBackendFormat MakeGL(GrGLenum format, GrGLenum target) {
47 return GrBackendFormat(format, target);
48 }
49
Greg Daniela8d92112018-03-09 12:05:04 -050050 static GrBackendFormat MakeVk(VkFormat format) {
Greg Daniel7e000222018-12-03 10:08:21 -050051 return GrBackendFormat(format, GrVkYcbcrConversionInfo());
Robert Phillipsfc711a22018-02-13 17:03:00 -050052 }
Robert Phillipsfc711a22018-02-13 17:03:00 -050053
Greg Daniel7e000222018-12-03 10:08:21 -050054 // 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
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
Greg Daniel45723ac2018-11-30 10:12:43 -050069 bool operator==(const GrBackendFormat& that) const;
70 bool operator!=(const GrBackendFormat& that) const { return !(*this == that); }
71
Greg Daniel4065d452018-11-16 15:43:41 -050072 GrBackendApi backend() const { return fBackend; }
73 GrTextureType textureType() const { return fTextureType; }
Robert Phillipsfc711a22018-02-13 17:03:00 -050074
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
Robert Phillipsfc711a22018-02-13 17:03:00 -050080 // If the backend API is Vulkan, this returns a pointer to a VkFormat. Otherwise
81 // it returns nullptr
82 const VkFormat* getVkFormat() const;
Robert Phillipsfc711a22018-02-13 17:03:00 -050083
Greg Daniel7e000222018-12-03 10:08:21 -050084 const GrVkYcbcrConversionInfo* getVkYcbcrConversionInfo() const;
85
Timothy Liang4e85e802018-06-28 16:37:18 -040086#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
Robert Phillipsfc711a22018-02-13 17:03:00 -050092 // If the backend API is Mock, this returns a pointer to a GrPixelConfig. Otherwise
93 // it returns nullptr.
94 const GrPixelConfig* getMockFormat() const;
95
Greg Daniel4065d452018-11-16 15:43:41 -050096 // If possible, copies the GrBackendFormat and forces the texture type to be Texture2D
97 GrBackendFormat makeTexture2D() const;
98
Robert Phillipsfc711a22018-02-13 17:03:00 -050099 // Returns true if the backend format has been initialized.
100 bool isValid() const { return fValid; }
101
102private:
103 GrBackendFormat(GrGLenum format, GrGLenum target);
104
Greg Daniel7e000222018-12-03 10:08:21 -0500105 GrBackendFormat(const VkFormat vkFormat, const GrVkYcbcrConversionInfo&);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500106
Timothy Liang4e85e802018-06-28 16:37:18 -0400107#ifdef SK_METAL
108 GrBackendFormat(const GrMTLPixelFormat mtlFormat);
109#endif
110
Robert Phillipsfc711a22018-02-13 17:03:00 -0500111 GrBackendFormat(const GrPixelConfig config);
112
Greg Danielbdf12ad2018-10-12 09:31:11 -0400113 GrBackendApi fBackend;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500114 bool fValid;
115
116 union {
Greg Daniel4065d452018-11-16 15:43:41 -0500117 GrGLenum fGLFormat; // the sized, internal format of the GL resource
Greg Daniel7e000222018-12-03 10:08:21 -0500118 struct {
119 VkFormat fFormat;
120 GrVkYcbcrConversionInfo fYcbcrConversionInfo;
121 } fVk;
Timothy Liang4e85e802018-06-28 16:37:18 -0400122#ifdef SK_METAL
123 GrMTLPixelFormat fMtlFormat;
124#endif
125 GrPixelConfig fMockFormat;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500126 };
Greg Daniel4065d452018-11-16 15:43:41 -0500127 GrTextureType fTextureType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500128};
129
Brian Salomonec045b42017-07-07 10:34:40 -0400130class SK_API GrBackendTexture {
Greg Daniel94403452017-04-18 15:52:36 -0400131public:
Brian Salomon8fe24272017-07-07 12:56:11 -0400132 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400133 GrBackendTexture() : fIsValid(false) {}
Brian Salomon8fe24272017-07-07 12:56:11 -0400134
Greg Daniele7d8da42017-12-04 11:23:19 -0500135 // The GrGLTextureInfo must have a valid fFormat.
136 GrBackendTexture(int width,
137 int height,
138 GrMipMapped,
139 const GrGLTextureInfo& glInfo);
140
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000141 GrBackendTexture(int width,
142 int height,
143 const GrVkImageInfo& vkInfo);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000144
Timothy Liang4e85e802018-06-28 16:37:18 -0400145#ifdef SK_METAL
146 GrBackendTexture(int width,
147 int height,
148 GrMipMapped,
149 const GrMtlTextureInfo& mtlInfo);
150#endif
151
Brian Salomon8fe24272017-07-07 12:56:11 -0400152 GrBackendTexture(int width,
153 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400154 GrMipMapped,
155 const GrMockTextureInfo& mockInfo);
156
Greg Daniel52e16d92018-04-10 09:34:07 -0400157 GrBackendTexture(const GrBackendTexture& that);
158
159 ~GrBackendTexture();
160
161 GrBackendTexture& operator=(const GrBackendTexture& that);
162
Greg Daniel94403452017-04-18 15:52:36 -0400163 int width() const { return fWidth; }
164 int height() const { return fHeight; }
Greg Daniel177e6952017-10-12 12:27:11 -0400165 bool hasMipMaps() const { return GrMipMapped::kYes == fMipMapped; }
Greg Danielbdf12ad2018-10-12 09:31:11 -0400166 GrBackendApi backend() const {return fBackend; }
Greg Daniel94403452017-04-18 15:52:36 -0400167
Greg Daniel52e16d92018-04-10 09:34:07 -0400168 // 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;
Greg Danielc0f8e422017-06-13 13:47:53 -0400171
Greg Daniel323fbcf2018-04-10 13:46:30 -0400172 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
Greg Daniel52e16d92018-04-10 09:34:07 -0400173 // 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
Greg Daniel323fbcf2018-04-10 13:46:30 -0400177 // 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.
Greg Daniel52e16d92018-04-10 09:34:07 -0400179 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000180
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
Brian Salomonf391d0f2018-12-14 09:18:50 -0500187 // Get the GrBackendFormat for this texture (or an invalid format if this is not valid).
188 GrBackendFormat getBackendFormat() const;
189
Greg Daniel52e16d92018-04-10 09:34:07 -0400190 // 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;
Brian Salomon8fe24272017-07-07 12:56:11 -0400193
Eric Karl914a36b2017-10-12 12:44:50 -0700194 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400195 bool isValid() const { return fIsValid; }
Brian Salomon8fe24272017-07-07 12:56:11 -0400196
Robert Phillipsc5509952018-04-04 15:54:55 -0400197#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -0400198 // 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).
201 GrPixelConfig pixelConfig() const { return fConfig; }
202 void setPixelConfig(GrPixelConfig config) { fConfig = config; }
203
Robert Phillipsc5509952018-04-04 15:54:55 -0400204 static bool TestingOnly_Equals(const GrBackendTexture& , const GrBackendTexture&);
205#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500206
Eric Karl914a36b2017-10-12 12:44:50 -0700207private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500208 // Friending for access to the GrPixelConfig
Greg Danielfaa095e2017-12-19 13:15:02 -0500209 friend class SkImage;
Brian Salomon6a426c12018-03-15 12:16:02 -0400210 friend class SkImage_Gpu;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400211 friend class SkImage_GpuBase;
Jim Van Verthf49262d2018-10-02 12:07:20 -0400212 friend class SkImage_GpuYUVA;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400213 friend class SkPromiseImageHelper;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500214 friend class SkSurface;
Greg Daniel108bb232018-07-03 16:18:29 -0400215 friend class GrAHardwareBufferImageGenerator;
Greg Daniele728f672018-01-17 10:52:04 -0500216 friend class GrBackendTextureImageGenerator;
Greg Danielf2336e42018-01-23 16:38:14 -0500217 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500218 friend class GrGpu;
219 friend class GrGLGpu;
220 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400221 friend class GrMtlGpu;
Greg Daniel057627f2018-03-14 15:51:58 -0400222 friend class PromiseImageHelper;
Robert Phillipsc5509952018-04-04 15:54:55 -0400223
Greg Daniel5254ccc2017-11-13 11:05:52 -0500224 GrPixelConfig config() const { return fConfig; }
225
Greg Daniel52e16d92018-04-10 09:34:07 -0400226 // Requires friending of GrVkGpu (done above already)
227 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
228
229 friend class GrVkTexture;
Greg Danielb4d89562018-10-03 18:44:49 +0000230#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400231 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
Greg Daniel9ca30652018-04-06 09:27:20 -0400240 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400241 int fWidth; //<! width in pixels
242 int fHeight; //<! height in pixels
243 GrPixelConfig fConfig;
Greg Daniel177e6952017-10-12 12:27:11 -0400244 GrMipMapped fMipMapped;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400245 GrBackendApi fBackend;
Greg Daniel94403452017-04-18 15:52:36 -0400246
247 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000248 GrGLTextureInfo fGLInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400249 GrVkBackendSurfaceInfo fVkInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400250#ifdef SK_METAL
251 GrMtlTextureInfo fMtlInfo;
252#endif
Brian Salomon8fe24272017-07-07 12:56:11 -0400253 GrMockTextureInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400254 };
255};
256
Brian Salomonec045b42017-07-07 10:34:40 -0400257class SK_API GrBackendRenderTarget {
Greg Daniel94403452017-04-18 15:52:36 -0400258public:
Robert Phillips57e08282017-11-16 14:59:48 -0500259 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400260 GrBackendRenderTarget() : fIsValid(false) {}
Robert Phillips57e08282017-11-16 14:59:48 -0500261
Greg Danielfaa095e2017-12-19 13:15:02 -0500262 // 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
Brian Salomonafdc6b12018-03-09 12:02:32 -0500269 /** Deprecated, use version that does not take stencil bits. */
Greg Daniel94403452017-04-18 15:52:36 -0400270 GrBackendRenderTarget(int width,
271 int height,
272 int sampleCnt,
273 int stencilBits,
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000274 const GrVkImageInfo& vkInfo);
Brian Salomonafdc6b12018-03-09 12:02:32 -0500275 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
Greg Daniel94403452017-04-18 15:52:36 -0400276
Timothy Liang4e85e802018-06-28 16:37:18 -0400277#ifdef SK_METAL
278 GrBackendRenderTarget(int width,
279 int height,
280 int sampleCnt,
281 const GrMtlTextureInfo& mtlInfo);
282#endif
283
Brian Salomon0c51eea2018-03-09 17:02:09 -0500284 GrBackendRenderTarget(int width,
285 int height,
286 int sampleCnt,
287 int stencilBits,
288 const GrMockRenderTargetInfo& mockInfo);
289
Greg Daniel323fbcf2018-04-10 13:46:30 -0400290 ~GrBackendRenderTarget();
291
292 GrBackendRenderTarget(const GrBackendRenderTarget& that);
293 GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
294
Greg Daniel94403452017-04-18 15:52:36 -0400295 int width() const { return fWidth; }
296 int height() const { return fHeight; }
297 int sampleCnt() const { return fSampleCnt; }
298 int stencilBits() const { return fStencilBits; }
Greg Danielbdf12ad2018-10-12 09:31:11 -0400299 GrBackendApi backend() const {return fBackend; }
Greg Daniel94403452017-04-18 15:52:36 -0400300
Greg Daniel323fbcf2018-04-10 13:46:30 -0400301 // 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;
Greg Danielc0f8e422017-06-13 13:47:53 -0400304
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
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 // 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);
Greg Daniel323fbcf2018-04-10 13:46:30 -0400356
357 // Free and release and resources being held by the GrBackendTexture.
358 void cleanup();
359
Greg Daniel9ca30652018-04-06 09:27:20 -0400360 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400361 int fWidth; //<! width in pixels
362 int fHeight; //<! height in pixels
363
364 int fSampleCnt;
365 int fStencilBits;
366 GrPixelConfig fConfig;
367
Greg Danielbdf12ad2018-10-12 09:31:11 -0400368 GrBackendApi fBackend;
Greg Daniel94403452017-04-18 15:52:36 -0400369
370 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000371 GrGLFramebufferInfo fGLInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400372 GrVkBackendSurfaceInfo fVkInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400373#ifdef SK_METAL
374 GrMtlTextureInfo fMtlInfo;
375#endif
Brian Salomon0c51eea2018-03-09 17:02:09 -0500376 GrMockRenderTargetInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400377 };
378};
379
380#endif
381
Robert Phillips8caf85f2018-04-05 09:30:38 -0400382#endif
383