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