|
| 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 | +} |
0 commit comments