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