Flutter Engine
The Flutter Engine
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PipelineData.h
Go to the documentation of this file.
1/*
2 * Copyright 2022 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_PipelineData_DEFINED
9#define skgpu_graphite_PipelineData_DEFINED
10
11#include <vector>
12#include "include/core/SkM44.h"
16#include "include/core/SkSpan.h"
20#include "src/core/SkTHash.h"
26
27class SkArenaAlloc;
28
29namespace skgpu::graphite {
30
31class Uniform;
32
34public:
36
38 UniformDataBlock() = default;
39
40 const char* data() const { return fData.data(); }
41 size_t size() const { return fData.size(); }
42
43 uint32_t hash() const;
44
45 bool operator==(const UniformDataBlock& that) const {
46 return fData.size() == that.fData.size() &&
47 !memcmp(fData.data(), that.fData.data(), fData.size());
48 }
49 bool operator!=(const UniformDataBlock& that) const { return !(*this == that); }
50
51private:
53};
54
56public:
57 using SampledTexture = std::pair<sk_sp<TextureProxy>, SamplerDesc>;
58
60 TextureDataBlock() = default;
61
62 bool empty() const { return fTextureData.empty(); }
63 int numTextures() const { return SkTo<int>(fTextureData.size()); }
64 const SampledTexture& texture(int index) const { return fTextureData[index]; }
65
66 bool operator==(const TextureDataBlock&) const;
67 bool operator!=(const TextureDataBlock& other) const { return !(*this == other); }
68 uint32_t hash() const;
69
70 void add(sk_sp<TextureProxy> proxy, const SamplerDesc& samplerDesc) {
71 fTextureData.push_back({std::move(proxy), samplerDesc});
72 }
73
74 void reset() {
75 fTextureData.clear();
76 }
77
78private:
79 // TODO: Move this into a SkSpan that's managed by the gatherer or copied into the arena.
80 std::vector<SampledTexture> fTextureData;
81};
82
83// The PipelineDataGatherer is just used to collect information for a given PaintParams object.
84// The UniformData is added to a cache and uniquified. Only that unique ID is passed around.
85// The TextureData is also added to a cache and uniquified. Only that ID is passed around.
86
87// TODO: The current plan for fixing uniform padding is for the PipelineDataGatherer to hold a
88// persistent uniformManager. A stretch goal for this system would be for this combination
89// to accumulate all the uniforms and then rearrange them to minimize padding. This would,
90// obviously, vastly complicate uniform accumulation.
92public:
94
95 void resetWithNewLayout(Layout layout);
96
97 // Check that the gatherer has been reset to its initial state prior to collecting new data.
98 SkDEBUGCODE(void checkReset();)
99
101 fTextureDataBlock.add(std::move(proxy), samplerDesc);
102 }
103 bool hasTextures() const { return !fTextureDataBlock.empty(); }
104
105 const TextureDataBlock& textureDataBlock() { return fTextureDataBlock; }
106
107 // Mimic the type-safe API available in UniformManager
108 template <typename T> void write(const T& t) { fUniformManager.write(t); }
109 template <typename T> void writeHalf(const T& t) { fUniformManager.writeHalf(t); }
110 template <typename T> void writeArray(SkSpan<const T> t) { fUniformManager.writeArray(t); }
111 template <typename T> void writeHalfArray(SkSpan<const T> t) {
112 fUniformManager.writeHalfArray(t);
113 }
114
115 void write(const Uniform& u, const void* data) { fUniformManager.write(u, data); }
116
117 void writePaintColor(const SkPMColor4f& color) { fUniformManager.writePaintColor(color); }
118
119 bool hasUniforms() const { return fUniformManager.size(); }
120
121 bool hasGradientBufferData() const { return !fGradientStorage.empty(); }
122
123 SkSpan<const float> gradientBufferData() const { return fGradientStorage; }
124
125 // Returns the uniform data written so far. Will automatically pad the end of the data as needed
126 // to the overall required alignment, and so should only be called when all writing is done.
128
129 // Checks if data already exists for the requested gradient shader, and returns a nullptr
130 // and the offset the data begins at. If it doesn't exist, it allocates the data for the
131 // required number of stops and caches the start index, returning the data pointer
132 // and index offset the data will begin at.
133 std::pair<float*, int> allocateGradientData(int numStops, const SkGradientBaseShader* shader) {
134 int* existingOfffset = fGradientOffsetCache.find(shader);
135 if (existingOfffset) {
136 return std::make_pair(nullptr, *existingOfffset);
137 }
138
139 auto dataPair = this->allocateFloatData(numStops * 5);
140 fGradientOffsetCache.set(shader, dataPair.second);
141
142 return dataPair;
143 }
144
145private:
146 // Allocates the data for the requested number of bytes and returns the
147 // pointer and buffer index offset the data will begin at.
148 std::pair<float*, int> allocateFloatData(int size) {
149 int lastSize = fGradientStorage.size();
150 fGradientStorage.resize(lastSize + size);
151 float* startPtr = fGradientStorage.begin() + lastSize;
152
153 return std::make_pair(startPtr, lastSize);
154 }
155
156#ifdef SK_DEBUG
157 friend class UniformExpectationsValidator;
158
159 void setExpectedUniforms(SkSpan<const Uniform> expectedUniforms);
160 void doneWithExpectedUniforms() { fUniformManager.doneWithExpectedUniforms(); }
161#endif // SK_DEBUG
162
163 TextureDataBlock fTextureDataBlock;
164 UniformManager fUniformManager;
165
166 SkTDArray<float> fGradientStorage;
167 // Storing the address of the shader as a proxy for comparing
168 // the colors and offsets arrays to keep lookup fast.
170};
171
172#ifdef SK_DEBUG
173class UniformExpectationsValidator {
174public:
175 UniformExpectationsValidator(PipelineDataGatherer *gatherer,
176 SkSpan<const Uniform> expectedUniforms);
177
178 ~UniformExpectationsValidator() {
179 fGatherer->doneWithExpectedUniforms();
180 }
181
182private:
183 PipelineDataGatherer *fGatherer;
184
185 UniformExpectationsValidator(UniformExpectationsValidator &&) = delete;
186 UniformExpectationsValidator(const UniformExpectationsValidator &) = delete;
187 UniformExpectationsValidator &operator=(UniformExpectationsValidator &&) = delete;
188 UniformExpectationsValidator &operator=(const UniformExpectationsValidator &) = delete;
189};
190#endif // SK_DEBUG
191
192} // namespace skgpu::graphite
193
194#endif // skgpu_graphite_PipelineData_DEFINED
constexpr T * data() const
Definition: SkSpan_impl.h:94
constexpr size_t size() const
Definition: SkSpan_impl.h:95
int size() const
Definition: SkTDArray.h:138
bool empty() const
Definition: SkTDArray.h:135
T * begin()
Definition: SkTDArray.h:150
void resize(int count)
Definition: SkTDArray.h:183
std::pair< float *, int > allocateGradientData(int numStops, const SkGradientBaseShader *shader)
Definition: PipelineData.h:133
void write(const Uniform &u, const void *data)
Definition: PipelineData.h:115
SkSpan< const float > gradientBufferData() const
Definition: PipelineData.h:123
void writeHalfArray(SkSpan< const T > t)
Definition: PipelineData.h:111
UniformDataBlock finishUniformDataBlock()
Definition: PipelineData.h:127
void writeArray(SkSpan< const T > t)
Definition: PipelineData.h:110
const TextureDataBlock & textureDataBlock()
Definition: PipelineData.h:105
SkDEBUGCODE(void checkReset();) void add(sk_sp< TextureProxy > proxy
void writePaintColor(const SkPMColor4f &color)
Definition: PipelineData.h:117
std::pair< sk_sp< TextureProxy >, SamplerDesc > SampledTexture
Definition: PipelineData.h:57
bool operator==(const TextureDataBlock &) const
static TextureDataBlock * Make(const TextureDataBlock &, SkArenaAlloc *)
void add(sk_sp< TextureProxy > proxy, const SamplerDesc &samplerDesc)
Definition: PipelineData.h:70
bool operator!=(const TextureDataBlock &other) const
Definition: PipelineData.h:67
const SampledTexture & texture(int index) const
Definition: PipelineData.h:64
UniformDataBlock(SkSpan< const char > data)
Definition: PipelineData.h:37
static UniformDataBlock * Make(const UniformDataBlock &, SkArenaAlloc *)
bool operator!=(const UniformDataBlock &that) const
Definition: PipelineData.h:49
bool operator==(const UniformDataBlock &that) const
Definition: PipelineData.h:45
void writeArray(SkSpan< const SkV4 > v)
UniformDataBlock finishUniformDataBlock()
void writeHalfArray(SkSpan< const SkPMColor4f > c)
void writePaintColor(const SkPMColor4f &color)
V * find(const K &key) const
Definition: SkTHash.h:494
V * set(K key, V val)
Definition: SkTHash.h:487
DlColor color
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
#define T
Definition: precompiler.cc:65
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63