Florin Malita | 36216fb | 2018-08-07 11:09:49 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 "SkSGColorFilter.h" |
| 9 | |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkColorFilter.h" |
| 12 | #include "SkSGColor.h" |
| 13 | |
| 14 | namespace sksg { |
| 15 | |
| 16 | ColorFilter::ColorFilter(sk_sp<RenderNode> child) |
| 17 | : INHERITED(std::move(child)) {} |
| 18 | |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame^] | 19 | void ColorFilter::onRender(SkCanvas* canvas, const RenderContext* ctx) const { |
Florin Malita | 36216fb | 2018-08-07 11:09:49 -0400 | [diff] [blame] | 20 | if (this->bounds().isEmpty()) |
| 21 | return; |
| 22 | |
| 23 | SkAutoCanvasRestore acr(canvas, false); |
| 24 | |
| 25 | if (fColorFilter) { |
| 26 | SkPaint p; |
| 27 | p.setColorFilter(fColorFilter); |
| 28 | canvas->saveLayer(this->bounds(), &p); |
| 29 | } |
| 30 | |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame^] | 31 | this->INHERITED::onRender(canvas, ctx); |
Florin Malita | 36216fb | 2018-08-07 11:09:49 -0400 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | ColorModeFilter::ColorModeFilter(sk_sp<RenderNode> child, sk_sp<Color> color, SkBlendMode mode) |
| 35 | : INHERITED(std::move(child)) |
| 36 | , fColor(std::move(color)) |
| 37 | , fMode(mode) { |
| 38 | this->observeInval(fColor); |
| 39 | } |
| 40 | |
| 41 | ColorModeFilter::~ColorModeFilter() { |
| 42 | this->unobserveInval(fColor); |
| 43 | } |
| 44 | |
| 45 | SkRect ColorModeFilter::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) { |
| 46 | SkASSERT(this->hasInval()); |
| 47 | |
| 48 | fColor->revalidate(ic, ctm); |
| 49 | fColorFilter = SkColorFilter::MakeModeFilter(fColor->getColor(), fMode); |
| 50 | |
| 51 | return this->INHERITED::onRevalidate(ic, ctm); |
| 52 | } |
| 53 | |
| 54 | } // namespace sksg |