| 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 |
#ifndef skgpu_graphite_TextureInfo_DEFINED |
| 9 |
#define skgpu_graphite_TextureInfo_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkString.h" |
| 12 |
#include "include/gpu/graphite/GraphiteTypes.h" |
| 13 |
#include "include/private/base/SkAPI.h" |
| 14 |
#include "include/private/base/SkAnySubclass.h" |
| 15 |
|
| 16 |
struct SkISize; |
| 17 |
|
| 18 |
namespace skgpu::graphite { |
| 19 |
|
| 20 |
enum class TextureFormat : uint8_t; |
| 21 |
|
| 22 |
/** |
| 23 |
* TextureInfo is a backend-agnostic wrapper around the properties of a texture, sans dimensions. |
| 24 |
* It is designed this way to be compilable w/o bringing in a specific backend's build files, and |
| 25 |
* without requiring heap allocations of virtual types. |
| 26 |
*/ |
| 27 |
class SK_API TextureInfo { |
| 28 |
private: |
| 29 |
class Data; |
| 30 |
friend class MtlTextureInfo; |
| 31 |
friend class DawnTextureInfo; |
| 32 |
friend class VulkanTextureInfo; |
| 33 |
|
| 34 |
// Size is the largest of the Data subclasses assuming a 64-bit compiler. |
| 35 |
inline constexpr static size_t kMaxSubclassSize = 112; |
| 36 |
using AnyTextureInfoData = SkAnySubclass<Data, kMaxSubclassSize>; |
| 37 |
|
| 38 |
// Base properties for all backend-specific properties. Clients managing textures directly |
| 39 |
// should use the public subclasses of Data directly, e.g. MtlTextureInfo/DawnTextureInfo. |
| 40 |
// |
| 41 |
// Each backend subclass must expose to TextureInfo[Priv]: |
| 42 |
// static constexpr BackendApi kBackend; |
| 43 |
// Protected isProtected() const; |
| 44 |
// TextureFormat viewFormat() const; |
| 45 |
class Data { |
| 46 |
public: |
| 47 |
virtual ~Data() = default; |
| 48 |
|
| 49 |
Data(SampleCount sampleCount, skgpu::Mipmapped mipmapped) |
| 50 |
: fSampleCount(sampleCount) |
| 51 |
, fMipmapped(mipmapped) {} |
| 52 |
|
| 53 |
Data() = default; |
| 54 |
Data(const Data&) = default; |
| 55 |
|
| 56 |
Data& operator=(const Data&) = default; |
| 57 |
|
| 58 |
// NOTE: These fields are accessible via the backend-specific subclasses. |
| 59 |
SampleCount fSampleCount = SampleCount::k1; |
| 60 |
Mipmapped fMipmapped = Mipmapped::kNo; |
| 61 |
|
| 62 |
private: |
| 63 |
friend class TextureInfo; |
| 64 |
friend class TextureInfoPriv; |
| 65 |
|
| 66 |
virtual SkString toBackendString() const = 0; |
| 67 |
|
| 68 |
virtual void copyTo(AnyTextureInfoData&) const = 0; |
| 69 |
// Passed in TextureInfo will have data of the same backend type and subclass, and |
| 70 |
// base properties of Data have already been checked for equality/compatibility. |
| 71 |
virtual bool isCompatible(const TextureInfo& that, bool requireExact) const = 0; |
| 72 |
}; |
| 73 |
|
| 74 |
public: |
| 75 |
TextureInfo() = default; |
| 76 |
~TextureInfo() = default; |
| 77 |
|
| 78 |
TextureInfo(const TextureInfo&); |
| 79 |
TextureInfo& operator=(const TextureInfo&); |
| 80 |
|
| 81 |
bool operator==(const TextureInfo& that) const { |
| 82 |
return this->isCompatible(that, /*requireExact=*/true); |
| 83 |
} |
| 84 |
bool operator!=(const TextureInfo& that) const { return !(*this == that); } |
| 85 |
|
| 86 |
bool isValid() const { return fData.has_value(); } |
| 87 |
BackendApi backend() const { |
| 88 |
SkASSERT(fData.has_value() || fBackend == BackendApi::kUnsupported); |
| 89 |
return fBackend; |
| 90 |
} |
| 91 |
|
| 92 |
Protected isProtected() const { return fProtected; } |
| 93 |
SampleCount sampleCount() const { |
| 94 |
return fData.has_value() ? fData->fSampleCount : SampleCount::k1; |
| 95 |
} |
| 96 |
Mipmapped mipmapped() const { |
| 97 |
return fData.has_value() ? fData->fMipmapped : Mipmapped::kNo; |
| 98 |
} |
| 99 |
|
| 100 |
// Return true if `that` describes a texture that is compatible with this info and can validly |
| 101 |
// be used to fulfill a promise image that was created with this TextureInfo. |
| 102 |
bool canBeFulfilledBy(const TextureInfo& that) const { |
| 103 |
return this->isCompatible(that, /*requireExact=*/false); |
| 104 |
} |
| 105 |
|
| 106 |
// Return a string containing the full description of this TextureInfo. |
| 107 |
SkString toString() const; |
| 108 |
|
| 109 |
private: |
| 110 |
friend class TextureInfoPriv; |
| 111 |
|
| 112 |
template <typename BackendTextureData, |
| 113 |
std::enable_if_t<std::is_base_of_v<Data, BackendTextureData>, bool> = true> |
| 114 |
explicit TextureInfo(const BackendTextureData& data) |
| 115 |
: fBackend(BackendTextureData::kBackend) |
| 116 |
, fViewFormat(data.viewFormat()) |
| 117 |
, fProtected(data.isProtected()) { |
| 118 |
fData.emplace<BackendTextureData>(data); |
| 119 |
} |
| 120 |
|
| 121 |
bool isCompatible(const TextureInfo& that, bool requireExact) const; |
| 122 |
|
| 123 |
skgpu::BackendApi fBackend = BackendApi::kUnsupported; |
| 124 |
AnyTextureInfoData fData; |
| 125 |
|
| 126 |
// Derived properties from the backend data, cached to avoid a virtual function call |
| 127 |
TextureFormat fViewFormat; |
| 128 |
Protected fProtected = Protected::kNo; |
| 129 |
}; |
| 130 |
|
| 131 |
} // namespace skgpu::graphite |
| 132 |
|
| 133 |
#endif // skgpu_graphite_TextureInfo_DEFINED |