blob: 888051dffc7fb534345d7f3ea9c7c3ef3463f78a [file] [log] [blame]
sugoi@google.com781cc762013-01-15 15:40:19 +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#include "SkDisplacementMapEffect.h"
10#include "SkBitmapSource.h"
11
12namespace skiagm {
13
14class DisplacementMapGM : public GM {
15public:
16 DisplacementMapGM() : fInitialized(false) {
17 this->setBGColor(0xFF000000);
18 }
19
20protected:
21 virtual SkString onShortName() {
22 return SkString("displacement");
23 }
24
25 void make_bitmap() {
26 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
27 fBitmap.allocPixels();
28 SkDevice device(fBitmap);
29 SkCanvas canvas(&device);
30 canvas.clear(0x00000000);
31 SkPaint paint;
32 paint.setAntiAlias(true);
33 paint.setColor(0xFF884422);
34 paint.setTextSize(SkIntToScalar(96));
35 const char* str = "g";
36 canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint);
37 }
38
39 void make_checkerboard() {
40 fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
41 fCheckerboard.allocPixels();
42 SkDevice device(fCheckerboard);
43 SkCanvas canvas(&device);
44 canvas.clear(0x00000000);
45 SkPaint darkPaint;
46 darkPaint.setColor(0xFF804020);
47 SkPaint lightPaint;
48 lightPaint.setColor(0xFF244484);
49 for (int y = 0; y < 80; y += 16) {
50 for (int x = 0; x < 80; x += 16) {
51 canvas.save();
52 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
53 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
54 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
55 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
56 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
57 canvas.restore();
58 }
59 }
60 }
61
62 virtual SkISize onISize() {
63 return make_isize(500, 200);
64 }
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +000065
sugoi@google.com781cc762013-01-15 15:40:19 +000066 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
67 canvas->save();
68 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
69 SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
70 canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
71 canvas->restore();
72 }
73
74 virtual void onDraw(SkCanvas* canvas) {
75 if (!fInitialized) {
76 make_bitmap();
77 make_checkerboard();
78 fInitialized = true;
79 }
80 canvas->clear(0x00000000);
81 SkPaint paint;
82 SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
83 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
84 (SkDisplacementMapEffect::kR_ChannelSelectorType,
85 SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ)))->unref();
86 drawClippedBitmap(canvas, 0, 0, paint);
87 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
88 (SkDisplacementMapEffect::kB_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +000089 SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000090 drawClippedBitmap(canvas, 100, 0, paint);
91 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
92 (SkDisplacementMapEffect::kR_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +000093 SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000094 drawClippedBitmap(canvas, 200, 0, paint);
95 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
96 (SkDisplacementMapEffect::kG_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +000097 SkDisplacementMapEffect::kA_ChannelSelectorType, 48.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000098 drawClippedBitmap(canvas, 300, 0, paint);
99 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
100 (SkDisplacementMapEffect::kR_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000101 SkDisplacementMapEffect::kA_ChannelSelectorType, 64.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000102 drawClippedBitmap(canvas, 400, 0, paint);
103
104 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
105 (SkDisplacementMapEffect::kR_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000106 SkDisplacementMapEffect::kG_ChannelSelectorType, 40.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000107 drawClippedBitmap(canvas, 0, 100, paint);
108 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
109 (SkDisplacementMapEffect::kB_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000110 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000111 drawClippedBitmap(canvas, 100, 100, paint);
112 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
113 (SkDisplacementMapEffect::kR_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000114 SkDisplacementMapEffect::kB_ChannelSelectorType, 40.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000115 drawClippedBitmap(canvas, 200, 100, paint);
116 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
117 (SkDisplacementMapEffect::kG_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000118 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000119 drawClippedBitmap(canvas, 300, 100, paint);
120 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
121 (SkDisplacementMapEffect::kR_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000122 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000123 drawClippedBitmap(canvas, 400, 100, paint);
124 }
125
126private:
127 typedef GM INHERITED;
128 SkBitmap fBitmap, fCheckerboard;
129 bool fInitialized;
130};
131
132//////////////////////////////////////////////////////////////////////////////
133
134static GM* MyFactory(void*) { return new DisplacementMapGM; }
135static GMRegistry reg(MyFactory);
136
137}