blob: ed7aba1c989f926a226a38ce78c060836895e739 [file] [log] [blame]
fmalitacd56f812015-09-14 13:31:18 -07001/*
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
robertphillips549c8992016-04-01 09:28:51 -070015static void fill_rect_filtered(SkCanvas* canvas,
16 const SkRect& clipRect,
17 sk_sp<SkImageFilter> filter) {
18 SkPaint paint;
19 paint.setImageFilter(std::move(filter));
20 canvas->save();
21 canvas->clipRect(clipRect);
22 canvas->drawPaint(paint);
23 canvas->restore();
24}
25
fmalitacd56f812015-09-14 13:31:18 -070026class ImageSourceGM : public skiagm::GM {
27public:
28 ImageSourceGM() { }
29
30protected:
31 SkString onShortName() override {
32 return SkString("imagesource");
33 }
34
35 SkISize onISize() override { return SkISize::Make(500, 150); }
36
37 void onOnceBeforeDraw() override {
38 SkBitmap bm = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
reed9ce9d672016-03-17 10:51:11 -070039 fImage = SkImage::MakeFromBitmap(bm);
fmalitacd56f812015-09-14 13:31:18 -070040 }
41
fmalitacd56f812015-09-14 13:31:18 -070042 void onDraw(SkCanvas* canvas) override {
43 canvas->clear(SK_ColorBLACK);
robertphillips549c8992016-04-01 09:28:51 -070044
45 const SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30);
46 const SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60);
47 const SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100);
48 const SkRect bounds = SkRect::MakeIWH(fImage->width(), fImage->height());
49
fmalitacd56f812015-09-14 13:31:18 -070050 {
fmalitacd56f812015-09-14 13:31:18 -070051 // Draw an unscaled bitmap.
robertphillips549c8992016-04-01 09:28:51 -070052 sk_sp<SkImageFilter> imageSource(SkImageSource::Make(fImage));
53 fill_rect_filtered(canvas, clipRect, std::move(imageSource));
fmalitacd56f812015-09-14 13:31:18 -070054 canvas->translate(SkIntToScalar(100), 0);
robertphillips549c8992016-04-01 09:28:51 -070055 }
56 {
fmalitacd56f812015-09-14 13:31:18 -070057 // Draw an unscaled subset of the source bitmap (srcRect -> srcRect).
robertphillips549c8992016-04-01 09:28:51 -070058 sk_sp<SkImageFilter> imageSourceSrcRect(
59 SkImageSource::Make(fImage, srcRect, srcRect, kHigh_SkFilterQuality));
60 fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRect));
fmalitacd56f812015-09-14 13:31:18 -070061 canvas->translate(SkIntToScalar(100), 0);
robertphillips549c8992016-04-01 09:28:51 -070062 }
63 {
fmalitacd56f812015-09-14 13:31:18 -070064 // Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect).
robertphillips549c8992016-04-01 09:28:51 -070065 sk_sp<SkImageFilter> imageSourceSrcRectDstRect(
66 SkImageSource::Make(fImage, srcRect, dstRect, kHigh_SkFilterQuality));
67 fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRectDstRect));
fmalitacd56f812015-09-14 13:31:18 -070068 canvas->translate(SkIntToScalar(100), 0);
robertphillips549c8992016-04-01 09:28:51 -070069 }
70 {
fmalitacd56f812015-09-14 13:31:18 -070071 // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect).
robertphillips549c8992016-04-01 09:28:51 -070072 sk_sp<SkImageFilter> imageSourceDstRectOnly(
73 SkImageSource::Make(fImage, bounds, dstRect, kHigh_SkFilterQuality));
74 fill_rect_filtered(canvas, clipRect, std::move(imageSourceDstRectOnly));
fmalitacd56f812015-09-14 13:31:18 -070075 canvas->translate(SkIntToScalar(100), 0);
76 }
77 }
78
79private:
reed9ce9d672016-03-17 10:51:11 -070080 sk_sp<SkImage> fImage;
fmalitacd56f812015-09-14 13:31:18 -070081 typedef GM INHERITED;
82};
83
84///////////////////////////////////////////////////////////////////////////////
85
86DEF_GM( return new ImageSourceGM; )