fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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.h" |
| 9 | |
| 10 | #include "SkImage.h" |
| 11 | #include "SkImageSource.h" |
| 12 | |
| 13 | // This GM exercises the SkImageSource ImageFilter class. |
| 14 | |
| 15 | class ImageSourceGM : public skiagm::GM { |
| 16 | public: |
| 17 | ImageSourceGM() { } |
| 18 | |
| 19 | protected: |
| 20 | SkString onShortName() override { |
| 21 | return SkString("imagesource"); |
| 22 | } |
| 23 | |
| 24 | SkISize onISize() override { return SkISize::Make(500, 150); } |
| 25 | |
| 26 | void onOnceBeforeDraw() override { |
| 27 | SkBitmap bm = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e"); |
| 28 | fImage.reset(SkImage::NewFromBitmap(bm)); |
| 29 | } |
| 30 | |
| 31 | static void FillRectFiltered(SkCanvas* canvas, const SkRect& clipRect, SkImageFilter* filter) { |
| 32 | SkPaint paint; |
| 33 | paint.setImageFilter(filter); |
| 34 | canvas->save(); |
| 35 | canvas->clipRect(clipRect); |
| 36 | canvas->drawPaint(paint); |
| 37 | canvas->restore(); |
| 38 | } |
| 39 | |
| 40 | void onDraw(SkCanvas* canvas) override { |
| 41 | canvas->clear(SK_ColorBLACK); |
| 42 | { |
| 43 | SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30); |
| 44 | SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60); |
| 45 | SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100); |
| 46 | SkRect bounds = SkRect::MakeIWH(fImage->width(), fImage->height()); |
| 47 | SkAutoTUnref<SkImageFilter> imageSource(SkImageSource::Create(fImage)); |
| 48 | SkAutoTUnref<SkImageFilter> imageSourceSrcRect( |
| 49 | SkImageSource::Create(fImage, srcRect, srcRect, kHigh_SkFilterQuality)); |
| 50 | SkAutoTUnref<SkImageFilter> imageSourceSrcRectDstRect( |
| 51 | SkImageSource::Create(fImage, srcRect, dstRect, kHigh_SkFilterQuality)); |
| 52 | SkAutoTUnref<SkImageFilter> imageSourceDstRectOnly( |
| 53 | SkImageSource::Create(fImage, bounds, dstRect, kHigh_SkFilterQuality)); |
| 54 | |
| 55 | // Draw an unscaled bitmap. |
| 56 | FillRectFiltered(canvas, clipRect, imageSource); |
| 57 | canvas->translate(SkIntToScalar(100), 0); |
| 58 | |
| 59 | // Draw an unscaled subset of the source bitmap (srcRect -> srcRect). |
| 60 | FillRectFiltered(canvas, clipRect, imageSourceSrcRect); |
| 61 | canvas->translate(SkIntToScalar(100), 0); |
| 62 | |
| 63 | // Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect). |
| 64 | FillRectFiltered(canvas, clipRect, imageSourceSrcRectDstRect); |
| 65 | canvas->translate(SkIntToScalar(100), 0); |
| 66 | |
| 67 | // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect). |
| 68 | FillRectFiltered(canvas, clipRect, imageSourceDstRectOnly); |
| 69 | canvas->translate(SkIntToScalar(100), 0); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | SkAutoTUnref<SkImage> fImage; |
| 75 | typedef GM INHERITED; |
| 76 | }; |
| 77 | |
| 78 | /////////////////////////////////////////////////////////////////////////////// |
| 79 | |
| 80 | DEF_GM( return new ImageSourceGM; ) |