• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
make_bm(SkBitmap * bm)24 static void make_bm(SkBitmap* bm) {
25     const SkColor colors[] = {
26         SK_ColorRED, SK_ColorGREEN,
27         SK_ColorBLUE, SK_ColorWHITE
28     };
29     SkColorTable* ctable = new SkColorTable(colors, 4);
30     bm->setConfig(SkBitmap::kIndex8_Config, 2, 2);
31     bm->allocPixels(ctable);
32     ctable->unref();
33 
34     *bm->getAddr8(0, 0) = 0;
35     *bm->getAddr8(1, 0) = 1;
36     *bm->getAddr8(0, 1) = 2;
37     *bm->getAddr8(1, 1) = 3;
38 }
39 
draw_bm(SkCanvas * canvas,const SkBitmap & bm,SkScalar x,SkScalar y,SkPaint * paint)40 static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm,
41                         SkScalar x, SkScalar y, SkPaint* paint) {
42 #if 1
43     canvas->drawBitmap(bm, x, y, paint);
44     return SkIntToScalar(bm.width()) * 5/4;
45 #else
46     SkAutoCanvasRestore acr(canvas, true);
47     canvas->translate(x, y);
48 
49     SkScalar w = SkIntToScalar(bm.width());
50     SkScalar h = SkIntToScalar(bm.height());
51     SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
52                                                SkShader::kRepeat_TileMode);
53     paint->setShader(s)->unref();
54     canvas->drawRect(SkRect::MakeWH(w, h), *paint);
55     paint->setShader(NULL);
56     return w * 5/4;
57 #endif
58 }
59 
draw_set(SkCanvas * c,const SkBitmap & bm,SkScalar x,SkPaint * p)60 static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x, SkPaint* p) {
61     x += draw_bm(c, bm, x, 0, p);
62     p->setFilterBitmap(true);
63     x += draw_bm(c, bm, x, 0, p);
64     p->setDither(true);
65     return x + draw_bm(c, bm, x, 0, p);
66 }
67 
68 static const char* gConfigNames[] = {
69     "unknown config",
70     "A1",
71     "A8",
72     "Index8",
73     "565",
74     "4444",
75     "8888"
76 };
77 
draw_row(SkCanvas * canvas,const SkBitmap & bm)78 static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) {
79     SkAutoCanvasRestore acr(canvas, true);
80 
81     SkPaint paint;
82     SkScalar x = 0;
83     const int scale = 32;
84 
85     paint.setAntiAlias(true);
86     const char* name = gConfigNames[bm.config()];
87     canvas->drawText(name, strlen(name), x, SkIntToScalar(bm.height())*scale*5/8,
88                      paint);
89     canvas->translate(SkIntToScalar(48), 0);
90 
91     canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));
92 
93     x += draw_set(canvas, bm, 0, &paint);
94     paint.reset();
95     paint.setAlpha(0x80);
96     draw_set(canvas, bm, x, &paint);
97     return x * scale / 3;
98 }
99 
100 class FilterView : public SampleView {
101 public:
102     SkBitmap    fBM8, fBM4444, fBM16, fBM32;
103 
FilterView()104 	FilterView() {
105         make_bm(&fBM8);
106         fBM8.copyTo(&fBM4444, SkBitmap::kARGB_4444_Config);
107         fBM8.copyTo(&fBM16, SkBitmap::kRGB_565_Config);
108         fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config);
109 
110         this->setBGColor(0xFFDDDDDD);
111     }
112 
113 protected:
114     // overrides from SkEventSink
onQuery(SkEvent * evt)115     virtual bool onQuery(SkEvent* evt) {
116         if (SampleCode::TitleQ(*evt)) {
117             SampleCode::TitleR(evt, "Filter");
118             return true;
119         }
120         return this->INHERITED::onQuery(evt);
121     }
122 
onDrawContent(SkCanvas * canvas)123     virtual void onDrawContent(SkCanvas* canvas) {
124         SkScalar x = SkIntToScalar(10);
125         SkScalar y = SkIntToScalar(10);
126 
127         canvas->translate(x, y);
128         y = draw_row(canvas, fBM8);
129         canvas->translate(0, y);
130         y = draw_row(canvas, fBM4444);
131         canvas->translate(0, y);
132         y = draw_row(canvas, fBM16);
133         canvas->translate(0, y);
134         draw_row(canvas, fBM32);
135     }
136 
137 private:
138     typedef SampleView INHERITED;
139 };
140 
141 //////////////////////////////////////////////////////////////////////////////
142 
MyFactory()143 static SkView* MyFactory() { return new FilterView; }
144 static SkViewRegister reg(MyFactory);
145 
146