Flutter Engine
The Flutter Engine
GrBackendSurface.cpp
Go to the documentation of this file.
1/*
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
9
11#include "include/gpu/GrTypes.h"
12#include "include/gpu/MutableTextureState.h" // IWYU pragma: keep
17
18#ifdef SK_DIRECT3D
22#endif
23
24#include <algorithm>
25#include <new>
26
29
31 : fBackend(that.fBackend)
32 , fValid(that.fValid)
33 , fTextureType(that.fTextureType) {
34 if (!fValid) {
35 return;
36 }
37
38 switch (fBackend) {
42 fFormatData.reset();
43 that.fFormatData->copyTo(fFormatData);
44 break; // fFormatData is sufficient
45#ifdef SK_DIRECT3D
47 fDxgiFormat = that.fDxgiFormat;
48 break;
49#endif
51 fMock = that.fMock;
52 break;
53 default:
54 SK_ABORT("Unknown GrBackend");
55 }
56}
57
59 if (this != &that) {
60 this->~GrBackendFormat();
61 new (this) GrBackendFormat(that);
62 }
63 return *this;
64}
65
66#ifdef SK_DIRECT3D
67GrBackendFormat::GrBackendFormat(DXGI_FORMAT dxgiFormat)
68 : fBackend(GrBackendApi::kDirect3D)
69 , fValid(true)
70 , fDxgiFormat(dxgiFormat)
71 , fTextureType(GrTextureType::k2D) {
72}
73
74bool GrBackendFormat::asDxgiFormat(DXGI_FORMAT* dxgiFormat) const {
75 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
76 *dxgiFormat = fDxgiFormat;
77 return true;
78 }
79 return false;
80}
81#endif
82
84 bool isStencilFormat)
85 : fBackend(GrBackendApi::kMock)
86 , fValid(true)
87 , fTextureType(GrTextureType::k2D) {
88 fMock.fColorType = colorType;
89 fMock.fCompressionType = compression;
90 fMock.fIsStencilFormat = isStencilFormat;
91 SkASSERT(this->validateMock());
92}
93
95 if (!this->isValid()) {
96 return 0;
97 }
98 switch (fBackend) {
102 return fFormatData->channelMask();
103#ifdef SK_DIRECT3D
105 return GrDxgiFormatChannels(fDxgiFormat);
106#endif
108 return GrColorTypeChannelFlags(fMock.fColorType);
109
110 default:
111 return 0;
112 }
113}
114
116 if (!this->isValid()) {
118 }
119 switch (fBackend) {
123 return fFormatData->desc();
124#ifdef SK_DIRECT3D
126 return GrDxgiFormatDesc(fDxgiFormat);
127#endif
129 return GrGetColorTypeDesc(fMock.fColorType);
130
131 default:
133 }
134}
135
136#ifdef SK_DEBUG
137bool GrBackendFormat::validateMock() const {
138 int trueStates = 0;
139 if (fMock.fCompressionType != SkTextureCompressionType::kNone) {
140 trueStates++;
141 }
142 if (fMock.fColorType != GrColorType::kUnknown) {
143 trueStates++;
144 }
145 if (fMock.fIsStencilFormat) {
146 trueStates++;
147 }
148 return trueStates == 1;
149}
150#endif
151
153 if (this->isValid() && GrBackendApi::kMock == fBackend) {
154 SkASSERT(this->validateMock());
155 return fMock.fColorType;
156 }
157
159}
160
162 if (this->isValid() && GrBackendApi::kMock == fBackend) {
163 SkASSERT(this->validateMock());
164 return fMock.fCompressionType;
165 }
166
168}
169
171 if (this->isValid() && GrBackendApi::kMock == fBackend) {
172 SkASSERT(this->validateMock());
173 return fMock.fIsStencilFormat;
174 }
175
176 return false;
177}
178
180 GrBackendFormat copy = *this;
181 // TODO(b/293490566): Remove this kVulkan check once all backends are using fFormatData.
182 if (fBackend==GrBackendApi::kVulkan) {
183 copy.fFormatData->makeTexture2D();
184 }
185 copy.fTextureType = GrTextureType::k2D;
186 return copy;
187}
188
190 SkTextureCompressionType compression,
191 bool isStencilFormat) {
192 return GrBackendFormat(colorType, compression, isStencilFormat);
193}
194
196 // Invalid GrBackendFormats are never equal to anything.
197 if (!fValid || !that.fValid) {
198 return false;
199 }
200
201 if (fBackend != that.fBackend) {
202 return false;
203 }
204
205 switch (fBackend) {
209 return fFormatData->equal(that.fFormatData.get());
211 return fMock.fColorType == that.fMock.fColorType &&
212 fMock.fCompressionType == that.fMock.fCompressionType;
213#ifdef SK_DIRECT3D
215 return fDxgiFormat == that.fDxgiFormat;
216#endif
217 default:
218 SK_ABORT("Unknown GrBackend");
219 }
220 return false;
221}
222
223#if defined(SK_DEBUG) || defined(GR_TEST_UTILS)
225
226SkString GrBackendFormat::toStr() const {
227 SkString str;
228
229 if (!fValid) {
230 str.append("invalid");
231 return str;
232 }
233
234 str.appendf("%s-", GrBackendApiToStr(fBackend));
235
236 switch (fBackend) {
240 str.append(fFormatData->toString());
241 break;
243#ifdef SK_DIRECT3D
244 str.append(GrDxgiFormatToStr(fDxgiFormat));
245#endif
246 break;
248 str.append(GrColorTypeToStr(fMock.fColorType));
249 str.appendf("-");
250 str.append(skgpu::CompressionTypeToStr(fMock.fCompressionType));
251 break;
253 break;
254 }
255
256 return str;
257}
258#endif
259
260///////////////////////////////////////////////////////////////////////////////////////////////////
262
263#ifdef SK_DIRECT3D
265 int height,
266 const GrD3DTextureResourceInfo& d3dInfo,
267 std::string_view label)
269 height,
270 d3dInfo,
272 static_cast<D3D12_RESOURCE_STATES>(d3dInfo.fResourceState))),
273 label) {}
274
276 int height,
277 const GrD3DTextureResourceInfo& d3dInfo,
279 std::string_view label)
280 : fIsValid(true)
281 , fWidth(width)
282 , fHeight(height)
283 , fLabel(label)
284 , fMipmapped(skgpu::Mipmapped(d3dInfo.fLevelCount > 1))
285 , fBackend(GrBackendApi::kDirect3D)
286 , fTextureType(GrTextureType::k2D)
287 , fD3DInfo(d3dInfo, state.release()) {}
288#endif
289
291 int height,
292 skgpu::Mipmapped mipmapped,
293 const GrMockTextureInfo& mockInfo,
294 std::string_view label)
295 : fIsValid(true)
296 , fWidth(width)
297 , fHeight(height)
298 , fLabel(label)
299 , fMipmapped(mipmapped)
300 , fBackend(GrBackendApi::kMock)
301 , fTextureType(GrTextureType::k2D)
302 , fMockInfo(mockInfo) {}
303
305 this->cleanup();
306}
307
308void GrBackendTexture::cleanup() {
309 fTextureData.reset();
310#ifdef SK_DIRECT3D
311 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
312 fD3DInfo.cleanup();
313 }
314#endif
315}
316
318 *this = that;
319}
320
322 if (this == &that) {
323 return *this;
324 }
325
326 if (!that.isValid()) {
327 this->cleanup();
328 fIsValid = false;
329 return *this;
330 } else if (fIsValid && this->fBackend != that.fBackend) {
331 this->cleanup();
332 fIsValid = false;
333 }
334 fWidth = that.fWidth;
335 fHeight = that.fHeight;
336 fMipmapped = that.fMipmapped;
337 fBackend = that.fBackend;
338 fTextureType = that.fTextureType;
339
340 switch (that.fBackend) {
344 fTextureData.reset();
345 that.fTextureData->copyTo(fTextureData);
346 break;
347#ifdef SK_DIRECT3D
349 fD3DInfo.assign(that.fD3DInfo, this->isValid());
350 break;
351#endif
353 fMockInfo = that.fMockInfo;
354 break;
355 default:
356 SK_ABORT("Unknown GrBackend");
357 }
358 fIsValid = true;
359 return *this;
360}
361
362sk_sp<skgpu::MutableTextureState> GrBackendTexture::getMutableState() const {
363 return fTextureData->getMutableState();
364}
365
366#ifdef SK_DIRECT3D
367bool GrBackendTexture::getD3DTextureResourceInfo(GrD3DTextureResourceInfo* outInfo) const {
368 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
369 *outInfo = fD3DInfo.snapTextureResourceInfo();
370 return true;
371 }
372 return false;
373}
374
375void GrBackendTexture::setD3DResourceState(GrD3DResourceStateEnum state) {
376 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
377 fD3DInfo.setResourceState(state);
378 }
379}
380
381sk_sp<GrD3DResourceState> GrBackendTexture::getGrD3DResourceState() const {
382 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
383 return fD3DInfo.getGrD3DResourceState();
384 }
385 return nullptr;
386}
387#endif
388
390 if (this->isValid() && GrBackendApi::kMock == fBackend) {
391 *outInfo = fMockInfo;
392 return true;
393 }
394 return false;
395}
396
398 fTextureData->setMutableState(state);
399}
400
402 if (!this->isValid()) {
403 return false;
404 }
405 if (this->backend() == GrBackendApi::kOpenGL || this->backend() == GrBackendApi::kVulkan) {
406 return fTextureData->isProtected();
407 }
408 if (this->backend() == GrBackendApi::kMock) {
409 return fMockInfo.isProtected();
410 }
411
412 return false;
413}
414
416 if (!this->isValid() || !that.isValid()) {
417 return false;
418 }
419 if (fBackend != that.fBackend) {
420 return false;
421 }
422 switch (fBackend) {
426 return fTextureData->isSameTexture(that.fTextureData.get());
427#ifdef SK_DIRECT3D
429 return fD3DInfo.snapTextureResourceInfo().fResource ==
430 that.fD3DInfo.snapTextureResourceInfo().fResource;
431#endif
433 return fMockInfo.id() == that.fMockInfo.id();
434 default:
435 return false;
436 }
437}
438
440 if (!this->isValid()) {
441 return GrBackendFormat();
442 }
443 switch (fBackend) {
447 return fTextureData->getBackendFormat();
448#ifdef SK_DIRECT3D
450 auto d3dInfo = fD3DInfo.snapTextureResourceInfo();
451 return GrBackendFormat::MakeDxgi(d3dInfo.fFormat);
452 }
453#endif
456 default:
457 return GrBackendFormat();
458 }
459}
460
461#if defined(GR_TEST_UTILS)
462bool GrBackendTexture::TestingOnly_Equals(const GrBackendTexture& t0, const GrBackendTexture& t1) {
463 if (!t0.isValid() || !t1.isValid()) {
464 return false; // two invalid backend textures are not considered equal
465 }
466
467 if (t0.fWidth != t1.fWidth ||
468 t0.fHeight != t1.fHeight ||
469 t0.fMipmapped != t1.fMipmapped ||
470 t0.fBackend != t1.fBackend) {
471 return false;
472 }
473
474 switch (t0.fBackend) {
478 return t0.fTextureData->equal(t1.fTextureData.get());
480 return t0.fMockInfo == t1.fMockInfo;
481#ifdef SK_DIRECT3D
483 return t0.fD3DInfo == t1.fD3DInfo;
484#endif
485 default:
486 return false;
487 }
488}
489#endif
490
491////////////////////////////////////////////////////////////////////////////////////////////////////
492
494
495#ifdef SK_DIRECT3D
497 const GrD3DTextureResourceInfo& d3dInfo)
499 width, height, d3dInfo,
501 static_cast<D3D12_RESOURCE_STATES>(d3dInfo.fResourceState)))) {}
502
504 int height,
505 const GrD3DTextureResourceInfo& d3dInfo,
507 : fIsValid(true)
508 , fWidth(width)
509 , fHeight(height)
510 , fSampleCnt(std::max(1U, d3dInfo.fSampleCount))
511 , fStencilBits(0)
512 , fBackend(GrBackendApi::kDirect3D)
513 , fD3DInfo(d3dInfo, state.release()) {}
514#endif
515
517 int height,
518 int sampleCnt,
519 int stencilBits,
520 const GrMockRenderTargetInfo& mockInfo)
521 : fIsValid(true)
522 , fWidth(width)
523 , fHeight(height)
524 , fSampleCnt(std::max(1, sampleCnt))
525 , fStencilBits(stencilBits)
526 , fBackend(GrBackendApi::kMock)
527 , fMockInfo(mockInfo) {}
528
530 this->cleanup();
531}
532
533void GrBackendRenderTarget::cleanup() {
534 fRTData.reset();
535#ifdef SK_DIRECT3D
536 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
537 fD3DInfo.cleanup();
538 }
539#endif
540}
541
543 *this = that;
544}
545
547 if (this == &that) {
548 return *this;
549 }
550
551 if (!that.isValid()) {
552 this->cleanup();
553 fIsValid = false;
554 return *this;
555 } else if (fIsValid && this->fBackend != that.fBackend) {
556 this->cleanup();
557 fIsValid = false;
558 }
559 fWidth = that.fWidth;
560 fHeight = that.fHeight;
561 fSampleCnt = that.fSampleCnt;
562 fStencilBits = that.fStencilBits;
563 fBackend = that.fBackend;
564
565 switch (that.fBackend) {
569 fRTData.reset();
570 that.fRTData->copyTo(fRTData);
571 break;
572#ifdef SK_DIRECT3D
574 fD3DInfo.assign(that.fD3DInfo, this->isValid());
575 break;
576#endif
578 fMockInfo = that.fMockInfo;
579 break;
580 default:
581 SK_ABORT("Unknown GrBackend");
582 }
583 fIsValid = that.fIsValid;
584 return *this;
585}
586
587sk_sp<skgpu::MutableTextureState> GrBackendRenderTarget::getMutableState() const {
588 return fRTData->getMutableState();
589}
590
591#ifdef SK_DIRECT3D
592bool GrBackendRenderTarget::getD3DTextureResourceInfo(GrD3DTextureResourceInfo* outInfo) const {
593 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
594 *outInfo = fD3DInfo.snapTextureResourceInfo();
595 return true;
596 }
597 return false;
598}
599
600void GrBackendRenderTarget::setD3DResourceState(GrD3DResourceStateEnum state) {
601 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
602 fD3DInfo.setResourceState(state);
603 }
604}
605
606sk_sp<GrD3DResourceState> GrBackendRenderTarget::getGrD3DResourceState() const {
607 if (this->isValid() && GrBackendApi::kDirect3D == fBackend) {
608 return fD3DInfo.getGrD3DResourceState();
609 }
610 return nullptr;
611}
612#endif
613
615 if (!this->isValid()) {
616 return GrBackendFormat();
617 }
618 switch (fBackend) {
622 return fRTData->getBackendFormat();
623#ifdef SK_DIRECT3D
625 auto info = fD3DInfo.snapTextureResourceInfo();
626 return GrBackendFormat::MakeDxgi(info.fFormat);
627 }
628#endif
631 default:
632 return GrBackendFormat();
633 }
634}
635
637 if (this->isValid() && GrBackendApi::kMock == fBackend) {
638 *outInfo = fMockInfo;
639 return true;
640 }
641 return false;
642}
643
645 fRTData->setMutableState(state);
646}
647
649 if (!this->isValid()) {
650 return false;
651 }
652 if (this->backend() == GrBackendApi::kOpenGL || this->backend() == GrBackendApi::kVulkan) {
653 return fRTData->isProtected();
654 }
655 if (this->backend() == GrBackendApi::kMock) {
656 return fMockInfo.isProtected();
657 }
658
659 return false;
660}
661
662#if defined(GR_TEST_UTILS)
663bool GrBackendRenderTarget::TestingOnly_Equals(const GrBackendRenderTarget& r0,
664 const GrBackendRenderTarget& r1) {
665 if (!r0.isValid() || !r1.isValid()) {
666 return false; // two invalid backend rendertargets are not considered equal
667 }
668
669 if (r0.fWidth != r1.fWidth ||
670 r0.fHeight != r1.fHeight ||
671 r0.fSampleCnt != r1.fSampleCnt ||
672 r0.fStencilBits != r1.fStencilBits ||
673 r0.fBackend != r1.fBackend) {
674 return false;
675 }
676
677 switch (r0.fBackend) {
681 return r0.fRTData->equal(r1.fRTData.get());
683 return r0.fMockInfo == r1.fMockInfo;
684#ifdef SK_DIRECT3D
686 return r0.fD3DInfo == r1.fD3DInfo;
687#endif
688 default:
689 return false;
690 }
691
692 SkASSERT(0);
693 return false;
694}
695#endif
696
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
int GrD3DResourceStateEnum
static constexpr uint32_t GrDxgiFormatChannels(DXGI_FORMAT format)
Definition: GrD3DUtil.h:37
static constexpr GrColorFormatDesc GrDxgiFormatDesc(DXGI_FORMAT format)
Definition: GrD3DUtil.h:61
static constexpr uint32_t GrColorTypeChannelFlags(GrColorType ct)
Definition: GrTypesPriv.h:661
static constexpr GrColorFormatDesc GrGetColorTypeDesc(GrColorType ct)
Definition: GrTypesPriv.h:794
GrTextureType
Definition: GrTypesPriv.h:268
GrColorType
Definition: GrTypesPriv.h:540
GrBackendApi
Definition: GrTypes.h:95
#define SK_ABORT(message,...)
Definition: SkAssert.h:70
#define SkASSERT(cond)
Definition: SkAssert.h:116
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
static void copy(void *dst, const uint8_t *src, int width, int bpp, int deltaSrc, int offset, const SkPMColor ctable[])
Definition: SkSwizzler.cpp:31
SkTextureCompressionType
virtual GrColorFormatDesc desc() const =0
virtual void copyTo(AnyFormatData &) const =0
virtual std::string toString() const =0
virtual bool equal(const GrBackendFormatData *that) const =0
virtual uint32_t channelMask() const =0
GrColorType fColorType
bool isMockStencilFormat() const
bool operator==(const GrBackendFormat &that) const
GrColorFormatDesc desc() const
GrBackendFormat & operator=(const GrBackendFormat &)
SkTextureCompressionType fCompressionType
static GrBackendFormat MakeMock(GrColorType colorType, SkTextureCompressionType compression, bool isStencilFormat=false)
bool isValid() const
SkTextureCompressionType asMockCompressionType() const
GrColorType asMockColorType() const
uint32_t channelMask() const
struct GrBackendFormat::@283::@285 fMock
GrBackendFormat makeTexture2D() const
virtual void setMutableState(const skgpu::MutableTextureState &)
virtual void copyTo(AnyRenderTargetData &) const =0
virtual bool isProtected() const =0
virtual sk_sp< skgpu::MutableTextureState > getMutableState() const
virtual GrBackendFormat getBackendFormat() const =0
virtual bool equal(const GrBackendRenderTargetData *that) const =0
void setMutableState(const skgpu::MutableTextureState &)
bool getMockRenderTargetInfo(GrMockRenderTargetInfo *) const
GrBackendRenderTarget & operator=(const GrBackendRenderTarget &)
GrMockRenderTargetInfo fMockInfo
GrBackendFormat getBackendFormat() const
GrBackendApi backend() const
virtual bool equal(const GrBackendTextureData *that) const =0
virtual bool isSameTexture(const GrBackendTextureData *) const =0
virtual bool isProtected() const =0
virtual sk_sp< skgpu::MutableTextureState > getMutableState() const
virtual void copyTo(AnyTextureData &) const =0
virtual GrBackendFormat getBackendFormat() const =0
virtual void setMutableState(const skgpu::MutableTextureState &)
GrBackendFormat getBackendFormat() const
bool isSameTexture(const GrBackendTexture &)
GrBackendTexture & operator=(const GrBackendTexture &that)
bool getMockTextureInfo(GrMockTextureInfo *) const
bool isValid() const
bool isProtected() const
GrMockTextureInfo fMockInfo
void setMutableState(const skgpu::MutableTextureState &)
GrBackendApi backend() const
static constexpr GrColorFormatDesc MakeInvalid()
Definition: GrTypesPriv.h:756
const Base * get() const
Definition: SkAnySubclass.h:55
void append(const char text[])
Definition: SkString.h:203
void void void appendf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:550
AtkStateType state
static float max(float r, float g, float b)
Definition: hsl.cpp:49
Definition: copy.py:1
Definition: GpuTools.h:21
Mipmapped
Definition: GpuTypes.h:53
static constexpr const char * CompressionTypeToStr(SkTextureCompressionType compression)
Definition: GpuTypesPriv.h:41
Definition: ref_ptr.h:256
int32_t height
int32_t width
GrBackendFormat getBackendFormat() const
Definition: GrMockTypes.cpp:15
bool isProtected() const
Definition: GrMockTypes.h:93
bool isProtected() const
Definition: GrMockTypes.h:59
GrBackendFormat getBackendFormat() const
Definition: GrMockTypes.cpp:19
int id() const
Definition: GrMockTypes.h:56