blob: 4bcf35d22fa043f55e551f402f01bc5920fe016c [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
Greg Daniel6c6caf42020-05-29 12:11:05 -040010#include "src/gpu/GrBackendSurfaceMutableStateImpl.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/gl/GrGLUtil.h"
Greg Daniele7d8da42017-12-04 11:23:19 -050012
Stephen White985741a2019-07-18 11:43:45 -040013#ifdef SK_DAWN
14#include "include/gpu/dawn/GrDawnTypes.h"
15#include "src/gpu/dawn/GrDawnUtil.h"
16#endif
17
Greg Daniel94403452017-04-18 15:52:36 -040018#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/gpu/vk/GrVkTypes.h"
20#include "src/gpu/vk/GrVkImageLayout.h"
21#include "src/gpu/vk/GrVkUtil.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000022#endif
Timothy Liang4e85e802018-06-28 16:37:18 -040023#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/gpu/mtl/GrMtlTypes.h"
25#include "src/gpu/mtl/GrMtlCppUtil.h"
Timothy Liang4e85e802018-06-28 16:37:18 -040026#endif
Jim Van Verthc86c83c2020-02-27 15:48:24 -050027#ifdef SK_DIRECT3D
Jim Van Verth05d09192020-03-20 11:23:39 -040028#include "include/gpu/d3d/GrD3DTypes.h"
29#include "src/gpu/d3d/GrD3DResourceState.h"
30#include "src/gpu/d3d/GrD3DUtil.h"
Jim Van Verthc86c83c2020-02-27 15:48:24 -050031#endif
Greg Daniel7ef28f32017-04-20 16:41:55 +000032
Robert Phillipsb2adbef2019-07-02 16:33:05 -040033GrBackendFormat::GrBackendFormat(const GrBackendFormat& that)
34 : fBackend(that.fBackend)
35 , fValid(that.fValid)
36 , fTextureType(that.fTextureType) {
37 if (!fValid) {
38 return;
39 }
40
41 switch (fBackend) {
42#ifdef SK_GL
43 case GrBackendApi::kOpenGL:
44 fGLFormat = that.fGLFormat;
45 break;
46#endif
47#ifdef SK_VULKAN
48 case GrBackendApi::kVulkan:
49 fVk = that.fVk;
50 break;
51#endif
52#ifdef SK_METAL
53 case GrBackendApi::kMetal:
54 fMtlFormat = that.fMtlFormat;
55 break;
56#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -050057#ifdef SK_DIRECT3D
58 case GrBackendApi::kDirect3D:
Jim Van Verthc86c83c2020-02-27 15:48:24 -050059 fDxgiFormat = that.fDxgiFormat;
Jim Van Verthb01e12b2020-02-18 14:34:38 -050060 break;
61#endif
Stephen White985741a2019-07-18 11:43:45 -040062#ifdef SK_DAWN
63 case GrBackendApi::kDawn:
64 fDawnFormat = that.fDawnFormat;
65 break;
66#endif
Robert Phillipsb2adbef2019-07-02 16:33:05 -040067 case GrBackendApi::kMock:
Robert Phillipsa27d6252019-12-10 14:48:36 -050068 fMock = that.fMock;
Robert Phillipsb2adbef2019-07-02 16:33:05 -040069 break;
70 default:
71 SK_ABORT("Unknown GrBackend");
72 }
73}
74
Ben Wagner833313b2020-03-23 17:22:24 -040075GrBackendFormat& GrBackendFormat::operator=(const GrBackendFormat& that) {
76 if (this != &that) {
77 this->~GrBackendFormat();
78 new (this) GrBackendFormat(that);
79 }
80 return *this;
81}
82
John Rosascoa9b348f2019-11-08 13:18:15 -080083#ifdef SK_GL
Robert Phillipsfc711a22018-02-13 17:03:00 -050084GrBackendFormat::GrBackendFormat(GrGLenum format, GrGLenum target)
Greg Danielbdf12ad2018-10-12 09:31:11 -040085 : fBackend(GrBackendApi::kOpenGL)
Greg Daniel4065d452018-11-16 15:43:41 -050086 , fValid(true)
87 , fGLFormat(format) {
88 switch (target) {
Robert Phillipsf209e882019-06-25 15:59:50 -040089 case GR_GL_TEXTURE_NONE:
90 fTextureType = GrTextureType::kNone;
91 break;
Greg Daniel4065d452018-11-16 15:43:41 -050092 case GR_GL_TEXTURE_2D:
93 fTextureType = GrTextureType::k2D;
94 break;
95 case GR_GL_TEXTURE_RECTANGLE:
96 fTextureType = GrTextureType::kRectangle;
97 break;
98 case GR_GL_TEXTURE_EXTERNAL:
99 fTextureType = GrTextureType::kExternal;
100 break;
101 default:
102 SK_ABORT("Unexpected texture target");
103 }
Robert Phillipsfc711a22018-02-13 17:03:00 -0500104}
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400105#endif
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}
113
Greg Daniel7e000222018-12-03 10:08:21 -0500114GrBackendFormat GrBackendFormat::MakeVk(const GrVkYcbcrConversionInfo& ycbcrInfo) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700115 SkASSERT(ycbcrInfo.isValid());
116 return GrBackendFormat(ycbcrInfo.fFormat, ycbcrInfo);
Greg Daniel7e000222018-12-03 10:08:21 -0500117}
118
119GrBackendFormat::GrBackendFormat(VkFormat vkFormat, const GrVkYcbcrConversionInfo& ycbcrInfo)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400120 : fBackend(GrBackendApi::kVulkan)
Greg Danielb4d89562018-10-03 18:44:49 +0000121#ifdef SK_VULKAN
Robert Phillipsfc711a22018-02-13 17:03:00 -0500122 , fValid(true)
Greg Danielb4d89562018-10-03 18:44:49 +0000123#else
Greg Daniel4065d452018-11-16 15:43:41 -0500124 , fValid(false)
Greg Danielb4d89562018-10-03 18:44:49 +0000125#endif
Greg Daniel4065d452018-11-16 15:43:41 -0500126 , fTextureType(GrTextureType::k2D) {
Greg Daniel7e000222018-12-03 10:08:21 -0500127 fVk.fFormat = vkFormat;
128 fVk.fYcbcrConversionInfo = ycbcrInfo;
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700129 if (fVk.fYcbcrConversionInfo.isValid() && fVk.fYcbcrConversionInfo.fExternalFormat) {
Greg Daniel387ec9a2019-03-07 16:44:54 -0500130 fTextureType = GrTextureType::kExternal;
131 }
Robert Phillipsfc711a22018-02-13 17:03:00 -0500132}
133
Brian Salomond4764a12019-08-08 12:08:24 -0400134bool GrBackendFormat::asVkFormat(VkFormat* format) const {
135 SkASSERT(format);
Greg Danielbdf12ad2018-10-12 09:31:11 -0400136 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400137 *format = fVk.fFormat;
138 return true;
Greg Daniel7e000222018-12-03 10:08:21 -0500139 }
Brian Salomond4764a12019-08-08 12:08:24 -0400140 return false;
Greg Daniel7e000222018-12-03 10:08:21 -0500141}
142
143const GrVkYcbcrConversionInfo* GrBackendFormat::getVkYcbcrConversionInfo() const {
144 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
145 return &fVk.fYcbcrConversionInfo;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500146 }
147 return nullptr;
148}
Robert Phillipsfc711a22018-02-13 17:03:00 -0500149
Stephen White985741a2019-07-18 11:43:45 -0400150#ifdef SK_DAWN
Stephen White3cc8d4f2019-10-30 09:56:23 -0400151GrBackendFormat::GrBackendFormat(wgpu::TextureFormat format)
Stephen White985741a2019-07-18 11:43:45 -0400152 : fBackend(GrBackendApi::kDawn)
153 , fValid(true)
154 , fDawnFormat(format)
155 , fTextureType(GrTextureType::k2D) {
156}
157
Stephen White3cc8d4f2019-10-30 09:56:23 -0400158bool GrBackendFormat::asDawnFormat(wgpu::TextureFormat* format) const {
Brian Salomond4764a12019-08-08 12:08:24 -0400159 SkASSERT(format);
Stephen White985741a2019-07-18 11:43:45 -0400160 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400161 *format = fDawnFormat;
162 return true;
Stephen White985741a2019-07-18 11:43:45 -0400163 }
Brian Salomond4764a12019-08-08 12:08:24 -0400164 return false;
Stephen White985741a2019-07-18 11:43:45 -0400165}
166#endif
167
Timothy Liang4e85e802018-06-28 16:37:18 -0400168#ifdef SK_METAL
169GrBackendFormat::GrBackendFormat(GrMTLPixelFormat mtlFormat)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400170 : fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400171 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500172 , fMtlFormat(mtlFormat)
173 , fTextureType(GrTextureType::k2D) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400174}
175
Brian Salomond4764a12019-08-08 12:08:24 -0400176GrMTLPixelFormat GrBackendFormat::asMtlFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400177 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Brian Salomond4764a12019-08-08 12:08:24 -0400178 return fMtlFormat;
Timothy Liang4e85e802018-06-28 16:37:18 -0400179 }
Brian Salomond4764a12019-08-08 12:08:24 -0400180 // MTLPixelFormatInvalid == 0
181 return GrMTLPixelFormat(0);
Timothy Liang4e85e802018-06-28 16:37:18 -0400182}
183#endif
184
Jim Van Verthc86c83c2020-02-27 15:48:24 -0500185#ifdef SK_DIRECT3D
186GrBackendFormat::GrBackendFormat(DXGI_FORMAT dxgiFormat)
187 : fBackend(GrBackendApi::kDirect3D)
188 , fValid(true)
189 , fDxgiFormat(dxgiFormat)
190 , fTextureType(GrTextureType::k2D) {
191}
192
193bool GrBackendFormat::asDxgiFormat(DXGI_FORMAT* dxgiFormat) const {
194 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
195 *dxgiFormat = fDxgiFormat;
196 return true;
197 }
198 return false;
199}
200#endif
201
Greg Daniela7f69c22020-10-07 13:04:15 -0400202GrBackendFormat::GrBackendFormat(GrColorType colorType, SkImage::CompressionType compression,
203 bool isStencilFormat)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400204 : fBackend(GrBackendApi::kMock)
Robert Phillipsfc711a22018-02-13 17:03:00 -0500205 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500206 , fTextureType(GrTextureType::k2D) {
Robert Phillipsa27d6252019-12-10 14:48:36 -0500207 fMock.fColorType = colorType;
208 fMock.fCompressionType = compression;
Greg Daniela7f69c22020-10-07 13:04:15 -0400209 fMock.fIsStencilFormat = isStencilFormat;
210 SkASSERT(this->validateMock());
Robert Phillipsfc711a22018-02-13 17:03:00 -0500211}
212
Brian Salomondc8fcdb2020-03-26 16:45:05 -0400213uint32_t GrBackendFormat::channelMask() const {
214 if (!this->isValid()) {
215 return 0;
216 }
217 switch (fBackend) {
218#ifdef SK_GL
219 case GrBackendApi::kOpenGL:
220 return GrGLFormatChannels(GrGLFormatFromGLEnum(fGLFormat));
221#endif
222#ifdef SK_VULKAN
223 case GrBackendApi::kVulkan:
224 return GrVkFormatChannels(fVk.fFormat);
225#endif
226#ifdef SK_METAL
227 case GrBackendApi::kMetal:
228 return GrMtlFormatChannels(fMtlFormat);
229#endif
230#ifdef SK_DAWN
231 case GrBackendApi::kDawn:
232 return GrDawnFormatChannels(fDawnFormat);
233#endif
234#ifdef SK_DIRECT3D
235 case GrBackendApi::kDirect3D:
236 return GrDxgiFormatChannels(fDxgiFormat);
237#endif
238 case GrBackendApi::kMock:
239 return GrColorTypeChannelFlags(fMock.fColorType);
240
241 default:
242 return 0;
243 }
244}
245
Greg Daniela7f69c22020-10-07 13:04:15 -0400246#ifdef SK_DEBUG
247bool GrBackendFormat::validateMock() const {
248 int trueStates = 0;
249 if (fMock.fCompressionType != SkImage::CompressionType::kNone) {
250 trueStates++;
251 }
252 if (fMock.fColorType != GrColorType::kUnknown) {
253 trueStates++;
254 }
255 if (fMock.fIsStencilFormat) {
256 trueStates++;
257 }
258 return trueStates == 1;
259}
260#endif
261
Brian Salomond4764a12019-08-08 12:08:24 -0400262GrColorType GrBackendFormat::asMockColorType() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400263 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniela7f69c22020-10-07 13:04:15 -0400264 SkASSERT(this->validateMock());
Robert Phillipsa27d6252019-12-10 14:48:36 -0500265 return fMock.fColorType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500266 }
Robert Phillipsa27d6252019-12-10 14:48:36 -0500267
Brian Salomond4764a12019-08-08 12:08:24 -0400268 return GrColorType::kUnknown;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500269}
270
Robert Phillipsa27d6252019-12-10 14:48:36 -0500271SkImage::CompressionType GrBackendFormat::asMockCompressionType() const {
272 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniela7f69c22020-10-07 13:04:15 -0400273 SkASSERT(this->validateMock());
Robert Phillipsa27d6252019-12-10 14:48:36 -0500274 return fMock.fCompressionType;
275 }
276
277 return SkImage::CompressionType::kNone;
278}
279
Greg Daniela7f69c22020-10-07 13:04:15 -0400280bool GrBackendFormat::isMockStencilFormat() const {
281 if (this->isValid() && GrBackendApi::kMock == fBackend) {
282 SkASSERT(this->validateMock());
283 return fMock.fIsStencilFormat;
284 }
285
286 return false;
287}
Robert Phillipsa27d6252019-12-10 14:48:36 -0500288
Greg Daniel4065d452018-11-16 15:43:41 -0500289GrBackendFormat GrBackendFormat::makeTexture2D() const {
Greg Daniel4065d452018-11-16 15:43:41 -0500290 GrBackendFormat copy = *this;
Greg Daniel387ec9a2019-03-07 16:44:54 -0500291 if (const GrVkYcbcrConversionInfo* ycbcrInfo = this->getVkYcbcrConversionInfo()) {
292 if (ycbcrInfo->isValid()) {
293 // If we have a ycbcr we remove it from the backend format and set the VkFormat to
294 // R8G8B8A8_UNORM
295 SkASSERT(copy.fBackend == GrBackendApi::kVulkan);
296 copy.fVk.fYcbcrConversionInfo = GrVkYcbcrConversionInfo();
297 copy.fVk.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
298 }
299 }
Greg Daniel4065d452018-11-16 15:43:41 -0500300 copy.fTextureType = GrTextureType::k2D;
301 return copy;
302}
303
Robert Phillipsa27d6252019-12-10 14:48:36 -0500304GrBackendFormat GrBackendFormat::MakeMock(GrColorType colorType,
Greg Daniela7f69c22020-10-07 13:04:15 -0400305 SkImage::CompressionType compression,
306 bool isStencilFormat) {
307 return GrBackendFormat(colorType, compression, isStencilFormat);
Robert Phillipsa27d6252019-12-10 14:48:36 -0500308}
309
Greg Daniel45723ac2018-11-30 10:12:43 -0500310bool GrBackendFormat::operator==(const GrBackendFormat& that) const {
311 // Invalid GrBackendFormats are never equal to anything.
312 if (!fValid || !that.fValid) {
313 return false;
314 }
315
316 if (fBackend != that.fBackend) {
317 return false;
318 }
319
320 switch (fBackend) {
John Rosascoa9b348f2019-11-08 13:18:15 -0800321#ifdef SK_GL
Greg Daniel45723ac2018-11-30 10:12:43 -0500322 case GrBackendApi::kOpenGL:
323 return fGLFormat == that.fGLFormat;
John Rosascoa9b348f2019-11-08 13:18:15 -0800324 break;
325#endif
Robert Phillips0efc01d2019-11-06 17:19:30 +0000326#ifdef SK_VULKAN
John Rosascoa9b348f2019-11-08 13:18:15 -0800327 case GrBackendApi::kVulkan:
Greg Daniel45723ac2018-11-30 10:12:43 -0500328 return fVk.fFormat == that.fVk.fFormat &&
329 fVk.fYcbcrConversionInfo == that.fVk.fYcbcrConversionInfo;
Robert Phillips0efc01d2019-11-06 17:19:30 +0000330 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800331#endif
Greg Daniel45723ac2018-11-30 10:12:43 -0500332#ifdef SK_METAL
333 case GrBackendApi::kMetal:
334 return fMtlFormat == that.fMtlFormat;
Robert Phillips0efc01d2019-11-06 17:19:30 +0000335 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800336#endif
Robert Phillips0efc01d2019-11-06 17:19:30 +0000337#ifdef SK_DAWN
John Rosascoa9b348f2019-11-08 13:18:15 -0800338 case GrBackendApi::kDawn:
Stephen White985741a2019-07-18 11:43:45 -0400339 return fDawnFormat == that.fDawnFormat;
Robert Phillips0efc01d2019-11-06 17:19:30 +0000340 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800341#endif
Greg Daniel45723ac2018-11-30 10:12:43 -0500342 case GrBackendApi::kMock:
Robert Phillipsa27d6252019-12-10 14:48:36 -0500343 return fMock.fColorType == that.fMock.fColorType &&
344 fMock.fCompressionType == that.fMock.fCompressionType;
Greg Daniel85da3362020-03-09 15:18:35 -0400345#ifdef SK_DIRECT3D
346 case GrBackendApi::kDirect3D:
347 return fDxgiFormat == that.fDxgiFormat;
348#endif
Greg Daniel45723ac2018-11-30 10:12:43 -0500349 default:
350 SK_ABORT("Unknown GrBackend");
351 }
352 return false;
353}
354
Robert Phillips8e54a6e2020-08-17 14:31:02 -0400355#if defined(SK_DEBUG) || GR_TEST_UTILS
Robert Phillipsbac46722019-08-01 15:09:17 -0400356#include "include/core/SkString.h"
Robert Phillipsbac46722019-08-01 15:09:17 -0400357
358#ifdef SK_GL
359#include "src/gpu/gl/GrGLUtil.h"
360#endif
361#ifdef SK_VULKAN
362#include "src/gpu/vk/GrVkUtil.h"
363#endif
364
365SkString GrBackendFormat::toStr() const {
366 SkString str;
367
368 if (!fValid) {
369 str.append("invalid");
370 return str;
371 }
372
373 str.appendf("%s-", GrBackendApiToStr(fBackend));
374
375 switch (fBackend) {
376 case GrBackendApi::kOpenGL:
377#ifdef SK_GL
378 str.append(GrGLFormatToStr(fGLFormat));
379#endif
380 break;
381 case GrBackendApi::kVulkan:
382#ifdef SK_VULKAN
383 str.append(GrVkFormatToStr(fVk.fFormat));
384#endif
385 break;
386 case GrBackendApi::kMetal:
387#ifdef SK_METAL
388 str.append(GrMtlFormatToStr(fMtlFormat));
389#endif
390 break;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500391 case GrBackendApi::kDirect3D:
392#ifdef SK_DIRECT3D
Jim Van Verth05d09192020-03-20 11:23:39 -0400393 str.append(GrDxgiFormatToStr(fDxgiFormat));
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500394#endif
395 break;
Robert Phillipsbac46722019-08-01 15:09:17 -0400396 case GrBackendApi::kDawn:
397#ifdef SK_DAWN
Stephen Whited7325182019-08-02 17:22:59 -0400398 str.append(GrDawnFormatToStr(fDawnFormat));
Robert Phillipsbac46722019-08-01 15:09:17 -0400399#endif
400 break;
401 case GrBackendApi::kMock:
Robert Phillipsa27d6252019-12-10 14:48:36 -0500402 str.append(GrColorTypeToStr(fMock.fColorType));
403 str.appendf("-");
404 str.append(GrCompressionTypeToStr(fMock.fCompressionType));
Robert Phillipsbac46722019-08-01 15:09:17 -0400405 break;
406 }
407
408 return str;
409}
410#endif
411
412///////////////////////////////////////////////////////////////////////////////////////////////////
Greg Daniel6c6caf42020-05-29 12:11:05 -0400413GrBackendTexture::GrBackendTexture() : fIsValid(false) {}
414
Stephen White985741a2019-07-18 11:43:45 -0400415#ifdef SK_DAWN
416GrBackendTexture::GrBackendTexture(int width,
417 int height,
Stephen Whitef03c1162020-01-28 12:39:45 -0500418 const GrDawnTextureInfo& dawnInfo)
Stephen White985741a2019-07-18 11:43:45 -0400419 : fIsValid(true)
420 , fWidth(width)
421 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400422 , fMipmapped(GrMipmapped(dawnInfo.fLevelCount > 1))
Stephen White985741a2019-07-18 11:43:45 -0400423 , fBackend(GrBackendApi::kDawn)
424 , fDawnInfo(dawnInfo) {}
425#endif
426
Greg Danielb4d89562018-10-03 18:44:49 +0000427#ifdef SK_VULKAN
Greg Daniel6c6caf42020-05-29 12:11:05 -0400428GrBackendTexture::GrBackendTexture(int width, int height, const GrVkImageInfo& vkInfo)
Brian Salomon4456a0d2019-07-18 15:05:11 -0400429 : GrBackendTexture(width, height, vkInfo,
Greg Daniel6c6caf42020-05-29 12:11:05 -0400430 sk_sp<GrBackendSurfaceMutableStateImpl>(
431 new GrBackendSurfaceMutableStateImpl(
432 vkInfo.fImageLayout, vkInfo.fCurrentQueueFamily))) {}
433
Greg Daniel7b62dca2020-08-21 11:26:12 -0400434static const VkImageUsageFlags kDefaultUsageFlags =
435 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
436
437// We don't know if the backend texture is made renderable or not, so we default the usage flags
438// to include color attachment as well.
439static const VkImageUsageFlags kDefaultTexRTUsageFlags =
440 kDefaultUsageFlags | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
441
442static GrVkImageInfo apply_default_usage_flags(const GrVkImageInfo& info,
443 VkImageUsageFlags defaultFlags) {
444 if (info.fImageUsageFlags == 0) {
445 GrVkImageInfo newInfo = info;
446 newInfo.fImageUsageFlags = defaultFlags;
447 return newInfo;
448 }
449 return info;
450}
451
Greg Daniel6c6caf42020-05-29 12:11:05 -0400452GrBackendTexture::GrBackendTexture(int width,
453 int height,
454 const GrVkImageInfo& vkInfo,
455 sk_sp<GrBackendSurfaceMutableStateImpl> mutableState)
456 : fIsValid(true)
457 , fWidth(width)
458 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400459 , fMipmapped(GrMipmapped(vkInfo.fLevelCount > 1))
Greg Daniel6c6caf42020-05-29 12:11:05 -0400460 , fBackend(GrBackendApi::kVulkan)
Greg Daniel7b62dca2020-08-21 11:26:12 -0400461 , fVkInfo(apply_default_usage_flags(vkInfo, kDefaultTexRTUsageFlags))
Greg Daniel6c6caf42020-05-29 12:11:05 -0400462 , fMutableState(std::move(mutableState)) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000463#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400464
Brian Salomone2826ab2019-06-04 15:58:31 -0400465#ifdef SK_GL
466GrBackendTexture::GrBackendTexture(int width,
467 int height,
Brian Salomon40a40622020-07-21 10:32:07 -0400468 GrMipmapped mipmapped,
Brian Salomone2826ab2019-06-04 15:58:31 -0400469 const GrGLTextureInfo glInfo,
470 sk_sp<GrGLTextureParameters> params)
471 : fIsValid(true)
472 , fWidth(width)
473 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400474 , fMipmapped(mipmapped)
Brian Salomone2826ab2019-06-04 15:58:31 -0400475 , fBackend(GrBackendApi::kOpenGL)
476 , fGLInfo(glInfo, params.release()) {}
477
478sk_sp<GrGLTextureParameters> GrBackendTexture::getGLTextureParams() const {
479 if (fBackend != GrBackendApi::kOpenGL) {
480 return nullptr;
481 }
482 return fGLInfo.refParameters();
483}
484#endif
485
Timothy Liang4e85e802018-06-28 16:37:18 -0400486#ifdef SK_METAL
487GrBackendTexture::GrBackendTexture(int width,
488 int height,
Brian Salomon40a40622020-07-21 10:32:07 -0400489 GrMipmapped mipmapped,
Timothy Liang4e85e802018-06-28 16:37:18 -0400490 const GrMtlTextureInfo& mtlInfo)
491 : fIsValid(true)
492 , fWidth(width)
493 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400494 , fMipmapped(mipmapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400495 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400496 , fMtlInfo(mtlInfo) {}
497#endif
498
Jim Van Verth05d09192020-03-20 11:23:39 -0400499#ifdef SK_DIRECT3D
Jim Van Verth6ec56882020-03-26 11:47:26 -0400500GrBackendTexture::GrBackendTexture(int width, int height, const GrD3DTextureResourceInfo& d3dInfo)
Jim Van Verth05d09192020-03-20 11:23:39 -0400501 : GrBackendTexture(
502 width, height, d3dInfo,
503 sk_sp<GrD3DResourceState>(new GrD3DResourceState(
504 static_cast<D3D12_RESOURCE_STATES>(d3dInfo.fResourceState)))) {}
505
506GrBackendTexture::GrBackendTexture(int width,
507 int height,
Jim Van Verth6ec56882020-03-26 11:47:26 -0400508 const GrD3DTextureResourceInfo& d3dInfo,
Jim Van Verth05d09192020-03-20 11:23:39 -0400509 sk_sp<GrD3DResourceState> state)
510 : fIsValid(true)
511 , fWidth(width)
512 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400513 , fMipmapped(GrMipmapped(d3dInfo.fLevelCount > 1))
Jim Van Verth05d09192020-03-20 11:23:39 -0400514 , fBackend(GrBackendApi::kDirect3D)
515 , fD3DInfo(d3dInfo, state.release()) {}
516#endif
517
John Rosascoa9b348f2019-11-08 13:18:15 -0800518#ifdef SK_GL
Brian Salomon8fe24272017-07-07 12:56:11 -0400519GrBackendTexture::GrBackendTexture(int width,
520 int height,
Brian Salomon40a40622020-07-21 10:32:07 -0400521 GrMipmapped mipmapped,
Greg Daniele7d8da42017-12-04 11:23:19 -0500522 const GrGLTextureInfo& glInfo)
Brian Salomon40a40622020-07-21 10:32:07 -0400523 : GrBackendTexture(width, height, mipmapped, glInfo, sk_make_sp<GrGLTextureParameters>()) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400524 // Make no assumptions about client's texture's parameters.
525 this->glTextureParametersModified();
526}
John Rosascoa9b348f2019-11-08 13:18:15 -0800527#endif
Greg Daniele7d8da42017-12-04 11:23:19 -0500528
529GrBackendTexture::GrBackendTexture(int width,
530 int height,
Brian Salomon40a40622020-07-21 10:32:07 -0400531 GrMipmapped mipmapped,
Greg Daniel177e6952017-10-12 12:27:11 -0400532 const GrMockTextureInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400533 : fIsValid(true)
534 , fWidth(width)
Brian Salomon8fe24272017-07-07 12:56:11 -0400535 , fHeight(height)
Brian Salomon40a40622020-07-21 10:32:07 -0400536 , fMipmapped(mipmapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400537 , fBackend(GrBackendApi::kMock)
Brian Salomon8fe24272017-07-07 12:56:11 -0400538 , fMockInfo(mockInfo) {}
539
Greg Daniel52e16d92018-04-10 09:34:07 -0400540GrBackendTexture::~GrBackendTexture() {
541 this->cleanup();
542}
543
544void GrBackendTexture::cleanup() {
Brian Salomone2826ab2019-06-04 15:58:31 -0400545#ifdef SK_GL
546 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
547 fGLInfo.cleanup();
548 }
549#endif
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000550#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400551 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400552 fVkInfo.cleanup();
553 }
554#endif
Jim Van Verth05d09192020-03-20 11:23:39 -0400555#ifdef SK_DIRECT3D
556 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
557 fD3DInfo.cleanup();
558 }
559#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400560}
561
562GrBackendTexture::GrBackendTexture(const GrBackendTexture& that) : fIsValid(false) {
563 *this = that;
564}
565
566GrBackendTexture& GrBackendTexture::operator=(const GrBackendTexture& that) {
567 if (!that.isValid()) {
568 this->cleanup();
569 fIsValid = false;
570 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -0400571 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400572 this->cleanup();
573 fIsValid = false;
Greg Daniel52e16d92018-04-10 09:34:07 -0400574 }
575 fWidth = that.fWidth;
576 fHeight = that.fHeight;
Brian Salomon40a40622020-07-21 10:32:07 -0400577 fMipmapped = that.fMipmapped;
Greg Daniel52e16d92018-04-10 09:34:07 -0400578 fBackend = that.fBackend;
579
580 switch (that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400581#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400582 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400583 fGLInfo.assign(that.fGLInfo, this->isValid());
Greg Daniel52e16d92018-04-10 09:34:07 -0400584 break;
Mike Kleina55e2142018-10-03 16:34:11 +0000585#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400586#ifdef SK_VULKAN
587 case GrBackendApi::kVulkan:
588 fVkInfo.assign(that.fVkInfo, this->isValid());
Greg Danielb4d89562018-10-03 18:44:49 +0000589 break;
Brian Salomone2826ab2019-06-04 15:58:31 -0400590#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400591#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400592 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000593 fMtlInfo = that.fMtlInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400594 break;
595#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500596#ifdef SK_DIRECT3D
597 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -0400598 fD3DInfo.assign(that.fD3DInfo, this->isValid());
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500599 break;
600#endif
Stephen White985741a2019-07-18 11:43:45 -0400601#ifdef SK_DAWN
602 case GrBackendApi::kDawn:
603 fDawnInfo = that.fDawnInfo;
604 break;
605#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400606 case GrBackendApi::kMock:
Greg Daniel52e16d92018-04-10 09:34:07 -0400607 fMockInfo = that.fMockInfo;
608 break;
609 default:
610 SK_ABORT("Unknown GrBackend");
611 }
Greg Daniel6c6caf42020-05-29 12:11:05 -0400612 fMutableState = that.fMutableState;
Brian Salomone2826ab2019-06-04 15:58:31 -0400613 fIsValid = true;
Greg Daniel52e16d92018-04-10 09:34:07 -0400614 return *this;
615}
616
Greg Daniel6c6caf42020-05-29 12:11:05 -0400617sk_sp<GrBackendSurfaceMutableStateImpl> GrBackendTexture::getMutableState() const {
618 return fMutableState;
619}
620
Stephen White985741a2019-07-18 11:43:45 -0400621#ifdef SK_DAWN
Stephen Whitef03c1162020-01-28 12:39:45 -0500622bool GrBackendTexture::getDawnTextureInfo(GrDawnTextureInfo* outInfo) const {
Stephen White985741a2019-07-18 11:43:45 -0400623 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
624 *outInfo = fDawnInfo;
625 return true;
626 }
627 return false;
628}
629#endif
630
Greg Daniel6c6caf42020-05-29 12:11:05 -0400631bool GrBackendTexture::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielcc7ec242020-05-29 14:43:36 -0400632#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400633 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -0400634 *outInfo = fVkInfo.snapImageInfo(fMutableState.get());
Greg Daniel52e16d92018-04-10 09:34:07 -0400635 return true;
636 }
Greg Danielcc7ec242020-05-29 14:43:36 -0400637#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400638 return false;
639}
640
641void GrBackendTexture::setVkImageLayout(VkImageLayout layout) {
Greg Danielcc7ec242020-05-29 14:43:36 -0400642#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400643 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -0400644 fMutableState->setImageLayout(layout);
Greg Daniel52e16d92018-04-10 09:34:07 -0400645 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400646#endif
Greg Danielcc7ec242020-05-29 14:43:36 -0400647}
Greg Daniel94403452017-04-18 15:52:36 -0400648
Timothy Liang4e85e802018-06-28 16:37:18 -0400649#ifdef SK_METAL
650bool GrBackendTexture::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400651 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000652 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400653 return true;
654 }
655 return false;
656}
657#endif
658
Jim Van Verth05d09192020-03-20 11:23:39 -0400659#ifdef SK_DIRECT3D
Jim Van Verth6ec56882020-03-26 11:47:26 -0400660bool GrBackendTexture::getD3DTextureResourceInfo(GrD3DTextureResourceInfo* outInfo) const {
Jim Van Verth05d09192020-03-20 11:23:39 -0400661 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
Jim Van Verth6ec56882020-03-26 11:47:26 -0400662 *outInfo = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -0400663 return true;
664 }
665 return false;
666}
667
668void GrBackendTexture::setD3DResourceState(GrD3DResourceStateEnum state) {
669 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
670 fD3DInfo.setResourceState(state);
671 }
672}
673
674sk_sp<GrD3DResourceState> GrBackendTexture::getGrD3DResourceState() const {
675 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
676 return fD3DInfo.getGrD3DResourceState();
677 }
678 return nullptr;
679}
680#endif
681
Greg Daniel52e16d92018-04-10 09:34:07 -0400682bool GrBackendTexture::getGLTextureInfo(GrGLTextureInfo* outInfo) const {
Brian Salomone2826ab2019-06-04 15:58:31 -0400683#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400684 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400685 *outInfo = fGLInfo.info();
Greg Daniel52e16d92018-04-10 09:34:07 -0400686 return true;
Brian Osmana7ef3a02019-03-27 15:54:14 -0400687 } else if (this->isValid() && GrBackendApi::kMock == fBackend) {
688 // Hack! This allows some blink unit tests to work when using the Mock GrContext.
689 // Specifically, tests that rely on CanvasResourceProviderTextureGpuMemoryBuffer.
690 // If that code ever goes away (or ideally becomes backend-agnostic), this can go away.
691 *outInfo = GrGLTextureInfo{ GR_GL_TEXTURE_2D,
Robert Phillipsa27d6252019-12-10 14:48:36 -0500692 static_cast<GrGLuint>(fMockInfo.id()),
Brian Osmana7ef3a02019-03-27 15:54:14 -0400693 GR_GL_RGBA8 };
694 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400695 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400696#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400697 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400698}
699
Brian Salomone2826ab2019-06-04 15:58:31 -0400700void GrBackendTexture::glTextureParametersModified() {
701#ifdef SK_GL
702 if (this->isValid() && fBackend == GrBackendApi::kOpenGL) {
703 fGLInfo.parameters()->invalidate();
704 }
705#endif
706}
707
Greg Daniel52e16d92018-04-10 09:34:07 -0400708bool GrBackendTexture::getMockTextureInfo(GrMockTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400709 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400710 *outInfo = fMockInfo;
711 return true;
Brian Salomon8fe24272017-07-07 12:56:11 -0400712 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400713 return false;
Brian Salomon8fe24272017-07-07 12:56:11 -0400714}
715
Greg Daniel6c6caf42020-05-29 12:11:05 -0400716void GrBackendTexture::setMutableState(const GrBackendSurfaceMutableState& state) {
717 fMutableState->set(state);
718}
719
Brian Salomon4456a0d2019-07-18 15:05:11 -0400720bool GrBackendTexture::isProtected() const {
721 if (!this->isValid() || this->backend() != GrBackendApi::kVulkan) {
722 return false;
723 }
724 return fVkInfo.isProtected();
725}
726
Brian Salomonaad83152019-05-24 10:16:35 -0400727bool GrBackendTexture::isSameTexture(const GrBackendTexture& that) {
728 if (!this->isValid() || !that.isValid()) {
729 return false;
730 }
731 if (fBackend != that.fBackend) {
732 return false;
733 }
734 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400735#ifdef SK_GL
Brian Salomonaad83152019-05-24 10:16:35 -0400736 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400737 return fGLInfo.info().fID == that.fGLInfo.info().fID;
738#endif
Brian Salomonaad83152019-05-24 10:16:35 -0400739#ifdef SK_VULKAN
740 case GrBackendApi::kVulkan:
Greg Daniel6c6caf42020-05-29 12:11:05 -0400741 return fVkInfo.snapImageInfo(fMutableState.get()).fImage ==
742 that.fVkInfo.snapImageInfo(that.fMutableState.get()).fImage;
Brian Salomonaad83152019-05-24 10:16:35 -0400743#endif
744#ifdef SK_METAL
745 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000746 return this->fMtlInfo.fTexture == that.fMtlInfo.fTexture;
Brian Salomonaad83152019-05-24 10:16:35 -0400747#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500748#ifdef SK_DIRECT3D
749 case GrBackendApi::kDirect3D:
Jim Van Verthde175ab2020-06-10 16:12:25 -0400750 return fD3DInfo.snapTextureResourceInfo().fResource ==
751 that.fD3DInfo.snapTextureResourceInfo().fResource;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500752#endif
Stephen Whited4b8bf92020-06-08 15:38:29 -0400753#ifdef SK_DAWN
754 case GrBackendApi::kDawn: {
755 return this->fDawnInfo.fTexture.Get() == that.fDawnInfo.fTexture.Get();
756 }
757#endif
Brian Salomonaad83152019-05-24 10:16:35 -0400758 case GrBackendApi::kMock:
Robert Phillipsa27d6252019-12-10 14:48:36 -0500759 return fMockInfo.id() == that.fMockInfo.id();
Brian Salomonaad83152019-05-24 10:16:35 -0400760 default:
761 return false;
762 }
763}
764
Brian Salomonf391d0f2018-12-14 09:18:50 -0500765GrBackendFormat GrBackendTexture::getBackendFormat() const {
766 if (!this->isValid()) {
767 return GrBackendFormat();
768 }
769 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400770#ifdef SK_GL
Brian Salomonf391d0f2018-12-14 09:18:50 -0500771 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400772 return GrBackendFormat::MakeGL(fGLInfo.info().fFormat, fGLInfo.info().fTarget);
773#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500774#ifdef SK_VULKAN
775 case GrBackendApi::kVulkan: {
Greg Daniel6c6caf42020-05-29 12:11:05 -0400776 auto info = fVkInfo.snapImageInfo(fMutableState.get());
Brian Salomonf391d0f2018-12-14 09:18:50 -0500777 if (info.fYcbcrConversionInfo.isValid()) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700778 SkASSERT(info.fFormat == info.fYcbcrConversionInfo.fFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500779 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
780 }
781 return GrBackendFormat::MakeVk(info.fFormat);
782 }
783#endif
784#ifdef SK_METAL
785 case GrBackendApi::kMetal: {
786 GrMtlTextureInfo mtlInfo;
787 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
788 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
789 }
790#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500791#ifdef SK_DIRECT3D
792 case GrBackendApi::kDirect3D: {
Jim Van Verth6ec56882020-03-26 11:47:26 -0400793 auto d3dInfo = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -0400794 return GrBackendFormat::MakeDxgi(d3dInfo.fFormat);
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500795 }
796#endif
797#ifdef SK_DAWN
798 case GrBackendApi::kDawn: {
799 return GrBackendFormat::MakeDawn(fDawnInfo.fFormat);
800 }
801#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500802 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400803 return fMockInfo.getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500804 default:
805 return GrBackendFormat();
806 }
807}
808
Robert Phillipsc5509952018-04-04 15:54:55 -0400809#if GR_TEST_UTILS
810bool GrBackendTexture::TestingOnly_Equals(const GrBackendTexture& t0, const GrBackendTexture& t1) {
811 if (!t0.isValid() || !t1.isValid()) {
812 return false; // two invalid backend textures are not considered equal
813 }
814
815 if (t0.fWidth != t1.fWidth ||
816 t0.fHeight != t1.fHeight ||
Brian Salomon40a40622020-07-21 10:32:07 -0400817 t0.fMipmapped != t1.fMipmapped ||
Robert Phillipsc5509952018-04-04 15:54:55 -0400818 t0.fBackend != t1.fBackend) {
819 return false;
820 }
821
Greg Daniel7f3408b2020-06-03 13:31:00 -0400822 // For our tests when checking equality we are assuming the both backendTexture objects will
823 // be using the same mutable state object.
824 if (t0.fMutableState != t1.fMutableState) {
825 return false;
826 }
827
Robert Phillipsc5509952018-04-04 15:54:55 -0400828 switch (t0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400829#ifdef SK_GL
830 case GrBackendApi::kOpenGL:
831 return t0.fGLInfo.info() == t1.fGLInfo.info();
832#endif
833 case GrBackendApi::kMock:
834 return t0.fMockInfo == t1.fMockInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400835#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400836 case GrBackendApi::kVulkan:
837 return t0.fVkInfo == t1.fVkInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400838#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400839#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -0400840 case GrBackendApi::kMetal:
841 return t0.fMtlInfo == t1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400842#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500843#ifdef SK_DIRECT3D
844 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -0400845 return t0.fD3DInfo == t1.fD3DInfo;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500846#endif
Stephen White985741a2019-07-18 11:43:45 -0400847#ifdef SK_DAWN
848 case GrBackendApi::kDawn:
849 return t0.fDawnInfo == t1.fDawnInfo;
850#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400851 default:
852 return false;
Robert Phillipsc5509952018-04-04 15:54:55 -0400853 }
Robert Phillipsc5509952018-04-04 15:54:55 -0400854}
855#endif
856
Greg Daniel94403452017-04-18 15:52:36 -0400857////////////////////////////////////////////////////////////////////////////////////////////////////
858
Greg Daniel6c6caf42020-05-29 12:11:05 -0400859GrBackendRenderTarget::GrBackendRenderTarget() : fIsValid(false) {}
860
861
Stephen White985741a2019-07-18 11:43:45 -0400862#ifdef SK_DAWN
863GrBackendRenderTarget::GrBackendRenderTarget(int width,
864 int height,
865 int sampleCnt,
866 int stencilBits,
Stephen Whitef03c1162020-01-28 12:39:45 -0500867 const GrDawnRenderTargetInfo& dawnInfo)
Stephen White985741a2019-07-18 11:43:45 -0400868 : fIsValid(true)
Stephen White3c0a50f2020-01-16 18:19:54 -0500869 , fFramebufferOnly(true)
Stephen White985741a2019-07-18 11:43:45 -0400870 , fWidth(width)
871 , fHeight(height)
872 , fSampleCnt(sampleCnt)
873 , fStencilBits(stencilBits)
Stephen White985741a2019-07-18 11:43:45 -0400874 , fBackend(GrBackendApi::kDawn)
875 , fDawnInfo(dawnInfo) {}
876#endif
877
Greg Daniel6c6caf42020-05-29 12:11:05 -0400878#ifdef SK_VULKAN
Brian Salomon57fd9232020-09-28 17:02:49 -0400879static GrVkImageInfo resolve_vkii_sample_count(const GrVkImageInfo& vkII, int sidebandSampleCnt) {
880 auto result = vkII;
881 result.fSampleCount = std::max({vkII.fSampleCount,
882 static_cast<uint32_t>(sidebandSampleCnt),
883 1U});
884 return result;
885}
886
Greg Daniel94403452017-04-18 15:52:36 -0400887GrBackendRenderTarget::GrBackendRenderTarget(int width,
888 int height,
889 int sampleCnt,
Brian Salomonafdc6b12018-03-09 12:02:32 -0500890 const GrVkImageInfo& vkInfo)
Brian Salomon57fd9232020-09-28 17:02:49 -0400891 : GrBackendRenderTarget(width, height, resolve_vkii_sample_count(vkInfo, sampleCnt)) {}
892
893GrBackendRenderTarget::GrBackendRenderTarget(int width,
894 int height,
895 const GrVkImageInfo& vkInfo)
896 : GrBackendRenderTarget(width, height, vkInfo,
Greg Daniel6c6caf42020-05-29 12:11:05 -0400897 sk_sp<GrBackendSurfaceMutableStateImpl>(
898 new GrBackendSurfaceMutableStateImpl(
Brian Salomon57fd9232020-09-28 17:02:49 -0400899 vkInfo.fImageLayout, vkInfo.fCurrentQueueFamily))) {}
Greg Daniel323fbcf2018-04-10 13:46:30 -0400900
Greg Daniel7b62dca2020-08-21 11:26:12 -0400901static const VkImageUsageFlags kDefaultRTUsageFlags =
902 kDefaultUsageFlags | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
903
Greg Daniel323fbcf2018-04-10 13:46:30 -0400904GrBackendRenderTarget::GrBackendRenderTarget(int width,
905 int height,
Greg Daniel323fbcf2018-04-10 13:46:30 -0400906 const GrVkImageInfo& vkInfo,
Greg Daniel6c6caf42020-05-29 12:11:05 -0400907 sk_sp<GrBackendSurfaceMutableStateImpl> mutableState)
Greg Daniel9ca30652018-04-06 09:27:20 -0400908 : fIsValid(true)
909 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400910 , fHeight(height)
Brian Salomon57fd9232020-09-28 17:02:49 -0400911 , fSampleCnt(std::max(1U, vkInfo.fSampleCount))
Brian Salomonafdc6b12018-03-09 12:02:32 -0500912 , fStencilBits(0) // We always create stencil buffers internally for vulkan
Greg Danielbdf12ad2018-10-12 09:31:11 -0400913 , fBackend(GrBackendApi::kVulkan)
Greg Daniel7b62dca2020-08-21 11:26:12 -0400914 , fVkInfo(apply_default_usage_flags(vkInfo, kDefaultRTUsageFlags))
Brian Salomon57fd9232020-09-28 17:02:49 -0400915 , fMutableState(mutableState) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000916#endif
Greg Daniel94403452017-04-18 15:52:36 -0400917
Timothy Liang4e85e802018-06-28 16:37:18 -0400918#ifdef SK_METAL
Brian Salomon8e0aa442020-09-30 13:50:18 -0400919GrBackendRenderTarget::GrBackendRenderTarget(int width, int height, const GrMtlTextureInfo& mtlInfo)
Timothy Liang4e85e802018-06-28 16:37:18 -0400920 : fIsValid(true)
Brian Salomon8e0aa442020-09-30 13:50:18 -0400921 , fFramebufferOnly(false) // TODO: set this from mtlInfo.fTexture->framebufferOnly
Timothy Liang4e85e802018-06-28 16:37:18 -0400922 , fWidth(width)
923 , fHeight(height)
Brian Salomon8e0aa442020-09-30 13:50:18 -0400924 , fSampleCnt(std::max(1, GrMtlTextureInfoSampleCount(mtlInfo)))
Timothy Liang4e85e802018-06-28 16:37:18 -0400925 , fStencilBits(0)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400926 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400927 , fMtlInfo(mtlInfo) {}
Brian Salomon8e0aa442020-09-30 13:50:18 -0400928
929GrBackendRenderTarget::GrBackendRenderTarget(int width, int height,
930 int sampleCount,
931 const GrMtlTextureInfo& mtlInfo)
932 : GrBackendRenderTarget(width, height, mtlInfo) {}
Timothy Liang4e85e802018-06-28 16:37:18 -0400933#endif
934
Jim Van Verth05d09192020-03-20 11:23:39 -0400935#ifdef SK_DIRECT3D
Brian Salomon718ae762020-09-29 16:52:49 -0400936GrBackendRenderTarget::GrBackendRenderTarget(int width, int height,
Jim Van Verth6ec56882020-03-26 11:47:26 -0400937 const GrD3DTextureResourceInfo& d3dInfo)
Jim Van Verth05d09192020-03-20 11:23:39 -0400938 : GrBackendRenderTarget(
Brian Salomon718ae762020-09-29 16:52:49 -0400939 width, height, d3dInfo,
Jim Van Verth05d09192020-03-20 11:23:39 -0400940 sk_sp<GrD3DResourceState>(new GrD3DResourceState(
941 static_cast<D3D12_RESOURCE_STATES>(d3dInfo.fResourceState)))) {}
942
943GrBackendRenderTarget::GrBackendRenderTarget(int width,
944 int height,
Jim Van Verth6ec56882020-03-26 11:47:26 -0400945 const GrD3DTextureResourceInfo& d3dInfo,
Jim Van Verth05d09192020-03-20 11:23:39 -0400946 sk_sp<GrD3DResourceState> state)
947 : fIsValid(true)
948 , fWidth(width)
949 , fHeight(height)
Brian Salomon718ae762020-09-29 16:52:49 -0400950 , fSampleCnt(std::max(1U, d3dInfo.fSampleCount))
Jim Van Verth05d09192020-03-20 11:23:39 -0400951 , fStencilBits(0)
952 , fBackend(GrBackendApi::kDirect3D)
953 , fD3DInfo(d3dInfo, state.release()) {}
954#endif
John Rosascoa9b348f2019-11-08 13:18:15 -0800955#ifdef SK_GL
Greg Danielfaa095e2017-12-19 13:15:02 -0500956GrBackendRenderTarget::GrBackendRenderTarget(int width,
957 int height,
958 int sampleCnt,
959 int stencilBits,
960 const GrGLFramebufferInfo& glInfo)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500961 : fWidth(width)
Greg Danielfaa095e2017-12-19 13:15:02 -0500962 , fHeight(height)
Brian Osman788b9162020-02-07 10:36:46 -0500963 , fSampleCnt(std::max(1, sampleCnt))
Greg Danielfaa095e2017-12-19 13:15:02 -0500964 , fStencilBits(stencilBits)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400965 , fBackend(GrBackendApi::kOpenGL)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500966 , fGLInfo(glInfo) {
967 fIsValid = SkToBool(glInfo.fFormat); // the glInfo must have a valid format
968}
John Rosascoa9b348f2019-11-08 13:18:15 -0800969#endif
Greg Danielfaa095e2017-12-19 13:15:02 -0500970
Brian Salomon0c51eea2018-03-09 17:02:09 -0500971GrBackendRenderTarget::GrBackendRenderTarget(int width,
972 int height,
973 int sampleCnt,
974 int stencilBits,
975 const GrMockRenderTargetInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400976 : fIsValid(true)
977 , fWidth(width)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500978 , fHeight(height)
Brian Osman788b9162020-02-07 10:36:46 -0500979 , fSampleCnt(std::max(1, sampleCnt))
Brian Salomon0c51eea2018-03-09 17:02:09 -0500980 , fStencilBits(stencilBits)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500981 , fMockInfo(mockInfo) {}
982
Greg Daniel323fbcf2018-04-10 13:46:30 -0400983GrBackendRenderTarget::~GrBackendRenderTarget() {
984 this->cleanup();
985}
986
987void GrBackendRenderTarget::cleanup() {
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000988#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400989 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400990 fVkInfo.cleanup();
991 }
992#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400993#ifdef SK_DIRECT3D
994 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
995 fD3DInfo.cleanup();
996 }
997#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400998}
999
1000GrBackendRenderTarget::GrBackendRenderTarget(const GrBackendRenderTarget& that) : fIsValid(false) {
1001 *this = that;
1002}
1003
1004GrBackendRenderTarget& GrBackendRenderTarget::operator=(const GrBackendRenderTarget& that) {
1005 if (!that.isValid()) {
1006 this->cleanup();
1007 fIsValid = false;
1008 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -04001009 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -04001010 this->cleanup();
1011 fIsValid = false;
Greg Daniel323fbcf2018-04-10 13:46:30 -04001012 }
1013 fWidth = that.fWidth;
1014 fHeight = that.fHeight;
1015 fSampleCnt = that.fSampleCnt;
1016 fStencilBits = that.fStencilBits;
Greg Daniel323fbcf2018-04-10 13:46:30 -04001017 fBackend = that.fBackend;
1018
1019 switch (that.fBackend) {
John Rosascoa9b348f2019-11-08 13:18:15 -08001020#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -04001021 case GrBackendApi::kOpenGL:
Greg Daniel323fbcf2018-04-10 13:46:30 -04001022 fGLInfo = that.fGLInfo;
1023 break;
John Rosasco078cf3e2019-10-31 16:21:39 -07001024#endif
John Rosascoa9b348f2019-11-08 13:18:15 -08001025#ifdef SK_VULKAN
1026 case GrBackendApi::kVulkan:
1027 fVkInfo.assign(that.fVkInfo, this->isValid());
Robert Phillips0efc01d2019-11-06 17:19:30 +00001028 break;
John Rosascoa9b348f2019-11-08 13:18:15 -08001029#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -04001030#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -04001031 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +00001032 fMtlInfo = that.fMtlInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -04001033 break;
1034#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001035#ifdef SK_DIRECT3D
1036 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -04001037 fD3DInfo.assign(that.fD3DInfo, this->isValid());
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001038 break;
1039#endif
1040#ifdef SK_DAWN
1041 case GrBackendApi::kDawn:
1042 fDawnInfo = that.fDawnInfo;
1043 break;
1044#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -04001045 case GrBackendApi::kMock:
Greg Daniel323fbcf2018-04-10 13:46:30 -04001046 fMockInfo = that.fMockInfo;
1047 break;
1048 default:
1049 SK_ABORT("Unknown GrBackend");
1050 }
Greg Daniel6c6caf42020-05-29 12:11:05 -04001051 fMutableState = that.fMutableState;
Greg Daniel323fbcf2018-04-10 13:46:30 -04001052 fIsValid = that.fIsValid;
1053 return *this;
1054}
1055
Greg Daniel6c6caf42020-05-29 12:11:05 -04001056sk_sp<GrBackendSurfaceMutableStateImpl> GrBackendRenderTarget::getMutableState() const {
1057 return fMutableState;
1058}
1059
Stephen White985741a2019-07-18 11:43:45 -04001060#ifdef SK_DAWN
Stephen Whitef03c1162020-01-28 12:39:45 -05001061bool GrBackendRenderTarget::getDawnRenderTargetInfo(GrDawnRenderTargetInfo* outInfo) const {
Stephen White985741a2019-07-18 11:43:45 -04001062 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
1063 *outInfo = fDawnInfo;
1064 return true;
1065 }
1066 return false;
1067}
1068#endif
1069
Greg Daniel6c6caf42020-05-29 12:11:05 -04001070bool GrBackendRenderTarget::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielcc7ec242020-05-29 14:43:36 -04001071#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -04001072 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -04001073 *outInfo = fVkInfo.snapImageInfo(fMutableState.get());
Greg Daniel323fbcf2018-04-10 13:46:30 -04001074 return true;
1075 }
Greg Danielcc7ec242020-05-29 14:43:36 -04001076#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -04001077 return false;
1078}
1079
1080void GrBackendRenderTarget::setVkImageLayout(VkImageLayout layout) {
Greg Danielcc7ec242020-05-29 14:43:36 -04001081#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -04001082 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -04001083 fMutableState->setImageLayout(layout);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001084 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001085#endif
Greg Danielcc7ec242020-05-29 14:43:36 -04001086}
Greg Daniel94403452017-04-18 15:52:36 -04001087
Timothy Liang4e85e802018-06-28 16:37:18 -04001088#ifdef SK_METAL
1089bool GrBackendRenderTarget::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001090 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +00001091 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -04001092 return true;
1093 }
1094 return false;
1095}
1096#endif
1097
Jim Van Verth05d09192020-03-20 11:23:39 -04001098#ifdef SK_DIRECT3D
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001099bool GrBackendRenderTarget::getD3DTextureResourceInfo(GrD3DTextureResourceInfo* outInfo) const {
Jim Van Verth05d09192020-03-20 11:23:39 -04001100 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
Jim Van Verth6ec56882020-03-26 11:47:26 -04001101 *outInfo = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -04001102 return true;
1103 }
1104 return false;
1105}
1106
1107void GrBackendRenderTarget::setD3DResourceState(GrD3DResourceStateEnum state) {
1108 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
1109 fD3DInfo.setResourceState(state);
1110 }
1111}
1112
1113sk_sp<GrD3DResourceState> GrBackendRenderTarget::getGrD3DResourceState() const {
1114 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
1115 return fD3DInfo.getGrD3DResourceState();
1116 }
1117 return nullptr;
1118}
1119#endif
1120
John Rosascoa9b348f2019-11-08 13:18:15 -08001121#ifdef SK_GL
Greg Daniel323fbcf2018-04-10 13:46:30 -04001122bool GrBackendRenderTarget::getGLFramebufferInfo(GrGLFramebufferInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001123 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -04001124 *outInfo = fGLInfo;
1125 return true;
Greg Daniel94403452017-04-18 15:52:36 -04001126 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001127 return false;
Greg Daniel94403452017-04-18 15:52:36 -04001128}
John Rosascoa9b348f2019-11-08 13:18:15 -08001129#endif
Greg Daniel94403452017-04-18 15:52:36 -04001130
Robert Phillipsf209e882019-06-25 15:59:50 -04001131GrBackendFormat GrBackendRenderTarget::getBackendFormat() const {
1132 if (!this->isValid()) {
1133 return GrBackendFormat();
1134 }
1135 switch (fBackend) {
1136#ifdef SK_GL
1137 case GrBackendApi::kOpenGL:
1138 return GrBackendFormat::MakeGL(fGLInfo.fFormat, GR_GL_TEXTURE_NONE);
1139#endif
1140#ifdef SK_VULKAN
1141 case GrBackendApi::kVulkan: {
Greg Daniel6c6caf42020-05-29 12:11:05 -04001142 auto info = fVkInfo.snapImageInfo(fMutableState.get());
Robert Phillipsf209e882019-06-25 15:59:50 -04001143 if (info.fYcbcrConversionInfo.isValid()) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -07001144 SkASSERT(info.fFormat == info.fYcbcrConversionInfo.fFormat);
Robert Phillipsf209e882019-06-25 15:59:50 -04001145 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
1146 }
1147 return GrBackendFormat::MakeVk(info.fFormat);
1148 }
1149#endif
1150#ifdef SK_METAL
1151 case GrBackendApi::kMetal: {
1152 GrMtlTextureInfo mtlInfo;
1153 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
1154 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
1155 }
1156#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001157#ifdef SK_DIRECT3D
1158 case GrBackendApi::kDirect3D: {
Jim Van Verth6ec56882020-03-26 11:47:26 -04001159 auto info = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -04001160 return GrBackendFormat::MakeDxgi(info.fFormat);
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001161 }
1162#endif
Stephen Whitef03c1162020-01-28 12:39:45 -05001163#ifdef SK_DAWN
1164 case GrBackendApi::kDawn: {
1165 GrDawnRenderTargetInfo dawnInfo;
1166 SkAssertResult(this->getDawnRenderTargetInfo(&dawnInfo));
1167 return GrBackendFormat::MakeDawn(dawnInfo.fFormat);
1168 }
1169#endif
Robert Phillipsf209e882019-06-25 15:59:50 -04001170 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -04001171 return fMockInfo.getBackendFormat();
Robert Phillipsf209e882019-06-25 15:59:50 -04001172 default:
1173 return GrBackendFormat();
1174 }
1175}
1176
Greg Daniel323fbcf2018-04-10 13:46:30 -04001177bool GrBackendRenderTarget::getMockRenderTargetInfo(GrMockRenderTargetInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001178 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -04001179 *outInfo = fMockInfo;
1180 return true;
Brian Salomon0c51eea2018-03-09 17:02:09 -05001181 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001182 return false;
Brian Salomon0c51eea2018-03-09 17:02:09 -05001183}
Robert Phillips8caf85f2018-04-05 09:30:38 -04001184
Greg Daniel6c6caf42020-05-29 12:11:05 -04001185void GrBackendRenderTarget::setMutableState(const GrBackendSurfaceMutableState& state) {
1186 fMutableState->set(state);
1187}
1188
Brian Salomon4456a0d2019-07-18 15:05:11 -04001189bool GrBackendRenderTarget::isProtected() const {
1190 if (!this->isValid() || this->backend() != GrBackendApi::kVulkan) {
1191 return false;
1192 }
1193 return fVkInfo.isProtected();
1194}
1195
Robert Phillips8caf85f2018-04-05 09:30:38 -04001196#if GR_TEST_UTILS
1197bool GrBackendRenderTarget::TestingOnly_Equals(const GrBackendRenderTarget& r0,
1198 const GrBackendRenderTarget& r1) {
1199 if (!r0.isValid() || !r1.isValid()) {
1200 return false; // two invalid backend rendertargets are not considered equal
1201 }
1202
1203 if (r0.fWidth != r1.fWidth ||
1204 r0.fHeight != r1.fHeight ||
1205 r0.fSampleCnt != r1.fSampleCnt ||
1206 r0.fStencilBits != r1.fStencilBits ||
Robert Phillips8caf85f2018-04-05 09:30:38 -04001207 r0.fBackend != r1.fBackend) {
1208 return false;
1209 }
1210
1211 switch (r0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -04001212#ifdef SK_GL
1213 case GrBackendApi::kOpenGL:
1214 return r0.fGLInfo == r1.fGLInfo;
1215#endif
1216 case GrBackendApi::kMock:
1217 return r0.fMockInfo == r1.fMockInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -04001218#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -04001219 case GrBackendApi::kVulkan:
1220 return r0.fVkInfo == r1.fVkInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -04001221#endif
Timothy Liang4e85e802018-06-28 16:37:18 -04001222#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -04001223 case GrBackendApi::kMetal:
1224 return r0.fMtlInfo == r1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -04001225#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001226#ifdef SK_DIRECT3D
1227 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -04001228 return r0.fD3DInfo == r1.fD3DInfo;
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001229#endif
Stephen White985741a2019-07-18 11:43:45 -04001230#ifdef SK_DAWN
1231 case GrBackendApi::kDawn:
1232 return r0.fDawnInfo == r1.fDawnInfo;
1233#endif
Brian Salomone2826ab2019-06-04 15:58:31 -04001234 default:
1235 return false;
Robert Phillips8caf85f2018-04-05 09:30:38 -04001236 }
1237
1238 SkASSERT(0);
1239 return false;
1240}
1241#endif