blob: 7d277d770bec3a2649f6d5413992fd1be84b4b0b [file] [log] [blame]
Matt Sarettcdc651d2017-03-30 12:41:48 -04001/*
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
Stan Iliev45fd9952017-12-18 14:46:30 -050011#include "SkCanvas.h"
Florin Malita39e08552017-07-06 14:16:18 -040012#include "SkColor.h"
Mike Kleinc8579562018-08-07 13:21:21 -040013#include "SkColorSpaceXformSteps.h"
14#include "SkRasterPipeline.h"
Florin Malita39e08552017-07-06 14:16:18 -040015#include "SkRefCnt.h"
16#include "SkTHash.h"
17
18class SkBitmap;
19class SkColorFilter;
20class SkColorSpace;
Florin Malita39e08552017-07-06 14:16:18 -040021class SkImage;
22class SkImageFilter;
23class SkPaint;
24class SkShader;
Matt Sarettcdc651d2017-03-30 12:41:48 -040025
26class SkColorSpaceXformer : public SkNoncopyable {
27public:
28 static std::unique_ptr<SkColorSpaceXformer> Make(sk_sp<SkColorSpace> dst);
29
Florin Malita39e08552017-07-06 14:16:18 -040030 ~SkColorSpaceXformer();
31
Mike Klein2814d912017-05-10 12:35:51 -040032 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&);
Matt Sarettcdc651d2017-03-30 12:41:48 -040038 void apply(SkColor dst[], const SkColor src[], int n);
Matt Sarett6d72ed92017-04-10 16:35:33 -040039 SkColor apply(SkColor srgb);
Matt Sarettcdc651d2017-03-30 12:41:48 -040040
Matt Sarette22a6a22017-04-12 16:26:21 -040041 sk_sp<SkColorSpace> dst() const { return fDst; }
42
Stan Iliev45fd9952017-12-18 14:46:30 -050043 SkCanvas::Lattice apply(const SkCanvas::Lattice&, SkColor*, int);
44
Matt Sarettcdc651d2017-03-30 12:41:48 -040045private:
Mike Kleinc8579562018-08-07 13:21:21 -040046 explicit SkColorSpaceXformer(sk_sp<SkColorSpace> dst);
Matt Sarettcdc651d2017-03-30 12:41:48 -040047
Florin Malita5449aad2017-07-10 11:14:40 -040048 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
Mike Klein9faf9fd2018-08-09 10:04:38 -040058 sk_sp<SkColorSpace> fDst;
Mike Kleinc8579562018-08-07 13:21:21 -040059 SkSTArenaAlloc<256> fAlloc;
60 std::function<void(size_t, size_t, size_t, size_t)> fFromSRGB;
61 SkColorSpaceXformSteps fFromSRGBSteps;
Mike Kleinb11ab572018-10-24 06:42:14 -040062 SkRasterPipeline_MemoryCtx fFromSRGBSrc{nullptr,0};
63 SkRasterPipeline_MemoryCtx fFromSRGBDst{nullptr,0};
Florin Malita39e08552017-07-06 14:16:18 -040064
Florin Malita5449aad2017-07-10 11:14:40 -040065 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;
Matt Sarettcdc651d2017-03-30 12:41:48 -040070};
71
72#endif