1 /*
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 "sk_tool_utils.h"
10
11 namespace skiagm {
12
make_bm(SkBitmap * bm)13 static void make_bm(SkBitmap* bm) {
14 const SkColor colors[4] = {
15 SK_ColorRED, SK_ColorGREEN,
16 SK_ColorBLUE, SK_ColorWHITE
17 };
18 SkPMColor colorsPM[4];
19 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) {
20 colorsPM[i] = SkPreMultiplyColor(colors[i]);
21 }
22 SkColorTable* ctable = new SkColorTable(colorsPM, 4);
23
24 bm->allocPixels(SkImageInfo::Make(2, 2, kIndex_8_SkColorType,
25 kPremul_SkAlphaType),
26 NULL, ctable);
27 ctable->unref();
28
29 *bm->getAddr8(0, 0) = 0;
30 *bm->getAddr8(1, 0) = 1;
31 *bm->getAddr8(0, 1) = 2;
32 *bm->getAddr8(1, 1) = 3;
33 }
34
draw_bm(SkCanvas * canvas,const SkBitmap & bm,SkScalar x,SkScalar y,SkPaint * paint)35 static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm,
36 SkScalar x, SkScalar y, SkPaint* paint) {
37 canvas->drawBitmap(bm, x, y, paint);
38 return SkIntToScalar(bm.width()) * 5/4;
39 }
40
draw_set(SkCanvas * c,const SkBitmap & bm,SkScalar x,SkPaint * p)41 static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x,
42 SkPaint* p) {
43 x += draw_bm(c, bm, x, 0, p);
44 p->setFilterQuality(kLow_SkFilterQuality);
45 x += draw_bm(c, bm, x, 0, p);
46 p->setDither(true);
47 return x + draw_bm(c, bm, x, 0, p);
48 }
49
draw_row(SkCanvas * canvas,const SkBitmap & bm)50 static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) {
51 SkAutoCanvasRestore acr(canvas, true);
52
53 SkPaint paint;
54 SkScalar x = 0;
55 const int scale = 32;
56
57 paint.setAntiAlias(true);
58 sk_tool_utils::set_portable_typeface(&paint);
59 const char* name = sk_tool_utils::colortype_name(bm.colorType());
60 canvas->drawText(name, strlen(name), x, SkIntToScalar(bm.height())*scale*5/8,
61 paint);
62 canvas->translate(SkIntToScalar(48), 0);
63
64 canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));
65
66 x += draw_set(canvas, bm, 0, &paint);
67 paint.reset();
68 paint.setAlpha(0x80);
69 draw_set(canvas, bm, x, &paint);
70 return x * scale / 3;
71 }
72
73 class FilterGM : public GM {
74 bool fOnce;
init()75 void init() {
76 if (fOnce) {
77 return;
78 }
79 fOnce = true;
80 make_bm(&fBM8);
81 fBM8.copyTo(&fBM4444, kARGB_4444_SkColorType);
82 fBM8.copyTo(&fBM16, kRGB_565_SkColorType);
83 fBM8.copyTo(&fBM32, kN32_SkColorType);
84 }
85 public:
86 SkBitmap fBM8, fBM4444, fBM16, fBM32;
87
FilterGM()88 FilterGM() : fOnce(false) {
89 this->setBGColor(0xFFDDDDDD);
90 }
91
92 protected:
onShortName()93 virtual SkString onShortName() {
94 return SkString("bitmapfilters");
95 }
96
onISize()97 virtual SkISize onISize() {
98 return SkISize::Make(540, 330);
99 }
100
onDraw(SkCanvas * canvas)101 virtual void onDraw(SkCanvas* canvas) {
102 this->init();
103
104 SkScalar x = SkIntToScalar(10);
105 SkScalar y = SkIntToScalar(10);
106
107 canvas->translate(x, y);
108 y = draw_row(canvas, fBM8);
109 canvas->translate(0, y);
110 y = draw_row(canvas, fBM4444);
111 canvas->translate(0, y);
112 y = draw_row(canvas, fBM16);
113 canvas->translate(0, y);
114 draw_row(canvas, fBM32);
115 }
116
117 private:
118 typedef GM INHERITED;
119 };
120
121 //////////////////////////////////////////////////////////////////////////////
122
MyFactory(void *)123 static GM* MyFactory(void*) { return new FilterGM; }
124 static GMRegistry reg(MyFactory);
125
126 }
127