blob: 1b1d1f27a1904c6def172c27990203bfd29091c3 [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
Greg Daniel54bfb182018-11-20 17:12:36 -05008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/gpu/GrBackendSurface.h"
Greg Daniel94403452017-04-18 15:52:36 -040010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/gl/GrGLUtil.h"
Greg Daniele7d8da42017-12-04 11:23:19 -050012
Stephen White985741a2019-07-18 11:43:45 -040013#ifdef SK_DAWN
14#include "include/gpu/dawn/GrDawnTypes.h"
15#include "src/gpu/dawn/GrDawnUtil.h"
16#endif
17
Greg Daniel94403452017-04-18 15:52:36 -040018#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/gpu/vk/GrVkTypes.h"
20#include "src/gpu/vk/GrVkImageLayout.h"
21#include "src/gpu/vk/GrVkUtil.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000022#endif
Timothy Liang4e85e802018-06-28 16:37:18 -040023#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/gpu/mtl/GrMtlTypes.h"
25#include "src/gpu/mtl/GrMtlCppUtil.h"
Timothy Liang4e85e802018-06-28 16:37:18 -040026#endif
Greg Daniel7ef28f32017-04-20 16:41:55 +000027
Robert Phillipsb2adbef2019-07-02 16:33:05 -040028GrBackendFormat::GrBackendFormat(const GrBackendFormat& that)
29 : fBackend(that.fBackend)
30 , fValid(that.fValid)
31 , fTextureType(that.fTextureType) {
32 if (!fValid) {
33 return;
34 }
35
36 switch (fBackend) {
37#ifdef SK_GL
38 case GrBackendApi::kOpenGL:
39 fGLFormat = that.fGLFormat;
40 break;
41#endif
42#ifdef SK_VULKAN
43 case GrBackendApi::kVulkan:
44 fVk = that.fVk;
45 break;
46#endif
47#ifdef SK_METAL
48 case GrBackendApi::kMetal:
49 fMtlFormat = that.fMtlFormat;
50 break;
51#endif
Stephen White985741a2019-07-18 11:43:45 -040052#ifdef SK_DAWN
53 case GrBackendApi::kDawn:
54 fDawnFormat = that.fDawnFormat;
55 break;
56#endif
Robert Phillipsb2adbef2019-07-02 16:33:05 -040057 case GrBackendApi::kMock:
Greg Daniele877dce2019-07-11 10:52:43 -040058 fMockColorType = that.fMockColorType;
Robert Phillipsb2adbef2019-07-02 16:33:05 -040059 break;
60 default:
61 SK_ABORT("Unknown GrBackend");
62 }
63}
64
John Rosascoa9b348f2019-11-08 13:18:15 -080065#ifdef SK_GL
Robert Phillipsfc711a22018-02-13 17:03:00 -050066GrBackendFormat::GrBackendFormat(GrGLenum format, GrGLenum target)
Greg Danielbdf12ad2018-10-12 09:31:11 -040067 : fBackend(GrBackendApi::kOpenGL)
Greg Daniel4065d452018-11-16 15:43:41 -050068 , fValid(true)
69 , fGLFormat(format) {
70 switch (target) {
Robert Phillipsf209e882019-06-25 15:59:50 -040071 case GR_GL_TEXTURE_NONE:
72 fTextureType = GrTextureType::kNone;
73 break;
Greg Daniel4065d452018-11-16 15:43:41 -050074 case GR_GL_TEXTURE_2D:
75 fTextureType = GrTextureType::k2D;
76 break;
77 case GR_GL_TEXTURE_RECTANGLE:
78 fTextureType = GrTextureType::kRectangle;
79 break;
80 case GR_GL_TEXTURE_EXTERNAL:
81 fTextureType = GrTextureType::kExternal;
82 break;
83 default:
84 SK_ABORT("Unexpected texture target");
85 }
Robert Phillipsfc711a22018-02-13 17:03:00 -050086}
87
Brian Salomond4764a12019-08-08 12:08:24 -040088GrGLFormat GrBackendFormat::asGLFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -040089 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -040090 return GrGLFormatFromGLEnum(fGLFormat);
Robert Phillipsfc711a22018-02-13 17:03:00 -050091 }
Brian Salomond4764a12019-08-08 12:08:24 -040092 return GrGLFormat::kUnknown;
Robert Phillipsfc711a22018-02-13 17:03:00 -050093}
John Rosascoa9b348f2019-11-08 13:18:15 -080094#endif
Robert Phillipsfc711a22018-02-13 17:03:00 -050095
Greg Daniel7e000222018-12-03 10:08:21 -050096GrBackendFormat GrBackendFormat::MakeVk(const GrVkYcbcrConversionInfo& ycbcrInfo) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -070097 SkASSERT(ycbcrInfo.isValid());
98 return GrBackendFormat(ycbcrInfo.fFormat, ycbcrInfo);
Greg Daniel7e000222018-12-03 10:08:21 -050099}
100
101GrBackendFormat::GrBackendFormat(VkFormat vkFormat, const GrVkYcbcrConversionInfo& ycbcrInfo)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400102 : fBackend(GrBackendApi::kVulkan)
Greg Danielb4d89562018-10-03 18:44:49 +0000103#ifdef SK_VULKAN
Robert Phillipsfc711a22018-02-13 17:03:00 -0500104 , fValid(true)
Greg Danielb4d89562018-10-03 18:44:49 +0000105#else
Greg Daniel4065d452018-11-16 15:43:41 -0500106 , fValid(false)
Greg Danielb4d89562018-10-03 18:44:49 +0000107#endif
Greg Daniel4065d452018-11-16 15:43:41 -0500108 , fTextureType(GrTextureType::k2D) {
Greg Daniel7e000222018-12-03 10:08:21 -0500109 fVk.fFormat = vkFormat;
110 fVk.fYcbcrConversionInfo = ycbcrInfo;
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700111 if (fVk.fYcbcrConversionInfo.isValid() && fVk.fYcbcrConversionInfo.fExternalFormat) {
Greg Daniel387ec9a2019-03-07 16:44:54 -0500112 fTextureType = GrTextureType::kExternal;
113 }
Robert Phillipsfc711a22018-02-13 17:03:00 -0500114}
115
Brian Salomond4764a12019-08-08 12:08:24 -0400116bool GrBackendFormat::asVkFormat(VkFormat* format) const {
117 SkASSERT(format);
Greg Danielbdf12ad2018-10-12 09:31:11 -0400118 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400119 *format = fVk.fFormat;
120 return true;
Greg Daniel7e000222018-12-03 10:08:21 -0500121 }
Brian Salomond4764a12019-08-08 12:08:24 -0400122 return false;
Greg Daniel7e000222018-12-03 10:08:21 -0500123}
124
125const GrVkYcbcrConversionInfo* GrBackendFormat::getVkYcbcrConversionInfo() const {
126 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
127 return &fVk.fYcbcrConversionInfo;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500128 }
129 return nullptr;
130}
Robert Phillipsfc711a22018-02-13 17:03:00 -0500131
Stephen White985741a2019-07-18 11:43:45 -0400132#ifdef SK_DAWN
Stephen White3cc8d4f2019-10-30 09:56:23 -0400133GrBackendFormat::GrBackendFormat(wgpu::TextureFormat format)
Stephen White985741a2019-07-18 11:43:45 -0400134 : fBackend(GrBackendApi::kDawn)
135 , fValid(true)
136 , fDawnFormat(format)
137 , fTextureType(GrTextureType::k2D) {
138}
139
Stephen White3cc8d4f2019-10-30 09:56:23 -0400140bool GrBackendFormat::asDawnFormat(wgpu::TextureFormat* format) const {
Brian Salomond4764a12019-08-08 12:08:24 -0400141 SkASSERT(format);
Stephen White985741a2019-07-18 11:43:45 -0400142 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400143 *format = fDawnFormat;
144 return true;
Stephen White985741a2019-07-18 11:43:45 -0400145 }
Brian Salomond4764a12019-08-08 12:08:24 -0400146 return false;
Stephen White985741a2019-07-18 11:43:45 -0400147}
148#endif
149
Timothy Liang4e85e802018-06-28 16:37:18 -0400150#ifdef SK_METAL
151GrBackendFormat::GrBackendFormat(GrMTLPixelFormat mtlFormat)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400152 : fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400153 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500154 , fMtlFormat(mtlFormat)
155 , fTextureType(GrTextureType::k2D) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400156}
157
Brian Salomond4764a12019-08-08 12:08:24 -0400158GrMTLPixelFormat GrBackendFormat::asMtlFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400159 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400160 return fMtlFormat;
Timothy Liang4e85e802018-06-28 16:37:18 -0400161 }
Brian Salomond4764a12019-08-08 12:08:24 -0400162 // MTLPixelFormatInvalid == 0
163 return GrMTLPixelFormat(0);
Timothy Liang4e85e802018-06-28 16:37:18 -0400164}
165#endif
166
Greg Daniele877dce2019-07-11 10:52:43 -0400167GrBackendFormat::GrBackendFormat(GrColorType colorType)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400168 : fBackend(GrBackendApi::kMock)
Robert Phillipsfc711a22018-02-13 17:03:00 -0500169 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500170 , fTextureType(GrTextureType::k2D) {
Greg Daniele877dce2019-07-11 10:52:43 -0400171 fMockColorType = colorType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500172}
173
Brian Salomond4764a12019-08-08 12:08:24 -0400174GrColorType GrBackendFormat::asMockColorType() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400175 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400176 return fMockColorType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500177 }
Brian Salomond4764a12019-08-08 12:08:24 -0400178 return GrColorType::kUnknown;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500179}
180
Greg Daniel4065d452018-11-16 15:43:41 -0500181GrBackendFormat GrBackendFormat::makeTexture2D() const {
Greg Daniel4065d452018-11-16 15:43:41 -0500182 GrBackendFormat copy = *this;
Greg Daniel387ec9a2019-03-07 16:44:54 -0500183 if (const GrVkYcbcrConversionInfo* ycbcrInfo = this->getVkYcbcrConversionInfo()) {
184 if (ycbcrInfo->isValid()) {
185 // If we have a ycbcr we remove it from the backend format and set the VkFormat to
186 // R8G8B8A8_UNORM
187 SkASSERT(copy.fBackend == GrBackendApi::kVulkan);
188 copy.fVk.fYcbcrConversionInfo = GrVkYcbcrConversionInfo();
189 copy.fVk.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
190 }
191 }
Greg Daniel4065d452018-11-16 15:43:41 -0500192 copy.fTextureType = GrTextureType::k2D;
193 return copy;
194}
195
Greg Daniel45723ac2018-11-30 10:12:43 -0500196bool GrBackendFormat::operator==(const GrBackendFormat& that) const {
197 // Invalid GrBackendFormats are never equal to anything.
198 if (!fValid || !that.fValid) {
199 return false;
200 }
201
202 if (fBackend != that.fBackend) {
203 return false;
204 }
205
206 switch (fBackend) {
John Rosascoa9b348f2019-11-08 13:18:15 -0800207#ifdef SK_GL
Greg Daniel45723ac2018-11-30 10:12:43 -0500208 case GrBackendApi::kOpenGL:
209 return fGLFormat == that.fGLFormat;
John Rosascoa9b348f2019-11-08 13:18:15 -0800210 break;
211#endif
Robert Phillips0efc01d2019-11-06 17:19:30 +0000212#ifdef SK_VULKAN
John Rosascoa9b348f2019-11-08 13:18:15 -0800213 case GrBackendApi::kVulkan:
Greg Daniel45723ac2018-11-30 10:12:43 -0500214 return fVk.fFormat == that.fVk.fFormat &&
215 fVk.fYcbcrConversionInfo == that.fVk.fYcbcrConversionInfo;
Robert Phillips0efc01d2019-11-06 17:19:30 +0000216 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800217#endif
Greg Daniel45723ac2018-11-30 10:12:43 -0500218#ifdef SK_METAL
219 case GrBackendApi::kMetal:
220 return fMtlFormat == that.fMtlFormat;
Robert Phillips0efc01d2019-11-06 17:19:30 +0000221 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800222#endif
Robert Phillips0efc01d2019-11-06 17:19:30 +0000223#ifdef SK_DAWN
John Rosascoa9b348f2019-11-08 13:18:15 -0800224 case GrBackendApi::kDawn:
Stephen White985741a2019-07-18 11:43:45 -0400225 return fDawnFormat == that.fDawnFormat;
Robert Phillips0efc01d2019-11-06 17:19:30 +0000226 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800227#endif
Greg Daniel45723ac2018-11-30 10:12:43 -0500228 case GrBackendApi::kMock:
Greg Daniele877dce2019-07-11 10:52:43 -0400229 return fMockColorType == that.fMockColorType;
Greg Daniel45723ac2018-11-30 10:12:43 -0500230 default:
231 SK_ABORT("Unknown GrBackend");
232 }
233 return false;
234}
235
Robert Phillipsbac46722019-08-01 15:09:17 -0400236#if GR_TEST_UTILS
237#include "include/core/SkString.h"
238#include "src/gpu/GrTestUtils.h"
239
240#ifdef SK_GL
241#include "src/gpu/gl/GrGLUtil.h"
242#endif
243#ifdef SK_VULKAN
244#include "src/gpu/vk/GrVkUtil.h"
245#endif
246
247SkString GrBackendFormat::toStr() const {
248 SkString str;
249
250 if (!fValid) {
251 str.append("invalid");
252 return str;
253 }
254
255 str.appendf("%s-", GrBackendApiToStr(fBackend));
256
257 switch (fBackend) {
258 case GrBackendApi::kOpenGL:
259#ifdef SK_GL
260 str.append(GrGLFormatToStr(fGLFormat));
261#endif
262 break;
263 case GrBackendApi::kVulkan:
264#ifdef SK_VULKAN
265 str.append(GrVkFormatToStr(fVk.fFormat));
266#endif
267 break;
268 case GrBackendApi::kMetal:
269#ifdef SK_METAL
270 str.append(GrMtlFormatToStr(fMtlFormat));
271#endif
272 break;
273 case GrBackendApi::kDawn:
274#ifdef SK_DAWN
Stephen Whited7325182019-08-02 17:22:59 -0400275 str.append(GrDawnFormatToStr(fDawnFormat));
Robert Phillipsbac46722019-08-01 15:09:17 -0400276#endif
277 break;
278 case GrBackendApi::kMock:
279 str.append(GrColorTypeToStr(fMockColorType));
280 break;
281 }
282
283 return str;
284}
285#endif
286
287///////////////////////////////////////////////////////////////////////////////////////////////////
Stephen White985741a2019-07-18 11:43:45 -0400288#ifdef SK_DAWN
289GrBackendTexture::GrBackendTexture(int width,
290 int height,
291 const GrDawnImageInfo& dawnInfo)
292 : fIsValid(true)
293 , fWidth(width)
294 , fHeight(height)
Stephen White985741a2019-07-18 11:43:45 -0400295 , fMipMapped(GrMipMapped(dawnInfo.fLevelCount > 1))
296 , fBackend(GrBackendApi::kDawn)
297 , fDawnInfo(dawnInfo) {}
298#endif
299
Brian Salomon4456a0d2019-07-18 15:05:11 -0400300GrBackendTexture::GrBackendTexture(int width, int height, const GrVkImageInfo& vkInfo)
Greg Danielb4d89562018-10-03 18:44:49 +0000301#ifdef SK_VULKAN
Brian Salomon4456a0d2019-07-18 15:05:11 -0400302 : GrBackendTexture(width, height, vkInfo,
Greg Daniel52e16d92018-04-10 09:34:07 -0400303 sk_sp<GrVkImageLayout>(new GrVkImageLayout(vkInfo.fImageLayout))) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000304#else
305 : fIsValid(false) {}
306#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400307
Brian Salomone2826ab2019-06-04 15:58:31 -0400308#ifdef SK_GL
309GrBackendTexture::GrBackendTexture(int width,
310 int height,
311 GrMipMapped mipMapped,
312 const GrGLTextureInfo glInfo,
313 sk_sp<GrGLTextureParameters> params)
314 : fIsValid(true)
315 , fWidth(width)
316 , fHeight(height)
Brian Salomone2826ab2019-06-04 15:58:31 -0400317 , fMipMapped(mipMapped)
318 , fBackend(GrBackendApi::kOpenGL)
319 , fGLInfo(glInfo, params.release()) {}
320
321sk_sp<GrGLTextureParameters> GrBackendTexture::getGLTextureParams() const {
322 if (fBackend != GrBackendApi::kOpenGL) {
323 return nullptr;
324 }
325 return fGLInfo.refParameters();
326}
327#endif
328
Greg Danielb4d89562018-10-03 18:44:49 +0000329#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400330GrBackendTexture::GrBackendTexture(int width,
331 int height,
332 const GrVkImageInfo& vkInfo,
333 sk_sp<GrVkImageLayout> layout)
Greg Daniel9ca30652018-04-06 09:27:20 -0400334 : fIsValid(true)
335 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400336 , fHeight(height)
Chris Dalton3b51df12017-11-27 14:33:06 -0700337 , fMipMapped(GrMipMapped(vkInfo.fLevelCount > 1))
Greg Danielbdf12ad2018-10-12 09:31:11 -0400338 , fBackend(GrBackendApi::kVulkan)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400339 , fVkInfo(vkInfo, layout.release()) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000340#endif
Greg Daniel94403452017-04-18 15:52:36 -0400341
Timothy Liang4e85e802018-06-28 16:37:18 -0400342#ifdef SK_METAL
343GrBackendTexture::GrBackendTexture(int width,
344 int height,
345 GrMipMapped mipMapped,
346 const GrMtlTextureInfo& mtlInfo)
347 : fIsValid(true)
348 , fWidth(width)
349 , fHeight(height)
Timothy Liang4e85e802018-06-28 16:37:18 -0400350 , fMipMapped(mipMapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400351 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400352 , fMtlInfo(mtlInfo) {}
353#endif
354
John Rosascoa9b348f2019-11-08 13:18:15 -0800355#ifdef SK_GL
Brian Salomon8fe24272017-07-07 12:56:11 -0400356GrBackendTexture::GrBackendTexture(int width,
357 int height,
Greg Daniele7d8da42017-12-04 11:23:19 -0500358 GrMipMapped mipMapped,
359 const GrGLTextureInfo& glInfo)
Brian Salomone2826ab2019-06-04 15:58:31 -0400360 : GrBackendTexture(width, height, mipMapped, glInfo, sk_make_sp<GrGLTextureParameters>()) {
361 // Make no assumptions about client's texture's parameters.
362 this->glTextureParametersModified();
363}
John Rosascoa9b348f2019-11-08 13:18:15 -0800364#endif
Greg Daniele7d8da42017-12-04 11:23:19 -0500365
366GrBackendTexture::GrBackendTexture(int width,
367 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400368 GrMipMapped mipMapped,
369 const GrMockTextureInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400370 : fIsValid(true)
371 , fWidth(width)
Brian Salomon8fe24272017-07-07 12:56:11 -0400372 , fHeight(height)
Greg Daniel177e6952017-10-12 12:27:11 -0400373 , fMipMapped(mipMapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400374 , fBackend(GrBackendApi::kMock)
Brian Salomon8fe24272017-07-07 12:56:11 -0400375 , fMockInfo(mockInfo) {}
376
Greg Daniel52e16d92018-04-10 09:34:07 -0400377GrBackendTexture::~GrBackendTexture() {
378 this->cleanup();
379}
380
381void GrBackendTexture::cleanup() {
Brian Salomone2826ab2019-06-04 15:58:31 -0400382#ifdef SK_GL
383 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
384 fGLInfo.cleanup();
385 }
386#endif
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000387#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400388 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400389 fVkInfo.cleanup();
390 }
391#endif
392}
393
394GrBackendTexture::GrBackendTexture(const GrBackendTexture& that) : fIsValid(false) {
395 *this = that;
396}
397
398GrBackendTexture& GrBackendTexture::operator=(const GrBackendTexture& that) {
399 if (!that.isValid()) {
400 this->cleanup();
401 fIsValid = false;
402 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -0400403 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400404 this->cleanup();
405 fIsValid = false;
Greg Daniel52e16d92018-04-10 09:34:07 -0400406 }
407 fWidth = that.fWidth;
408 fHeight = that.fHeight;
Greg Daniel52e16d92018-04-10 09:34:07 -0400409 fMipMapped = that.fMipMapped;
410 fBackend = that.fBackend;
411
412 switch (that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400413#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400414 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400415 fGLInfo.assign(that.fGLInfo, this->isValid());
Greg Daniel52e16d92018-04-10 09:34:07 -0400416 break;
Mike Kleina55e2142018-10-03 16:34:11 +0000417#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400418#ifdef SK_VULKAN
419 case GrBackendApi::kVulkan:
420 fVkInfo.assign(that.fVkInfo, this->isValid());
Greg Danielb4d89562018-10-03 18:44:49 +0000421 break;
Brian Salomone2826ab2019-06-04 15:58:31 -0400422#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400423#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400424 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000425 fMtlInfo = that.fMtlInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400426 break;
427#endif
Stephen White985741a2019-07-18 11:43:45 -0400428#ifdef SK_DAWN
429 case GrBackendApi::kDawn:
430 fDawnInfo = that.fDawnInfo;
431 break;
432#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400433 case GrBackendApi::kMock:
Greg Daniel52e16d92018-04-10 09:34:07 -0400434 fMockInfo = that.fMockInfo;
435 break;
436 default:
437 SK_ABORT("Unknown GrBackend");
438 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400439 fIsValid = true;
Greg Daniel52e16d92018-04-10 09:34:07 -0400440 return *this;
441}
442
Stephen White985741a2019-07-18 11:43:45 -0400443#ifdef SK_DAWN
444bool GrBackendTexture::getDawnImageInfo(GrDawnImageInfo* outInfo) const {
445 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
446 *outInfo = fDawnInfo;
447 return true;
448 }
449 return false;
450}
451#endif
452
Mike Kleina55e2142018-10-03 16:34:11 +0000453bool GrBackendTexture::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielb4d89562018-10-03 18:44:49 +0000454#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400455 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400456 *outInfo = fVkInfo.snapImageInfo();
457 return true;
458 }
Greg Danielb4d89562018-10-03 18:44:49 +0000459#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400460 return false;
461}
462
463void GrBackendTexture::setVkImageLayout(VkImageLayout layout) {
Greg Danielb4d89562018-10-03 18:44:49 +0000464#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400465 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400466 fVkInfo.setImageLayout(layout);
467 }
Greg Danielb4d89562018-10-03 18:44:49 +0000468#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400469}
470
Greg Danielb4d89562018-10-03 18:44:49 +0000471#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400472sk_sp<GrVkImageLayout> GrBackendTexture::getGrVkImageLayout() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400473 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400474 return fVkInfo.getGrVkImageLayout();
Greg Daniel94403452017-04-18 15:52:36 -0400475 }
476 return nullptr;
477}
Brian Salomone2826ab2019-06-04 15:58:31 -0400478#endif
Greg Daniel94403452017-04-18 15:52:36 -0400479
Timothy Liang4e85e802018-06-28 16:37:18 -0400480#ifdef SK_METAL
481bool GrBackendTexture::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400482 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000483 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400484 return true;
485 }
486 return false;
487}
488#endif
489
Greg Daniel52e16d92018-04-10 09:34:07 -0400490bool GrBackendTexture::getGLTextureInfo(GrGLTextureInfo* outInfo) const {
Brian Salomone2826ab2019-06-04 15:58:31 -0400491#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400492 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400493 *outInfo = fGLInfo.info();
Greg Daniel52e16d92018-04-10 09:34:07 -0400494 return true;
Brian Osmana7ef3a02019-03-27 15:54:14 -0400495 } else if (this->isValid() && GrBackendApi::kMock == fBackend) {
496 // Hack! This allows some blink unit tests to work when using the Mock GrContext.
497 // Specifically, tests that rely on CanvasResourceProviderTextureGpuMemoryBuffer.
498 // If that code ever goes away (or ideally becomes backend-agnostic), this can go away.
499 *outInfo = GrGLTextureInfo{ GR_GL_TEXTURE_2D,
500 static_cast<GrGLuint>(fMockInfo.fID),
501 GR_GL_RGBA8 };
502 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400503 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400504#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400505 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400506}
507
Brian Salomone2826ab2019-06-04 15:58:31 -0400508void GrBackendTexture::glTextureParametersModified() {
509#ifdef SK_GL
510 if (this->isValid() && fBackend == GrBackendApi::kOpenGL) {
511 fGLInfo.parameters()->invalidate();
512 }
513#endif
514}
515
Greg Daniel52e16d92018-04-10 09:34:07 -0400516bool GrBackendTexture::getMockTextureInfo(GrMockTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400517 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400518 *outInfo = fMockInfo;
519 return true;
Brian Salomon8fe24272017-07-07 12:56:11 -0400520 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400521 return false;
Brian Salomon8fe24272017-07-07 12:56:11 -0400522}
523
Brian Salomon4456a0d2019-07-18 15:05:11 -0400524bool GrBackendTexture::isProtected() const {
525 if (!this->isValid() || this->backend() != GrBackendApi::kVulkan) {
526 return false;
527 }
528 return fVkInfo.isProtected();
529}
530
Brian Salomonaad83152019-05-24 10:16:35 -0400531bool GrBackendTexture::isSameTexture(const GrBackendTexture& that) {
532 if (!this->isValid() || !that.isValid()) {
533 return false;
534 }
535 if (fBackend != that.fBackend) {
536 return false;
537 }
538 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400539#ifdef SK_GL
Brian Salomonaad83152019-05-24 10:16:35 -0400540 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400541 return fGLInfo.info().fID == that.fGLInfo.info().fID;
542#endif
Brian Salomonaad83152019-05-24 10:16:35 -0400543#ifdef SK_VULKAN
544 case GrBackendApi::kVulkan:
545 return fVkInfo.snapImageInfo().fImage == that.fVkInfo.snapImageInfo().fImage;
546#endif
547#ifdef SK_METAL
548 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000549 return this->fMtlInfo.fTexture == that.fMtlInfo.fTexture;
Brian Salomonaad83152019-05-24 10:16:35 -0400550#endif
551 case GrBackendApi::kMock:
552 return fMockInfo.fID == that.fMockInfo.fID;
553 default:
554 return false;
555 }
556}
557
Brian Salomonf391d0f2018-12-14 09:18:50 -0500558GrBackendFormat GrBackendTexture::getBackendFormat() const {
559 if (!this->isValid()) {
560 return GrBackendFormat();
561 }
562 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400563#ifdef SK_GL
Brian Salomonf391d0f2018-12-14 09:18:50 -0500564 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400565 return GrBackendFormat::MakeGL(fGLInfo.info().fFormat, fGLInfo.info().fTarget);
566#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500567#ifdef SK_VULKAN
568 case GrBackendApi::kVulkan: {
569 auto info = fVkInfo.snapImageInfo();
570 if (info.fYcbcrConversionInfo.isValid()) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700571 SkASSERT(info.fFormat == info.fYcbcrConversionInfo.fFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500572 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
573 }
574 return GrBackendFormat::MakeVk(info.fFormat);
575 }
576#endif
Stephen White985741a2019-07-18 11:43:45 -0400577#ifdef SK_DAWN
578 case GrBackendApi::kDawn: {
579 return GrBackendFormat::MakeDawn(fDawnInfo.fFormat);
580 }
581#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500582#ifdef SK_METAL
583 case GrBackendApi::kMetal: {
584 GrMtlTextureInfo mtlInfo;
585 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
586 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
587 }
588#endif
589 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400590 return fMockInfo.getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500591 default:
592 return GrBackendFormat();
593 }
594}
595
Robert Phillipsc5509952018-04-04 15:54:55 -0400596#if GR_TEST_UTILS
597bool GrBackendTexture::TestingOnly_Equals(const GrBackendTexture& t0, const GrBackendTexture& t1) {
598 if (!t0.isValid() || !t1.isValid()) {
599 return false; // two invalid backend textures are not considered equal
600 }
601
602 if (t0.fWidth != t1.fWidth ||
603 t0.fHeight != t1.fHeight ||
Robert Phillipsc5509952018-04-04 15:54:55 -0400604 t0.fMipMapped != t1.fMipMapped ||
605 t0.fBackend != t1.fBackend) {
606 return false;
607 }
608
609 switch (t0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400610#ifdef SK_GL
611 case GrBackendApi::kOpenGL:
612 return t0.fGLInfo.info() == t1.fGLInfo.info();
613#endif
614 case GrBackendApi::kMock:
615 return t0.fMockInfo == t1.fMockInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400616#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400617 case GrBackendApi::kVulkan:
618 return t0.fVkInfo == t1.fVkInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400619#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400620#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -0400621 case GrBackendApi::kMetal:
622 return t0.fMtlInfo == t1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400623#endif
Stephen White985741a2019-07-18 11:43:45 -0400624#ifdef SK_DAWN
625 case GrBackendApi::kDawn:
626 return t0.fDawnInfo == t1.fDawnInfo;
627#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400628 default:
629 return false;
Robert Phillipsc5509952018-04-04 15:54:55 -0400630 }
Robert Phillipsc5509952018-04-04 15:54:55 -0400631}
632#endif
633
Greg Daniel94403452017-04-18 15:52:36 -0400634////////////////////////////////////////////////////////////////////////////////////////////////////
635
Stephen White985741a2019-07-18 11:43:45 -0400636#ifdef SK_DAWN
637GrBackendRenderTarget::GrBackendRenderTarget(int width,
638 int height,
639 int sampleCnt,
640 int stencilBits,
641 const GrDawnImageInfo& dawnInfo)
642 : fIsValid(true)
643 , fWidth(width)
644 , fHeight(height)
645 , fSampleCnt(sampleCnt)
646 , fStencilBits(stencilBits)
Stephen White985741a2019-07-18 11:43:45 -0400647 , fBackend(GrBackendApi::kDawn)
648 , fDawnInfo(dawnInfo) {}
649#endif
650
Greg Daniel94403452017-04-18 15:52:36 -0400651GrBackendRenderTarget::GrBackendRenderTarget(int width,
652 int height,
653 int sampleCnt,
654 int stencilBits,
Greg Danielbcf612b2017-05-01 13:50:58 +0000655 const GrVkImageInfo& vkInfo)
Brian Salomon4456a0d2019-07-18 15:05:11 -0400656 : GrBackendRenderTarget(width, height, sampleCnt, vkInfo) {
Brian Salomonafdc6b12018-03-09 12:02:32 -0500657 // This is a deprecated constructor that takes a bogus stencil bits.
658 SkASSERT(0 == stencilBits);
659}
660
661GrBackendRenderTarget::GrBackendRenderTarget(int width,
662 int height,
663 int sampleCnt,
664 const GrVkImageInfo& vkInfo)
Greg Danielb4d89562018-10-03 18:44:49 +0000665#ifdef SK_VULKAN
Brian Salomon4456a0d2019-07-18 15:05:11 -0400666 : GrBackendRenderTarget(width, height, sampleCnt, vkInfo,
Greg Daniel323fbcf2018-04-10 13:46:30 -0400667 sk_sp<GrVkImageLayout>(new GrVkImageLayout(vkInfo.fImageLayout))) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000668#else
669 : fIsValid(false) {}
670#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400671
Greg Danielb4d89562018-10-03 18:44:49 +0000672#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400673GrBackendRenderTarget::GrBackendRenderTarget(int width,
674 int height,
675 int sampleCnt,
676 const GrVkImageInfo& vkInfo,
677 sk_sp<GrVkImageLayout> layout)
Greg Daniel9ca30652018-04-06 09:27:20 -0400678 : fIsValid(true)
679 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400680 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500681 , fSampleCnt(SkTMax(1, sampleCnt))
Brian Salomonafdc6b12018-03-09 12:02:32 -0500682 , fStencilBits(0) // We always create stencil buffers internally for vulkan
Greg Danielbdf12ad2018-10-12 09:31:11 -0400683 , fBackend(GrBackendApi::kVulkan)
Greg Daniel323fbcf2018-04-10 13:46:30 -0400684 , fVkInfo(vkInfo, layout.release()) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000685#endif
Greg Daniel94403452017-04-18 15:52:36 -0400686
Timothy Liang4e85e802018-06-28 16:37:18 -0400687#ifdef SK_METAL
688GrBackendRenderTarget::GrBackendRenderTarget(int width,
689 int height,
690 int sampleCnt,
691 const GrMtlTextureInfo& mtlInfo)
692 : fIsValid(true)
693 , fWidth(width)
694 , fHeight(height)
695 , fSampleCnt(SkTMax(1, sampleCnt))
696 , fStencilBits(0)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400697 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400698 , fMtlInfo(mtlInfo) {}
699#endif
700
John Rosascoa9b348f2019-11-08 13:18:15 -0800701#ifdef SK_GL
Greg Danielfaa095e2017-12-19 13:15:02 -0500702GrBackendRenderTarget::GrBackendRenderTarget(int width,
703 int height,
704 int sampleCnt,
705 int stencilBits,
706 const GrGLFramebufferInfo& glInfo)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500707 : fWidth(width)
Greg Danielfaa095e2017-12-19 13:15:02 -0500708 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500709 , fSampleCnt(SkTMax(1, sampleCnt))
Greg Danielfaa095e2017-12-19 13:15:02 -0500710 , fStencilBits(stencilBits)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400711 , fBackend(GrBackendApi::kOpenGL)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500712 , fGLInfo(glInfo) {
713 fIsValid = SkToBool(glInfo.fFormat); // the glInfo must have a valid format
714}
John Rosascoa9b348f2019-11-08 13:18:15 -0800715#endif
Greg Danielfaa095e2017-12-19 13:15:02 -0500716
Brian Salomon0c51eea2018-03-09 17:02:09 -0500717GrBackendRenderTarget::GrBackendRenderTarget(int width,
718 int height,
719 int sampleCnt,
720 int stencilBits,
721 const GrMockRenderTargetInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400722 : fIsValid(true)
723 , fWidth(width)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500724 , fHeight(height)
725 , fSampleCnt(SkTMax(1, sampleCnt))
726 , fStencilBits(stencilBits)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500727 , fMockInfo(mockInfo) {}
728
Greg Daniel323fbcf2018-04-10 13:46:30 -0400729GrBackendRenderTarget::~GrBackendRenderTarget() {
730 this->cleanup();
731}
732
733void GrBackendRenderTarget::cleanup() {
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000734#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400735 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400736 fVkInfo.cleanup();
737 }
738#endif
739}
740
741GrBackendRenderTarget::GrBackendRenderTarget(const GrBackendRenderTarget& that) : fIsValid(false) {
742 *this = that;
743}
744
745GrBackendRenderTarget& GrBackendRenderTarget::operator=(const GrBackendRenderTarget& that) {
746 if (!that.isValid()) {
747 this->cleanup();
748 fIsValid = false;
749 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -0400750 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400751 this->cleanup();
752 fIsValid = false;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400753 }
754 fWidth = that.fWidth;
755 fHeight = that.fHeight;
756 fSampleCnt = that.fSampleCnt;
757 fStencilBits = that.fStencilBits;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400758 fBackend = that.fBackend;
759
760 switch (that.fBackend) {
John Rosascoa9b348f2019-11-08 13:18:15 -0800761#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400762 case GrBackendApi::kOpenGL:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400763 fGLInfo = that.fGLInfo;
764 break;
John Rosasco078cf3e2019-10-31 16:21:39 -0700765#endif
John Rosascoa9b348f2019-11-08 13:18:15 -0800766#ifdef SK_VULKAN
767 case GrBackendApi::kVulkan:
768 fVkInfo.assign(that.fVkInfo, this->isValid());
Robert Phillips0efc01d2019-11-06 17:19:30 +0000769 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800770#endif
Stephen White985741a2019-07-18 11:43:45 -0400771#ifdef SK_DAWN
772 case GrBackendApi::kDawn:
773 fDawnInfo = that.fDawnInfo;
774 break;
775#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400776#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400777 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000778 fMtlInfo = that.fMtlInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400779 break;
780#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400781 case GrBackendApi::kMock:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400782 fMockInfo = that.fMockInfo;
783 break;
784 default:
785 SK_ABORT("Unknown GrBackend");
786 }
787 fIsValid = that.fIsValid;
788 return *this;
789}
790
Stephen White985741a2019-07-18 11:43:45 -0400791#ifdef SK_DAWN
792bool GrBackendRenderTarget::getDawnImageInfo(GrDawnImageInfo* outInfo) const {
793 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
794 *outInfo = fDawnInfo;
795 return true;
796 }
797 return false;
798}
799#endif
800
Mike Kleina55e2142018-10-03 16:34:11 +0000801bool GrBackendRenderTarget::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielb4d89562018-10-03 18:44:49 +0000802#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400803 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400804 *outInfo = fVkInfo.snapImageInfo();
805 return true;
806 }
Greg Danielb4d89562018-10-03 18:44:49 +0000807#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400808 return false;
809}
810
811void GrBackendRenderTarget::setVkImageLayout(VkImageLayout layout) {
Greg Danielb4d89562018-10-03 18:44:49 +0000812#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400813 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400814 fVkInfo.setImageLayout(layout);
815 }
Greg Danielb4d89562018-10-03 18:44:49 +0000816#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400817}
818
Greg Danielb4d89562018-10-03 18:44:49 +0000819#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400820sk_sp<GrVkImageLayout> GrBackendRenderTarget::getGrVkImageLayout() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400821 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400822 return fVkInfo.getGrVkImageLayout();
Greg Daniel94403452017-04-18 15:52:36 -0400823 }
824 return nullptr;
825}
Brian Salomone2826ab2019-06-04 15:58:31 -0400826#endif
Greg Daniel94403452017-04-18 15:52:36 -0400827
Timothy Liang4e85e802018-06-28 16:37:18 -0400828#ifdef SK_METAL
829bool GrBackendRenderTarget::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400830 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000831 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400832 return true;
833 }
834 return false;
835}
836#endif
837
John Rosascoa9b348f2019-11-08 13:18:15 -0800838#ifdef SK_GL
Greg Daniel323fbcf2018-04-10 13:46:30 -0400839bool GrBackendRenderTarget::getGLFramebufferInfo(GrGLFramebufferInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400840 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400841 *outInfo = fGLInfo;
842 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400843 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400844 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400845}
John Rosascoa9b348f2019-11-08 13:18:15 -0800846#endif
Greg Daniel94403452017-04-18 15:52:36 -0400847
Robert Phillipsf209e882019-06-25 15:59:50 -0400848GrBackendFormat GrBackendRenderTarget::getBackendFormat() const {
849 if (!this->isValid()) {
850 return GrBackendFormat();
851 }
852 switch (fBackend) {
853#ifdef SK_GL
854 case GrBackendApi::kOpenGL:
855 return GrBackendFormat::MakeGL(fGLInfo.fFormat, GR_GL_TEXTURE_NONE);
856#endif
857#ifdef SK_VULKAN
858 case GrBackendApi::kVulkan: {
859 auto info = fVkInfo.snapImageInfo();
860 if (info.fYcbcrConversionInfo.isValid()) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700861 SkASSERT(info.fFormat == info.fYcbcrConversionInfo.fFormat);
Robert Phillipsf209e882019-06-25 15:59:50 -0400862 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
863 }
864 return GrBackendFormat::MakeVk(info.fFormat);
865 }
866#endif
867#ifdef SK_METAL
868 case GrBackendApi::kMetal: {
869 GrMtlTextureInfo mtlInfo;
870 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
871 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
872 }
873#endif
874 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400875 return fMockInfo.getBackendFormat();
Robert Phillipsf209e882019-06-25 15:59:50 -0400876 default:
877 return GrBackendFormat();
878 }
879}
880
Greg Daniel323fbcf2018-04-10 13:46:30 -0400881bool GrBackendRenderTarget::getMockRenderTargetInfo(GrMockRenderTargetInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400882 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400883 *outInfo = fMockInfo;
884 return true;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500885 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400886 return false;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500887}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400888
Brian Salomon4456a0d2019-07-18 15:05:11 -0400889bool GrBackendRenderTarget::isProtected() const {
890 if (!this->isValid() || this->backend() != GrBackendApi::kVulkan) {
891 return false;
892 }
893 return fVkInfo.isProtected();
894}
895
Robert Phillips8caf85f2018-04-05 09:30:38 -0400896#if GR_TEST_UTILS
897bool GrBackendRenderTarget::TestingOnly_Equals(const GrBackendRenderTarget& r0,
898 const GrBackendRenderTarget& r1) {
899 if (!r0.isValid() || !r1.isValid()) {
900 return false; // two invalid backend rendertargets are not considered equal
901 }
902
903 if (r0.fWidth != r1.fWidth ||
904 r0.fHeight != r1.fHeight ||
905 r0.fSampleCnt != r1.fSampleCnt ||
906 r0.fStencilBits != r1.fStencilBits ||
Robert Phillips8caf85f2018-04-05 09:30:38 -0400907 r0.fBackend != r1.fBackend) {
908 return false;
909 }
910
911 switch (r0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400912#ifdef SK_GL
913 case GrBackendApi::kOpenGL:
914 return r0.fGLInfo == r1.fGLInfo;
915#endif
916 case GrBackendApi::kMock:
917 return r0.fMockInfo == r1.fMockInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400918#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400919 case GrBackendApi::kVulkan:
920 return r0.fVkInfo == r1.fVkInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400921#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400922#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -0400923 case GrBackendApi::kMetal:
924 return r0.fMtlInfo == r1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400925#endif
Stephen White985741a2019-07-18 11:43:45 -0400926#ifdef SK_DAWN
927 case GrBackendApi::kDawn:
928 return r0.fDawnInfo == r1.fDawnInfo;
929#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400930 default:
931 return false;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400932 }
933
934 SkASSERT(0);
935 return false;
936}
937#endif