Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 7d2ad0f

Browse files
lhkbobSkia Commit-Bot
authored andcommitted
Fix local matrix for crop rects in backdrop image filters
Bug: chromium:955467 Change-Id: I1c4cae2499db926aa6b629e8d730fcc8cb45be6d Reviewed-on: https://skia-review.googlesource.com/c/skia/+/212030 Reviewed-by: Robert Phillips <robertphillips@google.com> Commit-Queue: Michael Ludwig <michaelludwig@google.com>
1 parent 2f3637b commit 7d2ad0f

3 files changed

Lines changed: 87 additions & 3 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2019 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 "gm/gm.h"
9+
#include "include/core/SkCanvas.h"
10+
#include "include/effects/SkBlurImageFilter.h"
11+
#include "include/effects/SkColorFilterImageFilter.h"
12+
#include "include/core/SkPaint.h"
13+
14+
// This draws correctly if there's a small cyan rectangle above a much larger magenta rectangle.
15+
// There should be no red around the cyan rectangle and no green within the magenta rectangle.
16+
17+
DEF_SIMPLE_GM(backdrop_imagefilter_croprect, canvas, 600, 500) {
18+
// CTM translates to (150, 150)
19+
SkPoint origin = SkPoint::Make(150.f, 150.f);
20+
// The save layer specified after the CTM has negative coordinates, but
21+
// means that (100, 100) to (500, 250) in device-space will be saved
22+
SkRect clip = SkRect::MakeXYWH(-50.f, -50.f, 400.f, 150.f);
23+
// The image-filter crop relative to the CTM, which will map to
24+
// (200, 160) to (400, 190) in device space, or (100, 600) to (300, 90) in
25+
// the layer's image space.
26+
SkRect cropInLocal = SkRect::MakeLTRB(50.f, 10.f, 250.f, 40.f);
27+
28+
SkImageFilter::CropRect cropRect(cropInLocal);
29+
float matrix[20] = {-1.f, 0.f, 0.f, 0.f, 1.f,
30+
0.f, -1.f, 0.f, 0.f, 1.f,
31+
0.f, 0.f, -1.f, 0.f, 1.f,
32+
0.f, 0.f, 0.f, 1.f, 0.f};
33+
sk_sp<SkColorFilter> colorFilter = SkColorFilters::Matrix(matrix);
34+
sk_sp<SkImageFilter> imageFilter = SkColorFilterImageFilter::Make(colorFilter, nullptr,
35+
&cropRect);
36+
37+
SkPaint p;
38+
SkPaint l;
39+
l.setStyle(SkPaint::kStroke_Style);
40+
l.setStrokeWidth(0.f);
41+
42+
for (int i = 0; i < 2; ++i) {
43+
canvas->save();
44+
canvas->translate(origin.fX, origin.fY);
45+
46+
canvas->clipRect(clip);
47+
48+
if (i == 0) {
49+
// Primary save layer mode, so save layer before drawing the content
50+
SkPaint imfPaint;
51+
imfPaint.setImageFilter(imageFilter);
52+
canvas->saveLayer(nullptr, &imfPaint);
53+
} // else backdrop mode, so the content is drawn first
54+
55+
// Fill the clip with one color (cyan for i == 0 (inverse = red), and
56+
// magenta for i == 1 (inverse = green))
57+
p.setColor(i == 0 ? SK_ColorCYAN : SK_ColorMAGENTA);
58+
canvas->drawPaint(p);
59+
60+
// Then an inner rectangle with a color meant to be inverted by the image filter
61+
p.setColor(i == 0 ? SK_ColorRED : SK_ColorGREEN);
62+
canvas->drawRect(cropInLocal, p);
63+
64+
if (i == 1) {
65+
// Backdrop mode, so save a layer using the image filter as the backdrop to filter
66+
// content on initialization.
67+
canvas->saveLayer({nullptr, nullptr, imageFilter.get(), nullptr, nullptr,
68+
SkCanvas::kInitWithPrevious_SaveLayerFlag});
69+
}
70+
71+
// Restore the saved layer (either a main layer that was just drawn into and needs to be
72+
// filtered, or an "empty" layer initialized with the previously filtered backdrop)
73+
canvas->restore();
74+
75+
// Move down
76+
canvas->restore();
77+
origin.fY += 150.f;
78+
}
79+
}

gn/gm.gni

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ gm_sources = [
2727
"$_gm/atlastext.cpp",
2828
"$_gm/b_119394958.cpp",
2929
"$_gm/backdrop.cpp",
30+
"$_gm/backdrop_imagefilter_croprect.cpp",
3031
"$_gm/badpaint.cpp",
3132
"$_gm/beziereffects.cpp",
3233
"$_gm/beziers.cpp",

src/core/SkCanvas.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -938,13 +938,17 @@ void SkCanvas::DrawDeviceWithFilter(SkBaseDevice* src, const SkImageFilter* filt
938938
draw.fMatrix = &SkMatrix::I();
939939
draw.fRC = &rc;
940940

941+
int x = src->getOrigin().x() - dstOrigin.x();
942+
int y = src->getOrigin().y() - dstOrigin.y();
943+
941944
SkPaint p;
942945
if (filter) {
943-
p.setImageFilter(filter->makeWithLocalMatrix(ctm));
946+
SkMatrix actm = ctm;
947+
// Account for the origin offset in the local matrix
948+
actm.postTranslate(x, y);
949+
p.setImageFilter(filter->makeWithLocalMatrix(actm));
944950
}
945951

946-
int x = src->getOrigin().x() - dstOrigin.x();
947-
int y = src->getOrigin().y() - dstOrigin.y();
948952
auto special = src->snapSpecial();
949953
if (special) {
950954
dst->drawSpecial(special.get(), x, y, p, nullptr, SkMatrix::I());

0 commit comments

Comments
 (0)