• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 #ifndef SkColorSpaceXformer_DEFINED
9 #define SkColorSpaceXformer_DEFINED
10 
11 #include "SkCanvas.h"
12 #include "SkColor.h"
13 #include "SkColorSpaceXformSteps.h"
14 #include "SkRasterPipeline.h"
15 #include "SkRefCnt.h"
16 #include "SkTHash.h"
17 
18 class SkBitmap;
19 class SkColorFilter;
20 class SkColorSpace;
21 class SkImage;
22 class SkImageFilter;
23 class SkPaint;
24 class SkShader;
25 
26 class SkColorSpaceXformer : public SkNoncopyable {
27 public:
28     static std::unique_ptr<SkColorSpaceXformer> Make(sk_sp<SkColorSpace> dst);
29 
30     ~SkColorSpaceXformer();
31 
32     sk_sp<SkImage> apply(const SkImage*);
33     sk_sp<SkImage> apply(const SkBitmap&);
34     sk_sp<SkColorFilter> apply(const SkColorFilter*);
35     sk_sp<SkImageFilter> apply(const SkImageFilter*);
36     sk_sp<SkShader>      apply(const SkShader*);
37     SkPaint apply(const SkPaint&);
38     void apply(SkColor dst[], const SkColor src[], int n);
39     SkColor apply(SkColor srgb);
40 
dst()41     sk_sp<SkColorSpace> dst() const { return fDst; }
42 
43     SkCanvas::Lattice apply(const SkCanvas::Lattice&, SkColor*, int);
44 
45 private:
46     explicit SkColorSpaceXformer(sk_sp<SkColorSpace> dst);
47 
48     template <typename T>
49     using Cache = SkTHashMap<sk_sp<T>, sk_sp<T>>;
50 
51     template <typename T>
52     sk_sp<T> cachedApply(const T*, Cache<T>*, sk_sp<T> (*)(const T*, SkColorSpaceXformer*));
53 
54     void purgeCaches();
55 
56     class AutoCachePurge;
57 
58     sk_sp<SkColorSpace>                                 fDst;
59     SkSTArenaAlloc<256>                                 fAlloc;
60     std::function<void(size_t, size_t, size_t, size_t)> fFromSRGB;
61     SkColorSpaceXformSteps                              fFromSRGBSteps;
62     SkRasterPipeline_MemoryCtx                          fFromSRGBSrc{nullptr,0};
63     SkRasterPipeline_MemoryCtx                          fFromSRGBDst{nullptr,0};
64 
65     size_t fReentryCount; // tracks the number of nested apply() calls for cache purging.
66 
67     Cache<SkImage      > fImageCache;
68     Cache<SkColorFilter> fColorFilterCache;
69     Cache<SkImageFilter> fImageFilterCache;
70 };
71 
72 #endif
73