blob: 49770628aee0ccc8c4151c0cb3bf89cca8b44884 [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
41static uint16_t gBG[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
42
43class XfermodesGM : public GM {
reed@android.comdd0ac282009-06-20 02:38:16 +000044 SkBitmap fBG;
45 SkBitmap fSrcB, fDstB;
46
epoger@google.com4eed3402011-06-24 19:14:42 +000047 void draw_mode(SkCanvas* canvas, SkXfermode* mode, int alpha,
reed@google.comf3c1cc92010-12-23 16:45:33 +000048 SkScalar x, SkScalar y) {
reed@android.comdd0ac282009-06-20 02:38:16 +000049 SkPaint p;
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000050
epoger@google.com4eed3402011-06-24 19:14:42 +000051 canvas->drawBitmap(fSrcB, x, y, &p);
52 p.setAlpha(alpha);
reed@android.comdd0ac282009-06-20 02:38:16 +000053 p.setXfermode(mode);
epoger@google.com4eed3402011-06-24 19:14:42 +000054 canvas->drawBitmap(fDstB, x, y, &p);
reed@android.comdd0ac282009-06-20 02:38:16 +000055 }
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000056
reed@android.comdd0ac282009-06-20 02:38:16 +000057public:
reed@google.comf3c1cc92010-12-23 16:45:33 +000058 const static int W = 64;
59 const static int H = 64;
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000060 XfermodesGM() {
61 // Do all this work in a temporary so we get a deep copy,
62 // especially of gBG.
63 SkBitmap scratchBitmap;
64 scratchBitmap.setConfig(SkBitmap::kARGB_4444_Config, 2, 2, 4);
65 scratchBitmap.setPixels(gBG);
66 scratchBitmap.setIsOpaque(true);
67 scratchBitmap.copyTo(&fBG, SkBitmap::kARGB_4444_Config);
68
reed@android.comdd0ac282009-06-20 02:38:16 +000069 make_bitmaps(W, H, &fSrcB, &fDstB);
70 }
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000071
reed@android.comdd0ac282009-06-20 02:38:16 +000072protected:
reed@android.com048522d2009-06-23 12:19:41 +000073 virtual SkString onShortName() {
reed@android.com8015dd82009-06-21 00:49:18 +000074 return SkString("xfermodes");
75 }
76
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000077 virtual SkISize onISize() {
epoger@google.com4eed3402011-06-24 19:14:42 +000078 return make_isize(790, 640);
reed@android.com048522d2009-06-23 12:19:41 +000079 }
reed@android.comdd0ac282009-06-20 02:38:16 +000080
reed@android.comdd0ac282009-06-20 02:38:16 +000081 virtual void onDraw(SkCanvas* canvas) {
reed@google.comf3c1cc92010-12-23 16:45:33 +000082 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000083
reed@android.comdd0ac282009-06-20 02:38:16 +000084 const struct {
reed@google.comf3c1cc92010-12-23 16:45:33 +000085 SkXfermode::Mode fMode;
epoger@google.com4eed3402011-06-24 19:14:42 +000086 const char* fLabel;
reed@android.comdd0ac282009-06-20 02:38:16 +000087 } gModes[] = {
epoger@google.com4eed3402011-06-24 19:14:42 +000088 { SkXfermode::kClear_Mode, "Clear" },
89 { SkXfermode::kSrc_Mode, "Src" },
90 { SkXfermode::kDst_Mode, "Dst" },
91 { SkXfermode::kSrcOver_Mode, "SrcOver" },
92 { SkXfermode::kDstOver_Mode, "DstOver" },
93 { SkXfermode::kSrcIn_Mode, "SrcIn" },
94 { SkXfermode::kDstIn_Mode, "DstIn" },
95 { SkXfermode::kSrcOut_Mode, "SrcOut" },
96 { SkXfermode::kDstOut_Mode, "DstOut" },
97 { SkXfermode::kSrcATop_Mode, "SrcATop" },
98 { SkXfermode::kDstATop_Mode, "DstATop" },
99 { SkXfermode::kXor_Mode, "Xor" },
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000100
epoger@google.com4eed3402011-06-24 19:14:42 +0000101 { SkXfermode::kPlus_Mode, "Plus" },
102 { SkXfermode::kMultiply_Mode, "Multiply" },
103 { SkXfermode::kScreen_Mode, "Screen" },
104 { SkXfermode::kOverlay_Mode, "Overlay" },
105 { SkXfermode::kDarken_Mode, "Darken" },
106 { SkXfermode::kLighten_Mode, "Lighten" },
107 { SkXfermode::kColorDodge_Mode, "ColorDodge" },
108 { SkXfermode::kColorBurn_Mode, "ColorBurn" },
109 { SkXfermode::kHardLight_Mode, "HardLight" },
110 { SkXfermode::kSoftLight_Mode, "SoftLight" },
111 { SkXfermode::kDifference_Mode, "Difference" },
112 { SkXfermode::kExclusion_Mode, "Exclusion" },
reed@android.comdd0ac282009-06-20 02:38:16 +0000113 };
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000114
reed@google.comf3c1cc92010-12-23 16:45:33 +0000115 const SkScalar w = SkIntToScalar(W);
116 const SkScalar h = SkIntToScalar(H);
reed@android.comdd0ac282009-06-20 02:38:16 +0000117 SkShader* s = SkShader::CreateBitmapShader(fBG,
118 SkShader::kRepeat_TileMode,
119 SkShader::kRepeat_TileMode);
120 SkMatrix m;
121 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
122 s->setLocalMatrix(m);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000123
reed@android.comdd0ac282009-06-20 02:38:16 +0000124 SkPaint labelP;
125 labelP.setAntiAlias(true);
126 labelP.setTextAlign(SkPaint::kCenter_Align);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000127
reed@android.comc9c64222009-06-23 00:09:12 +0000128 const int W = 5;
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000129
reed@android.comdd0ac282009-06-20 02:38:16 +0000130 SkScalar x0 = 0;
epoger@google.com4eed3402011-06-24 19:14:42 +0000131 for (int twice = 0; twice < 2; twice++) {
132 SkScalar x = x0, y = 0;
reed@android.comdd0ac282009-06-20 02:38:16 +0000133 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
reed@android.comc9c64222009-06-23 00:09:12 +0000134 SkXfermode* mode = SkXfermode::Create(gModes[i].fMode);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000135 SkAutoUnref aur(mode);
reed@android.comdd0ac282009-06-20 02:38:16 +0000136 SkRect r;
137 r.set(x, y, x+w, y+h);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000138
reed@google.comf3c1cc92010-12-23 16:45:33 +0000139 SkPaint p;
reed@android.comdd0ac282009-06-20 02:38:16 +0000140 p.setStyle(SkPaint::kFill_Style);
141 p.setShader(s);
reed@android.comdd0ac282009-06-20 02:38:16 +0000142 canvas->drawRect(r, p);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000143
reed@google.comf3c1cc92010-12-23 16:45:33 +0000144 canvas->saveLayer(&r, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
epoger@google.com4eed3402011-06-24 19:14:42 +0000145 draw_mode(canvas, mode, twice ? 0x88 : 0xFF, r.fLeft, r.fTop);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000146 canvas->restore();
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000147
reed@google.comf3c1cc92010-12-23 16:45:33 +0000148 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
149 p.setStyle(SkPaint::kStroke_Style);
150 p.setShader(NULL);
151 canvas->drawRect(r, p);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000152
reed@google.comf3c1cc92010-12-23 16:45:33 +0000153#if 1
reed@android.comdd0ac282009-06-20 02:38:16 +0000154 canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
155 x + w/2, y - labelP.getTextSize()/2, labelP);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000156#endif
reed@android.comdd0ac282009-06-20 02:38:16 +0000157 x += w + SkIntToScalar(10);
reed@android.comc9c64222009-06-23 00:09:12 +0000158 if ((i % W) == W - 1) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000159 x = x0;
160 y += h + SkIntToScalar(30);
161 }
162 }
epoger@google.com4eed3402011-06-24 19:14:42 +0000163 x0 += SkIntToScalar(400);
reed@android.comdd0ac282009-06-20 02:38:16 +0000164 }
165 s->unref();
166 }
167
168private:
169 typedef GM INHERITED;
170};
171
172//////////////////////////////////////////////////////////////////////////////
173
174static GM* MyFactory(void*) { return new XfermodesGM; }
175static GMRegistry reg(MyFactory);
176
177}