1
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 */
8 #include "SampleCode.h"
9 #include "SkView.h"
10 #include "SkCanvas.h"
11 #include "SkGradientShader.h"
12 #include "SkPath.h"
13 #include "SkRegion.h"
14 #include "SkShader.h"
15 #include "SkUtils.h"
16 #include "Sk1DPathEffect.h"
17 #include "SkCornerPathEffect.h"
18 #include "SkPathMeasure.h"
19 #include "SkRandom.h"
20 #include "SkColorPriv.h"
21 #include "SkColorFilter.h"
22 #include "SkDither.h"
23 #include "sk_tool_utils.h"
24
make_bm(SkBitmap * bm)25 static void make_bm(SkBitmap* bm) {
26 const SkPMColor colors[] = {
27 SkPreMultiplyColor(SK_ColorRED), SkPreMultiplyColor(SK_ColorGREEN),
28 SkPreMultiplyColor(SK_ColorBLUE), SkPreMultiplyColor(SK_ColorWHITE)
29 };
30 SkColorTable* ctable = new SkColorTable(colors, 4);
31 bm->allocPixels(SkImageInfo::Make(2, 2, kIndex_8_SkColorType,
32 kOpaque_SkAlphaType),
33 NULL, ctable);
34 ctable->unref();
35
36 *bm->getAddr8(0, 0) = 0;
37 *bm->getAddr8(1, 0) = 1;
38 *bm->getAddr8(0, 1) = 2;
39 *bm->getAddr8(1, 1) = 3;
40 }
41
draw_bm(SkCanvas * canvas,const SkBitmap & bm,SkScalar x,SkScalar y,SkPaint * paint)42 static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm,
43 SkScalar x, SkScalar y, SkPaint* paint) {
44 canvas->drawBitmap(bm, x, y, paint);
45 return SkIntToScalar(bm.width()) * 5/4;
46 }
47
draw_set(SkCanvas * c,const SkBitmap & bm,SkScalar x,SkPaint * p)48 static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x, SkPaint* p) {
49 x += draw_bm(c, bm, x, 0, p);
50 p->setFilterQuality(kLow_SkFilterQuality);
51 x += draw_bm(c, bm, x, 0, p);
52 p->setDither(true);
53 return x + draw_bm(c, bm, x, 0, p);
54 }
55
draw_row(SkCanvas * canvas,const SkBitmap & bm)56 static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) {
57 SkAutoCanvasRestore acr(canvas, true);
58
59 SkPaint paint;
60 SkScalar x = 0;
61 const int scale = 32;
62
63 paint.setAntiAlias(true);
64 const char* name = sk_tool_utils::colortype_name(bm.colorType());
65 canvas->drawText(name, strlen(name), x, SkIntToScalar(bm.height())*scale*5/8,
66 paint);
67 canvas->translate(SkIntToScalar(48), 0);
68
69 canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));
70
71 x += draw_set(canvas, bm, 0, &paint);
72 paint.reset();
73 paint.setAlpha(0x80);
74 draw_set(canvas, bm, x, &paint);
75 return x * scale / 3;
76 }
77
78 class FilterView : public SampleView {
79 public:
80 SkBitmap fBM8, fBM4444, fBM16, fBM32;
81
FilterView()82 FilterView() {
83 make_bm(&fBM8);
84 fBM8.copyTo(&fBM4444, kARGB_4444_SkColorType);
85 fBM8.copyTo(&fBM16, kRGB_565_SkColorType);
86 fBM8.copyTo(&fBM32, kN32_SkColorType);
87
88 this->setBGColor(0xFFDDDDDD);
89 }
90
91 protected:
92 // overrides from SkEventSink
onQuery(SkEvent * evt)93 virtual bool onQuery(SkEvent* evt) {
94 if (SampleCode::TitleQ(*evt)) {
95 SampleCode::TitleR(evt, "Filter");
96 return true;
97 }
98 return this->INHERITED::onQuery(evt);
99 }
100
onDrawContent(SkCanvas * canvas)101 virtual void onDrawContent(SkCanvas* canvas) {
102 SkScalar x = SkIntToScalar(10);
103 SkScalar y = SkIntToScalar(10);
104
105 canvas->translate(x, y);
106 y = draw_row(canvas, fBM8);
107 canvas->translate(0, y);
108 y = draw_row(canvas, fBM4444);
109 canvas->translate(0, y);
110 y = draw_row(canvas, fBM16);
111 canvas->translate(0, y);
112 draw_row(canvas, fBM32);
113 }
114
115 private:
116 typedef SampleView INHERITED;
117 };
118
119 //////////////////////////////////////////////////////////////////////////////
120
MyFactory()121 static SkView* MyFactory() { return new FilterView; }
122 static SkViewRegister reg(MyFactory);
123