blob: e50f2225e8b3cd87dc8ea7f89afbb4c29bf0b9c8 [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 Daniel4065d452018-11-16 15:43:41 -050069 GrBackendApi backend() const { return fBackend; }
70 GrTextureType textureType() const { return fTextureType; }
Robert Phillipsfc711a22018-02-13 17:03:00 -050071
72 // If the backend API is GL, these return a pointer to the format and target. Otherwise
73 // it returns nullptr.
74 const GrGLenum* getGLFormat() const;
75 const GrGLenum* getGLTarget() const;
76
Robert Phillipsfc711a22018-02-13 17:03:00 -050077 // If the backend API is Vulkan, this returns a pointer to a VkFormat. Otherwise
78 // it returns nullptr
79 const VkFormat* getVkFormat() const;
Robert Phillipsfc711a22018-02-13 17:03:00 -050080
Greg Daniel7e000222018-12-03 10:08:21 -050081 const GrVkYcbcrConversionInfo* getVkYcbcrConversionInfo() const;
82
Timothy Liang4e85e802018-06-28 16:37:18 -040083#ifdef SK_METAL
84 // If the backend API is Metal, this returns a pointer to a GrMTLPixelFormat. Otherwise
85 // it returns nullptr
86 const GrMTLPixelFormat* getMtlFormat() const;
87#endif
88
Robert Phillipsfc711a22018-02-13 17:03:00 -050089 // If the backend API is Mock, this returns a pointer to a GrPixelConfig. Otherwise
90 // it returns nullptr.
91 const GrPixelConfig* getMockFormat() const;
92
Greg Daniel4065d452018-11-16 15:43:41 -050093 // If possible, copies the GrBackendFormat and forces the texture type to be Texture2D
94 GrBackendFormat makeTexture2D() const;
95
Robert Phillipsfc711a22018-02-13 17:03:00 -050096 // Returns true if the backend format has been initialized.
97 bool isValid() const { return fValid; }
98
99private:
100 GrBackendFormat(GrGLenum format, GrGLenum target);
101
Greg Daniel7e000222018-12-03 10:08:21 -0500102 GrBackendFormat(const VkFormat vkFormat, const GrVkYcbcrConversionInfo&);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500103
Timothy Liang4e85e802018-06-28 16:37:18 -0400104#ifdef SK_METAL
105 GrBackendFormat(const GrMTLPixelFormat mtlFormat);
106#endif
107
Robert Phillipsfc711a22018-02-13 17:03:00 -0500108 GrBackendFormat(const GrPixelConfig config);
109
Greg Danielbdf12ad2018-10-12 09:31:11 -0400110 GrBackendApi fBackend;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500111 bool fValid;
112
113 union {
Greg Daniel4065d452018-11-16 15:43:41 -0500114 GrGLenum fGLFormat; // the sized, internal format of the GL resource
Greg Daniel7e000222018-12-03 10:08:21 -0500115 struct {
116 VkFormat fFormat;
117 GrVkYcbcrConversionInfo fYcbcrConversionInfo;
118 } fVk;
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 };
Greg Daniel4065d452018-11-16 15:43:41 -0500124 GrTextureType fTextureType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500125};
126
Brian Salomonec045b42017-07-07 10:34:40 -0400127class SK_API GrBackendTexture {
Greg Daniel94403452017-04-18 15:52:36 -0400128public:
Brian Salomon8fe24272017-07-07 12:56:11 -0400129 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400130 GrBackendTexture() : fIsValid(false) {}
Brian Salomon8fe24272017-07-07 12:56:11 -0400131
Greg Daniele7d8da42017-12-04 11:23:19 -0500132 // The GrGLTextureInfo must have a valid fFormat.
133 GrBackendTexture(int width,
134 int height,
135 GrMipMapped,
136 const GrGLTextureInfo& glInfo);
137
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000138 GrBackendTexture(int width,
139 int height,
140 const GrVkImageInfo& vkInfo);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000141
Timothy Liang4e85e802018-06-28 16:37:18 -0400142#ifdef SK_METAL
143 GrBackendTexture(int width,
144 int height,
145 GrMipMapped,
146 const GrMtlTextureInfo& mtlInfo);
147#endif
148
Brian Salomon8fe24272017-07-07 12:56:11 -0400149 GrBackendTexture(int width,
150 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400151 GrMipMapped,
152 const GrMockTextureInfo& mockInfo);
153
Greg Daniel52e16d92018-04-10 09:34:07 -0400154 GrBackendTexture(const GrBackendTexture& that);
155
156 ~GrBackendTexture();
157
158 GrBackendTexture& operator=(const GrBackendTexture& that);
159
Greg Daniel94403452017-04-18 15:52:36 -0400160 int width() const { return fWidth; }
161 int height() const { return fHeight; }
Greg Daniel177e6952017-10-12 12:27:11 -0400162 bool hasMipMaps() const { return GrMipMapped::kYes == fMipMapped; }
Greg Danielbdf12ad2018-10-12 09:31:11 -0400163 GrBackendApi backend() const {return fBackend; }
Greg Daniel94403452017-04-18 15:52:36 -0400164
Greg Daniel52e16d92018-04-10 09:34:07 -0400165 // If the backend API is GL, copies a snapshot of the GrGLTextureInfo struct into the passed in
166 // pointer and returns true. Otherwise returns false if the backend API is not GL.
167 bool getGLTextureInfo(GrGLTextureInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400168
Greg Daniel323fbcf2018-04-10 13:46:30 -0400169 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
Greg Daniel52e16d92018-04-10 09:34:07 -0400170 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
171 // state. Otherwise returns false if the backend API is not Vulkan.
172 bool getVkImageInfo(GrVkImageInfo*) const;
173
Greg Daniel323fbcf2018-04-10 13:46:30 -0400174 // Anytime the client changes the VkImageLayout of the VkImage captured by this
175 // GrBackendTexture, they must call this function to notify Skia of the changed layout.
Greg Daniel52e16d92018-04-10 09:34:07 -0400176 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000177
Timothy Liang4e85e802018-06-28 16:37:18 -0400178#ifdef SK_METAL
179 // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
180 // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
181 bool getMtlTextureInfo(GrMtlTextureInfo*) const;
182#endif
183
Greg Daniel52e16d92018-04-10 09:34:07 -0400184 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
185 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
186 bool getMockTextureInfo(GrMockTextureInfo*) const;
Brian Salomon8fe24272017-07-07 12:56:11 -0400187
Eric Karl914a36b2017-10-12 12:44:50 -0700188 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400189 bool isValid() const { return fIsValid; }
Brian Salomon8fe24272017-07-07 12:56:11 -0400190
Robert Phillipsc5509952018-04-04 15:54:55 -0400191#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -0400192 // We can remove the pixelConfig getter and setter once we remove the GrPixelConfig from the
193 // GrBackendTexture and plumb the GrPixelconfig manually throughout our code (or remove all use
194 // of GrPixelConfig in general).
195 GrPixelConfig pixelConfig() const { return fConfig; }
196 void setPixelConfig(GrPixelConfig config) { fConfig = config; }
197
Robert Phillipsc5509952018-04-04 15:54:55 -0400198 static bool TestingOnly_Equals(const GrBackendTexture& , const GrBackendTexture&);
199#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500200
Eric Karl914a36b2017-10-12 12:44:50 -0700201private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500202 // Friending for access to the GrPixelConfig
Greg Danielfaa095e2017-12-19 13:15:02 -0500203 friend class SkImage;
Brian Salomon6a426c12018-03-15 12:16:02 -0400204 friend class SkImage_Gpu;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400205 friend class SkImage_GpuBase;
Jim Van Verthf49262d2018-10-02 12:07:20 -0400206 friend class SkImage_GpuYUVA;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400207 friend class SkPromiseImageHelper;
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 // Requires friending of GrVkGpu (done above already)
221 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
222
223 friend class GrVkTexture;
Greg Danielb4d89562018-10-03 18:44:49 +0000224#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400225 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 Danielbdf12ad2018-10-12 09:31:11 -0400239 GrBackendApi fBackend;
Greg Daniel94403452017-04-18 15:52:36 -0400240
241 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000242 GrGLTextureInfo fGLInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400243 GrVkBackendSurfaceInfo fVkInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400244#ifdef SK_METAL
245 GrMtlTextureInfo fMtlInfo;
246#endif
Brian Salomon8fe24272017-07-07 12:56:11 -0400247 GrMockTextureInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400248 };
249};
250
Brian Salomonec045b42017-07-07 10:34:40 -0400251class SK_API GrBackendRenderTarget {
Greg Daniel94403452017-04-18 15:52:36 -0400252public:
Robert Phillips57e08282017-11-16 14:59:48 -0500253 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400254 GrBackendRenderTarget() : fIsValid(false) {}
Robert Phillips57e08282017-11-16 14:59:48 -0500255
Greg Danielfaa095e2017-12-19 13:15:02 -0500256 // The GrGLTextureInfo must have a valid fFormat.
257 GrBackendRenderTarget(int width,
258 int height,
259 int sampleCnt,
260 int stencilBits,
261 const GrGLFramebufferInfo& glInfo);
262
Brian Salomonafdc6b12018-03-09 12:02:32 -0500263 /** Deprecated, use version that does not take stencil bits. */
Greg Daniel94403452017-04-18 15:52:36 -0400264 GrBackendRenderTarget(int width,
265 int height,
266 int sampleCnt,
267 int stencilBits,
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000268 const GrVkImageInfo& vkInfo);
Brian Salomonafdc6b12018-03-09 12:02:32 -0500269 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
Greg Daniel94403452017-04-18 15:52:36 -0400270
Timothy Liang4e85e802018-06-28 16:37:18 -0400271#ifdef SK_METAL
272 GrBackendRenderTarget(int width,
273 int height,
274 int sampleCnt,
275 const GrMtlTextureInfo& mtlInfo);
276#endif
277
Brian Salomon0c51eea2018-03-09 17:02:09 -0500278 GrBackendRenderTarget(int width,
279 int height,
280 int sampleCnt,
281 int stencilBits,
282 const GrMockRenderTargetInfo& mockInfo);
283
Greg Daniel323fbcf2018-04-10 13:46:30 -0400284 ~GrBackendRenderTarget();
285
286 GrBackendRenderTarget(const GrBackendRenderTarget& that);
287 GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
288
Greg Daniel94403452017-04-18 15:52:36 -0400289 int width() const { return fWidth; }
290 int height() const { return fHeight; }
291 int sampleCnt() const { return fSampleCnt; }
292 int stencilBits() const { return fStencilBits; }
Greg Danielbdf12ad2018-10-12 09:31:11 -0400293 GrBackendApi backend() const {return fBackend; }
Greg Daniel94403452017-04-18 15:52:36 -0400294
Greg Daniel323fbcf2018-04-10 13:46:30 -0400295 // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
296 // in pointer and returns true. Otherwise returns false if the backend API is not GL.
297 bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400298
Greg Daniel323fbcf2018-04-10 13:46:30 -0400299 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
300 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
301 // state. Otherwise returns false if the backend API is not Vulkan.
302 bool getVkImageInfo(GrVkImageInfo*) const;
303
304 // Anytime the client changes the VkImageLayout of the VkImage captured by this
305 // GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
306 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000307
Timothy Liang4e85e802018-06-28 16:37:18 -0400308#ifdef SK_METAL
309 // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
310 // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
311 bool getMtlTextureInfo(GrMtlTextureInfo*) const;
312#endif
313
Greg Daniel323fbcf2018-04-10 13:46:30 -0400314 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
315 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
316 bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500317
Robert Phillips57e08282017-11-16 14:59:48 -0500318 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400319 bool isValid() const { return fIsValid; }
Robert Phillips57e08282017-11-16 14:59:48 -0500320
Robert Phillips8caf85f2018-04-05 09:30:38 -0400321
322#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -0400323 // We can remove the pixelConfig getter and setter once we remove the pixel config from the
324 // GrBackendRenderTarget and plumb the pixel config manually throughout our code (or remove all
325 // use of GrPixelConfig in general).
326 GrPixelConfig pixelConfig() const { return fConfig; }
327 void setPixelConfig(GrPixelConfig config) { fConfig = config; }
328
Robert Phillips8caf85f2018-04-05 09:30:38 -0400329 static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
330#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500331
Greg Daniel94403452017-04-18 15:52:36 -0400332private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500333 // Friending for access to the GrPixelConfig
334 friend class SkSurface;
Greg Danielfaa095e2017-12-19 13:15:02 -0500335 friend class SkSurface_Gpu;
336 friend class SkImage_Gpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500337 friend class GrGpu;
338 friend class GrGLGpu;
Greg Daniel2a303902018-02-20 10:25:54 -0500339 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500340 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400341 friend class GrMtlGpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500342 GrPixelConfig config() const { return fConfig; }
343
Greg Daniel323fbcf2018-04-10 13:46:30 -0400344 // Requires friending of GrVkGpu (done above already)
345 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
346
347 friend class GrVkRenderTarget;
348 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
349 sk_sp<GrVkImageLayout> layout);
Greg Daniel323fbcf2018-04-10 13:46:30 -0400350
351 // Free and release and resources being held by the GrBackendTexture.
352 void cleanup();
353
Greg Daniel9ca30652018-04-06 09:27:20 -0400354 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400355 int fWidth; //<! width in pixels
356 int fHeight; //<! height in pixels
357
358 int fSampleCnt;
359 int fStencilBits;
360 GrPixelConfig fConfig;
361
Greg Danielbdf12ad2018-10-12 09:31:11 -0400362 GrBackendApi fBackend;
Greg Daniel94403452017-04-18 15:52:36 -0400363
364 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000365 GrGLFramebufferInfo fGLInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400366 GrVkBackendSurfaceInfo fVkInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400367#ifdef SK_METAL
368 GrMtlTextureInfo fMtlInfo;
369#endif
Brian Salomon0c51eea2018-03-09 17:02:09 -0500370 GrMockRenderTargetInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400371 };
372};
373
374#endif
375
Robert Phillips8caf85f2018-04-05 09:30:38 -0400376#endif
377