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