xref: /aosp_15_r20/external/skia/src/gpu/ganesh/gradients/GrGradientBitmapCache.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2018 Google Inc.
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 #ifndef GrGradientBitmapCache_DEFINED
8 #define GrGradientBitmapCache_DEFINED
9 
10 #include "include/core/SkScalar.h"
11 #include "include/effects/SkGradientShader.h"
12 #include "include/private/SkColorData.h"
13 #include "include/private/base/SkDebug.h"
14 #include "include/private/base/SkMutex.h"
15 #include "include/private/base/SkNoncopyable.h"
16 
17 #include <cstddef>
18 
19 class SkBitmap;
20 class SkColorSpace;
21 enum SkAlphaType : int;
22 enum SkColorType : int;
23 
24 class GrGradientBitmapCache : SkNoncopyable {
25 public:
26     GrGradientBitmapCache(int maxEntries, int resolution);
27     ~GrGradientBitmapCache();
28 
29     // Assumes colors are compatible with the specified alphaType (e.g. if it's premul then colors
30     // are already premultiplied). Thread safe.
31     void getGradient(const SkPMColor4f* colors,
32                      const SkScalar* positions,
33                      int count,
34                      bool colorsAreOpaque,
35                      const SkGradientShader::Interpolation& interpolation,
36                      const SkColorSpace* intermediateColorSpace,
37                      const SkColorSpace* dstColorSpace,
38                      SkColorType colorType,
39                      SkAlphaType alphaType,
40                      SkBitmap* bitmap);
41 
42 private:
43     SkMutex fMutex;
44 
45     int fEntryCount;
46     const int fMaxEntries;
47     const int fResolution;
48 
49     struct Entry;
50     mutable Entry*  fHead;
51     mutable Entry*  fTail;
52 
53     inline Entry* release(Entry*) const;
54     inline void attachToHead(Entry*) const;
55 
56     bool find(const void* buffer, size_t len, SkBitmap*) const;
57     void add(const void* buffer, size_t len, const SkBitmap&);
58 
59     void fillGradient(const SkPMColor4f* colors,
60                       const SkScalar* positions,
61                       int count,
62                       bool colorsAreOpaque,
63                       const SkGradientShader::Interpolation& interpolation,
64                       const SkColorSpace* intermediateColorSpace,
65                       const SkColorSpace* dstColorSpace,
66                       SkBitmap* bitmap);
67 
68 #ifdef SK_DEBUG
69     void validate() const;
70 #else
validate()71     void validate() const {}
72 #endif
73 
74     class AutoValidate : SkNoncopyable {
75     public:
AutoValidate(const GrGradientBitmapCache * bc)76         AutoValidate(const GrGradientBitmapCache* bc) : fBC(bc) { bc->validate(); }
~AutoValidate()77         ~AutoValidate() { fBC->validate(); }
78     private:
79         const GrGradientBitmapCache* fBC;
80     };
81 };
82 
83 #endif
84