blob: 452eb4945cb91f464eca7b10efa64e20b2f9770b [file] [log] [blame]
senorblanco@chromium.orgb2ee33c2012-08-22 16:24:44 +00001/*
2 * Copyright 2012 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 "SkBlendImageFilter.h"
10#include "SkBitmapSource.h"
11
12namespace skiagm {
13
14class ImageBlendGM : public GM {
15public:
16 ImageBlendGM() : fInitialized(false) {
17 this->setBGColor(0xFF000000);
18 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000019
senorblanco@chromium.orgb2ee33c2012-08-22 16:24:44 +000020protected:
21 virtual SkString onShortName() {
22 return SkString("blend");
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(0xD000D000);
34 paint.setTextSize(SkIntToScalar(96));
35 const char* str = "e";
36 canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), 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(0xFF404040);
47 SkPaint lightPaint;
48 lightPaint.setColor(0xFFA0A0A0);
49 for (int y = 0; y < 80; y += 16) {
50 for (int x = 0; x < 80; x += 16) {
51 canvas.save();
robertphillips@google.com94acc702012-09-06 18:43:21 +000052 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
senorblanco@chromium.orgb2ee33c2012-08-22 16:24:44 +000053 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, 100);
64 }
65
junov@chromium.orgff06af22013-01-14 16:27:50 +000066 void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x) {
67 canvas->save();
68 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), 0,
69 SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
70 canvas->drawBitmap(fBitmap, SkIntToScalar(x), 0, &paint);
71 canvas->restore();
72 }
73
senorblanco@chromium.orgb2ee33c2012-08-22 16:24:44 +000074 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> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
83 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter::kNormal_Mode, background)))->unref();
junov@chromium.orgff06af22013-01-14 16:27:50 +000084 drawClippedBitmap(canvas, paint, 0);
senorblanco@chromium.orgb2ee33c2012-08-22 16:24:44 +000085 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter::kMultiply_Mode, background)))->unref();
junov@chromium.orgff06af22013-01-14 16:27:50 +000086 drawClippedBitmap(canvas, paint, 100);
senorblanco@chromium.orgb2ee33c2012-08-22 16:24:44 +000087 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter::kScreen_Mode, background)))->unref();
junov@chromium.orgff06af22013-01-14 16:27:50 +000088 drawClippedBitmap(canvas, paint, 200);
senorblanco@chromium.orgb2ee33c2012-08-22 16:24:44 +000089 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter::kDarken_Mode, background)))->unref();
junov@chromium.orgff06af22013-01-14 16:27:50 +000090 drawClippedBitmap(canvas, paint, 300);
senorblanco@chromium.orgb2ee33c2012-08-22 16:24:44 +000091 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter::kLighten_Mode, background)))->unref();
junov@chromium.orgff06af22013-01-14 16:27:50 +000092 drawClippedBitmap(canvas, paint, 400);
senorblanco@chromium.orgb2ee33c2012-08-22 16:24:44 +000093 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
senorblanco@chromium.orgb2ee33c2012-08-22 16:24:44 +000095private:
96 typedef GM INHERITED;
97 SkBitmap fBitmap, fCheckerboard;
98 bool fInitialized;
99};
100
101//////////////////////////////////////////////////////////////////////////////
102
103static GM* MyFactory(void*) { return new ImageBlendGM; }
104static GMRegistry reg(MyFactory);
105
106}