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 "include/core/SkBitmap.h"
13#include "include/private/SkColorData.h"
14#include "include/private/SkMutex.h"
15#include "include/private/SkNoncopyable.h"
16
17class GrGradientBitmapCache : SkNoncopyable {
18public:
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
27private:
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
50 void validate() const {}
51#endif
52
53 class AutoValidate : SkNoncopyable {
54 public:
55 AutoValidate(const GrGradientBitmapCache* bc) : fBC(bc) { bc->validate(); }
56 ~AutoValidate() { fBC->validate(); }
57 private:
58 const GrGradientBitmapCache* fBC;
59 };
60};
61
62#endif
63