blob: eb7aea2020d1c751314dcaf7341b10bd3ec6b066 [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
8#include "GrBackendSurface.h"
9
Greg Daniele7d8da42017-12-04 11:23:19 -050010#include "gl/GrGLUtil.h"
11
Greg Daniel94403452017-04-18 15:52:36 -040012#ifdef SK_VULKAN
Greg Daniel52e16d92018-04-10 09:34:07 -040013#include "vk/GrVkImageLayout.h"
Greg Daniel94403452017-04-18 15:52:36 -040014#include "vk/GrVkTypes.h"
15#include "vk/GrVkUtil.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000016#endif
17
Robert Phillipsfc711a22018-02-13 17:03:00 -050018GrBackendFormat::GrBackendFormat(GrGLenum format, GrGLenum target)
19 : fBackend(kOpenGL_GrBackend)
20 , fValid(true) {
21 fGL.fTarget = target;
22 fGL.fFormat = format;
23}
24
25const GrGLenum* GrBackendFormat::getGLFormat() const {
26 if (this->isValid() && kOpenGL_GrBackend == fBackend) {
27 return &fGL.fFormat;
28 }
29 return nullptr;
30}
31
32const GrGLenum* GrBackendFormat::getGLTarget() const {
33 if (this->isValid() && kOpenGL_GrBackend == fBackend) {
34 return &fGL.fTarget;
35 }
36 return nullptr;
37}
38
39#ifdef SK_VULKAN
40GrBackendFormat::GrBackendFormat(VkFormat vkFormat)
41 : fBackend(kVulkan_GrBackend)
42 , fValid(true)
43 , fVkFormat(vkFormat) {
44}
45
46const VkFormat* GrBackendFormat::getVkFormat() const {
47 if (this->isValid() && kVulkan_GrBackend == fBackend) {
48 return &fVkFormat;
49 }
50 return nullptr;
51}
52#endif
53
54GrBackendFormat::GrBackendFormat(GrPixelConfig config)
55 : fBackend(kMock_GrBackend)
56 , fValid(true)
57 , fMockFormat(config) {
58}
59
60const GrPixelConfig* GrBackendFormat::getMockFormat() const {
61 if (this->isValid() && kMock_GrBackend == fBackend) {
62 return &fMockFormat;
63 }
64 return nullptr;
65}
66
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +000067#ifdef SK_VULKAN
Greg Daniel94403452017-04-18 15:52:36 -040068GrBackendTexture::GrBackendTexture(int width,
69 int height,
Greg Daniel207282e2017-04-26 13:29:21 -040070 const GrVkImageInfo& vkInfo)
Greg Daniel52e16d92018-04-10 09:34:07 -040071 : GrBackendTexture(width, height, vkInfo,
72 sk_sp<GrVkImageLayout>(new GrVkImageLayout(vkInfo.fImageLayout))) {}
73
74GrBackendTexture::GrBackendTexture(int width,
75 int height,
76 const GrVkImageInfo& vkInfo,
77 sk_sp<GrVkImageLayout> layout)
Greg Daniel9ca30652018-04-06 09:27:20 -040078 : fIsValid(true)
79 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -040080 , fHeight(height)
Greg Daniel8a3f55c2018-03-14 17:32:12 +000081 , fConfig(GrVkFormatToPixelConfig(vkInfo.fFormat))
Chris Dalton3b51df12017-11-27 14:33:06 -070082 , fMipMapped(GrMipMapped(vkInfo.fLevelCount > 1))
Greg Daniel94403452017-04-18 15:52:36 -040083 , fBackend(kVulkan_GrBackend)
Greg Daniel52e16d92018-04-10 09:34:07 -040084 , fVkInfo(vkInfo, layout.release()) {
85}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +000086#endif
Greg Daniel94403452017-04-18 15:52:36 -040087
Brian Salomon34df0d32018-03-23 18:23:23 -040088#if GR_TEST_UTILS
89
Greg Daniel94403452017-04-18 15:52:36 -040090GrBackendTexture::GrBackendTexture(int width,
91 int height,
92 GrPixelConfig config,
Greg Daniel207282e2017-04-26 13:29:21 -040093 const GrGLTextureInfo& glInfo)
Greg Daniel177e6952017-10-12 12:27:11 -040094 : GrBackendTexture(width, height, config, GrMipMapped::kNo, glInfo) {}
95
96GrBackendTexture::GrBackendTexture(int width,
97 int height,
98 GrPixelConfig config,
99 GrMipMapped mipMapped,
100 const GrGLTextureInfo& glInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400101 : fIsValid(true)
102 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400103 , fHeight(height)
104 , fConfig(config)
Greg Daniel177e6952017-10-12 12:27:11 -0400105 , fMipMapped(mipMapped)
Greg Daniel94403452017-04-18 15:52:36 -0400106 , fBackend(kOpenGL_GrBackend)
107 , fGLInfo(glInfo) {}
Brian Salomon34df0d32018-03-23 18:23:23 -0400108#endif
Greg Daniel94403452017-04-18 15:52:36 -0400109
Brian Salomon8fe24272017-07-07 12:56:11 -0400110GrBackendTexture::GrBackendTexture(int width,
111 int height,
Greg Daniele7d8da42017-12-04 11:23:19 -0500112 GrMipMapped mipMapped,
113 const GrGLTextureInfo& glInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400114 : fIsValid(true)
115 , fWidth(width)
Greg Daniele7d8da42017-12-04 11:23:19 -0500116 , fHeight(height)
Greg Daniel8a3f55c2018-03-14 17:32:12 +0000117 , fConfig(GrGLSizedFormatToPixelConfig(glInfo.fFormat))
Greg Daniele7d8da42017-12-04 11:23:19 -0500118 , fMipMapped(mipMapped)
119 , fBackend(kOpenGL_GrBackend)
120 , fGLInfo(glInfo) {}
121
122GrBackendTexture::GrBackendTexture(int width,
123 int height,
Greg Daniel177e6952017-10-12 12:27:11 -0400124 GrMipMapped mipMapped,
125 const GrMockTextureInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400126 : fIsValid(true)
127 , fWidth(width)
Brian Salomon8fe24272017-07-07 12:56:11 -0400128 , fHeight(height)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500129 , fConfig(mockInfo.fConfig)
Greg Daniel177e6952017-10-12 12:27:11 -0400130 , fMipMapped(mipMapped)
Brian Salomon8fe24272017-07-07 12:56:11 -0400131 , fBackend(kMock_GrBackend)
132 , fMockInfo(mockInfo) {}
133
Greg Daniel52e16d92018-04-10 09:34:07 -0400134GrBackendTexture::~GrBackendTexture() {
135 this->cleanup();
136}
137
138void GrBackendTexture::cleanup() {
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000139#ifdef SK_VULKAN
Brian Salomon8fe24272017-07-07 12:56:11 -0400140 if (this->isValid() && kVulkan_GrBackend == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400141 fVkInfo.cleanup();
142 }
143#endif
144}
145
146GrBackendTexture::GrBackendTexture(const GrBackendTexture& that) : fIsValid(false) {
147 *this = that;
148}
149
150GrBackendTexture& GrBackendTexture::operator=(const GrBackendTexture& that) {
151 if (!that.isValid()) {
152 this->cleanup();
153 fIsValid = false;
154 return *this;
155 }
156 fWidth = that.fWidth;
157 fHeight = that.fHeight;
158 fConfig = that.fConfig;
159 fMipMapped = that.fMipMapped;
160 fBackend = that.fBackend;
161
162 switch (that.fBackend) {
163 case kOpenGL_GrBackend:
164 fGLInfo = that.fGLInfo;
165 break;
166#ifdef SK_VULKAN
167 case kVulkan_GrBackend:
168 fVkInfo.assign(that.fVkInfo, this->isValid());
169 break;
170#endif
171#ifdef SK_METAL
172 case kMetal_GrBackend:
173 break;
174#endif
175 case kMock_GrBackend:
176 fMockInfo = that.fMockInfo;
177 break;
178 default:
179 SK_ABORT("Unknown GrBackend");
180 }
181 fIsValid = that.fIsValid;
182 return *this;
183}
184
185#ifdef SK_VULKAN
186bool GrBackendTexture::getVkImageInfo(GrVkImageInfo* outInfo) const {
187 if (this->isValid() && kVulkan_GrBackend == fBackend) {
188 *outInfo = fVkInfo.snapImageInfo();
189 return true;
190 }
191 return false;
192}
193
194void GrBackendTexture::setVkImageLayout(VkImageLayout layout) {
195 if (this->isValid() && kVulkan_GrBackend == fBackend) {
196 fVkInfo.setImageLayout(layout);
197 }
198}
199
200sk_sp<GrVkImageLayout> GrBackendTexture::getGrVkImageLayout() const {
201 if (this->isValid() && kVulkan_GrBackend == fBackend) {
202 return fVkInfo.getGrVkImageLayout();
Greg Daniel94403452017-04-18 15:52:36 -0400203 }
204 return nullptr;
205}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000206#endif
Greg Daniel94403452017-04-18 15:52:36 -0400207
Greg Daniel52e16d92018-04-10 09:34:07 -0400208bool GrBackendTexture::getGLTextureInfo(GrGLTextureInfo* outInfo) const {
Brian Salomon8fe24272017-07-07 12:56:11 -0400209 if (this->isValid() && kOpenGL_GrBackend == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400210 *outInfo = fGLInfo;
211 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400212 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400213 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400214}
215
Greg Daniel52e16d92018-04-10 09:34:07 -0400216bool GrBackendTexture::getMockTextureInfo(GrMockTextureInfo* outInfo) const {
Brian Salomon8fe24272017-07-07 12:56:11 -0400217 if (this->isValid() && kMock_GrBackend == fBackend) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400218 *outInfo = fMockInfo;
219 return true;
Brian Salomon8fe24272017-07-07 12:56:11 -0400220 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400221 return false;
Brian Salomon8fe24272017-07-07 12:56:11 -0400222}
223
Robert Phillips7f441962018-03-15 11:15:19 -0400224GrBackendFormat GrBackendTexture::format() const {
Greg Daniel52e16d92018-04-10 09:34:07 -0400225 if (!this->isValid()) {
226 return GrBackendFormat();
227 }
228
Robert Phillips7f441962018-03-15 11:15:19 -0400229 switch (this->backend()) {
230#ifdef SK_VULKAN
231 case kVulkan_GrBackend: {
Greg Daniel52e16d92018-04-10 09:34:07 -0400232 GrVkImageInfo vkInfo;
233 SkAssertResult(this->getVkImageInfo(&vkInfo));
234 return GrBackendFormat::MakeVk(vkInfo.fFormat);
Robert Phillips7f441962018-03-15 11:15:19 -0400235 }
236#endif
237 case kOpenGL_GrBackend: {
Greg Daniel52e16d92018-04-10 09:34:07 -0400238 GrGLTextureInfo glInfo;
239 SkAssertResult(this->getGLTextureInfo(&glInfo));
240 return GrBackendFormat::MakeGL(glInfo.fFormat, glInfo.fTarget);
Robert Phillips7f441962018-03-15 11:15:19 -0400241 }
242 case kMock_GrBackend: {
Greg Daniel52e16d92018-04-10 09:34:07 -0400243 GrMockTextureInfo mockInfo;
244 SkAssertResult(this->getMockTextureInfo(&mockInfo));
245 return GrBackendFormat::MakeMock(mockInfo.fConfig);
Robert Phillips7f441962018-03-15 11:15:19 -0400246 }
247 default:
248 return GrBackendFormat();
249 }
250}
251
Robert Phillipsc5509952018-04-04 15:54:55 -0400252#if GR_TEST_UTILS
253bool GrBackendTexture::TestingOnly_Equals(const GrBackendTexture& t0, const GrBackendTexture& t1) {
254 if (!t0.isValid() || !t1.isValid()) {
255 return false; // two invalid backend textures are not considered equal
256 }
257
258 if (t0.fWidth != t1.fWidth ||
259 t0.fHeight != t1.fHeight ||
260 t0.fConfig != t1.fConfig ||
261 t0.fMipMapped != t1.fMipMapped ||
262 t0.fBackend != t1.fBackend) {
263 return false;
264 }
265
266 switch (t0.fBackend) {
267 case kOpenGL_GrBackend:
268 return t0.fGLInfo == t1.fGLInfo;
269 case kMock_GrBackend:
270 return t0.fMockInfo == t1.fMockInfo;
271 case kVulkan_GrBackend:
272#ifdef SK_VULKAN
273 return t0.fVkInfo == t1.fVkInfo;
274#else
275 // fall through
276#endif
277 case kMetal_GrBackend: // fall through
278 default:
279 return false;
280 }
281
282 SkASSERT(0);
283 return false;
284}
285#endif
286
Greg Daniel94403452017-04-18 15:52:36 -0400287////////////////////////////////////////////////////////////////////////////////////////////////////
288
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000289#ifdef SK_VULKAN
Greg Daniel94403452017-04-18 15:52:36 -0400290GrBackendRenderTarget::GrBackendRenderTarget(int width,
291 int height,
292 int sampleCnt,
293 int stencilBits,
Greg Danielbcf612b2017-05-01 13:50:58 +0000294 const GrVkImageInfo& vkInfo)
Brian Salomonafdc6b12018-03-09 12:02:32 -0500295 : GrBackendRenderTarget(width, height, sampleCnt, vkInfo) {
296 // This is a deprecated constructor that takes a bogus stencil bits.
297 SkASSERT(0 == stencilBits);
298}
299
300GrBackendRenderTarget::GrBackendRenderTarget(int width,
301 int height,
302 int sampleCnt,
303 const GrVkImageInfo& vkInfo)
Greg Daniel323fbcf2018-04-10 13:46:30 -0400304 : GrBackendRenderTarget(width, height, sampleCnt, vkInfo,
305 sk_sp<GrVkImageLayout>(new GrVkImageLayout(vkInfo.fImageLayout))) {}
306
307GrBackendRenderTarget::GrBackendRenderTarget(int width,
308 int height,
309 int sampleCnt,
310 const GrVkImageInfo& vkInfo,
311 sk_sp<GrVkImageLayout> layout)
Greg Daniel9ca30652018-04-06 09:27:20 -0400312 : fIsValid(true)
313 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400314 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500315 , fSampleCnt(SkTMax(1, sampleCnt))
Brian Salomonafdc6b12018-03-09 12:02:32 -0500316 , fStencilBits(0) // We always create stencil buffers internally for vulkan
Greg Daniel8a3f55c2018-03-14 17:32:12 +0000317 , fConfig(GrVkFormatToPixelConfig(vkInfo.fFormat))
Greg Daniel94403452017-04-18 15:52:36 -0400318 , fBackend(kVulkan_GrBackend)
Greg Daniel323fbcf2018-04-10 13:46:30 -0400319 , fVkInfo(vkInfo, layout.release()) {}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000320#endif
Greg Daniel94403452017-04-18 15:52:36 -0400321
Brian Salomon34df0d32018-03-23 18:23:23 -0400322#if GR_TEST_UTILS
323
Greg Daniel94403452017-04-18 15:52:36 -0400324GrBackendRenderTarget::GrBackendRenderTarget(int width,
325 int height,
326 int sampleCnt,
327 int stencilBits,
328 GrPixelConfig config,
Greg Danielbcf612b2017-05-01 13:50:58 +0000329 const GrGLFramebufferInfo& glInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400330 : fIsValid(true)
331 , fWidth(width)
Greg Daniel94403452017-04-18 15:52:36 -0400332 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500333 , fSampleCnt(SkTMax(1, sampleCnt))
Greg Daniel94403452017-04-18 15:52:36 -0400334 , fStencilBits(stencilBits)
335 , fConfig(config)
336 , fBackend(kOpenGL_GrBackend)
337 , fGLInfo(glInfo) {}
Brian Salomon34df0d32018-03-23 18:23:23 -0400338#endif
Greg Daniel94403452017-04-18 15:52:36 -0400339
Greg Danielfaa095e2017-12-19 13:15:02 -0500340GrBackendRenderTarget::GrBackendRenderTarget(int width,
341 int height,
342 int sampleCnt,
343 int stencilBits,
344 const GrGLFramebufferInfo& glInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400345 : fIsValid(true)
346 , fWidth(width)
Greg Danielfaa095e2017-12-19 13:15:02 -0500347 , fHeight(height)
Brian Salomonbdecacf2018-02-02 20:32:49 -0500348 , fSampleCnt(SkTMax(1, sampleCnt))
Greg Danielfaa095e2017-12-19 13:15:02 -0500349 , fStencilBits(stencilBits)
Greg Daniel8a3f55c2018-03-14 17:32:12 +0000350 , fConfig(GrGLSizedFormatToPixelConfig(glInfo.fFormat))
Greg Danielfaa095e2017-12-19 13:15:02 -0500351 , fBackend(kOpenGL_GrBackend)
352 , fGLInfo(glInfo) {}
353
Brian Salomon0c51eea2018-03-09 17:02:09 -0500354GrBackendRenderTarget::GrBackendRenderTarget(int width,
355 int height,
356 int sampleCnt,
357 int stencilBits,
358 const GrMockRenderTargetInfo& mockInfo)
Greg Daniel9ca30652018-04-06 09:27:20 -0400359 : fIsValid(true)
360 , fWidth(width)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500361 , fHeight(height)
362 , fSampleCnt(SkTMax(1, sampleCnt))
363 , fStencilBits(stencilBits)
364 , fConfig(mockInfo.fConfig)
365 , fMockInfo(mockInfo) {}
366
Greg Daniel323fbcf2018-04-10 13:46:30 -0400367GrBackendRenderTarget::~GrBackendRenderTarget() {
368 this->cleanup();
369}
370
371void GrBackendRenderTarget::cleanup() {
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000372#ifdef SK_VULKAN
Greg Daniel323fbcf2018-04-10 13:46:30 -0400373 if (this->isValid() && kVulkan_GrBackend == fBackend) {
374 fVkInfo.cleanup();
375 }
376#endif
377}
378
379GrBackendRenderTarget::GrBackendRenderTarget(const GrBackendRenderTarget& that) : fIsValid(false) {
380 *this = that;
381}
382
383GrBackendRenderTarget& GrBackendRenderTarget::operator=(const GrBackendRenderTarget& that) {
384 if (!that.isValid()) {
385 this->cleanup();
386 fIsValid = false;
387 return *this;
388 }
389 fWidth = that.fWidth;
390 fHeight = that.fHeight;
391 fSampleCnt = that.fSampleCnt;
392 fStencilBits = that.fStencilBits;
393 fConfig = that.fConfig;
394 fBackend = that.fBackend;
395
396 switch (that.fBackend) {
397 case kOpenGL_GrBackend:
398 fGLInfo = that.fGLInfo;
399 break;
400#ifdef SK_VULKAN
401 case kVulkan_GrBackend:
402 fVkInfo.assign(that.fVkInfo, this->isValid());
403 break;
404#endif
405#ifdef SK_METAL
406 case kMetal_GrBackend:
407 break;
408#endif
409 case kMock_GrBackend:
410 fMockInfo = that.fMockInfo;
411 break;
412 default:
413 SK_ABORT("Unknown GrBackend");
414 }
415 fIsValid = that.fIsValid;
416 return *this;
417}
418
419#ifdef SK_VULKAN
420bool GrBackendRenderTarget::getVkImageInfo(GrVkImageInfo* outInfo) const {
421 if (this->isValid() && kVulkan_GrBackend == fBackend) {
422 *outInfo = fVkInfo.snapImageInfo();
423 return true;
424 }
425 return false;
426}
427
428void GrBackendRenderTarget::setVkImageLayout(VkImageLayout layout) {
429 if (this->isValid() && kVulkan_GrBackend == fBackend) {
430 fVkInfo.setImageLayout(layout);
431 }
432}
433
434sk_sp<GrVkImageLayout> GrBackendRenderTarget::getGrVkImageLayout() const {
435 if (this->isValid() && kVulkan_GrBackend == fBackend) {
436 return fVkInfo.getGrVkImageLayout();
Greg Daniel94403452017-04-18 15:52:36 -0400437 }
438 return nullptr;
439}
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000440#endif
Greg Daniel94403452017-04-18 15:52:36 -0400441
Greg Daniel323fbcf2018-04-10 13:46:30 -0400442bool GrBackendRenderTarget::getGLFramebufferInfo(GrGLFramebufferInfo* outInfo) const {
443 if (this->isValid() && kOpenGL_GrBackend == fBackend) {
444 *outInfo = fGLInfo;
445 return true;
Greg Daniel94403452017-04-18 15:52:36 -0400446 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400447 return false;
Greg Daniel94403452017-04-18 15:52:36 -0400448}
449
Greg Daniel323fbcf2018-04-10 13:46:30 -0400450bool GrBackendRenderTarget::getMockRenderTargetInfo(GrMockRenderTargetInfo* outInfo) const {
451 if (this->isValid() && kMock_GrBackend == fBackend) {
452 *outInfo = fMockInfo;
453 return true;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500454 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400455 return false;
Brian Salomon0c51eea2018-03-09 17:02:09 -0500456}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400457
458#if GR_TEST_UTILS
459bool GrBackendRenderTarget::TestingOnly_Equals(const GrBackendRenderTarget& r0,
460 const GrBackendRenderTarget& r1) {
461 if (!r0.isValid() || !r1.isValid()) {
462 return false; // two invalid backend rendertargets are not considered equal
463 }
464
465 if (r0.fWidth != r1.fWidth ||
466 r0.fHeight != r1.fHeight ||
467 r0.fSampleCnt != r1.fSampleCnt ||
468 r0.fStencilBits != r1.fStencilBits ||
469 r0.fConfig != r1.fConfig ||
470 r0.fBackend != r1.fBackend) {
471 return false;
472 }
473
474 switch (r0.fBackend) {
475 case kOpenGL_GrBackend:
476 return r0.fGLInfo == r1.fGLInfo;
477 case kMock_GrBackend:
478 return r0.fMockInfo == r1.fMockInfo;
479 case kVulkan_GrBackend:
480#ifdef SK_VULKAN
481 return r0.fVkInfo == r1.fVkInfo;
482#else
483 // fall through
484#endif
485 case kMetal_GrBackend: // fall through
486 default:
487 return false;
488 }
489
490 SkASSERT(0);
491 return false;
492}
493#endif