Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 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" |
Brian Osman | e3f543c | 2018-11-02 12:52:10 -0400 | [diff] [blame] | 13 | #include "SkColorData.h" |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 14 | #include "SkMutex.h" |
| 15 | #include "SkNoncopyable.h" |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 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. |
Brian Osman | 021ed51 | 2018-10-16 15:19:44 -0400 | [diff] [blame] | 24 | void getGradient(const SkPMColor4f* colors, const SkScalar* positions, int count, |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 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 | |
Brian Osman | 021ed51 | 2018-10-16 15:19:44 -0400 | [diff] [blame] | 44 | void fillGradient(const SkPMColor4f* colors, const SkScalar* positions, int count, |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 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 |