blob: 2fe33096f3327adc3412ffb975616c66347bb0d2 [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"
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +000014
15#ifdef SK_VULKAN
Greg Danielbcf612b2017-05-01 13:50:58 +000016#include "vk/GrVkTypes.h"
Greg Daniel52e16d92018-04-10 09:34:07 -040017#include "../private/GrVkTypesPriv.h"
18
19class GrVkImageLayout;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +000020#endif
Greg Daniel94403452017-04-18 15:52:36 -040021
Robert Phillips8caf85f2018-04-05 09:30:38 -040022#if !SK_SUPPORT_GPU
23
24// SkSurface and SkImage rely on a minimal version of these always being available
25class SK_API GrBackendTexture {
26public:
27 GrBackendTexture() {}
28
29 bool isValid() const { return false; }
30};
31
32class SK_API GrBackendRenderTarget {
33public:
34 GrBackendRenderTarget() {}
35
36 bool isValid() const { return false; }
37};
38#else
39
Robert Phillipsfc711a22018-02-13 17:03:00 -050040class SK_API GrBackendFormat {
41public:
42 // Creates an invalid backend format.
43 GrBackendFormat() : fValid(false) {}
44
45 static GrBackendFormat MakeGL(GrGLenum format, GrGLenum target) {
46 return GrBackendFormat(format, target);
47 }
48
49#ifdef SK_VULKAN
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 }
53#endif
54
55 static GrBackendFormat MakeMock(GrPixelConfig config) {
56 return GrBackendFormat(config);
57 }
58
59 GrBackend backend() const {return fBackend; }
60
61 // If the backend API is GL, these return a pointer to the format and target. Otherwise
62 // it returns nullptr.
63 const GrGLenum* getGLFormat() const;
64 const GrGLenum* getGLTarget() const;
65
66#ifdef SK_VULKAN
67 // If the backend API is Vulkan, this returns a pointer to a VkFormat. Otherwise
68 // it returns nullptr
69 const VkFormat* getVkFormat() const;
70#endif
71
72 // If the backend API is Mock, this returns a pointer to a GrPixelConfig. Otherwise
73 // it returns nullptr.
74 const GrPixelConfig* getMockFormat() const;
75
76 // Returns true if the backend format has been initialized.
77 bool isValid() const { return fValid; }
78
79private:
80 GrBackendFormat(GrGLenum format, GrGLenum target);
81
82#ifdef SK_VULKAN
83 GrBackendFormat(const VkFormat vkFormat);
84#endif
85
86 GrBackendFormat(const GrPixelConfig config);
87
88 GrBackend fBackend;
89 bool fValid;
90
91 union {
92 struct {
93 GrGLenum fTarget; // GL_TEXTURE_2D, GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE
94 GrGLenum fFormat; // the sized, internal format of the GL resource
95 } fGL;
96#ifdef SK_VULKAN
97 VkFormat fVkFormat;
98#endif
99 GrPixelConfig fMockFormat;
100 };
101};
102
Brian Salomonec045b42017-07-07 10:34:40 -0400103class SK_API GrBackendTexture {
Greg Daniel94403452017-04-18 15:52:36 -0400104public:
Brian Salomon8fe24272017-07-07 12:56:11 -0400105 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400106 GrBackendTexture() : fIsValid(false) {}
Brian Salomon8fe24272017-07-07 12:56:11 -0400107
Brian Salomon34df0d32018-03-23 18:23:23 -0400108#if GR_TEST_UTILS
Greg Daniele7d8da42017-12-04 11:23:19 -0500109 // GrGLTextureInfo::fFormat is ignored
110 // Deprecated: Should use version that does not take a GrPixelConfig instead
Greg Daniel94403452017-04-18 15:52:36 -0400111 GrBackendTexture(int width,
112 int height,
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000113 GrPixelConfig config,
114 const GrGLTextureInfo& glInfo);
Greg Danielc0f8e422017-06-13 13:47:53 -0400115
Greg Daniele7d8da42017-12-04 11:23:19 -0500116 // GrGLTextureInfo::fFormat is ignored
117 // Deprecated: Should use version that does not take a GrPixelConfig instead
Greg Daniel177e6952017-10-12 12:27:11 -0400118 GrBackendTexture(int width,
119 int height,
120 GrPixelConfig config,
121 GrMipMapped,
122 const GrGLTextureInfo& glInfo);
Brian Salomon34df0d32018-03-23 18:23:23 -0400123#endif
Greg Daniel177e6952017-10-12 12:27:11 -0400124
Greg Daniele7d8da42017-12-04 11:23:19 -0500125 // The GrGLTextureInfo must have a valid fFormat.
126 GrBackendTexture(int width,
127 int height,
128 GrMipMapped,
129 const GrGLTextureInfo& glInfo);
130
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000131#ifdef SK_VULKAN
132 GrBackendTexture(int width,
133 int height,
134 const GrVkImageInfo& vkInfo);
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
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000157#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400158 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
Greg Daniel52e16d92018-04-10 09:34:07 -0400159 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
160 // state. Otherwise returns false if the backend API is not Vulkan.
161 bool getVkImageInfo(GrVkImageInfo*) const;
162
Greg Daniel323fbcf2018-04-10 13:46:30 -0400163 // Anytime the client changes the VkImageLayout of the VkImage captured by this
164 // GrBackendTexture, they must call this function to notify Skia of the changed layout.
Greg Daniel52e16d92018-04-10 09:34:07 -0400165 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000166#endif
167
Greg Daniel52e16d92018-04-10 09:34:07 -0400168 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
169 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
170 bool getMockTextureInfo(GrMockTextureInfo*) const;
Brian Salomon8fe24272017-07-07 12:56:11 -0400171
Eric Karl914a36b2017-10-12 12:44:50 -0700172 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400173 bool isValid() const { return fIsValid; }
Brian Salomon8fe24272017-07-07 12:56:11 -0400174
Robert Phillips7f441962018-03-15 11:15:19 -0400175 /**
176 * Create a GrBackendFormat object that matches this texture
177 */
178 GrBackendFormat format() const;
179
Robert Phillipsc5509952018-04-04 15:54:55 -0400180#if GR_TEST_UTILS
Greg Daniel8a3f55c2018-03-14 17:32:12 +0000181 GrPixelConfig testingOnly_getPixelConfig() const;
Robert Phillipsc5509952018-04-04 15:54:55 -0400182 static bool TestingOnly_Equals(const GrBackendTexture& , const GrBackendTexture&);
183#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500184
Eric Karl914a36b2017-10-12 12:44:50 -0700185private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500186 // Friending for access to the GrPixelConfig
Greg Danielfaa095e2017-12-19 13:15:02 -0500187 friend class SkImage;
Brian Salomon6a426c12018-03-15 12:16:02 -0400188 friend class SkImage_Gpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500189 friend class SkSurface;
Greg Daniele728f672018-01-17 10:52:04 -0500190 friend class GrBackendTextureImageGenerator;
Greg Danielf2336e42018-01-23 16:38:14 -0500191 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500192 friend class GrGpu;
193 friend class GrGLGpu;
194 friend class GrVkGpu;
Greg Daniel057627f2018-03-14 15:51:58 -0400195 friend class PromiseImageHelper;
Robert Phillipsc5509952018-04-04 15:54:55 -0400196
Greg Daniel5254ccc2017-11-13 11:05:52 -0500197 GrPixelConfig config() const { return fConfig; }
198
Greg Daniel52e16d92018-04-10 09:34:07 -0400199#ifdef SK_VULKAN
200 // Requires friending of GrVkGpu (done above already)
201 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
202
203 friend class GrVkTexture;
204 GrBackendTexture(int width,
205 int height,
206 const GrVkImageInfo& vkInfo,
207 sk_sp<GrVkImageLayout> layout);
208#endif
209
210 // Free and release and resources being held by the GrBackendTexture.
211 void cleanup();
212
Greg Daniel9ca30652018-04-06 09:27:20 -0400213 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400214 int fWidth; //<! width in pixels
215 int fHeight; //<! height in pixels
216 GrPixelConfig fConfig;
Greg Daniel177e6952017-10-12 12:27:11 -0400217 GrMipMapped fMipMapped;
Greg Daniel94403452017-04-18 15:52:36 -0400218 GrBackend fBackend;
219
220 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000221 GrGLTextureInfo fGLInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000222#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400223 GrVkBackendSurfaceInfo fVkInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000224#endif
Brian Salomon8fe24272017-07-07 12:56:11 -0400225 GrMockTextureInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400226 };
227};
228
Brian Salomonec045b42017-07-07 10:34:40 -0400229class SK_API GrBackendRenderTarget {
Greg Daniel94403452017-04-18 15:52:36 -0400230public:
Robert Phillips57e08282017-11-16 14:59:48 -0500231 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400232 GrBackendRenderTarget() : fIsValid(false) {}
Robert Phillips57e08282017-11-16 14:59:48 -0500233
Brian Salomon34df0d32018-03-23 18:23:23 -0400234#if GR_TEST_UTILS
Greg Danielfaa095e2017-12-19 13:15:02 -0500235 // GrGLTextureInfo::fFormat is ignored
236 // Deprecated: Should use version that does not take a GrPixelConfig instead
Greg Daniel94403452017-04-18 15:52:36 -0400237 GrBackendRenderTarget(int width,
238 int height,
239 int sampleCnt,
240 int stencilBits,
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000241 GrPixelConfig config,
242 const GrGLFramebufferInfo& glInfo);
Brian Salomon34df0d32018-03-23 18:23:23 -0400243#endif
Greg Daniel94403452017-04-18 15:52:36 -0400244
Greg Danielfaa095e2017-12-19 13:15:02 -0500245 // The GrGLTextureInfo must have a valid fFormat.
246 GrBackendRenderTarget(int width,
247 int height,
248 int sampleCnt,
249 int stencilBits,
250 const GrGLFramebufferInfo& glInfo);
251
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000252#ifdef SK_VULKAN
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);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000260#endif
Greg Daniel94403452017-04-18 15:52:36 -0400261
Brian Salomon0c51eea2018-03-09 17:02:09 -0500262 GrBackendRenderTarget(int width,
263 int height,
264 int sampleCnt,
265 int stencilBits,
266 const GrMockRenderTargetInfo& mockInfo);
267
Greg Daniel323fbcf2018-04-10 13:46:30 -0400268 ~GrBackendRenderTarget();
269
270 GrBackendRenderTarget(const GrBackendRenderTarget& that);
271 GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
272
Greg Daniel94403452017-04-18 15:52:36 -0400273 int width() const { return fWidth; }
274 int height() const { return fHeight; }
275 int sampleCnt() const { return fSampleCnt; }
276 int stencilBits() const { return fStencilBits; }
Greg Daniel94403452017-04-18 15:52:36 -0400277 GrBackend backend() const {return fBackend; }
278
Greg Daniel323fbcf2018-04-10 13:46:30 -0400279 // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
280 // in pointer and returns true. Otherwise returns false if the backend API is not GL.
281 bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400282
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000283#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400284 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
285 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
286 // state. Otherwise returns false if the backend API is not Vulkan.
287 bool getVkImageInfo(GrVkImageInfo*) const;
288
289 // Anytime the client changes the VkImageLayout of the VkImage captured by this
290 // GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
291 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000292#endif
293
Greg Daniel323fbcf2018-04-10 13:46:30 -0400294 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
295 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
296 bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500297
Robert Phillips57e08282017-11-16 14:59:48 -0500298 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400299 bool isValid() const { return fIsValid; }
Robert Phillips57e08282017-11-16 14:59:48 -0500300
Robert Phillips8caf85f2018-04-05 09:30:38 -0400301
302#if GR_TEST_UTILS
Greg Daniel8a3f55c2018-03-14 17:32:12 +0000303 GrPixelConfig testingOnly_getPixelConfig() const;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400304 static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
305#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500306
Greg Daniel94403452017-04-18 15:52:36 -0400307private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500308 // Friending for access to the GrPixelConfig
309 friend class SkSurface;
Greg Danielfaa095e2017-12-19 13:15:02 -0500310 friend class SkSurface_Gpu;
311 friend class SkImage_Gpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500312 friend class GrGpu;
313 friend class GrGLGpu;
Greg Daniel2a303902018-02-20 10:25:54 -0500314 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500315 friend class GrVkGpu;
316 GrPixelConfig config() const { return fConfig; }
317
Greg Daniel323fbcf2018-04-10 13:46:30 -0400318#ifdef SK_VULKAN
319 // Requires friending of GrVkGpu (done above already)
320 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
321
322 friend class GrVkRenderTarget;
323 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
324 sk_sp<GrVkImageLayout> layout);
325#endif
326
327 // Free and release and resources being held by the GrBackendTexture.
328 void cleanup();
329
Greg Daniel9ca30652018-04-06 09:27:20 -0400330 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400331 int fWidth; //<! width in pixels
332 int fHeight; //<! height in pixels
333
334 int fSampleCnt;
335 int fStencilBits;
336 GrPixelConfig fConfig;
337
338 GrBackend fBackend;
339
340 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000341 GrGLFramebufferInfo fGLInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000342#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400343 GrVkBackendSurfaceInfo fVkInfo;
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000344#endif
Brian Salomon0c51eea2018-03-09 17:02:09 -0500345 GrMockRenderTargetInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400346 };
347};
348
349#endif
350
Robert Phillips8caf85f2018-04-05 09:30:38 -0400351#endif
352