blob: f1357d6b50bdcee3bd2c4b12c5ae1b0fa3ad5ea4 [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
Greg Daniel0e9d34d2021-08-13 16:20:18 -040085
86static GrTextureType gl_target_to_gr_target(GrGLenum target) {
87 switch (target) {
88 case GR_GL_TEXTURE_NONE:
89 return GrTextureType::kNone;
90 case GR_GL_TEXTURE_2D:
91 return GrTextureType::k2D;
92 case GR_GL_TEXTURE_RECTANGLE:
93 return GrTextureType::kRectangle;
94 case GR_GL_TEXTURE_EXTERNAL:
95 return GrTextureType::kExternal;
96 default:
97 SkUNREACHABLE;
98 }
99}
100
Robert Phillipsfc711a22018-02-13 17:03:00 -0500101GrBackendFormat::GrBackendFormat(GrGLenum format, GrGLenum target)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400102 : fBackend(GrBackendApi::kOpenGL)
Greg Daniel4065d452018-11-16 15:43:41 -0500103 , fValid(true)
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400104 , fGLFormat(format)
105 , fTextureType(gl_target_to_gr_target(target)) {}
Robert Phillipsfc711a22018-02-13 17:03:00 -0500106
Brian Salomond4764a12019-08-08 12:08:24 -0400107GrGLFormat GrBackendFormat::asGLFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400108 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400109 return GrGLFormatFromGLEnum(fGLFormat);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500110 }
Brian Salomond4764a12019-08-08 12:08:24 -0400111 return GrGLFormat::kUnknown;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500112}
Greg Daniel84261652021-09-19 17:53:40 -0400113#endif
Robert Phillipsfc711a22018-02-13 17:03:00 -0500114
Greg Daniel84261652021-09-19 17:53:40 -0400115#ifdef SK_VULKAN
Greg Daniel7e000222018-12-03 10:08:21 -0500116GrBackendFormat GrBackendFormat::MakeVk(const GrVkYcbcrConversionInfo& ycbcrInfo) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700117 SkASSERT(ycbcrInfo.isValid());
118 return GrBackendFormat(ycbcrInfo.fFormat, ycbcrInfo);
Greg Daniel7e000222018-12-03 10:08:21 -0500119}
120
121GrBackendFormat::GrBackendFormat(VkFormat vkFormat, const GrVkYcbcrConversionInfo& ycbcrInfo)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400122 : fBackend(GrBackendApi::kVulkan)
Robert Phillipsfc711a22018-02-13 17:03:00 -0500123 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500124 , fTextureType(GrTextureType::k2D) {
Greg Daniel7e000222018-12-03 10:08:21 -0500125 fVk.fFormat = vkFormat;
126 fVk.fYcbcrConversionInfo = ycbcrInfo;
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700127 if (fVk.fYcbcrConversionInfo.isValid() && fVk.fYcbcrConversionInfo.fExternalFormat) {
Greg Daniel387ec9a2019-03-07 16:44:54 -0500128 fTextureType = GrTextureType::kExternal;
129 }
Robert Phillipsfc711a22018-02-13 17:03:00 -0500130}
131
Brian Salomond4764a12019-08-08 12:08:24 -0400132bool GrBackendFormat::asVkFormat(VkFormat* format) const {
133 SkASSERT(format);
Greg Danielbdf12ad2018-10-12 09:31:11 -0400134 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400135 *format = fVk.fFormat;
136 return true;
Greg Daniel7e000222018-12-03 10:08:21 -0500137 }
Brian Salomond4764a12019-08-08 12:08:24 -0400138 return false;
Greg Daniel7e000222018-12-03 10:08:21 -0500139}
140
141const GrVkYcbcrConversionInfo* GrBackendFormat::getVkYcbcrConversionInfo() const {
142 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
143 return &fVk.fYcbcrConversionInfo;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500144 }
145 return nullptr;
146}
Greg Daniel84261652021-09-19 17:53:40 -0400147#endif
Robert Phillipsfc711a22018-02-13 17:03:00 -0500148
Stephen White985741a2019-07-18 11:43:45 -0400149#ifdef SK_DAWN
Stephen White3cc8d4f2019-10-30 09:56:23 -0400150GrBackendFormat::GrBackendFormat(wgpu::TextureFormat format)
Stephen White985741a2019-07-18 11:43:45 -0400151 : fBackend(GrBackendApi::kDawn)
152 , fValid(true)
153 , fDawnFormat(format)
154 , fTextureType(GrTextureType::k2D) {
155}
156
Stephen White3cc8d4f2019-10-30 09:56:23 -0400157bool GrBackendFormat::asDawnFormat(wgpu::TextureFormat* format) const {
Brian Salomond4764a12019-08-08 12:08:24 -0400158 SkASSERT(format);
Stephen White985741a2019-07-18 11:43:45 -0400159 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400160 *format = fDawnFormat;
161 return true;
Stephen White985741a2019-07-18 11:43:45 -0400162 }
Brian Salomond4764a12019-08-08 12:08:24 -0400163 return false;
Stephen White985741a2019-07-18 11:43:45 -0400164}
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
Brian Salomond4764a12019-08-08 12:08:24 -0400175GrMTLPixelFormat GrBackendFormat::asMtlFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400176 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400177 return fMtlFormat;
Timothy Liang4e85e802018-06-28 16:37:18 -0400178 }
Brian Salomond4764a12019-08-08 12:08:24 -0400179 // MTLPixelFormatInvalid == 0
180 return GrMTLPixelFormat(0);
Timothy Liang4e85e802018-06-28 16:37:18 -0400181}
182#endif
183
Jim Van Verthc86c83c2020-02-27 15:48:24 -0500184#ifdef SK_DIRECT3D
185GrBackendFormat::GrBackendFormat(DXGI_FORMAT dxgiFormat)
186 : fBackend(GrBackendApi::kDirect3D)
187 , fValid(true)
188 , fDxgiFormat(dxgiFormat)
189 , fTextureType(GrTextureType::k2D) {
190}
191
192bool GrBackendFormat::asDxgiFormat(DXGI_FORMAT* dxgiFormat) const {
193 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
194 *dxgiFormat = fDxgiFormat;
195 return true;
196 }
197 return false;
198}
199#endif
200
Greg Daniela7f69c22020-10-07 13:04:15 -0400201GrBackendFormat::GrBackendFormat(GrColorType colorType, SkImage::CompressionType compression,
202 bool isStencilFormat)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400203 : fBackend(GrBackendApi::kMock)
Robert Phillipsfc711a22018-02-13 17:03:00 -0500204 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500205 , fTextureType(GrTextureType::k2D) {
Robert Phillipsa27d6252019-12-10 14:48:36 -0500206 fMock.fColorType = colorType;
207 fMock.fCompressionType = compression;
Greg Daniela7f69c22020-10-07 13:04:15 -0400208 fMock.fIsStencilFormat = isStencilFormat;
209 SkASSERT(this->validateMock());
Robert Phillipsfc711a22018-02-13 17:03:00 -0500210}
211
Brian Salomondc8fcdb2020-03-26 16:45:05 -0400212uint32_t GrBackendFormat::channelMask() const {
213 if (!this->isValid()) {
214 return 0;
215 }
216 switch (fBackend) {
217#ifdef SK_GL
218 case GrBackendApi::kOpenGL:
219 return GrGLFormatChannels(GrGLFormatFromGLEnum(fGLFormat));
220#endif
221#ifdef SK_VULKAN
222 case GrBackendApi::kVulkan:
223 return GrVkFormatChannels(fVk.fFormat);
224#endif
225#ifdef SK_METAL
226 case GrBackendApi::kMetal:
227 return GrMtlFormatChannels(fMtlFormat);
228#endif
229#ifdef SK_DAWN
230 case GrBackendApi::kDawn:
231 return GrDawnFormatChannels(fDawnFormat);
232#endif
233#ifdef SK_DIRECT3D
234 case GrBackendApi::kDirect3D:
235 return GrDxgiFormatChannels(fDxgiFormat);
236#endif
237 case GrBackendApi::kMock:
238 return GrColorTypeChannelFlags(fMock.fColorType);
239
240 default:
241 return 0;
242 }
243}
244
Brian Salomond6584bd2021-05-18 16:29:09 -0400245GrColorFormatDesc GrBackendFormat::desc() const {
246 if (!this->isValid()) {
247 return GrColorFormatDesc::MakeInvalid();
248 }
249 switch (fBackend) {
250#ifdef SK_GL
251 case GrBackendApi::kOpenGL:
252 return GrGLFormatDesc(GrGLFormatFromGLEnum(fGLFormat));
253#endif
254#ifdef SK_VULKAN
255 case GrBackendApi::kVulkan:
256 return GrVkFormatDesc(fVk.fFormat);
257#endif
258#ifdef SK_METAL
259 case GrBackendApi::kMetal:
260 return GrMtlFormatDesc(fMtlFormat);
261#endif
262#ifdef SK_DAWN
263 case GrBackendApi::kDawn:
264 return GrDawnFormatDesc(fDawnFormat);
265#endif
266#ifdef SK_DIRECT3D
267 case GrBackendApi::kDirect3D:
268 return GrDxgiFormatDesc(fDxgiFormat);
269#endif
270 case GrBackendApi::kMock:
271 return GrGetColorTypeDesc(fMock.fColorType);
272
273 default:
274 return GrColorFormatDesc::MakeInvalid();
275 }
276}
277
Greg Daniela7f69c22020-10-07 13:04:15 -0400278#ifdef SK_DEBUG
279bool GrBackendFormat::validateMock() const {
280 int trueStates = 0;
281 if (fMock.fCompressionType != SkImage::CompressionType::kNone) {
282 trueStates++;
283 }
284 if (fMock.fColorType != GrColorType::kUnknown) {
285 trueStates++;
286 }
287 if (fMock.fIsStencilFormat) {
288 trueStates++;
289 }
290 return trueStates == 1;
291}
292#endif
293
Brian Salomond4764a12019-08-08 12:08:24 -0400294GrColorType GrBackendFormat::asMockColorType() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400295 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniela7f69c22020-10-07 13:04:15 -0400296 SkASSERT(this->validateMock());
Robert Phillipsa27d6252019-12-10 14:48:36 -0500297 return fMock.fColorType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500298 }
Robert Phillipsa27d6252019-12-10 14:48:36 -0500299
Brian Salomond4764a12019-08-08 12:08:24 -0400300 return GrColorType::kUnknown;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500301}
302
Robert Phillipsa27d6252019-12-10 14:48:36 -0500303SkImage::CompressionType GrBackendFormat::asMockCompressionType() const {
304 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniela7f69c22020-10-07 13:04:15 -0400305 SkASSERT(this->validateMock());
Robert Phillipsa27d6252019-12-10 14:48:36 -0500306 return fMock.fCompressionType;
307 }
308
309 return SkImage::CompressionType::kNone;
310}
311
Greg Daniela7f69c22020-10-07 13:04:15 -0400312bool GrBackendFormat::isMockStencilFormat() const {
313 if (this->isValid() && GrBackendApi::kMock == fBackend) {
314 SkASSERT(this->validateMock());
315 return fMock.fIsStencilFormat;
316 }
317
318 return false;
319}
Robert Phillipsa27d6252019-12-10 14:48:36 -0500320
Greg Daniel4065d452018-11-16 15:43:41 -0500321GrBackendFormat GrBackendFormat::makeTexture2D() const {
Greg Daniel4065d452018-11-16 15:43:41 -0500322 GrBackendFormat copy = *this;
Greg Daniel84261652021-09-19 17:53:40 -0400323#ifdef SK_VULKAN
Greg Daniel387ec9a2019-03-07 16:44:54 -0500324 if (const GrVkYcbcrConversionInfo* ycbcrInfo = this->getVkYcbcrConversionInfo()) {
325 if (ycbcrInfo->isValid()) {
326 // If we have a ycbcr we remove it from the backend format and set the VkFormat to
327 // R8G8B8A8_UNORM
328 SkASSERT(copy.fBackend == GrBackendApi::kVulkan);
329 copy.fVk.fYcbcrConversionInfo = GrVkYcbcrConversionInfo();
330 copy.fVk.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
331 }
332 }
Greg Daniel84261652021-09-19 17:53:40 -0400333#endif
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)
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400458 , fTextureType(GrTextureType::k2D)
Stephen White985741a2019-07-18 11:43:45 -0400459 , fDawnInfo(dawnInfo) {}
460#endif
461
Greg Danielb4d89562018-10-03 18:44:49 +0000462#ifdef SK_VULKAN
Greg Daniel6c6caf42020-05-29 12:11:05 -0400463GrBackendTexture::GrBackendTexture(int width, int height, const GrVkImageInfo& vkInfo)
Brian Salomon4456a0d2019-07-18 15:05:11 -0400464 : GrBackendTexture(width, height, vkInfo,
Greg Daniel6c6caf42020-05-29 12:11:05 -0400465 sk_sp<GrBackendSurfaceMutableStateImpl>(
466 new GrBackendSurfaceMutableStateImpl(
467 vkInfo.fImageLayout, vkInfo.fCurrentQueueFamily))) {}
468
Greg Daniel7b62dca2020-08-21 11:26:12 -0400469static const VkImageUsageFlags kDefaultUsageFlags =
470 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
471
472// We don't know if the backend texture is made renderable or not, so we default the usage flags
473// to include color attachment as well.
474static const VkImageUsageFlags kDefaultTexRTUsageFlags =
475 kDefaultUsageFlags | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
476
477static GrVkImageInfo apply_default_usage_flags(const GrVkImageInfo& info,
478 VkImageUsageFlags defaultFlags) {
479 if (info.fImageUsageFlags == 0) {
480 GrVkImageInfo newInfo = info;
481 newInfo.fImageUsageFlags = defaultFlags;
482 return newInfo;
483 }
484 return info;
485}
486
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400487static GrTextureType vk_image_info_to_texture_type(const GrVkImageInfo& info) {
488 if (info.fYcbcrConversionInfo.isValid() && info.fYcbcrConversionInfo.fExternalFormat != 0) {
489 return GrTextureType::kExternal;
490 }
491 return GrTextureType::k2D;
492}
493
Greg Daniel6c6caf42020-05-29 12:11:05 -0400494GrBackendTexture::GrBackendTexture(int width,
495 int height,
496 const GrVkImageInfo& vkInfo,
497 sk_sp<GrBackendSurfaceMutableStateImpl> mutableState)
498 : fIsValid(true)
499 , fWidth(width)
500 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400501 , fMipmapped(GrMipmapped(vkInfo.fLevelCount > 1))
Greg Daniel6c6caf42020-05-29 12:11:05 -0400502 , fBackend(GrBackendApi::kVulkan)
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400503 , fTextureType(vk_image_info_to_texture_type(vkInfo))
Greg Daniel7b62dca2020-08-21 11:26:12 -0400504 , fVkInfo(apply_default_usage_flags(vkInfo, kDefaultTexRTUsageFlags))
Greg Daniel6c6caf42020-05-29 12:11:05 -0400505 , fMutableState(std::move(mutableState)) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000506#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400507
Brian Salomone2826ab2019-06-04 15:58:31 -0400508#ifdef SK_GL
509GrBackendTexture::GrBackendTexture(int width,
510 int height,
Brian Salomon40a40622020-07-21 10:32:07 -0400511 GrMipmapped mipmapped,
Brian Salomone2826ab2019-06-04 15:58:31 -0400512 const GrGLTextureInfo glInfo,
513 sk_sp<GrGLTextureParameters> params)
514 : fIsValid(true)
515 , fWidth(width)
516 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400517 , fMipmapped(mipmapped)
Brian Salomone2826ab2019-06-04 15:58:31 -0400518 , fBackend(GrBackendApi::kOpenGL)
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400519 , fTextureType(gl_target_to_gr_target(glInfo.fTarget))
Brian Salomone2826ab2019-06-04 15:58:31 -0400520 , fGLInfo(glInfo, params.release()) {}
521
522sk_sp<GrGLTextureParameters> GrBackendTexture::getGLTextureParams() const {
523 if (fBackend != GrBackendApi::kOpenGL) {
524 return nullptr;
525 }
526 return fGLInfo.refParameters();
527}
528#endif
529
Timothy Liang4e85e802018-06-28 16:37:18 -0400530#ifdef SK_METAL
531GrBackendTexture::GrBackendTexture(int width,
532 int height,
Brian Salomon40a40622020-07-21 10:32:07 -0400533 GrMipmapped mipmapped,
Timothy Liang4e85e802018-06-28 16:37:18 -0400534 const GrMtlTextureInfo& mtlInfo)
535 : fIsValid(true)
536 , fWidth(width)
537 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400538 , fMipmapped(mipmapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400539 , fBackend(GrBackendApi::kMetal)
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400540 , fTextureType(GrTextureType::k2D)
Timothy Liang4e85e802018-06-28 16:37:18 -0400541 , fMtlInfo(mtlInfo) {}
542#endif
543
Jim Van Verth05d09192020-03-20 11:23:39 -0400544#ifdef SK_DIRECT3D
Jim Van Verth6ec56882020-03-26 11:47:26 -0400545GrBackendTexture::GrBackendTexture(int width, int height, const GrD3DTextureResourceInfo& d3dInfo)
Jim Van Verth05d09192020-03-20 11:23:39 -0400546 : GrBackendTexture(
547 width, height, d3dInfo,
548 sk_sp<GrD3DResourceState>(new GrD3DResourceState(
549 static_cast<D3D12_RESOURCE_STATES>(d3dInfo.fResourceState)))) {}
550
551GrBackendTexture::GrBackendTexture(int width,
552 int height,
Jim Van Verth6ec56882020-03-26 11:47:26 -0400553 const GrD3DTextureResourceInfo& d3dInfo,
Jim Van Verth05d09192020-03-20 11:23:39 -0400554 sk_sp<GrD3DResourceState> state)
555 : fIsValid(true)
556 , fWidth(width)
557 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400558 , fMipmapped(GrMipmapped(d3dInfo.fLevelCount > 1))
Jim Van Verth05d09192020-03-20 11:23:39 -0400559 , fBackend(GrBackendApi::kDirect3D)
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400560 , fTextureType(GrTextureType::k2D)
Jim Van Verth05d09192020-03-20 11:23:39 -0400561 , fD3DInfo(d3dInfo, state.release()) {}
562#endif
563
John Rosascoa9b348f2019-11-08 13:18:15 -0800564#ifdef SK_GL
Brian Salomon8fe24272017-07-07 12:56:11 -0400565GrBackendTexture::GrBackendTexture(int width,
566 int height,
Brian Salomon40a40622020-07-21 10:32:07 -0400567 GrMipmapped mipmapped,
Greg Daniele7d8da42017-12-04 11:23:19 -0500568 const GrGLTextureInfo& glInfo)
Brian Salomon40a40622020-07-21 10:32:07 -0400569 : GrBackendTexture(width, height, mipmapped, glInfo, sk_make_sp<GrGLTextureParameters>()) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400570 // Make no assumptions about client's texture's parameters.
571 this->glTextureParametersModified();
572}
John Rosascoa9b348f2019-11-08 13:18:15 -0800573#endif
Greg Daniele7d8da42017-12-04 11:23:19 -0500574
575GrBackendTexture::GrBackendTexture(int width,
576 int height,
Brian Salomon40a40622020-07-21 10:32:07 -0400577 GrMipmapped mipmapped,
Greg Daniel177e6952017-10-12 12:27:11 -0400578 const GrMockTextureInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400579 : fIsValid(true)
580 , fWidth(width)
Brian Salomon8fe24272017-07-07 12:56:11 -0400581 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400582 , fMipmapped(mipmapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400583 , fBackend(GrBackendApi::kMock)
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400584 , fTextureType(GrTextureType::k2D)
Brian Salomon8fe24272017-07-07 12:56:11 -0400585 , fMockInfo(mockInfo) {}
586
Greg Daniel52e16d92018-04-10 09:34:07 -0400587GrBackendTexture::~GrBackendTexture() {
588 this->cleanup();
589}
590
591void GrBackendTexture::cleanup() {
Brian Salomone2826ab2019-06-04 15:58:31 -0400592#ifdef SK_GL
593 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
594 fGLInfo.cleanup();
595 }
596#endif
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000597#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400598 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400599 fVkInfo.cleanup();
600 }
601#endif
Jim Van Verth05d09192020-03-20 11:23:39 -0400602#ifdef SK_DIRECT3D
603 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
604 fD3DInfo.cleanup();
605 }
606#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400607}
608
609GrBackendTexture::GrBackendTexture(const GrBackendTexture& that) : fIsValid(false) {
610 *this = that;
611}
612
613GrBackendTexture& GrBackendTexture::operator=(const GrBackendTexture& that) {
614 if (!that.isValid()) {
615 this->cleanup();
616 fIsValid = false;
617 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -0400618 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400619 this->cleanup();
620 fIsValid = false;
Greg Daniel52e16d92018-04-10 09:34:07 -0400621 }
622 fWidth = that.fWidth;
623 fHeight = that.fHeight;
Brian Salomon40a40622020-07-21 10:32:07 -0400624 fMipmapped = that.fMipmapped;
Greg Daniel52e16d92018-04-10 09:34:07 -0400625 fBackend = that.fBackend;
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400626 fTextureType = that.fTextureType;
Greg Daniel52e16d92018-04-10 09:34:07 -0400627
628 switch (that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400629#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400630 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400631 fGLInfo.assign(that.fGLInfo, this->isValid());
Greg Daniel52e16d92018-04-10 09:34:07 -0400632 break;
Mike Kleina55e2142018-10-03 16:34:11 +0000633#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400634#ifdef SK_VULKAN
635 case GrBackendApi::kVulkan:
636 fVkInfo.assign(that.fVkInfo, this->isValid());
Greg Danielb4d89562018-10-03 18:44:49 +0000637 break;
Brian Salomone2826ab2019-06-04 15:58:31 -0400638#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400639#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400640 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000641 fMtlInfo = that.fMtlInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400642 break;
643#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500644#ifdef SK_DIRECT3D
645 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -0400646 fD3DInfo.assign(that.fD3DInfo, this->isValid());
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500647 break;
648#endif
Stephen White985741a2019-07-18 11:43:45 -0400649#ifdef SK_DAWN
650 case GrBackendApi::kDawn:
651 fDawnInfo = that.fDawnInfo;
652 break;
653#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400654 case GrBackendApi::kMock:
Greg Daniel52e16d92018-04-10 09:34:07 -0400655 fMockInfo = that.fMockInfo;
656 break;
657 default:
658 SK_ABORT("Unknown GrBackend");
659 }
Greg Daniel6c6caf42020-05-29 12:11:05 -0400660 fMutableState = that.fMutableState;
Brian Salomone2826ab2019-06-04 15:58:31 -0400661 fIsValid = true;
Greg Daniel52e16d92018-04-10 09:34:07 -0400662 return *this;
663}
664
Greg Daniel6c6caf42020-05-29 12:11:05 -0400665sk_sp<GrBackendSurfaceMutableStateImpl> GrBackendTexture::getMutableState() const {
666 return fMutableState;
667}
668
Stephen White985741a2019-07-18 11:43:45 -0400669#ifdef SK_DAWN
Stephen Whitef03c1162020-01-28 12:39:45 -0500670bool GrBackendTexture::getDawnTextureInfo(GrDawnTextureInfo* outInfo) const {
Stephen White985741a2019-07-18 11:43:45 -0400671 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
672 *outInfo = fDawnInfo;
673 return true;
674 }
675 return false;
676}
677#endif
678
Jim Van Verth2f7ee022021-09-18 16:33:57 +0000679#ifdef SK_VULKAN
Greg Daniel84261652021-09-19 17:53:40 -0400680bool GrBackendTexture::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400681 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -0400682 *outInfo = fVkInfo.snapImageInfo(fMutableState.get());
Greg Daniel52e16d92018-04-10 09:34:07 -0400683 return true;
684 }
685 return false;
686}
687
688void GrBackendTexture::setVkImageLayout(VkImageLayout layout) {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400689 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -0400690 fMutableState->setImageLayout(layout);
Greg Daniel52e16d92018-04-10 09:34:07 -0400691 }
Jim Van Verth2f7ee022021-09-18 16:33:57 +0000692}
Greg Daniel84261652021-09-19 17:53:40 -0400693#endif
Greg Daniel94403452017-04-18 15:52:36 -0400694
Timothy Liang4e85e802018-06-28 16:37:18 -0400695#ifdef SK_METAL
696bool GrBackendTexture::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400697 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000698 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400699 return true;
700 }
701 return false;
702}
703#endif
704
Jim Van Verth05d09192020-03-20 11:23:39 -0400705#ifdef SK_DIRECT3D
Jim Van Verth6ec56882020-03-26 11:47:26 -0400706bool GrBackendTexture::getD3DTextureResourceInfo(GrD3DTextureResourceInfo* outInfo) const {
Jim Van Verth05d09192020-03-20 11:23:39 -0400707 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
Jim Van Verth6ec56882020-03-26 11:47:26 -0400708 *outInfo = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -0400709 return true;
710 }
711 return false;
712}
713
714void GrBackendTexture::setD3DResourceState(GrD3DResourceStateEnum state) {
715 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
716 fD3DInfo.setResourceState(state);
717 }
718}
719
720sk_sp<GrD3DResourceState> GrBackendTexture::getGrD3DResourceState() const {
721 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
722 return fD3DInfo.getGrD3DResourceState();
723 }
724 return nullptr;
725}
726#endif
727
Jim Van Verth2f7ee022021-09-18 16:33:57 +0000728#ifdef SK_GL
Greg Daniel84261652021-09-19 17:53:40 -0400729bool GrBackendTexture::getGLTextureInfo(GrGLTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400730 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400731 *outInfo = fGLInfo.info();
Greg Daniel52e16d92018-04-10 09:34:07 -0400732 return true;
Greg Daniel84261652021-09-19 17:53:40 -0400733 }
734 else if (this->isValid() && GrBackendApi::kMock == fBackend) {
Brian Osmana7ef3a02019-03-27 15:54:14 -0400735 // Hack! This allows some blink unit tests to work when using the Mock GrContext.
736 // Specifically, tests that rely on CanvasResourceProviderTextureGpuMemoryBuffer.
737 // If that code ever goes away (or ideally becomes backend-agnostic), this can go away.
738 *outInfo = GrGLTextureInfo{ GR_GL_TEXTURE_2D,
Robert Phillipsa27d6252019-12-10 14:48:36 -0500739 static_cast<GrGLuint>(fMockInfo.id()),
Brian Osmana7ef3a02019-03-27 15:54:14 -0400740 GR_GL_RGBA8 };
741 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400742 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400743 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400744}
745
Brian Salomone2826ab2019-06-04 15:58:31 -0400746void GrBackendTexture::glTextureParametersModified() {
Brian Salomone2826ab2019-06-04 15:58:31 -0400747 if (this->isValid() && fBackend == GrBackendApi::kOpenGL) {
748 fGLInfo.parameters()->invalidate();
749 }
Jim Van Verth2f7ee022021-09-18 16:33:57 +0000750}
Greg Daniel84261652021-09-19 17:53:40 -0400751#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400752
Greg Daniel52e16d92018-04-10 09:34:07 -0400753bool GrBackendTexture::getMockTextureInfo(GrMockTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400754 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400755 *outInfo = fMockInfo;
756 return true;
Brian Salomon8fe24272017-07-07 12:56:11 -0400757 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400758 return false;
Brian Salomon8fe24272017-07-07 12:56:11 -0400759}
760
Greg Daniel6c6caf42020-05-29 12:11:05 -0400761void GrBackendTexture::setMutableState(const GrBackendSurfaceMutableState& state) {
762 fMutableState->set(state);
763}
764
Brian Salomon4456a0d2019-07-18 15:05:11 -0400765bool GrBackendTexture::isProtected() const {
Greg Daniel84261652021-09-19 17:53:40 -0400766 if (!this->isValid()) {
Brian Salomon4456a0d2019-07-18 15:05:11 -0400767 return false;
768 }
Greg Daniel84261652021-09-19 17:53:40 -0400769#ifdef SK_VULKAN
770 if (this->backend() == GrBackendApi::kVulkan) {
771 return fVkInfo.isProtected();
772 }
773#endif
774 return false;
Brian Salomon4456a0d2019-07-18 15:05:11 -0400775}
776
Brian Salomonaad83152019-05-24 10:16:35 -0400777bool GrBackendTexture::isSameTexture(const GrBackendTexture& that) {
778 if (!this->isValid() || !that.isValid()) {
779 return false;
780 }
781 if (fBackend != that.fBackend) {
782 return false;
783 }
784 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400785#ifdef SK_GL
Brian Salomonaad83152019-05-24 10:16:35 -0400786 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400787 return fGLInfo.info().fID == that.fGLInfo.info().fID;
788#endif
Brian Salomonaad83152019-05-24 10:16:35 -0400789#ifdef SK_VULKAN
790 case GrBackendApi::kVulkan:
Greg Daniel6c6caf42020-05-29 12:11:05 -0400791 return fVkInfo.snapImageInfo(fMutableState.get()).fImage ==
792 that.fVkInfo.snapImageInfo(that.fMutableState.get()).fImage;
Brian Salomonaad83152019-05-24 10:16:35 -0400793#endif
794#ifdef SK_METAL
795 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000796 return this->fMtlInfo.fTexture == that.fMtlInfo.fTexture;
Brian Salomonaad83152019-05-24 10:16:35 -0400797#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500798#ifdef SK_DIRECT3D
799 case GrBackendApi::kDirect3D:
Jim Van Verthde175ab2020-06-10 16:12:25 -0400800 return fD3DInfo.snapTextureResourceInfo().fResource ==
801 that.fD3DInfo.snapTextureResourceInfo().fResource;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500802#endif
Stephen Whited4b8bf92020-06-08 15:38:29 -0400803#ifdef SK_DAWN
804 case GrBackendApi::kDawn: {
805 return this->fDawnInfo.fTexture.Get() == that.fDawnInfo.fTexture.Get();
806 }
807#endif
Brian Salomonaad83152019-05-24 10:16:35 -0400808 case GrBackendApi::kMock:
Robert Phillipsa27d6252019-12-10 14:48:36 -0500809 return fMockInfo.id() == that.fMockInfo.id();
Brian Salomonaad83152019-05-24 10:16:35 -0400810 default:
811 return false;
812 }
813}
814
Brian Salomonf391d0f2018-12-14 09:18:50 -0500815GrBackendFormat GrBackendTexture::getBackendFormat() const {
816 if (!this->isValid()) {
817 return GrBackendFormat();
818 }
819 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400820#ifdef SK_GL
Brian Salomonf391d0f2018-12-14 09:18:50 -0500821 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400822 return GrBackendFormat::MakeGL(fGLInfo.info().fFormat, fGLInfo.info().fTarget);
823#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500824#ifdef SK_VULKAN
825 case GrBackendApi::kVulkan: {
Greg Daniel6c6caf42020-05-29 12:11:05 -0400826 auto info = fVkInfo.snapImageInfo(fMutableState.get());
Brian Salomonf391d0f2018-12-14 09:18:50 -0500827 if (info.fYcbcrConversionInfo.isValid()) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700828 SkASSERT(info.fFormat == info.fYcbcrConversionInfo.fFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500829 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
830 }
831 return GrBackendFormat::MakeVk(info.fFormat);
832 }
833#endif
834#ifdef SK_METAL
835 case GrBackendApi::kMetal: {
836 GrMtlTextureInfo mtlInfo;
837 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
838 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
839 }
840#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500841#ifdef SK_DIRECT3D
842 case GrBackendApi::kDirect3D: {
Jim Van Verth6ec56882020-03-26 11:47:26 -0400843 auto d3dInfo = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -0400844 return GrBackendFormat::MakeDxgi(d3dInfo.fFormat);
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500845 }
846#endif
847#ifdef SK_DAWN
848 case GrBackendApi::kDawn: {
849 return GrBackendFormat::MakeDawn(fDawnInfo.fFormat);
850 }
851#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500852 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400853 return fMockInfo.getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500854 default:
855 return GrBackendFormat();
856 }
857}
858
Robert Phillipsc5509952018-04-04 15:54:55 -0400859#if GR_TEST_UTILS
860bool GrBackendTexture::TestingOnly_Equals(const GrBackendTexture& t0, const GrBackendTexture& t1) {
861 if (!t0.isValid() || !t1.isValid()) {
862 return false; // two invalid backend textures are not considered equal
863 }
864
865 if (t0.fWidth != t1.fWidth ||
866 t0.fHeight != t1.fHeight ||
Brian Salomon40a40622020-07-21 10:32:07 -0400867 t0.fMipmapped != t1.fMipmapped ||
Robert Phillipsc5509952018-04-04 15:54:55 -0400868 t0.fBackend != t1.fBackend) {
869 return false;
870 }
871
Greg Daniel7f3408b2020-06-03 13:31:00 -0400872 // For our tests when checking equality we are assuming the both backendTexture objects will
873 // be using the same mutable state object.
874 if (t0.fMutableState != t1.fMutableState) {
875 return false;
876 }
877
Robert Phillipsc5509952018-04-04 15:54:55 -0400878 switch (t0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400879#ifdef SK_GL
880 case GrBackendApi::kOpenGL:
881 return t0.fGLInfo.info() == t1.fGLInfo.info();
882#endif
883 case GrBackendApi::kMock:
884 return t0.fMockInfo == t1.fMockInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400885#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400886 case GrBackendApi::kVulkan:
887 return t0.fVkInfo == t1.fVkInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400888#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400889#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -0400890 case GrBackendApi::kMetal:
891 return t0.fMtlInfo == t1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400892#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500893#ifdef SK_DIRECT3D
894 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -0400895 return t0.fD3DInfo == t1.fD3DInfo;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500896#endif
Stephen White985741a2019-07-18 11:43:45 -0400897#ifdef SK_DAWN
898 case GrBackendApi::kDawn:
899 return t0.fDawnInfo == t1.fDawnInfo;
900#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400901 default:
902 return false;
Robert Phillipsc5509952018-04-04 15:54:55 -0400903 }
Robert Phillipsc5509952018-04-04 15:54:55 -0400904}
905#endif
906
Greg Daniel94403452017-04-18 15:52:36 -0400907////////////////////////////////////////////////////////////////////////////////////////////////////
908
Greg Daniel6c6caf42020-05-29 12:11:05 -0400909GrBackendRenderTarget::GrBackendRenderTarget() : fIsValid(false) {}
910
911
Stephen White985741a2019-07-18 11:43:45 -0400912#ifdef SK_DAWN
913GrBackendRenderTarget::GrBackendRenderTarget(int width,
914 int height,
915 int sampleCnt,
916 int stencilBits,
Stephen Whitef03c1162020-01-28 12:39:45 -0500917 const GrDawnRenderTargetInfo& dawnInfo)
Stephen White985741a2019-07-18 11:43:45 -0400918 : fIsValid(true)
Stephen White3c0a50f2020-01-16 18:19:54 -0500919 , fFramebufferOnly(true)
Stephen White985741a2019-07-18 11:43:45 -0400920 , fWidth(width)
921 , fHeight(height)
922 , fSampleCnt(sampleCnt)
923 , fStencilBits(stencilBits)
Stephen White985741a2019-07-18 11:43:45 -0400924 , fBackend(GrBackendApi::kDawn)
925 , fDawnInfo(dawnInfo) {}
926#endif
927
Greg Daniel6c6caf42020-05-29 12:11:05 -0400928#ifdef SK_VULKAN
Brian Salomon57fd9232020-09-28 17:02:49 -0400929static GrVkImageInfo resolve_vkii_sample_count(const GrVkImageInfo& vkII, int sidebandSampleCnt) {
930 auto result = vkII;
931 result.fSampleCount = std::max({vkII.fSampleCount,
932 static_cast<uint32_t>(sidebandSampleCnt),
933 1U});
934 return result;
935}
936
Greg Daniel94403452017-04-18 15:52:36 -0400937GrBackendRenderTarget::GrBackendRenderTarget(int width,
938 int height,
939 int sampleCnt,
Brian Salomonafdc6b12018-03-09 12:02:32 -0500940 const GrVkImageInfo& vkInfo)
Brian Salomon57fd9232020-09-28 17:02:49 -0400941 : GrBackendRenderTarget(width, height, resolve_vkii_sample_count(vkInfo, sampleCnt)) {}
942
943GrBackendRenderTarget::GrBackendRenderTarget(int width,
944 int height,
945 const GrVkImageInfo& vkInfo)
946 : GrBackendRenderTarget(width, height, vkInfo,
Greg Daniel6c6caf42020-05-29 12:11:05 -0400947 sk_sp<GrBackendSurfaceMutableStateImpl>(
948 new GrBackendSurfaceMutableStateImpl(
Brian Salomon57fd9232020-09-28 17:02:49 -0400949 vkInfo.fImageLayout, vkInfo.fCurrentQueueFamily))) {}
Greg Daniel323fbcf2018-04-10 13:46:30 -0400950
Greg Daniel7b62dca2020-08-21 11:26:12 -0400951static const VkImageUsageFlags kDefaultRTUsageFlags =
952 kDefaultUsageFlags | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
953
Greg Daniel323fbcf2018-04-10 13:46:30 -0400954GrBackendRenderTarget::GrBackendRenderTarget(int width,
955 int height,
Greg Daniel323fbcf2018-04-10 13:46:30 -0400956 const GrVkImageInfo& vkInfo,
Greg Daniel6c6caf42020-05-29 12:11:05 -0400957 sk_sp<GrBackendSurfaceMutableStateImpl> mutableState)
Greg Daniel9ca30652018-04-06 09:27:20 -0400958 : fIsValid(true)
959 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400960 , fHeight(height)
Brian Salomon57fd9232020-09-28 17:02:49 -0400961 , fSampleCnt(std::max(1U, vkInfo.fSampleCount))
Brian Salomonafdc6b12018-03-09 12:02:32 -0500962 , fStencilBits(0) // We always create stencil buffers internally for vulkan
Greg Danielbdf12ad2018-10-12 09:31:11 -0400963 , fBackend(GrBackendApi::kVulkan)
Greg Daniel7b62dca2020-08-21 11:26:12 -0400964 , fVkInfo(apply_default_usage_flags(vkInfo, kDefaultRTUsageFlags))
Brian Salomon57fd9232020-09-28 17:02:49 -0400965 , fMutableState(mutableState) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000966#endif
Greg Daniel94403452017-04-18 15:52:36 -0400967
Timothy Liang4e85e802018-06-28 16:37:18 -0400968#ifdef SK_METAL
Brian Salomon8e0aa442020-09-30 13:50:18 -0400969GrBackendRenderTarget::GrBackendRenderTarget(int width, int height, const GrMtlTextureInfo& mtlInfo)
Timothy Liang4e85e802018-06-28 16:37:18 -0400970 : fIsValid(true)
Brian Salomon8e0aa442020-09-30 13:50:18 -0400971 , fFramebufferOnly(false) // TODO: set this from mtlInfo.fTexture->framebufferOnly
Timothy Liang4e85e802018-06-28 16:37:18 -0400972 , fWidth(width)
973 , fHeight(height)
Brian Salomon8e0aa442020-09-30 13:50:18 -0400974 , fSampleCnt(std::max(1, GrMtlTextureInfoSampleCount(mtlInfo)))
Timothy Liang4e85e802018-06-28 16:37:18 -0400975 , fStencilBits(0)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400976 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400977 , fMtlInfo(mtlInfo) {}
Brian Salomon8e0aa442020-09-30 13:50:18 -0400978
979GrBackendRenderTarget::GrBackendRenderTarget(int width, int height,
980 int sampleCount,
981 const GrMtlTextureInfo& mtlInfo)
Jim Van Verthd2567de2021-05-12 17:12:17 -0400982 : GrBackendRenderTarget(width, height, mtlInfo) {
983 fSampleCnt = sampleCount;
984}
Timothy Liang4e85e802018-06-28 16:37:18 -0400985#endif
986
Jim Van Verth05d09192020-03-20 11:23:39 -0400987#ifdef SK_DIRECT3D
Brian Salomon718ae762020-09-29 16:52:49 -0400988GrBackendRenderTarget::GrBackendRenderTarget(int width, int height,
Jim Van Verth6ec56882020-03-26 11:47:26 -0400989 const GrD3DTextureResourceInfo& d3dInfo)
Jim Van Verth05d09192020-03-20 11:23:39 -0400990 : GrBackendRenderTarget(
Brian Salomon718ae762020-09-29 16:52:49 -0400991 width, height, d3dInfo,
Jim Van Verth05d09192020-03-20 11:23:39 -0400992 sk_sp<GrD3DResourceState>(new GrD3DResourceState(
993 static_cast<D3D12_RESOURCE_STATES>(d3dInfo.fResourceState)))) {}
994
995GrBackendRenderTarget::GrBackendRenderTarget(int width,
996 int height,
Jim Van Verth6ec56882020-03-26 11:47:26 -0400997 const GrD3DTextureResourceInfo& d3dInfo,
Jim Van Verth05d09192020-03-20 11:23:39 -0400998 sk_sp<GrD3DResourceState> state)
999 : fIsValid(true)
1000 , fWidth(width)
1001 , fHeight(height)
Brian Salomon718ae762020-09-29 16:52:49 -04001002 , fSampleCnt(std::max(1U, d3dInfo.fSampleCount))
Jim Van Verth05d09192020-03-20 11:23:39 -04001003 , fStencilBits(0)
1004 , fBackend(GrBackendApi::kDirect3D)
1005 , fD3DInfo(d3dInfo, state.release()) {}
1006#endif
John Rosascoa9b348f2019-11-08 13:18:15 -08001007#ifdef SK_GL
Greg Danielfaa095e2017-12-19 13:15:02 -05001008GrBackendRenderTarget::GrBackendRenderTarget(int width,
1009 int height,
1010 int sampleCnt,
1011 int stencilBits,
1012 const GrGLFramebufferInfo& glInfo)
Robert Phillipsb45f47d2019-02-03 17:17:54 -05001013 : fWidth(width)
Greg Danielfaa095e2017-12-19 13:15:02 -05001014 , fHeight(height)
Brian Osman788b9162020-02-07 10:36:46 -05001015 , fSampleCnt(std::max(1, sampleCnt))
Greg Danielfaa095e2017-12-19 13:15:02 -05001016 , fStencilBits(stencilBits)
Greg Danielbdf12ad2018-10-12 09:31:11 -04001017 , fBackend(GrBackendApi::kOpenGL)
Robert Phillipsb45f47d2019-02-03 17:17:54 -05001018 , fGLInfo(glInfo) {
1019 fIsValid = SkToBool(glInfo.fFormat); // the glInfo must have a valid format
1020}
John Rosascoa9b348f2019-11-08 13:18:15 -08001021#endif
Greg Danielfaa095e2017-12-19 13:15:02 -05001022
Brian Salomon0c51eea2018-03-09 17:02:09 -05001023GrBackendRenderTarget::GrBackendRenderTarget(int width,
1024 int height,
1025 int sampleCnt,
1026 int stencilBits,
1027 const GrMockRenderTargetInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -04001028 : fIsValid(true)
1029 , fWidth(width)
Brian Salomon0c51eea2018-03-09 17:02:09 -05001030 , fHeight(height)
Brian Osman788b9162020-02-07 10:36:46 -05001031 , fSampleCnt(std::max(1, sampleCnt))
Brian Salomon0c51eea2018-03-09 17:02:09 -05001032 , fStencilBits(stencilBits)
Brian Salomon0c51eea2018-03-09 17:02:09 -05001033 , fMockInfo(mockInfo) {}
1034
Greg Daniel323fbcf2018-04-10 13:46:30 -04001035GrBackendRenderTarget::~GrBackendRenderTarget() {
1036 this->cleanup();
1037}
1038
1039void GrBackendRenderTarget::cleanup() {
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +00001040#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -04001041 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -04001042 fVkInfo.cleanup();
1043 }
1044#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001045#ifdef SK_DIRECT3D
1046 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
1047 fD3DInfo.cleanup();
1048 }
1049#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -04001050}
1051
1052GrBackendRenderTarget::GrBackendRenderTarget(const GrBackendRenderTarget& that) : fIsValid(false) {
1053 *this = that;
1054}
1055
1056GrBackendRenderTarget& GrBackendRenderTarget::operator=(const GrBackendRenderTarget& that) {
1057 if (!that.isValid()) {
1058 this->cleanup();
1059 fIsValid = false;
1060 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -04001061 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -04001062 this->cleanup();
1063 fIsValid = false;
Greg Daniel323fbcf2018-04-10 13:46:30 -04001064 }
1065 fWidth = that.fWidth;
1066 fHeight = that.fHeight;
1067 fSampleCnt = that.fSampleCnt;
1068 fStencilBits = that.fStencilBits;
Greg Daniel323fbcf2018-04-10 13:46:30 -04001069 fBackend = that.fBackend;
1070
1071 switch (that.fBackend) {
John Rosascoa9b348f2019-11-08 13:18:15 -08001072#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -04001073 case GrBackendApi::kOpenGL:
Greg Daniel323fbcf2018-04-10 13:46:30 -04001074 fGLInfo = that.fGLInfo;
1075 break;
John Rosasco078cf3e2019-10-31 16:21:39 -07001076#endif
John Rosascoa9b348f2019-11-08 13:18:15 -08001077#ifdef SK_VULKAN
1078 case GrBackendApi::kVulkan:
1079 fVkInfo.assign(that.fVkInfo, this->isValid());
Robert Phillips0efc01d2019-11-06 17:19:30 +00001080 break;
John Rosascoa9b348f2019-11-08 13:18:15 -08001081#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -04001082#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -04001083 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +00001084 fMtlInfo = that.fMtlInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -04001085 break;
1086#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001087#ifdef SK_DIRECT3D
1088 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -04001089 fD3DInfo.assign(that.fD3DInfo, this->isValid());
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001090 break;
1091#endif
1092#ifdef SK_DAWN
1093 case GrBackendApi::kDawn:
1094 fDawnInfo = that.fDawnInfo;
1095 break;
1096#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -04001097 case GrBackendApi::kMock:
Greg Daniel323fbcf2018-04-10 13:46:30 -04001098 fMockInfo = that.fMockInfo;
1099 break;
1100 default:
1101 SK_ABORT("Unknown GrBackend");
1102 }
Greg Daniel6c6caf42020-05-29 12:11:05 -04001103 fMutableState = that.fMutableState;
Greg Daniel323fbcf2018-04-10 13:46:30 -04001104 fIsValid = that.fIsValid;
1105 return *this;
1106}
1107
Greg Daniel6c6caf42020-05-29 12:11:05 -04001108sk_sp<GrBackendSurfaceMutableStateImpl> GrBackendRenderTarget::getMutableState() const {
1109 return fMutableState;
1110}
1111
Stephen White985741a2019-07-18 11:43:45 -04001112#ifdef SK_DAWN
Stephen Whitef03c1162020-01-28 12:39:45 -05001113bool GrBackendRenderTarget::getDawnRenderTargetInfo(GrDawnRenderTargetInfo* outInfo) const {
Stephen White985741a2019-07-18 11:43:45 -04001114 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
1115 *outInfo = fDawnInfo;
1116 return true;
1117 }
1118 return false;
1119}
1120#endif
1121
Jim Van Verth2f7ee022021-09-18 16:33:57 +00001122#ifdef SK_VULKAN
Greg Daniel84261652021-09-19 17:53:40 -04001123bool GrBackendRenderTarget::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001124 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -04001125 *outInfo = fVkInfo.snapImageInfo(fMutableState.get());
Greg Daniel323fbcf2018-04-10 13:46:30 -04001126 return true;
1127 }
1128 return false;
1129}
1130
1131void GrBackendRenderTarget::setVkImageLayout(VkImageLayout layout) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001132 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -04001133 fMutableState->setImageLayout(layout);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001134 }
Jim Van Verth2f7ee022021-09-18 16:33:57 +00001135}
Greg Daniel84261652021-09-19 17:53:40 -04001136#endif
Greg Daniel94403452017-04-18 15:52:36 -04001137
Timothy Liang4e85e802018-06-28 16:37:18 -04001138#ifdef SK_METAL
1139bool GrBackendRenderTarget::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001140 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +00001141 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -04001142 return true;
1143 }
1144 return false;
1145}
1146#endif
1147
Jim Van Verth05d09192020-03-20 11:23:39 -04001148#ifdef SK_DIRECT3D
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001149bool GrBackendRenderTarget::getD3DTextureResourceInfo(GrD3DTextureResourceInfo* outInfo) const {
Jim Van Verth05d09192020-03-20 11:23:39 -04001150 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
Jim Van Verth6ec56882020-03-26 11:47:26 -04001151 *outInfo = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -04001152 return true;
1153 }
1154 return false;
1155}
1156
1157void GrBackendRenderTarget::setD3DResourceState(GrD3DResourceStateEnum state) {
1158 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
1159 fD3DInfo.setResourceState(state);
1160 }
1161}
1162
1163sk_sp<GrD3DResourceState> GrBackendRenderTarget::getGrD3DResourceState() const {
1164 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
1165 return fD3DInfo.getGrD3DResourceState();
1166 }
1167 return nullptr;
1168}
1169#endif
1170
John Rosascoa9b348f2019-11-08 13:18:15 -08001171#ifdef SK_GL
Greg Daniel323fbcf2018-04-10 13:46:30 -04001172bool GrBackendRenderTarget::getGLFramebufferInfo(GrGLFramebufferInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001173 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -04001174 *outInfo = fGLInfo;
1175 return true;
Greg Daniel94403452017-04-18 15:52:36 -04001176 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001177 return false;
Greg Daniel94403452017-04-18 15:52:36 -04001178}
John Rosascoa9b348f2019-11-08 13:18:15 -08001179#endif
Greg Daniel94403452017-04-18 15:52:36 -04001180
Robert Phillipsf209e882019-06-25 15:59:50 -04001181GrBackendFormat GrBackendRenderTarget::getBackendFormat() const {
1182 if (!this->isValid()) {
1183 return GrBackendFormat();
1184 }
1185 switch (fBackend) {
1186#ifdef SK_GL
1187 case GrBackendApi::kOpenGL:
1188 return GrBackendFormat::MakeGL(fGLInfo.fFormat, GR_GL_TEXTURE_NONE);
1189#endif
1190#ifdef SK_VULKAN
1191 case GrBackendApi::kVulkan: {
Greg Daniel6c6caf42020-05-29 12:11:05 -04001192 auto info = fVkInfo.snapImageInfo(fMutableState.get());
Robert Phillipsf209e882019-06-25 15:59:50 -04001193 if (info.fYcbcrConversionInfo.isValid()) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -07001194 SkASSERT(info.fFormat == info.fYcbcrConversionInfo.fFormat);
Robert Phillipsf209e882019-06-25 15:59:50 -04001195 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
1196 }
1197 return GrBackendFormat::MakeVk(info.fFormat);
1198 }
1199#endif
1200#ifdef SK_METAL
1201 case GrBackendApi::kMetal: {
1202 GrMtlTextureInfo mtlInfo;
1203 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
1204 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
1205 }
1206#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001207#ifdef SK_DIRECT3D
1208 case GrBackendApi::kDirect3D: {
Jim Van Verth6ec56882020-03-26 11:47:26 -04001209 auto info = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -04001210 return GrBackendFormat::MakeDxgi(info.fFormat);
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001211 }
1212#endif
Stephen Whitef03c1162020-01-28 12:39:45 -05001213#ifdef SK_DAWN
1214 case GrBackendApi::kDawn: {
1215 GrDawnRenderTargetInfo dawnInfo;
1216 SkAssertResult(this->getDawnRenderTargetInfo(&dawnInfo));
1217 return GrBackendFormat::MakeDawn(dawnInfo.fFormat);
1218 }
1219#endif
Robert Phillipsf209e882019-06-25 15:59:50 -04001220 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -04001221 return fMockInfo.getBackendFormat();
Robert Phillipsf209e882019-06-25 15:59:50 -04001222 default:
1223 return GrBackendFormat();
1224 }
1225}
1226
Greg Daniel323fbcf2018-04-10 13:46:30 -04001227bool GrBackendRenderTarget::getMockRenderTargetInfo(GrMockRenderTargetInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001228 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -04001229 *outInfo = fMockInfo;
1230 return true;
Brian Salomon0c51eea2018-03-09 17:02:09 -05001231 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001232 return false;
Brian Salomon0c51eea2018-03-09 17:02:09 -05001233}
Robert Phillips8caf85f2018-04-05 09:30:38 -04001234
Greg Daniel6c6caf42020-05-29 12:11:05 -04001235void GrBackendRenderTarget::setMutableState(const GrBackendSurfaceMutableState& state) {
1236 fMutableState->set(state);
1237}
1238
Brian Salomon4456a0d2019-07-18 15:05:11 -04001239bool GrBackendRenderTarget::isProtected() const {
1240 if (!this->isValid() || this->backend() != GrBackendApi::kVulkan) {
1241 return false;
1242 }
Greg Daniel84261652021-09-19 17:53:40 -04001243#ifdef SK_VULKAN
Brian Salomon4456a0d2019-07-18 15:05:11 -04001244 return fVkInfo.isProtected();
Greg Daniel84261652021-09-19 17:53:40 -04001245#else
1246 return false;
1247#endif
Brian Salomon4456a0d2019-07-18 15:05:11 -04001248}
1249
Robert Phillips8caf85f2018-04-05 09:30:38 -04001250#if GR_TEST_UTILS
1251bool GrBackendRenderTarget::TestingOnly_Equals(const GrBackendRenderTarget& r0,
1252 const GrBackendRenderTarget& r1) {
1253 if (!r0.isValid() || !r1.isValid()) {
1254 return false; // two invalid backend rendertargets are not considered equal
1255 }
1256
1257 if (r0.fWidth != r1.fWidth ||
1258 r0.fHeight != r1.fHeight ||
1259 r0.fSampleCnt != r1.fSampleCnt ||
1260 r0.fStencilBits != r1.fStencilBits ||
Robert Phillips8caf85f2018-04-05 09:30:38 -04001261 r0.fBackend != r1.fBackend) {
1262 return false;
1263 }
1264
1265 switch (r0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -04001266#ifdef SK_GL
1267 case GrBackendApi::kOpenGL:
1268 return r0.fGLInfo == r1.fGLInfo;
1269#endif
1270 case GrBackendApi::kMock:
1271 return r0.fMockInfo == r1.fMockInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -04001272#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -04001273 case GrBackendApi::kVulkan:
1274 return r0.fVkInfo == r1.fVkInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -04001275#endif
Timothy Liang4e85e802018-06-28 16:37:18 -04001276#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -04001277 case GrBackendApi::kMetal:
1278 return r0.fMtlInfo == r1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -04001279#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001280#ifdef SK_DIRECT3D
1281 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -04001282 return r0.fD3DInfo == r1.fD3DInfo;
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001283#endif
Stephen White985741a2019-07-18 11:43:45 -04001284#ifdef SK_DAWN
1285 case GrBackendApi::kDawn:
1286 return r0.fDawnInfo == r1.fDawnInfo;
1287#endif
Brian Salomone2826ab2019-06-04 15:58:31 -04001288 default:
1289 return false;
Robert Phillips8caf85f2018-04-05 09:30:38 -04001290 }
1291
1292 SkASSERT(0);
1293 return false;
1294}
1295#endif