blob: 63be6c387166660f22c03bcc98c3baad3ecac90a [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();
junov@google.comdbfac8a2012-12-06 21:47:40 +000022 bm.eraseColor(SK_ColorTRANSPARENT);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000023 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[] = {
senorblanco@chromium.org35c733c2013-05-28 19:43:05 +000032 SK_ColorTRANSPARENT, SK_ColorGREEN, SK_ColorCYAN,
33 SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000034 };
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
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000057static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
58 SkPaint paint;
reed@google.comeb0fa292011-12-12 22:01:06 +000059 paint.setTextSize(SkIntToScalar(24));
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000060 paint.setAntiAlias(true);
61 for (int i = 0; i < 4; ++i) {
62 SkString str;
63 str.appendScalar(k[i]);
64 SkScalar width = paint.measureText(str.c_str(), str.size());
reed@google.comeb0fa292011-12-12 22:01:06 +000065 canvas->drawText(str.c_str(), str.size(), x, y + paint.getTextSize(), paint);
66 x += width + SkIntToScalar(10);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000067 }
68}
69
70class ArithmodeGM : public skiagm::GM {
71public:
72 ArithmodeGM () {}
73
74protected:
75
76 virtual SkString onShortName() {
77 return SkString("arithmode");
78 }
79
80 virtual SkISize onISize() { return SkISize::Make(640, 480); }
81
82 virtual void onDraw(SkCanvas* canvas) {
83 SkBitmap src = make_src();
84 SkBitmap dst = make_dst();
rmistry@google.comae933ce2012-08-23 18:19:56 +000085
reed@google.comeb0fa292011-12-12 22:01:06 +000086 const SkScalar one = SK_Scalar1;
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000087 static const SkScalar K[] = {
88 0, 0, 0, 0,
reed@google.comeb0fa292011-12-12 22:01:06 +000089 0, 0, 0, one,
90 0, one, 0, 0,
91 0, 0, one, 0,
92 0, one, one, 0,
93 0, one, -one, 0,
94 0, one/2, one/2, 0,
95 0, one/2, one/2, one/4,
96 0, one/2, one/2, -one/4,
97 one/4, one/2, one/2, 0,
98 -one/4, one/2, one/2, 0,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000099 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000101 const SkScalar* k = K;
102 const SkScalar* stop = k + SK_ARRAY_COUNT(K);
103 SkScalar y = 0;
reed@google.comeb856302011-12-12 22:15:18 +0000104 SkScalar gap = SkIntToScalar(src.width() + 20);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000105 while (k < stop) {
106 SkScalar x = 0;
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000107 canvas->drawBitmap(src, x, y, NULL);
108 x += gap;
109 canvas->drawBitmap(dst, x, y, NULL);
110 x += gap;
senorblanco@chromium.org9a6eb0e2013-05-29 20:55:09 +0000111 SkRect rect = SkRect::MakeXYWH(x, y, SkIntToScalar(WW), SkIntToScalar(HH));
112 canvas->saveLayer(&rect, NULL);
113 canvas->drawBitmap(dst, x, y, NULL);
114 SkXfermode* xfer = SkArithmeticMode::Create(k[0], k[1], k[2], k[3]);
115 SkPaint paint;
116 paint.setXfermode(xfer)->unref();
117 canvas->drawBitmap(src, x, y, &paint);
118 canvas->restore();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000119 x += gap;
120 show_k_text(canvas, x, y, k);
121 k += 4;
reed@google.comeb856302011-12-12 22:15:18 +0000122 y += SkIntToScalar(src.height() + 12);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000123 }
124 }
125
126private:
127 typedef GM INHERITED;
128};
129
130///////////////////////////////////////////////////////////////////////////////
131
132static skiagm::GM* MyFactory(void*) { return new ArithmodeGM; }
133static skiagm::GMRegistry reg(MyFactory);