blob: 1e56d4da936dc06a89a065507e124529e0f32adf [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"
Mike Kleine28a6b52018-07-25 13:05:17 -04009#include "SkColorSpacePriv.h"
Matt Sarettcdc651d2017-03-30 12:41:48 -040010#include "SkColorSpaceXformer.h"
Mike Klein6747f522018-05-22 10:32:20 -040011#include "SkColorSpaceXformPriv.h"
Matt Sarettcdc651d2017-03-30 12:41:48 -040012#include "SkDrawLooper.h"
13#include "SkGradientShader.h"
Florin Malita39e08552017-07-06 14:16:18 -040014#include "SkImage.h"
Matt Sarettcdc651d2017-03-30 12:41:48 -040015#include "SkImage_Base.h"
Matt Sarett31abf1f2017-04-07 16:54:04 -040016#include "SkImageFilter.h"
Matt Sarette6844832017-04-03 10:35:42 -040017#include "SkImagePriv.h"
Florin Malita4aed1382017-05-25 10:38:07 -040018#include "SkShaderBase.h"
Matt Sarettcdc651d2017-03-30 12:41:48 -040019
Florin Malita24a2ecf2017-07-06 15:12:12 -040020SkColorSpaceXformer::SkColorSpaceXformer(sk_sp<SkColorSpace> dst,
21 std::unique_ptr<SkColorSpaceXform> fromSRGB)
22 : fDst(std::move(dst))
Florin Malita5449aad2017-07-10 11:14:40 -040023 , fFromSRGB(std::move(fromSRGB))
24 , fReentryCount(0) {}
Florin Malita24a2ecf2017-07-06 15:12:12 -040025
Florin Malita39e08552017-07-06 14:16:18 -040026SkColorSpaceXformer::~SkColorSpaceXformer() {}
27
Matt Sarettcdc651d2017-03-30 12:41:48 -040028std::unique_ptr<SkColorSpaceXformer> SkColorSpaceXformer::Make(sk_sp<SkColorSpace> dst) {
Mike Klein6747f522018-05-22 10:32:20 -040029 std::unique_ptr<SkColorSpaceXform> fromSRGB = SkMakeColorSpaceXform(
Mike Kleine28a6b52018-07-25 13:05:17 -040030 sk_srgb_singleton(), dst.get());
Matt Sarettcdc651d2017-03-30 12:41:48 -040031
Florin Malita24a2ecf2017-07-06 15:12:12 -040032 return fromSRGB
33 ? std::unique_ptr<SkColorSpaceXformer>(new SkColorSpaceXformer(std::move(dst),
34 std::move(fromSRGB)))
35 : nullptr;
Matt Sarettcdc651d2017-03-30 12:41:48 -040036}
37
Florin Malita5449aad2017-07-10 11:14:40 -040038// So what's up with these caches?
39//
40// We want to cache transformed objects for a couple of reasons:
41//
42// 1) to avoid redundant work - the inputs are a DAG, not a tree (e.g. same SkImage drawn multiple
43// times in a SkPicture), so if we blindly recurse we could end up transforming the same objects
44// repeatedly.
45//
46// 2) to avoid topology changes - we want the output to remain isomorphic with the input -- this is
47// particularly important for image filters (to maintain their original DAG structure in order
48// to not defeat their own/internal caching), but also for avoiding unnecessary cloning
49// (e.g. duplicated SkImages allocated for the example in #1 above).
50//
51// The caching scope is naturaly bound by the lifetime of the SkColorSpaceXformer object, but
52// clients may choose to not discard xformers immediately - in which case, caching indefinitely
53// is problematic. The solution is to limit the cache scope to the top level apply() call
54// (i.e. we only keep cached objects alive while transforming).
55
56class SkColorSpaceXformer::AutoCachePurge {
57public:
58 AutoCachePurge(SkColorSpaceXformer* xformer)
59 : fXformer(xformer) {
60 fXformer->fReentryCount++;
61 }
62
63 ~AutoCachePurge() {
64 SkASSERT(fXformer->fReentryCount > 0);
65 if (--fXformer->fReentryCount == 0) {
66 fXformer->purgeCaches();
67 }
68 }
69
70private:
71 SkColorSpaceXformer* fXformer;
72};
73
74template <typename T>
75sk_sp<T> SkColorSpaceXformer::cachedApply(const T* src, Cache<T>* cache,
76 sk_sp<T> (*applyFunc)(const T*, SkColorSpaceXformer*)) {
77 if (!src) {
78 return nullptr;
79 }
80
81 auto key = sk_ref_sp(const_cast<T*>(src));
82 if (auto* xformed = cache->find(key)) {
83 return sk_ref_sp(xformed->get());
84 }
85
86 auto xformed = applyFunc(src, this);
87 cache->set(std::move(key), xformed);
88
89 return xformed;
90}
91
92void SkColorSpaceXformer::purgeCaches() {
93 fImageCache.reset();
94 fColorFilterCache.reset();
95 fImageFilterCache.reset();
96}
97
Matt Sarettcdc651d2017-03-30 12:41:48 -040098sk_sp<SkImage> SkColorSpaceXformer::apply(const SkImage* src) {
Florin Malita5449aad2017-07-10 11:14:40 -040099 const AutoCachePurge autoPurge(this);
100 return this->cachedApply<SkImage>(src, &fImageCache,
101 [](const SkImage* img, SkColorSpaceXformer* xformer) {
Brian Osmanb62f50c2018-07-12 14:44:27 -0400102 return img->makeColorSpace(xformer->fDst);
Florin Malita5449aad2017-07-10 11:14:40 -0400103 });
Matt Sarettcdc651d2017-03-30 12:41:48 -0400104}
105
Matt Sarette6844832017-04-03 10:35:42 -0400106sk_sp<SkImage> SkColorSpaceXformer::apply(const SkBitmap& src) {
Florin Malita5449aad2017-07-10 11:14:40 -0400107 const AutoCachePurge autoPurge(this);
Matt Sarette6844832017-04-03 10:35:42 -0400108 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(src, kNever_SkCopyPixelsMode);
109 if (!image) {
110 return nullptr;
111 }
112
Brian Osmanb62f50c2018-07-12 14:44:27 -0400113 sk_sp<SkImage> xformed = image->makeColorSpace(fDst);
Matt Sarette6844832017-04-03 10:35:42 -0400114 // We want to be sure we don't let the kNever_SkCopyPixelsMode image escape this stack frame.
115 SkASSERT(xformed != image);
116 return xformed;
117}
118
Matt Sarett6d72ed92017-04-10 16:35:33 -0400119sk_sp<SkColorFilter> SkColorSpaceXformer::apply(const SkColorFilter* colorFilter) {
Florin Malita5449aad2017-07-10 11:14:40 -0400120 const AutoCachePurge autoPurge(this);
121 return this->cachedApply<SkColorFilter>(colorFilter, &fColorFilterCache,
122 [](const SkColorFilter* f, SkColorSpaceXformer* xformer) {
123 return f->makeColorSpace(xformer);
124 });
Matt Sarett6d72ed92017-04-10 16:35:33 -0400125}
126
Mike Kleine908b942017-04-25 13:09:40 -0400127sk_sp<SkImageFilter> SkColorSpaceXformer::apply(const SkImageFilter* imageFilter) {
Florin Malita5449aad2017-07-10 11:14:40 -0400128 const AutoCachePurge autoPurge(this);
129 return this->cachedApply<SkImageFilter>(imageFilter, &fImageFilterCache,
130 [](const SkImageFilter* f, SkColorSpaceXformer* xformer) {
131 return f->makeColorSpace(xformer);
132 });
Mike Kleine908b942017-04-25 13:09:40 -0400133}
134
Mike Klein2814d912017-05-10 12:35:51 -0400135sk_sp<SkShader> SkColorSpaceXformer::apply(const SkShader* shader) {
Florin Malita5449aad2017-07-10 11:14:40 -0400136 const AutoCachePurge autoPurge(this);
Florin Malita4aed1382017-05-25 10:38:07 -0400137 return as_SB(shader)->makeColorSpace(this);
Mike Klein2814d912017-05-10 12:35:51 -0400138}
139
Matt Sarettcdc651d2017-03-30 12:41:48 -0400140void SkColorSpaceXformer::apply(SkColor* xformed, const SkColor* srgb, int n) {
141 SkAssertResult(fFromSRGB->apply(SkColorSpaceXform::kBGRA_8888_ColorFormat, xformed,
142 SkColorSpaceXform::kBGRA_8888_ColorFormat, srgb,
143 n, kUnpremul_SkAlphaType));
144}
145
146SkColor SkColorSpaceXformer::apply(SkColor srgb) {
147 SkColor xformed;
148 this->apply(&xformed, &srgb, 1);
149 return xformed;
150}
151
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400152SkPaint SkColorSpaceXformer::apply(const SkPaint& src) {
Florin Malita5449aad2017-07-10 11:14:40 -0400153 const AutoCachePurge autoPurge(this);
154
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400155 SkPaint dst = src;
Matt Sarettcdc651d2017-03-30 12:41:48 -0400156
157 // All SkColorSpaces have the same black point.
158 if (src.getColor() & 0xffffff) {
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400159 dst.setColor(this->apply(src.getColor()));
Matt Sarettcdc651d2017-03-30 12:41:48 -0400160 }
161
162 if (auto shader = src.getShader()) {
Mike Klein2814d912017-05-10 12:35:51 -0400163 dst.setShader(this->apply(shader));
Matt Sarettcdc651d2017-03-30 12:41:48 -0400164 }
165
Matt Sarettcdc651d2017-03-30 12:41:48 -0400166 if (auto cf = src.getColorFilter()) {
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400167 dst.setColorFilter(this->apply(cf));
Matt Sarettcdc651d2017-03-30 12:41:48 -0400168 }
169
170 if (auto looper = src.getDrawLooper()) {
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400171 dst.setDrawLooper(looper->makeColorSpace(this));
Matt Sarettcdc651d2017-03-30 12:41:48 -0400172 }
173
Matt Sarett31abf1f2017-04-07 16:54:04 -0400174 if (auto imageFilter = src.getImageFilter()) {
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400175 dst.setImageFilter(this->apply(imageFilter));
Matt Sarett31abf1f2017-04-07 16:54:04 -0400176 }
177
Matt Sarettc15bb7b2017-04-25 13:53:11 -0400178 return dst;
Matt Sarettcdc651d2017-03-30 12:41:48 -0400179}
Stan Iliev45fd9952017-12-18 14:46:30 -0500180
181SkCanvas::Lattice SkColorSpaceXformer::apply(const SkCanvas::Lattice& lattice,
182 SkColor* colorBuffer, int count) {
183 if (count) {
184 this->apply(colorBuffer, lattice.fColors, count);
185 return {lattice.fXDivs, lattice.fYDivs, lattice.fRectTypes,
186 lattice.fXCount, lattice.fYCount, lattice.fBounds, colorBuffer};
187 }
188
189 return lattice;
190}