blob: 91199c2ec9b14636bb346dc4bbc36c9dde7480b2 [file] [log] [blame]
reed@android.com755dd472009-08-20 21:29:45 +00001#include "SampleCode.h"
2#include "SkColorPriv.h"
reed@android.com3c9b2a42009-08-27 19:28:37 +00003#include "SkGradientShader.h"
reed@android.com755dd472009-08-20 21:29:45 +00004#include "SkView.h"
5#include "SkCanvas.h"
6#include "SkUtils.h"
7
reed@android.com3c9b2a42009-08-27 19:28:37 +00008static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
9 canvas->drawRect(r, p);
10
11 SkPaint frame(p);
12 frame.setShader(NULL);
13 frame.setStyle(SkPaint::kStroke_Style);
14 canvas->drawRect(r, frame);
15}
16
17static void draw_gradient(SkCanvas* canvas) {
18 SkRect r = { 0, 0, SkIntToScalar(256), SkIntToScalar(32) };
19 SkPoint pts[] = { r.fLeft, r.fTop, r.fRight, r.fTop };
20 SkColor colors[] = { 0xFF000000, 0xFFFF0000 };
21 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2,
22 SkShader::kClamp_TileMode);
23
24 SkPaint p;
25 p.setShader(s)->unref();
26 draw_rect(canvas, r, p);
27
28 canvas->translate(0, SkIntToScalar(40));
29 p.setDither(true);
30 draw_rect(canvas, r, p);
31}
32
reed@android.comc6459962009-08-25 19:15:31 +000033static void test_pathregion() {
34 SkPath path;
35 SkRegion region;
36 path.moveTo(25071800.f, -141823808.f);
37 path.lineTo(25075500.f, -141824000.f);
38 path.lineTo(25075400.f, -141827712.f);
39 path.lineTo(25071810.f, -141827600.f);
40 path.close();
41
42 SkIRect bounds;
43 path.getBounds().round(&bounds);
44 SkRegion clip(bounds);
45 bool result = region.setPath(path, clip); // <-- !! DOWN !!
46 SkDebugf("----- result %d\n", result);
47}
48
reed@android.com755dd472009-08-20 21:29:45 +000049static SkBitmap make_bitmap() {
50 SkBitmap bm;
51 SkColorTable* ctable = new SkColorTable(256);
52
53 SkPMColor* c = ctable->lockColors();
54 for (int i = 0; i < 256; i++) {
reed@android.comcafc9f92009-08-22 03:44:57 +000055 c[i] = SkPackARGB32(0xFF, i, 0, 0);
reed@android.com755dd472009-08-20 21:29:45 +000056 }
57 ctable->unlockColors(true);
58 bm.setConfig(SkBitmap::kIndex8_Config, 256, 32);
59 bm.allocPixels(ctable);
60 ctable->unref();
61
62 bm.lockPixels();
63 for (int y = 0; y < bm.height(); y++) {
64 uint8_t* p = bm.getAddr8(0, y);
65 for (int x = 0; x < 256; x++) {
66 p[x] = x;
67 }
68 }
69 bm.unlockPixels();
70 return bm;
71}
72
reed@google.com0faac1e2011-05-11 05:58:58 +000073class DitherBitmapView : public SampleView {
reed@android.com755dd472009-08-20 21:29:45 +000074 SkBitmap fBM8;
75 SkBitmap fBM32;
76public:
77 DitherBitmapView() {
reed@android.comc6459962009-08-25 19:15:31 +000078 test_pathregion();
reed@android.com755dd472009-08-20 21:29:45 +000079 fBM8 = make_bitmap();
80 fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config);
reed@google.com0faac1e2011-05-11 05:58:58 +000081
82 this->setBGColor(0xFFDDDDDD);
reed@android.com755dd472009-08-20 21:29:45 +000083 }
84
85protected:
86 // overrides from SkEventSink
87 virtual bool onQuery(SkEvent* evt) {
88 if (SampleCode::TitleQ(*evt)) {
89 SampleCode::TitleR(evt, "DitherBitmap");
90 return true;
91 }
92 return this->INHERITED::onQuery(evt);
93 }
94
reed@android.comcafc9f92009-08-22 03:44:57 +000095 static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
96 SkAutoLockPixels alp(*bm); // needed for ctable
97 bm->setIsOpaque(isOpaque);
98 SkColorTable* ctable = bm->getColorTable();
99 if (ctable) {
100 ctable->setIsOpaque(isOpaque);
101 }
102 }
103
reed@android.com755dd472009-08-20 21:29:45 +0000104 static void draw2(SkCanvas* canvas, const SkBitmap& bm) {
105 SkPaint paint;
reed@android.comcafc9f92009-08-22 03:44:57 +0000106 SkBitmap bitmap(bm);
107
108 setBitmapOpaque(&bitmap, false);
109 paint.setDither(false);
110 canvas->drawBitmap(bitmap, 0, 0, &paint);
reed@android.com755dd472009-08-20 21:29:45 +0000111 paint.setDither(true);
reed@android.comcafc9f92009-08-22 03:44:57 +0000112 canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint);
113
114 setBitmapOpaque(&bitmap, true);
115 SkScalar x = SkIntToScalar(bm.width() + 10);
116 paint.setDither(false);
117 canvas->drawBitmap(bitmap, x, 0, &paint);
118 paint.setDither(true);
119 canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint);
reed@android.com755dd472009-08-20 21:29:45 +0000120 }
121
reed@google.com0faac1e2011-05-11 05:58:58 +0000122 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com755dd472009-08-20 21:29:45 +0000123 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
124
125 draw2(canvas, fBM8);
126 canvas->translate(0, SkIntToScalar(fBM8.height() *3));
127 draw2(canvas, fBM32);
reed@android.com3c9b2a42009-08-27 19:28:37 +0000128
129 canvas->translate(0, SkIntToScalar(fBM8.height() *3));
130 draw_gradient(canvas);
reed@android.com755dd472009-08-20 21:29:45 +0000131 }
132
133private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000134 typedef SampleView INHERITED;
reed@android.com755dd472009-08-20 21:29:45 +0000135};
136
137//////////////////////////////////////////////////////////////////////////////
138
139static SkView* MyFactory() { return new DitherBitmapView; }
140static SkViewRegister reg(MyFactory);
141