blob: 350887e62cc878c2788fecf3034c5d70d4ae4d59 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrTypes.h"
12#include "include/gpu/gl/GrGLTypes.h"
13#include "include/gpu/mock/GrMockTypes.h"
14#include "include/gpu/vk/GrVkTypes.h"
15#include "include/private/GrVkTypesPriv.h"
Greg Daniel52e16d92018-04-10 09:34:07 -040016
17class GrVkImageLayout;
Greg Daniel94403452017-04-18 15:52:36 -040018
Timothy Liang4e85e802018-06-28 16:37:18 -040019#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/gpu/mtl/GrMtlTypes.h"
Timothy Liang4e85e802018-06-28 16:37:18 -040021#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 Daniel45723ac2018-11-30 10:12:43 -050069 bool operator==(const GrBackendFormat& that) const;
70 bool operator!=(const GrBackendFormat& that) const { return !(*this == that); }
71
Greg Daniel4065d452018-11-16 15:43:41 -050072 GrBackendApi backend() const { return fBackend; }
73 GrTextureType textureType() const { return fTextureType; }
Robert Phillipsfc711a22018-02-13 17:03:00 -050074
75 // If the backend API is GL, these return a pointer to the format and target. Otherwise
76 // it returns nullptr.
77 const GrGLenum* getGLFormat() const;
78 const GrGLenum* getGLTarget() const;
79
Robert Phillipsfc711a22018-02-13 17:03:00 -050080 // If the backend API is Vulkan, this returns a pointer to a VkFormat. Otherwise
81 // it returns nullptr
82 const VkFormat* getVkFormat() const;
Robert Phillipsfc711a22018-02-13 17:03:00 -050083
Greg Daniel7e000222018-12-03 10:08:21 -050084 const GrVkYcbcrConversionInfo* getVkYcbcrConversionInfo() const;
85
Timothy Liang4e85e802018-06-28 16:37:18 -040086#ifdef SK_METAL
87 // If the backend API is Metal, this returns a pointer to a GrMTLPixelFormat. Otherwise
88 // it returns nullptr
89 const GrMTLPixelFormat* getMtlFormat() const;
90#endif
91
Robert Phillipsfc711a22018-02-13 17:03:00 -050092 // If the backend API is Mock, this returns a pointer to a GrPixelConfig. Otherwise
93 // it returns nullptr.
94 const GrPixelConfig* getMockFormat() const;
95
Greg Daniel387ec9a2019-03-07 16:44:54 -050096 // If possible, copies the GrBackendFormat and forces the texture type to be Texture2D. If the
97 // GrBackendFormat was for Vulkan and it originally had a GrVkYcbcrConversionInfo, we will
98 // remove the conversion and set the format to be VK_FORMAT_R8G8B8A8_UNORM.
Greg Daniel4065d452018-11-16 15:43:41 -050099 GrBackendFormat makeTexture2D() const;
100
Robert Phillipsfc711a22018-02-13 17:03:00 -0500101 // Returns true if the backend format has been initialized.
102 bool isValid() const { return fValid; }
103
104private:
105 GrBackendFormat(GrGLenum format, GrGLenum target);
106
Greg Daniel7e000222018-12-03 10:08:21 -0500107 GrBackendFormat(const VkFormat vkFormat, const GrVkYcbcrConversionInfo&);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500108
Timothy Liang4e85e802018-06-28 16:37:18 -0400109#ifdef SK_METAL
110 GrBackendFormat(const GrMTLPixelFormat mtlFormat);
111#endif
112
Robert Phillipsfc711a22018-02-13 17:03:00 -0500113 GrBackendFormat(const GrPixelConfig config);
114
Greg Danielbdf12ad2018-10-12 09:31:11 -0400115 GrBackendApi fBackend;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500116 bool fValid;
117
118 union {
Greg Daniel4065d452018-11-16 15:43:41 -0500119 GrGLenum fGLFormat; // the sized, internal format of the GL resource
Greg Daniel7e000222018-12-03 10:08:21 -0500120 struct {
121 VkFormat fFormat;
122 GrVkYcbcrConversionInfo fYcbcrConversionInfo;
123 } fVk;
Timothy Liang4e85e802018-06-28 16:37:18 -0400124#ifdef SK_METAL
125 GrMTLPixelFormat fMtlFormat;
126#endif
127 GrPixelConfig fMockFormat;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500128 };
Greg Daniel4065d452018-11-16 15:43:41 -0500129 GrTextureType fTextureType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500130};
131
Brian Salomonec045b42017-07-07 10:34:40 -0400132class SK_API GrBackendTexture {
Greg Daniel94403452017-04-18 15:52:36 -0400133public:
Brian Salomon8fe24272017-07-07 12:56:11 -0400134 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400135 GrBackendTexture() : fIsValid(false) {}
Brian Salomon8fe24272017-07-07 12:56:11 -0400136
Greg Daniele7d8da42017-12-04 11:23:19 -0500137 // The GrGLTextureInfo must have a valid fFormat.
138 GrBackendTexture(int width,
139 int height,
140 GrMipMapped,
141 const GrGLTextureInfo& glInfo);
142
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000143 GrBackendTexture(int width,
144 int height,
145 const GrVkImageInfo& vkInfo);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000146
Timothy Liang4e85e802018-06-28 16:37:18 -0400147#ifdef SK_METAL
148 GrBackendTexture(int width,
149 int height,
150 GrMipMapped,
151 const GrMtlTextureInfo& mtlInfo);
152#endif
153
Brian Salomon8fe24272017-07-07 12:56:11 -0400154 GrBackendTexture(int width,
155 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400156 GrMipMapped,
157 const GrMockTextureInfo& mockInfo);
158
Greg Daniel52e16d92018-04-10 09:34:07 -0400159 GrBackendTexture(const GrBackendTexture& that);
160
161 ~GrBackendTexture();
162
163 GrBackendTexture& operator=(const GrBackendTexture& that);
164
Greg Daniel94403452017-04-18 15:52:36 -0400165 int width() const { return fWidth; }
166 int height() const { return fHeight; }
Greg Daniel177e6952017-10-12 12:27:11 -0400167 bool hasMipMaps() const { return GrMipMapped::kYes == fMipMapped; }
Greg Danielbdf12ad2018-10-12 09:31:11 -0400168 GrBackendApi backend() const {return fBackend; }
Greg Daniel94403452017-04-18 15:52:36 -0400169
Greg Daniel52e16d92018-04-10 09:34:07 -0400170 // If the backend API is GL, copies a snapshot of the GrGLTextureInfo struct into the passed in
171 // pointer and returns true. Otherwise returns false if the backend API is not GL.
172 bool getGLTextureInfo(GrGLTextureInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400173
Greg Daniel323fbcf2018-04-10 13:46:30 -0400174 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
Greg Daniel52e16d92018-04-10 09:34:07 -0400175 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
176 // state. Otherwise returns false if the backend API is not Vulkan.
177 bool getVkImageInfo(GrVkImageInfo*) const;
178
Greg Daniel323fbcf2018-04-10 13:46:30 -0400179 // Anytime the client changes the VkImageLayout of the VkImage captured by this
180 // GrBackendTexture, they must call this function to notify Skia of the changed layout.
Greg Daniel52e16d92018-04-10 09:34:07 -0400181 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000182
Timothy Liang4e85e802018-06-28 16:37:18 -0400183#ifdef SK_METAL
184 // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
185 // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
186 bool getMtlTextureInfo(GrMtlTextureInfo*) const;
187#endif
188
Brian Salomonf391d0f2018-12-14 09:18:50 -0500189 // Get the GrBackendFormat for this texture (or an invalid format if this is not valid).
190 GrBackendFormat getBackendFormat() const;
191
Greg Daniel52e16d92018-04-10 09:34:07 -0400192 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
193 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
194 bool getMockTextureInfo(GrMockTextureInfo*) const;
Brian Salomon8fe24272017-07-07 12:56:11 -0400195
Eric Karl914a36b2017-10-12 12:44:50 -0700196 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400197 bool isValid() const { return fIsValid; }
Brian Salomon8fe24272017-07-07 12:56:11 -0400198
Robert Phillipsc5509952018-04-04 15:54:55 -0400199#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -0400200 // We can remove the pixelConfig getter and setter once we remove the GrPixelConfig from the
201 // GrBackendTexture and plumb the GrPixelconfig manually throughout our code (or remove all use
202 // of GrPixelConfig in general).
203 GrPixelConfig pixelConfig() const { return fConfig; }
204 void setPixelConfig(GrPixelConfig config) { fConfig = config; }
205
Robert Phillipsc5509952018-04-04 15:54:55 -0400206 static bool TestingOnly_Equals(const GrBackendTexture& , const GrBackendTexture&);
207#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500208
Eric Karl914a36b2017-10-12 12:44:50 -0700209private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500210 // Friending for access to the GrPixelConfig
Greg Danielfaa095e2017-12-19 13:15:02 -0500211 friend class SkImage;
Brian Salomon6a426c12018-03-15 12:16:02 -0400212 friend class SkImage_Gpu;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400213 friend class SkImage_GpuBase;
Jim Van Verthf49262d2018-10-02 12:07:20 -0400214 friend class SkImage_GpuYUVA;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400215 friend class SkPromiseImageHelper;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500216 friend class SkSurface;
Greg Daniel108bb232018-07-03 16:18:29 -0400217 friend class GrAHardwareBufferImageGenerator;
Greg Daniele728f672018-01-17 10:52:04 -0500218 friend class GrBackendTextureImageGenerator;
Greg Danielf2336e42018-01-23 16:38:14 -0500219 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500220 friend class GrGpu;
221 friend class GrGLGpu;
222 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400223 friend class GrMtlGpu;
Greg Daniel057627f2018-03-14 15:51:58 -0400224 friend class PromiseImageHelper;
Robert Phillipsc5509952018-04-04 15:54:55 -0400225
Greg Daniel5254ccc2017-11-13 11:05:52 -0500226 GrPixelConfig config() const { return fConfig; }
227
Greg Daniel52e16d92018-04-10 09:34:07 -0400228 // Requires friending of GrVkGpu (done above already)
229 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
230
231 friend class GrVkTexture;
Greg Danielb4d89562018-10-03 18:44:49 +0000232#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400233 GrBackendTexture(int width,
234 int height,
235 const GrVkImageInfo& vkInfo,
236 sk_sp<GrVkImageLayout> layout);
237#endif
238
239 // Free and release and resources being held by the GrBackendTexture.
240 void cleanup();
241
Greg Daniel9ca30652018-04-06 09:27:20 -0400242 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400243 int fWidth; //<! width in pixels
244 int fHeight; //<! height in pixels
245 GrPixelConfig fConfig;
Greg Daniel177e6952017-10-12 12:27:11 -0400246 GrMipMapped fMipMapped;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400247 GrBackendApi fBackend;
Greg Daniel94403452017-04-18 15:52:36 -0400248
249 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000250 GrGLTextureInfo fGLInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400251 GrVkBackendSurfaceInfo fVkInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400252#ifdef SK_METAL
253 GrMtlTextureInfo fMtlInfo;
254#endif
Brian Salomon8fe24272017-07-07 12:56:11 -0400255 GrMockTextureInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400256 };
257};
258
Brian Salomonec045b42017-07-07 10:34:40 -0400259class SK_API GrBackendRenderTarget {
Greg Daniel94403452017-04-18 15:52:36 -0400260public:
Robert Phillips57e08282017-11-16 14:59:48 -0500261 // Creates an invalid backend texture.
Greg Daniel9ca30652018-04-06 09:27:20 -0400262 GrBackendRenderTarget() : fIsValid(false) {}
Robert Phillips57e08282017-11-16 14:59:48 -0500263
Greg Danielfaa095e2017-12-19 13:15:02 -0500264 // The GrGLTextureInfo must have a valid fFormat.
265 GrBackendRenderTarget(int width,
266 int height,
267 int sampleCnt,
268 int stencilBits,
269 const GrGLFramebufferInfo& glInfo);
270
Brian Salomonafdc6b12018-03-09 12:02:32 -0500271 /** Deprecated, use version that does not take stencil bits. */
Greg Daniel94403452017-04-18 15:52:36 -0400272 GrBackendRenderTarget(int width,
273 int height,
274 int sampleCnt,
275 int stencilBits,
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000276 const GrVkImageInfo& vkInfo);
Brian Salomonafdc6b12018-03-09 12:02:32 -0500277 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
Greg Daniel94403452017-04-18 15:52:36 -0400278
Timothy Liang4e85e802018-06-28 16:37:18 -0400279#ifdef SK_METAL
280 GrBackendRenderTarget(int width,
281 int height,
282 int sampleCnt,
283 const GrMtlTextureInfo& mtlInfo);
284#endif
285
Brian Salomon0c51eea2018-03-09 17:02:09 -0500286 GrBackendRenderTarget(int width,
287 int height,
288 int sampleCnt,
289 int stencilBits,
290 const GrMockRenderTargetInfo& mockInfo);
291
Greg Daniel323fbcf2018-04-10 13:46:30 -0400292 ~GrBackendRenderTarget();
293
294 GrBackendRenderTarget(const GrBackendRenderTarget& that);
295 GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
296
Greg Daniel94403452017-04-18 15:52:36 -0400297 int width() const { return fWidth; }
298 int height() const { return fHeight; }
299 int sampleCnt() const { return fSampleCnt; }
300 int stencilBits() const { return fStencilBits; }
Greg Danielbdf12ad2018-10-12 09:31:11 -0400301 GrBackendApi backend() const {return fBackend; }
Greg Daniel94403452017-04-18 15:52:36 -0400302
Greg Daniel323fbcf2018-04-10 13:46:30 -0400303 // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
304 // in pointer and returns true. Otherwise returns false if the backend API is not GL.
305 bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
Greg Danielc0f8e422017-06-13 13:47:53 -0400306
Greg Daniel323fbcf2018-04-10 13:46:30 -0400307 // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
308 // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
309 // state. Otherwise returns false if the backend API is not Vulkan.
310 bool getVkImageInfo(GrVkImageInfo*) const;
311
312 // Anytime the client changes the VkImageLayout of the VkImage captured by this
313 // GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
314 void setVkImageLayout(VkImageLayout);
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000315
Timothy Liang4e85e802018-06-28 16:37:18 -0400316#ifdef SK_METAL
317 // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
318 // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
319 bool getMtlTextureInfo(GrMtlTextureInfo*) const;
320#endif
321
Greg Daniel323fbcf2018-04-10 13:46:30 -0400322 // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
323 // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
324 bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500325
Robert Phillips57e08282017-11-16 14:59:48 -0500326 // Returns true if the backend texture has been initialized.
Greg Daniel9ca30652018-04-06 09:27:20 -0400327 bool isValid() const { return fIsValid; }
Robert Phillips57e08282017-11-16 14:59:48 -0500328
Robert Phillips8caf85f2018-04-05 09:30:38 -0400329
330#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -0400331 // We can remove the pixelConfig getter and setter once we remove the pixel config from the
332 // GrBackendRenderTarget and plumb the pixel config manually throughout our code (or remove all
333 // use of GrPixelConfig in general).
334 GrPixelConfig pixelConfig() const { return fConfig; }
335 void setPixelConfig(GrPixelConfig config) { fConfig = config; }
336
Robert Phillips8caf85f2018-04-05 09:30:38 -0400337 static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
338#endif
Greg Daniel2a303902018-02-20 10:25:54 -0500339
Greg Daniel94403452017-04-18 15:52:36 -0400340private:
Greg Daniel5254ccc2017-11-13 11:05:52 -0500341 // Friending for access to the GrPixelConfig
342 friend class SkSurface;
Greg Danielfaa095e2017-12-19 13:15:02 -0500343 friend class SkSurface_Gpu;
344 friend class SkImage_Gpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500345 friend class GrGpu;
346 friend class GrGLGpu;
Greg Daniel2a303902018-02-20 10:25:54 -0500347 friend class GrProxyProvider;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500348 friend class GrVkGpu;
Timothy Liang4e85e802018-06-28 16:37:18 -0400349 friend class GrMtlGpu;
Greg Daniel5254ccc2017-11-13 11:05:52 -0500350 GrPixelConfig config() const { return fConfig; }
351
Greg Daniel323fbcf2018-04-10 13:46:30 -0400352 // Requires friending of GrVkGpu (done above already)
353 sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
354
355 friend class GrVkRenderTarget;
356 GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
357 sk_sp<GrVkImageLayout> layout);
Greg Daniel323fbcf2018-04-10 13:46:30 -0400358
359 // Free and release and resources being held by the GrBackendTexture.
360 void cleanup();
361
Greg Daniel9ca30652018-04-06 09:27:20 -0400362 bool fIsValid;
Greg Daniel94403452017-04-18 15:52:36 -0400363 int fWidth; //<! width in pixels
364 int fHeight; //<! height in pixels
365
366 int fSampleCnt;
367 int fStencilBits;
368 GrPixelConfig fConfig;
369
Greg Danielbdf12ad2018-10-12 09:31:11 -0400370 GrBackendApi fBackend;
Greg Daniel94403452017-04-18 15:52:36 -0400371
372 union {
Robert Phillipsfad9e3f2017-06-13 22:16:08 +0000373 GrGLFramebufferInfo fGLInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400374 GrVkBackendSurfaceInfo fVkInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400375#ifdef SK_METAL
376 GrMtlTextureInfo fMtlInfo;
377#endif
Brian Salomon0c51eea2018-03-09 17:02:09 -0500378 GrMockRenderTargetInfo fMockInfo;
Greg Daniel94403452017-04-18 15:52:36 -0400379 };
380};
381
382#endif
383
Robert Phillips8caf85f2018-04-05 09:30:38 -0400384#endif
385