blob: 1de6fb04146f7821840a73dc16e1d01d882b6dbd [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 Danielf4bf9732018-12-03 14:34:28 +000051 return GrBackendFormat(format);
Robert Phillipsfc711a22018-02-13 17:03:00 -050052 }
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
Greg Daniel4065d452018-11-16 15:43:41 -050064 GrBackendApi backend() const { return fBackend; }
65 GrTextureType textureType() const { return fTextureType; }
Robert Phillipsfc711a22018-02-13 17:03:00 -050066
67 // If the backend API is GL, these return a pointer to the format and target. Otherwise
68 // it returns nullptr.
69 const GrGLenum* getGLFormat() const;
70 const GrGLenum* getGLTarget() const;
71
Robert Phillipsfc711a22018-02-13 17:03:00 -050072 // If the backend API is Vulkan, this returns a pointer to a VkFormat. Otherwise
73 // it returns nullptr
74 const VkFormat* getVkFormat() const;
Robert Phillipsfc711a22018-02-13 17:03:00 -050075
Timothy Liang4e85e802018-06-28 16:37:18 -040076#ifdef SK_METAL
77 // If the backend API is Metal, this returns a pointer to a GrMTLPixelFormat. Otherwise
78 // it returns nullptr
79 const GrMTLPixelFormat* getMtlFormat() const;
80#endif
81
Robert Phillipsfc711a22018-02-13 17:03:00 -050082 // If the backend API is Mock, this returns a pointer to a GrPixelConfig. Otherwise
83 // it returns nullptr.
84 const GrPixelConfig* getMockFormat() const;
85
Greg Daniel4065d452018-11-16 15:43:41 -050086 // If possible, copies the GrBackendFormat and forces the texture type to be Texture2D
87 GrBackendFormat makeTexture2D() const;
88
Robert Phillipsfc711a22018-02-13 17:03:00 -050089 // Returns true if the backend format has been initialized.
90 bool isValid() const { return fValid; }
91
92private:
93 GrBackendFormat(GrGLenum format, GrGLenum target);
94
Greg Danielf4bf9732018-12-03 14:34:28 +000095 GrBackendFormat(const VkFormat vkFormat);
Robert Phillipsfc711a22018-02-13 17:03:00 -050096
Timothy Liang4e85e802018-06-28 16:37:18 -040097#ifdef SK_METAL
98 GrBackendFormat(const GrMTLPixelFormat mtlFormat);
99#endif
100
Robert Phillipsfc711a22018-02-13 17:03:00 -0500101 GrBackendFormat(const GrPixelConfig config);
102
Greg Danielbdf12ad2018-10-12 09:31:11 -0400103 GrBackendApi fBackend;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500104 bool fValid;
105
106 union {
Greg Daniel4065d452018-11-16 15:43:41 -0500107 GrGLenum fGLFormat; // the sized, internal format of the GL resource
Greg Danielf4bf9732018-12-03 14:34:28 +0000108 VkFormat fVkFormat;
Timothy Liang4e85e802018-06-28 16:37:18 -0400109#ifdef SK_METAL
110 GrMTLPixelFormat fMtlFormat;
111#endif
112 GrPixelConfig fMockFormat;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500113 };
Greg Daniel4065d452018-11-16 15:43:41 -0500114 GrTextureType fTextureType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500115};
116
Brian Salomonec045b42017-07-07 10:34:40 -0400117class SK_API GrBackendTexture {
Greg Daniel94403452017-04-18 15:52:36 -0400118public:
Brian Salomon8fe24272017-07-07 12:56:11 -0400119 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400120 GrBackendTexture() : fIsValid(false) {}
Brian Salomon8fe24272017-07-07 12:56:11 -0400121
Greg Daniele7d8da42017-12-04 11:23:19 -0500122 // The GrGLTextureInfo must have a valid fFormat.
123 GrBackendTexture(int width,
124 int height,
125 GrMipMapped,
126 const GrGLTextureInfo& glInfo);
127
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000128 GrBackendTexture(int width,
129 int height,
130 const GrVkImageInfo& vkInfo);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000131
Timothy Liang4e85e802018-06-28 16:37:18 -0400132#ifdef SK_METAL
133 GrBackendTexture(int width,
134 int height,
135 GrMipMapped,
136 const GrMtlTextureInfo& mtlInfo);
137#endif
138
Brian Salomon8fe24272017-07-07 12:56:11 -0400139 GrBackendTexture(int width,
140 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400141 GrMipMapped,
142 const GrMockTextureInfo& mockInfo);
143
Greg Daniel52e16d92018-04-10 09:34:07 -0400144 GrBackendTexture(const GrBackendTexture& that);
145
146 ~GrBackendTexture();
147
148 GrBackendTexture& operator=(const GrBackendTexture& that);
149
Greg Daniel94403452017-04-18 15:52:36 -0400150 int width() const { return fWidth; }
151 int height() const { return fHeight; }
Greg Daniel177e6952017-10-12 12:27:11 -0400152 bool hasMipMaps() const { return GrMipMapped::kYes == fMipMapped; }
Greg Danielbdf12ad2018-10-12 09:31:11 -0400153 GrBackendApi backend() const {return fBackend; }
Greg Daniel94403452017-04-18 15:52:36 -0400154
Greg Daniel52e16d92018-04-10 09:34:07 -0400155 // If the backend API is GL, copies a snapshot of the GrGLTextureInfo struct into the passed in
156 // pointer and returns true. Otherwise returns false if the backend API is not GL.
157 bool getGLTextureInfo(GrGLTextureInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400158
Greg Daniel323fbcf2018-04-10 13:46:30 -0400159 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
Greg Daniel52e16d92018-04-10 09:34:07 -0400160 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
161 // state. Otherwise returns false if the backend API is not Vulkan.
162 bool getVkImageInfo(GrVkImageInfo*) const;
163
Greg Daniel323fbcf2018-04-10 13:46:30 -0400164 // Anytime the client changes the VkImageLayout of the VkImage captured by this
165 // GrBackendTexture, they must call this function to notify Skia of the changed layout.
Greg Daniel52e16d92018-04-10 09:34:07 -0400166 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000167
Timothy Liang4e85e802018-06-28 16:37:18 -0400168#ifdef SK_METAL
169 // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
170 // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
171 bool getMtlTextureInfo(GrMtlTextureInfo*) const;
172#endif
173
Greg Daniel52e16d92018-04-10 09:34:07 -0400174 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
175 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
176 bool getMockTextureInfo(GrMockTextureInfo*) const;
Brian Salomon8fe24272017-07-07 12:56:11 -0400177
Eric Karl914a36b2017-10-12 12:44:50 -0700178 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400179 bool isValid() const { return fIsValid; }
Brian Salomon8fe24272017-07-07 12:56:11 -0400180
Robert Phillipsc5509952018-04-04 15:54:55 -0400181#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -0400182 // We can remove the pixelConfig getter and setter once we remove the GrPixelConfig from the
183 // GrBackendTexture and plumb the GrPixelconfig manually throughout our code (or remove all use
184 // of GrPixelConfig in general).
185 GrPixelConfig pixelConfig() const { return fConfig; }
186 void setPixelConfig(GrPixelConfig config) { fConfig = config; }
187
Robert Phillipsc5509952018-04-04 15:54:55 -0400188 static bool TestingOnly_Equals(const GrBackendTexture& , const GrBackendTexture&);
189#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500190
Eric Karl914a36b2017-10-12 12:44:50 -0700191private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500192 // Friending for access to the GrPixelConfig
Greg Danielfaa095e2017-12-19 13:15:02 -0500193 friend class SkImage;
Brian Salomon6a426c12018-03-15 12:16:02 -0400194 friend class SkImage_Gpu;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400195 friend class SkImage_GpuBase;
Jim Van Verthf49262d2018-10-02 12:07:20 -0400196 friend class SkImage_GpuYUVA;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400197 friend class SkPromiseImageHelper;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500198 friend class SkSurface;
Greg Daniel108bb232018-07-03 16:18:29 -0400199 friend class GrAHardwareBufferImageGenerator;
Greg Daniele728f672018-01-17 10:52:04 -0500200 friend class GrBackendTextureImageGenerator;
Greg Danielf2336e42018-01-23 16:38:14 -0500201 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500202 friend class GrGpu;
203 friend class GrGLGpu;
204 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400205 friend class GrMtlGpu;
Greg Daniel057627f2018-03-14 15:51:58 -0400206 friend class PromiseImageHelper;
Robert Phillipsc5509952018-04-04 15:54:55 -0400207
Greg Daniel5254ccc2017-11-13 11:05:52 -0500208 GrPixelConfig config() const { return fConfig; }
209
Greg Daniel52e16d92018-04-10 09:34:07 -0400210 // Requires friending of GrVkGpu (done above already)
211 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
212
213 friend class GrVkTexture;
Greg Danielb4d89562018-10-03 18:44:49 +0000214#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400215 GrBackendTexture(int width,
216 int height,
217 const GrVkImageInfo& vkInfo,
218 sk_sp<GrVkImageLayout> layout);
219#endif
220
221 // Free and release and resources being held by the GrBackendTexture.
222 void cleanup();
223
Greg Daniel9ca30652018-04-06 09:27:20 -0400224 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400225 int fWidth; //<! width in pixels
226 int fHeight; //<! height in pixels
227 GrPixelConfig fConfig;
Greg Daniel177e6952017-10-12 12:27:11 -0400228 GrMipMapped fMipMapped;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400229 GrBackendApi fBackend;
Greg Daniel94403452017-04-18 15:52:36 -0400230
231 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000232 GrGLTextureInfo fGLInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400233 GrVkBackendSurfaceInfo fVkInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400234#ifdef SK_METAL
235 GrMtlTextureInfo fMtlInfo;
236#endif
Brian Salomon8fe24272017-07-07 12:56:11 -0400237 GrMockTextureInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400238 };
239};
240
Brian Salomonec045b42017-07-07 10:34:40 -0400241class SK_API GrBackendRenderTarget {
Greg Daniel94403452017-04-18 15:52:36 -0400242public:
Robert Phillips57e08282017-11-16 14:59:48 -0500243 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400244 GrBackendRenderTarget() : fIsValid(false) {}
Robert Phillips57e08282017-11-16 14:59:48 -0500245
Greg Danielfaa095e2017-12-19 13:15:02 -0500246 // The GrGLTextureInfo must have a valid fFormat.
247 GrBackendRenderTarget(int width,
248 int height,
249 int sampleCnt,
250 int stencilBits,
251 const GrGLFramebufferInfo& glInfo);
252
Brian Salomonafdc6b12018-03-09 12:02:32 -0500253 /** Deprecated, use version that does not take stencil bits. */
Greg Daniel94403452017-04-18 15:52:36 -0400254 GrBackendRenderTarget(int width,
255 int height,
256 int sampleCnt,
257 int stencilBits,
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000258 const GrVkImageInfo& vkInfo);
Brian Salomonafdc6b12018-03-09 12:02:32 -0500259 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
Greg Daniel94403452017-04-18 15:52:36 -0400260
Timothy Liang4e85e802018-06-28 16:37:18 -0400261#ifdef SK_METAL
262 GrBackendRenderTarget(int width,
263 int height,
264 int sampleCnt,
265 const GrMtlTextureInfo& mtlInfo);
266#endif
267
Brian Salomon0c51eea2018-03-09 17:02:09 -0500268 GrBackendRenderTarget(int width,
269 int height,
270 int sampleCnt,
271 int stencilBits,
272 const GrMockRenderTargetInfo& mockInfo);
273
Greg Daniel323fbcf2018-04-10 13:46:30 -0400274 ~GrBackendRenderTarget();
275
276 GrBackendRenderTarget(const GrBackendRenderTarget& that);
277 GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
278
Greg Daniel94403452017-04-18 15:52:36 -0400279 int width() const { return fWidth; }
280 int height() const { return fHeight; }
281 int sampleCnt() const { return fSampleCnt; }
282 int stencilBits() const { return fStencilBits; }
Greg Danielbdf12ad2018-10-12 09:31:11 -0400283 GrBackendApi backend() const {return fBackend; }
Greg Daniel94403452017-04-18 15:52:36 -0400284
Greg Daniel323fbcf2018-04-10 13:46:30 -0400285 // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
286 // in pointer and returns true. Otherwise returns false if the backend API is not GL.
287 bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400288
Greg Daniel323fbcf2018-04-10 13:46:30 -0400289 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
290 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
291 // state. Otherwise returns false if the backend API is not Vulkan.
292 bool getVkImageInfo(GrVkImageInfo*) const;
293
294 // Anytime the client changes the VkImageLayout of the VkImage captured by this
295 // GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
296 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000297
Timothy Liang4e85e802018-06-28 16:37:18 -0400298#ifdef SK_METAL
299 // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
300 // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
301 bool getMtlTextureInfo(GrMtlTextureInfo*) const;
302#endif
303
Greg Daniel323fbcf2018-04-10 13:46:30 -0400304 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
305 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
306 bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500307
Robert Phillips57e08282017-11-16 14:59:48 -0500308 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400309 bool isValid() const { return fIsValid; }
Robert Phillips57e08282017-11-16 14:59:48 -0500310
Robert Phillips8caf85f2018-04-05 09:30:38 -0400311
312#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -0400313 // We can remove the pixelConfig getter and setter once we remove the pixel config from the
314 // GrBackendRenderTarget and plumb the pixel config manually throughout our code (or remove all
315 // use of GrPixelConfig in general).
316 GrPixelConfig pixelConfig() const { return fConfig; }
317 void setPixelConfig(GrPixelConfig config) { fConfig = config; }
318
Robert Phillips8caf85f2018-04-05 09:30:38 -0400319 static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
320#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500321
Greg Daniel94403452017-04-18 15:52:36 -0400322private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500323 // Friending for access to the GrPixelConfig
324 friend class SkSurface;
Greg Danielfaa095e2017-12-19 13:15:02 -0500325 friend class SkSurface_Gpu;
326 friend class SkImage_Gpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500327 friend class GrGpu;
328 friend class GrGLGpu;
Greg Daniel2a303902018-02-20 10:25:54 -0500329 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500330 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400331 friend class GrMtlGpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500332 GrPixelConfig config() const { return fConfig; }
333
Greg Daniel323fbcf2018-04-10 13:46:30 -0400334 // Requires friending of GrVkGpu (done above already)
335 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
336
337 friend class GrVkRenderTarget;
338 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
339 sk_sp<GrVkImageLayout> layout);
Greg Daniel323fbcf2018-04-10 13:46:30 -0400340
341 // Free and release and resources being held by the GrBackendTexture.
342 void cleanup();
343
Greg Daniel9ca30652018-04-06 09:27:20 -0400344 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400345 int fWidth; //<! width in pixels
346 int fHeight; //<! height in pixels
347
348 int fSampleCnt;
349 int fStencilBits;
350 GrPixelConfig fConfig;
351
Greg Danielbdf12ad2018-10-12 09:31:11 -0400352 GrBackendApi fBackend;
Greg Daniel94403452017-04-18 15:52:36 -0400353
354 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000355 GrGLFramebufferInfo fGLInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400356 GrVkBackendSurfaceInfo fVkInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400357#ifdef SK_METAL
358 GrMtlTextureInfo fMtlInfo;
359#endif
Brian Salomon0c51eea2018-03-09 17:02:09 -0500360 GrMockRenderTargetInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400361 };
362};
363
364#endif
365
Robert Phillips8caf85f2018-04-05 09:30:38 -0400366#endif
367