blob: 8b3e2f167b359d174dd6c157f39d50313683bdfb [file] [log] [blame]
Greg Daniel94403452017-04-18 15:52:36 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Greg Daniel54bfb182018-11-20 17:12:36 -05008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/gpu/GrBackendSurface.h"
Greg Daniel94403452017-04-18 15:52:36 -040010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/gl/GrGLUtil.h"
Greg Daniele7d8da42017-12-04 11:23:19 -050012
Greg Daniel94403452017-04-18 15:52:36 -040013#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/gpu/vk/GrVkTypes.h"
15#include "src/gpu/vk/GrVkImageLayout.h"
16#include "src/gpu/vk/GrVkUtil.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000017#endif
Timothy Liang4e85e802018-06-28 16:37:18 -040018#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/gpu/mtl/GrMtlTypes.h"
20#include "src/gpu/mtl/GrMtlCppUtil.h"
Timothy Liang4e85e802018-06-28 16:37:18 -040021#endif
Greg Daniel7ef28f32017-04-20 16:41:55 +000022
Robert Phillipsb2adbef2019-07-02 16:33:05 -040023GrBackendFormat::GrBackendFormat(const GrBackendFormat& that)
24 : fBackend(that.fBackend)
25 , fValid(that.fValid)
26 , fTextureType(that.fTextureType) {
27 if (!fValid) {
28 return;
29 }
30
31 switch (fBackend) {
32#ifdef SK_GL
33 case GrBackendApi::kOpenGL:
34 fGLFormat = that.fGLFormat;
35 break;
36#endif
37#ifdef SK_VULKAN
38 case GrBackendApi::kVulkan:
39 fVk = that.fVk;
40 break;
41#endif
42#ifdef SK_METAL
43 case GrBackendApi::kMetal:
44 fMtlFormat = that.fMtlFormat;
45 break;
46#endif
47 case GrBackendApi::kMock:
Greg Daniele877dce2019-07-11 10:52:43 -040048 fMockColorType = that.fMockColorType;
Robert Phillipsb2adbef2019-07-02 16:33:05 -040049 break;
50 default:
51 SK_ABORT("Unknown GrBackend");
52 }
53}
54
Robert Phillipsfc711a22018-02-13 17:03:00 -050055GrBackendFormat::GrBackendFormat(GrGLenum format, GrGLenum target)
Greg Danielbdf12ad2018-10-12 09:31:11 -040056 : fBackend(GrBackendApi::kOpenGL)
Greg Daniel4065d452018-11-16 15:43:41 -050057 , fValid(true)
58 , fGLFormat(format) {
59 switch (target) {
Robert Phillipsf209e882019-06-25 15:59:50 -040060 case GR_GL_TEXTURE_NONE:
61 fTextureType = GrTextureType::kNone;
62 break;
Greg Daniel4065d452018-11-16 15:43:41 -050063 case GR_GL_TEXTURE_2D:
64 fTextureType = GrTextureType::k2D;
65 break;
66 case GR_GL_TEXTURE_RECTANGLE:
67 fTextureType = GrTextureType::kRectangle;
68 break;
69 case GR_GL_TEXTURE_EXTERNAL:
70 fTextureType = GrTextureType::kExternal;
71 break;
72 default:
73 SK_ABORT("Unexpected texture target");
74 }
Robert Phillipsfc711a22018-02-13 17:03:00 -050075}
76
77const GrGLenum* GrBackendFormat::getGLFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -040078 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel4065d452018-11-16 15:43:41 -050079 return &fGLFormat;
Robert Phillipsfc711a22018-02-13 17:03:00 -050080 }
81 return nullptr;
82}
83
84const GrGLenum* GrBackendFormat::getGLTarget() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -040085 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Robert Phillipsf209e882019-06-25 15:59:50 -040086 static constexpr GrGLenum kNone = GR_GL_TEXTURE_NONE;
Greg Daniel4065d452018-11-16 15:43:41 -050087 static constexpr GrGLenum k2D = GR_GL_TEXTURE_2D;
88 static constexpr GrGLenum kRect = GR_GL_TEXTURE_RECTANGLE;
89 static constexpr GrGLenum kExternal = GR_GL_TEXTURE_EXTERNAL;
90 switch (fTextureType) {
Robert Phillipsf209e882019-06-25 15:59:50 -040091 case GrTextureType::kNone:
92 return &kNone;
Greg Daniel4065d452018-11-16 15:43:41 -050093 case GrTextureType::k2D:
94 return &k2D;
95 case GrTextureType::kRectangle:
96 return &kRect;
97 case GrTextureType::kExternal:
98 return &kExternal;
99 }
Robert Phillipsfc711a22018-02-13 17:03:00 -0500100 }
101 return nullptr;
102}
103
Greg Daniel7e000222018-12-03 10:08:21 -0500104GrBackendFormat GrBackendFormat::MakeVk(const GrVkYcbcrConversionInfo& ycbcrInfo) {
105#ifdef SK_BUILD_FOR_ANDROID
106 return GrBackendFormat(VK_FORMAT_UNDEFINED, ycbcrInfo);
107#else
108 return GrBackendFormat();
109#endif
110}
111
112GrBackendFormat::GrBackendFormat(VkFormat vkFormat, const GrVkYcbcrConversionInfo& ycbcrInfo)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400113 : fBackend(GrBackendApi::kVulkan)
Greg Danielb4d89562018-10-03 18:44:49 +0000114#ifdef SK_VULKAN
Robert Phillipsfc711a22018-02-13 17:03:00 -0500115 , fValid(true)
Greg Danielb4d89562018-10-03 18:44:49 +0000116#else
Greg Daniel4065d452018-11-16 15:43:41 -0500117 , fValid(false)
Greg Danielb4d89562018-10-03 18:44:49 +0000118#endif
Greg Daniel4065d452018-11-16 15:43:41 -0500119 , fTextureType(GrTextureType::k2D) {
Greg Daniel7e000222018-12-03 10:08:21 -0500120 fVk.fFormat = vkFormat;
121 fVk.fYcbcrConversionInfo = ycbcrInfo;
Greg Daniel387ec9a2019-03-07 16:44:54 -0500122 if (fVk.fYcbcrConversionInfo.isValid()) {
123 fTextureType = GrTextureType::kExternal;
124 }
Robert Phillipsfc711a22018-02-13 17:03:00 -0500125}
126
127const VkFormat* GrBackendFormat::getVkFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400128 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel7e000222018-12-03 10:08:21 -0500129 return &fVk.fFormat;
130 }
131 return nullptr;
132}
133
134const GrVkYcbcrConversionInfo* GrBackendFormat::getVkYcbcrConversionInfo() const {
135 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
136 return &fVk.fYcbcrConversionInfo;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500137 }
138 return nullptr;
139}
Robert Phillipsfc711a22018-02-13 17:03:00 -0500140
Timothy Liang4e85e802018-06-28 16:37:18 -0400141#ifdef SK_METAL
142GrBackendFormat::GrBackendFormat(GrMTLPixelFormat mtlFormat)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400143 : fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400144 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500145 , fMtlFormat(mtlFormat)
146 , fTextureType(GrTextureType::k2D) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400147}
148
149const GrMTLPixelFormat* GrBackendFormat::getMtlFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400150 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400151 return &fMtlFormat;
152 }
153 return nullptr;
154}
155#endif
156
Greg Daniele877dce2019-07-11 10:52:43 -0400157GrBackendFormat::GrBackendFormat(GrColorType colorType)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400158 : fBackend(GrBackendApi::kMock)
Robert Phillipsfc711a22018-02-13 17:03:00 -0500159 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500160 , fTextureType(GrTextureType::k2D) {
Greg Daniele877dce2019-07-11 10:52:43 -0400161 fMockColorType = colorType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500162}
163
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400164const GrColorType* GrBackendFormat::getMockColorType() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400165 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniele877dce2019-07-11 10:52:43 -0400166 return &fMockColorType;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500167 }
168 return nullptr;
169}
170
Greg Daniel4065d452018-11-16 15:43:41 -0500171GrBackendFormat GrBackendFormat::makeTexture2D() const {
Greg Daniel4065d452018-11-16 15:43:41 -0500172 GrBackendFormat copy = *this;
Greg Daniel387ec9a2019-03-07 16:44:54 -0500173 if (const GrVkYcbcrConversionInfo* ycbcrInfo = this->getVkYcbcrConversionInfo()) {
174 if (ycbcrInfo->isValid()) {
175 // If we have a ycbcr we remove it from the backend format and set the VkFormat to
176 // R8G8B8A8_UNORM
177 SkASSERT(copy.fBackend == GrBackendApi::kVulkan);
178 copy.fVk.fYcbcrConversionInfo = GrVkYcbcrConversionInfo();
179 copy.fVk.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
180 }
181 }
Greg Daniel4065d452018-11-16 15:43:41 -0500182 copy.fTextureType = GrTextureType::k2D;
183 return copy;
184}
185
Greg Daniel45723ac2018-11-30 10:12:43 -0500186bool GrBackendFormat::operator==(const GrBackendFormat& that) const {
187 // Invalid GrBackendFormats are never equal to anything.
188 if (!fValid || !that.fValid) {
189 return false;
190 }
191
192 if (fBackend != that.fBackend) {
193 return false;
194 }
195
196 switch (fBackend) {
197 case GrBackendApi::kOpenGL:
198 return fGLFormat == that.fGLFormat;
199 case GrBackendApi::kVulkan:
200#ifdef SK_VULKAN
201 return fVk.fFormat == that.fVk.fFormat &&
202 fVk.fYcbcrConversionInfo == that.fVk.fYcbcrConversionInfo;
203#endif
204 break;
205#ifdef SK_METAL
206 case GrBackendApi::kMetal:
207 return fMtlFormat == that.fMtlFormat;
208#endif
209 break;
210 case GrBackendApi::kMock:
Greg Daniele877dce2019-07-11 10:52:43 -0400211 return fMockColorType == that.fMockColorType;
Greg Daniel45723ac2018-11-30 10:12:43 -0500212 default:
213 SK_ABORT("Unknown GrBackend");
214 }
215 return false;
216}
217
Greg Daniel94403452017-04-18 15:52:36 -0400218GrBackendTexture::GrBackendTexture(int width,
219 int height,
Greg Daniel207282e2017-04-26 13:29:21 -0400220 const GrVkImageInfo& vkInfo)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400221 : GrBackendTexture(width, height, GrProtected::kNo, vkInfo) {}
222
223GrBackendTexture::GrBackendTexture(int width,
224 int height,
225 GrProtected isProtected,
226 const GrVkImageInfo& vkInfo)
Greg Danielb4d89562018-10-03 18:44:49 +0000227#ifdef SK_VULKAN
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400228 : GrBackendTexture(width, height, isProtected, vkInfo,
Greg Daniel52e16d92018-04-10 09:34:07 -0400229 sk_sp<GrVkImageLayout>(new GrVkImageLayout(vkInfo.fImageLayout))) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000230#else
231 : fIsValid(false) {}
232#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400233
Brian Salomone2826ab2019-06-04 15:58:31 -0400234#ifdef SK_GL
235GrBackendTexture::GrBackendTexture(int width,
236 int height,
237 GrMipMapped mipMapped,
238 const GrGLTextureInfo glInfo,
239 sk_sp<GrGLTextureParameters> params)
240 : fIsValid(true)
241 , fWidth(width)
242 , fHeight(height)
243 , fConfig(kUnknown_GrPixelConfig)
244 , fMipMapped(mipMapped)
245 , fBackend(GrBackendApi::kOpenGL)
246 , fGLInfo(glInfo, params.release()) {}
247
248sk_sp<GrGLTextureParameters> GrBackendTexture::getGLTextureParams() const {
249 if (fBackend != GrBackendApi::kOpenGL) {
250 return nullptr;
251 }
252 return fGLInfo.refParameters();
253}
254#endif
255
Greg Danielb4d89562018-10-03 18:44:49 +0000256#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400257GrBackendTexture::GrBackendTexture(int width,
258 int height,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400259 GrProtected isProtected,
Greg Daniel52e16d92018-04-10 09:34:07 -0400260 const GrVkImageInfo& vkInfo,
261 sk_sp<GrVkImageLayout> layout)
Greg Daniel9ca30652018-04-06 09:27:20 -0400262 : fIsValid(true)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400263 , fIsProtected(isProtected)
Greg Daniel9ca30652018-04-06 09:27:20 -0400264 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400265 , fHeight(height)
Greg Daniel108bb232018-07-03 16:18:29 -0400266 , fConfig(kUnknown_GrPixelConfig)
Chris Dalton3b51df12017-11-27 14:33:06 -0700267 , fMipMapped(GrMipMapped(vkInfo.fLevelCount > 1))
Greg Danielbdf12ad2018-10-12 09:31:11 -0400268 , fBackend(GrBackendApi::kVulkan)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400269 , fVkInfo(vkInfo, layout.release()) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000270#endif
Greg Daniel94403452017-04-18 15:52:36 -0400271
Timothy Liang4e85e802018-06-28 16:37:18 -0400272#ifdef SK_METAL
273GrBackendTexture::GrBackendTexture(int width,
274 int height,
275 GrMipMapped mipMapped,
276 const GrMtlTextureInfo& mtlInfo)
277 : fIsValid(true)
278 , fWidth(width)
279 , fHeight(height)
280 , fConfig(GrPixelConfig::kUnknown_GrPixelConfig)
281 , fMipMapped(mipMapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400282 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400283 , fMtlInfo(mtlInfo) {}
284#endif
285
Brian Salomon8fe24272017-07-07 12:56:11 -0400286GrBackendTexture::GrBackendTexture(int width,
287 int height,
Greg Daniele7d8da42017-12-04 11:23:19 -0500288 GrMipMapped mipMapped,
289 const GrGLTextureInfo& glInfo)
Brian Salomone2826ab2019-06-04 15:58:31 -0400290 : GrBackendTexture(width, height, mipMapped, glInfo, sk_make_sp<GrGLTextureParameters>()) {
291 // Make no assumptions about client's texture's parameters.
292 this->glTextureParametersModified();
293}
Greg Daniele7d8da42017-12-04 11:23:19 -0500294
295GrBackendTexture::GrBackendTexture(int width,
296 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400297 GrMipMapped mipMapped,
298 const GrMockTextureInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400299 : fIsValid(true)
300 , fWidth(width)
Brian Salomon8fe24272017-07-07 12:56:11 -0400301 , fHeight(height)
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400302 , fConfig(mockInfo.pixelConfig())
Greg Daniel177e6952017-10-12 12:27:11 -0400303 , fMipMapped(mipMapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400304 , fBackend(GrBackendApi::kMock)
Brian Salomon8fe24272017-07-07 12:56:11 -0400305 , fMockInfo(mockInfo) {}
306
Greg Daniel52e16d92018-04-10 09:34:07 -0400307GrBackendTexture::~GrBackendTexture() {
308 this->cleanup();
309}
310
311void GrBackendTexture::cleanup() {
Brian Salomone2826ab2019-06-04 15:58:31 -0400312#ifdef SK_GL
313 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
314 fGLInfo.cleanup();
315 }
316#endif
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000317#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400318 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400319 fVkInfo.cleanup();
320 }
321#endif
322}
323
324GrBackendTexture::GrBackendTexture(const GrBackendTexture& that) : fIsValid(false) {
325 *this = that;
326}
327
328GrBackendTexture& GrBackendTexture::operator=(const GrBackendTexture& that) {
329 if (!that.isValid()) {
330 this->cleanup();
331 fIsValid = false;
332 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -0400333 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400334 this->cleanup();
335 fIsValid = false;
Greg Daniel52e16d92018-04-10 09:34:07 -0400336 }
337 fWidth = that.fWidth;
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400338 fIsProtected = that.fIsProtected;
Greg Daniel52e16d92018-04-10 09:34:07 -0400339 fHeight = that.fHeight;
340 fConfig = that.fConfig;
341 fMipMapped = that.fMipMapped;
342 fBackend = that.fBackend;
343
344 switch (that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400345#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400346 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400347 fGLInfo.assign(that.fGLInfo, this->isValid());
Greg Daniel52e16d92018-04-10 09:34:07 -0400348 break;
Mike Kleina55e2142018-10-03 16:34:11 +0000349#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400350#ifdef SK_VULKAN
351 case GrBackendApi::kVulkan:
352 fVkInfo.assign(that.fVkInfo, this->isValid());
Greg Danielb4d89562018-10-03 18:44:49 +0000353 break;
Brian Salomone2826ab2019-06-04 15:58:31 -0400354#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400355#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400356 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000357 fMtlInfo = that.fMtlInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400358 break;
359#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400360 case GrBackendApi::kMock:
Greg Daniel52e16d92018-04-10 09:34:07 -0400361 fMockInfo = that.fMockInfo;
362 break;
363 default:
364 SK_ABORT("Unknown GrBackend");
365 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400366 fIsValid = true;
Greg Daniel52e16d92018-04-10 09:34:07 -0400367 return *this;
368}
369
Mike Kleina55e2142018-10-03 16:34:11 +0000370bool GrBackendTexture::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielb4d89562018-10-03 18:44:49 +0000371#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400372 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400373 *outInfo = fVkInfo.snapImageInfo();
374 return true;
375 }
Greg Danielb4d89562018-10-03 18:44:49 +0000376#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400377 return false;
378}
379
380void GrBackendTexture::setVkImageLayout(VkImageLayout layout) {
Greg Danielb4d89562018-10-03 18:44:49 +0000381#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400382 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400383 fVkInfo.setImageLayout(layout);
384 }
Greg Danielb4d89562018-10-03 18:44:49 +0000385#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400386}
387
Greg Danielb4d89562018-10-03 18:44:49 +0000388#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400389sk_sp<GrVkImageLayout> GrBackendTexture::getGrVkImageLayout() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400390 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400391 return fVkInfo.getGrVkImageLayout();
Greg Daniel94403452017-04-18 15:52:36 -0400392 }
393 return nullptr;
394}
Brian Salomone2826ab2019-06-04 15:58:31 -0400395#endif
Greg Daniel94403452017-04-18 15:52:36 -0400396
Timothy Liang4e85e802018-06-28 16:37:18 -0400397#ifdef SK_METAL
398bool GrBackendTexture::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400399 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000400 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400401 return true;
402 }
403 return false;
404}
405#endif
406
Greg Daniel52e16d92018-04-10 09:34:07 -0400407bool GrBackendTexture::getGLTextureInfo(GrGLTextureInfo* outInfo) const {
Brian Salomone2826ab2019-06-04 15:58:31 -0400408#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400409 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400410 *outInfo = fGLInfo.info();
Greg Daniel52e16d92018-04-10 09:34:07 -0400411 return true;
Brian Osmana7ef3a02019-03-27 15:54:14 -0400412 } else if (this->isValid() && GrBackendApi::kMock == fBackend) {
413 // Hack! This allows some blink unit tests to work when using the Mock GrContext.
414 // Specifically, tests that rely on CanvasResourceProviderTextureGpuMemoryBuffer.
415 // If that code ever goes away (or ideally becomes backend-agnostic), this can go away.
416 *outInfo = GrGLTextureInfo{ GR_GL_TEXTURE_2D,
417 static_cast<GrGLuint>(fMockInfo.fID),
418 GR_GL_RGBA8 };
419 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400420 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400421#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400422 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400423}
424
Brian Salomone2826ab2019-06-04 15:58:31 -0400425void GrBackendTexture::glTextureParametersModified() {
426#ifdef SK_GL
427 if (this->isValid() && fBackend == GrBackendApi::kOpenGL) {
428 fGLInfo.parameters()->invalidate();
429 }
430#endif
431}
432
Greg Daniel52e16d92018-04-10 09:34:07 -0400433bool GrBackendTexture::getMockTextureInfo(GrMockTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400434 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400435 *outInfo = fMockInfo;
436 return true;
Brian Salomon8fe24272017-07-07 12:56:11 -0400437 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400438 return false;
Brian Salomon8fe24272017-07-07 12:56:11 -0400439}
440
Brian Salomonaad83152019-05-24 10:16:35 -0400441bool GrBackendTexture::isSameTexture(const GrBackendTexture& that) {
442 if (!this->isValid() || !that.isValid()) {
443 return false;
444 }
445 if (fBackend != that.fBackend) {
446 return false;
447 }
448 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400449#ifdef SK_GL
Brian Salomonaad83152019-05-24 10:16:35 -0400450 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400451 return fGLInfo.info().fID == that.fGLInfo.info().fID;
452#endif
Brian Salomonaad83152019-05-24 10:16:35 -0400453#ifdef SK_VULKAN
454 case GrBackendApi::kVulkan:
455 return fVkInfo.snapImageInfo().fImage == that.fVkInfo.snapImageInfo().fImage;
456#endif
457#ifdef SK_METAL
458 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000459 return this->fMtlInfo.fTexture == that.fMtlInfo.fTexture;
Brian Salomonaad83152019-05-24 10:16:35 -0400460#endif
461 case GrBackendApi::kMock:
462 return fMockInfo.fID == that.fMockInfo.fID;
463 default:
464 return false;
465 }
466}
467
Brian Salomonf391d0f2018-12-14 09:18:50 -0500468GrBackendFormat GrBackendTexture::getBackendFormat() const {
469 if (!this->isValid()) {
470 return GrBackendFormat();
471 }
472 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400473#ifdef SK_GL
Brian Salomonf391d0f2018-12-14 09:18:50 -0500474 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400475 return GrBackendFormat::MakeGL(fGLInfo.info().fFormat, fGLInfo.info().fTarget);
476#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500477#ifdef SK_VULKAN
478 case GrBackendApi::kVulkan: {
479 auto info = fVkInfo.snapImageInfo();
480 if (info.fYcbcrConversionInfo.isValid()) {
481 SkASSERT(info.fFormat == VK_FORMAT_UNDEFINED);
482 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
483 }
484 return GrBackendFormat::MakeVk(info.fFormat);
485 }
486#endif
487#ifdef SK_METAL
488 case GrBackendApi::kMetal: {
489 GrMtlTextureInfo mtlInfo;
490 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
491 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
492 }
493#endif
494 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400495 return fMockInfo.getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500496 default:
497 return GrBackendFormat();
498 }
499}
500
Robert Phillipsc5509952018-04-04 15:54:55 -0400501#if GR_TEST_UTILS
502bool GrBackendTexture::TestingOnly_Equals(const GrBackendTexture& t0, const GrBackendTexture& t1) {
503 if (!t0.isValid() || !t1.isValid()) {
504 return false; // two invalid backend textures are not considered equal
505 }
506
507 if (t0.fWidth != t1.fWidth ||
508 t0.fHeight != t1.fHeight ||
509 t0.fConfig != t1.fConfig ||
510 t0.fMipMapped != t1.fMipMapped ||
511 t0.fBackend != t1.fBackend) {
512 return false;
513 }
514
515 switch (t0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400516#ifdef SK_GL
517 case GrBackendApi::kOpenGL:
518 return t0.fGLInfo.info() == t1.fGLInfo.info();
519#endif
520 case GrBackendApi::kMock:
521 return t0.fMockInfo == t1.fMockInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400522#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400523 case GrBackendApi::kVulkan:
524 return t0.fVkInfo == t1.fVkInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400525#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400526#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -0400527 case GrBackendApi::kMetal:
528 return t0.fMtlInfo == t1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400529#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400530 default:
531 return false;
Robert Phillipsc5509952018-04-04 15:54:55 -0400532 }
Robert Phillipsc5509952018-04-04 15:54:55 -0400533}
534#endif
535
Greg Daniel94403452017-04-18 15:52:36 -0400536////////////////////////////////////////////////////////////////////////////////////////////////////
537
Greg Daniel94403452017-04-18 15:52:36 -0400538GrBackendRenderTarget::GrBackendRenderTarget(int width,
539 int height,
540 int sampleCnt,
541 int stencilBits,
Greg Danielbcf612b2017-05-01 13:50:58 +0000542 const GrVkImageInfo& vkInfo)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400543 : GrBackendRenderTarget(width, height, sampleCnt, GrProtected::kNo, vkInfo) {
Brian Salomonafdc6b12018-03-09 12:02:32 -0500544 // This is a deprecated constructor that takes a bogus stencil bits.
545 SkASSERT(0 == stencilBits);
546}
547
548GrBackendRenderTarget::GrBackendRenderTarget(int width,
549 int height,
550 int sampleCnt,
551 const GrVkImageInfo& vkInfo)
Greg Danielb4d89562018-10-03 18:44:49 +0000552#ifdef SK_VULKAN
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400553 : GrBackendRenderTarget(width, height, sampleCnt, false, vkInfo) {}
554#else
555 : fIsValid(false) {}
556#endif
557
558GrBackendRenderTarget::GrBackendRenderTarget(int width,
559 int height,
560 int sampleCnt,
561 GrProtected isProtected,
562 const GrVkImageInfo& vkInfo)
563#ifdef SK_VULKAN
564 : GrBackendRenderTarget(width, height, sampleCnt, isProtected, vkInfo,
Greg Daniel323fbcf2018-04-10 13:46:30 -0400565 sk_sp<GrVkImageLayout>(new GrVkImageLayout(vkInfo.fImageLayout))) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000566#else
567 : fIsValid(false) {}
568#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400569
Greg Danielb4d89562018-10-03 18:44:49 +0000570#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400571GrBackendRenderTarget::GrBackendRenderTarget(int width,
572 int height,
573 int sampleCnt,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400574 GrProtected isProtected,
Greg Daniel323fbcf2018-04-10 13:46:30 -0400575 const GrVkImageInfo& vkInfo,
576 sk_sp<GrVkImageLayout> layout)
Greg Daniel9ca30652018-04-06 09:27:20 -0400577 : fIsValid(true)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400578 , fIsProtected(isProtected)
Greg Daniel9ca30652018-04-06 09:27:20 -0400579 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400580 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500581 , fSampleCnt(SkTMax(1, sampleCnt))
Brian Salomonafdc6b12018-03-09 12:02:32 -0500582 , fStencilBits(0) // We always create stencil buffers internally for vulkan
Greg Daniel108bb232018-07-03 16:18:29 -0400583 , fConfig(kUnknown_GrPixelConfig)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400584 , fBackend(GrBackendApi::kVulkan)
Greg Daniel323fbcf2018-04-10 13:46:30 -0400585 , fVkInfo(vkInfo, layout.release()) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000586#endif
Greg Daniel94403452017-04-18 15:52:36 -0400587
Timothy Liang4e85e802018-06-28 16:37:18 -0400588#ifdef SK_METAL
589GrBackendRenderTarget::GrBackendRenderTarget(int width,
590 int height,
591 int sampleCnt,
592 const GrMtlTextureInfo& mtlInfo)
593 : fIsValid(true)
594 , fWidth(width)
595 , fHeight(height)
596 , fSampleCnt(SkTMax(1, sampleCnt))
597 , fStencilBits(0)
598 , fConfig(GrPixelConfig::kUnknown_GrPixelConfig)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400599 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400600 , fMtlInfo(mtlInfo) {}
601#endif
602
Greg Danielfaa095e2017-12-19 13:15:02 -0500603GrBackendRenderTarget::GrBackendRenderTarget(int width,
604 int height,
605 int sampleCnt,
606 int stencilBits,
607 const GrGLFramebufferInfo& glInfo)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500608 : fWidth(width)
Greg Danielfaa095e2017-12-19 13:15:02 -0500609 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500610 , fSampleCnt(SkTMax(1, sampleCnt))
Greg Danielfaa095e2017-12-19 13:15:02 -0500611 , fStencilBits(stencilBits)
Greg Daniel108bb232018-07-03 16:18:29 -0400612 , fConfig(kUnknown_GrPixelConfig)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400613 , fBackend(GrBackendApi::kOpenGL)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500614 , fGLInfo(glInfo) {
615 fIsValid = SkToBool(glInfo.fFormat); // the glInfo must have a valid format
616}
Greg Danielfaa095e2017-12-19 13:15:02 -0500617
Brian Salomon0c51eea2018-03-09 17:02:09 -0500618GrBackendRenderTarget::GrBackendRenderTarget(int width,
619 int height,
620 int sampleCnt,
621 int stencilBits,
622 const GrMockRenderTargetInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400623 : fIsValid(true)
624 , fWidth(width)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500625 , fHeight(height)
626 , fSampleCnt(SkTMax(1, sampleCnt))
627 , fStencilBits(stencilBits)
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400628 , fConfig(mockInfo.pixelConfig())
Brian Salomon0c51eea2018-03-09 17:02:09 -0500629 , fMockInfo(mockInfo) {}
630
Greg Daniel323fbcf2018-04-10 13:46:30 -0400631GrBackendRenderTarget::~GrBackendRenderTarget() {
632 this->cleanup();
633}
634
635void GrBackendRenderTarget::cleanup() {
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000636#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400637 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400638 fVkInfo.cleanup();
639 }
640#endif
641}
642
643GrBackendRenderTarget::GrBackendRenderTarget(const GrBackendRenderTarget& that) : fIsValid(false) {
644 *this = that;
645}
646
647GrBackendRenderTarget& GrBackendRenderTarget::operator=(const GrBackendRenderTarget& that) {
648 if (!that.isValid()) {
649 this->cleanup();
650 fIsValid = false;
651 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -0400652 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400653 this->cleanup();
654 fIsValid = false;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400655 }
656 fWidth = that.fWidth;
657 fHeight = that.fHeight;
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400658 fIsProtected = that.fIsProtected;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400659 fSampleCnt = that.fSampleCnt;
660 fStencilBits = that.fStencilBits;
661 fConfig = that.fConfig;
662 fBackend = that.fBackend;
663
664 switch (that.fBackend) {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400665 case GrBackendApi::kOpenGL:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400666 fGLInfo = that.fGLInfo;
667 break;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400668 case GrBackendApi::kVulkan:
Greg Danielb4d89562018-10-03 18:44:49 +0000669#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400670 fVkInfo.assign(that.fVkInfo, this->isValid());
Mike Kleina55e2142018-10-03 16:34:11 +0000671#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000672 break;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400673#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400674 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000675 fMtlInfo = that.fMtlInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400676 break;
677#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400678 case GrBackendApi::kMock:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400679 fMockInfo = that.fMockInfo;
680 break;
681 default:
682 SK_ABORT("Unknown GrBackend");
683 }
684 fIsValid = that.fIsValid;
685 return *this;
686}
687
Mike Kleina55e2142018-10-03 16:34:11 +0000688bool GrBackendRenderTarget::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielb4d89562018-10-03 18:44:49 +0000689#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400690 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400691 *outInfo = fVkInfo.snapImageInfo();
692 return true;
693 }
Greg Danielb4d89562018-10-03 18:44:49 +0000694#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400695 return false;
696}
697
698void GrBackendRenderTarget::setVkImageLayout(VkImageLayout layout) {
Greg Danielb4d89562018-10-03 18:44:49 +0000699#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400700 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400701 fVkInfo.setImageLayout(layout);
702 }
Greg Danielb4d89562018-10-03 18:44:49 +0000703#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400704}
705
Greg Danielb4d89562018-10-03 18:44:49 +0000706#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400707sk_sp<GrVkImageLayout> GrBackendRenderTarget::getGrVkImageLayout() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400708 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400709 return fVkInfo.getGrVkImageLayout();
Greg Daniel94403452017-04-18 15:52:36 -0400710 }
711 return nullptr;
712}
Brian Salomone2826ab2019-06-04 15:58:31 -0400713#endif
Greg Daniel94403452017-04-18 15:52:36 -0400714
Timothy Liang4e85e802018-06-28 16:37:18 -0400715#ifdef SK_METAL
716bool GrBackendRenderTarget::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400717 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000718 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400719 return true;
720 }
721 return false;
722}
723#endif
724
Greg Daniel323fbcf2018-04-10 13:46:30 -0400725bool GrBackendRenderTarget::getGLFramebufferInfo(GrGLFramebufferInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400726 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400727 *outInfo = fGLInfo;
728 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400729 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400730 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400731}
732
Robert Phillipsf209e882019-06-25 15:59:50 -0400733GrBackendFormat GrBackendRenderTarget::getBackendFormat() const {
734 if (!this->isValid()) {
735 return GrBackendFormat();
736 }
737 switch (fBackend) {
738#ifdef SK_GL
739 case GrBackendApi::kOpenGL:
740 return GrBackendFormat::MakeGL(fGLInfo.fFormat, GR_GL_TEXTURE_NONE);
741#endif
742#ifdef SK_VULKAN
743 case GrBackendApi::kVulkan: {
744 auto info = fVkInfo.snapImageInfo();
745 if (info.fYcbcrConversionInfo.isValid()) {
746 SkASSERT(info.fFormat == VK_FORMAT_UNDEFINED);
747 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
748 }
749 return GrBackendFormat::MakeVk(info.fFormat);
750 }
751#endif
752#ifdef SK_METAL
753 case GrBackendApi::kMetal: {
754 GrMtlTextureInfo mtlInfo;
755 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
756 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
757 }
758#endif
759 case GrBackendApi::kMock:
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400760 return fMockInfo.getBackendFormat();
Robert Phillipsf209e882019-06-25 15:59:50 -0400761 default:
762 return GrBackendFormat();
763 }
764}
765
Greg Daniel323fbcf2018-04-10 13:46:30 -0400766bool GrBackendRenderTarget::getMockRenderTargetInfo(GrMockRenderTargetInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400767 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400768 *outInfo = fMockInfo;
769 return true;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500770 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400771 return false;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500772}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400773
774#if GR_TEST_UTILS
775bool GrBackendRenderTarget::TestingOnly_Equals(const GrBackendRenderTarget& r0,
776 const GrBackendRenderTarget& r1) {
777 if (!r0.isValid() || !r1.isValid()) {
778 return false; // two invalid backend rendertargets are not considered equal
779 }
780
781 if (r0.fWidth != r1.fWidth ||
782 r0.fHeight != r1.fHeight ||
783 r0.fSampleCnt != r1.fSampleCnt ||
784 r0.fStencilBits != r1.fStencilBits ||
785 r0.fConfig != r1.fConfig ||
786 r0.fBackend != r1.fBackend) {
787 return false;
788 }
789
790 switch (r0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400791#ifdef SK_GL
792 case GrBackendApi::kOpenGL:
793 return r0.fGLInfo == r1.fGLInfo;
794#endif
795 case GrBackendApi::kMock:
796 return r0.fMockInfo == r1.fMockInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400797#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400798 case GrBackendApi::kVulkan:
799 return r0.fVkInfo == r1.fVkInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400800#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400801#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -0400802 case GrBackendApi::kMetal:
803 return r0.fMtlInfo == r1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400804#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400805 default:
806 return false;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400807 }
808
809 SkASSERT(0);
810 return false;
811}
812#endif