blob: ba8803546a0314458b4224cf32af7df721564edc [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 Phillipsfc711a22018-02-13 17:03:00 -050023GrBackendFormat::GrBackendFormat(GrGLenum format, GrGLenum target)
Greg Danielbdf12ad2018-10-12 09:31:11 -040024 : fBackend(GrBackendApi::kOpenGL)
Greg Daniel4065d452018-11-16 15:43:41 -050025 , fValid(true)
26 , fGLFormat(format) {
27 switch (target) {
Robert Phillipsf209e882019-06-25 15:59:50 -040028 case GR_GL_TEXTURE_NONE:
29 fTextureType = GrTextureType::kNone;
30 break;
Greg Daniel4065d452018-11-16 15:43:41 -050031 case GR_GL_TEXTURE_2D:
32 fTextureType = GrTextureType::k2D;
33 break;
34 case GR_GL_TEXTURE_RECTANGLE:
35 fTextureType = GrTextureType::kRectangle;
36 break;
37 case GR_GL_TEXTURE_EXTERNAL:
38 fTextureType = GrTextureType::kExternal;
39 break;
40 default:
41 SK_ABORT("Unexpected texture target");
42 }
Robert Phillipsfc711a22018-02-13 17:03:00 -050043}
44
45const GrGLenum* GrBackendFormat::getGLFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -040046 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel4065d452018-11-16 15:43:41 -050047 return &fGLFormat;
Robert Phillipsfc711a22018-02-13 17:03:00 -050048 }
49 return nullptr;
50}
51
52const GrGLenum* GrBackendFormat::getGLTarget() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -040053 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Robert Phillipsf209e882019-06-25 15:59:50 -040054 static constexpr GrGLenum kNone = GR_GL_TEXTURE_NONE;
Greg Daniel4065d452018-11-16 15:43:41 -050055 static constexpr GrGLenum k2D = GR_GL_TEXTURE_2D;
56 static constexpr GrGLenum kRect = GR_GL_TEXTURE_RECTANGLE;
57 static constexpr GrGLenum kExternal = GR_GL_TEXTURE_EXTERNAL;
58 switch (fTextureType) {
Robert Phillipsf209e882019-06-25 15:59:50 -040059 case GrTextureType::kNone:
60 return &kNone;
Greg Daniel4065d452018-11-16 15:43:41 -050061 case GrTextureType::k2D:
62 return &k2D;
63 case GrTextureType::kRectangle:
64 return &kRect;
65 case GrTextureType::kExternal:
66 return &kExternal;
67 }
Robert Phillipsfc711a22018-02-13 17:03:00 -050068 }
69 return nullptr;
70}
71
Greg Daniel7e000222018-12-03 10:08:21 -050072GrBackendFormat GrBackendFormat::MakeVk(const GrVkYcbcrConversionInfo& ycbcrInfo) {
73#ifdef SK_BUILD_FOR_ANDROID
74 return GrBackendFormat(VK_FORMAT_UNDEFINED, ycbcrInfo);
75#else
76 return GrBackendFormat();
77#endif
78}
79
80GrBackendFormat::GrBackendFormat(VkFormat vkFormat, const GrVkYcbcrConversionInfo& ycbcrInfo)
Greg Danielbdf12ad2018-10-12 09:31:11 -040081 : fBackend(GrBackendApi::kVulkan)
Greg Danielb4d89562018-10-03 18:44:49 +000082#ifdef SK_VULKAN
Robert Phillipsfc711a22018-02-13 17:03:00 -050083 , fValid(true)
Greg Danielb4d89562018-10-03 18:44:49 +000084#else
Greg Daniel4065d452018-11-16 15:43:41 -050085 , fValid(false)
Greg Danielb4d89562018-10-03 18:44:49 +000086#endif
Greg Daniel4065d452018-11-16 15:43:41 -050087 , fTextureType(GrTextureType::k2D) {
Greg Daniel7e000222018-12-03 10:08:21 -050088 fVk.fFormat = vkFormat;
89 fVk.fYcbcrConversionInfo = ycbcrInfo;
Greg Daniel387ec9a2019-03-07 16:44:54 -050090 if (fVk.fYcbcrConversionInfo.isValid()) {
91 fTextureType = GrTextureType::kExternal;
92 }
Robert Phillipsfc711a22018-02-13 17:03:00 -050093}
94
95const VkFormat* GrBackendFormat::getVkFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -040096 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel7e000222018-12-03 10:08:21 -050097 return &fVk.fFormat;
98 }
99 return nullptr;
100}
101
102const GrVkYcbcrConversionInfo* GrBackendFormat::getVkYcbcrConversionInfo() const {
103 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
104 return &fVk.fYcbcrConversionInfo;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500105 }
106 return nullptr;
107}
Robert Phillipsfc711a22018-02-13 17:03:00 -0500108
Timothy Liang4e85e802018-06-28 16:37:18 -0400109#ifdef SK_METAL
110GrBackendFormat::GrBackendFormat(GrMTLPixelFormat mtlFormat)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400111 : fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400112 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500113 , fMtlFormat(mtlFormat)
114 , fTextureType(GrTextureType::k2D) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400115}
116
117const GrMTLPixelFormat* GrBackendFormat::getMtlFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400118 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400119 return &fMtlFormat;
120 }
121 return nullptr;
122}
123#endif
124
Robert Phillipsfc711a22018-02-13 17:03:00 -0500125GrBackendFormat::GrBackendFormat(GrPixelConfig config)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400126 : fBackend(GrBackendApi::kMock)
Robert Phillipsfc711a22018-02-13 17:03:00 -0500127 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500128 , fMockFormat(config)
129 , fTextureType(GrTextureType::k2D) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500130}
131
132const GrPixelConfig* GrBackendFormat::getMockFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400133 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500134 return &fMockFormat;
135 }
136 return nullptr;
137}
138
Greg Daniel4065d452018-11-16 15:43:41 -0500139GrBackendFormat GrBackendFormat::makeTexture2D() const {
Greg Daniel4065d452018-11-16 15:43:41 -0500140 GrBackendFormat copy = *this;
Greg Daniel387ec9a2019-03-07 16:44:54 -0500141 if (const GrVkYcbcrConversionInfo* ycbcrInfo = this->getVkYcbcrConversionInfo()) {
142 if (ycbcrInfo->isValid()) {
143 // If we have a ycbcr we remove it from the backend format and set the VkFormat to
144 // R8G8B8A8_UNORM
145 SkASSERT(copy.fBackend == GrBackendApi::kVulkan);
146 copy.fVk.fYcbcrConversionInfo = GrVkYcbcrConversionInfo();
147 copy.fVk.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
148 }
149 }
Greg Daniel4065d452018-11-16 15:43:41 -0500150 copy.fTextureType = GrTextureType::k2D;
151 return copy;
152}
153
Greg Daniel45723ac2018-11-30 10:12:43 -0500154bool GrBackendFormat::operator==(const GrBackendFormat& that) const {
155 // Invalid GrBackendFormats are never equal to anything.
156 if (!fValid || !that.fValid) {
157 return false;
158 }
159
160 if (fBackend != that.fBackend) {
161 return false;
162 }
163
164 switch (fBackend) {
165 case GrBackendApi::kOpenGL:
166 return fGLFormat == that.fGLFormat;
167 case GrBackendApi::kVulkan:
168#ifdef SK_VULKAN
169 return fVk.fFormat == that.fVk.fFormat &&
170 fVk.fYcbcrConversionInfo == that.fVk.fYcbcrConversionInfo;
171#endif
172 break;
173#ifdef SK_METAL
174 case GrBackendApi::kMetal:
175 return fMtlFormat == that.fMtlFormat;
176#endif
177 break;
178 case GrBackendApi::kMock:
179 return fMockFormat == that.fMockFormat;
180 default:
181 SK_ABORT("Unknown GrBackend");
182 }
183 return false;
184}
185
Greg Daniel94403452017-04-18 15:52:36 -0400186GrBackendTexture::GrBackendTexture(int width,
187 int height,
Greg Daniel207282e2017-04-26 13:29:21 -0400188 const GrVkImageInfo& vkInfo)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400189 : GrBackendTexture(width, height, GrProtected::kNo, vkInfo) {}
190
191GrBackendTexture::GrBackendTexture(int width,
192 int height,
193 GrProtected isProtected,
194 const GrVkImageInfo& vkInfo)
Greg Danielb4d89562018-10-03 18:44:49 +0000195#ifdef SK_VULKAN
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400196 : GrBackendTexture(width, height, isProtected, vkInfo,
Greg Daniel52e16d92018-04-10 09:34:07 -0400197 sk_sp<GrVkImageLayout>(new GrVkImageLayout(vkInfo.fImageLayout))) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000198#else
199 : fIsValid(false) {}
200#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400201
Brian Salomone2826ab2019-06-04 15:58:31 -0400202#ifdef SK_GL
203GrBackendTexture::GrBackendTexture(int width,
204 int height,
205 GrMipMapped mipMapped,
206 const GrGLTextureInfo glInfo,
207 sk_sp<GrGLTextureParameters> params)
208 : fIsValid(true)
209 , fWidth(width)
210 , fHeight(height)
211 , fConfig(kUnknown_GrPixelConfig)
212 , fMipMapped(mipMapped)
213 , fBackend(GrBackendApi::kOpenGL)
214 , fGLInfo(glInfo, params.release()) {}
215
216sk_sp<GrGLTextureParameters> GrBackendTexture::getGLTextureParams() const {
217 if (fBackend != GrBackendApi::kOpenGL) {
218 return nullptr;
219 }
220 return fGLInfo.refParameters();
221}
222#endif
223
Greg Danielb4d89562018-10-03 18:44:49 +0000224#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400225GrBackendTexture::GrBackendTexture(int width,
226 int height,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400227 GrProtected isProtected,
Greg Daniel52e16d92018-04-10 09:34:07 -0400228 const GrVkImageInfo& vkInfo,
229 sk_sp<GrVkImageLayout> layout)
Greg Daniel9ca30652018-04-06 09:27:20 -0400230 : fIsValid(true)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400231 , fIsProtected(isProtected)
Greg Daniel9ca30652018-04-06 09:27:20 -0400232 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400233 , fHeight(height)
Greg Daniel108bb232018-07-03 16:18:29 -0400234 , fConfig(kUnknown_GrPixelConfig)
Chris Dalton3b51df12017-11-27 14:33:06 -0700235 , fMipMapped(GrMipMapped(vkInfo.fLevelCount > 1))
Greg Danielbdf12ad2018-10-12 09:31:11 -0400236 , fBackend(GrBackendApi::kVulkan)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400237 , fVkInfo(vkInfo, layout.release()) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000238#endif
Greg Daniel94403452017-04-18 15:52:36 -0400239
Timothy Liang4e85e802018-06-28 16:37:18 -0400240#ifdef SK_METAL
241GrBackendTexture::GrBackendTexture(int width,
242 int height,
243 GrMipMapped mipMapped,
244 const GrMtlTextureInfo& mtlInfo)
245 : fIsValid(true)
246 , fWidth(width)
247 , fHeight(height)
248 , fConfig(GrPixelConfig::kUnknown_GrPixelConfig)
249 , fMipMapped(mipMapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400250 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400251 , fMtlInfo(mtlInfo) {}
252#endif
253
Brian Salomon8fe24272017-07-07 12:56:11 -0400254GrBackendTexture::GrBackendTexture(int width,
255 int height,
Greg Daniele7d8da42017-12-04 11:23:19 -0500256 GrMipMapped mipMapped,
257 const GrGLTextureInfo& glInfo)
Brian Salomone2826ab2019-06-04 15:58:31 -0400258 : GrBackendTexture(width, height, mipMapped, glInfo, sk_make_sp<GrGLTextureParameters>()) {
259 // Make no assumptions about client's texture's parameters.
260 this->glTextureParametersModified();
261}
Greg Daniele7d8da42017-12-04 11:23:19 -0500262
263GrBackendTexture::GrBackendTexture(int width,
264 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400265 GrMipMapped mipMapped,
266 const GrMockTextureInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400267 : fIsValid(true)
268 , fWidth(width)
Brian Salomon8fe24272017-07-07 12:56:11 -0400269 , fHeight(height)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500270 , fConfig(mockInfo.fConfig)
Greg Daniel177e6952017-10-12 12:27:11 -0400271 , fMipMapped(mipMapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400272 , fBackend(GrBackendApi::kMock)
Brian Salomon8fe24272017-07-07 12:56:11 -0400273 , fMockInfo(mockInfo) {}
274
Greg Daniel52e16d92018-04-10 09:34:07 -0400275GrBackendTexture::~GrBackendTexture() {
276 this->cleanup();
277}
278
279void GrBackendTexture::cleanup() {
Brian Salomone2826ab2019-06-04 15:58:31 -0400280#ifdef SK_GL
281 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
282 fGLInfo.cleanup();
283 }
284#endif
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000285#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400286 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400287 fVkInfo.cleanup();
288 }
289#endif
290}
291
292GrBackendTexture::GrBackendTexture(const GrBackendTexture& that) : fIsValid(false) {
293 *this = that;
294}
295
296GrBackendTexture& GrBackendTexture::operator=(const GrBackendTexture& that) {
297 if (!that.isValid()) {
298 this->cleanup();
299 fIsValid = false;
300 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -0400301 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400302 this->cleanup();
303 fIsValid = false;
Greg Daniel52e16d92018-04-10 09:34:07 -0400304 }
305 fWidth = that.fWidth;
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400306 fIsProtected = that.fIsProtected;
Greg Daniel52e16d92018-04-10 09:34:07 -0400307 fHeight = that.fHeight;
308 fConfig = that.fConfig;
309 fMipMapped = that.fMipMapped;
310 fBackend = that.fBackend;
311
312 switch (that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400313#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400314 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400315 fGLInfo.assign(that.fGLInfo, this->isValid());
Greg Daniel52e16d92018-04-10 09:34:07 -0400316 break;
Mike Kleina55e2142018-10-03 16:34:11 +0000317#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400318#ifdef SK_VULKAN
319 case GrBackendApi::kVulkan:
320 fVkInfo.assign(that.fVkInfo, this->isValid());
Greg Danielb4d89562018-10-03 18:44:49 +0000321 break;
Brian Salomone2826ab2019-06-04 15:58:31 -0400322#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400323#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400324 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000325 fMtlInfo = that.fMtlInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400326 break;
327#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400328 case GrBackendApi::kMock:
Greg Daniel52e16d92018-04-10 09:34:07 -0400329 fMockInfo = that.fMockInfo;
330 break;
331 default:
332 SK_ABORT("Unknown GrBackend");
333 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400334 fIsValid = true;
Greg Daniel52e16d92018-04-10 09:34:07 -0400335 return *this;
336}
337
Mike Kleina55e2142018-10-03 16:34:11 +0000338bool GrBackendTexture::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielb4d89562018-10-03 18:44:49 +0000339#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400340 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400341 *outInfo = fVkInfo.snapImageInfo();
342 return true;
343 }
Greg Danielb4d89562018-10-03 18:44:49 +0000344#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400345 return false;
346}
347
348void GrBackendTexture::setVkImageLayout(VkImageLayout layout) {
Greg Danielb4d89562018-10-03 18:44:49 +0000349#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400350 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400351 fVkInfo.setImageLayout(layout);
352 }
Greg Danielb4d89562018-10-03 18:44:49 +0000353#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400354}
355
Greg Danielb4d89562018-10-03 18:44:49 +0000356#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400357sk_sp<GrVkImageLayout> GrBackendTexture::getGrVkImageLayout() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400358 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400359 return fVkInfo.getGrVkImageLayout();
Greg Daniel94403452017-04-18 15:52:36 -0400360 }
361 return nullptr;
362}
Brian Salomone2826ab2019-06-04 15:58:31 -0400363#endif
Greg Daniel94403452017-04-18 15:52:36 -0400364
Timothy Liang4e85e802018-06-28 16:37:18 -0400365#ifdef SK_METAL
366bool GrBackendTexture::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400367 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000368 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400369 return true;
370 }
371 return false;
372}
373#endif
374
Greg Daniel52e16d92018-04-10 09:34:07 -0400375bool GrBackendTexture::getGLTextureInfo(GrGLTextureInfo* outInfo) const {
Brian Salomone2826ab2019-06-04 15:58:31 -0400376#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400377 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400378 *outInfo = fGLInfo.info();
Greg Daniel52e16d92018-04-10 09:34:07 -0400379 return true;
Brian Osmana7ef3a02019-03-27 15:54:14 -0400380 } else if (this->isValid() && GrBackendApi::kMock == fBackend) {
381 // Hack! This allows some blink unit tests to work when using the Mock GrContext.
382 // Specifically, tests that rely on CanvasResourceProviderTextureGpuMemoryBuffer.
383 // If that code ever goes away (or ideally becomes backend-agnostic), this can go away.
384 *outInfo = GrGLTextureInfo{ GR_GL_TEXTURE_2D,
385 static_cast<GrGLuint>(fMockInfo.fID),
386 GR_GL_RGBA8 };
387 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400388 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400389#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400390 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400391}
392
Brian Salomone2826ab2019-06-04 15:58:31 -0400393void GrBackendTexture::glTextureParametersModified() {
394#ifdef SK_GL
395 if (this->isValid() && fBackend == GrBackendApi::kOpenGL) {
396 fGLInfo.parameters()->invalidate();
397 }
398#endif
399}
400
Greg Daniel52e16d92018-04-10 09:34:07 -0400401bool GrBackendTexture::getMockTextureInfo(GrMockTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400402 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400403 *outInfo = fMockInfo;
404 return true;
Brian Salomon8fe24272017-07-07 12:56:11 -0400405 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400406 return false;
Brian Salomon8fe24272017-07-07 12:56:11 -0400407}
408
Brian Salomonaad83152019-05-24 10:16:35 -0400409bool GrBackendTexture::isSameTexture(const GrBackendTexture& that) {
410 if (!this->isValid() || !that.isValid()) {
411 return false;
412 }
413 if (fBackend != that.fBackend) {
414 return false;
415 }
416 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400417#ifdef SK_GL
Brian Salomonaad83152019-05-24 10:16:35 -0400418 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400419 return fGLInfo.info().fID == that.fGLInfo.info().fID;
420#endif
Brian Salomonaad83152019-05-24 10:16:35 -0400421#ifdef SK_VULKAN
422 case GrBackendApi::kVulkan:
423 return fVkInfo.snapImageInfo().fImage == that.fVkInfo.snapImageInfo().fImage;
424#endif
425#ifdef SK_METAL
426 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000427 return this->fMtlInfo.fTexture == that.fMtlInfo.fTexture;
Brian Salomonaad83152019-05-24 10:16:35 -0400428#endif
429 case GrBackendApi::kMock:
430 return fMockInfo.fID == that.fMockInfo.fID;
431 default:
432 return false;
433 }
434}
435
Brian Salomonf391d0f2018-12-14 09:18:50 -0500436GrBackendFormat GrBackendTexture::getBackendFormat() const {
437 if (!this->isValid()) {
438 return GrBackendFormat();
439 }
440 switch (fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400441#ifdef SK_GL
Brian Salomonf391d0f2018-12-14 09:18:50 -0500442 case GrBackendApi::kOpenGL:
Brian Salomone2826ab2019-06-04 15:58:31 -0400443 return GrBackendFormat::MakeGL(fGLInfo.info().fFormat, fGLInfo.info().fTarget);
444#endif
Brian Salomonf391d0f2018-12-14 09:18:50 -0500445#ifdef SK_VULKAN
446 case GrBackendApi::kVulkan: {
447 auto info = fVkInfo.snapImageInfo();
448 if (info.fYcbcrConversionInfo.isValid()) {
449 SkASSERT(info.fFormat == VK_FORMAT_UNDEFINED);
450 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
451 }
452 return GrBackendFormat::MakeVk(info.fFormat);
453 }
454#endif
455#ifdef SK_METAL
456 case GrBackendApi::kMetal: {
457 GrMtlTextureInfo mtlInfo;
458 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
459 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
460 }
461#endif
462 case GrBackendApi::kMock:
463 return GrBackendFormat::MakeMock(fMockInfo.fConfig);
464 default:
465 return GrBackendFormat();
466 }
467}
468
Robert Phillipsc5509952018-04-04 15:54:55 -0400469#if GR_TEST_UTILS
470bool GrBackendTexture::TestingOnly_Equals(const GrBackendTexture& t0, const GrBackendTexture& t1) {
471 if (!t0.isValid() || !t1.isValid()) {
472 return false; // two invalid backend textures are not considered equal
473 }
474
475 if (t0.fWidth != t1.fWidth ||
476 t0.fHeight != t1.fHeight ||
477 t0.fConfig != t1.fConfig ||
478 t0.fMipMapped != t1.fMipMapped ||
479 t0.fBackend != t1.fBackend) {
480 return false;
481 }
482
483 switch (t0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400484#ifdef SK_GL
485 case GrBackendApi::kOpenGL:
486 return t0.fGLInfo.info() == t1.fGLInfo.info();
487#endif
488 case GrBackendApi::kMock:
489 return t0.fMockInfo == t1.fMockInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400490#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400491 case GrBackendApi::kVulkan:
492 return t0.fVkInfo == t1.fVkInfo;
Robert Phillipsc5509952018-04-04 15:54:55 -0400493#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400494#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -0400495 case GrBackendApi::kMetal:
496 return t0.fMtlInfo == t1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400497#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400498 default:
499 return false;
Robert Phillipsc5509952018-04-04 15:54:55 -0400500 }
Robert Phillipsc5509952018-04-04 15:54:55 -0400501}
502#endif
503
Greg Daniel94403452017-04-18 15:52:36 -0400504////////////////////////////////////////////////////////////////////////////////////////////////////
505
Greg Daniel94403452017-04-18 15:52:36 -0400506GrBackendRenderTarget::GrBackendRenderTarget(int width,
507 int height,
508 int sampleCnt,
509 int stencilBits,
Greg Danielbcf612b2017-05-01 13:50:58 +0000510 const GrVkImageInfo& vkInfo)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400511 : GrBackendRenderTarget(width, height, sampleCnt, GrProtected::kNo, vkInfo) {
Brian Salomonafdc6b12018-03-09 12:02:32 -0500512 // This is a deprecated constructor that takes a bogus stencil bits.
513 SkASSERT(0 == stencilBits);
514}
515
516GrBackendRenderTarget::GrBackendRenderTarget(int width,
517 int height,
518 int sampleCnt,
519 const GrVkImageInfo& vkInfo)
Greg Danielb4d89562018-10-03 18:44:49 +0000520#ifdef SK_VULKAN
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400521 : GrBackendRenderTarget(width, height, sampleCnt, false, vkInfo) {}
522#else
523 : fIsValid(false) {}
524#endif
525
526GrBackendRenderTarget::GrBackendRenderTarget(int width,
527 int height,
528 int sampleCnt,
529 GrProtected isProtected,
530 const GrVkImageInfo& vkInfo)
531#ifdef SK_VULKAN
532 : GrBackendRenderTarget(width, height, sampleCnt, isProtected, vkInfo,
Greg Daniel323fbcf2018-04-10 13:46:30 -0400533 sk_sp<GrVkImageLayout>(new GrVkImageLayout(vkInfo.fImageLayout))) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000534#else
535 : fIsValid(false) {}
536#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400537
Greg Danielb4d89562018-10-03 18:44:49 +0000538#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400539GrBackendRenderTarget::GrBackendRenderTarget(int width,
540 int height,
541 int sampleCnt,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400542 GrProtected isProtected,
Greg Daniel323fbcf2018-04-10 13:46:30 -0400543 const GrVkImageInfo& vkInfo,
544 sk_sp<GrVkImageLayout> layout)
Greg Daniel9ca30652018-04-06 09:27:20 -0400545 : fIsValid(true)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400546 , fIsProtected(isProtected)
Greg Daniel9ca30652018-04-06 09:27:20 -0400547 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400548 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500549 , fSampleCnt(SkTMax(1, sampleCnt))
Brian Salomonafdc6b12018-03-09 12:02:32 -0500550 , fStencilBits(0) // We always create stencil buffers internally for vulkan
Greg Daniel108bb232018-07-03 16:18:29 -0400551 , fConfig(kUnknown_GrPixelConfig)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400552 , fBackend(GrBackendApi::kVulkan)
Greg Daniel323fbcf2018-04-10 13:46:30 -0400553 , fVkInfo(vkInfo, layout.release()) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000554#endif
Greg Daniel94403452017-04-18 15:52:36 -0400555
Timothy Liang4e85e802018-06-28 16:37:18 -0400556#ifdef SK_METAL
557GrBackendRenderTarget::GrBackendRenderTarget(int width,
558 int height,
559 int sampleCnt,
560 const GrMtlTextureInfo& mtlInfo)
561 : fIsValid(true)
562 , fWidth(width)
563 , fHeight(height)
564 , fSampleCnt(SkTMax(1, sampleCnt))
565 , fStencilBits(0)
566 , fConfig(GrPixelConfig::kUnknown_GrPixelConfig)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400567 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400568 , fMtlInfo(mtlInfo) {}
569#endif
570
Greg Danielfaa095e2017-12-19 13:15:02 -0500571GrBackendRenderTarget::GrBackendRenderTarget(int width,
572 int height,
573 int sampleCnt,
574 int stencilBits,
575 const GrGLFramebufferInfo& glInfo)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500576 : fWidth(width)
Greg Danielfaa095e2017-12-19 13:15:02 -0500577 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500578 , fSampleCnt(SkTMax(1, sampleCnt))
Greg Danielfaa095e2017-12-19 13:15:02 -0500579 , fStencilBits(stencilBits)
Greg Daniel108bb232018-07-03 16:18:29 -0400580 , fConfig(kUnknown_GrPixelConfig)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400581 , fBackend(GrBackendApi::kOpenGL)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500582 , fGLInfo(glInfo) {
583 fIsValid = SkToBool(glInfo.fFormat); // the glInfo must have a valid format
584}
Greg Danielfaa095e2017-12-19 13:15:02 -0500585
Brian Salomon0c51eea2018-03-09 17:02:09 -0500586GrBackendRenderTarget::GrBackendRenderTarget(int width,
587 int height,
588 int sampleCnt,
589 int stencilBits,
590 const GrMockRenderTargetInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400591 : fIsValid(true)
592 , fWidth(width)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500593 , fHeight(height)
594 , fSampleCnt(SkTMax(1, sampleCnt))
595 , fStencilBits(stencilBits)
596 , fConfig(mockInfo.fConfig)
597 , fMockInfo(mockInfo) {}
598
Greg Daniel323fbcf2018-04-10 13:46:30 -0400599GrBackendRenderTarget::~GrBackendRenderTarget() {
600 this->cleanup();
601}
602
603void GrBackendRenderTarget::cleanup() {
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000604#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400605 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400606 fVkInfo.cleanup();
607 }
608#endif
609}
610
611GrBackendRenderTarget::GrBackendRenderTarget(const GrBackendRenderTarget& that) : fIsValid(false) {
612 *this = that;
613}
614
615GrBackendRenderTarget& GrBackendRenderTarget::operator=(const GrBackendRenderTarget& that) {
616 if (!that.isValid()) {
617 this->cleanup();
618 fIsValid = false;
619 return *this;
Ben Wagnera797cc92019-06-05 23:59:46 -0400620 } else if (fIsValid && this->fBackend != that.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400621 this->cleanup();
622 fIsValid = false;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400623 }
624 fWidth = that.fWidth;
625 fHeight = that.fHeight;
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400626 fIsProtected = that.fIsProtected;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400627 fSampleCnt = that.fSampleCnt;
628 fStencilBits = that.fStencilBits;
629 fConfig = that.fConfig;
630 fBackend = that.fBackend;
631
632 switch (that.fBackend) {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400633 case GrBackendApi::kOpenGL:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400634 fGLInfo = that.fGLInfo;
635 break;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400636 case GrBackendApi::kVulkan:
Greg Danielb4d89562018-10-03 18:44:49 +0000637#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400638 fVkInfo.assign(that.fVkInfo, this->isValid());
Mike Kleina55e2142018-10-03 16:34:11 +0000639#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000640 break;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400641#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400642 case GrBackendApi::kMetal:
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000643 fMtlInfo = that.fMtlInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400644 break;
645#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400646 case GrBackendApi::kMock:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400647 fMockInfo = that.fMockInfo;
648 break;
649 default:
650 SK_ABORT("Unknown GrBackend");
651 }
652 fIsValid = that.fIsValid;
653 return *this;
654}
655
Mike Kleina55e2142018-10-03 16:34:11 +0000656bool GrBackendRenderTarget::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielb4d89562018-10-03 18:44:49 +0000657#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400658 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400659 *outInfo = fVkInfo.snapImageInfo();
660 return true;
661 }
Greg Danielb4d89562018-10-03 18:44:49 +0000662#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400663 return false;
664}
665
666void GrBackendRenderTarget::setVkImageLayout(VkImageLayout layout) {
Greg Danielb4d89562018-10-03 18:44:49 +0000667#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400668 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400669 fVkInfo.setImageLayout(layout);
670 }
Greg Danielb4d89562018-10-03 18:44:49 +0000671#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400672}
673
Greg Danielb4d89562018-10-03 18:44:49 +0000674#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400675sk_sp<GrVkImageLayout> GrBackendRenderTarget::getGrVkImageLayout() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400676 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400677 return fVkInfo.getGrVkImageLayout();
Greg Daniel94403452017-04-18 15:52:36 -0400678 }
679 return nullptr;
680}
Brian Salomone2826ab2019-06-04 15:58:31 -0400681#endif
Greg Daniel94403452017-04-18 15:52:36 -0400682
Timothy Liang4e85e802018-06-28 16:37:18 -0400683#ifdef SK_METAL
684bool GrBackendRenderTarget::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400685 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Jim Van Verth7730d7c2019-05-28 03:03:45 +0000686 *outInfo = fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400687 return true;
688 }
689 return false;
690}
691#endif
692
Greg Daniel323fbcf2018-04-10 13:46:30 -0400693bool GrBackendRenderTarget::getGLFramebufferInfo(GrGLFramebufferInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400694 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400695 *outInfo = fGLInfo;
696 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400697 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400698 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400699}
700
Robert Phillipsf209e882019-06-25 15:59:50 -0400701GrBackendFormat GrBackendRenderTarget::getBackendFormat() const {
702 if (!this->isValid()) {
703 return GrBackendFormat();
704 }
705 switch (fBackend) {
706#ifdef SK_GL
707 case GrBackendApi::kOpenGL:
708 return GrBackendFormat::MakeGL(fGLInfo.fFormat, GR_GL_TEXTURE_NONE);
709#endif
710#ifdef SK_VULKAN
711 case GrBackendApi::kVulkan: {
712 auto info = fVkInfo.snapImageInfo();
713 if (info.fYcbcrConversionInfo.isValid()) {
714 SkASSERT(info.fFormat == VK_FORMAT_UNDEFINED);
715 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
716 }
717 return GrBackendFormat::MakeVk(info.fFormat);
718 }
719#endif
720#ifdef SK_METAL
721 case GrBackendApi::kMetal: {
722 GrMtlTextureInfo mtlInfo;
723 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
724 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
725 }
726#endif
727 case GrBackendApi::kMock:
728 return GrBackendFormat::MakeMock(fMockInfo.fConfig);
729 default:
730 return GrBackendFormat();
731 }
732}
733
Greg Daniel323fbcf2018-04-10 13:46:30 -0400734bool GrBackendRenderTarget::getMockRenderTargetInfo(GrMockRenderTargetInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400735 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400736 *outInfo = fMockInfo;
737 return true;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500738 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400739 return false;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500740}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400741
742#if GR_TEST_UTILS
743bool GrBackendRenderTarget::TestingOnly_Equals(const GrBackendRenderTarget& r0,
744 const GrBackendRenderTarget& r1) {
745 if (!r0.isValid() || !r1.isValid()) {
746 return false; // two invalid backend rendertargets are not considered equal
747 }
748
749 if (r0.fWidth != r1.fWidth ||
750 r0.fHeight != r1.fHeight ||
751 r0.fSampleCnt != r1.fSampleCnt ||
752 r0.fStencilBits != r1.fStencilBits ||
753 r0.fConfig != r1.fConfig ||
754 r0.fBackend != r1.fBackend) {
755 return false;
756 }
757
758 switch (r0.fBackend) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400759#ifdef SK_GL
760 case GrBackendApi::kOpenGL:
761 return r0.fGLInfo == r1.fGLInfo;
762#endif
763 case GrBackendApi::kMock:
764 return r0.fMockInfo == r1.fMockInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400765#ifdef SK_VULKAN
Brian Salomone2826ab2019-06-04 15:58:31 -0400766 case GrBackendApi::kVulkan:
767 return r0.fVkInfo == r1.fVkInfo;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400768#endif
Timothy Liang4e85e802018-06-28 16:37:18 -0400769#ifdef SK_METAL
Brian Salomone2826ab2019-06-04 15:58:31 -0400770 case GrBackendApi::kMetal:
771 return r0.fMtlInfo == r1.fMtlInfo;
Timothy Liang4e85e802018-06-28 16:37:18 -0400772#endif
Brian Salomone2826ab2019-06-04 15:58:31 -0400773 default:
774 return false;
Robert Phillips8caf85f2018-04-05 09:30:38 -0400775 }
776
777 SkASSERT(0);
778 return false;
779}
780#endif