blob: 9cf5a75cae3e7fac0b1261c7dde2009e2111efaf [file] [log] [blame]
senorblanco@chromium.orge93e1db2013-12-09 18:31:42 +00001/*
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 "SkBitmapSource.h"
11
12// This GM exercises the SkBitmapSource ImageFilter class.
13
14class BitmapSourceGM : public skiagm::GM {
15public:
16 BitmapSourceGM() {
17 }
18
19protected:
20 virtual SkString onShortName() SK_OVERRIDE {
21 return SkString("bitmapsource");
22 }
23
24 void makeBitmap() {
reed@google.comeb9a46c2014-01-25 16:46:20 +000025 fBitmap.allocN32Pixels(100, 100);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000026 SkCanvas canvas(fBitmap);
senorblanco@chromium.orge93e1db2013-12-09 18:31:42 +000027 canvas.clear(0x00000000);
28 SkPaint paint;
29 paint.setAntiAlias(true);
30 paint.setColor(0xFFFFFFFF);
31 paint.setTextSize(SkIntToScalar(96));
32 const char* str = "e";
33 canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
34 }
35
36 virtual SkISize onISize() SK_OVERRIDE { return SkISize::Make(500, 150); }
37
38 virtual void onOnceBeforeDraw() SK_OVERRIDE {
39 this->makeBitmap();
40 }
41
42 static void fillRectFiltered(SkCanvas* canvas, const SkRect& clipRect, SkImageFilter* filter) {
43 SkPaint paint;
44 paint.setImageFilter(filter);
45 canvas->save();
46 canvas->clipRect(clipRect);
47 canvas->drawPaint(paint);
48 canvas->restore();
49 }
50
51 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
52 canvas->clear(0x00000000);
53 {
54 SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30);
55 SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60);
56 SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100);
57 SkRect bounds;
58 fBitmap.getBounds(&bounds);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000059 SkAutoTUnref<SkImageFilter> bitmapSource(SkBitmapSource::Create(fBitmap));
60 SkAutoTUnref<SkImageFilter> bitmapSourceSrcRect(SkBitmapSource::Create(fBitmap, srcRect, srcRect));
61 SkAutoTUnref<SkImageFilter> bitmapSourceSrcRectDstRect(SkBitmapSource::Create(fBitmap, srcRect, dstRect));
62 SkAutoTUnref<SkImageFilter> bitmapSourceDstRectOnly(SkBitmapSource::Create(fBitmap, bounds, dstRect));
senorblanco@chromium.orge93e1db2013-12-09 18:31:42 +000063
64 // Draw an unscaled bitmap.
65 fillRectFiltered(canvas, clipRect, bitmapSource);
66 canvas->translate(SkIntToScalar(100), 0);
67
68 // Draw an unscaled subset of the source bitmap (srcRect -> srcRect).
69 fillRectFiltered(canvas, clipRect, bitmapSourceSrcRect);
70 canvas->translate(SkIntToScalar(100), 0);
71
72 // Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect).
73 fillRectFiltered(canvas, clipRect, bitmapSourceSrcRectDstRect);
74 canvas->translate(SkIntToScalar(100), 0);
75
76 // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect).
77 fillRectFiltered(canvas, clipRect, bitmapSourceDstRectOnly);
78 canvas->translate(SkIntToScalar(100), 0);
79 }
80 }
81
82private:
83 SkBitmap fBitmap;
84 typedef GM INHERITED;
85};
86
87///////////////////////////////////////////////////////////////////////////////
88
89DEF_GM( return new BitmapSourceGM; )