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" |
| 11 | #include "SkPaint.h" |
| 12 | |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 13 | namespace sksg { |
| 14 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 15 | RenderNode::RenderNode() : INHERITED(0) {} |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 16 | |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 17 | void RenderNode::render(SkCanvas* canvas, const RenderContext* ctx) const { |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 18 | SkASSERT(!this->hasInval()); |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 19 | this->onRender(canvas, ctx); |
| 20 | } |
| 21 | |
| 22 | bool RenderNode::RenderContext::modulatePaint(SkPaint* paint) const { |
| 23 | const auto initial_alpha = paint->getAlpha(), |
| 24 | alpha = SkToU8(sk_float_round2int(initial_alpha * fOpacity)); |
| 25 | |
Florin Malita | e1c9d3c | 2018-08-09 11:19:14 -0400 | [diff] [blame] | 26 | if (alpha != initial_alpha || fColorFilter) { |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 27 | paint->setAlpha(alpha); |
Florin Malita | e1c9d3c | 2018-08-09 11:19:14 -0400 | [diff] [blame] | 28 | paint->setColorFilter(SkColorFilter::MakeComposeFilter(fColorFilter, |
| 29 | paint->refColorFilter())); |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 30 | return true; |
| 31 | } |
| 32 | |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | RenderNode::ScopedRenderContext::ScopedRenderContext(SkCanvas* canvas, const RenderContext* ctx) |
| 37 | : fCanvas(canvas) |
| 38 | , fCtx(ctx) |
| 39 | , fRestoreCount(canvas->getSaveCount()) {} |
| 40 | |
| 41 | RenderNode::ScopedRenderContext::~ScopedRenderContext() { |
| 42 | if (fRestoreCount >= 0) { |
| 43 | fCanvas->restoreToCount(fRestoreCount); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | RenderNode::RenderContext* RenderNode::ScopedRenderContext::writableContext() { |
| 48 | // If no inherited context is present, allocate one in local storage. |
| 49 | if (!fCtx.get()) { |
| 50 | // N.B.: we have to force a copy while the default source is in scope. |
| 51 | // TODO: add SkTCopyOnWrite::init_copy() to simplify this |
| 52 | RenderContext default_ctx; |
| 53 | fCtx.init(default_ctx); |
| 54 | return fCtx.writable(); |
| 55 | } |
| 56 | return fCtx.writable(); |
| 57 | } |
| 58 | |
| 59 | RenderNode::ScopedRenderContext&& |
| 60 | RenderNode::ScopedRenderContext::modulateOpacity(float opacity) { |
| 61 | SkASSERT(opacity >= 0 && opacity <= 1); |
| 62 | if (opacity < 1) { |
| 63 | this->writableContext()->fOpacity *= opacity; |
| 64 | } |
| 65 | return std::move(*this); |
| 66 | } |
| 67 | |
| 68 | RenderNode::ScopedRenderContext&& |
Florin Malita | e1c9d3c | 2018-08-09 11:19:14 -0400 | [diff] [blame] | 69 | RenderNode::ScopedRenderContext::modulateColorFilter(sk_sp<SkColorFilter> cf) { |
| 70 | if (cf) { |
| 71 | auto* ctx = this->writableContext(); |
| 72 | ctx->fColorFilter = SkColorFilter::MakeComposeFilter(std::move(ctx->fColorFilter), cf); |
| 73 | } |
| 74 | return std::move(*this); |
| 75 | } |
| 76 | |
| 77 | RenderNode::ScopedRenderContext&& |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 78 | RenderNode::ScopedRenderContext::setIsolation(const SkRect& bounds, bool isolation) { |
| 79 | if (isolation && fCtx.get()) { |
| 80 | SkPaint layer_paint; |
| 81 | if (fCtx->modulatePaint(&layer_paint)) { |
| 82 | fCanvas->saveLayer(bounds, &layer_paint); |
| 83 | *fCtx.writable() = RenderContext(); |
| 84 | } |
| 85 | } |
| 86 | return std::move(*this); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace sksg |