• 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 
8 #include "SampleCode.h"
9 #include "SkCanvas.h"
10 #include "SkColorPriv.h"
11 #include "SkGradientShader.h"
12 #include "SkPath.h"
13 #include "SkRegion.h"
14 #include "SkUtils.h"
15 #include "SkView.h"
16 
draw_rect(SkCanvas * canvas,const SkRect & r,const SkPaint & p)17 static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
18     canvas->drawRect(r, p);
19 
20     SkPaint frame(p);
21     frame.setShader(nullptr);
22     frame.setStyle(SkPaint::kStroke_Style);
23     canvas->drawRect(r, frame);
24 }
25 
draw_gradient(SkCanvas * canvas)26 static void draw_gradient(SkCanvas* canvas) {
27     SkRect r = { 0, 0, SkIntToScalar(256), SkIntToScalar(32) };
28     SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } };
29     SkColor colors[] = { 0xFF000000, 0xFFFF0000 };
30     SkPaint p;
31     p.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode));
32     draw_rect(canvas, r, p);
33 
34     canvas->translate(0, SkIntToScalar(40));
35     p.setDither(true);
36     draw_rect(canvas, r, p);
37 }
38 
test_pathregion()39 static bool test_pathregion() {
40     SkPath path;
41     SkRegion region;
42     path.moveTo(25071800.f, -141823808.f);
43     path.lineTo(25075500.f, -141824000.f);
44     path.lineTo(25075400.f, -141827712.f);
45     path.lineTo(25071810.f, -141827600.f);
46     path.close();
47 
48     SkIRect bounds;
49     path.getBounds().round(&bounds);
50     SkRegion clip(bounds);
51     return region.setPath(path, clip); // <-- !! DOWN !!
52 }
53 
make_bitmap()54 static SkBitmap make_bitmap() {
55     SkPMColor c[256];
56     for (int i = 0; i < 256; i++) {
57         c[i] = SkPackARGB32(0xFF, i, 0, 0);
58     }
59     SkColorTable* ctable = new SkColorTable(c, 256);
60 
61     SkBitmap bm;
62     bm.allocPixels(SkImageInfo::Make(256, 32, kIndex_8_SkColorType, kPremul_SkAlphaType),
63                    nullptr, ctable);
64     ctable->unref();
65 
66     bm.lockPixels();
67     for (int y = 0; y < bm.height(); y++) {
68         uint8_t* p = bm.getAddr8(0, y);
69         for (int x = 0; x < 256; x++) {
70             p[x] = x;
71         }
72     }
73     bm.unlockPixels();
74     return bm;
75 }
76 
77 class DitherBitmapView : public SampleView {
78     SkBitmap    fBM8;
79     SkBitmap    fBM32;
80     bool        fResult;
81 public:
DitherBitmapView()82     DitherBitmapView() {
83         fResult = test_pathregion();
84         fBM8 = make_bitmap();
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, "DitherBitmap");
95             return true;
96         }
97         return this->INHERITED::onQuery(evt);
98     }
99 
setBitmapOpaque(SkBitmap * bm,bool isOpaque)100     static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
101         SkAutoLockPixels alp(*bm);  // needed for ctable
102         bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
103     }
104 
draw2(SkCanvas * canvas,const SkBitmap & bm)105     static void draw2(SkCanvas* canvas, const SkBitmap& bm) {
106         SkPaint paint;
107         SkBitmap bitmap(bm);
108 
109         setBitmapOpaque(&bitmap, false);
110         paint.setDither(false);
111         canvas->drawBitmap(bitmap, 0, 0, &paint);
112         paint.setDither(true);
113         canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint);
114 
115         setBitmapOpaque(&bitmap, true);
116         SkScalar x = SkIntToScalar(bm.width() + 10);
117         paint.setDither(false);
118         canvas->drawBitmap(bitmap, x, 0, &paint);
119         paint.setDither(true);
120         canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint);
121     }
122 
onDrawContent(SkCanvas * canvas)123     virtual void onDrawContent(SkCanvas* canvas) {
124         canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
125 
126         draw2(canvas, fBM8);
127         canvas->translate(0, SkIntToScalar(fBM8.height() *3));
128         draw2(canvas, fBM32);
129 
130         canvas->translate(0, SkIntToScalar(fBM8.height() *3));
131         draw_gradient(canvas);
132 
133         char resultTrue[] = "SkRegion::setPath returned true";
134         char resultFalse[] = "SkRegion::setPath returned false";
135         SkPaint p;
136         if (fResult)
137             canvas->drawText(resultTrue, sizeof(resultTrue) - 1, 0, 50, p);
138         else
139             canvas->drawText(resultFalse, sizeof(resultFalse) - 1, 0, 50, p);
140     }
141 
142 private:
143     typedef SampleView INHERITED;
144 };
145 
146 //////////////////////////////////////////////////////////////////////////////
147 
MyFactory()148 static SkView* MyFactory() { return new DitherBitmapView; }
149 static SkViewRegister reg(MyFactory);
150