blob: 2807e7cfed9216117bcb3feb1b1b0476a7939970 [file] [log] [blame]
Michael Ludwiga7914d32018-09-14 09:47:21 -04001/*
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"
Brian Osmane3f543c2018-11-02 12:52:10 -040013#include "SkColorData.h"
Michael Ludwiga7914d32018-09-14 09:47:21 -040014#include "SkMutex.h"
15#include "SkNoncopyable.h"
Michael Ludwiga7914d32018-09-14 09:47:21 -040016
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.
Brian Osman021ed512018-10-16 15:19:44 -040024 void getGradient(const SkPMColor4f* colors, const SkScalar* positions, int count,
Michael Ludwiga7914d32018-09-14 09:47:21 -040025 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
Brian Osman021ed512018-10-16 15:19:44 -040044 void fillGradient(const SkPMColor4f* colors, const SkScalar* positions, int count,
Michael Ludwiga7914d32018-09-14 09:47:21 -040045 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