blob: e0db0a4282013587b5e6a1d428713b8891ab7af0 [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 Verthf49262d2018-10-02 12:07:20 -0400193 friend class SkImage_GpuYUVA;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500194 friend class SkSurface;
Greg Daniel108bb232018-07-03 16:18:29 -0400195 friend class GrAHardwareBufferImageGenerator;
Greg Daniele728f672018-01-17 10:52:04 -0500196 friend class GrBackendTextureImageGenerator;
Greg Danielf2336e42018-01-23 16:38:14 -0500197 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500198 friend class GrGpu;
199 friend class GrGLGpu;
200 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400201 friend class GrMtlGpu;
Greg Daniel057627f2018-03-14 15:51:58 -0400202 friend class PromiseImageHelper;
Robert Phillipsc5509952018-04-04 15:54:55 -0400203
Greg Daniel5254ccc2017-11-13 11:05:52 -0500204 GrPixelConfig config() const { return fConfig; }
205
Greg Daniel52e16d92018-04-10 09:34:07 -0400206 // Requires friending of GrVkGpu (done above already)
207 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
208
209 friend class GrVkTexture;
Greg Danielb4d89562018-10-03 18:44:49 +0000210#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400211 GrBackendTexture(int width,
212 int height,
213 const GrVkImageInfo& vkInfo,
214 sk_sp<GrVkImageLayout> layout);
215#endif
216
217 // Free and release and resources being held by the GrBackendTexture.
218 void cleanup();
219
Greg Daniel9ca30652018-04-06 09:27:20 -0400220 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400221 int fWidth; //<! width in pixels
222 int fHeight; //<! height in pixels
223 GrPixelConfig fConfig;
Greg Daniel177e6952017-10-12 12:27:11 -0400224 GrMipMapped fMipMapped;
Greg Daniel94403452017-04-18 15:52:36 -0400225 GrBackend fBackend;
226
227 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000228 GrGLTextureInfo fGLInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400229 GrVkBackendSurfaceInfo fVkInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400230#ifdef SK_METAL
231 GrMtlTextureInfo fMtlInfo;
232#endif
Brian Salomon8fe24272017-07-07 12:56:11 -0400233 GrMockTextureInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400234 };
235};
236
Brian Salomonec045b42017-07-07 10:34:40 -0400237class SK_API GrBackendRenderTarget {
Greg Daniel94403452017-04-18 15:52:36 -0400238public:
Robert Phillips57e08282017-11-16 14:59:48 -0500239 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400240 GrBackendRenderTarget() : fIsValid(false) {}
Robert Phillips57e08282017-11-16 14:59:48 -0500241
Greg Danielfaa095e2017-12-19 13:15:02 -0500242 // The GrGLTextureInfo must have a valid fFormat.
243 GrBackendRenderTarget(int width,
244 int height,
245 int sampleCnt,
246 int stencilBits,
247 const GrGLFramebufferInfo& glInfo);
248
Brian Salomonafdc6b12018-03-09 12:02:32 -0500249 /** Deprecated, use version that does not take stencil bits. */
Greg Daniel94403452017-04-18 15:52:36 -0400250 GrBackendRenderTarget(int width,
251 int height,
252 int sampleCnt,
253 int stencilBits,
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000254 const GrVkImageInfo& vkInfo);
Brian Salomonafdc6b12018-03-09 12:02:32 -0500255 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
Greg Daniel94403452017-04-18 15:52:36 -0400256
Timothy Liang4e85e802018-06-28 16:37:18 -0400257#ifdef SK_METAL
258 GrBackendRenderTarget(int width,
259 int height,
260 int sampleCnt,
261 const GrMtlTextureInfo& mtlInfo);
262#endif
263
Brian Salomon0c51eea2018-03-09 17:02:09 -0500264 GrBackendRenderTarget(int width,
265 int height,
266 int sampleCnt,
267 int stencilBits,
268 const GrMockRenderTargetInfo& mockInfo);
269
Greg Daniel323fbcf2018-04-10 13:46:30 -0400270 ~GrBackendRenderTarget();
271
272 GrBackendRenderTarget(const GrBackendRenderTarget& that);
273 GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
274
Greg Daniel94403452017-04-18 15:52:36 -0400275 int width() const { return fWidth; }
276 int height() const { return fHeight; }
277 int sampleCnt() const { return fSampleCnt; }
278 int stencilBits() const { return fStencilBits; }
Greg Daniel94403452017-04-18 15:52:36 -0400279 GrBackend backend() const {return fBackend; }
280
Greg Daniel323fbcf2018-04-10 13:46:30 -0400281 // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
282 // in pointer and returns true. Otherwise returns false if the backend API is not GL.
283 bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400284
Greg Daniel323fbcf2018-04-10 13:46:30 -0400285 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
286 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
287 // state. Otherwise returns false if the backend API is not Vulkan.
288 bool getVkImageInfo(GrVkImageInfo*) const;
289
290 // Anytime the client changes the VkImageLayout of the VkImage captured by this
291 // GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
292 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000293
Timothy Liang4e85e802018-06-28 16:37:18 -0400294#ifdef SK_METAL
295 // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
296 // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
297 bool getMtlTextureInfo(GrMtlTextureInfo*) const;
298#endif
299
Greg Daniel323fbcf2018-04-10 13:46:30 -0400300 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
301 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
302 bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500303
Robert Phillips57e08282017-11-16 14:59:48 -0500304 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400305 bool isValid() const { return fIsValid; }
Robert Phillips57e08282017-11-16 14:59:48 -0500306
Robert Phillips8caf85f2018-04-05 09:30:38 -0400307
308#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -0400309 // We can remove the pixelConfig getter and setter once we remove the pixel config from the
310 // GrBackendRenderTarget and plumb the pixel config manually throughout our code (or remove all
311 // use of GrPixelConfig in general).
312 GrPixelConfig pixelConfig() const { return fConfig; }
313 void setPixelConfig(GrPixelConfig config) { fConfig = config; }
314
Robert Phillips8caf85f2018-04-05 09:30:38 -0400315 static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
316#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500317
Greg Daniel94403452017-04-18 15:52:36 -0400318private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500319 // Friending for access to the GrPixelConfig
320 friend class SkSurface;
Greg Danielfaa095e2017-12-19 13:15:02 -0500321 friend class SkSurface_Gpu;
322 friend class SkImage_Gpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500323 friend class GrGpu;
324 friend class GrGLGpu;
Greg Daniel2a303902018-02-20 10:25:54 -0500325 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500326 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400327 friend class GrMtlGpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500328 GrPixelConfig config() const { return fConfig; }
329
Greg Daniel323fbcf2018-04-10 13:46:30 -0400330 // Requires friending of GrVkGpu (done above already)
331 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
332
333 friend class GrVkRenderTarget;
334 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
335 sk_sp<GrVkImageLayout> layout);
Greg Daniel323fbcf2018-04-10 13:46:30 -0400336
337 // Free and release and resources being held by the GrBackendTexture.
338 void cleanup();
339
Greg Daniel9ca30652018-04-06 09:27:20 -0400340 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400341 int fWidth; //<! width in pixels
342 int fHeight; //<! height in pixels
343
344 int fSampleCnt;
345 int fStencilBits;
346 GrPixelConfig fConfig;
347
348 GrBackend fBackend;
349
350 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000351 GrGLFramebufferInfo fGLInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400352 GrVkBackendSurfaceInfo fVkInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400353#ifdef SK_METAL
354 GrMtlTextureInfo fMtlInfo;
355#endif
Brian Salomon0c51eea2018-03-09 17:02:09 -0500356 GrMockRenderTargetInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400357 };
358};
359
360#endif
361
Robert Phillips8caf85f2018-04-05 09:30:38 -0400362#endif
363