blob: a1e0203ff461303470579ba62f8872bd356e2826 [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
Robert Phillipsa27d6252019-12-10 14:48:36 -0500202GrBackendFormat::GrBackendFormat(GrColorType colorType, SkImage::CompressionType compression)
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;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500208}
209
Brian Salomondc8fcdb2020-03-26 16:45:05 -0400210uint32_t GrBackendFormat::channelMask() const {
211 if (!this->isValid()) {
212 return 0;
213 }
214 switch (fBackend) {
215#ifdef SK_GL
216 case GrBackendApi::kOpenGL:
217 return GrGLFormatChannels(GrGLFormatFromGLEnum(fGLFormat));
218#endif
219#ifdef SK_VULKAN
220 case GrBackendApi::kVulkan:
221 return GrVkFormatChannels(fVk.fFormat);
222#endif
223#ifdef SK_METAL
224 case GrBackendApi::kMetal:
225 return GrMtlFormatChannels(fMtlFormat);
226#endif
227#ifdef SK_DAWN
228 case GrBackendApi::kDawn:
229 return GrDawnFormatChannels(fDawnFormat);
230#endif
231#ifdef SK_DIRECT3D
232 case GrBackendApi::kDirect3D:
233 return GrDxgiFormatChannels(fDxgiFormat);
234#endif
235 case GrBackendApi::kMock:
236 return GrColorTypeChannelFlags(fMock.fColorType);
237
238 default:
239 return 0;
240 }
241}
242
Brian Salomond4764a12019-08-08 12:08:24 -0400243GrColorType GrBackendFormat::asMockColorType() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400244 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Robert Phillipsa27d6252019-12-10 14:48:36 -0500245 SkASSERT(fMock.fCompressionType == SkImage::CompressionType::kNone ||
246 fMock.fColorType == GrColorType::kUnknown);
247
248 return fMock.fColorType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500249 }
Robert Phillipsa27d6252019-12-10 14:48:36 -0500250
Brian Salomond4764a12019-08-08 12:08:24 -0400251 return GrColorType::kUnknown;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500252}
253
Robert Phillipsa27d6252019-12-10 14:48:36 -0500254SkImage::CompressionType GrBackendFormat::asMockCompressionType() const {
255 if (this->isValid() && GrBackendApi::kMock == fBackend) {
256 SkASSERT(fMock.fCompressionType == SkImage::CompressionType::kNone ||
257 fMock.fColorType == GrColorType::kUnknown);
258
259 return fMock.fCompressionType;
260 }
261
262 return SkImage::CompressionType::kNone;
263}
264
265
Greg Daniel4065d452018-11-16 15:43:41 -0500266GrBackendFormat GrBackendFormat::makeTexture2D() const {
Greg Daniel4065d452018-11-16 15:43:41 -0500267 GrBackendFormat copy = *this;
Greg Daniel387ec9a2019-03-07 16:44:54 -0500268 if (const GrVkYcbcrConversionInfo* ycbcrInfo = this->getVkYcbcrConversionInfo()) {
269 if (ycbcrInfo->isValid()) {
270 // If we have a ycbcr we remove it from the backend format and set the VkFormat to
271 // R8G8B8A8_UNORM
272 SkASSERT(copy.fBackend == GrBackendApi::kVulkan);
273 copy.fVk.fYcbcrConversionInfo = GrVkYcbcrConversionInfo();
274 copy.fVk.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
275 }
276 }
Greg Daniel4065d452018-11-16 15:43:41 -0500277 copy.fTextureType = GrTextureType::k2D;
278 return copy;
279}
280
Robert Phillipsa27d6252019-12-10 14:48:36 -0500281GrBackendFormat GrBackendFormat::MakeMock(GrColorType colorType,
282 SkImage::CompressionType compression) {
283 return GrBackendFormat(colorType, compression);
284}
285
Greg Daniel45723ac2018-11-30 10:12:43 -0500286bool GrBackendFormat::operator==(const GrBackendFormat& that) const {
287 // Invalid GrBackendFormats are never equal to anything.
288 if (!fValid || !that.fValid) {
289 return false;
290 }
291
292 if (fBackend != that.fBackend) {
293 return false;
294 }
295
296 switch (fBackend) {
John Rosascoa9b348f2019-11-08 13:18:15 -0800297#ifdef SK_GL
Greg Daniel45723ac2018-11-30 10:12:43 -0500298 case GrBackendApi::kOpenGL:
299 return fGLFormat == that.fGLFormat;
John Rosascoa9b348f2019-11-08 13:18:15 -0800300 break;
301#endif
Robert Phillips0efc01d2019-11-06 17:19:30 +0000302#ifdef SK_VULKAN
John Rosascoa9b348f2019-11-08 13:18:15 -0800303 case GrBackendApi::kVulkan:
Greg Daniel45723ac2018-11-30 10:12:43 -0500304 return fVk.fFormat == that.fVk.fFormat &&
305 fVk.fYcbcrConversionInfo == that.fVk.fYcbcrConversionInfo;
Robert Phillips0efc01d2019-11-06 17:19:30 +0000306 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800307#endif
Greg Daniel45723ac2018-11-30 10:12:43 -0500308#ifdef SK_METAL
309 case GrBackendApi::kMetal:
310 return fMtlFormat == that.fMtlFormat;
Robert Phillips0efc01d2019-11-06 17:19:30 +0000311 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800312#endif
Robert Phillips0efc01d2019-11-06 17:19:30 +0000313#ifdef SK_DAWN
John Rosascoa9b348f2019-11-08 13:18:15 -0800314 case GrBackendApi::kDawn:
Stephen White985741a2019-07-18 11:43:45 -0400315 return fDawnFormat == that.fDawnFormat;
Robert Phillips0efc01d2019-11-06 17:19:30 +0000316 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800317#endif
Greg Daniel45723ac2018-11-30 10:12:43 -0500318 case GrBackendApi::kMock:
Robert Phillipsa27d6252019-12-10 14:48:36 -0500319 return fMock.fColorType == that.fMock.fColorType &&
320 fMock.fCompressionType == that.fMock.fCompressionType;
Greg Daniel85da3362020-03-09 15:18:35 -0400321#ifdef SK_DIRECT3D
322 case GrBackendApi::kDirect3D:
323 return fDxgiFormat == that.fDxgiFormat;
324#endif
Greg Daniel45723ac2018-11-30 10:12:43 -0500325 default:
326 SK_ABORT("Unknown GrBackend");
327 }
328 return false;
329}
330
Robert Phillipsbac46722019-08-01 15:09:17 -0400331#if GR_TEST_UTILS
332#include "include/core/SkString.h"
333#include "src/gpu/GrTestUtils.h"
334
335#ifdef SK_GL
336#include "src/gpu/gl/GrGLUtil.h"
337#endif
338#ifdef SK_VULKAN
339#include "src/gpu/vk/GrVkUtil.h"
340#endif
341
342SkString GrBackendFormat::toStr() const {
343 SkString str;
344
345 if (!fValid) {
346 str.append("invalid");
347 return str;
348 }
349
350 str.appendf("%s-", GrBackendApiToStr(fBackend));
351
352 switch (fBackend) {
353 case GrBackendApi::kOpenGL:
354#ifdef SK_GL
355 str.append(GrGLFormatToStr(fGLFormat));
356#endif
357 break;
358 case GrBackendApi::kVulkan:
359#ifdef SK_VULKAN
360 str.append(GrVkFormatToStr(fVk.fFormat));
361#endif
362 break;
363 case GrBackendApi::kMetal:
364#ifdef SK_METAL
365 str.append(GrMtlFormatToStr(fMtlFormat));
366#endif
367 break;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500368 case GrBackendApi::kDirect3D:
369#ifdef SK_DIRECT3D
Jim Van Verth05d09192020-03-20 11:23:39 -0400370 str.append(GrDxgiFormatToStr(fDxgiFormat));
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500371#endif
372 break;
Robert Phillipsbac46722019-08-01 15:09:17 -0400373 case GrBackendApi::kDawn:
374#ifdef SK_DAWN
Stephen Whited7325182019-08-02 17:22:59 -0400375 str.append(GrDawnFormatToStr(fDawnFormat));
Robert Phillipsbac46722019-08-01 15:09:17 -0400376#endif
377 break;
378 case GrBackendApi::kMock:
Robert Phillipsa27d6252019-12-10 14:48:36 -0500379 str.append(GrColorTypeToStr(fMock.fColorType));
380 str.appendf("-");
381 str.append(GrCompressionTypeToStr(fMock.fCompressionType));
Robert Phillipsbac46722019-08-01 15:09:17 -0400382 break;
383 }
384
385 return str;
386}
387#endif
388
389///////////////////////////////////////////////////////////////////////////////////////////////////
Greg Daniel6c6caf42020-05-29 12:11:05 -0400390GrBackendTexture::GrBackendTexture() : fIsValid(false) {}
391
Stephen White985741a2019-07-18 11:43:45 -0400392#ifdef SK_DAWN
393GrBackendTexture::GrBackendTexture(int width,
394 int height,
Stephen Whitef03c1162020-01-28 12:39:45 -0500395 const GrDawnTextureInfo& dawnInfo)
Stephen White985741a2019-07-18 11:43:45 -0400396 : fIsValid(true)
397 , fWidth(width)
398 , fHeight(height)
Stephen White985741a2019-07-18 11:43:45 -0400399 , fMipMapped(GrMipMapped(dawnInfo.fLevelCount > 1))
400 , fBackend(GrBackendApi::kDawn)
401 , fDawnInfo(dawnInfo) {}
402#endif
403
Greg Danielb4d89562018-10-03 18:44:49 +0000404#ifdef SK_VULKAN
Greg Daniel6c6caf42020-05-29 12:11:05 -0400405GrBackendTexture::GrBackendTexture(int width, int height, const GrVkImageInfo& vkInfo)
Brian Salomon4456a0d2019-07-18 15:05:11 -0400406 : GrBackendTexture(width, height, vkInfo,
Greg Daniel6c6caf42020-05-29 12:11:05 -0400407 sk_sp<GrBackendSurfaceMutableStateImpl>(
408 new GrBackendSurfaceMutableStateImpl(
409 vkInfo.fImageLayout, vkInfo.fCurrentQueueFamily))) {}
410
411GrBackendTexture::GrBackendTexture(int width,
412 int height,
413 const GrVkImageInfo& vkInfo,
414 sk_sp<GrBackendSurfaceMutableStateImpl> mutableState)
415 : fIsValid(true)
416 , fWidth(width)
417 , fHeight(height)
418 , fMipMapped(GrMipMapped(vkInfo.fLevelCount > 1))
419 , fBackend(GrBackendApi::kVulkan)
420 , fVkInfo(vkInfo)
421 , fMutableState(std::move(mutableState)) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000422#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400423
Brian Salomone2826ab2019-06-04 15:58:31 -0400424#ifdef SK_GL
425GrBackendTexture::GrBackendTexture(int width,
426 int height,
427 GrMipMapped mipMapped,
428 const GrGLTextureInfo glInfo,
429 sk_sp<GrGLTextureParameters> params)
430 : fIsValid(true)
431 , fWidth(width)
432 , fHeight(height)
Brian Salomone2826ab2019-06-04 15:58:31 -0400433 , fMipMapped(mipMapped)
434 , fBackend(GrBackendApi::kOpenGL)
435 , fGLInfo(glInfo, params.release()) {}
436
437sk_sp<GrGLTextureParameters> GrBackendTexture::getGLTextureParams() const {
438 if (fBackend != GrBackendApi::kOpenGL) {
439 return nullptr;
440 }
441 return fGLInfo.refParameters();
442}
443#endif
444
Timothy Liang4e85e802018-06-28 16:37:18 -0400445#ifdef SK_METAL
446GrBackendTexture::GrBackendTexture(int width,
447 int height,
448 GrMipMapped mipMapped,
449 const GrMtlTextureInfo& mtlInfo)
450 : fIsValid(true)
451 , fWidth(width)
452 , fHeight(height)
Timothy Liang4e85e802018-06-28 16:37:18 -0400453 , fMipMapped(mipMapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400454 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400455 , fMtlInfo(mtlInfo) {}
456#endif
457
Jim Van Verth05d09192020-03-20 11:23:39 -0400458#ifdef SK_DIRECT3D
Jim Van Verth6ec56882020-03-26 11:47:26 -0400459GrBackendTexture::GrBackendTexture(int width, int height, const GrD3DTextureResourceInfo& d3dInfo)
Jim Van Verth05d09192020-03-20 11:23:39 -0400460 : GrBackendTexture(
461 width, height, d3dInfo,
462 sk_sp<GrD3DResourceState>(new GrD3DResourceState(
463 static_cast<D3D12_RESOURCE_STATES>(d3dInfo.fResourceState)))) {}
464
465GrBackendTexture::GrBackendTexture(int width,
466 int height,
Jim Van Verth6ec56882020-03-26 11:47:26 -0400467 const GrD3DTextureResourceInfo& d3dInfo,
Jim Van Verth05d09192020-03-20 11:23:39 -0400468 sk_sp<GrD3DResourceState> state)
469 : fIsValid(true)
470 , fWidth(width)
471 , fHeight(height)
472 , fMipMapped(GrMipMapped(d3dInfo.fLevelCount > 1))
473 , fBackend(GrBackendApi::kDirect3D)
474 , fD3DInfo(d3dInfo, state.release()) {}
475#endif
476
John Rosascoa9b348f2019-11-08 13:18:15 -0800477#ifdef SK_GL
Brian Salomon8fe24272017-07-07 12:56:11 -0400478GrBackendTexture::GrBackendTexture(int width,
479 int height,
Greg Daniele7d8da42017-12-04 11:23:19 -0500480 GrMipMapped mipMapped,
481 const GrGLTextureInfo& glInfo)
Brian Salomone2826ab2019-06-04 15:58:31 -0400482 : GrBackendTexture(width, height, mipMapped, glInfo, sk_make_sp<GrGLTextureParameters>()) {
483 // Make no assumptions about client's texture's parameters.
484 this->glTextureParametersModified();
485}
John Rosascoa9b348f2019-11-08 13:18:15 -0800486#endif
Greg Daniele7d8da42017-12-04 11:23:19 -0500487
488GrBackendTexture::GrBackendTexture(int width,
489 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400490 GrMipMapped mipMapped,
491 const GrMockTextureInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400492 : fIsValid(true)
493 , fWidth(width)
Brian Salomon8fe24272017-07-07 12:56:11 -0400494 , fHeight(height)
Greg Daniel177e6952017-10-12 12:27:11 -0400495 , fMipMapped(mipMapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400496 , fBackend(GrBackendApi::kMock)
Brian Salomon8fe24272017-07-07 12:56:11 -0400497 , fMockInfo(mockInfo) {}
498
Greg Daniel52e16d92018-04-10 09:34:07 -0400499GrBackendTexture::~GrBackendTexture() {
500 this->cleanup();
501}
502
503void GrBackendTexture::cleanup() {
Brian Salomone2826ab2019-06-04 15:58:31 -0400504#ifdef SK_GL
505 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
506 fGLInfo.cleanup();
507 }
508#endif
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000509#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400510 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400511 fVkInfo.cleanup();
512 }
513#endif
Jim Van Verth05d09192020-03-20 11:23:39 -0400514#ifdef SK_DIRECT3D
515 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
516 fD3DInfo.cleanup();
517 }
518#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400519}
520
521GrBackendTexture::GrBackendTexture(const GrBackendTexture& that) : fIsValid(false) {
522 *this = that;
523}
524
525GrBackendTexture& GrBackendTexture::operator=(const GrBackendTexture& that) {
526 if (!that.isValid()) {
527 this->cleanup();
528 fIsValid = false;
529 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -0400530 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400531 this->cleanup();
532 fIsValid = false;
Greg Daniel52e16d92018-04-10 09:34:07 -0400533 }
534 fWidth = that.fWidth;
535 fHeight = that.fHeight;
Greg Daniel52e16d92018-04-10 09:34:07 -0400536 fMipMapped = that.fMipMapped;
537 fBackend = that.fBackend;
538
539 switch (that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400540#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400541 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400542 fGLInfo.assign(that.fGLInfo, this->isValid());
Greg Daniel52e16d92018-04-10 09:34:07 -0400543 break;
Mike Kleina55e2142018-10-03 16:34:11 +0000544#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400545#ifdef SK_VULKAN
546 case GrBackendApi::kVulkan:
547 fVkInfo.assign(that.fVkInfo, this->isValid());
Greg Danielb4d89562018-10-03 18:44:49 +0000548 break;
Brian Salomone2826ab2019-06-04 15:58:31 -0400549#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400550#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400551 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000552 fMtlInfo = that.fMtlInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400553 break;
554#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500555#ifdef SK_DIRECT3D
556 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -0400557 fD3DInfo.assign(that.fD3DInfo, this->isValid());
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500558 break;
559#endif
Stephen White985741a2019-07-18 11:43:45 -0400560#ifdef SK_DAWN
561 case GrBackendApi::kDawn:
562 fDawnInfo = that.fDawnInfo;
563 break;
564#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400565 case GrBackendApi::kMock:
Greg Daniel52e16d92018-04-10 09:34:07 -0400566 fMockInfo = that.fMockInfo;
567 break;
568 default:
569 SK_ABORT("Unknown GrBackend");
570 }
Greg Daniel6c6caf42020-05-29 12:11:05 -0400571 fMutableState = that.fMutableState;
Brian Salomone2826ab2019-06-04 15:58:31 -0400572 fIsValid = true;
Greg Daniel52e16d92018-04-10 09:34:07 -0400573 return *this;
574}
575
Greg Daniel6c6caf42020-05-29 12:11:05 -0400576sk_sp<GrBackendSurfaceMutableStateImpl> GrBackendTexture::getMutableState() const {
577 return fMutableState;
578}
579
Stephen White985741a2019-07-18 11:43:45 -0400580#ifdef SK_DAWN
Stephen Whitef03c1162020-01-28 12:39:45 -0500581bool GrBackendTexture::getDawnTextureInfo(GrDawnTextureInfo* outInfo) const {
Stephen White985741a2019-07-18 11:43:45 -0400582 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
583 *outInfo = fDawnInfo;
584 return true;
585 }
586 return false;
587}
588#endif
589
Greg Daniel6c6caf42020-05-29 12:11:05 -0400590bool GrBackendTexture::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielcc7ec242020-05-29 14:43:36 -0400591#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400592 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -0400593 *outInfo = fVkInfo.snapImageInfo(fMutableState.get());
Greg Daniel52e16d92018-04-10 09:34:07 -0400594 return true;
595 }
Greg Danielcc7ec242020-05-29 14:43:36 -0400596#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400597 return false;
598}
599
600void GrBackendTexture::setVkImageLayout(VkImageLayout layout) {
Greg Danielcc7ec242020-05-29 14:43:36 -0400601#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400602 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -0400603 fMutableState->setImageLayout(layout);
Greg Daniel52e16d92018-04-10 09:34:07 -0400604 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400605#endif
Greg Danielcc7ec242020-05-29 14:43:36 -0400606}
Greg Daniel94403452017-04-18 15:52:36 -0400607
Timothy Liang4e85e802018-06-28 16:37:18 -0400608#ifdef SK_METAL
609bool GrBackendTexture::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400610 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000611 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400612 return true;
613 }
614 return false;
615}
616#endif
617
Jim Van Verth05d09192020-03-20 11:23:39 -0400618#ifdef SK_DIRECT3D
Jim Van Verth6ec56882020-03-26 11:47:26 -0400619bool GrBackendTexture::getD3DTextureResourceInfo(GrD3DTextureResourceInfo* outInfo) const {
Jim Van Verth05d09192020-03-20 11:23:39 -0400620 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
Jim Van Verth6ec56882020-03-26 11:47:26 -0400621 *outInfo = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -0400622 return true;
623 }
624 return false;
625}
626
627void GrBackendTexture::setD3DResourceState(GrD3DResourceStateEnum state) {
628 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
629 fD3DInfo.setResourceState(state);
630 }
631}
632
633sk_sp<GrD3DResourceState> GrBackendTexture::getGrD3DResourceState() const {
634 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
635 return fD3DInfo.getGrD3DResourceState();
636 }
637 return nullptr;
638}
639#endif
640
Greg Daniel52e16d92018-04-10 09:34:07 -0400641bool GrBackendTexture::getGLTextureInfo(GrGLTextureInfo* outInfo) const {
Brian Salomone2826ab2019-06-04 15:58:31 -0400642#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400643 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400644 *outInfo = fGLInfo.info();
Greg Daniel52e16d92018-04-10 09:34:07 -0400645 return true;
Brian Osmana7ef3a02019-03-27 15:54:14 -0400646 } else if (this->isValid() && GrBackendApi::kMock == fBackend) {
647 // Hack! This allows some blink unit tests to work when using the Mock GrContext.
648 // Specifically, tests that rely on CanvasResourceProviderTextureGpuMemoryBuffer.
649 // If that code ever goes away (or ideally becomes backend-agnostic), this can go away.
650 *outInfo = GrGLTextureInfo{ GR_GL_TEXTURE_2D,
Robert Phillipsa27d6252019-12-10 14:48:36 -0500651 static_cast<GrGLuint>(fMockInfo.id()),
Brian Osmana7ef3a02019-03-27 15:54:14 -0400652 GR_GL_RGBA8 };
653 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400654 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400655#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400656 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400657}
658
Brian Salomone2826ab2019-06-04 15:58:31 -0400659void GrBackendTexture::glTextureParametersModified() {
660#ifdef SK_GL
661 if (this->isValid() && fBackend == GrBackendApi::kOpenGL) {
662 fGLInfo.parameters()->invalidate();
663 }
664#endif
665}
666
Greg Daniel52e16d92018-04-10 09:34:07 -0400667bool GrBackendTexture::getMockTextureInfo(GrMockTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400668 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400669 *outInfo = fMockInfo;
670 return true;
Brian Salomon8fe24272017-07-07 12:56:11 -0400671 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400672 return false;
Brian Salomon8fe24272017-07-07 12:56:11 -0400673}
674
Greg Daniel6c6caf42020-05-29 12:11:05 -0400675void GrBackendTexture::setMutableState(const GrBackendSurfaceMutableState& state) {
676 fMutableState->set(state);
677}
678
Brian Salomon4456a0d2019-07-18 15:05:11 -0400679bool GrBackendTexture::isProtected() const {
680 if (!this->isValid() || this->backend() != GrBackendApi::kVulkan) {
681 return false;
682 }
683 return fVkInfo.isProtected();
684}
685
Brian Salomonaad83152019-05-24 10:16:35 -0400686bool GrBackendTexture::isSameTexture(const GrBackendTexture& that) {
687 if (!this->isValid() || !that.isValid()) {
688 return false;
689 }
690 if (fBackend != that.fBackend) {
691 return false;
692 }
693 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400694#ifdef SK_GL
Brian Salomonaad83152019-05-24 10:16:35 -0400695 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400696 return fGLInfo.info().fID == that.fGLInfo.info().fID;
697#endif
Brian Salomonaad83152019-05-24 10:16:35 -0400698#ifdef SK_VULKAN
699 case GrBackendApi::kVulkan:
Greg Daniel6c6caf42020-05-29 12:11:05 -0400700 return fVkInfo.snapImageInfo(fMutableState.get()).fImage ==
701 that.fVkInfo.snapImageInfo(that.fMutableState.get()).fImage;
Brian Salomonaad83152019-05-24 10:16:35 -0400702#endif
703#ifdef SK_METAL
704 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000705 return this->fMtlInfo.fTexture == that.fMtlInfo.fTexture;
Brian Salomonaad83152019-05-24 10:16:35 -0400706#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500707#ifdef SK_DIRECT3D
708 case GrBackendApi::kDirect3D:
Jim Van Verthde175ab2020-06-10 16:12:25 -0400709 return fD3DInfo.snapTextureResourceInfo().fResource ==
710 that.fD3DInfo.snapTextureResourceInfo().fResource;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500711#endif
Stephen Whited4b8bf92020-06-08 15:38:29 -0400712#ifdef SK_DAWN
713 case GrBackendApi::kDawn: {
714 return this->fDawnInfo.fTexture.Get() == that.fDawnInfo.fTexture.Get();
715 }
716#endif
Brian Salomonaad83152019-05-24 10:16:35 -0400717 case GrBackendApi::kMock:
Robert Phillipsa27d6252019-12-10 14:48:36 -0500718 return fMockInfo.id() == that.fMockInfo.id();
Brian Salomonaad83152019-05-24 10:16:35 -0400719 default:
720 return false;
721 }
722}
723
Brian Salomonf391d0f2018-12-14 09:18:50 -0500724GrBackendFormat GrBackendTexture::getBackendFormat() const {
725 if (!this->isValid()) {
726 return GrBackendFormat();
727 }
728 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400729#ifdef SK_GL
Brian Salomonf391d0f2018-12-14 09:18:50 -0500730 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400731 return GrBackendFormat::MakeGL(fGLInfo.info().fFormat, fGLInfo.info().fTarget);
732#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500733#ifdef SK_VULKAN
734 case GrBackendApi::kVulkan: {
Greg Daniel6c6caf42020-05-29 12:11:05 -0400735 auto info = fVkInfo.snapImageInfo(fMutableState.get());
Brian Salomonf391d0f2018-12-14 09:18:50 -0500736 if (info.fYcbcrConversionInfo.isValid()) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700737 SkASSERT(info.fFormat == info.fYcbcrConversionInfo.fFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500738 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
739 }
740 return GrBackendFormat::MakeVk(info.fFormat);
741 }
742#endif
743#ifdef SK_METAL
744 case GrBackendApi::kMetal: {
745 GrMtlTextureInfo mtlInfo;
746 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
747 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
748 }
749#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500750#ifdef SK_DIRECT3D
751 case GrBackendApi::kDirect3D: {
Jim Van Verth6ec56882020-03-26 11:47:26 -0400752 auto d3dInfo = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -0400753 return GrBackendFormat::MakeDxgi(d3dInfo.fFormat);
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500754 }
755#endif
756#ifdef SK_DAWN
757 case GrBackendApi::kDawn: {
758 return GrBackendFormat::MakeDawn(fDawnInfo.fFormat);
759 }
760#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500761 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400762 return fMockInfo.getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500763 default:
764 return GrBackendFormat();
765 }
766}
767
Robert Phillipsc5509952018-04-04 15:54:55 -0400768#if GR_TEST_UTILS
769bool GrBackendTexture::TestingOnly_Equals(const GrBackendTexture& t0, const GrBackendTexture& t1) {
770 if (!t0.isValid() || !t1.isValid()) {
771 return false; // two invalid backend textures are not considered equal
772 }
773
774 if (t0.fWidth != t1.fWidth ||
775 t0.fHeight != t1.fHeight ||
Robert Phillipsc5509952018-04-04 15:54:55 -0400776 t0.fMipMapped != t1.fMipMapped ||
777 t0.fBackend != t1.fBackend) {
778 return false;
779 }
780
Greg Daniel7f3408b2020-06-03 13:31:00 -0400781 // For our tests when checking equality we are assuming the both backendTexture objects will
782 // be using the same mutable state object.
783 if (t0.fMutableState != t1.fMutableState) {
784 return false;
785 }
786
Robert Phillipsc5509952018-04-04 15:54:55 -0400787 switch (t0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400788#ifdef SK_GL
789 case GrBackendApi::kOpenGL:
790 return t0.fGLInfo.info() == t1.fGLInfo.info();
791#endif
792 case GrBackendApi::kMock:
793 return t0.fMockInfo == t1.fMockInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400794#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400795 case GrBackendApi::kVulkan:
796 return t0.fVkInfo == t1.fVkInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400797#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400798#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -0400799 case GrBackendApi::kMetal:
800 return t0.fMtlInfo == t1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400801#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500802#ifdef SK_DIRECT3D
803 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -0400804 return t0.fD3DInfo == t1.fD3DInfo;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500805#endif
Stephen White985741a2019-07-18 11:43:45 -0400806#ifdef SK_DAWN
807 case GrBackendApi::kDawn:
808 return t0.fDawnInfo == t1.fDawnInfo;
809#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400810 default:
811 return false;
Robert Phillipsc5509952018-04-04 15:54:55 -0400812 }
Robert Phillipsc5509952018-04-04 15:54:55 -0400813}
814#endif
815
Greg Daniel94403452017-04-18 15:52:36 -0400816////////////////////////////////////////////////////////////////////////////////////////////////////
817
Greg Daniel6c6caf42020-05-29 12:11:05 -0400818GrBackendRenderTarget::GrBackendRenderTarget() : fIsValid(false) {}
819
820
Stephen White985741a2019-07-18 11:43:45 -0400821#ifdef SK_DAWN
822GrBackendRenderTarget::GrBackendRenderTarget(int width,
823 int height,
824 int sampleCnt,
825 int stencilBits,
Stephen Whitef03c1162020-01-28 12:39:45 -0500826 const GrDawnRenderTargetInfo& dawnInfo)
Stephen White985741a2019-07-18 11:43:45 -0400827 : fIsValid(true)
Stephen White3c0a50f2020-01-16 18:19:54 -0500828 , fFramebufferOnly(true)
Stephen White985741a2019-07-18 11:43:45 -0400829 , fWidth(width)
830 , fHeight(height)
831 , fSampleCnt(sampleCnt)
832 , fStencilBits(stencilBits)
Stephen White985741a2019-07-18 11:43:45 -0400833 , fBackend(GrBackendApi::kDawn)
834 , fDawnInfo(dawnInfo) {}
835#endif
836
Greg Daniel6c6caf42020-05-29 12:11:05 -0400837#ifdef SK_VULKAN
Greg Daniel94403452017-04-18 15:52:36 -0400838GrBackendRenderTarget::GrBackendRenderTarget(int width,
839 int height,
840 int sampleCnt,
841 int stencilBits,
Greg Danielbcf612b2017-05-01 13:50:58 +0000842 const GrVkImageInfo& vkInfo)
Brian Salomon4456a0d2019-07-18 15:05:11 -0400843 : GrBackendRenderTarget(width, height, sampleCnt, vkInfo) {
Brian Salomonafdc6b12018-03-09 12:02:32 -0500844 // This is a deprecated constructor that takes a bogus stencil bits.
845 SkASSERT(0 == stencilBits);
846}
847
848GrBackendRenderTarget::GrBackendRenderTarget(int width,
849 int height,
850 int sampleCnt,
851 const GrVkImageInfo& vkInfo)
Brian Salomon4456a0d2019-07-18 15:05:11 -0400852 : GrBackendRenderTarget(width, height, sampleCnt, vkInfo,
Greg Daniel6c6caf42020-05-29 12:11:05 -0400853 sk_sp<GrBackendSurfaceMutableStateImpl>(
854 new GrBackendSurfaceMutableStateImpl(
855 vkInfo.fImageLayout, vkInfo.fCurrentQueueFamily))) {}
Greg Daniel323fbcf2018-04-10 13:46:30 -0400856
857GrBackendRenderTarget::GrBackendRenderTarget(int width,
858 int height,
859 int sampleCnt,
860 const GrVkImageInfo& vkInfo,
Greg Daniel6c6caf42020-05-29 12:11:05 -0400861 sk_sp<GrBackendSurfaceMutableStateImpl> mutableState)
Greg Daniel9ca30652018-04-06 09:27:20 -0400862 : fIsValid(true)
863 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400864 , fHeight(height)
Brian Osman788b9162020-02-07 10:36:46 -0500865 , fSampleCnt(std::max(1, sampleCnt))
Brian Salomonafdc6b12018-03-09 12:02:32 -0500866 , fStencilBits(0) // We always create stencil buffers internally for vulkan
Greg Danielbdf12ad2018-10-12 09:31:11 -0400867 , fBackend(GrBackendApi::kVulkan)
Greg Daniel6c6caf42020-05-29 12:11:05 -0400868 , fVkInfo(vkInfo)
869 , fMutableState(mutableState) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000870#endif
Greg Daniel94403452017-04-18 15:52:36 -0400871
Timothy Liang4e85e802018-06-28 16:37:18 -0400872#ifdef SK_METAL
873GrBackendRenderTarget::GrBackendRenderTarget(int width,
874 int height,
875 int sampleCnt,
876 const GrMtlTextureInfo& mtlInfo)
877 : fIsValid(true)
Stephen White3c0a50f2020-01-16 18:19:54 -0500878 , fFramebufferOnly(false) // TODO: set this from mtlInfo.fTexture->framebufferOnly
Timothy Liang4e85e802018-06-28 16:37:18 -0400879 , fWidth(width)
880 , fHeight(height)
Brian Osman788b9162020-02-07 10:36:46 -0500881 , fSampleCnt(std::max(1, sampleCnt))
Timothy Liang4e85e802018-06-28 16:37:18 -0400882 , fStencilBits(0)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400883 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400884 , fMtlInfo(mtlInfo) {}
885#endif
886
Jim Van Verth05d09192020-03-20 11:23:39 -0400887#ifdef SK_DIRECT3D
888GrBackendRenderTarget::GrBackendRenderTarget(int width, int height, int sampleCnt,
Jim Van Verth6ec56882020-03-26 11:47:26 -0400889 const GrD3DTextureResourceInfo& d3dInfo)
Jim Van Verth05d09192020-03-20 11:23:39 -0400890 : GrBackendRenderTarget(
891 width, height, sampleCnt, d3dInfo,
892 sk_sp<GrD3DResourceState>(new GrD3DResourceState(
893 static_cast<D3D12_RESOURCE_STATES>(d3dInfo.fResourceState)))) {}
894
895GrBackendRenderTarget::GrBackendRenderTarget(int width,
896 int height,
897 int sampleCnt,
Jim Van Verth6ec56882020-03-26 11:47:26 -0400898 const GrD3DTextureResourceInfo& d3dInfo,
Jim Van Verth05d09192020-03-20 11:23:39 -0400899 sk_sp<GrD3DResourceState> state)
900 : fIsValid(true)
901 , fWidth(width)
902 , fHeight(height)
903 , fSampleCnt(std::max(1, sampleCnt))
904 , fStencilBits(0)
905 , fBackend(GrBackendApi::kDirect3D)
906 , fD3DInfo(d3dInfo, state.release()) {}
907#endif
John Rosascoa9b348f2019-11-08 13:18:15 -0800908#ifdef SK_GL
Greg Danielfaa095e2017-12-19 13:15:02 -0500909GrBackendRenderTarget::GrBackendRenderTarget(int width,
910 int height,
911 int sampleCnt,
912 int stencilBits,
913 const GrGLFramebufferInfo& glInfo)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500914 : fWidth(width)
Greg Danielfaa095e2017-12-19 13:15:02 -0500915 , fHeight(height)
Brian Osman788b9162020-02-07 10:36:46 -0500916 , fSampleCnt(std::max(1, sampleCnt))
Greg Danielfaa095e2017-12-19 13:15:02 -0500917 , fStencilBits(stencilBits)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400918 , fBackend(GrBackendApi::kOpenGL)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500919 , fGLInfo(glInfo) {
920 fIsValid = SkToBool(glInfo.fFormat); // the glInfo must have a valid format
921}
John Rosascoa9b348f2019-11-08 13:18:15 -0800922#endif
Greg Danielfaa095e2017-12-19 13:15:02 -0500923
Brian Salomon0c51eea2018-03-09 17:02:09 -0500924GrBackendRenderTarget::GrBackendRenderTarget(int width,
925 int height,
926 int sampleCnt,
927 int stencilBits,
928 const GrMockRenderTargetInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400929 : fIsValid(true)
930 , fWidth(width)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500931 , fHeight(height)
Brian Osman788b9162020-02-07 10:36:46 -0500932 , fSampleCnt(std::max(1, sampleCnt))
Brian Salomon0c51eea2018-03-09 17:02:09 -0500933 , fStencilBits(stencilBits)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500934 , fMockInfo(mockInfo) {}
935
Greg Daniel323fbcf2018-04-10 13:46:30 -0400936GrBackendRenderTarget::~GrBackendRenderTarget() {
937 this->cleanup();
938}
939
940void GrBackendRenderTarget::cleanup() {
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000941#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400942 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400943 fVkInfo.cleanup();
944 }
945#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400946#ifdef SK_DIRECT3D
947 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
948 fD3DInfo.cleanup();
949 }
950#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400951}
952
953GrBackendRenderTarget::GrBackendRenderTarget(const GrBackendRenderTarget& that) : fIsValid(false) {
954 *this = that;
955}
956
957GrBackendRenderTarget& GrBackendRenderTarget::operator=(const GrBackendRenderTarget& that) {
958 if (!that.isValid()) {
959 this->cleanup();
960 fIsValid = false;
961 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -0400962 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400963 this->cleanup();
964 fIsValid = false;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400965 }
966 fWidth = that.fWidth;
967 fHeight = that.fHeight;
968 fSampleCnt = that.fSampleCnt;
969 fStencilBits = that.fStencilBits;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400970 fBackend = that.fBackend;
971
972 switch (that.fBackend) {
John Rosascoa9b348f2019-11-08 13:18:15 -0800973#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400974 case GrBackendApi::kOpenGL:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400975 fGLInfo = that.fGLInfo;
976 break;
John Rosasco078cf3e2019-10-31 16:21:39 -0700977#endif
John Rosascoa9b348f2019-11-08 13:18:15 -0800978#ifdef SK_VULKAN
979 case GrBackendApi::kVulkan:
980 fVkInfo.assign(that.fVkInfo, this->isValid());
Robert Phillips0efc01d2019-11-06 17:19:30 +0000981 break;
John Rosascoa9b348f2019-11-08 13:18:15 -0800982#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400983#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400984 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000985 fMtlInfo = that.fMtlInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400986 break;
987#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500988#ifdef SK_DIRECT3D
989 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -0400990 fD3DInfo.assign(that.fD3DInfo, this->isValid());
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500991 break;
992#endif
993#ifdef SK_DAWN
994 case GrBackendApi::kDawn:
995 fDawnInfo = that.fDawnInfo;
996 break;
997#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400998 case GrBackendApi::kMock:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400999 fMockInfo = that.fMockInfo;
1000 break;
1001 default:
1002 SK_ABORT("Unknown GrBackend");
1003 }
Greg Daniel6c6caf42020-05-29 12:11:05 -04001004 fMutableState = that.fMutableState;
Greg Daniel323fbcf2018-04-10 13:46:30 -04001005 fIsValid = that.fIsValid;
1006 return *this;
1007}
1008
Greg Daniel6c6caf42020-05-29 12:11:05 -04001009sk_sp<GrBackendSurfaceMutableStateImpl> GrBackendRenderTarget::getMutableState() const {
1010 return fMutableState;
1011}
1012
Stephen White985741a2019-07-18 11:43:45 -04001013#ifdef SK_DAWN
Stephen Whitef03c1162020-01-28 12:39:45 -05001014bool GrBackendRenderTarget::getDawnRenderTargetInfo(GrDawnRenderTargetInfo* outInfo) const {
Stephen White985741a2019-07-18 11:43:45 -04001015 if (this->isValid() && GrBackendApi::kDawn == fBackend) {
1016 *outInfo = fDawnInfo;
1017 return true;
1018 }
1019 return false;
1020}
1021#endif
1022
Greg Daniel6c6caf42020-05-29 12:11:05 -04001023bool GrBackendRenderTarget::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielcc7ec242020-05-29 14:43:36 -04001024#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -04001025 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -04001026 *outInfo = fVkInfo.snapImageInfo(fMutableState.get());
Greg Daniel323fbcf2018-04-10 13:46:30 -04001027 return true;
1028 }
Greg Danielcc7ec242020-05-29 14:43:36 -04001029#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -04001030 return false;
1031}
1032
1033void GrBackendRenderTarget::setVkImageLayout(VkImageLayout layout) {
Greg Danielcc7ec242020-05-29 14:43:36 -04001034#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -04001035 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel6c6caf42020-05-29 12:11:05 -04001036 fMutableState->setImageLayout(layout);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001037 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001038#endif
Greg Danielcc7ec242020-05-29 14:43:36 -04001039}
Greg Daniel94403452017-04-18 15:52:36 -04001040
Timothy Liang4e85e802018-06-28 16:37:18 -04001041#ifdef SK_METAL
1042bool GrBackendRenderTarget::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001043 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +00001044 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -04001045 return true;
1046 }
1047 return false;
1048}
1049#endif
1050
Jim Van Verth05d09192020-03-20 11:23:39 -04001051#ifdef SK_DIRECT3D
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001052bool GrBackendRenderTarget::getD3DTextureResourceInfo(GrD3DTextureResourceInfo* outInfo) const {
Jim Van Verth05d09192020-03-20 11:23:39 -04001053 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
Jim Van Verth6ec56882020-03-26 11:47:26 -04001054 *outInfo = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -04001055 return true;
1056 }
1057 return false;
1058}
1059
1060void GrBackendRenderTarget::setD3DResourceState(GrD3DResourceStateEnum state) {
1061 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
1062 fD3DInfo.setResourceState(state);
1063 }
1064}
1065
1066sk_sp<GrD3DResourceState> GrBackendRenderTarget::getGrD3DResourceState() const {
1067 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
1068 return fD3DInfo.getGrD3DResourceState();
1069 }
1070 return nullptr;
1071}
1072#endif
1073
John Rosascoa9b348f2019-11-08 13:18:15 -08001074#ifdef SK_GL
Greg Daniel323fbcf2018-04-10 13:46:30 -04001075bool GrBackendRenderTarget::getGLFramebufferInfo(GrGLFramebufferInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001076 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -04001077 *outInfo = fGLInfo;
1078 return true;
Greg Daniel94403452017-04-18 15:52:36 -04001079 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001080 return false;
Greg Daniel94403452017-04-18 15:52:36 -04001081}
John Rosascoa9b348f2019-11-08 13:18:15 -08001082#endif
Greg Daniel94403452017-04-18 15:52:36 -04001083
Robert Phillipsf209e882019-06-25 15:59:50 -04001084GrBackendFormat GrBackendRenderTarget::getBackendFormat() const {
1085 if (!this->isValid()) {
1086 return GrBackendFormat();
1087 }
1088 switch (fBackend) {
1089#ifdef SK_GL
1090 case GrBackendApi::kOpenGL:
1091 return GrBackendFormat::MakeGL(fGLInfo.fFormat, GR_GL_TEXTURE_NONE);
1092#endif
1093#ifdef SK_VULKAN
1094 case GrBackendApi::kVulkan: {
Greg Daniel6c6caf42020-05-29 12:11:05 -04001095 auto info = fVkInfo.snapImageInfo(fMutableState.get());
Robert Phillipsf209e882019-06-25 15:59:50 -04001096 if (info.fYcbcrConversionInfo.isValid()) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -07001097 SkASSERT(info.fFormat == info.fYcbcrConversionInfo.fFormat);
Robert Phillipsf209e882019-06-25 15:59:50 -04001098 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
1099 }
1100 return GrBackendFormat::MakeVk(info.fFormat);
1101 }
1102#endif
1103#ifdef SK_METAL
1104 case GrBackendApi::kMetal: {
1105 GrMtlTextureInfo mtlInfo;
1106 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
1107 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
1108 }
1109#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001110#ifdef SK_DIRECT3D
1111 case GrBackendApi::kDirect3D: {
Jim Van Verth6ec56882020-03-26 11:47:26 -04001112 auto info = fD3DInfo.snapTextureResourceInfo();
Jim Van Verth05d09192020-03-20 11:23:39 -04001113 return GrBackendFormat::MakeDxgi(info.fFormat);
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001114 }
1115#endif
Stephen Whitef03c1162020-01-28 12:39:45 -05001116#ifdef SK_DAWN
1117 case GrBackendApi::kDawn: {
1118 GrDawnRenderTargetInfo dawnInfo;
1119 SkAssertResult(this->getDawnRenderTargetInfo(&dawnInfo));
1120 return GrBackendFormat::MakeDawn(dawnInfo.fFormat);
1121 }
1122#endif
Robert Phillipsf209e882019-06-25 15:59:50 -04001123 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -04001124 return fMockInfo.getBackendFormat();
Robert Phillipsf209e882019-06-25 15:59:50 -04001125 default:
1126 return GrBackendFormat();
1127 }
1128}
1129
Greg Daniel323fbcf2018-04-10 13:46:30 -04001130bool GrBackendRenderTarget::getMockRenderTargetInfo(GrMockRenderTargetInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001131 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -04001132 *outInfo = fMockInfo;
1133 return true;
Brian Salomon0c51eea2018-03-09 17:02:09 -05001134 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001135 return false;
Brian Salomon0c51eea2018-03-09 17:02:09 -05001136}
Robert Phillips8caf85f2018-04-05 09:30:38 -04001137
Greg Daniel6c6caf42020-05-29 12:11:05 -04001138void GrBackendRenderTarget::setMutableState(const GrBackendSurfaceMutableState& state) {
1139 fMutableState->set(state);
1140}
1141
Brian Salomon4456a0d2019-07-18 15:05:11 -04001142bool GrBackendRenderTarget::isProtected() const {
1143 if (!this->isValid() || this->backend() != GrBackendApi::kVulkan) {
1144 return false;
1145 }
1146 return fVkInfo.isProtected();
1147}
1148
Robert Phillips8caf85f2018-04-05 09:30:38 -04001149#if GR_TEST_UTILS
1150bool GrBackendRenderTarget::TestingOnly_Equals(const GrBackendRenderTarget& r0,
1151 const GrBackendRenderTarget& r1) {
1152 if (!r0.isValid() || !r1.isValid()) {
1153 return false; // two invalid backend rendertargets are not considered equal
1154 }
1155
1156 if (r0.fWidth != r1.fWidth ||
1157 r0.fHeight != r1.fHeight ||
1158 r0.fSampleCnt != r1.fSampleCnt ||
1159 r0.fStencilBits != r1.fStencilBits ||
Robert Phillips8caf85f2018-04-05 09:30:38 -04001160 r0.fBackend != r1.fBackend) {
1161 return false;
1162 }
1163
1164 switch (r0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -04001165#ifdef SK_GL
1166 case GrBackendApi::kOpenGL:
1167 return r0.fGLInfo == r1.fGLInfo;
1168#endif
1169 case GrBackendApi::kMock:
1170 return r0.fMockInfo == r1.fMockInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -04001171#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -04001172 case GrBackendApi::kVulkan:
1173 return r0.fVkInfo == r1.fVkInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -04001174#endif
Timothy Liang4e85e802018-06-28 16:37:18 -04001175#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -04001176 case GrBackendApi::kMetal:
1177 return r0.fMtlInfo == r1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -04001178#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001179#ifdef SK_DIRECT3D
1180 case GrBackendApi::kDirect3D:
Jim Van Verth05d09192020-03-20 11:23:39 -04001181 return r0.fD3DInfo == r1.fD3DInfo;
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001182#endif
Stephen White985741a2019-07-18 11:43:45 -04001183#ifdef SK_DAWN
1184 case GrBackendApi::kDawn:
1185 return r0.fDawnInfo == r1.fDawnInfo;
1186#endif
Brian Salomone2826ab2019-06-04 15:58:31 -04001187 default:
1188 return false;
Robert Phillips8caf85f2018-04-05 09:30:38 -04001189 }
1190
1191 SkASSERT(0);
1192 return false;
1193}
1194#endif