Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 1 | /* |
| 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 "SkSGRenderNode.h" |
| 9 | |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 10 | #include "SkCanvas.h" |
Florin Malita | dafd652 | 2019-02-10 01:49:46 +0000 | [diff] [blame] | 11 | #include "SkImageFilter.h" |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 12 | #include "SkPaint.h" |
| 13 | |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 14 | namespace sksg { |
| 15 | |
Florin Malita | ef26fcb | 2019-02-10 12:54:20 -0500 | [diff] [blame] | 16 | RenderNode::RenderNode(uint32_t inval_traits) : INHERITED(inval_traits) {} |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 17 | |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 18 | void RenderNode::render(SkCanvas* canvas, const RenderContext* ctx) const { |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 19 | SkASSERT(!this->hasInval()); |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 20 | this->onRender(canvas, ctx); |
| 21 | } |
| 22 | |
Florin Malita | eb46bd8 | 2019-02-12 09:33:21 -0500 | [diff] [blame] | 23 | const RenderNode* RenderNode::nodeAt(const SkPoint& p) const { |
| 24 | return this->bounds().contains(p.x(), p.y()) ? this->onNodeAt(p) : nullptr; |
| 25 | } |
| 26 | |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 27 | bool RenderNode::RenderContext::modulatePaint(SkPaint* paint) const { |
| 28 | const auto initial_alpha = paint->getAlpha(), |
| 29 | alpha = SkToU8(sk_float_round2int(initial_alpha * fOpacity)); |
| 30 | |
Florin Malita | e1c9d3c | 2018-08-09 11:19:14 -0400 | [diff] [blame] | 31 | if (alpha != initial_alpha || fColorFilter) { |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 32 | paint->setAlpha(alpha); |
Florin Malita | e1c9d3c | 2018-08-09 11:19:14 -0400 | [diff] [blame] | 33 | paint->setColorFilter(SkColorFilter::MakeComposeFilter(fColorFilter, |
| 34 | paint->refColorFilter())); |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 35 | return true; |
| 36 | } |
| 37 | |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | RenderNode::ScopedRenderContext::ScopedRenderContext(SkCanvas* canvas, const RenderContext* ctx) |
| 42 | : fCanvas(canvas) |
Florin Malita | 679d05c | 2018-08-10 12:50:08 -0400 | [diff] [blame] | 43 | , fCtx(ctx ? *ctx : RenderContext()) |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 44 | , fRestoreCount(canvas->getSaveCount()) {} |
| 45 | |
| 46 | RenderNode::ScopedRenderContext::~ScopedRenderContext() { |
| 47 | if (fRestoreCount >= 0) { |
| 48 | fCanvas->restoreToCount(fRestoreCount); |
| 49 | } |
| 50 | } |
| 51 | |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 52 | RenderNode::ScopedRenderContext&& |
| 53 | RenderNode::ScopedRenderContext::modulateOpacity(float opacity) { |
| 54 | SkASSERT(opacity >= 0 && opacity <= 1); |
Florin Malita | 679d05c | 2018-08-10 12:50:08 -0400 | [diff] [blame] | 55 | fCtx.fOpacity *= opacity; |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 56 | return std::move(*this); |
| 57 | } |
| 58 | |
| 59 | RenderNode::ScopedRenderContext&& |
Florin Malita | e1c9d3c | 2018-08-09 11:19:14 -0400 | [diff] [blame] | 60 | RenderNode::ScopedRenderContext::modulateColorFilter(sk_sp<SkColorFilter> cf) { |
Florin Malita | 679d05c | 2018-08-10 12:50:08 -0400 | [diff] [blame] | 61 | fCtx.fColorFilter = SkColorFilter::MakeComposeFilter(std::move(fCtx.fColorFilter), |
| 62 | std::move(cf)); |
Florin Malita | e1c9d3c | 2018-08-09 11:19:14 -0400 | [diff] [blame] | 63 | return std::move(*this); |
| 64 | } |
| 65 | |
| 66 | RenderNode::ScopedRenderContext&& |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 67 | RenderNode::ScopedRenderContext::setIsolation(const SkRect& bounds, bool isolation) { |
Florin Malita | 679d05c | 2018-08-10 12:50:08 -0400 | [diff] [blame] | 68 | if (isolation) { |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 69 | SkPaint layer_paint; |
Florin Malita | 679d05c | 2018-08-10 12:50:08 -0400 | [diff] [blame] | 70 | if (fCtx.modulatePaint(&layer_paint)) { |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 71 | fCanvas->saveLayer(bounds, &layer_paint); |
Florin Malita | 679d05c | 2018-08-10 12:50:08 -0400 | [diff] [blame] | 72 | fCtx = RenderContext(); |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | return std::move(*this); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Florin Malita | dafd652 | 2019-02-10 01:49:46 +0000 | [diff] [blame] | 78 | RenderNode::ScopedRenderContext&& |
| 79 | RenderNode::ScopedRenderContext::setFilterIsolation(const SkRect& bounds, |
| 80 | sk_sp<SkImageFilter> filter) { |
| 81 | SkPaint layer_paint; |
| 82 | fCtx.modulatePaint(&layer_paint); |
| 83 | |
| 84 | SkASSERT(!layer_paint.getImageFilter()); |
| 85 | layer_paint.setImageFilter(std::move(filter)); |
| 86 | fCanvas->saveLayer(bounds, &layer_paint); |
| 87 | fCtx = RenderContext(); |
| 88 | |
| 89 | return std::move(*this); |
| 90 | } |
| 91 | |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 92 | } // namespace sksg |