Home | History | Annotate | Download | only in src
      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 "SkSGDraw.h"
      9 
     10 #include "SkSGGeometryNode.h"
     11 #include "SkSGInvalidationController.h"
     12 #include "SkSGPaintNode.h"
     13 #include "SkTLazy.h"
     14 
     15 namespace sksg {
     16 
     17 Draw::Draw(sk_sp<GeometryNode> geometry, sk_sp<PaintNode> paint)
     18     : fGeometry(std::move(geometry))
     19     , fPaint(std::move(paint)) {
     20     this->observeInval(fGeometry);
     21     this->observeInval(fPaint);
     22 }
     23 
     24 Draw::~Draw() {
     25     this->unobserveInval(fGeometry);
     26     this->unobserveInval(fPaint);
     27 }
     28 
     29 void Draw::onRender(SkCanvas* canvas, const RenderContext* ctx) const {
     30     auto paint = fPaint->makePaint();
     31     if (ctx) {
     32         ctx->modulatePaint(&paint);
     33     }
     34 
     35     const auto skipDraw = paint.nothingToDraw() ||
     36             (paint.getStyle() == SkPaint::kStroke_Style && paint.getStrokeWidth() <= 0);
     37 
     38     if (!skipDraw) {
     39         fGeometry->draw(canvas, paint);
     40     }
     41 }
     42 
     43 SkRect Draw::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
     44     SkASSERT(this->hasInval());
     45 
     46     auto bounds = fGeometry->revalidate(ic, ctm);
     47     fPaint->revalidate(ic, ctm);
     48 
     49     const auto paint = fPaint->makePaint();
     50     SkASSERT(paint.canComputeFastBounds());
     51 
     52     return paint.computeFastBounds(bounds, &bounds);
     53 }
     54 
     55 } // namespace sksg
     56