blob: 5fa4d836b74f889dbcb1a91973a9c91973662545 [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
Florin Malita39e08552017-07-06 14:16:18 -040011#include "SkColor.h"
12#include "SkRefCnt.h"
13#include "SkTHash.h"
14
15class SkBitmap;
16class SkColorFilter;
17class SkColorSpace;
18class SkColorSpaceXform;
19class SkImage;
20class SkImageFilter;
21class SkPaint;
22class SkShader;
Matt Sarettcdc651d2017-03-30 12:41:48 -040023
24class SkColorSpaceXformer : public SkNoncopyable {
25public:
26 static std::unique_ptr<SkColorSpaceXformer> Make(sk_sp<SkColorSpace> dst);
27
Florin Malita39e08552017-07-06 14:16:18 -040028 ~SkColorSpaceXformer();
29
Mike Klein2814d912017-05-10 12:35:51 -040030 sk_sp<SkImage> apply(const SkImage*);
31 sk_sp<SkImage> apply(const SkBitmap&);
32 sk_sp<SkColorFilter> apply(const SkColorFilter*);
33 sk_sp<SkImageFilter> apply(const SkImageFilter*);
34 sk_sp<SkShader> apply(const SkShader*);
35 SkPaint apply(const SkPaint&);
Matt Sarettcdc651d2017-03-30 12:41:48 -040036 void apply(SkColor dst[], const SkColor src[], int n);
Matt Sarett6d72ed92017-04-10 16:35:33 -040037 SkColor apply(SkColor srgb);
Matt Sarettcdc651d2017-03-30 12:41:48 -040038
Matt Sarette22a6a22017-04-12 16:26:21 -040039 sk_sp<SkColorSpace> dst() const { return fDst; }
40
Matt Sarettcdc651d2017-03-30 12:41:48 -040041private:
Florin Malita24a2ecf2017-07-06 15:12:12 -040042 SkColorSpaceXformer(sk_sp<SkColorSpace> dst, std::unique_ptr<SkColorSpaceXform> fromSRGB);
Matt Sarettcdc651d2017-03-30 12:41:48 -040043
Florin Malita5449aad2017-07-10 11:14:40 -040044 template <typename T>
45 using Cache = SkTHashMap<sk_sp<T>, sk_sp<T>>;
46
47 template <typename T>
48 sk_sp<T> cachedApply(const T*, Cache<T>*, sk_sp<T> (*)(const T*, SkColorSpaceXformer*));
49
50 void purgeCaches();
51
52 class AutoCachePurge;
53
Matt Sarettcdc651d2017-03-30 12:41:48 -040054 sk_sp<SkColorSpace> fDst;
55 std::unique_ptr<SkColorSpaceXform> fFromSRGB;
Florin Malita39e08552017-07-06 14:16:18 -040056
Florin Malita5449aad2017-07-10 11:14:40 -040057 size_t fReentryCount; // tracks the number of nested apply() calls for cache purging.
58
59 Cache<SkImage > fImageCache;
60 Cache<SkColorFilter> fColorFilterCache;
61 Cache<SkImageFilter> fImageFilterCache;
Matt Sarettcdc651d2017-03-30 12:41:48 -040062};
63
64#endif