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 "modules/sksg/include/SkSGImage.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkImage.h"
12 #include "src/core/SkPaintPriv.h"
13
14 namespace sksg {
15
Image(sk_sp<SkImage> image)16 Image::Image(sk_sp<SkImage> image) : fImage(std::move(image)) {}
17
onRender(SkCanvas * canvas,const RenderContext * ctx) const18 void Image::onRender(SkCanvas* canvas, const RenderContext* ctx) const {
19 if (!fImage) {
20 return;
21 }
22
23 SkPaint paint;
24 paint.setAntiAlias(fAntiAlias);
25
26 sksg::RenderNode::ScopedRenderContext local_ctx(canvas, ctx);
27 if (ctx) {
28 if (ctx->fMaskShader) {
29 // Mask shaders cannot be applied via drawImage - we need layer isolation.
30 // TODO: remove after clipShader conversion.
31 local_ctx.setIsolation(this->bounds(), canvas->getTotalMatrix(), true);
32 }
33 local_ctx->modulatePaint(canvas->getTotalMatrix(), &paint);
34 }
35
36 canvas->drawImage(fImage, 0, 0, fSamplingOptions, &paint);
37 }
38
onNodeAt(const SkPoint & p) const39 const RenderNode* Image::onNodeAt(const SkPoint& p) const {
40 SkASSERT(this->bounds().contains(p.x(), p.y()));
41 return this;
42 }
43
onRevalidate(InvalidationController *,const SkMatrix & ctm)44 SkRect Image::onRevalidate(InvalidationController*, const SkMatrix& ctm) {
45 return fImage ? SkRect::Make(fImage->bounds()) : SkRect::MakeEmpty();
46 }
47
48 } // namespace sksg
49