• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 
9 #ifndef GrGradientBitmapCache_DEFINED
10 #define GrGradientBitmapCache_DEFINED
11 
12 #include "SkBitmap.h"
13 #include "SkColorData.h"
14 #include "SkMutex.h"
15 #include "SkNoncopyable.h"
16 
17 class GrGradientBitmapCache : SkNoncopyable {
18 public:
19     GrGradientBitmapCache(int maxEntries, int resolution);
20     ~GrGradientBitmapCache();
21 
22     // Assumes colors are compatible with the specified alphaType (e.g. if it's premul then colors
23     // are already premultiplied). Thread safe.
24     void getGradient(const SkPMColor4f* colors, const SkScalar* positions, int count,
25                      SkColorType colorType, SkAlphaType alphaType, SkBitmap* bitmap);
26 
27 private:
28     SkMutex fMutex;
29 
30     int fEntryCount;
31     const int fMaxEntries;
32     const int fResolution;
33 
34     struct Entry;
35     mutable Entry*  fHead;
36     mutable Entry*  fTail;
37 
38     inline Entry* release(Entry*) const;
39     inline void attachToHead(Entry*) const;
40 
41     bool find(const void* buffer, size_t len, SkBitmap*) const;
42     void add(const void* buffer, size_t len, const SkBitmap&);
43 
44     void fillGradient(const SkPMColor4f* colors, const SkScalar* positions, int count,
45                       SkColorType colorType, SkBitmap* bitmap);
46 
47 #ifdef SK_DEBUG
48     void validate() const;
49 #else
validate()50     void validate() const {}
51 #endif
52 
53     class AutoValidate : SkNoncopyable {
54     public:
AutoValidate(const GrGradientBitmapCache * bc)55         AutoValidate(const GrGradientBitmapCache* bc) : fBC(bc) { bc->validate(); }
~AutoValidate()56         ~AutoValidate() { fBC->validate(); }
57     private:
58         const GrGradientBitmapCache* fBC;
59     };
60 };
61 
62 #endif
63