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