blob: ea015c6427b3d952be8ded24a75fa673b1aefd34 [file] [log] [blame]
mike@reedtribe.orge51755f2011-12-10 19:36:56 +00001/*
2 * Copyright 2011 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 "SkCanvas.h"
10#include "SkColorPriv.h"
11#include "SkShader.h"
12
13#include "SkArithmeticMode.h"
14#include "SkGradientShader.h"
15#define WW 100
16#define HH 32
17
18static SkBitmap make_bm() {
19 SkBitmap bm;
20 bm.setConfig(SkBitmap::kARGB_8888_Config, WW, HH);
21 bm.allocPixels();
22 bm.eraseColor(0);
23 return bm;
24}
25
26static SkBitmap make_src() {
27 SkBitmap bm = make_bm();
28 SkCanvas canvas(bm);
29 SkPaint paint;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000030 SkPoint pts[] = { {0, 0}, {SkIntToScalar(WW), SkIntToScalar(HH)} };
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000031 SkColor colors[] = {
32 SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN,
33 SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE
34 };
35 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors),
36 SkShader::kClamp_TileMode);
37 paint.setShader(s)->unref();
38 canvas.drawPaint(paint);
39 return bm;
40}
41
42static SkBitmap make_dst() {
43 SkBitmap bm = make_bm();
44 SkCanvas canvas(bm);
45 SkPaint paint;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000046 SkPoint pts[] = { {0, SkIntToScalar(HH)}, {SkIntToScalar(WW), 0} };
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000047 SkColor colors[] = {
48 SK_ColorBLUE, SK_ColorYELLOW, SK_ColorBLACK, SK_ColorGREEN, SK_ColorGRAY
49 };
50 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors),
51 SkShader::kClamp_TileMode);
52 paint.setShader(s)->unref();
53 canvas.drawPaint(paint);
54 return bm;
55}
56
57static SkBitmap make_arith(const SkBitmap& src, const SkBitmap& dst,
58 const SkScalar k[]) {
59 SkBitmap bm = make_bm();
60 SkCanvas canvas(bm);
61 SkPaint paint;
62 canvas.drawBitmap(dst, 0, 0, NULL);
63 SkXfermode* xfer = SkArithmeticMode::Create(k[0], k[1], k[2], k[3]);
64 paint.setXfermode(xfer)->unref();
65 canvas.drawBitmap(src, 0, 0, &paint);
66 return bm;
67}
68
69static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
70 SkPaint paint;
reed@google.comeb0fa292011-12-12 22:01:06 +000071 paint.setTextSize(SkIntToScalar(24));
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000072 paint.setAntiAlias(true);
73 for (int i = 0; i < 4; ++i) {
74 SkString str;
75 str.appendScalar(k[i]);
76 SkScalar width = paint.measureText(str.c_str(), str.size());
reed@google.comeb0fa292011-12-12 22:01:06 +000077 canvas->drawText(str.c_str(), str.size(), x, y + paint.getTextSize(), paint);
78 x += width + SkIntToScalar(10);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000079 }
80}
81
82class ArithmodeGM : public skiagm::GM {
83public:
84 ArithmodeGM () {}
85
86protected:
87
88 virtual SkString onShortName() {
89 return SkString("arithmode");
90 }
91
92 virtual SkISize onISize() { return SkISize::Make(640, 480); }
93
94 virtual void onDraw(SkCanvas* canvas) {
95 SkBitmap src = make_src();
96 SkBitmap dst = make_dst();
97
reed@google.comeb0fa292011-12-12 22:01:06 +000098 const SkScalar one = SK_Scalar1;
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000099 static const SkScalar K[] = {
100 0, 0, 0, 0,
reed@google.comeb0fa292011-12-12 22:01:06 +0000101 0, 0, 0, one,
102 0, one, 0, 0,
103 0, 0, one, 0,
104 0, one, one, 0,
105 0, one, -one, 0,
106 0, one/2, one/2, 0,
107 0, one/2, one/2, one/4,
108 0, one/2, one/2, -one/4,
109 one/4, one/2, one/2, 0,
110 -one/4, one/2, one/2, 0,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000111 };
112
113 const SkScalar* k = K;
114 const SkScalar* stop = k + SK_ARRAY_COUNT(K);
115 SkScalar y = 0;
116 SkScalar x = 0;
reed@google.comeb856302011-12-12 22:15:18 +0000117 SkScalar gap = SkIntToScalar(src.width() + 20);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000118 while (k < stop) {
119 SkScalar x = 0;
120 SkBitmap res = make_arith(src, dst, k);
121 canvas->drawBitmap(src, x, y, NULL);
122 x += gap;
123 canvas->drawBitmap(dst, x, y, NULL);
124 x += gap;
125 canvas->drawBitmap(res, x, y, NULL);
126 x += gap;
127 show_k_text(canvas, x, y, k);
128 k += 4;
reed@google.comeb856302011-12-12 22:15:18 +0000129 y += SkIntToScalar(src.height() + 12);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000130 }
131 }
132
133private:
134 typedef GM INHERITED;
135};
136
137///////////////////////////////////////////////////////////////////////////////
138
139static skiagm::GM* MyFactory(void*) { return new ArithmodeGM; }
140static skiagm::GMRegistry reg(MyFactory);