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