| 1 |
From 5ecba665f5d1cf8b0fadd5986f7eaf579cca282e Mon Sep 17 00:00:00 2001 |
| 2 |
From: Michael Ludwig <[email protected]> |
| 3 |
Date: Fri, 22 May 2026 16:44:17 -0400 |
| 4 |
Subject: [PATCH] [graphite] Drop excessively large gradient draws |
| 5 |
|
| 6 |
This skips recording draws with more than 1M color stops, primarily as |
| 7 |
a way to avoid worrying about overflowing during intermediate |
| 8 |
calculations. We can increase it if necessary, but hopefully this is |
| 9 |
healthy enough no one is trying to make shaders this large. |
| 10 |
|
| 11 |
This also skips recording draws when the FSM has maxed out its |
| 12 |
allocatable size for a single buffer. Given how large that is, |
| 13 |
we shouldn't encounter it in the wild but this lets us fail semi |
| 14 |
gracefully. If needed, we can revisit by either flushing the entire |
| 15 |
Recorder when reaching a limit, or by allowing a recording to use |
| 16 |
multiple buffers |
| 17 |
|
| 18 |
Bug: https://issues.chromium.org/issues/515467789 |
| 19 |
Fixed: 515467789 |
| 20 |
Change-Id: Ie032f9ed35b6cf0316a18b32bb36e3ec3c047097 |
| 21 |
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1243936 |
| 22 |
Commit-Queue: Michael Ludwig <[email protected]> |
| 23 |
Reviewed-by: Robert Phillips <[email protected]> |
| 24 |
Reviewed-by: Thomas Smith <[email protected]> |
| 25 |
--- |
| 26 |
src/gpu/graphite/KeyHelpers.cpp | 14 ++++++++++++- |
| 27 |
src/gpu/graphite/PipelineData.h | 35 ++++++++++++++++++++++++++++++++- |
| 28 |
2 files changed, 47 insertions(+), 2 deletions(-) |
| 29 |
|
| 30 |
diff --git a/src/gpu/graphite/KeyHelpers.cpp b/src/gpu/graphite/KeyHelpers.cpp |
| 31 |
index 36ebed3e9e..1e94cc7667 100644 |
| 32 |
--- a/src/gpu/graphite/KeyHelpers.cpp |
| 33 |
+++ b/src/gpu/graphite/KeyHelpers.cpp |
| 34 |
@@ -289,6 +289,9 @@ void add_conical_gradient_uniform_data(const KeyContext& keyContext, |
| 35 |
|
| 36 |
// Writes the color and offset data directly in the gatherer gradient buffer and returns the |
| 37 |
// offset the data begins at in the buffer. |
| 38 |
+// |
| 39 |
+// Returns a negative offset to signal failure, in which case the paint key must be poisoned |
| 40 |
+// to drop the draw. |
| 41 |
static int write_color_and_offset_bufdata(int numStops, |
| 42 |
const SkPMColor4f* colors, |
| 43 |
const float* offsets, |
| 44 |
@@ -296,6 +299,7 @@ static int write_color_and_offset_bufdata(int numStops, |
| 45 |
FloatStorageManager* floatStorageManager) { |
| 46 |
auto [dstData, bufferOffset] = floatStorageManager->allocateGradientData(numStops, shader); |
| 47 |
if (dstData) { |
| 48 |
+ SkASSERT(bufferOffset >= 0); |
| 49 |
// Data doesn't already exist so we need to write it. |
| 50 |
// Writes all offset data, then color data. This way when binary searching through the |
| 51 |
// offsets, there is better cache locality. |
| 52 |
@@ -388,16 +392,24 @@ GradientShaderBlocks::GradientData::GradientData(SkShaderBase::GradientType type |
| 53 |
void GradientShaderBlocks::AddBlock(const KeyContext& keyContext, const GradientData& gradData) { |
| 54 |
int bufferOffset = 0; |
| 55 |
if (gradData.fNumStops > GradientData::kNumInternalStorageStops && keyContext.recorder()) { |
| 56 |
+ bool hasStorage; |
| 57 |
if (gradData.fUseStorageBuffer) { |
| 58 |
bufferOffset = write_color_and_offset_bufdata(gradData.fNumStops, |
| 59 |
gradData.fSrcColors, |
| 60 |
gradData.fSrcOffsets, |
| 61 |
gradData.fSrcShader, |
| 62 |
keyContext.floatStorageManager()); |
| 63 |
+ hasStorage = bufferOffset >= 0; |
| 64 |
} else { |
| 65 |
- SkASSERT(gradData.fColorsAndOffsetsProxy); |
| 66 |
keyContext.pipelineDataGatherer()->add(gradData.fColorsAndOffsetsProxy, |
| 67 |
{SkFilterMode::kNearest, SkTileMode::kClamp}); |
| 68 |
+ hasStorage = SkToBool(gradData.fColorsAndOffsetsProxy); |
| 69 |
+ } |
| 70 |
+ |
| 71 |
+ if (!hasStorage) { |
| 72 |
+ keyContext.paintParamsKeyBuilder()->addErrorBlock(); |
| 73 |
+ SKGPU_LOG_W("Couldn't upload large gradient color stop data"); |
| 74 |
+ return; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
diff --git a/src/gpu/graphite/PipelineData.h b/src/gpu/graphite/PipelineData.h |
| 79 |
index 236a3f177d..bc255428f7 100644 |
| 80 |
--- a/src/gpu/graphite/PipelineData.h |
| 81 |
+++ b/src/gpu/graphite/PipelineData.h |
| 82 |
@@ -462,6 +462,23 @@ private: |
| 83 |
* DrawPass. It de-duplicates gradient data by caching based on the SkGradientBaseShader pointer. |
| 84 |
*/ |
| 85 |
class FloatStorageManager : public SkRefCnt { |
| 86 |
+ // Size limit for individual gradients (anything larger will be dropped) |
| 87 |
+ static constexpr int kMaxGradientStops = 1024 * 1024; // ~5MB of data in the shader |
| 88 |
+ |
| 89 |
+ // Size limit for the max buffer size. If a new draw would exceed this limit, we drop the draw. |
| 90 |
+ // The float storage manager is used by all Devices in a Recorder and is reset at snap(), |
| 91 |
+ // requiring a global flush to otherwise get a new buffer (which is undesirable). Instead, we |
| 92 |
+ // assume that exceeding this limit happens in two situations: |
| 93 |
+ // 1. Adversarial content, at which point correctness is not critical. |
| 94 |
+ // 2. A truly bespoke application requiring 4GB of gradient color data should be having its |
| 95 |
+ // workload managed at the application level where it can snap Recordings. |
| 96 |
+ // |
| 97 |
+ // It is also likely that even if we accumulate this much CPU data, a GPU driver will fail to |
| 98 |
+ // create a buffer for us to copy to, causing the snap() to fail. |
| 99 |
+ static constexpr int kMaxStorageFloats = |
| 100 |
+ static_cast<int>(std::numeric_limits<uint32_t>::max() / sizeof(float)); |
| 101 |
+ static_assert(std::numeric_limits<uint32_t>::max() / sizeof(float) |
| 102 |
+ <= (uint32_t) std::numeric_limits<int>::max()); |
| 103 |
public: |
| 104 |
FloatStorageManager() = default; |
| 105 |
|
| 106 |
@@ -473,14 +490,26 @@ public: |
| 107 |
// Checks if data already exists for the requested gradient shader. If so, it returns |
| 108 |
// a nullptr and the existing offset. If not, it allocates space, caches the offset, |
| 109 |
// and returns a pointer to the start of the new data and the calculated offset. |
| 110 |
+ // |
| 111 |
+ // If it was not possible to store the gradient data, a nullptr and negative offset |
| 112 |
+ // are returned to signal the error state. |
| 113 |
std::pair<float*, int> allocateGradientData(int numStops, const SkGradientBaseShader* shader) { |
| 114 |
SkASSERT(!this->isFinalized()); |
| 115 |
+ if (numStops > kMaxGradientStops) { |
| 116 |
+ return {nullptr, -1}; |
| 117 |
+ } |
| 118 |
+ |
| 119 |
int* existingOffset = fGradientOffsetCache.find(shader->uniqueID()); |
| 120 |
if (existingOffset) { |
| 121 |
return {nullptr, *existingOffset}; |
| 122 |
} |
| 123 |
auto [ptr, offset] = this->allocateFloatData(numStops * 5); // 4 for color, 1 for offset |
| 124 |
- fGradientOffsetCache.set(shader->uniqueID(), offset); |
| 125 |
+ |
| 126 |
+ // Only cache the storage if it was allocated successfully. |
| 127 |
+ if (ptr) { |
| 128 |
+ SkASSERT(offset >= 0); |
| 129 |
+ fGradientOffsetCache.set(shader->uniqueID(), offset); |
| 130 |
+ } |
| 131 |
|
| 132 |
return {ptr, offset}; |
| 133 |
} |
| 134 |
@@ -488,6 +517,7 @@ public: |
| 135 |
bool finalize(DrawBufferManager* bufferMgr) { |
| 136 |
SkASSERT(!this->isFinalized()); |
| 137 |
if (!fGradientStorage.empty()) { |
| 138 |
+ SkASSERT(fGradientStorage.size() <= kMaxStorageFloats); |
| 139 |
auto [writer, bufferInfo, _] = |
| 140 |
bufferMgr->getMappedStorageBuffer(fGradientStorage.size(), sizeof(float)); |
| 141 |
if (writer) { |
| 142 |
@@ -512,6 +542,9 @@ private: |
| 143 |
// of the new allocation and its offset from the beginning of the buffer. |
| 144 |
std::pair<float*, int> allocateFloatData(int floatCount) { |
| 145 |
int currentSize = fGradientStorage.size(); |
| 146 |
+ if (kMaxStorageFloats - floatCount < currentSize) { |
| 147 |
+ return {nullptr, -1}; // We've accumulated too much |
| 148 |
+ } |
| 149 |
fGradientStorage.resize(currentSize + floatCount); |
| 150 |
float* startPtr = fGradientStorage.begin() + currentSize; |
| 151 |
|
| 152 |
-- |
| 153 |
2.53.0 |
| 154 |
|