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