blob: 42b8a6a8ab535b69b950ec949e9f1d6487bd52e1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comdd0ac282009-06-20 02:38:16 +00008#include "gm.h"
9#include "SkBitmap.h"
10#include "SkShader.h"
11#include "SkXfermode.h"
12
13namespace skiagm {
14
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000015static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst) {
reed@android.comdd0ac282009-06-20 02:38:16 +000016 src->setConfig(SkBitmap::kARGB_8888_Config, w, h);
17 src->allocPixels();
18 src->eraseColor(0);
19
20 SkCanvas c(*src);
21 SkPaint p;
22 SkRect r;
23 SkScalar ww = SkIntToScalar(w);
24 SkScalar hh = SkIntToScalar(h);
25
26 p.setAntiAlias(true);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000027 p.setColor(0xFFFFCC44);
reed@android.comdd0ac282009-06-20 02:38:16 +000028 r.set(0, 0, ww*3/4, hh*3/4);
29 c.drawOval(r, p);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000030
reed@android.comdd0ac282009-06-20 02:38:16 +000031 dst->setConfig(SkBitmap::kARGB_8888_Config, w, h);
32 dst->allocPixels();
33 dst->eraseColor(0);
34 c.setBitmapDevice(*dst);
35
36 p.setColor(0xFF66AAFF);
37 r.set(ww/3, hh/3, ww*19/20, hh*19/20);
38 c.drawRect(r, p);
39}
40
reed@android.comdd0ac282009-06-20 02:38:16 +000041class XfermodesGM : public GM {
reed@android.comdd0ac282009-06-20 02:38:16 +000042 SkBitmap fBG;
43 SkBitmap fSrcB, fDstB;
reed@google.com5213c042011-12-20 13:41:09 +000044 bool fOnce;
reed@android.comdd0ac282009-06-20 02:38:16 +000045
epoger@google.com4eed3402011-06-24 19:14:42 +000046 void draw_mode(SkCanvas* canvas, SkXfermode* mode, int alpha,
reed@google.comf3c1cc92010-12-23 16:45:33 +000047 SkScalar x, SkScalar y) {
reed@android.comdd0ac282009-06-20 02:38:16 +000048 SkPaint p;
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000049
epoger@google.com4eed3402011-06-24 19:14:42 +000050 canvas->drawBitmap(fSrcB, x, y, &p);
51 p.setAlpha(alpha);
reed@android.comdd0ac282009-06-20 02:38:16 +000052 p.setXfermode(mode);
epoger@google.com4eed3402011-06-24 19:14:42 +000053 canvas->drawBitmap(fDstB, x, y, &p);
reed@android.comdd0ac282009-06-20 02:38:16 +000054 }
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000055
reed@google.com5213c042011-12-20 13:41:09 +000056 void init() {
57 if (!fOnce) {
reed@google.com263cb3a2011-12-20 14:35:32 +000058 // Do all this work in a temporary so we get a deep copy
59 uint16_t localData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
reed@google.com5213c042011-12-20 13:41:09 +000060 SkBitmap scratchBitmap;
61 scratchBitmap.setConfig(SkBitmap::kARGB_4444_Config, 2, 2, 4);
reed@google.com263cb3a2011-12-20 14:35:32 +000062 scratchBitmap.setPixels(localData);
reed@google.com5213c042011-12-20 13:41:09 +000063 scratchBitmap.setIsOpaque(true);
64 scratchBitmap.copyTo(&fBG, SkBitmap::kARGB_4444_Config);
65
66 make_bitmaps(W, H, &fSrcB, &fDstB);
67 fOnce = true;
68 }
69 }
70
reed@android.comdd0ac282009-06-20 02:38:16 +000071public:
reed@google.comf3c1cc92010-12-23 16:45:33 +000072 const static int W = 64;
73 const static int H = 64;
reed@google.com5213c042011-12-20 13:41:09 +000074 XfermodesGM() : fOnce(false) {}
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000075
reed@android.comdd0ac282009-06-20 02:38:16 +000076protected:
reed@android.com048522d2009-06-23 12:19:41 +000077 virtual SkString onShortName() {
reed@android.com8015dd82009-06-21 00:49:18 +000078 return SkString("xfermodes");
79 }
80
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000081 virtual SkISize onISize() {
epoger@google.com4eed3402011-06-24 19:14:42 +000082 return make_isize(790, 640);
reed@android.com048522d2009-06-23 12:19:41 +000083 }
reed@android.comdd0ac282009-06-20 02:38:16 +000084
reed@android.comdd0ac282009-06-20 02:38:16 +000085 virtual void onDraw(SkCanvas* canvas) {
reed@google.com5213c042011-12-20 13:41:09 +000086 this->init();
87
reed@google.comf3c1cc92010-12-23 16:45:33 +000088 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000089
reed@android.comdd0ac282009-06-20 02:38:16 +000090 const struct {
reed@google.comf3c1cc92010-12-23 16:45:33 +000091 SkXfermode::Mode fMode;
epoger@google.com4eed3402011-06-24 19:14:42 +000092 const char* fLabel;
reed@android.comdd0ac282009-06-20 02:38:16 +000093 } gModes[] = {
epoger@google.com4eed3402011-06-24 19:14:42 +000094 { SkXfermode::kClear_Mode, "Clear" },
95 { SkXfermode::kSrc_Mode, "Src" },
96 { SkXfermode::kDst_Mode, "Dst" },
97 { SkXfermode::kSrcOver_Mode, "SrcOver" },
98 { SkXfermode::kDstOver_Mode, "DstOver" },
99 { SkXfermode::kSrcIn_Mode, "SrcIn" },
100 { SkXfermode::kDstIn_Mode, "DstIn" },
101 { SkXfermode::kSrcOut_Mode, "SrcOut" },
102 { SkXfermode::kDstOut_Mode, "DstOut" },
103 { SkXfermode::kSrcATop_Mode, "SrcATop" },
104 { SkXfermode::kDstATop_Mode, "DstATop" },
105 { SkXfermode::kXor_Mode, "Xor" },
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000106
epoger@google.com4eed3402011-06-24 19:14:42 +0000107 { SkXfermode::kPlus_Mode, "Plus" },
108 { SkXfermode::kMultiply_Mode, "Multiply" },
109 { SkXfermode::kScreen_Mode, "Screen" },
110 { SkXfermode::kOverlay_Mode, "Overlay" },
111 { SkXfermode::kDarken_Mode, "Darken" },
112 { SkXfermode::kLighten_Mode, "Lighten" },
113 { SkXfermode::kColorDodge_Mode, "ColorDodge" },
114 { SkXfermode::kColorBurn_Mode, "ColorBurn" },
115 { SkXfermode::kHardLight_Mode, "HardLight" },
116 { SkXfermode::kSoftLight_Mode, "SoftLight" },
117 { SkXfermode::kDifference_Mode, "Difference" },
118 { SkXfermode::kExclusion_Mode, "Exclusion" },
reed@android.comdd0ac282009-06-20 02:38:16 +0000119 };
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000120
reed@google.comf3c1cc92010-12-23 16:45:33 +0000121 const SkScalar w = SkIntToScalar(W);
122 const SkScalar h = SkIntToScalar(H);
reed@android.comdd0ac282009-06-20 02:38:16 +0000123 SkShader* s = SkShader::CreateBitmapShader(fBG,
124 SkShader::kRepeat_TileMode,
125 SkShader::kRepeat_TileMode);
126 SkMatrix m;
127 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
128 s->setLocalMatrix(m);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000129
reed@android.comdd0ac282009-06-20 02:38:16 +0000130 SkPaint labelP;
131 labelP.setAntiAlias(true);
132 labelP.setTextAlign(SkPaint::kCenter_Align);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000133
reed@android.comc9c64222009-06-23 00:09:12 +0000134 const int W = 5;
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000135
reed@android.comdd0ac282009-06-20 02:38:16 +0000136 SkScalar x0 = 0;
epoger@google.com4eed3402011-06-24 19:14:42 +0000137 for (int twice = 0; twice < 2; twice++) {
138 SkScalar x = x0, y = 0;
reed@android.comdd0ac282009-06-20 02:38:16 +0000139 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
reed@android.comc9c64222009-06-23 00:09:12 +0000140 SkXfermode* mode = SkXfermode::Create(gModes[i].fMode);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000141 SkAutoUnref aur(mode);
reed@android.comdd0ac282009-06-20 02:38:16 +0000142 SkRect r;
143 r.set(x, y, x+w, y+h);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000144
reed@google.comf3c1cc92010-12-23 16:45:33 +0000145 SkPaint p;
reed@android.comdd0ac282009-06-20 02:38:16 +0000146 p.setStyle(SkPaint::kFill_Style);
147 p.setShader(s);
reed@android.comdd0ac282009-06-20 02:38:16 +0000148 canvas->drawRect(r, p);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000149
reed@google.comf3c1cc92010-12-23 16:45:33 +0000150 canvas->saveLayer(&r, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
epoger@google.com4eed3402011-06-24 19:14:42 +0000151 draw_mode(canvas, mode, twice ? 0x88 : 0xFF, r.fLeft, r.fTop);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000152 canvas->restore();
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000153
reed@google.comf3c1cc92010-12-23 16:45:33 +0000154 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
155 p.setStyle(SkPaint::kStroke_Style);
156 p.setShader(NULL);
157 canvas->drawRect(r, p);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000158
reed@google.comf3c1cc92010-12-23 16:45:33 +0000159#if 1
reed@android.comdd0ac282009-06-20 02:38:16 +0000160 canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
161 x + w/2, y - labelP.getTextSize()/2, labelP);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000162#endif
reed@android.comdd0ac282009-06-20 02:38:16 +0000163 x += w + SkIntToScalar(10);
reed@android.comc9c64222009-06-23 00:09:12 +0000164 if ((i % W) == W - 1) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000165 x = x0;
166 y += h + SkIntToScalar(30);
167 }
168 }
epoger@google.com4eed3402011-06-24 19:14:42 +0000169 x0 += SkIntToScalar(400);
reed@android.comdd0ac282009-06-20 02:38:16 +0000170 }
171 s->unref();
172 }
173
174private:
175 typedef GM INHERITED;
176};
177
178//////////////////////////////////////////////////////////////////////////////
179
180static GM* MyFactory(void*) { return new XfermodesGM; }
181static GMRegistry reg(MyFactory);
182
183}