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