blob: 58b3f4958aec249db60cdee6050b1c4f01151e31 [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#include "SkColorFilter.h"
9#include "SkColorSpaceXformer.h"
10#include "SkColorSpaceXform_Base.h"
11#include "SkDrawLooper.h"
12#include "SkGradientShader.h"
Florin Malita39e08552017-07-06 14:16:18 -040013#include "SkImage.h"
Matt Sarettcdc651d2017-03-30 12:41:48 -040014#include "SkImage_Base.h"
Matt Sarett31abf1f2017-04-07 16:54:04 -040015#include "SkImageFilter.h"
Matt Sarette6844832017-04-03 10:35:42 -040016#include "SkImagePriv.h"
Florin Malita4aed1382017-05-25 10:38:07 -040017#include "SkShaderBase.h"
Matt Sarettcdc651d2017-03-30 12:41:48 -040018
Florin Malita24a2ecf2017-07-06 15:12:12 -040019SkColorSpaceXformer::SkColorSpaceXformer(sk_sp<SkColorSpace> dst,
20 std::unique_ptr<SkColorSpaceXform> fromSRGB)
21 : fDst(std::move(dst))
Florin Malita5449aad2017-07-10 11:14:40 -040022 , fFromSRGB(std::move(fromSRGB))
23 , fReentryCount(0) {}
Florin Malita24a2ecf2017-07-06 15:12:12 -040024
Florin Malita39e08552017-07-06 14:16:18 -040025SkColorSpaceXformer::~SkColorSpaceXformer() {}
26
Matt Sarettcdc651d2017-03-30 12:41:48 -040027std::unique_ptr<SkColorSpaceXformer> SkColorSpaceXformer::Make(sk_sp<SkColorSpace> dst) {
28 std::unique_ptr<SkColorSpaceXform> fromSRGB = SkColorSpaceXform_Base::New(
29 SkColorSpace::MakeSRGB().get(), dst.get(), SkTransferFunctionBehavior::kIgnore);
Matt Sarettcdc651d2017-03-30 12:41:48 -040030
Florin Malita24a2ecf2017-07-06 15:12:12 -040031 return fromSRGB
32 ? std::unique_ptr<SkColorSpaceXformer>(new SkColorSpaceXformer(std::move(dst),
33 std::move(fromSRGB)))
34 : nullptr;
Matt Sarettcdc651d2017-03-30 12:41:48 -040035}
36
Florin Malita5449aad2017-07-10 11:14:40 -040037// So what's up with these caches?
38//
39// We want to cache transformed objects for a couple of reasons:
40//
41// 1) to avoid redundant work - the inputs are a DAG, not a tree (e.g. same SkImage drawn multiple
42// times in a SkPicture), so if we blindly recurse we could end up transforming the same objects
43// repeatedly.
44//
45// 2) to avoid topology changes - we want the output to remain isomorphic with the input -- this is
46// particularly important for image filters (to maintain their original DAG structure in order
47// to not defeat their own/internal caching), but also for avoiding unnecessary cloning
48// (e.g. duplicated SkImages allocated for the example in #1 above).
49//
50// The caching scope is naturaly bound by the lifetime of the SkColorSpaceXformer object, but
51// clients may choose to not discard xformers immediately - in which case, caching indefinitely
52// is problematic. The solution is to limit the cache scope to the top level apply() call
53// (i.e. we only keep cached objects alive while transforming).
54
55class SkColorSpaceXformer::AutoCachePurge {
56public:
57 AutoCachePurge(SkColorSpaceXformer* xformer)
58 : fXformer(xformer) {
59 fXformer->fReentryCount++;
60 }
61
62 ~AutoCachePurge() {
63 SkASSERT(fXformer->fReentryCount > 0);
64 if (--fXformer->fReentryCount == 0) {
65 fXformer->purgeCaches();
66 }
67 }
68
69private:
70 SkColorSpaceXformer* fXformer;
71};
72
73template <typename T>
74sk_sp<T> SkColorSpaceXformer::cachedApply(const T* src, Cache<T>* cache,
75 sk_sp<T> (*applyFunc)(const T*, SkColorSpaceXformer*)) {
76 if (!src) {
77 return nullptr;
78 }
79
80 auto key = sk_ref_sp(const_cast<T*>(src));
81 if (auto* xformed = cache->find(key)) {
82 return sk_ref_sp(xformed->get());
83 }
84
85 auto xformed = applyFunc(src, this);
86 cache->set(std::move(key), xformed);
87
88 return xformed;
89}
90
91void SkColorSpaceXformer::purgeCaches() {
92 fImageCache.reset();
93 fColorFilterCache.reset();
94 fImageFilterCache.reset();
95}
96
Matt Sarettcdc651d2017-03-30 12:41:48 -040097sk_sp<SkImage> SkColorSpaceXformer::apply(const SkImage* src) {
Florin Malita5449aad2017-07-10 11:14:40 -040098 const AutoCachePurge autoPurge(this);
99 return this->cachedApply<SkImage>(src, &fImageCache,
100 [](const SkImage* img, SkColorSpaceXformer* xformer) {
101 return img->makeColorSpace(xformer->fDst, SkTransferFunctionBehavior::kIgnore);
102 });
Matt Sarettcdc651d2017-03-30 12:41:48 -0400103}
104
Matt Sarette6844832017-04-03 10:35:42 -0400105sk_sp<SkImage> SkColorSpaceXformer::apply(const SkBitmap& src) {
Florin Malita5449aad2017-07-10 11:14:40 -0400106 const AutoCachePurge autoPurge(this);
Matt Sarette6844832017-04-03 10:35:42 -0400107 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(src, kNever_SkCopyPixelsMode);
108 if (!image) {
109 return nullptr;
110 }
111
Matt Sarettcb874232017-04-05 11:41:27 -0400112 sk_sp<SkImage> xformed = image->makeColorSpace(fDst, SkTransferFunctionBehavior::kIgnore);
Matt Sarette6844832017-04-03 10:35:42 -0400113 // We want to be sure we don't let the kNever_SkCopyPixelsMode image escape this stack frame.
114 SkASSERT(xformed != image);
115 return xformed;
116}
117
Matt Sarett6d72ed92017-04-10 16:35:33 -0400118sk_sp<SkColorFilter> SkColorSpaceXformer::apply(const SkColorFilter* colorFilter) {
Florin Malita5449aad2017-07-10 11:14:40 -0400119 const AutoCachePurge autoPurge(this);
120 return this->cachedApply<SkColorFilter>(colorFilter, &fColorFilterCache,
121 [](const SkColorFilter* f, SkColorSpaceXformer* xformer) {
122 return f->makeColorSpace(xformer);
123 });
Matt Sarett6d72ed92017-04-10 16:35:33 -0400124}
125
Mike Kleine908b942017-04-25 13:09:40 -0400126sk_sp<SkImageFilter> SkColorSpaceXformer::apply(const SkImageFilter* imageFilter) {
Florin Malita5449aad2017-07-10 11:14:40 -0400127 const AutoCachePurge autoPurge(this);
128 return this->cachedApply<SkImageFilter>(imageFilter, &fImageFilterCache,
129 [](const SkImageFilter* f, SkColorSpaceXformer* xformer) {
130 return f->makeColorSpace(xformer);
131 });
Mike Kleine908b942017-04-25 13:09:40 -0400132}
133
Mike Klein2814d912017-05-10 12:35:51 -0400134sk_sp<SkShader> SkColorSpaceXformer::apply(const SkShader* shader) {
Florin Malita5449aad2017-07-10 11:14:40 -0400135 const AutoCachePurge autoPurge(this);
Florin Malita4aed1382017-05-25 10:38:07 -0400136 return as_SB(shader)->makeColorSpace(this);
Mike Klein2814d912017-05-10 12:35:51 -0400137}
138
Matt Sarettcdc651d2017-03-30 12:41:48 -0400139void SkColorSpaceXformer::apply(SkColor* xformed, const SkColor* srgb, int n) {
140 SkAssertResult(fFromSRGB->apply(SkColorSpaceXform::kBGRA_8888_ColorFormat, xformed,
141 SkColorSpaceXform::kBGRA_8888_ColorFormat, srgb,
142 n, kUnpremul_SkAlphaType));
143}
144
145SkColor SkColorSpaceXformer::apply(SkColor srgb) {
146 SkColor xformed;
147 this->apply(&xformed, &srgb, 1);
148 return xformed;
149}
150
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400151SkPaint SkColorSpaceXformer::apply(const SkPaint& src) {
Florin Malita5449aad2017-07-10 11:14:40 -0400152 const AutoCachePurge autoPurge(this);
153
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400154 SkPaint dst = src;
Matt Sarettcdc651d2017-03-30 12:41:48 -0400155
156 // All SkColorSpaces have the same black point.
157 if (src.getColor() & 0xffffff) {
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400158 dst.setColor(this->apply(src.getColor()));
Matt Sarettcdc651d2017-03-30 12:41:48 -0400159 }
160
161 if (auto shader = src.getShader()) {
Mike Klein2814d912017-05-10 12:35:51 -0400162 dst.setShader(this->apply(shader));
Matt Sarettcdc651d2017-03-30 12:41:48 -0400163 }
164
Matt Sarettcdc651d2017-03-30 12:41:48 -0400165 if (auto cf = src.getColorFilter()) {
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400166 dst.setColorFilter(this->apply(cf));
Matt Sarettcdc651d2017-03-30 12:41:48 -0400167 }
168
169 if (auto looper = src.getDrawLooper()) {
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400170 dst.setDrawLooper(looper->makeColorSpace(this));
Matt Sarettcdc651d2017-03-30 12:41:48 -0400171 }
172
Matt Sarett31abf1f2017-04-07 16:54:04 -0400173 if (auto imageFilter = src.getImageFilter()) {
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400174 dst.setImageFilter(this->apply(imageFilter));
Matt Sarett31abf1f2017-04-07 16:54:04 -0400175 }
176
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400177 return dst;
Matt Sarettcdc651d2017-03-30 12:41:48 -0400178}