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