1 /*
2 * Copyright 2021 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/effects/SkImageFilters.h"
9 #include "modules/svg/include/SkSVGFeImage.h"
10 #include "modules/svg/include/SkSVGFilterContext.h"
11 #include "modules/svg/include/SkSVGImage.h"
12 #include "modules/svg/include/SkSVGRenderContext.h"
13 #include "modules/svg/include/SkSVGValue.h"
14
parseAndSetAttribute(const char * n,const char * v)15 bool SkSVGFeImage::parseAndSetAttribute(const char* n, const char* v) {
16 return INHERITED::parseAndSetAttribute(n, v) ||
17 this->setHref(SkSVGAttributeParser::parse<SkSVGIRI>("xlink:href", n, v)) ||
18 this->setPreserveAspectRatio(SkSVGAttributeParser::parse<SkSVGPreserveAspectRatio>(
19 "preserveAspectRatio", n, v));
20 }
21
onMakeImageFilter(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx) const22 sk_sp<SkImageFilter> SkSVGFeImage::onMakeImageFilter(const SkSVGRenderContext& ctx,
23 const SkSVGFilterContext& fctx) const {
24 // Load image and map viewbox (image bounds) to viewport (filter effects subregion).
25 const SkRect viewport = this->resolveFilterSubregion(ctx, fctx);
26 const auto imgInfo =
27 SkSVGImage::LoadImage(ctx.resourceProvider(), fHref, viewport, fPreserveAspectRatio);
28 if (!imgInfo.fImage) {
29 return nullptr;
30 }
31
32 // Create the image filter mapped according to aspect ratio
33 const SkRect srcRect = SkRect::Make(imgInfo.fImage->bounds());
34 const SkRect& dstRect = imgInfo.fDst;
35 // TODO: image-rendering property
36 const SkFilterQuality fq = SkFilterQuality::kMedium_SkFilterQuality;
37 auto imgfilt = SkImageFilters::Image(imgInfo.fImage, srcRect, dstRect, SkSamplingOptions(fq));
38
39 // Aspect ratio mapping may end up drawing content outside of the filter effects region,
40 // so perform an explicit crop.
41 return SkImageFilters::Merge(&imgfilt, 1, fctx.filterEffectsRegion());
42 }
43