| 1 |
From 2e4a568f6f0d854b27dfbb2c8c8723a196071d65 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Michael Ludwig <[email protected]> |
| 3 |
Date: Fri, 22 May 2026 16:44:59 -0400 |
| 4 |
Subject: [PATCH] [graphite] Ref count large gradient shaders in |
| 5 |
FloatStorageManager |
| 6 |
|
| 7 |
This also removes the unique ID from SkShaderBase as the FSM was the |
| 8 |
only system that relied on it. |
| 9 |
|
| 10 |
Fixed: 512986879 |
| 11 |
Bug: https://issues.chromium.org/issues/512986879 |
| 12 |
Change-Id: Icd43c13a75a1cdd212eea2b9db03033d13be47f4 |
| 13 |
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1243937 |
| 14 |
Reviewed-by: Thomas Smith <[email protected]> |
| 15 |
Commit-Queue: Michael Ludwig <[email protected]> |
| 16 |
--- |
| 17 |
src/gpu/graphite/PipelineData.h | 27 ++++++++++++++++++++++----- |
| 18 |
src/shaders/SkShaderBase.cpp | 18 +----------------- |
| 19 |
src/shaders/SkShaderBase.h | 4 ---- |
| 20 |
3 files changed, 23 insertions(+), 26 deletions(-) |
| 21 |
|
| 22 |
diff --git a/src/gpu/graphite/PipelineData.h b/src/gpu/graphite/PipelineData.h |
| 23 |
index bc255428f7..d528c86f11 100644 |
| 24 |
--- a/src/gpu/graphite/PipelineData.h |
| 25 |
+++ b/src/gpu/graphite/PipelineData.h |
| 26 |
@@ -480,9 +480,13 @@ class FloatStorageManager : public SkRefCnt { |
| 27 |
static_assert(std::numeric_limits<uint32_t>::max() / sizeof(float) |
| 28 |
<= (uint32_t) std::numeric_limits<int>::max()); |
| 29 |
public: |
| 30 |
- FloatStorageManager() = default; |
| 31 |
+ FloatStorageManager() { this->reset(); } |
| 32 |
|
| 33 |
void reset() { |
| 34 |
+ // Remove the manually added refs on the keys in fGradientOffsetCache |
| 35 |
+ for (auto [k, _] : fGradientOffsetCache) { |
| 36 |
+ k->unref(); // k might be deleted at this point but we won't use it anymore |
| 37 |
+ } |
| 38 |
fGradientStorage.clear(); |
| 39 |
fGradientOffsetCache.reset(); |
| 40 |
} |
| 41 |
@@ -499,7 +503,7 @@ public: |
| 42 |
return {nullptr, -1}; |
| 43 |
} |
| 44 |
|
| 45 |
- int* existingOffset = fGradientOffsetCache.find(shader->uniqueID()); |
| 46 |
+ int* existingOffset = fGradientOffsetCache.find(shader); |
| 47 |
if (existingOffset) { |
| 48 |
return {nullptr, *existingOffset}; |
| 49 |
} |
| 50 |
@@ -508,7 +512,10 @@ public: |
| 51 |
// Only cache the storage if it was allocated successfully. |
| 52 |
if (ptr) { |
| 53 |
SkASSERT(offset >= 0); |
| 54 |
- fGradientOffsetCache.set(shader->uniqueID(), offset); |
| 55 |
+ // Since FloatStorageManager is single threaded, adding a ref and then storing in the |
| 56 |
+ // map should be fine. |
| 57 |
+ shader->ref(); |
| 58 |
+ fGradientOffsetCache.set(shader, offset); |
| 59 |
} |
| 60 |
|
| 61 |
return {ptr, offset}; |
| 62 |
@@ -555,8 +562,18 @@ private: |
| 63 |
// storage buffer can be bound once and accessed at random. |
| 64 |
SkTDArray<float> fGradientStorage; |
| 65 |
|
| 66 |
- // We use the shader's unique ID as a key to de-duplicate gradient data. |
| 67 |
- skia_private::THashMap<uint32_t, int> fGradientOffsetCache; |
| 68 |
+ // We use the shader's address as a key to de-duplicate gradient data. Each key has a ref added |
| 69 |
+ // when it's first put in the map. The map does not key off of sk_sp<SkGradientBaseShader> to as |
| 70 |
+ // that is not compatible with SkGoodHash. These refs are dropped in reset(). While this extends |
| 71 |
+ // the lifetime of the SkShaders, it only applies to large gradients. Hopefully clients are |
| 72 |
+ // trying to reuse such shaders across frames already. |
| 73 |
+ // |
| 74 |
+ // If we didn't keep the shaders alive, we'd have to worry about cache collisions from |
| 75 |
+ // re-allocations using the same address. Using a unique ID can wrap, leading to potential |
| 76 |
+ // mismatches. Adding sufficient data to eliminate this risk (e.g. keying off the unique ID, the |
| 77 |
+ // address, the number of color stops, AND a hash of the color data) is likely more expensive |
| 78 |
+ // than taking a ref. |
| 79 |
+ skia_private::THashMap<const SkGradientBaseShader*, int> fGradientOffsetCache; |
| 80 |
|
| 81 |
std::optional<BindBufferInfo> fBufferInfo = std::nullopt; |
| 82 |
}; |
| 83 |
diff --git a/src/shaders/SkShaderBase.cpp b/src/shaders/SkShaderBase.cpp |
| 84 |
index 5d20ba49ea..dfbc3e2b73 100644 |
| 85 |
--- a/src/shaders/SkShaderBase.cpp |
| 86 |
+++ b/src/shaders/SkShaderBase.cpp |
| 87 |
@@ -20,22 +20,6 @@ |
| 88 |
|
| 89 |
class SkWriteBuffer; |
| 90 |
|
| 91 |
-namespace { |
| 92 |
- |
| 93 |
-// The 32-bit shader ID counter is not expected to wrap. In the unlikely event that it does, we |
| 94 |
-// assume that the associated shaders will be sufficiently temporally separated such that shaders |
| 95 |
-// with the same recycled IDs are no longer in use. |
| 96 |
-uint32_t next_unique_id() { |
| 97 |
- static std::atomic<uint32_t> gNextUniqueID{SK_InvalidUniqueID + 1}; |
| 98 |
- uint32_t id = SK_InvalidUniqueID; |
| 99 |
- do { |
| 100 |
- id = gNextUniqueID.fetch_add(1, std::memory_order_relaxed); |
| 101 |
- } while (id == SK_InvalidUniqueID); |
| 102 |
- return id; |
| 103 |
-} |
| 104 |
- |
| 105 |
-} // anonymous namespace |
| 106 |
- |
| 107 |
namespace SkShaders { |
| 108 |
MatrixRec::MatrixRec(const SkMatrix& ctm) : fCTM(ctm) {} |
| 109 |
|
| 110 |
@@ -92,7 +76,7 @@ MatrixRec MatrixRec::concat(const SkMatrix& m) const { |
| 111 |
|
| 112 |
/////////////////////////////////////////////////////////////////////////////////////// |
| 113 |
|
| 114 |
-SkShaderBase::SkShaderBase() : fUniqueID(next_unique_id()) {} |
| 115 |
+SkShaderBase::SkShaderBase() {} |
| 116 |
|
| 117 |
SkShaderBase::~SkShaderBase() = default; |
| 118 |
|
| 119 |
diff --git a/src/shaders/SkShaderBase.h b/src/shaders/SkShaderBase.h |
| 120 |
index 16b80c4713..ecd25e2204 100644 |
| 121 |
--- a/src/shaders/SkShaderBase.h |
| 122 |
+++ b/src/shaders/SkShaderBase.h |
| 123 |
@@ -186,8 +186,6 @@ class SkShaderBase : public SkShader { |
| 124 |
public: |
| 125 |
~SkShaderBase() override; |
| 126 |
|
| 127 |
- uint32_t uniqueID() const { return fUniqueID; } |
| 128 |
- |
| 129 |
sk_sp<SkShader> makeInvertAlpha() const; |
| 130 |
sk_sp<SkShader> makeWithCTM(const SkMatrix&) const; // owns its own ctm |
| 131 |
|
| 132 |
@@ -409,8 +407,6 @@ protected: |
| 133 |
} |
| 134 |
|
| 135 |
private: |
| 136 |
- const uint32_t fUniqueID; |
| 137 |
- |
| 138 |
friend class SkShaders::MatrixRec; |
| 139 |
}; |
| 140 |
inline SkShaderBase* as_SB(SkShader* shader) { |
| 141 |
-- |
| 142 |
2.53.0 |
| 143 |
|