blob: 4e6eb2732f8ee09664c520e3e363c27a66d32287 [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
Greg Daniel94403452017-04-18 15:52:36 -04009#include "GrBackendSurface.h"
10
Greg Daniele7d8da42017-12-04 11:23:19 -050011#include "gl/GrGLUtil.h"
12
Greg Daniel94403452017-04-18 15:52:36 -040013#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -040014#include "vk/GrVkImageLayout.h"
Greg Daniel94403452017-04-18 15:52:36 -040015#include "vk/GrVkTypes.h"
16#include "vk/GrVkUtil.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000017#endif
Timothy Liang4e85e802018-06-28 16:37:18 -040018#ifdef SK_METAL
19#include "mtl/GrMtlTypes.h"
Brian Salomonf391d0f2018-12-14 09:18:50 -050020#include "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) {
28 case GR_GL_TEXTURE_2D:
29 fTextureType = GrTextureType::k2D;
30 break;
31 case GR_GL_TEXTURE_RECTANGLE:
32 fTextureType = GrTextureType::kRectangle;
33 break;
34 case GR_GL_TEXTURE_EXTERNAL:
35 fTextureType = GrTextureType::kExternal;
36 break;
37 default:
38 SK_ABORT("Unexpected texture target");
39 }
Robert Phillipsfc711a22018-02-13 17:03:00 -050040}
41
42const GrGLenum* GrBackendFormat::getGLFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -040043 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel4065d452018-11-16 15:43:41 -050044 return &fGLFormat;
Robert Phillipsfc711a22018-02-13 17:03:00 -050045 }
46 return nullptr;
47}
48
49const GrGLenum* GrBackendFormat::getGLTarget() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -040050 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel4065d452018-11-16 15:43:41 -050051 static constexpr GrGLenum k2D = GR_GL_TEXTURE_2D;
52 static constexpr GrGLenum kRect = GR_GL_TEXTURE_RECTANGLE;
53 static constexpr GrGLenum kExternal = GR_GL_TEXTURE_EXTERNAL;
54 switch (fTextureType) {
55 case GrTextureType::k2D:
56 return &k2D;
57 case GrTextureType::kRectangle:
58 return &kRect;
59 case GrTextureType::kExternal:
60 return &kExternal;
61 }
Robert Phillipsfc711a22018-02-13 17:03:00 -050062 }
63 return nullptr;
64}
65
Greg Daniel7e000222018-12-03 10:08:21 -050066GrBackendFormat GrBackendFormat::MakeVk(const GrVkYcbcrConversionInfo& ycbcrInfo) {
67#ifdef SK_BUILD_FOR_ANDROID
68 return GrBackendFormat(VK_FORMAT_UNDEFINED, ycbcrInfo);
69#else
70 return GrBackendFormat();
71#endif
72}
73
74GrBackendFormat::GrBackendFormat(VkFormat vkFormat, const GrVkYcbcrConversionInfo& ycbcrInfo)
Greg Danielbdf12ad2018-10-12 09:31:11 -040075 : fBackend(GrBackendApi::kVulkan)
Greg Danielb4d89562018-10-03 18:44:49 +000076#ifdef SK_VULKAN
Robert Phillipsfc711a22018-02-13 17:03:00 -050077 , fValid(true)
Greg Danielb4d89562018-10-03 18:44:49 +000078#else
Greg Daniel4065d452018-11-16 15:43:41 -050079 , fValid(false)
Greg Danielb4d89562018-10-03 18:44:49 +000080#endif
Greg Daniel4065d452018-11-16 15:43:41 -050081 , fTextureType(GrTextureType::k2D) {
Greg Daniel7e000222018-12-03 10:08:21 -050082 fVk.fFormat = vkFormat;
83 fVk.fYcbcrConversionInfo = ycbcrInfo;
Robert Phillipsfc711a22018-02-13 17:03:00 -050084}
85
86const VkFormat* GrBackendFormat::getVkFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -040087 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel7e000222018-12-03 10:08:21 -050088 return &fVk.fFormat;
89 }
90 return nullptr;
91}
92
93const GrVkYcbcrConversionInfo* GrBackendFormat::getVkYcbcrConversionInfo() const {
94 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
95 return &fVk.fYcbcrConversionInfo;
Robert Phillipsfc711a22018-02-13 17:03:00 -050096 }
97 return nullptr;
98}
Robert Phillipsfc711a22018-02-13 17:03:00 -050099
Timothy Liang4e85e802018-06-28 16:37:18 -0400100#ifdef SK_METAL
101GrBackendFormat::GrBackendFormat(GrMTLPixelFormat mtlFormat)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400102 : fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400103 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500104 , fMtlFormat(mtlFormat)
105 , fTextureType(GrTextureType::k2D) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400106}
107
108const GrMTLPixelFormat* GrBackendFormat::getMtlFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400109 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400110 return &fMtlFormat;
111 }
112 return nullptr;
113}
114#endif
115
Robert Phillipsfc711a22018-02-13 17:03:00 -0500116GrBackendFormat::GrBackendFormat(GrPixelConfig config)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400117 : fBackend(GrBackendApi::kMock)
Robert Phillipsfc711a22018-02-13 17:03:00 -0500118 , fValid(true)
Greg Daniel4065d452018-11-16 15:43:41 -0500119 , fMockFormat(config)
120 , fTextureType(GrTextureType::k2D) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500121}
122
123const GrPixelConfig* GrBackendFormat::getMockFormat() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400124 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500125 return &fMockFormat;
126 }
127 return nullptr;
128}
129
Greg Daniel4065d452018-11-16 15:43:41 -0500130GrBackendFormat GrBackendFormat::makeTexture2D() const {
131 // TODO: once we support ycbcr conversions in Vulkan we need to check if we are using an
132 // external format since they will not be able to be made into a Texture2D.
133 GrBackendFormat copy = *this;
134 copy.fTextureType = GrTextureType::k2D;
135 return copy;
136}
137
Greg Daniel45723ac2018-11-30 10:12:43 -0500138bool GrBackendFormat::operator==(const GrBackendFormat& that) const {
139 // Invalid GrBackendFormats are never equal to anything.
140 if (!fValid || !that.fValid) {
141 return false;
142 }
143
144 if (fBackend != that.fBackend) {
145 return false;
146 }
147
148 switch (fBackend) {
149 case GrBackendApi::kOpenGL:
150 return fGLFormat == that.fGLFormat;
151 case GrBackendApi::kVulkan:
152#ifdef SK_VULKAN
153 return fVk.fFormat == that.fVk.fFormat &&
154 fVk.fYcbcrConversionInfo == that.fVk.fYcbcrConversionInfo;
155#endif
156 break;
157#ifdef SK_METAL
158 case GrBackendApi::kMetal:
159 return fMtlFormat == that.fMtlFormat;
160#endif
161 break;
162 case GrBackendApi::kMock:
163 return fMockFormat == that.fMockFormat;
164 default:
165 SK_ABORT("Unknown GrBackend");
166 }
167 return false;
168}
169
Greg Daniel94403452017-04-18 15:52:36 -0400170GrBackendTexture::GrBackendTexture(int width,
171 int height,
Greg Daniel207282e2017-04-26 13:29:21 -0400172 const GrVkImageInfo& vkInfo)
Greg Danielb4d89562018-10-03 18:44:49 +0000173#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400174 : GrBackendTexture(width, height, vkInfo,
175 sk_sp<GrVkImageLayout>(new GrVkImageLayout(vkInfo.fImageLayout))) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000176#else
177 : fIsValid(false) {}
178#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400179
Greg Danielb4d89562018-10-03 18:44:49 +0000180#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400181GrBackendTexture::GrBackendTexture(int width,
182 int height,
183 const GrVkImageInfo& vkInfo,
184 sk_sp<GrVkImageLayout> layout)
Greg Daniel9ca30652018-04-06 09:27:20 -0400185 : fIsValid(true)
186 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400187 , fHeight(height)
Greg Daniel108bb232018-07-03 16:18:29 -0400188 , fConfig(kUnknown_GrPixelConfig)
Chris Dalton3b51df12017-11-27 14:33:06 -0700189 , fMipMapped(GrMipMapped(vkInfo.fLevelCount > 1))
Greg Danielbdf12ad2018-10-12 09:31:11 -0400190 , fBackend(GrBackendApi::kVulkan)
Greg Daniel52e16d92018-04-10 09:34:07 -0400191 , fVkInfo(vkInfo, layout.release()) {
192}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000193#endif
Greg Daniel94403452017-04-18 15:52:36 -0400194
Timothy Liang4e85e802018-06-28 16:37:18 -0400195#ifdef SK_METAL
196GrBackendTexture::GrBackendTexture(int width,
197 int height,
198 GrMipMapped mipMapped,
199 const GrMtlTextureInfo& mtlInfo)
200 : fIsValid(true)
201 , fWidth(width)
202 , fHeight(height)
203 , fConfig(GrPixelConfig::kUnknown_GrPixelConfig)
204 , fMipMapped(mipMapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400205 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400206 , fMtlInfo(mtlInfo) {}
207#endif
208
Brian Salomon8fe24272017-07-07 12:56:11 -0400209GrBackendTexture::GrBackendTexture(int width,
210 int height,
Greg Daniele7d8da42017-12-04 11:23:19 -0500211 GrMipMapped mipMapped,
212 const GrGLTextureInfo& glInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400213 : fIsValid(true)
214 , fWidth(width)
Greg Daniele7d8da42017-12-04 11:23:19 -0500215 , fHeight(height)
Greg Daniel108bb232018-07-03 16:18:29 -0400216 , fConfig(kUnknown_GrPixelConfig)
Greg Daniele7d8da42017-12-04 11:23:19 -0500217 , fMipMapped(mipMapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400218 , fBackend(GrBackendApi::kOpenGL)
Greg Daniele7d8da42017-12-04 11:23:19 -0500219 , fGLInfo(glInfo) {}
220
221GrBackendTexture::GrBackendTexture(int width,
222 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400223 GrMipMapped mipMapped,
224 const GrMockTextureInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400225 : fIsValid(true)
226 , fWidth(width)
Brian Salomon8fe24272017-07-07 12:56:11 -0400227 , fHeight(height)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500228 , fConfig(mockInfo.fConfig)
Greg Daniel177e6952017-10-12 12:27:11 -0400229 , fMipMapped(mipMapped)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400230 , fBackend(GrBackendApi::kMock)
Brian Salomon8fe24272017-07-07 12:56:11 -0400231 , fMockInfo(mockInfo) {}
232
Greg Daniel52e16d92018-04-10 09:34:07 -0400233GrBackendTexture::~GrBackendTexture() {
234 this->cleanup();
235}
236
237void GrBackendTexture::cleanup() {
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000238#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400239 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400240 fVkInfo.cleanup();
241 }
242#endif
243}
244
245GrBackendTexture::GrBackendTexture(const GrBackendTexture& that) : fIsValid(false) {
246 *this = that;
247}
248
249GrBackendTexture& GrBackendTexture::operator=(const GrBackendTexture& that) {
250 if (!that.isValid()) {
251 this->cleanup();
252 fIsValid = false;
253 return *this;
254 }
255 fWidth = that.fWidth;
256 fHeight = that.fHeight;
257 fConfig = that.fConfig;
258 fMipMapped = that.fMipMapped;
259 fBackend = that.fBackend;
260
261 switch (that.fBackend) {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400262 case GrBackendApi::kOpenGL:
Greg Daniel52e16d92018-04-10 09:34:07 -0400263 fGLInfo = that.fGLInfo;
264 break;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400265 case GrBackendApi::kVulkan:
Greg Danielb4d89562018-10-03 18:44:49 +0000266#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -0400267 fVkInfo.assign(that.fVkInfo, this->isValid());
Mike Kleina55e2142018-10-03 16:34:11 +0000268#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000269 break;
Greg Daniel52e16d92018-04-10 09:34:07 -0400270#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400271 case GrBackendApi::kMetal:
Timothy Liang4e85e802018-06-28 16:37:18 -0400272 fMtlInfo = that.fMtlInfo;
Greg Daniel52e16d92018-04-10 09:34:07 -0400273 break;
274#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400275 case GrBackendApi::kMock:
Greg Daniel52e16d92018-04-10 09:34:07 -0400276 fMockInfo = that.fMockInfo;
277 break;
278 default:
279 SK_ABORT("Unknown GrBackend");
280 }
281 fIsValid = that.fIsValid;
282 return *this;
283}
284
Mike Kleina55e2142018-10-03 16:34:11 +0000285bool GrBackendTexture::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielb4d89562018-10-03 18:44:49 +0000286#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400287 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400288 *outInfo = fVkInfo.snapImageInfo();
289 return true;
290 }
Greg Danielb4d89562018-10-03 18:44:49 +0000291#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400292 return false;
293}
294
295void GrBackendTexture::setVkImageLayout(VkImageLayout layout) {
Greg Danielb4d89562018-10-03 18:44:49 +0000296#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400297 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400298 fVkInfo.setImageLayout(layout);
299 }
Greg Danielb4d89562018-10-03 18:44:49 +0000300#endif
Greg Daniel52e16d92018-04-10 09:34:07 -0400301}
302
Greg Danielb4d89562018-10-03 18:44:49 +0000303// We need a stubbed version of GrVkImageLayout for non vulkan builds
304#ifndef SK_VULKAN
305class GrVkImageLayout : public SkRefCnt {
306 GrVkImageLayout(VkImageLayout layout) {}
307};
308#endif
309
Greg Daniel52e16d92018-04-10 09:34:07 -0400310sk_sp<GrVkImageLayout> GrBackendTexture::getGrVkImageLayout() const {
Greg Danielb4d89562018-10-03 18:44:49 +0000311#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400312 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400313 return fVkInfo.getGrVkImageLayout();
Greg Daniel94403452017-04-18 15:52:36 -0400314 }
Greg Danielb4d89562018-10-03 18:44:49 +0000315#endif
Greg Daniel94403452017-04-18 15:52:36 -0400316 return nullptr;
317}
318
Timothy Liang4e85e802018-06-28 16:37:18 -0400319#ifdef SK_METAL
320bool GrBackendTexture::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400321 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400322 *outInfo = fMtlInfo;
323 return true;
324 }
325 return false;
326}
327#endif
328
Greg Daniel52e16d92018-04-10 09:34:07 -0400329bool GrBackendTexture::getGLTextureInfo(GrGLTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400330 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400331 *outInfo = fGLInfo;
332 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400333 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400334 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400335}
336
Greg Daniel52e16d92018-04-10 09:34:07 -0400337bool GrBackendTexture::getMockTextureInfo(GrMockTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400338 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400339 *outInfo = fMockInfo;
340 return true;
Brian Salomon8fe24272017-07-07 12:56:11 -0400341 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400342 return false;
Brian Salomon8fe24272017-07-07 12:56:11 -0400343}
344
Brian Salomonf391d0f2018-12-14 09:18:50 -0500345GrBackendFormat GrBackendTexture::getBackendFormat() const {
346 if (!this->isValid()) {
347 return GrBackendFormat();
348 }
349 switch (fBackend) {
350 case GrBackendApi::kOpenGL:
351 return GrBackendFormat::MakeGL(fGLInfo.fFormat, fGLInfo.fTarget);
352#ifdef SK_VULKAN
353 case GrBackendApi::kVulkan: {
354 auto info = fVkInfo.snapImageInfo();
355 if (info.fYcbcrConversionInfo.isValid()) {
356 SkASSERT(info.fFormat == VK_FORMAT_UNDEFINED);
357 return GrBackendFormat::MakeVk(info.fYcbcrConversionInfo);
358 }
359 return GrBackendFormat::MakeVk(info.fFormat);
360 }
361#endif
362#ifdef SK_METAL
363 case GrBackendApi::kMetal: {
364 GrMtlTextureInfo mtlInfo;
365 SkAssertResult(this->getMtlTextureInfo(&mtlInfo));
366 return GrBackendFormat::MakeMtl(GrGetMTLPixelFormatFromMtlTextureInfo(mtlInfo));
367 }
368#endif
369 case GrBackendApi::kMock:
370 return GrBackendFormat::MakeMock(fMockInfo.fConfig);
371 default:
372 return GrBackendFormat();
373 }
374}
375
Robert Phillipsc5509952018-04-04 15:54:55 -0400376#if GR_TEST_UTILS
377bool GrBackendTexture::TestingOnly_Equals(const GrBackendTexture& t0, const GrBackendTexture& t1) {
378 if (!t0.isValid() || !t1.isValid()) {
379 return false; // two invalid backend textures are not considered equal
380 }
381
382 if (t0.fWidth != t1.fWidth ||
383 t0.fHeight != t1.fHeight ||
384 t0.fConfig != t1.fConfig ||
385 t0.fMipMapped != t1.fMipMapped ||
386 t0.fBackend != t1.fBackend) {
387 return false;
388 }
389
390 switch (t0.fBackend) {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400391 case GrBackendApi::kOpenGL:
Robert Phillipsc5509952018-04-04 15:54:55 -0400392 return t0.fGLInfo == t1.fGLInfo;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400393 case GrBackendApi::kMock:
Robert Phillipsc5509952018-04-04 15:54:55 -0400394 return t0.fMockInfo == t1.fMockInfo;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400395 case GrBackendApi::kVulkan:
Robert Phillipsc5509952018-04-04 15:54:55 -0400396#ifdef SK_VULKAN
397 return t0.fVkInfo == t1.fVkInfo;
398#else
399 // fall through
400#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400401 case GrBackendApi::kMetal:
Timothy Liang4e85e802018-06-28 16:37:18 -0400402#ifdef SK_METAL
403 return t0.fMtlInfo == t1.fMtlInfo;
404#else
405 // fall through
406#endif
Robert Phillipsc5509952018-04-04 15:54:55 -0400407 default:
408 return false;
409 }
410
411 SkASSERT(0);
412 return false;
413}
414#endif
415
Greg Daniel94403452017-04-18 15:52:36 -0400416////////////////////////////////////////////////////////////////////////////////////////////////////
417
Greg Daniel94403452017-04-18 15:52:36 -0400418GrBackendRenderTarget::GrBackendRenderTarget(int width,
419 int height,
420 int sampleCnt,
421 int stencilBits,
Greg Danielbcf612b2017-05-01 13:50:58 +0000422 const GrVkImageInfo& vkInfo)
Brian Salomonafdc6b12018-03-09 12:02:32 -0500423 : GrBackendRenderTarget(width, height, sampleCnt, vkInfo) {
424 // This is a deprecated constructor that takes a bogus stencil bits.
425 SkASSERT(0 == stencilBits);
426}
427
428GrBackendRenderTarget::GrBackendRenderTarget(int width,
429 int height,
430 int sampleCnt,
431 const GrVkImageInfo& vkInfo)
Greg Danielb4d89562018-10-03 18:44:49 +0000432#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400433 : GrBackendRenderTarget(width, height, sampleCnt, vkInfo,
434 sk_sp<GrVkImageLayout>(new GrVkImageLayout(vkInfo.fImageLayout))) {}
Greg Danielb4d89562018-10-03 18:44:49 +0000435#else
436 : fIsValid(false) {}
437#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400438
Greg Danielb4d89562018-10-03 18:44:49 +0000439#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400440GrBackendRenderTarget::GrBackendRenderTarget(int width,
441 int height,
442 int sampleCnt,
443 const GrVkImageInfo& vkInfo,
444 sk_sp<GrVkImageLayout> layout)
Greg Daniel9ca30652018-04-06 09:27:20 -0400445 : fIsValid(true)
446 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400447 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500448 , fSampleCnt(SkTMax(1, sampleCnt))
Brian Salomonafdc6b12018-03-09 12:02:32 -0500449 , fStencilBits(0) // We always create stencil buffers internally for vulkan
Greg Daniel108bb232018-07-03 16:18:29 -0400450 , fConfig(kUnknown_GrPixelConfig)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400451 , fBackend(GrBackendApi::kVulkan)
Greg Daniel323fbcf2018-04-10 13:46:30 -0400452 , fVkInfo(vkInfo, layout.release()) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000453#endif
Greg Daniel94403452017-04-18 15:52:36 -0400454
Timothy Liang4e85e802018-06-28 16:37:18 -0400455#ifdef SK_METAL
456GrBackendRenderTarget::GrBackendRenderTarget(int width,
457 int height,
458 int sampleCnt,
459 const GrMtlTextureInfo& mtlInfo)
460 : fIsValid(true)
461 , fWidth(width)
462 , fHeight(height)
463 , fSampleCnt(SkTMax(1, sampleCnt))
464 , fStencilBits(0)
465 , fConfig(GrPixelConfig::kUnknown_GrPixelConfig)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400466 , fBackend(GrBackendApi::kMetal)
Timothy Liang4e85e802018-06-28 16:37:18 -0400467 , fMtlInfo(mtlInfo) {}
468#endif
469
Greg Danielfaa095e2017-12-19 13:15:02 -0500470GrBackendRenderTarget::GrBackendRenderTarget(int width,
471 int height,
472 int sampleCnt,
473 int stencilBits,
474 const GrGLFramebufferInfo& glInfo)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500475 : fWidth(width)
Greg Danielfaa095e2017-12-19 13:15:02 -0500476 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500477 , fSampleCnt(SkTMax(1, sampleCnt))
Greg Danielfaa095e2017-12-19 13:15:02 -0500478 , fStencilBits(stencilBits)
Greg Daniel108bb232018-07-03 16:18:29 -0400479 , fConfig(kUnknown_GrPixelConfig)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400480 , fBackend(GrBackendApi::kOpenGL)
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500481 , fGLInfo(glInfo) {
482 fIsValid = SkToBool(glInfo.fFormat); // the glInfo must have a valid format
483}
Greg Danielfaa095e2017-12-19 13:15:02 -0500484
Brian Salomon0c51eea2018-03-09 17:02:09 -0500485GrBackendRenderTarget::GrBackendRenderTarget(int width,
486 int height,
487 int sampleCnt,
488 int stencilBits,
489 const GrMockRenderTargetInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400490 : fIsValid(true)
491 , fWidth(width)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500492 , fHeight(height)
493 , fSampleCnt(SkTMax(1, sampleCnt))
494 , fStencilBits(stencilBits)
495 , fConfig(mockInfo.fConfig)
496 , fMockInfo(mockInfo) {}
497
Greg Daniel323fbcf2018-04-10 13:46:30 -0400498GrBackendRenderTarget::~GrBackendRenderTarget() {
499 this->cleanup();
500}
501
502void GrBackendRenderTarget::cleanup() {
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000503#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400504 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400505 fVkInfo.cleanup();
506 }
507#endif
508}
509
510GrBackendRenderTarget::GrBackendRenderTarget(const GrBackendRenderTarget& that) : fIsValid(false) {
511 *this = that;
512}
513
514GrBackendRenderTarget& GrBackendRenderTarget::operator=(const GrBackendRenderTarget& that) {
515 if (!that.isValid()) {
516 this->cleanup();
517 fIsValid = false;
518 return *this;
519 }
520 fWidth = that.fWidth;
521 fHeight = that.fHeight;
522 fSampleCnt = that.fSampleCnt;
523 fStencilBits = that.fStencilBits;
524 fConfig = that.fConfig;
525 fBackend = that.fBackend;
526
527 switch (that.fBackend) {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400528 case GrBackendApi::kOpenGL:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400529 fGLInfo = that.fGLInfo;
530 break;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400531 case GrBackendApi::kVulkan:
Greg Danielb4d89562018-10-03 18:44:49 +0000532#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400533 fVkInfo.assign(that.fVkInfo, this->isValid());
Mike Kleina55e2142018-10-03 16:34:11 +0000534#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000535 break;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400536#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400537 case GrBackendApi::kMetal:
Timothy Liang4e85e802018-06-28 16:37:18 -0400538 fMtlInfo = that.fMtlInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400539 break;
540#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400541 case GrBackendApi::kMock:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400542 fMockInfo = that.fMockInfo;
543 break;
544 default:
545 SK_ABORT("Unknown GrBackend");
546 }
547 fIsValid = that.fIsValid;
548 return *this;
549}
550
Mike Kleina55e2142018-10-03 16:34:11 +0000551bool GrBackendRenderTarget::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielb4d89562018-10-03 18:44:49 +0000552#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400553 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400554 *outInfo = fVkInfo.snapImageInfo();
555 return true;
556 }
Greg Danielb4d89562018-10-03 18:44:49 +0000557#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400558 return false;
559}
560
561void GrBackendRenderTarget::setVkImageLayout(VkImageLayout layout) {
Greg Danielb4d89562018-10-03 18:44:49 +0000562#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400563 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400564 fVkInfo.setImageLayout(layout);
565 }
Greg Danielb4d89562018-10-03 18:44:49 +0000566#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400567}
568
569sk_sp<GrVkImageLayout> GrBackendRenderTarget::getGrVkImageLayout() const {
Greg Danielb4d89562018-10-03 18:44:49 +0000570#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400571 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400572 return fVkInfo.getGrVkImageLayout();
Greg Daniel94403452017-04-18 15:52:36 -0400573 }
Greg Danielb4d89562018-10-03 18:44:49 +0000574#endif
Greg Daniel94403452017-04-18 15:52:36 -0400575 return nullptr;
576}
577
Timothy Liang4e85e802018-06-28 16:37:18 -0400578#ifdef SK_METAL
579bool GrBackendRenderTarget::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400580 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400581 *outInfo = fMtlInfo;
582 return true;
583 }
584 return false;
585}
586#endif
587
Greg Daniel323fbcf2018-04-10 13:46:30 -0400588bool GrBackendRenderTarget::getGLFramebufferInfo(GrGLFramebufferInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400589 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400590 *outInfo = fGLInfo;
591 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400592 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400593 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400594}
595
Greg Daniel323fbcf2018-04-10 13:46:30 -0400596bool GrBackendRenderTarget::getMockRenderTargetInfo(GrMockRenderTargetInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400597 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400598 *outInfo = fMockInfo;
599 return true;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500600 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400601 return false;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500602}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400603
604#if GR_TEST_UTILS
605bool GrBackendRenderTarget::TestingOnly_Equals(const GrBackendRenderTarget& r0,
606 const GrBackendRenderTarget& r1) {
607 if (!r0.isValid() || !r1.isValid()) {
608 return false; // two invalid backend rendertargets are not considered equal
609 }
610
611 if (r0.fWidth != r1.fWidth ||
612 r0.fHeight != r1.fHeight ||
613 r0.fSampleCnt != r1.fSampleCnt ||
614 r0.fStencilBits != r1.fStencilBits ||
615 r0.fConfig != r1.fConfig ||
616 r0.fBackend != r1.fBackend) {
617 return false;
618 }
619
620 switch (r0.fBackend) {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400621 case GrBackendApi::kOpenGL:
Robert Phillips8caf85f2018-04-05 09:30:38 -0400622 return r0.fGLInfo == r1.fGLInfo;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400623 case GrBackendApi::kMock:
Robert Phillips8caf85f2018-04-05 09:30:38 -0400624 return r0.fMockInfo == r1.fMockInfo;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400625 case GrBackendApi::kVulkan:
Robert Phillips8caf85f2018-04-05 09:30:38 -0400626#ifdef SK_VULKAN
627 return r0.fVkInfo == r1.fVkInfo;
628#else
629 // fall through
630#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400631 case GrBackendApi::kMetal:
Timothy Liang4e85e802018-06-28 16:37:18 -0400632#ifdef SK_METAL
633 return r0.fMtlInfo == r1.fMtlInfo;
634#else
635 // fall through
636#endif
Robert Phillips8caf85f2018-04-05 09:30:38 -0400637 default:
638 return false;
639 }
640
641 SkASSERT(0);
642 return false;
643}
644#endif