blob: 2e366d0fd7428d18ef3ab210d120d83f05a06ee3 [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) {
Robert Phillipsfc711a22018-02-13 17:03:00 -050051 return GrBackendFormat(format);
52 }
Robert Phillipsfc711a22018-02-13 17:03:00 -050053
Timothy Liang4e85e802018-06-28 16:37:18 -040054#ifdef SK_METAL
55 static GrBackendFormat MakeMtl(GrMTLPixelFormat format) {
56 return GrBackendFormat(format);
57 }
58#endif
59
Robert Phillipsfc711a22018-02-13 17:03:00 -050060 static GrBackendFormat MakeMock(GrPixelConfig config) {
61 return GrBackendFormat(config);
62 }
63
64 GrBackend backend() const {return fBackend; }
65
66 // If the backend API is GL, these return a pointer to the format and target. Otherwise
67 // it returns nullptr.
68 const GrGLenum* getGLFormat() const;
69 const GrGLenum* getGLTarget() const;
70
Robert Phillipsfc711a22018-02-13 17:03:00 -050071 // If the backend API is Vulkan, this returns a pointer to a VkFormat. Otherwise
72 // it returns nullptr
73 const VkFormat* getVkFormat() const;
Robert Phillipsfc711a22018-02-13 17:03:00 -050074
Timothy Liang4e85e802018-06-28 16:37:18 -040075#ifdef SK_METAL
76 // If the backend API is Metal, this returns a pointer to a GrMTLPixelFormat. Otherwise
77 // it returns nullptr
78 const GrMTLPixelFormat* getMtlFormat() const;
79#endif
80
Robert Phillipsfc711a22018-02-13 17:03:00 -050081 // If the backend API is Mock, this returns a pointer to a GrPixelConfig. Otherwise
82 // it returns nullptr.
83 const GrPixelConfig* getMockFormat() const;
84
85 // Returns true if the backend format has been initialized.
86 bool isValid() const { return fValid; }
87
88private:
89 GrBackendFormat(GrGLenum format, GrGLenum target);
90
Robert Phillipsfc711a22018-02-13 17:03:00 -050091 GrBackendFormat(const VkFormat vkFormat);
Robert Phillipsfc711a22018-02-13 17:03:00 -050092
Timothy Liang4e85e802018-06-28 16:37:18 -040093#ifdef SK_METAL
94 GrBackendFormat(const GrMTLPixelFormat mtlFormat);
95#endif
96
Robert Phillipsfc711a22018-02-13 17:03:00 -050097 GrBackendFormat(const GrPixelConfig config);
98
99 GrBackend fBackend;
100 bool fValid;
101
102 union {
103 struct {
104 GrGLenum fTarget; // GL_TEXTURE_2D, GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE
105 GrGLenum fFormat; // the sized, internal format of the GL resource
106 } fGL;
Timothy Liang4e85e802018-06-28 16:37:18 -0400107 VkFormat fVkFormat;
Timothy Liang4e85e802018-06-28 16:37:18 -0400108#ifdef SK_METAL
109 GrMTLPixelFormat fMtlFormat;
110#endif
111 GrPixelConfig fMockFormat;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500112 };
113};
114
Brian Salomonec045b42017-07-07 10:34:40 -0400115class SK_API GrBackendTexture {
Greg Daniel94403452017-04-18 15:52:36 -0400116public:
Brian Salomon8fe24272017-07-07 12:56:11 -0400117 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400118 GrBackendTexture() : fIsValid(false) {}
Brian Salomon8fe24272017-07-07 12:56:11 -0400119
Greg Daniele7d8da42017-12-04 11:23:19 -0500120 // The GrGLTextureInfo must have a valid fFormat.
121 GrBackendTexture(int width,
122 int height,
123 GrMipMapped,
124 const GrGLTextureInfo& glInfo);
125
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000126 GrBackendTexture(int width,
127 int height,
128 const GrVkImageInfo& vkInfo);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000129
Timothy Liang4e85e802018-06-28 16:37:18 -0400130#ifdef SK_METAL
131 GrBackendTexture(int width,
132 int height,
133 GrMipMapped,
134 const GrMtlTextureInfo& mtlInfo);
135#endif
136
Brian Salomon8fe24272017-07-07 12:56:11 -0400137 GrBackendTexture(int width,
138 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400139 GrMipMapped,
140 const GrMockTextureInfo& mockInfo);
141
Greg Daniel52e16d92018-04-10 09:34:07 -0400142 GrBackendTexture(const GrBackendTexture& that);
143
144 ~GrBackendTexture();
145
146 GrBackendTexture& operator=(const GrBackendTexture& that);
147
Greg Daniel94403452017-04-18 15:52:36 -0400148 int width() const { return fWidth; }
149 int height() const { return fHeight; }
Greg Daniel177e6952017-10-12 12:27:11 -0400150 bool hasMipMaps() const { return GrMipMapped::kYes == fMipMapped; }
Greg Daniel94403452017-04-18 15:52:36 -0400151 GrBackend backend() const {return fBackend; }
152
Greg Daniel52e16d92018-04-10 09:34:07 -0400153 // If the backend API is GL, copies a snapshot of the GrGLTextureInfo struct into the passed in
154 // pointer and returns true. Otherwise returns false if the backend API is not GL.
155 bool getGLTextureInfo(GrGLTextureInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400156
Greg Daniel323fbcf2018-04-10 13:46:30 -0400157 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
Greg Daniel52e16d92018-04-10 09:34:07 -0400158 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
159 // state. Otherwise returns false if the backend API is not Vulkan.
160 bool getVkImageInfo(GrVkImageInfo*) const;
161
Greg Daniel323fbcf2018-04-10 13:46:30 -0400162 // Anytime the client changes the VkImageLayout of the VkImage captured by this
163 // GrBackendTexture, they must call this function to notify Skia of the changed layout.
Greg Daniel52e16d92018-04-10 09:34:07 -0400164 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000165
Timothy Liang4e85e802018-06-28 16:37:18 -0400166#ifdef SK_METAL
167 // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
168 // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
169 bool getMtlTextureInfo(GrMtlTextureInfo*) const;
170#endif
171
Greg Daniel52e16d92018-04-10 09:34:07 -0400172 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
173 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
174 bool getMockTextureInfo(GrMockTextureInfo*) const;
Brian Salomon8fe24272017-07-07 12:56:11 -0400175
Eric Karl914a36b2017-10-12 12:44:50 -0700176 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400177 bool isValid() const { return fIsValid; }
Brian Salomon8fe24272017-07-07 12:56:11 -0400178
Robert Phillipsc5509952018-04-04 15:54:55 -0400179#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -0400180 // We can remove the pixelConfig getter and setter once we remove the GrPixelConfig from the
181 // GrBackendTexture and plumb the GrPixelconfig manually throughout our code (or remove all use
182 // of GrPixelConfig in general).
183 GrPixelConfig pixelConfig() const { return fConfig; }
184 void setPixelConfig(GrPixelConfig config) { fConfig = config; }
185
Robert Phillipsc5509952018-04-04 15:54:55 -0400186 static bool TestingOnly_Equals(const GrBackendTexture& , const GrBackendTexture&);
187#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500188
Eric Karl914a36b2017-10-12 12:44:50 -0700189private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500190 // Friending for access to the GrPixelConfig
Greg Danielfaa095e2017-12-19 13:15:02 -0500191 friend class SkImage;
Brian Salomon6a426c12018-03-15 12:16:02 -0400192 friend class SkImage_Gpu;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400193 friend class SkImage_GpuBase;
Jim Van Verthf49262d2018-10-02 12:07:20 -0400194 friend class SkImage_GpuYUVA;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400195 friend class SkPromiseImageHelper;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500196 friend class SkSurface;
Greg Daniel108bb232018-07-03 16:18:29 -0400197 friend class GrAHardwareBufferImageGenerator;
Greg Daniele728f672018-01-17 10:52:04 -0500198 friend class GrBackendTextureImageGenerator;
Greg Danielf2336e42018-01-23 16:38:14 -0500199 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500200 friend class GrGpu;
201 friend class GrGLGpu;
202 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400203 friend class GrMtlGpu;
Greg Daniel057627f2018-03-14 15:51:58 -0400204 friend class PromiseImageHelper;
Robert Phillipsc5509952018-04-04 15:54:55 -0400205
Greg Daniel5254ccc2017-11-13 11:05:52 -0500206 GrPixelConfig config() const { return fConfig; }
207
Greg Daniel52e16d92018-04-10 09:34:07 -0400208 // Requires friending of GrVkGpu (done above already)
209 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
210
211 friend class GrVkTexture;
Greg Danielb4d89562018-10-03 18:44:49 +0000212#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400213 GrBackendTexture(int width,
214 int height,
215 const GrVkImageInfo& vkInfo,
216 sk_sp<GrVkImageLayout> layout);
217#endif
218
219 // Free and release and resources being held by the GrBackendTexture.
220 void cleanup();
221
Greg Daniel9ca30652018-04-06 09:27:20 -0400222 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400223 int fWidth; //<! width in pixels
224 int fHeight; //<! height in pixels
225 GrPixelConfig fConfig;
Greg Daniel177e6952017-10-12 12:27:11 -0400226 GrMipMapped fMipMapped;
Greg Daniel94403452017-04-18 15:52:36 -0400227 GrBackend fBackend;
228
229 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000230 GrGLTextureInfo fGLInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400231 GrVkBackendSurfaceInfo fVkInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400232#ifdef SK_METAL
233 GrMtlTextureInfo fMtlInfo;
234#endif
Brian Salomon8fe24272017-07-07 12:56:11 -0400235 GrMockTextureInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400236 };
237};
238
Brian Salomonec045b42017-07-07 10:34:40 -0400239class SK_API GrBackendRenderTarget {
Greg Daniel94403452017-04-18 15:52:36 -0400240public:
Robert Phillips57e08282017-11-16 14:59:48 -0500241 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400242 GrBackendRenderTarget() : fIsValid(false) {}
Robert Phillips57e08282017-11-16 14:59:48 -0500243
Greg Danielfaa095e2017-12-19 13:15:02 -0500244 // The GrGLTextureInfo must have a valid fFormat.
245 GrBackendRenderTarget(int width,
246 int height,
247 int sampleCnt,
248 int stencilBits,
249 const GrGLFramebufferInfo& glInfo);
250
Brian Salomonafdc6b12018-03-09 12:02:32 -0500251 /** Deprecated, use version that does not take stencil bits. */
Greg Daniel94403452017-04-18 15:52:36 -0400252 GrBackendRenderTarget(int width,
253 int height,
254 int sampleCnt,
255 int stencilBits,
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000256 const GrVkImageInfo& vkInfo);
Brian Salomonafdc6b12018-03-09 12:02:32 -0500257 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
Greg Daniel94403452017-04-18 15:52:36 -0400258
Timothy Liang4e85e802018-06-28 16:37:18 -0400259#ifdef SK_METAL
260 GrBackendRenderTarget(int width,
261 int height,
262 int sampleCnt,
263 const GrMtlTextureInfo& mtlInfo);
264#endif
265
Brian Salomon0c51eea2018-03-09 17:02:09 -0500266 GrBackendRenderTarget(int width,
267 int height,
268 int sampleCnt,
269 int stencilBits,
270 const GrMockRenderTargetInfo& mockInfo);
271
Greg Daniel323fbcf2018-04-10 13:46:30 -0400272 ~GrBackendRenderTarget();
273
274 GrBackendRenderTarget(const GrBackendRenderTarget& that);
275 GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
276
Greg Daniel94403452017-04-18 15:52:36 -0400277 int width() const { return fWidth; }
278 int height() const { return fHeight; }
279 int sampleCnt() const { return fSampleCnt; }
280 int stencilBits() const { return fStencilBits; }
Greg Daniel94403452017-04-18 15:52:36 -0400281 GrBackend backend() const {return fBackend; }
282
Greg Daniel323fbcf2018-04-10 13:46:30 -0400283 // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
284 // in pointer and returns true. Otherwise returns false if the backend API is not GL.
285 bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400286
Greg Daniel323fbcf2018-04-10 13:46:30 -0400287 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
288 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
289 // state. Otherwise returns false if the backend API is not Vulkan.
290 bool getVkImageInfo(GrVkImageInfo*) const;
291
292 // Anytime the client changes the VkImageLayout of the VkImage captured by this
293 // GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
294 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000295
Timothy Liang4e85e802018-06-28 16:37:18 -0400296#ifdef SK_METAL
297 // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
298 // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
299 bool getMtlTextureInfo(GrMtlTextureInfo*) const;
300#endif
301
Greg Daniel323fbcf2018-04-10 13:46:30 -0400302 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
303 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
304 bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500305
Robert Phillips57e08282017-11-16 14:59:48 -0500306 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400307 bool isValid() const { return fIsValid; }
Robert Phillips57e08282017-11-16 14:59:48 -0500308
Robert Phillips8caf85f2018-04-05 09:30:38 -0400309
310#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -0400311 // We can remove the pixelConfig getter and setter once we remove the pixel config from the
312 // GrBackendRenderTarget and plumb the pixel config manually throughout our code (or remove all
313 // use of GrPixelConfig in general).
314 GrPixelConfig pixelConfig() const { return fConfig; }
315 void setPixelConfig(GrPixelConfig config) { fConfig = config; }
316
Robert Phillips8caf85f2018-04-05 09:30:38 -0400317 static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
318#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500319
Greg Daniel94403452017-04-18 15:52:36 -0400320private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500321 // Friending for access to the GrPixelConfig
322 friend class SkSurface;
Greg Danielfaa095e2017-12-19 13:15:02 -0500323 friend class SkSurface_Gpu;
324 friend class SkImage_Gpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500325 friend class GrGpu;
326 friend class GrGLGpu;
Greg Daniel2a303902018-02-20 10:25:54 -0500327 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500328 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400329 friend class GrMtlGpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500330 GrPixelConfig config() const { return fConfig; }
331
Greg Daniel323fbcf2018-04-10 13:46:30 -0400332 // Requires friending of GrVkGpu (done above already)
333 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
334
335 friend class GrVkRenderTarget;
336 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
337 sk_sp<GrVkImageLayout> layout);
Greg Daniel323fbcf2018-04-10 13:46:30 -0400338
339 // Free and release and resources being held by the GrBackendTexture.
340 void cleanup();
341
Greg Daniel9ca30652018-04-06 09:27:20 -0400342 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400343 int fWidth; //<! width in pixels
344 int fHeight; //<! height in pixels
345
346 int fSampleCnt;
347 int fStencilBits;
348 GrPixelConfig fConfig;
349
350 GrBackend fBackend;
351
352 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000353 GrGLFramebufferInfo fGLInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400354 GrVkBackendSurfaceInfo fVkInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400355#ifdef SK_METAL
356 GrMtlTextureInfo fMtlInfo;
357#endif
Brian Salomon0c51eea2018-03-09 17:02:09 -0500358 GrMockRenderTargetInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400359 };
360};
361
362#endif
363
Robert Phillips8caf85f2018-04-05 09:30:38 -0400364#endif
365