Home | History | Annotate | Download | only in effects
      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 "SkSGOpacityEffect.h"
      9 
     10 #include "SkCanvas.h"
     11 
     12 #include <math.h>
     13 
     14 namespace sksg {
     15 
     16 OpacityEffect::OpacityEffect(sk_sp<RenderNode> child, float opacity)
     17     : INHERITED(std::move(child))
     18     , fOpacity(opacity) {}
     19 
     20 void OpacityEffect::onRender(SkCanvas* canvas) const {
     21     // opacity <= 0 disables rendering
     22     if (fOpacity <= 0)
     23         return;
     24 
     25     // TODO: we could avoid savelayer if there is no more than one drawing primitive
     26     //       in the sub-DAG.
     27     SkAutoCanvasRestore acr(canvas, false);
     28     if (fOpacity < 1) {
     29         canvas->saveLayerAlpha(&this->bounds(), roundf(fOpacity * 255));
     30     }
     31 
     32     this->INHERITED::onRender(canvas);
     33 }
     34 
     35 SkRect OpacityEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
     36     SkASSERT(this->hasInval());
     37 
     38     // opacity <= 0 disables rendering AND revalidation for the sub-DAG
     39     return fOpacity > 0 ? this->INHERITED::onRevalidate(ic, ctm) : SkRect::MakeEmpty();
     40 }
     41 
     42 } // namespace sksg
     43