blob: ecb7844793a944ff9bcfc6091c4f9a9477c27eab [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)
Greg Daniel9ca30652018-04-06 09:27:20 -0400475 : fIsValid(true)
476 , fWidth(width)
Greg Danielfaa095e2017-12-19 13:15:02 -0500477 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500478 , fSampleCnt(SkTMax(1, sampleCnt))
Greg Danielfaa095e2017-12-19 13:15:02 -0500479 , fStencilBits(stencilBits)
Greg Daniel108bb232018-07-03 16:18:29 -0400480 , fConfig(kUnknown_GrPixelConfig)
Greg Danielbdf12ad2018-10-12 09:31:11 -0400481 , fBackend(GrBackendApi::kOpenGL)
Greg Danielfaa095e2017-12-19 13:15:02 -0500482 , fGLInfo(glInfo) {}
483
Brian Salomon0c51eea2018-03-09 17:02:09 -0500484GrBackendRenderTarget::GrBackendRenderTarget(int width,
485 int height,
486 int sampleCnt,
487 int stencilBits,
488 const GrMockRenderTargetInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400489 : fIsValid(true)
490 , fWidth(width)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500491 , fHeight(height)
492 , fSampleCnt(SkTMax(1, sampleCnt))
493 , fStencilBits(stencilBits)
494 , fConfig(mockInfo.fConfig)
495 , fMockInfo(mockInfo) {}
496
Greg Daniel323fbcf2018-04-10 13:46:30 -0400497GrBackendRenderTarget::~GrBackendRenderTarget() {
498 this->cleanup();
499}
500
501void GrBackendRenderTarget::cleanup() {
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000502#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400503 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400504 fVkInfo.cleanup();
505 }
506#endif
507}
508
509GrBackendRenderTarget::GrBackendRenderTarget(const GrBackendRenderTarget& that) : fIsValid(false) {
510 *this = that;
511}
512
513GrBackendRenderTarget& GrBackendRenderTarget::operator=(const GrBackendRenderTarget& that) {
514 if (!that.isValid()) {
515 this->cleanup();
516 fIsValid = false;
517 return *this;
518 }
519 fWidth = that.fWidth;
520 fHeight = that.fHeight;
521 fSampleCnt = that.fSampleCnt;
522 fStencilBits = that.fStencilBits;
523 fConfig = that.fConfig;
524 fBackend = that.fBackend;
525
526 switch (that.fBackend) {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400527 case GrBackendApi::kOpenGL:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400528 fGLInfo = that.fGLInfo;
529 break;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400530 case GrBackendApi::kVulkan:
Greg Danielb4d89562018-10-03 18:44:49 +0000531#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400532 fVkInfo.assign(that.fVkInfo, this->isValid());
Mike Kleina55e2142018-10-03 16:34:11 +0000533#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000534 break;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400535#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400536 case GrBackendApi::kMetal:
Timothy Liang4e85e802018-06-28 16:37:18 -0400537 fMtlInfo = that.fMtlInfo;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400538 break;
539#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400540 case GrBackendApi::kMock:
Greg Daniel323fbcf2018-04-10 13:46:30 -0400541 fMockInfo = that.fMockInfo;
542 break;
543 default:
544 SK_ABORT("Unknown GrBackend");
545 }
546 fIsValid = that.fIsValid;
547 return *this;
548}
549
Mike Kleina55e2142018-10-03 16:34:11 +0000550bool GrBackendRenderTarget::getVkImageInfo(GrVkImageInfo* outInfo) const {
Greg Danielb4d89562018-10-03 18:44:49 +0000551#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400552 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400553 *outInfo = fVkInfo.snapImageInfo();
554 return true;
555 }
Greg Danielb4d89562018-10-03 18:44:49 +0000556#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400557 return false;
558}
559
560void GrBackendRenderTarget::setVkImageLayout(VkImageLayout layout) {
Greg Danielb4d89562018-10-03 18:44:49 +0000561#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400562 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400563 fVkInfo.setImageLayout(layout);
564 }
Greg Danielb4d89562018-10-03 18:44:49 +0000565#endif
Greg Daniel323fbcf2018-04-10 13:46:30 -0400566}
567
568sk_sp<GrVkImageLayout> GrBackendRenderTarget::getGrVkImageLayout() const {
Greg Danielb4d89562018-10-03 18:44:49 +0000569#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400570 if (this->isValid() && GrBackendApi::kVulkan == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400571 return fVkInfo.getGrVkImageLayout();
Greg Daniel94403452017-04-18 15:52:36 -0400572 }
Greg Danielb4d89562018-10-03 18:44:49 +0000573#endif
Greg Daniel94403452017-04-18 15:52:36 -0400574 return nullptr;
575}
576
Timothy Liang4e85e802018-06-28 16:37:18 -0400577#ifdef SK_METAL
578bool GrBackendRenderTarget::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400579 if (this->isValid() && GrBackendApi::kMetal == fBackend) {
Timothy Liang4e85e802018-06-28 16:37:18 -0400580 *outInfo = fMtlInfo;
581 return true;
582 }
583 return false;
584}
585#endif
586
Greg Daniel323fbcf2018-04-10 13:46:30 -0400587bool GrBackendRenderTarget::getGLFramebufferInfo(GrGLFramebufferInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400588 if (this->isValid() && GrBackendApi::kOpenGL == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400589 *outInfo = fGLInfo;
590 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400591 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400592 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400593}
594
Greg Daniel323fbcf2018-04-10 13:46:30 -0400595bool GrBackendRenderTarget::getMockRenderTargetInfo(GrMockRenderTargetInfo* outInfo) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400596 if (this->isValid() && GrBackendApi::kMock == fBackend) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400597 *outInfo = fMockInfo;
598 return true;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500599 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400600 return false;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500601}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400602
603#if GR_TEST_UTILS
604bool GrBackendRenderTarget::TestingOnly_Equals(const GrBackendRenderTarget& r0,
605 const GrBackendRenderTarget& r1) {
606 if (!r0.isValid() || !r1.isValid()) {
607 return false; // two invalid backend rendertargets are not considered equal
608 }
609
610 if (r0.fWidth != r1.fWidth ||
611 r0.fHeight != r1.fHeight ||
612 r0.fSampleCnt != r1.fSampleCnt ||
613 r0.fStencilBits != r1.fStencilBits ||
614 r0.fConfig != r1.fConfig ||
615 r0.fBackend != r1.fBackend) {
616 return false;
617 }
618
619 switch (r0.fBackend) {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400620 case GrBackendApi::kOpenGL:
Robert Phillips8caf85f2018-04-05 09:30:38 -0400621 return r0.fGLInfo == r1.fGLInfo;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400622 case GrBackendApi::kMock:
Robert Phillips8caf85f2018-04-05 09:30:38 -0400623 return r0.fMockInfo == r1.fMockInfo;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400624 case GrBackendApi::kVulkan:
Robert Phillips8caf85f2018-04-05 09:30:38 -0400625#ifdef SK_VULKAN
626 return r0.fVkInfo == r1.fVkInfo;
627#else
628 // fall through
629#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400630 case GrBackendApi::kMetal:
Timothy Liang4e85e802018-06-28 16:37:18 -0400631#ifdef SK_METAL
632 return r0.fMtlInfo == r1.fMtlInfo;
633#else
634 // fall through
635#endif
Robert Phillips8caf85f2018-04-05 09:30:38 -0400636 default:
637 return false;
638 }
639
640 SkASSERT(0);
641 return false;
642}
643#endif