blob: 50d818b2fabe30a77ce436613cd2c73b1e16a7d6 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
fmalitacd56f812015-09-14 13:31:18 -070010
11#include "SkImage.h"
12#include "SkImageSource.h"
13
14// This GM exercises the SkImageSource ImageFilter class.
15
robertphillips549c8992016-04-01 09:28:51 -070016static void fill_rect_filtered(SkCanvas* canvas,
17 const SkRect& clipRect,
18 sk_sp<SkImageFilter> filter) {
19 SkPaint paint;
20 paint.setImageFilter(std::move(filter));
21 canvas->save();
22 canvas->clipRect(clipRect);
23 canvas->drawPaint(paint);
24 canvas->restore();
25}
26
fmalitacd56f812015-09-14 13:31:18 -070027class ImageSourceGM : public skiagm::GM {
28public:
29 ImageSourceGM() { }
30
31protected:
32 SkString onShortName() override {
33 return SkString("imagesource");
34 }
35
36 SkISize onISize() override { return SkISize::Make(500, 150); }
37
38 void onOnceBeforeDraw() override {
39 SkBitmap bm = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
reed9ce9d672016-03-17 10:51:11 -070040 fImage = SkImage::MakeFromBitmap(bm);
fmalitacd56f812015-09-14 13:31:18 -070041 }
42
fmalitacd56f812015-09-14 13:31:18 -070043 void onDraw(SkCanvas* canvas) override {
44 canvas->clear(SK_ColorBLACK);
robertphillips549c8992016-04-01 09:28:51 -070045
46 const SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30);
47 const SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60);
48 const SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100);
49 const SkRect bounds = SkRect::MakeIWH(fImage->width(), fImage->height());
50
fmalitacd56f812015-09-14 13:31:18 -070051 {
fmalitacd56f812015-09-14 13:31:18 -070052 // Draw an unscaled bitmap.
robertphillips549c8992016-04-01 09:28:51 -070053 sk_sp<SkImageFilter> imageSource(SkImageSource::Make(fImage));
54 fill_rect_filtered(canvas, clipRect, std::move(imageSource));
fmalitacd56f812015-09-14 13:31:18 -070055 canvas->translate(SkIntToScalar(100), 0);
robertphillips549c8992016-04-01 09:28:51 -070056 }
57 {
fmalitacd56f812015-09-14 13:31:18 -070058 // Draw an unscaled subset of the source bitmap (srcRect -> srcRect).
robertphillips549c8992016-04-01 09:28:51 -070059 sk_sp<SkImageFilter> imageSourceSrcRect(
60 SkImageSource::Make(fImage, srcRect, srcRect, kHigh_SkFilterQuality));
61 fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRect));
fmalitacd56f812015-09-14 13:31:18 -070062 canvas->translate(SkIntToScalar(100), 0);
robertphillips549c8992016-04-01 09:28:51 -070063 }
64 {
fmalitacd56f812015-09-14 13:31:18 -070065 // Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect).
robertphillips549c8992016-04-01 09:28:51 -070066 sk_sp<SkImageFilter> imageSourceSrcRectDstRect(
67 SkImageSource::Make(fImage, srcRect, dstRect, kHigh_SkFilterQuality));
68 fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRectDstRect));
fmalitacd56f812015-09-14 13:31:18 -070069 canvas->translate(SkIntToScalar(100), 0);
robertphillips549c8992016-04-01 09:28:51 -070070 }
71 {
fmalitacd56f812015-09-14 13:31:18 -070072 // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect).
robertphillips549c8992016-04-01 09:28:51 -070073 sk_sp<SkImageFilter> imageSourceDstRectOnly(
74 SkImageSource::Make(fImage, bounds, dstRect, kHigh_SkFilterQuality));
75 fill_rect_filtered(canvas, clipRect, std::move(imageSourceDstRectOnly));
fmalitacd56f812015-09-14 13:31:18 -070076 canvas->translate(SkIntToScalar(100), 0);
77 }
78 }
79
80private:
reed9ce9d672016-03-17 10:51:11 -070081 sk_sp<SkImage> fImage;
fmalitacd56f812015-09-14 13:31:18 -070082 typedef GM INHERITED;
83};
84
85///////////////////////////////////////////////////////////////////////////////
86
87DEF_GM( return new ImageSourceGM; )