jvanverth | 8355100 | 2015-07-03 05:48:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 9 | #include "SkBitmapSource.h" |
| 10 | #include "SkColorFilter.h" |
| 11 | #include "SkColorFilterImageFilter.h" |
| 12 | #include "SkDisplacementMapEffect.h" |
| 13 | #include "SkTileImageFilter.h" |
| 14 | #include "SkXfermode.h" |
| 15 | #include "gm.h" |
| 16 | |
| 17 | namespace skiagm { |
| 18 | |
| 19 | // This tests the image filter graph: |
| 20 | // |
| 21 | // BitmapSource1 -- all red 512x512 |
| 22 | // | |
| 23 | // ColorFilterImageFilter -- with a 64x64 crop rect - makes the pixels green |
| 24 | // | |
| 25 | // TileImageFilter -- which tiles the 64x64 green pixels across 512x512 |
| 26 | // | |
| 27 | // | BitmapSource1 -- all red 512x512 |
| 28 | // | displacement | color |
| 29 | // | | |
| 30 | // DisplacementMapEffect -- this is only necessary to preserve the clip in the computed bounds |
| 31 | // TileImageFilter by itself bloats the bounds to include the src |
| 32 | // It has the TileImageFilter as the offset input. |
| 33 | // |
| 34 | // What was going on was that the clipRect being applied to the draw (64, 64, 512, 512) |
| 35 | // was eliminating the "displacement" chain due to the crop rect. |
| 36 | // This reproduces crbug/499499 |
| 37 | class CroppedDisplacementGM : public GM { |
| 38 | public: |
| 39 | CroppedDisplacementGM() { } |
| 40 | |
| 41 | protected: |
| 42 | |
| 43 | SkString onShortName() override { |
| 44 | return SkString("cropped-displacement"); |
| 45 | } |
| 46 | |
| 47 | SkISize onISize() override { |
| 48 | return SkISize::Make(kWidth, kHeight); |
| 49 | } |
| 50 | |
| 51 | void onOnceBeforeDraw() override { |
| 52 | fRedBitmap.allocN32Pixels(kWidth, kHeight); |
| 53 | SkCanvas canvas(fRedBitmap); |
| 54 | canvas.clear(SK_ColorRED); |
| 55 | } |
| 56 | |
| 57 | void onDraw(SkCanvas* canvas) override { |
| 58 | |
| 59 | SkPaint p; |
| 60 | |
| 61 | const SkRect smRect = SkRect::MakeWH(SkIntToScalar(kSmallSize), SkIntToScalar(kSmallSize)); |
| 62 | SkImageFilter::CropRect cr(smRect); |
| 63 | |
| 64 | SkAutoTUnref<SkBitmapSource> bms(SkBitmapSource::Create(fRedBitmap)); |
| 65 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorGREEN, |
| 66 | SkXfermode::kSrc_Mode)); |
| 67 | SkAutoTUnref<SkColorFilterImageFilter> cfif(SkColorFilterImageFilter::Create(cf, bms, &cr)); |
| 68 | |
| 69 | SkAutoTUnref<SkTileImageFilter> tif(SkTileImageFilter::Create( |
| 70 | SkRect::MakeWH(SkIntToScalar(kSmallSize), SkIntToScalar(kSmallSize)), |
| 71 | SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)), |
| 72 | cfif)); |
| 73 | |
| 74 | static const SkScalar kScale = 20.0f; |
| 75 | |
| 76 | SkAutoTUnref<SkDisplacementMapEffect> dif(SkDisplacementMapEffect::Create( |
| 77 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 78 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 79 | kScale, |
| 80 | tif, bms)); |
| 81 | |
| 82 | p.setImageFilter(dif); |
| 83 | |
| 84 | canvas->clipRect(SkRect::MakeLTRB(kSmallSize+kScale/2.0f, |
| 85 | kSmallSize+kScale/2.0f, |
| 86 | SkIntToScalar(kWidth), SkIntToScalar(kHeight))); |
| 87 | canvas->saveLayer(NULL, &p); |
| 88 | canvas->restore(); |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | static const int kWidth = 512; |
| 93 | static const int kHeight = 512; |
| 94 | static const int kSmallSize = 64; |
| 95 | |
| 96 | SkBitmap fRedBitmap; |
| 97 | |
| 98 | typedef GM INHERITED; |
| 99 | }; |
| 100 | |
| 101 | ////////////////////////////////////////////////////////////////////////////// |
| 102 | |
| 103 | DEF_GM( return SkNEW(CroppedDisplacementGM); ) |
| 104 | |
| 105 | } |