| 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 "include/core/SkCanvas.h" |
| 9 |
#include "include/core/SkPaint.h" |
| 10 |
#include "include/core/SkPath.h" |
| 11 |
#include "include/core/SkPathBuilder.h" |
| 12 |
#include "include/core/SkPathUtils.h" |
| 13 |
#include "include/core/SkPoint.h" |
| 14 |
#include "include/private/base/SkAssert.h" |
| 15 |
#include "modules/sksg/include/SkSGDraw.h" |
| 16 |
#include "modules/sksg/include/SkSGGeometryNode.h" |
| 17 |
#include "modules/sksg/include/SkSGNode.h" |
| 18 |
#include "modules/sksg/include/SkSGPaint.h" |
| 19 |
|
| 20 |
class SkMatrix; |
| 21 |
|
| 22 |
namespace sksg { |
| 23 |
|
| 24 |
Draw::Draw(sk_sp<GeometryNode> geometry, sk_sp<PaintNode> paint) |
| 25 |
: fGeometry(std::move(geometry)) |
| 26 |
, fPaint(std::move(paint)) { |
| 27 |
this->observeInval(fGeometry); |
| 28 |
this->observeInval(fPaint); |
| 29 |
} |
| 30 |
|
| 31 |
Draw::~Draw() { |
| 32 |
this->unobserveInval(fGeometry); |
| 33 |
this->unobserveInval(fPaint); |
| 34 |
} |
| 35 |
|
| 36 |
void Draw::onRender(SkCanvas* canvas, const RenderContext* ctx) const { |
| 37 |
auto paint = fPaint->makePaint(); |
| 38 |
if (ctx) { |
| 39 |
ctx->modulatePaint(canvas->getTotalMatrix(), &paint); |
| 40 |
} |
| 41 |
|
| 42 |
const auto skipDraw = paint.nothingToDraw() || |
| 43 |
(paint.getStyle() == SkPaint::kStroke_Style && paint.getStrokeWidth() <= 0); |
| 44 |
|
| 45 |
if (!skipDraw) { |
| 46 |
fGeometry->draw(canvas, paint); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
const RenderNode* Draw::onNodeAt(const SkPoint& p) const { |
| 51 |
const auto paint = fPaint->makePaint(); |
| 52 |
|
| 53 |
if (!paint.getAlpha()) { |
| 54 |
return nullptr; |
| 55 |
} |
| 56 |
|
| 57 |
if (paint.getStyle() == SkPaint::Style::kFill_Style && fGeometry->contains(p)) { |
| 58 |
return this; |
| 59 |
} |
| 60 |
|
| 61 |
SkPathBuilder stroke_path; |
| 62 |
if (!skpathutils::FillPathWithPaint(fGeometry->asPath(), paint, &stroke_path)) { |
| 63 |
return nullptr; |
| 64 |
} |
| 65 |
|
| 66 |
// todo: can we shared code (via SkPathRaw) for the impl of contains() in builder? |
| 67 |
return stroke_path.detach().contains(p.x(), p.y()) ? this : nullptr; |
| 68 |
} |
| 69 |
|
| 70 |
SkRect Draw::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) { |
| 71 |
SkASSERT(this->hasInval()); |
| 72 |
|
| 73 |
auto bounds = fGeometry->revalidate(ic, ctm); |
| 74 |
fPaint->revalidate(ic, ctm); |
| 75 |
|
| 76 |
const auto paint = fPaint->makePaint(); |
| 77 |
SkASSERT(paint.canComputeFastBounds()); |
| 78 |
|
| 79 |
return paint.computeFastBounds(bounds, &bounds); |
| 80 |
} |
| 81 |
|
| 82 |
} // namespace sksg |