blob: fb297a73b6d03513b46598c5ab846c2be05411a8 [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
Robert Phillipsfc711a22018-02-13 17:03:00 -050065GrBackendFormat::GrBackendFormat(GrGLenum format, GrGLenum target)
Greg Danielbdf12ad2018-10-12 09:31:11 -040066 : fBackend(GrBackendApi::kOpenGL)
Greg Daniel4065d452018-11-16 15:43:41 -050067 , fValid(true)
68 , fGLFormat(format) {
69 switch (target) {
Robert Phillipsf209e882019-06-25 15:59:50 -040070 case GR_GL_TEXTURE_NONE:
71 fTextureType = GrTextureType::kNone;
72 break;
Greg Daniel4065d452018-11-16 15:43:41 -050073 case GR_GL_TEXTURE_2D:
74 fTextureType = GrTextureType::k2D;
75 break;
76 case GR_GL_TEXTURE_RECTANGLE:
77 fTextureType = GrTextureType::kRectangle;
78 break;
79 case GR_GL_TEXTURE_EXTERNAL:
80 fTextureType = GrTextureType::kExternal;
81 break;
82 default:
83 SK_ABORT("Unexpected texture target");
84 }
Robert Phillipsfc711a22018-02-13 17:03:00 -050085}
86
87const GrGLenum* GrBackendFormat::getGLFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -040088 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel4065d452018-11-16 15:43:41 -050089 return &fGLFormat;
Robert Phillipsfc711a22018-02-13 17:03:00 -050090 }
91 return nullptr;
92}
93
94const GrGLenum* GrBackendFormat::getGLTarget() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -040095 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Robert Phillipsf209e882019-06-25 15:59:50 -040096 static constexpr GrGLenum kNone = GR_GL_TEXTURE_NONE;
Greg Daniel4065d452018-11-16 15:43:41 -050097 static constexpr GrGLenum k2D = GR_GL_TEXTURE_2D;
98 static constexpr GrGLenum kRect = GR_GL_TEXTURE_RECTANGLE;
99 static constexpr GrGLenum kExternal = GR_GL_TEXTURE_EXTERNAL;
100 switch (fTextureType) {
Robert Phillipsf209e882019-06-25 15:59:50 -0400101 case GrTextureType::kNone:
102 return &kNone;
Greg Daniel4065d452018-11-16 15:43:41 -0500103 case GrTextureType::k2D:
104 return &k2D;
105 case GrTextureType::kRectangle:
106 return &kRect;
107 case GrTextureType::kExternal:
108 return &kExternal;
109 }
Robert Phillipsfc711a22018-02-13 17:03:00 -0500110 }
111 return nullptr;
112}
113
Greg Daniel7e000222018-12-03 10:08:21 -0500114GrBackendFormat GrBackendFormat::MakeVk(const GrVkYcbcrConversionInfo& ycbcrInfo) {
115#ifdef SK_BUILD_FOR_ANDROID
116 return GrBackendFormat(VK_FORMAT_UNDEFINED, ycbcrInfo);
117#else
118 return GrBackendFormat();
119#endif
120}
121
122GrBackendFormat::GrBackendFormat(VkFormat vkFormat, const GrVkYcbcrConversionInfo& ycbcrInfo)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400123 : fBackend(GrBackendApi::kVulkan)
Greg Danielb4d89562018-10-03 18:44:49 +0000124#ifdef SK_VULKAN
Robert Phillipsfc711a22018-02-13 17:03:00 -0500125 , fValid(true)
Greg Danielb4d89562018-10-03 18:44:49 +0000126#else
Greg Daniel4065d452018-11-16 15:43:41 -0500127 , fValid(false)
Greg Danielb4d89562018-10-03 18:44:49 +0000128#endif
Greg Daniel4065d452018-11-16 15:43:41 -0500129 , fTextureType(GrTextureType::k2D) {
Greg Daniel7e000222018-12-03 10:08:21 -0500130 fVk.fFormat = vkFormat;
131 fVk.fYcbcrConversionInfo = ycbcrInfo;
Greg Daniel387ec9a2019-03-07 16:44:54 -0500132 if (fVk.fYcbcrConversionInfo.isValid()) {
133 fTextureType = GrTextureType::kExternal;
134 }
Robert Phillipsfc711a22018-02-13 17:03:00 -0500135}
136
137const VkFormat* GrBackendFormat::getVkFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400138 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel7e000222018-12-03 10:08:21 -0500139 return &fVk.fFormat;
140 }
141 return nullptr;
142}
143
144const GrVkYcbcrConversionInfo* GrBackendFormat::getVkYcbcrConversionInfo() const {
145 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
146 return &fVk.fYcbcrConversionInfo;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500147 }
148 return nullptr;
149}
Robert Phillipsfc711a22018-02-13 17:03:00 -0500150
Stephen White985741a2019-07-18 11:43:45 -0400151#ifdef SK_DAWN
152GrBackendFormat::GrBackendFormat(dawn::TextureFormat format)
153 : fBackend(GrBackendApi::kDawn)
154 , fValid(true)
155 , fDawnFormat(format)
156 , fTextureType(GrTextureType::k2D) {
157}
158
159const dawn::TextureFormat* GrBackendFormat::getDawnFormat() const {
160 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
161 return &fDawnFormat;
162 }
163 return nullptr;
164}
165#endif
166
Timothy Liang4e85e802018-06-28 16:37:18 -0400167#ifdef SK_METAL
168GrBackendFormat::GrBackendFormat(GrMTLPixelFormat mtlFormat)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400169 : fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400170 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500171 , fMtlFormat(mtlFormat)
172 , fTextureType(GrTextureType::k2D) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400173}
174
175const GrMTLPixelFormat* GrBackendFormat::getMtlFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400176 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400177 return &fMtlFormat;
178 }
179 return nullptr;
180}
181#endif
182
Greg Daniele877dce2019-07-11 10:52:43 -0400183GrBackendFormat::GrBackendFormat(GrColorType colorType)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400184 : fBackend(GrBackendApi::kMock)
Robert Phillipsfc711a22018-02-13 17:03:00 -0500185 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500186 , fTextureType(GrTextureType::k2D) {
Greg Daniele877dce2019-07-11 10:52:43 -0400187 fMockColorType = colorType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500188}
189
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400190const GrColorType* GrBackendFormat::getMockColorType() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400191 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniele877dce2019-07-11 10:52:43 -0400192 return &fMockColorType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500193 }
194 return nullptr;
195}
196
Greg Daniel4065d452018-11-16 15:43:41 -0500197GrBackendFormat GrBackendFormat::makeTexture2D() const {
Greg Daniel4065d452018-11-16 15:43:41 -0500198 GrBackendFormat copy = *this;
Greg Daniel387ec9a2019-03-07 16:44:54 -0500199 if (const GrVkYcbcrConversionInfo* ycbcrInfo = this->getVkYcbcrConversionInfo()) {
200 if (ycbcrInfo->isValid()) {
201 // If we have a ycbcr we remove it from the backend format and set the VkFormat to
202 // R8G8B8A8_UNORM
203 SkASSERT(copy.fBackend == GrBackendApi::kVulkan);
204 copy.fVk.fYcbcrConversionInfo = GrVkYcbcrConversionInfo();
205 copy.fVk.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
206 }
207 }
Greg Daniel4065d452018-11-16 15:43:41 -0500208 copy.fTextureType = GrTextureType::k2D;
209 return copy;
210}
211
Greg Daniel45723ac2018-11-30 10:12:43 -0500212bool GrBackendFormat::operator==(const GrBackendFormat& that) const {
213 // Invalid GrBackendFormats are never equal to anything.
214 if (!fValid || !that.fValid) {
215 return false;
216 }
217
218 if (fBackend != that.fBackend) {
219 return false;
220 }
221
222 switch (fBackend) {
223 case GrBackendApi::kOpenGL:
224 return fGLFormat == that.fGLFormat;
225 case GrBackendApi::kVulkan:
226#ifdef SK_VULKAN
227 return fVk.fFormat == that.fVk.fFormat &&
228 fVk.fYcbcrConversionInfo == that.fVk.fYcbcrConversionInfo;
229#endif
230 break;
231#ifdef SK_METAL
232 case GrBackendApi::kMetal:
233 return fMtlFormat == that.fMtlFormat;
234#endif
235 break;
Stephen White985741a2019-07-18 11:43:45 -0400236 case GrBackendApi::kDawn:
237#ifdef SK_DAWN
238 return fDawnFormat == that.fDawnFormat;
239#endif
240 break;
Greg Daniel45723ac2018-11-30 10:12:43 -0500241 case GrBackendApi::kMock:
Greg Daniele877dce2019-07-11 10:52:43 -0400242 return fMockColorType == that.fMockColorType;
Greg Daniel45723ac2018-11-30 10:12:43 -0500243 default:
244 SK_ABORT("Unknown GrBackend");
245 }
246 return false;
247}
248
Robert Phillipsbac46722019-08-01 15:09:17 -0400249#if GR_TEST_UTILS
250#include "include/core/SkString.h"
251#include "src/gpu/GrTestUtils.h"
252
253#ifdef SK_GL
254#include "src/gpu/gl/GrGLUtil.h"
255#endif
256#ifdef SK_VULKAN
257#include "src/gpu/vk/GrVkUtil.h"
258#endif
259
260SkString GrBackendFormat::toStr() const {
261 SkString str;
262
263 if (!fValid) {
264 str.append("invalid");
265 return str;
266 }
267
268 str.appendf("%s-", GrBackendApiToStr(fBackend));
269
270 switch (fBackend) {
271 case GrBackendApi::kOpenGL:
272#ifdef SK_GL
273 str.append(GrGLFormatToStr(fGLFormat));
274#endif
275 break;
276 case GrBackendApi::kVulkan:
277#ifdef SK_VULKAN
278 str.append(GrVkFormatToStr(fVk.fFormat));
279#endif
280 break;
281 case GrBackendApi::kMetal:
282#ifdef SK_METAL
283 str.append(GrMtlFormatToStr(fMtlFormat));
284#endif
285 break;
286 case GrBackendApi::kDawn:
287#ifdef SK_DAWN
288 str.appendU32(fDawnFormat);
289#endif
290 break;
291 case GrBackendApi::kMock:
292 str.append(GrColorTypeToStr(fMockColorType));
293 break;
294 }
295
296 return str;
297}
298#endif
299
300///////////////////////////////////////////////////////////////////////////////////////////////////
Stephen White985741a2019-07-18 11:43:45 -0400301#ifdef SK_DAWN
302GrBackendTexture::GrBackendTexture(int width,
303 int height,
304 const GrDawnImageInfo& dawnInfo)
305 : fIsValid(true)
306 , fWidth(width)
307 , fHeight(height)
Stephen White985741a2019-07-18 11:43:45 -0400308 , fMipMapped(GrMipMapped(dawnInfo.fLevelCount > 1))
309 , fBackend(GrBackendApi::kDawn)
310 , fDawnInfo(dawnInfo) {}
311#endif
312
Brian Salomon4456a0d2019-07-18 15:05:11 -0400313GrBackendTexture::GrBackendTexture(int width, int height, const GrVkImageInfo& vkInfo)
Greg Danielb4d89562018-10-03 18:44:49 +0000314#ifdef SK_VULKAN
Brian Salomon4456a0d2019-07-18 15:05:11 -0400315 : GrBackendTexture(width, height, vkInfo,
Greg Daniel52e16d92018-04-10 09:34:07 -0400316 sk_sp<GrVkImageLayout>(new GrVkImageLayout(vkInfo.fImageLayout))) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000317#else
318 : fIsValid(false) {}
319#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400320
Brian Salomone2826ab2019-06-04 15:58:31 -0400321#ifdef SK_GL
322GrBackendTexture::GrBackendTexture(int width,
323 int height,
324 GrMipMapped mipMapped,
325 const GrGLTextureInfo glInfo,
326 sk_sp<GrGLTextureParameters> params)
327 : fIsValid(true)
328 , fWidth(width)
329 , fHeight(height)
Brian Salomone2826ab2019-06-04 15:58:31 -0400330 , fMipMapped(mipMapped)
331 , fBackend(GrBackendApi::kOpenGL)
332 , fGLInfo(glInfo, params.release()) {}
333
334sk_sp<GrGLTextureParameters> GrBackendTexture::getGLTextureParams() const {
335 if (fBackend != GrBackendApi::kOpenGL) {
336 return nullptr;
337 }
338 return fGLInfo.refParameters();
339}
340#endif
341
Greg Danielb4d89562018-10-03 18:44:49 +0000342#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400343GrBackendTexture::GrBackendTexture(int width,
344 int height,
345 const GrVkImageInfo& vkInfo,
346 sk_sp<GrVkImageLayout> layout)
Greg Daniel9ca30652018-04-06 09:27:20 -0400347 : fIsValid(true)
348 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400349 , fHeight(height)
Chris Dalton3b51df12017-11-27 14:33:06 -0700350 , fMipMapped(GrMipMapped(vkInfo.fLevelCount > 1))
Greg Danielbdf12ad2018-10-12 09:31:11 -0400351 , fBackend(GrBackendApi::kVulkan)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400352 , fVkInfo(vkInfo, layout.release()) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000353#endif
Greg Daniel94403452017-04-18 15:52:36 -0400354
Timothy Liang4e85e802018-06-28 16:37:18 -0400355#ifdef SK_METAL
356GrBackendTexture::GrBackendTexture(int width,
357 int height,
358 GrMipMapped mipMapped,
359 const GrMtlTextureInfo& mtlInfo)
360 : fIsValid(true)
361 , fWidth(width)
362 , fHeight(height)
Timothy Liang4e85e802018-06-28 16:37:18 -0400363 , fMipMapped(mipMapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400364 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400365 , fMtlInfo(mtlInfo) {}
366#endif
367
Brian Salomon8fe24272017-07-07 12:56:11 -0400368GrBackendTexture::GrBackendTexture(int width,
369 int height,
Greg Daniele7d8da42017-12-04 11:23:19 -0500370 GrMipMapped mipMapped,
371 const GrGLTextureInfo& glInfo)
Brian Salomone2826ab2019-06-04 15:58:31 -0400372 : GrBackendTexture(width, height, mipMapped, glInfo, sk_make_sp<GrGLTextureParameters>()) {
373 // Make no assumptions about client's texture's parameters.
374 this->glTextureParametersModified();
375}
Greg Daniele7d8da42017-12-04 11:23:19 -0500376
377GrBackendTexture::GrBackendTexture(int width,
378 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400379 GrMipMapped mipMapped,
380 const GrMockTextureInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400381 : fIsValid(true)
382 , fWidth(width)
Brian Salomon8fe24272017-07-07 12:56:11 -0400383 , fHeight(height)
Greg Daniel177e6952017-10-12 12:27:11 -0400384 , fMipMapped(mipMapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400385 , fBackend(GrBackendApi::kMock)
Brian Salomon8fe24272017-07-07 12:56:11 -0400386 , fMockInfo(mockInfo) {}
387
Greg Daniel52e16d92018-04-10 09:34:07 -0400388GrBackendTexture::~GrBackendTexture() {
389 this->cleanup();
390}
391
392void GrBackendTexture::cleanup() {
Brian Salomone2826ab2019-06-04 15:58:31 -0400393#ifdef SK_GL
394 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
395 fGLInfo.cleanup();
396 }
397#endif
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000398#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400399 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400400 fVkInfo.cleanup();
401 }
402#endif
403}
404
405GrBackendTexture::GrBackendTexture(const GrBackendTexture& that) : fIsValid(false) {
406 *this = that;
407}
408
409GrBackendTexture& GrBackendTexture::operator=(const GrBackendTexture& that) {
410 if (!that.isValid()) {
411 this->cleanup();
412 fIsValid = false;
413 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -0400414 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400415 this->cleanup();
416 fIsValid = false;
Greg Daniel52e16d92018-04-10 09:34:07 -0400417 }
418 fWidth = that.fWidth;
419 fHeight = that.fHeight;
Greg Daniel52e16d92018-04-10 09:34:07 -0400420 fMipMapped = that.fMipMapped;
421 fBackend = that.fBackend;
422
423 switch (that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400424#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400425 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400426 fGLInfo.assign(that.fGLInfo, this->isValid());
Greg Daniel52e16d92018-04-10 09:34:07 -0400427 break;
Mike Kleina55e2142018-10-03 16:34:11 +0000428#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400429#ifdef SK_VULKAN
430 case GrBackendApi::kVulkan:
431 fVkInfo.assign(that.fVkInfo, this->isValid());
Greg Danielb4d89562018-10-03 18:44:49 +0000432 break;
Brian Salomone2826ab2019-06-04 15:58:31 -0400433#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400434#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400435 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000436 fMtlInfo = that.fMtlInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400437 break;
438#endif
Stephen White985741a2019-07-18 11:43:45 -0400439#ifdef SK_DAWN
440 case GrBackendApi::kDawn:
441 fDawnInfo = that.fDawnInfo;
442 break;
443#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400444 case GrBackendApi::kMock:
Greg Daniel52e16d92018-04-10 09:34:07 -0400445 fMockInfo = that.fMockInfo;
446 break;
447 default:
448 SK_ABORT("Unknown GrBackend");
449 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400450 fIsValid = true;
Greg Daniel52e16d92018-04-10 09:34:07 -0400451 return *this;
452}
453
Stephen White985741a2019-07-18 11:43:45 -0400454#ifdef SK_DAWN
455bool GrBackendTexture::getDawnImageInfo(GrDawnImageInfo* outInfo) const {
456 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
457 *outInfo = fDawnInfo;
458 return true;
459 }
460 return false;
461}
462#endif
463
Mike Kleina55e2142018-10-03 16:34:11 +0000464bool GrBackendTexture::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielb4d89562018-10-03 18:44:49 +0000465#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400466 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400467 *outInfo = fVkInfo.snapImageInfo();
468 return true;
469 }
Greg Danielb4d89562018-10-03 18:44:49 +0000470#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400471 return false;
472}
473
474void GrBackendTexture::setVkImageLayout(VkImageLayout layout) {
Greg Danielb4d89562018-10-03 18:44:49 +0000475#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400476 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400477 fVkInfo.setImageLayout(layout);
478 }
Greg Danielb4d89562018-10-03 18:44:49 +0000479#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400480}
481
Greg Danielb4d89562018-10-03 18:44:49 +0000482#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400483sk_sp<GrVkImageLayout> GrBackendTexture::getGrVkImageLayout() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400484 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400485 return fVkInfo.getGrVkImageLayout();
Greg Daniel94403452017-04-18 15:52:36 -0400486 }
487 return nullptr;
488}
Brian Salomone2826ab2019-06-04 15:58:31 -0400489#endif
Greg Daniel94403452017-04-18 15:52:36 -0400490
Timothy Liang4e85e802018-06-28 16:37:18 -0400491#ifdef SK_METAL
492bool GrBackendTexture::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400493 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000494 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400495 return true;
496 }
497 return false;
498}
499#endif
500
Greg Daniel52e16d92018-04-10 09:34:07 -0400501bool GrBackendTexture::getGLTextureInfo(GrGLTextureInfo* outInfo) const {
Brian Salomone2826ab2019-06-04 15:58:31 -0400502#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400503 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400504 *outInfo = fGLInfo.info();
Greg Daniel52e16d92018-04-10 09:34:07 -0400505 return true;
Brian Osmana7ef3a02019-03-27 15:54:14 -0400506 } else if (this->isValid() && GrBackendApi::kMock == fBackend) {
507 // Hack! This allows some blink unit tests to work when using the Mock GrContext.
508 // Specifically, tests that rely on CanvasResourceProviderTextureGpuMemoryBuffer.
509 // If that code ever goes away (or ideally becomes backend-agnostic), this can go away.
510 *outInfo = GrGLTextureInfo{ GR_GL_TEXTURE_2D,
511 static_cast<GrGLuint>(fMockInfo.fID),
512 GR_GL_RGBA8 };
513 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400514 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400515#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400516 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400517}
518
Brian Salomone2826ab2019-06-04 15:58:31 -0400519void GrBackendTexture::glTextureParametersModified() {
520#ifdef SK_GL
521 if (this->isValid() && fBackend == GrBackendApi::kOpenGL) {
522 fGLInfo.parameters()->invalidate();
523 }
524#endif
525}
526
Greg Daniel52e16d92018-04-10 09:34:07 -0400527bool GrBackendTexture::getMockTextureInfo(GrMockTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400528 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400529 *outInfo = fMockInfo;
530 return true;
Brian Salomon8fe24272017-07-07 12:56:11 -0400531 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400532 return false;
Brian Salomon8fe24272017-07-07 12:56:11 -0400533}
534
Brian Salomon4456a0d2019-07-18 15:05:11 -0400535bool GrBackendTexture::isProtected() const {
536 if (!this->isValid() || this->backend() != GrBackendApi::kVulkan) {
537 return false;
538 }
539 return fVkInfo.isProtected();
540}
541
Brian Salomonaad83152019-05-24 10:16:35 -0400542bool GrBackendTexture::isSameTexture(const GrBackendTexture& that) {
543 if (!this->isValid() || !that.isValid()) {
544 return false;
545 }
546 if (fBackend != that.fBackend) {
547 return false;
548 }
549 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400550#ifdef SK_GL
Brian Salomonaad83152019-05-24 10:16:35 -0400551 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400552 return fGLInfo.info().fID == that.fGLInfo.info().fID;
553#endif
Brian Salomonaad83152019-05-24 10:16:35 -0400554#ifdef SK_VULKAN
555 case GrBackendApi::kVulkan:
556 return fVkInfo.snapImageInfo().fImage == that.fVkInfo.snapImageInfo().fImage;
557#endif
558#ifdef SK_METAL
559 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000560 return this->fMtlInfo.fTexture == that.fMtlInfo.fTexture;
Brian Salomonaad83152019-05-24 10:16:35 -0400561#endif
562 case GrBackendApi::kMock:
563 return fMockInfo.fID == that.fMockInfo.fID;
564 default:
565 return false;
566 }
567}
568
Brian Salomonf391d0f2018-12-14 09:18:50 -0500569GrBackendFormat GrBackendTexture::getBackendFormat() const {
570 if (!this->isValid()) {
571 return GrBackendFormat();
572 }
573 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400574#ifdef SK_GL
Brian Salomonf391d0f2018-12-14 09:18:50 -0500575 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400576 return GrBackendFormat::MakeGL(fGLInfo.info().fFormat, fGLInfo.info().fTarget);
577#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500578#ifdef SK_VULKAN
579 case GrBackendApi::kVulkan: {
580 auto info = fVkInfo.snapImageInfo();
581 if (info.fYcbcrConversionInfo.isValid()) {
582 SkASSERT(info.fFormat == VK_FORMAT_UNDEFINED);
583 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
584 }
585 return GrBackendFormat::MakeVk(info.fFormat);
586 }
587#endif
Stephen White985741a2019-07-18 11:43:45 -0400588#ifdef SK_DAWN
589 case GrBackendApi::kDawn: {
590 return GrBackendFormat::MakeDawn(fDawnInfo.fFormat);
591 }
592#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500593#ifdef SK_METAL
594 case GrBackendApi::kMetal: {
595 GrMtlTextureInfo mtlInfo;
596 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
597 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
598 }
599#endif
600 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400601 return fMockInfo.getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500602 default:
603 return GrBackendFormat();
604 }
605}
606
Robert Phillipsc5509952018-04-04 15:54:55 -0400607#if GR_TEST_UTILS
608bool GrBackendTexture::TestingOnly_Equals(const GrBackendTexture& t0, const GrBackendTexture& t1) {
609 if (!t0.isValid() || !t1.isValid()) {
610 return false; // two invalid backend textures are not considered equal
611 }
612
613 if (t0.fWidth != t1.fWidth ||
614 t0.fHeight != t1.fHeight ||
Robert Phillipsc5509952018-04-04 15:54:55 -0400615 t0.fMipMapped != t1.fMipMapped ||
616 t0.fBackend != t1.fBackend) {
617 return false;
618 }
619
620 switch (t0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400621#ifdef SK_GL
622 case GrBackendApi::kOpenGL:
623 return t0.fGLInfo.info() == t1.fGLInfo.info();
624#endif
625 case GrBackendApi::kMock:
626 return t0.fMockInfo == t1.fMockInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400627#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400628 case GrBackendApi::kVulkan:
629 return t0.fVkInfo == t1.fVkInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400630#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400631#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -0400632 case GrBackendApi::kMetal:
633 return t0.fMtlInfo == t1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400634#endif
Stephen White985741a2019-07-18 11:43:45 -0400635#ifdef SK_DAWN
636 case GrBackendApi::kDawn:
637 return t0.fDawnInfo == t1.fDawnInfo;
638#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400639 default:
640 return false;
Robert Phillipsc5509952018-04-04 15:54:55 -0400641 }
Robert Phillipsc5509952018-04-04 15:54:55 -0400642}
643#endif
644
Greg Daniel94403452017-04-18 15:52:36 -0400645////////////////////////////////////////////////////////////////////////////////////////////////////
646
Stephen White985741a2019-07-18 11:43:45 -0400647#ifdef SK_DAWN
648GrBackendRenderTarget::GrBackendRenderTarget(int width,
649 int height,
650 int sampleCnt,
651 int stencilBits,
652 const GrDawnImageInfo& dawnInfo)
653 : fIsValid(true)
654 , fWidth(width)
655 , fHeight(height)
656 , fSampleCnt(sampleCnt)
657 , fStencilBits(stencilBits)
Stephen White985741a2019-07-18 11:43:45 -0400658 , fBackend(GrBackendApi::kDawn)
659 , fDawnInfo(dawnInfo) {}
660#endif
661
Greg Daniel94403452017-04-18 15:52:36 -0400662GrBackendRenderTarget::GrBackendRenderTarget(int width,
663 int height,
664 int sampleCnt,
665 int stencilBits,
Greg Danielbcf612b2017-05-01 13:50:58 +0000666 const GrVkImageInfo& vkInfo)
Brian Salomon4456a0d2019-07-18 15:05:11 -0400667 : GrBackendRenderTarget(width, height, sampleCnt, vkInfo) {
Brian Salomonafdc6b12018-03-09 12:02:32 -0500668 // This is a deprecated constructor that takes a bogus stencil bits.
669 SkASSERT(0 == stencilBits);
670}
671
672GrBackendRenderTarget::GrBackendRenderTarget(int width,
673 int height,
674 int sampleCnt,
675 const GrVkImageInfo& vkInfo)
Greg Danielb4d89562018-10-03 18:44:49 +0000676#ifdef SK_VULKAN
Brian Salomon4456a0d2019-07-18 15:05:11 -0400677 : GrBackendRenderTarget(width, height, sampleCnt, vkInfo,
Greg Daniel323fbcf2018-04-10 13:46:30 -0400678 sk_sp<GrVkImageLayout>(new GrVkImageLayout(vkInfo.fImageLayout))) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000679#else
680 : fIsValid(false) {}
681#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400682
Greg Danielb4d89562018-10-03 18:44:49 +0000683#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400684GrBackendRenderTarget::GrBackendRenderTarget(int width,
685 int height,
686 int sampleCnt,
687 const GrVkImageInfo& vkInfo,
688 sk_sp<GrVkImageLayout> layout)
Greg Daniel9ca30652018-04-06 09:27:20 -0400689 : fIsValid(true)
690 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400691 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500692 , fSampleCnt(SkTMax(1, sampleCnt))
Brian Salomonafdc6b12018-03-09 12:02:32 -0500693 , fStencilBits(0) // We always create stencil buffers internally for vulkan
Greg Danielbdf12ad2018-10-12 09:31:11 -0400694 , fBackend(GrBackendApi::kVulkan)
Greg Daniel323fbcf2018-04-10 13:46:30 -0400695 , fVkInfo(vkInfo, layout.release()) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000696#endif
Greg Daniel94403452017-04-18 15:52:36 -0400697
Timothy Liang4e85e802018-06-28 16:37:18 -0400698#ifdef SK_METAL
699GrBackendRenderTarget::GrBackendRenderTarget(int width,
700 int height,
701 int sampleCnt,
702 const GrMtlTextureInfo& mtlInfo)
703 : fIsValid(true)
704 , fWidth(width)
705 , fHeight(height)
706 , fSampleCnt(SkTMax(1, sampleCnt))
707 , fStencilBits(0)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400708 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400709 , fMtlInfo(mtlInfo) {}
710#endif
711
Greg Danielfaa095e2017-12-19 13:15:02 -0500712GrBackendRenderTarget::GrBackendRenderTarget(int width,
713 int height,
714 int sampleCnt,
715 int stencilBits,
716 const GrGLFramebufferInfo& glInfo)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500717 : fWidth(width)
Greg Danielfaa095e2017-12-19 13:15:02 -0500718 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500719 , fSampleCnt(SkTMax(1, sampleCnt))
Greg Danielfaa095e2017-12-19 13:15:02 -0500720 , fStencilBits(stencilBits)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400721 , fBackend(GrBackendApi::kOpenGL)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500722 , fGLInfo(glInfo) {
723 fIsValid = SkToBool(glInfo.fFormat); // the glInfo must have a valid format
724}
Greg Danielfaa095e2017-12-19 13:15:02 -0500725
Brian Salomon0c51eea2018-03-09 17:02:09 -0500726GrBackendRenderTarget::GrBackendRenderTarget(int width,
727 int height,
728 int sampleCnt,
729 int stencilBits,
730 const GrMockRenderTargetInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400731 : fIsValid(true)
732 , fWidth(width)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500733 , fHeight(height)
734 , fSampleCnt(SkTMax(1, sampleCnt))
735 , fStencilBits(stencilBits)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500736 , fMockInfo(mockInfo) {}
737
Greg Daniel323fbcf2018-04-10 13:46:30 -0400738GrBackendRenderTarget::~GrBackendRenderTarget() {
739 this->cleanup();
740}
741
742void GrBackendRenderTarget::cleanup() {
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000743#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400744 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400745 fVkInfo.cleanup();
746 }
747#endif
748}
749
750GrBackendRenderTarget::GrBackendRenderTarget(const GrBackendRenderTarget& that) : fIsValid(false) {
751 *this = that;
752}
753
754GrBackendRenderTarget& GrBackendRenderTarget::operator=(const GrBackendRenderTarget& that) {
755 if (!that.isValid()) {
756 this->cleanup();
757 fIsValid = false;
758 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -0400759 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400760 this->cleanup();
761 fIsValid = false;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400762 }
763 fWidth = that.fWidth;
764 fHeight = that.fHeight;
765 fSampleCnt = that.fSampleCnt;
766 fStencilBits = that.fStencilBits;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400767 fBackend = that.fBackend;
768
769 switch (that.fBackend) {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400770 case GrBackendApi::kOpenGL:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400771 fGLInfo = that.fGLInfo;
772 break;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400773 case GrBackendApi::kVulkan:
Greg Danielb4d89562018-10-03 18:44:49 +0000774#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400775 fVkInfo.assign(that.fVkInfo, this->isValid());
Mike Kleina55e2142018-10-03 16:34:11 +0000776#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000777 break;
Stephen White985741a2019-07-18 11:43:45 -0400778#ifdef SK_DAWN
779 case GrBackendApi::kDawn:
780 fDawnInfo = that.fDawnInfo;
781 break;
782#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400783#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400784 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000785 fMtlInfo = that.fMtlInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400786 break;
787#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400788 case GrBackendApi::kMock:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400789 fMockInfo = that.fMockInfo;
790 break;
791 default:
792 SK_ABORT("Unknown GrBackend");
793 }
794 fIsValid = that.fIsValid;
795 return *this;
796}
797
Stephen White985741a2019-07-18 11:43:45 -0400798#ifdef SK_DAWN
799bool GrBackendRenderTarget::getDawnImageInfo(GrDawnImageInfo* outInfo) const {
800 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
801 *outInfo = fDawnInfo;
802 return true;
803 }
804 return false;
805}
806#endif
807
Mike Kleina55e2142018-10-03 16:34:11 +0000808bool GrBackendRenderTarget::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielb4d89562018-10-03 18:44:49 +0000809#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400810 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400811 *outInfo = fVkInfo.snapImageInfo();
812 return true;
813 }
Greg Danielb4d89562018-10-03 18:44:49 +0000814#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400815 return false;
816}
817
818void GrBackendRenderTarget::setVkImageLayout(VkImageLayout layout) {
Greg Danielb4d89562018-10-03 18:44:49 +0000819#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400820 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400821 fVkInfo.setImageLayout(layout);
822 }
Greg Danielb4d89562018-10-03 18:44:49 +0000823#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400824}
825
Greg Danielb4d89562018-10-03 18:44:49 +0000826#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400827sk_sp<GrVkImageLayout> GrBackendRenderTarget::getGrVkImageLayout() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400828 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400829 return fVkInfo.getGrVkImageLayout();
Greg Daniel94403452017-04-18 15:52:36 -0400830 }
831 return nullptr;
832}
Brian Salomone2826ab2019-06-04 15:58:31 -0400833#endif
Greg Daniel94403452017-04-18 15:52:36 -0400834
Timothy Liang4e85e802018-06-28 16:37:18 -0400835#ifdef SK_METAL
836bool GrBackendRenderTarget::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400837 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000838 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400839 return true;
840 }
841 return false;
842}
843#endif
844
Greg Daniel323fbcf2018-04-10 13:46:30 -0400845bool GrBackendRenderTarget::getGLFramebufferInfo(GrGLFramebufferInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400846 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400847 *outInfo = fGLInfo;
848 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400849 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400850 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400851}
852
Robert Phillipsf209e882019-06-25 15:59:50 -0400853GrBackendFormat GrBackendRenderTarget::getBackendFormat() const {
854 if (!this->isValid()) {
855 return GrBackendFormat();
856 }
857 switch (fBackend) {
858#ifdef SK_GL
859 case GrBackendApi::kOpenGL:
860 return GrBackendFormat::MakeGL(fGLInfo.fFormat, GR_GL_TEXTURE_NONE);
861#endif
862#ifdef SK_VULKAN
863 case GrBackendApi::kVulkan: {
864 auto info = fVkInfo.snapImageInfo();
865 if (info.fYcbcrConversionInfo.isValid()) {
866 SkASSERT(info.fFormat == VK_FORMAT_UNDEFINED);
867 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
868 }
869 return GrBackendFormat::MakeVk(info.fFormat);
870 }
871#endif
872#ifdef SK_METAL
873 case GrBackendApi::kMetal: {
874 GrMtlTextureInfo mtlInfo;
875 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
876 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
877 }
878#endif
879 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400880 return fMockInfo.getBackendFormat();
Robert Phillipsf209e882019-06-25 15:59:50 -0400881 default:
882 return GrBackendFormat();
883 }
884}
885
Greg Daniel323fbcf2018-04-10 13:46:30 -0400886bool GrBackendRenderTarget::getMockRenderTargetInfo(GrMockRenderTargetInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400887 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400888 *outInfo = fMockInfo;
889 return true;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500890 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400891 return false;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500892}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400893
Brian Salomon4456a0d2019-07-18 15:05:11 -0400894bool GrBackendRenderTarget::isProtected() const {
895 if (!this->isValid() || this->backend() != GrBackendApi::kVulkan) {
896 return false;
897 }
898 return fVkInfo.isProtected();
899}
900
Robert Phillips8caf85f2018-04-05 09:30:38 -0400901#if GR_TEST_UTILS
902bool GrBackendRenderTarget::TestingOnly_Equals(const GrBackendRenderTarget& r0,
903 const GrBackendRenderTarget& r1) {
904 if (!r0.isValid() || !r1.isValid()) {
905 return false; // two invalid backend rendertargets are not considered equal
906 }
907
908 if (r0.fWidth != r1.fWidth ||
909 r0.fHeight != r1.fHeight ||
910 r0.fSampleCnt != r1.fSampleCnt ||
911 r0.fStencilBits != r1.fStencilBits ||
Robert Phillips8caf85f2018-04-05 09:30:38 -0400912 r0.fBackend != r1.fBackend) {
913 return false;
914 }
915
916 switch (r0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400917#ifdef SK_GL
918 case GrBackendApi::kOpenGL:
919 return r0.fGLInfo == r1.fGLInfo;
920#endif
921 case GrBackendApi::kMock:
922 return r0.fMockInfo == r1.fMockInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400923#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400924 case GrBackendApi::kVulkan:
925 return r0.fVkInfo == r1.fVkInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400926#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400927#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -0400928 case GrBackendApi::kMetal:
929 return r0.fMtlInfo == r1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400930#endif
Stephen White985741a2019-07-18 11:43:45 -0400931#ifdef SK_DAWN
932 case GrBackendApi::kDawn:
933 return r0.fDawnInfo == r1.fDawnInfo;
934#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400935 default:
936 return false;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400937 }
938
939 SkASSERT(0);
940 return false;
941}
942#endif