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 "include/private/base/SkAssert.h"
9 #include "modules/sksg/include/SkSGOpacityEffect.h"
10
11 class SkCanvas;
12 class SkMatrix;
13 struct SkPoint;
14
15 namespace sksg {
16 class InvalidationController;
17
OpacityEffect(sk_sp<RenderNode> child,float opacity)18 OpacityEffect::OpacityEffect(sk_sp<RenderNode> child, float opacity)
19 : INHERITED(std::move(child))
20 , fOpacity(opacity) {}
21
onRender(SkCanvas * canvas,const RenderContext * ctx) const22 void OpacityEffect::onRender(SkCanvas* canvas, const RenderContext* ctx) const {
23 // opacity <= 0 disables rendering
24 if (fOpacity <= 0)
25 return;
26
27 // opacity >= 1 has no effect
28 if (fOpacity >= 1) {
29 this->INHERITED::onRender(canvas, ctx);
30 return;
31 }
32
33 const auto local_context = ScopedRenderContext(canvas, ctx).modulateOpacity(fOpacity);
34
35 this->INHERITED::onRender(canvas, local_context);
36 }
37
onNodeAt(const SkPoint & p) const38 const RenderNode* OpacityEffect::onNodeAt(const SkPoint& p) const {
39 return (fOpacity > 0) ? this->INHERITED::onNodeAt(p) : nullptr;
40 }
41
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)42 SkRect OpacityEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
43 SkASSERT(this->hasInval());
44
45 // opacity <= 0 disables rendering AND revalidation for the sub-DAG
46 return fOpacity > 0 ? this->INHERITED::onRevalidate(ic, ctm) : SkRect::MakeEmpty();
47 }
48
49 } // namespace sksg
50