blob: fbbcf9e974158465c095a2b5c8d0ec3bb016583c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrBackendSurface.h"
Greg Daniel94403452017-04-18 15:52:36 -04009
Brian Salomond6584bd2021-05-18 16:29:09 -040010#include "include/private/GrTypesPriv.h"
Greg Daniel6c6caf42020-05-29 12:11:05 -040011#include "src/gpu/GrBackendSurfaceMutableStateImpl.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/gl/GrGLUtil.h"
Greg Daniele7d8da42017-12-04 11:23:19 -050013
Stephen White985741a2019-07-18 11:43:45 -040014#ifdef SK_DAWN
15#include "include/gpu/dawn/GrDawnTypes.h"
16#include "src/gpu/dawn/GrDawnUtil.h"
17#endif
18
Greg Daniel94403452017-04-18 15:52:36 -040019#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/gpu/vk/GrVkTypes.h"
21#include "src/gpu/vk/GrVkImageLayout.h"
22#include "src/gpu/vk/GrVkUtil.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000023#endif
Timothy Liang4e85e802018-06-28 16:37:18 -040024#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/gpu/mtl/GrMtlTypes.h"
26#include "src/gpu/mtl/GrMtlCppUtil.h"
Timothy Liang4e85e802018-06-28 16:37:18 -040027#endif
Jim Van Verthc86c83c2020-02-27 15:48:24 -050028#ifdef SK_DIRECT3D
Jim Van Verth05d09192020-03-20 11:23:39 -040029#include "include/gpu/d3d/GrD3DTypes.h"
30#include "src/gpu/d3d/GrD3DResourceState.h"
31#include "src/gpu/d3d/GrD3DUtil.h"
Jim Van Verthc86c83c2020-02-27 15:48:24 -050032#endif
Greg Daniel7ef28f32017-04-20 16:41:55 +000033
Robert Phillipsb2adbef2019-07-02 16:33:05 -040034GrBackendFormat::GrBackendFormat(const GrBackendFormat& that)
35 : fBackend(that.fBackend)
36 , fValid(that.fValid)
37 , fTextureType(that.fTextureType) {
38 if (!fValid) {
39 return;
40 }
41
42 switch (fBackend) {
43#ifdef SK_GL
44 case GrBackendApi::kOpenGL:
45 fGLFormat = that.fGLFormat;
46 break;
47#endif
48#ifdef SK_VULKAN
49 case GrBackendApi::kVulkan:
50 fVk = that.fVk;
51 break;
52#endif
53#ifdef SK_METAL
54 case GrBackendApi::kMetal:
55 fMtlFormat = that.fMtlFormat;
56 break;
57#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -050058#ifdef SK_DIRECT3D
59 case GrBackendApi::kDirect3D:
Jim Van Verthc86c83c2020-02-27 15:48:24 -050060 fDxgiFormat = that.fDxgiFormat;
Jim Van Verthb01e12b2020-02-18 14:34:38 -050061 break;
62#endif
Stephen White985741a2019-07-18 11:43:45 -040063#ifdef SK_DAWN
64 case GrBackendApi::kDawn:
65 fDawnFormat = that.fDawnFormat;
66 break;
67#endif
Robert Phillipsb2adbef2019-07-02 16:33:05 -040068 case GrBackendApi::kMock:
Robert Phillipsa27d6252019-12-10 14:48:36 -050069 fMock = that.fMock;
Robert Phillipsb2adbef2019-07-02 16:33:05 -040070 break;
71 default:
72 SK_ABORT("Unknown GrBackend");
73 }
74}
75
Ben Wagner833313b2020-03-23 17:22:24 -040076GrBackendFormat& GrBackendFormat::operator=(const GrBackendFormat& that) {
77 if (this != &that) {
78 this->~GrBackendFormat();
79 new (this) GrBackendFormat(that);
80 }
81 return *this;
82}
83
John Rosascoa9b348f2019-11-08 13:18:15 -080084#ifdef SK_GL
Robert Phillipsfc711a22018-02-13 17:03:00 -050085GrBackendFormat::GrBackendFormat(GrGLenum format, GrGLenum target)
Greg Danielbdf12ad2018-10-12 09:31:11 -040086 : fBackend(GrBackendApi::kOpenGL)
Greg Daniel4065d452018-11-16 15:43:41 -050087 , fValid(true)
88 , fGLFormat(format) {
89 switch (target) {
Robert Phillipsf209e882019-06-25 15:59:50 -040090 case GR_GL_TEXTURE_NONE:
91 fTextureType = GrTextureType::kNone;
92 break;
Greg Daniel4065d452018-11-16 15:43:41 -050093 case GR_GL_TEXTURE_2D:
94 fTextureType = GrTextureType::k2D;
95 break;
96 case GR_GL_TEXTURE_RECTANGLE:
97 fTextureType = GrTextureType::kRectangle;
98 break;
99 case GR_GL_TEXTURE_EXTERNAL:
100 fTextureType = GrTextureType::kExternal;
101 break;
102 default:
103 SK_ABORT("Unexpected texture target");
104 }
Robert Phillipsfc711a22018-02-13 17:03:00 -0500105}
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400106#endif
Robert Phillipsfc711a22018-02-13 17:03:00 -0500107
Brian Salomond4764a12019-08-08 12:08:24 -0400108GrGLFormat GrBackendFormat::asGLFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400109 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400110 return GrGLFormatFromGLEnum(fGLFormat);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500111 }
Brian Salomond4764a12019-08-08 12:08:24 -0400112 return GrGLFormat::kUnknown;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500113}
114
Greg Daniel7e000222018-12-03 10:08:21 -0500115GrBackendFormat GrBackendFormat::MakeVk(const GrVkYcbcrConversionInfo& ycbcrInfo) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700116 SkASSERT(ycbcrInfo.isValid());
117 return GrBackendFormat(ycbcrInfo.fFormat, ycbcrInfo);
Greg Daniel7e000222018-12-03 10:08:21 -0500118}
119
120GrBackendFormat::GrBackendFormat(VkFormat vkFormat, const GrVkYcbcrConversionInfo& ycbcrInfo)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400121 : fBackend(GrBackendApi::kVulkan)
Greg Danielb4d89562018-10-03 18:44:49 +0000122#ifdef SK_VULKAN
Robert Phillipsfc711a22018-02-13 17:03:00 -0500123 , fValid(true)
Greg Danielb4d89562018-10-03 18:44:49 +0000124#else
Greg Daniel4065d452018-11-16 15:43:41 -0500125 , fValid(false)
Greg Danielb4d89562018-10-03 18:44:49 +0000126#endif
Greg Daniel4065d452018-11-16 15:43:41 -0500127 , fTextureType(GrTextureType::k2D) {
Greg Daniel7e000222018-12-03 10:08:21 -0500128 fVk.fFormat = vkFormat;
129 fVk.fYcbcrConversionInfo = ycbcrInfo;
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700130 if (fVk.fYcbcrConversionInfo.isValid() && fVk.fYcbcrConversionInfo.fExternalFormat) {
Greg Daniel387ec9a2019-03-07 16:44:54 -0500131 fTextureType = GrTextureType::kExternal;
132 }
Robert Phillipsfc711a22018-02-13 17:03:00 -0500133}
134
Brian Salomond4764a12019-08-08 12:08:24 -0400135bool GrBackendFormat::asVkFormat(VkFormat* format) const {
136 SkASSERT(format);
Greg Danielbdf12ad2018-10-12 09:31:11 -0400137 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400138 *format = fVk.fFormat;
139 return true;
Greg Daniel7e000222018-12-03 10:08:21 -0500140 }
Brian Salomond4764a12019-08-08 12:08:24 -0400141 return false;
Greg Daniel7e000222018-12-03 10:08:21 -0500142}
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
Stephen White3cc8d4f2019-10-30 09:56:23 -0400152GrBackendFormat::GrBackendFormat(wgpu::TextureFormat format)
Stephen White985741a2019-07-18 11:43:45 -0400153 : fBackend(GrBackendApi::kDawn)
154 , fValid(true)
155 , fDawnFormat(format)
156 , fTextureType(GrTextureType::k2D) {
157}
158
Stephen White3cc8d4f2019-10-30 09:56:23 -0400159bool GrBackendFormat::asDawnFormat(wgpu::TextureFormat* format) const {
Brian Salomond4764a12019-08-08 12:08:24 -0400160 SkASSERT(format);
Stephen White985741a2019-07-18 11:43:45 -0400161 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400162 *format = fDawnFormat;
163 return true;
Stephen White985741a2019-07-18 11:43:45 -0400164 }
Brian Salomond4764a12019-08-08 12:08:24 -0400165 return false;
Stephen White985741a2019-07-18 11:43:45 -0400166}
167#endif
168
Timothy Liang4e85e802018-06-28 16:37:18 -0400169#ifdef SK_METAL
170GrBackendFormat::GrBackendFormat(GrMTLPixelFormat mtlFormat)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400171 : fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400172 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500173 , fMtlFormat(mtlFormat)
174 , fTextureType(GrTextureType::k2D) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400175}
176
Brian Salomond4764a12019-08-08 12:08:24 -0400177GrMTLPixelFormat GrBackendFormat::asMtlFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400178 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400179 return fMtlFormat;
Timothy Liang4e85e802018-06-28 16:37:18 -0400180 }
Brian Salomond4764a12019-08-08 12:08:24 -0400181 // MTLPixelFormatInvalid == 0
182 return GrMTLPixelFormat(0);
Timothy Liang4e85e802018-06-28 16:37:18 -0400183}
184#endif
185
Jim Van Verthc86c83c2020-02-27 15:48:24 -0500186#ifdef SK_DIRECT3D
187GrBackendFormat::GrBackendFormat(DXGI_FORMAT dxgiFormat)
188 : fBackend(GrBackendApi::kDirect3D)
189 , fValid(true)
190 , fDxgiFormat(dxgiFormat)
191 , fTextureType(GrTextureType::k2D) {
192}
193
194bool GrBackendFormat::asDxgiFormat(DXGI_FORMAT* dxgiFormat) const {
195 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
196 *dxgiFormat = fDxgiFormat;
197 return true;
198 }
199 return false;
200}
201#endif
202
Greg Daniela7f69c22020-10-07 13:04:15 -0400203GrBackendFormat::GrBackendFormat(GrColorType colorType, SkImage::CompressionType compression,
204 bool isStencilFormat)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400205 : fBackend(GrBackendApi::kMock)
Robert Phillipsfc711a22018-02-13 17:03:00 -0500206 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500207 , fTextureType(GrTextureType::k2D) {
Robert Phillipsa27d6252019-12-10 14:48:36 -0500208 fMock.fColorType = colorType;
209 fMock.fCompressionType = compression;
Greg Daniela7f69c22020-10-07 13:04:15 -0400210 fMock.fIsStencilFormat = isStencilFormat;
211 SkASSERT(this->validateMock());
Robert Phillipsfc711a22018-02-13 17:03:00 -0500212}
213
Brian Salomondc8fcdb2020-03-26 16:45:05 -0400214uint32_t GrBackendFormat::channelMask() const {
215 if (!this->isValid()) {
216 return 0;
217 }
218 switch (fBackend) {
219#ifdef SK_GL
220 case GrBackendApi::kOpenGL:
221 return GrGLFormatChannels(GrGLFormatFromGLEnum(fGLFormat));
222#endif
223#ifdef SK_VULKAN
224 case GrBackendApi::kVulkan:
225 return GrVkFormatChannels(fVk.fFormat);
226#endif
227#ifdef SK_METAL
228 case GrBackendApi::kMetal:
229 return GrMtlFormatChannels(fMtlFormat);
230#endif
231#ifdef SK_DAWN
232 case GrBackendApi::kDawn:
233 return GrDawnFormatChannels(fDawnFormat);
234#endif
235#ifdef SK_DIRECT3D
236 case GrBackendApi::kDirect3D:
237 return GrDxgiFormatChannels(fDxgiFormat);
238#endif
239 case GrBackendApi::kMock:
240 return GrColorTypeChannelFlags(fMock.fColorType);
241
242 default:
243 return 0;
244 }
245}
246
Brian Salomond6584bd2021-05-18 16:29:09 -0400247GrColorFormatDesc GrBackendFormat::desc() const {
248 if (!this->isValid()) {
249 return GrColorFormatDesc::MakeInvalid();
250 }
251 switch (fBackend) {
252#ifdef SK_GL
253 case GrBackendApi::kOpenGL:
254 return GrGLFormatDesc(GrGLFormatFromGLEnum(fGLFormat));
255#endif
256#ifdef SK_VULKAN
257 case GrBackendApi::kVulkan:
258 return GrVkFormatDesc(fVk.fFormat);
259#endif
260#ifdef SK_METAL
261 case GrBackendApi::kMetal:
262 return GrMtlFormatDesc(fMtlFormat);
263#endif
264#ifdef SK_DAWN
265 case GrBackendApi::kDawn:
266 return GrDawnFormatDesc(fDawnFormat);
267#endif
268#ifdef SK_DIRECT3D
269 case GrBackendApi::kDirect3D:
270 return GrDxgiFormatDesc(fDxgiFormat);
271#endif
272 case GrBackendApi::kMock:
273 return GrGetColorTypeDesc(fMock.fColorType);
274
275 default:
276 return GrColorFormatDesc::MakeInvalid();
277 }
278}
279
Greg Daniela7f69c22020-10-07 13:04:15 -0400280#ifdef SK_DEBUG
281bool GrBackendFormat::validateMock() const {
282 int trueStates = 0;
283 if (fMock.fCompressionType != SkImage::CompressionType::kNone) {
284 trueStates++;
285 }
286 if (fMock.fColorType != GrColorType::kUnknown) {
287 trueStates++;
288 }
289 if (fMock.fIsStencilFormat) {
290 trueStates++;
291 }
292 return trueStates == 1;
293}
294#endif
295
Brian Salomond4764a12019-08-08 12:08:24 -0400296GrColorType GrBackendFormat::asMockColorType() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400297 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniela7f69c22020-10-07 13:04:15 -0400298 SkASSERT(this->validateMock());
Robert Phillipsa27d6252019-12-10 14:48:36 -0500299 return fMock.fColorType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500300 }
Robert Phillipsa27d6252019-12-10 14:48:36 -0500301
Brian Salomond4764a12019-08-08 12:08:24 -0400302 return GrColorType::kUnknown;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500303}
304
Robert Phillipsa27d6252019-12-10 14:48:36 -0500305SkImage::CompressionType GrBackendFormat::asMockCompressionType() const {
306 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniela7f69c22020-10-07 13:04:15 -0400307 SkASSERT(this->validateMock());
Robert Phillipsa27d6252019-12-10 14:48:36 -0500308 return fMock.fCompressionType;
309 }
310
311 return SkImage::CompressionType::kNone;
312}
313
Greg Daniela7f69c22020-10-07 13:04:15 -0400314bool GrBackendFormat::isMockStencilFormat() const {
315 if (this->isValid() && GrBackendApi::kMock == fBackend) {
316 SkASSERT(this->validateMock());
317 return fMock.fIsStencilFormat;
318 }
319
320 return false;
321}
Robert Phillipsa27d6252019-12-10 14:48:36 -0500322
Greg Daniel4065d452018-11-16 15:43:41 -0500323GrBackendFormat GrBackendFormat::makeTexture2D() const {
Greg Daniel4065d452018-11-16 15:43:41 -0500324 GrBackendFormat copy = *this;
Greg Daniel387ec9a2019-03-07 16:44:54 -0500325 if (const GrVkYcbcrConversionInfo* ycbcrInfo = this->getVkYcbcrConversionInfo()) {
326 if (ycbcrInfo->isValid()) {
327 // If we have a ycbcr we remove it from the backend format and set the VkFormat to
328 // R8G8B8A8_UNORM
329 SkASSERT(copy.fBackend == GrBackendApi::kVulkan);
330 copy.fVk.fYcbcrConversionInfo = GrVkYcbcrConversionInfo();
331 copy.fVk.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
332 }
333 }
Greg Daniel4065d452018-11-16 15:43:41 -0500334 copy.fTextureType = GrTextureType::k2D;
335 return copy;
336}
337
Robert Phillipsa27d6252019-12-10 14:48:36 -0500338GrBackendFormat GrBackendFormat::MakeMock(GrColorType colorType,
Greg Daniela7f69c22020-10-07 13:04:15 -0400339 SkImage::CompressionType compression,
340 bool isStencilFormat) {
341 return GrBackendFormat(colorType, compression, isStencilFormat);
Robert Phillipsa27d6252019-12-10 14:48:36 -0500342}
343
Greg Daniel45723ac2018-11-30 10:12:43 -0500344bool GrBackendFormat::operator==(const GrBackendFormat& that) const {
345 // Invalid GrBackendFormats are never equal to anything.
346 if (!fValid || !that.fValid) {
347 return false;
348 }
349
350 if (fBackend != that.fBackend) {
351 return false;
352 }
353
354 switch (fBackend) {
John Rosascoa9b348f2019-11-08 13:18:15 -0800355#ifdef SK_GL
Greg Daniel45723ac2018-11-30 10:12:43 -0500356 case GrBackendApi::kOpenGL:
357 return fGLFormat == that.fGLFormat;
John Rosascoa9b348f2019-11-08 13:18:15 -0800358 break;
359#endif
Robert Phillips0efc01d2019-11-06 17:19:30 +0000360#ifdef SK_VULKAN
John Rosascoa9b348f2019-11-08 13:18:15 -0800361 case GrBackendApi::kVulkan:
Greg Daniel45723ac2018-11-30 10:12:43 -0500362 return fVk.fFormat == that.fVk.fFormat &&
363 fVk.fYcbcrConversionInfo == that.fVk.fYcbcrConversionInfo;
Robert Phillips0efc01d2019-11-06 17:19:30 +0000364 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800365#endif
Greg Daniel45723ac2018-11-30 10:12:43 -0500366#ifdef SK_METAL
367 case GrBackendApi::kMetal:
368 return fMtlFormat == that.fMtlFormat;
Robert Phillips0efc01d2019-11-06 17:19:30 +0000369 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800370#endif
Robert Phillips0efc01d2019-11-06 17:19:30 +0000371#ifdef SK_DAWN
John Rosascoa9b348f2019-11-08 13:18:15 -0800372 case GrBackendApi::kDawn:
Stephen White985741a2019-07-18 11:43:45 -0400373 return fDawnFormat == that.fDawnFormat;
Robert Phillips0efc01d2019-11-06 17:19:30 +0000374 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800375#endif
Greg Daniel45723ac2018-11-30 10:12:43 -0500376 case GrBackendApi::kMock:
Robert Phillipsa27d6252019-12-10 14:48:36 -0500377 return fMock.fColorType == that.fMock.fColorType &&
378 fMock.fCompressionType == that.fMock.fCompressionType;
Greg Daniel85da3362020-03-09 15:18:35 -0400379#ifdef SK_DIRECT3D
380 case GrBackendApi::kDirect3D:
381 return fDxgiFormat == that.fDxgiFormat;
382#endif
Greg Daniel45723ac2018-11-30 10:12:43 -0500383 default:
384 SK_ABORT("Unknown GrBackend");
385 }
386 return false;
387}
388
Robert Phillips8e54a6e2020-08-17 14:31:02 -0400389#if defined(SK_DEBUG) || GR_TEST_UTILS
Robert Phillipsbac46722019-08-01 15:09:17 -0400390#include "include/core/SkString.h"
Robert Phillipsbac46722019-08-01 15:09:17 -0400391
392#ifdef SK_GL
393#include "src/gpu/gl/GrGLUtil.h"
394#endif
395#ifdef SK_VULKAN
396#include "src/gpu/vk/GrVkUtil.h"
397#endif
398
399SkString GrBackendFormat::toStr() const {
400 SkString str;
401
402 if (!fValid) {
403 str.append("invalid");
404 return str;
405 }
406
407 str.appendf("%s-", GrBackendApiToStr(fBackend));
408
409 switch (fBackend) {
410 case GrBackendApi::kOpenGL:
411#ifdef SK_GL
412 str.append(GrGLFormatToStr(fGLFormat));
413#endif
414 break;
415 case GrBackendApi::kVulkan:
416#ifdef SK_VULKAN
417 str.append(GrVkFormatToStr(fVk.fFormat));
418#endif
419 break;
420 case GrBackendApi::kMetal:
421#ifdef SK_METAL
422 str.append(GrMtlFormatToStr(fMtlFormat));
423#endif
424 break;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500425 case GrBackendApi::kDirect3D:
426#ifdef SK_DIRECT3D
Jim Van Verth05d09192020-03-20 11:23:39 -0400427 str.append(GrDxgiFormatToStr(fDxgiFormat));
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500428#endif
429 break;
Robert Phillipsbac46722019-08-01 15:09:17 -0400430 case GrBackendApi::kDawn:
431#ifdef SK_DAWN
Stephen Whited7325182019-08-02 17:22:59 -0400432 str.append(GrDawnFormatToStr(fDawnFormat));
Robert Phillipsbac46722019-08-01 15:09:17 -0400433#endif
434 break;
435 case GrBackendApi::kMock:
Robert Phillipsa27d6252019-12-10 14:48:36 -0500436 str.append(GrColorTypeToStr(fMock.fColorType));
437 str.appendf("-");
438 str.append(GrCompressionTypeToStr(fMock.fCompressionType));
Robert Phillipsbac46722019-08-01 15:09:17 -0400439 break;
440 }
441
442 return str;
443}
444#endif
445
446///////////////////////////////////////////////////////////////////////////////////////////////////
Greg Daniel6c6caf42020-05-29 12:11:05 -0400447GrBackendTexture::GrBackendTexture() : fIsValid(false) {}
448
Stephen White985741a2019-07-18 11:43:45 -0400449#ifdef SK_DAWN
450GrBackendTexture::GrBackendTexture(int width,
451 int height,
Stephen Whitef03c1162020-01-28 12:39:45 -0500452 const GrDawnTextureInfo& dawnInfo)
Stephen White985741a2019-07-18 11:43:45 -0400453 : fIsValid(true)
454 , fWidth(width)
455 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400456 , fMipmapped(GrMipmapped(dawnInfo.fLevelCount > 1))
Stephen White985741a2019-07-18 11:43:45 -0400457 , fBackend(GrBackendApi::kDawn)
458 , fDawnInfo(dawnInfo) {}
459#endif
460
Greg Danielb4d89562018-10-03 18:44:49 +0000461#ifdef SK_VULKAN
Greg Daniel6c6caf42020-05-29 12:11:05 -0400462GrBackendTexture::GrBackendTexture(int width, int height, const GrVkImageInfo& vkInfo)
Brian Salomon4456a0d2019-07-18 15:05:11 -0400463 : GrBackendTexture(width, height, vkInfo,
Greg Daniel6c6caf42020-05-29 12:11:05 -0400464 sk_sp<GrBackendSurfaceMutableStateImpl>(
465 new GrBackendSurfaceMutableStateImpl(
466 vkInfo.fImageLayout, vkInfo.fCurrentQueueFamily))) {}
467
Greg Daniel7b62dca2020-08-21 11:26:12 -0400468static const VkImageUsageFlags kDefaultUsageFlags =
469 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
470
471// We don't know if the backend texture is made renderable or not, so we default the usage flags
472// to include color attachment as well.
473static const VkImageUsageFlags kDefaultTexRTUsageFlags =
474 kDefaultUsageFlags | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
475
476static GrVkImageInfo apply_default_usage_flags(const GrVkImageInfo& info,
477 VkImageUsageFlags defaultFlags) {
478 if (info.fImageUsageFlags == 0) {
479 GrVkImageInfo newInfo = info;
480 newInfo.fImageUsageFlags = defaultFlags;
481 return newInfo;
482 }
483 return info;
484}
485
Greg Daniel6c6caf42020-05-29 12:11:05 -0400486GrBackendTexture::GrBackendTexture(int width,
487 int height,
488 const GrVkImageInfo& vkInfo,
489 sk_sp<GrBackendSurfaceMutableStateImpl> mutableState)
490 : fIsValid(true)
491 , fWidth(width)
492 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400493 , fMipmapped(GrMipmapped(vkInfo.fLevelCount > 1))
Greg Daniel6c6caf42020-05-29 12:11:05 -0400494 , fBackend(GrBackendApi::kVulkan)
Greg Daniel7b62dca2020-08-21 11:26:12 -0400495 , fVkInfo(apply_default_usage_flags(vkInfo, kDefaultTexRTUsageFlags))
Greg Daniel6c6caf42020-05-29 12:11:05 -0400496 , fMutableState(std::move(mutableState)) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000497#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400498
Brian Salomone2826ab2019-06-04 15:58:31 -0400499#ifdef SK_GL
500GrBackendTexture::GrBackendTexture(int width,
501 int height,
Brian Salomon40a40622020-07-21 10:32:07 -0400502 GrMipmapped mipmapped,
Brian Salomone2826ab2019-06-04 15:58:31 -0400503 const GrGLTextureInfo glInfo,
504 sk_sp<GrGLTextureParameters> params)
505 : fIsValid(true)
506 , fWidth(width)
507 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400508 , fMipmapped(mipmapped)
Brian Salomone2826ab2019-06-04 15:58:31 -0400509 , fBackend(GrBackendApi::kOpenGL)
510 , fGLInfo(glInfo, params.release()) {}
511
512sk_sp<GrGLTextureParameters> GrBackendTexture::getGLTextureParams() const {
513 if (fBackend != GrBackendApi::kOpenGL) {
514 return nullptr;
515 }
516 return fGLInfo.refParameters();
517}
518#endif
519
Timothy Liang4e85e802018-06-28 16:37:18 -0400520#ifdef SK_METAL
521GrBackendTexture::GrBackendTexture(int width,
522 int height,
Brian Salomon40a40622020-07-21 10:32:07 -0400523 GrMipmapped mipmapped,
Timothy Liang4e85e802018-06-28 16:37:18 -0400524 const GrMtlTextureInfo& mtlInfo)
525 : fIsValid(true)
526 , fWidth(width)
527 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400528 , fMipmapped(mipmapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400529 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400530 , fMtlInfo(mtlInfo) {}
531#endif
532
Jim Van Verth05d09192020-03-20 11:23:39 -0400533#ifdef SK_DIRECT3D
Jim Van Verth6ec56882020-03-26 11:47:26 -0400534GrBackendTexture::GrBackendTexture(int width, int height, const GrD3DTextureResourceInfo& d3dInfo)
Jim Van Verth05d09192020-03-20 11:23:39 -0400535 : GrBackendTexture(
536 width, height, d3dInfo,
537 sk_sp<GrD3DResourceState>(new GrD3DResourceState(
538 static_cast<D3D12_RESOURCE_STATES>(d3dInfo.fResourceState)))) {}
539
540GrBackendTexture::GrBackendTexture(int width,
541 int height,
Jim Van Verth6ec56882020-03-26 11:47:26 -0400542 const GrD3DTextureResourceInfo& d3dInfo,
Jim Van Verth05d09192020-03-20 11:23:39 -0400543 sk_sp<GrD3DResourceState> state)
544 : fIsValid(true)
545 , fWidth(width)
546 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400547 , fMipmapped(GrMipmapped(d3dInfo.fLevelCount > 1))
Jim Van Verth05d09192020-03-20 11:23:39 -0400548 , fBackend(GrBackendApi::kDirect3D)
549 , fD3DInfo(d3dInfo, state.release()) {}
550#endif
551
John Rosascoa9b348f2019-11-08 13:18:15 -0800552#ifdef SK_GL
Brian Salomon8fe24272017-07-07 12:56:11 -0400553GrBackendTexture::GrBackendTexture(int width,
554 int height,
Brian Salomon40a40622020-07-21 10:32:07 -0400555 GrMipmapped mipmapped,
Greg Daniele7d8da42017-12-04 11:23:19 -0500556 const GrGLTextureInfo& glInfo)
Brian Salomon40a40622020-07-21 10:32:07 -0400557 : GrBackendTexture(width, height, mipmapped, glInfo, sk_make_sp<GrGLTextureParameters>()) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400558 // Make no assumptions about client's texture's parameters.
559 this->glTextureParametersModified();
560}
John Rosascoa9b348f2019-11-08 13:18:15 -0800561#endif
Greg Daniele7d8da42017-12-04 11:23:19 -0500562
563GrBackendTexture::GrBackendTexture(int width,
564 int height,
Brian Salomon40a40622020-07-21 10:32:07 -0400565 GrMipmapped mipmapped,
Greg Daniel177e6952017-10-12 12:27:11 -0400566 const GrMockTextureInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400567 : fIsValid(true)
568 , fWidth(width)
Brian Salomon8fe24272017-07-07 12:56:11 -0400569 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400570 , fMipmapped(mipmapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400571 , fBackend(GrBackendApi::kMock)
Brian Salomon8fe24272017-07-07 12:56:11 -0400572 , fMockInfo(mockInfo) {}
573
Greg Daniel52e16d92018-04-10 09:34:07 -0400574GrBackendTexture::~GrBackendTexture() {
575 this->cleanup();
576}
577
578void GrBackendTexture::cleanup() {
Brian Salomone2826ab2019-06-04 15:58:31 -0400579#ifdef SK_GL
580 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
581 fGLInfo.cleanup();
582 }
583#endif
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000584#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400585 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400586 fVkInfo.cleanup();
587 }
588#endif
Jim Van Verth05d09192020-03-20 11:23:39 -0400589#ifdef SK_DIRECT3D
590 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
591 fD3DInfo.cleanup();
592 }
593#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400594}
595
596GrBackendTexture::GrBackendTexture(const GrBackendTexture& that) : fIsValid(false) {
597 *this = that;
598}
599
600GrBackendTexture& GrBackendTexture::operator=(const GrBackendTexture& that) {
601 if (!that.isValid()) {
602 this->cleanup();
603 fIsValid = false;
604 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -0400605 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400606 this->cleanup();
607 fIsValid = false;
Greg Daniel52e16d92018-04-10 09:34:07 -0400608 }
609 fWidth = that.fWidth;
610 fHeight = that.fHeight;
Brian Salomon40a40622020-07-21 10:32:07 -0400611 fMipmapped = that.fMipmapped;
Greg Daniel52e16d92018-04-10 09:34:07 -0400612 fBackend = that.fBackend;
613
614 switch (that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400615#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400616 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400617 fGLInfo.assign(that.fGLInfo, this->isValid());
Greg Daniel52e16d92018-04-10 09:34:07 -0400618 break;
Mike Kleina55e2142018-10-03 16:34:11 +0000619#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400620#ifdef SK_VULKAN
621 case GrBackendApi::kVulkan:
622 fVkInfo.assign(that.fVkInfo, this->isValid());
Greg Danielb4d89562018-10-03 18:44:49 +0000623 break;
Brian Salomone2826ab2019-06-04 15:58:31 -0400624#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400625#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400626 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000627 fMtlInfo = that.fMtlInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400628 break;
629#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500630#ifdef SK_DIRECT3D
631 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -0400632 fD3DInfo.assign(that.fD3DInfo, this->isValid());
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500633 break;
634#endif
Stephen White985741a2019-07-18 11:43:45 -0400635#ifdef SK_DAWN
636 case GrBackendApi::kDawn:
637 fDawnInfo = that.fDawnInfo;
638 break;
639#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400640 case GrBackendApi::kMock:
Greg Daniel52e16d92018-04-10 09:34:07 -0400641 fMockInfo = that.fMockInfo;
642 break;
643 default:
644 SK_ABORT("Unknown GrBackend");
645 }
Greg Daniel6c6caf42020-05-29 12:11:05 -0400646 fMutableState = that.fMutableState;
Brian Salomone2826ab2019-06-04 15:58:31 -0400647 fIsValid = true;
Greg Daniel52e16d92018-04-10 09:34:07 -0400648 return *this;
649}
650
Greg Daniel6c6caf42020-05-29 12:11:05 -0400651sk_sp<GrBackendSurfaceMutableStateImpl> GrBackendTexture::getMutableState() const {
652 return fMutableState;
653}
654
Stephen White985741a2019-07-18 11:43:45 -0400655#ifdef SK_DAWN
Stephen Whitef03c1162020-01-28 12:39:45 -0500656bool GrBackendTexture::getDawnTextureInfo(GrDawnTextureInfo* outInfo) const {
Stephen White985741a2019-07-18 11:43:45 -0400657 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
658 *outInfo = fDawnInfo;
659 return true;
660 }
661 return false;
662}
663#endif
664
Greg Daniel6c6caf42020-05-29 12:11:05 -0400665bool GrBackendTexture::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielcc7ec242020-05-29 14:43:36 -0400666#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400667 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -0400668 *outInfo = fVkInfo.snapImageInfo(fMutableState.get());
Greg Daniel52e16d92018-04-10 09:34:07 -0400669 return true;
670 }
Greg Danielcc7ec242020-05-29 14:43:36 -0400671#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400672 return false;
673}
674
675void GrBackendTexture::setVkImageLayout(VkImageLayout layout) {
Greg Danielcc7ec242020-05-29 14:43:36 -0400676#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400677 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -0400678 fMutableState->setImageLayout(layout);
Greg Daniel52e16d92018-04-10 09:34:07 -0400679 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400680#endif
Greg Danielcc7ec242020-05-29 14:43:36 -0400681}
Greg Daniel94403452017-04-18 15:52:36 -0400682
Timothy Liang4e85e802018-06-28 16:37:18 -0400683#ifdef SK_METAL
684bool GrBackendTexture::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400685 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000686 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400687 return true;
688 }
689 return false;
690}
691#endif
692
Jim Van Verth05d09192020-03-20 11:23:39 -0400693#ifdef SK_DIRECT3D
Jim Van Verth6ec56882020-03-26 11:47:26 -0400694bool GrBackendTexture::getD3DTextureResourceInfo(GrD3DTextureResourceInfo* outInfo) const {
Jim Van Verth05d09192020-03-20 11:23:39 -0400695 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
Jim Van Verth6ec56882020-03-26 11:47:26 -0400696 *outInfo = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -0400697 return true;
698 }
699 return false;
700}
701
702void GrBackendTexture::setD3DResourceState(GrD3DResourceStateEnum state) {
703 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
704 fD3DInfo.setResourceState(state);
705 }
706}
707
708sk_sp<GrD3DResourceState> GrBackendTexture::getGrD3DResourceState() const {
709 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
710 return fD3DInfo.getGrD3DResourceState();
711 }
712 return nullptr;
713}
714#endif
715
Greg Daniel52e16d92018-04-10 09:34:07 -0400716bool GrBackendTexture::getGLTextureInfo(GrGLTextureInfo* outInfo) const {
Brian Salomone2826ab2019-06-04 15:58:31 -0400717#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400718 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400719 *outInfo = fGLInfo.info();
Greg Daniel52e16d92018-04-10 09:34:07 -0400720 return true;
Brian Osmana7ef3a02019-03-27 15:54:14 -0400721 } else if (this->isValid() && GrBackendApi::kMock == fBackend) {
722 // Hack! This allows some blink unit tests to work when using the Mock GrContext.
723 // Specifically, tests that rely on CanvasResourceProviderTextureGpuMemoryBuffer.
724 // If that code ever goes away (or ideally becomes backend-agnostic), this can go away.
725 *outInfo = GrGLTextureInfo{ GR_GL_TEXTURE_2D,
Robert Phillipsa27d6252019-12-10 14:48:36 -0500726 static_cast<GrGLuint>(fMockInfo.id()),
Brian Osmana7ef3a02019-03-27 15:54:14 -0400727 GR_GL_RGBA8 };
728 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400729 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400730#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400731 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400732}
733
Brian Salomone2826ab2019-06-04 15:58:31 -0400734void GrBackendTexture::glTextureParametersModified() {
735#ifdef SK_GL
736 if (this->isValid() && fBackend == GrBackendApi::kOpenGL) {
737 fGLInfo.parameters()->invalidate();
738 }
739#endif
740}
741
Greg Daniel52e16d92018-04-10 09:34:07 -0400742bool GrBackendTexture::getMockTextureInfo(GrMockTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400743 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400744 *outInfo = fMockInfo;
745 return true;
Brian Salomon8fe24272017-07-07 12:56:11 -0400746 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400747 return false;
Brian Salomon8fe24272017-07-07 12:56:11 -0400748}
749
Greg Daniel6c6caf42020-05-29 12:11:05 -0400750void GrBackendTexture::setMutableState(const GrBackendSurfaceMutableState& state) {
751 fMutableState->set(state);
752}
753
Brian Salomon4456a0d2019-07-18 15:05:11 -0400754bool GrBackendTexture::isProtected() const {
755 if (!this->isValid() || this->backend() != GrBackendApi::kVulkan) {
756 return false;
757 }
758 return fVkInfo.isProtected();
759}
760
Brian Salomonaad83152019-05-24 10:16:35 -0400761bool GrBackendTexture::isSameTexture(const GrBackendTexture& that) {
762 if (!this->isValid() || !that.isValid()) {
763 return false;
764 }
765 if (fBackend != that.fBackend) {
766 return false;
767 }
768 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400769#ifdef SK_GL
Brian Salomonaad83152019-05-24 10:16:35 -0400770 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400771 return fGLInfo.info().fID == that.fGLInfo.info().fID;
772#endif
Brian Salomonaad83152019-05-24 10:16:35 -0400773#ifdef SK_VULKAN
774 case GrBackendApi::kVulkan:
Greg Daniel6c6caf42020-05-29 12:11:05 -0400775 return fVkInfo.snapImageInfo(fMutableState.get()).fImage ==
776 that.fVkInfo.snapImageInfo(that.fMutableState.get()).fImage;
Brian Salomonaad83152019-05-24 10:16:35 -0400777#endif
778#ifdef SK_METAL
779 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000780 return this->fMtlInfo.fTexture == that.fMtlInfo.fTexture;
Brian Salomonaad83152019-05-24 10:16:35 -0400781#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500782#ifdef SK_DIRECT3D
783 case GrBackendApi::kDirect3D:
Jim Van Verthde175ab2020-06-10 16:12:25 -0400784 return fD3DInfo.snapTextureResourceInfo().fResource ==
785 that.fD3DInfo.snapTextureResourceInfo().fResource;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500786#endif
Stephen Whited4b8bf92020-06-08 15:38:29 -0400787#ifdef SK_DAWN
788 case GrBackendApi::kDawn: {
789 return this->fDawnInfo.fTexture.Get() == that.fDawnInfo.fTexture.Get();
790 }
791#endif
Brian Salomonaad83152019-05-24 10:16:35 -0400792 case GrBackendApi::kMock:
Robert Phillipsa27d6252019-12-10 14:48:36 -0500793 return fMockInfo.id() == that.fMockInfo.id();
Brian Salomonaad83152019-05-24 10:16:35 -0400794 default:
795 return false;
796 }
797}
798
Brian Salomonf391d0f2018-12-14 09:18:50 -0500799GrBackendFormat GrBackendTexture::getBackendFormat() const {
800 if (!this->isValid()) {
801 return GrBackendFormat();
802 }
803 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400804#ifdef SK_GL
Brian Salomonf391d0f2018-12-14 09:18:50 -0500805 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400806 return GrBackendFormat::MakeGL(fGLInfo.info().fFormat, fGLInfo.info().fTarget);
807#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500808#ifdef SK_VULKAN
809 case GrBackendApi::kVulkan: {
Greg Daniel6c6caf42020-05-29 12:11:05 -0400810 auto info = fVkInfo.snapImageInfo(fMutableState.get());
Brian Salomonf391d0f2018-12-14 09:18:50 -0500811 if (info.fYcbcrConversionInfo.isValid()) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700812 SkASSERT(info.fFormat == info.fYcbcrConversionInfo.fFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500813 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
814 }
815 return GrBackendFormat::MakeVk(info.fFormat);
816 }
817#endif
818#ifdef SK_METAL
819 case GrBackendApi::kMetal: {
820 GrMtlTextureInfo mtlInfo;
821 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
822 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
823 }
824#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500825#ifdef SK_DIRECT3D
826 case GrBackendApi::kDirect3D: {
Jim Van Verth6ec56882020-03-26 11:47:26 -0400827 auto d3dInfo = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -0400828 return GrBackendFormat::MakeDxgi(d3dInfo.fFormat);
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500829 }
830#endif
831#ifdef SK_DAWN
832 case GrBackendApi::kDawn: {
833 return GrBackendFormat::MakeDawn(fDawnInfo.fFormat);
834 }
835#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500836 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400837 return fMockInfo.getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500838 default:
839 return GrBackendFormat();
840 }
841}
842
Robert Phillipsc5509952018-04-04 15:54:55 -0400843#if GR_TEST_UTILS
844bool GrBackendTexture::TestingOnly_Equals(const GrBackendTexture& t0, const GrBackendTexture& t1) {
845 if (!t0.isValid() || !t1.isValid()) {
846 return false; // two invalid backend textures are not considered equal
847 }
848
849 if (t0.fWidth != t1.fWidth ||
850 t0.fHeight != t1.fHeight ||
Brian Salomon40a40622020-07-21 10:32:07 -0400851 t0.fMipmapped != t1.fMipmapped ||
Robert Phillipsc5509952018-04-04 15:54:55 -0400852 t0.fBackend != t1.fBackend) {
853 return false;
854 }
855
Greg Daniel7f3408b2020-06-03 13:31:00 -0400856 // For our tests when checking equality we are assuming the both backendTexture objects will
857 // be using the same mutable state object.
858 if (t0.fMutableState != t1.fMutableState) {
859 return false;
860 }
861
Robert Phillipsc5509952018-04-04 15:54:55 -0400862 switch (t0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400863#ifdef SK_GL
864 case GrBackendApi::kOpenGL:
865 return t0.fGLInfo.info() == t1.fGLInfo.info();
866#endif
867 case GrBackendApi::kMock:
868 return t0.fMockInfo == t1.fMockInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400869#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400870 case GrBackendApi::kVulkan:
871 return t0.fVkInfo == t1.fVkInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400872#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400873#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -0400874 case GrBackendApi::kMetal:
875 return t0.fMtlInfo == t1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400876#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500877#ifdef SK_DIRECT3D
878 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -0400879 return t0.fD3DInfo == t1.fD3DInfo;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500880#endif
Stephen White985741a2019-07-18 11:43:45 -0400881#ifdef SK_DAWN
882 case GrBackendApi::kDawn:
883 return t0.fDawnInfo == t1.fDawnInfo;
884#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400885 default:
886 return false;
Robert Phillipsc5509952018-04-04 15:54:55 -0400887 }
Robert Phillipsc5509952018-04-04 15:54:55 -0400888}
889#endif
890
Greg Daniel94403452017-04-18 15:52:36 -0400891////////////////////////////////////////////////////////////////////////////////////////////////////
892
Greg Daniel6c6caf42020-05-29 12:11:05 -0400893GrBackendRenderTarget::GrBackendRenderTarget() : fIsValid(false) {}
894
895
Stephen White985741a2019-07-18 11:43:45 -0400896#ifdef SK_DAWN
897GrBackendRenderTarget::GrBackendRenderTarget(int width,
898 int height,
899 int sampleCnt,
900 int stencilBits,
Stephen Whitef03c1162020-01-28 12:39:45 -0500901 const GrDawnRenderTargetInfo& dawnInfo)
Stephen White985741a2019-07-18 11:43:45 -0400902 : fIsValid(true)
Stephen White3c0a50f2020-01-16 18:19:54 -0500903 , fFramebufferOnly(true)
Stephen White985741a2019-07-18 11:43:45 -0400904 , fWidth(width)
905 , fHeight(height)
906 , fSampleCnt(sampleCnt)
907 , fStencilBits(stencilBits)
Stephen White985741a2019-07-18 11:43:45 -0400908 , fBackend(GrBackendApi::kDawn)
909 , fDawnInfo(dawnInfo) {}
910#endif
911
Greg Daniel6c6caf42020-05-29 12:11:05 -0400912#ifdef SK_VULKAN
Brian Salomon57fd9232020-09-28 17:02:49 -0400913static GrVkImageInfo resolve_vkii_sample_count(const GrVkImageInfo& vkII, int sidebandSampleCnt) {
914 auto result = vkII;
915 result.fSampleCount = std::max({vkII.fSampleCount,
916 static_cast<uint32_t>(sidebandSampleCnt),
917 1U});
918 return result;
919}
920
Greg Daniel94403452017-04-18 15:52:36 -0400921GrBackendRenderTarget::GrBackendRenderTarget(int width,
922 int height,
923 int sampleCnt,
Brian Salomonafdc6b12018-03-09 12:02:32 -0500924 const GrVkImageInfo& vkInfo)
Brian Salomon57fd9232020-09-28 17:02:49 -0400925 : GrBackendRenderTarget(width, height, resolve_vkii_sample_count(vkInfo, sampleCnt)) {}
926
927GrBackendRenderTarget::GrBackendRenderTarget(int width,
928 int height,
929 const GrVkImageInfo& vkInfo)
930 : GrBackendRenderTarget(width, height, vkInfo,
Greg Daniel6c6caf42020-05-29 12:11:05 -0400931 sk_sp<GrBackendSurfaceMutableStateImpl>(
932 new GrBackendSurfaceMutableStateImpl(
Brian Salomon57fd9232020-09-28 17:02:49 -0400933 vkInfo.fImageLayout, vkInfo.fCurrentQueueFamily))) {}
Greg Daniel323fbcf2018-04-10 13:46:30 -0400934
Greg Daniel7b62dca2020-08-21 11:26:12 -0400935static const VkImageUsageFlags kDefaultRTUsageFlags =
936 kDefaultUsageFlags | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
937
Greg Daniel323fbcf2018-04-10 13:46:30 -0400938GrBackendRenderTarget::GrBackendRenderTarget(int width,
939 int height,
Greg Daniel323fbcf2018-04-10 13:46:30 -0400940 const GrVkImageInfo& vkInfo,
Greg Daniel6c6caf42020-05-29 12:11:05 -0400941 sk_sp<GrBackendSurfaceMutableStateImpl> mutableState)
Greg Daniel9ca30652018-04-06 09:27:20 -0400942 : fIsValid(true)
943 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400944 , fHeight(height)
Brian Salomon57fd9232020-09-28 17:02:49 -0400945 , fSampleCnt(std::max(1U, vkInfo.fSampleCount))
Brian Salomonafdc6b12018-03-09 12:02:32 -0500946 , fStencilBits(0) // We always create stencil buffers internally for vulkan
Greg Danielbdf12ad2018-10-12 09:31:11 -0400947 , fBackend(GrBackendApi::kVulkan)
Greg Daniel7b62dca2020-08-21 11:26:12 -0400948 , fVkInfo(apply_default_usage_flags(vkInfo, kDefaultRTUsageFlags))
Brian Salomon57fd9232020-09-28 17:02:49 -0400949 , fMutableState(mutableState) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000950#endif
Greg Daniel94403452017-04-18 15:52:36 -0400951
Timothy Liang4e85e802018-06-28 16:37:18 -0400952#ifdef SK_METAL
Brian Salomon8e0aa442020-09-30 13:50:18 -0400953GrBackendRenderTarget::GrBackendRenderTarget(int width, int height, const GrMtlTextureInfo& mtlInfo)
Timothy Liang4e85e802018-06-28 16:37:18 -0400954 : fIsValid(true)
Brian Salomon8e0aa442020-09-30 13:50:18 -0400955 , fFramebufferOnly(false) // TODO: set this from mtlInfo.fTexture->framebufferOnly
Timothy Liang4e85e802018-06-28 16:37:18 -0400956 , fWidth(width)
957 , fHeight(height)
Brian Salomon8e0aa442020-09-30 13:50:18 -0400958 , fSampleCnt(std::max(1, GrMtlTextureInfoSampleCount(mtlInfo)))
Timothy Liang4e85e802018-06-28 16:37:18 -0400959 , fStencilBits(0)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400960 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400961 , fMtlInfo(mtlInfo) {}
Brian Salomon8e0aa442020-09-30 13:50:18 -0400962
963GrBackendRenderTarget::GrBackendRenderTarget(int width, int height,
964 int sampleCount,
965 const GrMtlTextureInfo& mtlInfo)
Jim Van Verthd2567de2021-05-12 17:12:17 -0400966 : GrBackendRenderTarget(width, height, mtlInfo) {
967 fSampleCnt = sampleCount;
968}
Timothy Liang4e85e802018-06-28 16:37:18 -0400969#endif
970
Jim Van Verth05d09192020-03-20 11:23:39 -0400971#ifdef SK_DIRECT3D
Brian Salomon718ae762020-09-29 16:52:49 -0400972GrBackendRenderTarget::GrBackendRenderTarget(int width, int height,
Jim Van Verth6ec56882020-03-26 11:47:26 -0400973 const GrD3DTextureResourceInfo& d3dInfo)
Jim Van Verth05d09192020-03-20 11:23:39 -0400974 : GrBackendRenderTarget(
Brian Salomon718ae762020-09-29 16:52:49 -0400975 width, height, d3dInfo,
Jim Van Verth05d09192020-03-20 11:23:39 -0400976 sk_sp<GrD3DResourceState>(new GrD3DResourceState(
977 static_cast<D3D12_RESOURCE_STATES>(d3dInfo.fResourceState)))) {}
978
979GrBackendRenderTarget::GrBackendRenderTarget(int width,
980 int height,
Jim Van Verth6ec56882020-03-26 11:47:26 -0400981 const GrD3DTextureResourceInfo& d3dInfo,
Jim Van Verth05d09192020-03-20 11:23:39 -0400982 sk_sp<GrD3DResourceState> state)
983 : fIsValid(true)
984 , fWidth(width)
985 , fHeight(height)
Brian Salomon718ae762020-09-29 16:52:49 -0400986 , fSampleCnt(std::max(1U, d3dInfo.fSampleCount))
Jim Van Verth05d09192020-03-20 11:23:39 -0400987 , fStencilBits(0)
988 , fBackend(GrBackendApi::kDirect3D)
989 , fD3DInfo(d3dInfo, state.release()) {}
990#endif
John Rosascoa9b348f2019-11-08 13:18:15 -0800991#ifdef SK_GL
Greg Danielfaa095e2017-12-19 13:15:02 -0500992GrBackendRenderTarget::GrBackendRenderTarget(int width,
993 int height,
994 int sampleCnt,
995 int stencilBits,
996 const GrGLFramebufferInfo& glInfo)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500997 : fWidth(width)
Greg Danielfaa095e2017-12-19 13:15:02 -0500998 , fHeight(height)
Brian Osman788b9162020-02-07 10:36:46 -0500999 , fSampleCnt(std::max(1, sampleCnt))
Greg Danielfaa095e2017-12-19 13:15:02 -05001000 , fStencilBits(stencilBits)
Greg Danielbdf12ad2018-10-12 09:31:11 -04001001 , fBackend(GrBackendApi::kOpenGL)
Robert Phillipsb45f47d2019-02-03 17:17:54 -05001002 , fGLInfo(glInfo) {
1003 fIsValid = SkToBool(glInfo.fFormat); // the glInfo must have a valid format
1004}
John Rosascoa9b348f2019-11-08 13:18:15 -08001005#endif
Greg Danielfaa095e2017-12-19 13:15:02 -05001006
Brian Salomon0c51eea2018-03-09 17:02:09 -05001007GrBackendRenderTarget::GrBackendRenderTarget(int width,
1008 int height,
1009 int sampleCnt,
1010 int stencilBits,
1011 const GrMockRenderTargetInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -04001012 : fIsValid(true)
1013 , fWidth(width)
Brian Salomon0c51eea2018-03-09 17:02:09 -05001014 , fHeight(height)
Brian Osman788b9162020-02-07 10:36:46 -05001015 , fSampleCnt(std::max(1, sampleCnt))
Brian Salomon0c51eea2018-03-09 17:02:09 -05001016 , fStencilBits(stencilBits)
Brian Salomon0c51eea2018-03-09 17:02:09 -05001017 , fMockInfo(mockInfo) {}
1018
Greg Daniel323fbcf2018-04-10 13:46:30 -04001019GrBackendRenderTarget::~GrBackendRenderTarget() {
1020 this->cleanup();
1021}
1022
1023void GrBackendRenderTarget::cleanup() {
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +00001024#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -04001025 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -04001026 fVkInfo.cleanup();
1027 }
1028#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001029#ifdef SK_DIRECT3D
1030 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
1031 fD3DInfo.cleanup();
1032 }
1033#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -04001034}
1035
1036GrBackendRenderTarget::GrBackendRenderTarget(const GrBackendRenderTarget& that) : fIsValid(false) {
1037 *this = that;
1038}
1039
1040GrBackendRenderTarget& GrBackendRenderTarget::operator=(const GrBackendRenderTarget& that) {
1041 if (!that.isValid()) {
1042 this->cleanup();
1043 fIsValid = false;
1044 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -04001045 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -04001046 this->cleanup();
1047 fIsValid = false;
Greg Daniel323fbcf2018-04-10 13:46:30 -04001048 }
1049 fWidth = that.fWidth;
1050 fHeight = that.fHeight;
1051 fSampleCnt = that.fSampleCnt;
1052 fStencilBits = that.fStencilBits;
Greg Daniel323fbcf2018-04-10 13:46:30 -04001053 fBackend = that.fBackend;
1054
1055 switch (that.fBackend) {
John Rosascoa9b348f2019-11-08 13:18:15 -08001056#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -04001057 case GrBackendApi::kOpenGL:
Greg Daniel323fbcf2018-04-10 13:46:30 -04001058 fGLInfo = that.fGLInfo;
1059 break;
John Rosasco078cf3e2019-10-31 16:21:39 -07001060#endif
John Rosascoa9b348f2019-11-08 13:18:15 -08001061#ifdef SK_VULKAN
1062 case GrBackendApi::kVulkan:
1063 fVkInfo.assign(that.fVkInfo, this->isValid());
Robert Phillips0efc01d2019-11-06 17:19:30 +00001064 break;
John Rosascoa9b348f2019-11-08 13:18:15 -08001065#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -04001066#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -04001067 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +00001068 fMtlInfo = that.fMtlInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -04001069 break;
1070#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001071#ifdef SK_DIRECT3D
1072 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -04001073 fD3DInfo.assign(that.fD3DInfo, this->isValid());
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001074 break;
1075#endif
1076#ifdef SK_DAWN
1077 case GrBackendApi::kDawn:
1078 fDawnInfo = that.fDawnInfo;
1079 break;
1080#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -04001081 case GrBackendApi::kMock:
Greg Daniel323fbcf2018-04-10 13:46:30 -04001082 fMockInfo = that.fMockInfo;
1083 break;
1084 default:
1085 SK_ABORT("Unknown GrBackend");
1086 }
Greg Daniel6c6caf42020-05-29 12:11:05 -04001087 fMutableState = that.fMutableState;
Greg Daniel323fbcf2018-04-10 13:46:30 -04001088 fIsValid = that.fIsValid;
1089 return *this;
1090}
1091
Greg Daniel6c6caf42020-05-29 12:11:05 -04001092sk_sp<GrBackendSurfaceMutableStateImpl> GrBackendRenderTarget::getMutableState() const {
1093 return fMutableState;
1094}
1095
Stephen White985741a2019-07-18 11:43:45 -04001096#ifdef SK_DAWN
Stephen Whitef03c1162020-01-28 12:39:45 -05001097bool GrBackendRenderTarget::getDawnRenderTargetInfo(GrDawnRenderTargetInfo* outInfo) const {
Stephen White985741a2019-07-18 11:43:45 -04001098 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
1099 *outInfo = fDawnInfo;
1100 return true;
1101 }
1102 return false;
1103}
1104#endif
1105
Greg Daniel6c6caf42020-05-29 12:11:05 -04001106bool GrBackendRenderTarget::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielcc7ec242020-05-29 14:43:36 -04001107#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -04001108 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -04001109 *outInfo = fVkInfo.snapImageInfo(fMutableState.get());
Greg Daniel323fbcf2018-04-10 13:46:30 -04001110 return true;
1111 }
Greg Danielcc7ec242020-05-29 14:43:36 -04001112#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -04001113 return false;
1114}
1115
1116void GrBackendRenderTarget::setVkImageLayout(VkImageLayout layout) {
Greg Danielcc7ec242020-05-29 14:43:36 -04001117#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -04001118 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -04001119 fMutableState->setImageLayout(layout);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001120 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001121#endif
Greg Danielcc7ec242020-05-29 14:43:36 -04001122}
Greg Daniel94403452017-04-18 15:52:36 -04001123
Timothy Liang4e85e802018-06-28 16:37:18 -04001124#ifdef SK_METAL
1125bool GrBackendRenderTarget::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001126 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +00001127 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -04001128 return true;
1129 }
1130 return false;
1131}
1132#endif
1133
Jim Van Verth05d09192020-03-20 11:23:39 -04001134#ifdef SK_DIRECT3D
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001135bool GrBackendRenderTarget::getD3DTextureResourceInfo(GrD3DTextureResourceInfo* outInfo) const {
Jim Van Verth05d09192020-03-20 11:23:39 -04001136 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
Jim Van Verth6ec56882020-03-26 11:47:26 -04001137 *outInfo = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -04001138 return true;
1139 }
1140 return false;
1141}
1142
1143void GrBackendRenderTarget::setD3DResourceState(GrD3DResourceStateEnum state) {
1144 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
1145 fD3DInfo.setResourceState(state);
1146 }
1147}
1148
1149sk_sp<GrD3DResourceState> GrBackendRenderTarget::getGrD3DResourceState() const {
1150 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
1151 return fD3DInfo.getGrD3DResourceState();
1152 }
1153 return nullptr;
1154}
1155#endif
1156
John Rosascoa9b348f2019-11-08 13:18:15 -08001157#ifdef SK_GL
Greg Daniel323fbcf2018-04-10 13:46:30 -04001158bool GrBackendRenderTarget::getGLFramebufferInfo(GrGLFramebufferInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001159 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -04001160 *outInfo = fGLInfo;
1161 return true;
Greg Daniel94403452017-04-18 15:52:36 -04001162 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001163 return false;
Greg Daniel94403452017-04-18 15:52:36 -04001164}
John Rosascoa9b348f2019-11-08 13:18:15 -08001165#endif
Greg Daniel94403452017-04-18 15:52:36 -04001166
Robert Phillipsf209e882019-06-25 15:59:50 -04001167GrBackendFormat GrBackendRenderTarget::getBackendFormat() const {
1168 if (!this->isValid()) {
1169 return GrBackendFormat();
1170 }
1171 switch (fBackend) {
1172#ifdef SK_GL
1173 case GrBackendApi::kOpenGL:
1174 return GrBackendFormat::MakeGL(fGLInfo.fFormat, GR_GL_TEXTURE_NONE);
1175#endif
1176#ifdef SK_VULKAN
1177 case GrBackendApi::kVulkan: {
Greg Daniel6c6caf42020-05-29 12:11:05 -04001178 auto info = fVkInfo.snapImageInfo(fMutableState.get());
Robert Phillipsf209e882019-06-25 15:59:50 -04001179 if (info.fYcbcrConversionInfo.isValid()) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -07001180 SkASSERT(info.fFormat == info.fYcbcrConversionInfo.fFormat);
Robert Phillipsf209e882019-06-25 15:59:50 -04001181 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
1182 }
1183 return GrBackendFormat::MakeVk(info.fFormat);
1184 }
1185#endif
1186#ifdef SK_METAL
1187 case GrBackendApi::kMetal: {
1188 GrMtlTextureInfo mtlInfo;
1189 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
1190 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
1191 }
1192#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001193#ifdef SK_DIRECT3D
1194 case GrBackendApi::kDirect3D: {
Jim Van Verth6ec56882020-03-26 11:47:26 -04001195 auto info = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -04001196 return GrBackendFormat::MakeDxgi(info.fFormat);
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001197 }
1198#endif
Stephen Whitef03c1162020-01-28 12:39:45 -05001199#ifdef SK_DAWN
1200 case GrBackendApi::kDawn: {
1201 GrDawnRenderTargetInfo dawnInfo;
1202 SkAssertResult(this->getDawnRenderTargetInfo(&dawnInfo));
1203 return GrBackendFormat::MakeDawn(dawnInfo.fFormat);
1204 }
1205#endif
Robert Phillipsf209e882019-06-25 15:59:50 -04001206 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -04001207 return fMockInfo.getBackendFormat();
Robert Phillipsf209e882019-06-25 15:59:50 -04001208 default:
1209 return GrBackendFormat();
1210 }
1211}
1212
Greg Daniel323fbcf2018-04-10 13:46:30 -04001213bool GrBackendRenderTarget::getMockRenderTargetInfo(GrMockRenderTargetInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001214 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -04001215 *outInfo = fMockInfo;
1216 return true;
Brian Salomon0c51eea2018-03-09 17:02:09 -05001217 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001218 return false;
Brian Salomon0c51eea2018-03-09 17:02:09 -05001219}
Robert Phillips8caf85f2018-04-05 09:30:38 -04001220
Greg Daniel6c6caf42020-05-29 12:11:05 -04001221void GrBackendRenderTarget::setMutableState(const GrBackendSurfaceMutableState& state) {
1222 fMutableState->set(state);
1223}
1224
Brian Salomon4456a0d2019-07-18 15:05:11 -04001225bool GrBackendRenderTarget::isProtected() const {
1226 if (!this->isValid() || this->backend() != GrBackendApi::kVulkan) {
1227 return false;
1228 }
1229 return fVkInfo.isProtected();
1230}
1231
Robert Phillips8caf85f2018-04-05 09:30:38 -04001232#if GR_TEST_UTILS
1233bool GrBackendRenderTarget::TestingOnly_Equals(const GrBackendRenderTarget& r0,
1234 const GrBackendRenderTarget& r1) {
1235 if (!r0.isValid() || !r1.isValid()) {
1236 return false; // two invalid backend rendertargets are not considered equal
1237 }
1238
1239 if (r0.fWidth != r1.fWidth ||
1240 r0.fHeight != r1.fHeight ||
1241 r0.fSampleCnt != r1.fSampleCnt ||
1242 r0.fStencilBits != r1.fStencilBits ||
Robert Phillips8caf85f2018-04-05 09:30:38 -04001243 r0.fBackend != r1.fBackend) {
1244 return false;
1245 }
1246
1247 switch (r0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -04001248#ifdef SK_GL
1249 case GrBackendApi::kOpenGL:
1250 return r0.fGLInfo == r1.fGLInfo;
1251#endif
1252 case GrBackendApi::kMock:
1253 return r0.fMockInfo == r1.fMockInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -04001254#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -04001255 case GrBackendApi::kVulkan:
1256 return r0.fVkInfo == r1.fVkInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -04001257#endif
Timothy Liang4e85e802018-06-28 16:37:18 -04001258#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -04001259 case GrBackendApi::kMetal:
1260 return r0.fMtlInfo == r1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -04001261#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001262#ifdef SK_DIRECT3D
1263 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -04001264 return r0.fD3DInfo == r1.fD3DInfo;
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001265#endif
Stephen White985741a2019-07-18 11:43:45 -04001266#ifdef SK_DAWN
1267 case GrBackendApi::kDawn:
1268 return r0.fDawnInfo == r1.fDawnInfo;
1269#endif
Brian Salomone2826ab2019-06-04 15:58:31 -04001270 default:
1271 return false;
Robert Phillips8caf85f2018-04-05 09:30:38 -04001272 }
1273
1274 SkASSERT(0);
1275 return false;
1276}
1277#endif