| 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 | |
| 13 | namespace sksg { |
| 14 | |
| 15 | Image::Image(sk_sp<SkImage> image) : fImage(std::move(image)) {} |
| 16 | |
| 17 | void Image::onRender(SkCanvas* canvas, const RenderContext* ctx) const { |
| 18 | if (!fImage) { |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | SkPaint paint; |
| 23 | paint.setAntiAlias(fAntiAlias); |
| 24 | paint.setFilterQuality(fQuality); |
| 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, &paint); |
| 37 | } |
| 38 | |
| 39 | const RenderNode* Image::onNodeAt(const SkPoint& p) const { |
| 40 | SkASSERT(this->bounds().contains(p.x(), p.y())); |
| 41 | return this; |
| 42 | } |
| 43 | |
| 44 | SkRect Image::onRevalidate(InvalidationController*, const SkMatrix& ctm) { |
| 45 | return fImage ? SkRect::Make(fImage->bounds()) : SkRect::MakeEmpty(); |
| 46 | } |
| 47 | |
| 48 | } // namespace sksg |
| 49 | |