1 /*
2 * Copyright 2021 Google LLC
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 "include/gpu/graphite/TextureInfo.h"
9
10 #include "include/core/SkFourByteTag.h"
11 #include "include/core/SkStream.h"
12 #include "src/gpu/GpuTypesPriv.h"
13 #include "src/gpu/graphite/Caps.h"
14 #include "src/gpu/graphite/TextureFormat.h"
15 #include "src/gpu/graphite/TextureInfoPriv.h"
16
17 namespace skgpu::graphite {
18
TextureInfo(const TextureInfo & that)19 TextureInfo::TextureInfo(const TextureInfo& that)
20 : fBackend(that.fBackend)
21 , fViewFormat(that.fViewFormat)
22 , fProtected(that.fProtected) {
23 if (!that.fData.has_value()) {
24 SkASSERT(!fData.has_value());
25 return;
26 }
27
28 that.fData->copyTo(fData);
29 }
30
operator =(const TextureInfo & that)31 TextureInfo& TextureInfo::operator=(const TextureInfo& that) {
32 if (this != &that) {
33 this->~TextureInfo();
34 new (this) TextureInfo(that);
35 }
36 return *this;
37 }
38
isCompatible(const TextureInfo & that,bool requireExact) const39 bool TextureInfo::isCompatible(const TextureInfo& that, bool requireExact) const {
40 if (fBackend != that.fBackend) {
41 return false;
42 } else if (fBackend == skgpu::BackendApi::kUnsupported) {
43 SkASSERT(!fData.has_value() && !that.fData.has_value());
44 return true;
45 } else {
46 SkASSERT(fData.has_value() && that.fData.has_value());
47 return fData->fSampleCount == that.fData->fSampleCount &&
48 fData->fMipmapped == that.fData->fMipmapped &&
49 fData->isCompatible(that, requireExact);
50 }
51 }
52
toString() const53 SkString TextureInfo::toString() const {
54 if (!this->isValid()) {
55 return SkString("{}");
56 }
57
58 // Strip the leading "k" from the enum name when creating the TextureInfo string.
59 SkASSERT(BackendApiToStr(fBackend)[0] == 'k');
60 const char* backendName = BackendApiToStr(fBackend) + 1;
61
62 return SkStringPrintf("%s(viewFormat=%s,%s,bpp=%zu,sampleCount=%u,mipmapped=%d,protected=%d)",
63 backendName,
64 TextureFormatName(fViewFormat),
65 fData->toBackendString().c_str(),
66 TextureFormatBytesPerBlock(fViewFormat),
67 fData->fSampleCount,
68 static_cast<int>(fData->fMipmapped),
69 static_cast<int>(fProtected));
70 }
71
GetAttachmentLabel(const TextureInfo & info)72 SkString TextureInfoPriv::GetAttachmentLabel(const TextureInfo& info) {
73 if (!info.isValid()) {
74 return SkString("{}");
75 }
76
77 // Strip the leading "k" from the enum name when creating the TextureInfo string.
78 SkASSERT(BackendApiToStr(info.backend())[0] == 'k');
79 const char* backendName = BackendApiToStr(info.backend()) + 1;
80 // For renderpass attachments, the string will contain the view format and sample count only
81 return SkStringPrintf("%s(f=%s,s=%u)",
82 backendName,
83 TextureFormatName(ViewFormat(info)),
84 info.fData->fSampleCount);
85 }
86
87 static constexpr uint8_t kInfoSentinel = 0xFF;
88
GetInfoTag(const TextureInfo & info)89 uint32_t TextureInfoPriv::GetInfoTag(const TextureInfo& info) {
90 SkASSERT(info.isValid());
91 return (kInfoSentinel << 24) | // force value to be non-zero
92 (SkTo<uint8_t>(info.backend()) << 16) |
93 (SkTo<uint8_t>(info.mipmapped()) << 8) |
94 (SkTo<uint8_t>(info.numSamples()) << 0);
95 }
96
ParseInfoTag(uint32_t tag)97 std::tuple<BackendApi, Mipmapped, int> TextureInfoPriv::ParseInfoTag(uint32_t tag) {
98 static constexpr std::tuple<BackendApi, Mipmapped, int> kInvalid =
99 {BackendApi::kUnsupported, Mipmapped::kNo, 0};
100
101 uint8_t sentinel = 0xFF & (tag >> 24);
102 uint8_t backend = 0xFF & (tag >> 16);
103 uint8_t mipmapped = 0xFF & (tag >> 8);
104 uint8_t samples = 0xFF & (tag >> 0);
105 if (sentinel != kInfoSentinel) {
106 return kInvalid;
107 }
108 if (backend >= SkTo<uint8_t>(BackendApi::kUnsupported)) {
109 return kInvalid;
110 }
111 return {static_cast<BackendApi>(backend),
112 mipmapped ? Mipmapped::kYes : Mipmapped::kNo,
113 static_cast<int>(samples)};
114 }
115
116 } // namespace skgpu::graphite
117