blob: d825c71a505a3ccef223c83758a116154774aa79 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.com755dd472009-08-20 21:29:45 +00008#include "SampleCode.h"
9#include "SkColorPriv.h"
reed@android.com3c9b2a42009-08-27 19:28:37 +000010#include "SkGradientShader.h"
reed@android.com755dd472009-08-20 21:29:45 +000011#include "SkView.h"
12#include "SkCanvas.h"
13#include "SkUtils.h"
14
reed@android.com3c9b2a42009-08-27 19:28:37 +000015static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
16 canvas->drawRect(r, p);
17
18 SkPaint frame(p);
19 frame.setShader(NULL);
20 frame.setStyle(SkPaint::kStroke_Style);
21 canvas->drawRect(r, frame);
22}
23
24static void draw_gradient(SkCanvas* canvas) {
25 SkRect r = { 0, 0, SkIntToScalar(256), SkIntToScalar(32) };
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000026 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } };
reed@android.com3c9b2a42009-08-27 19:28:37 +000027 SkColor colors[] = { 0xFF000000, 0xFFFF0000 };
28 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2,
29 SkShader::kClamp_TileMode);
30
31 SkPaint p;
32 p.setShader(s)->unref();
33 draw_rect(canvas, r, p);
34
35 canvas->translate(0, SkIntToScalar(40));
36 p.setDither(true);
37 draw_rect(canvas, r, p);
38}
39
halcanary@google.com9acb8cd2014-01-10 14:53:49 +000040static bool test_pathregion() {
reed@android.comc6459962009-08-25 19:15:31 +000041 SkPath path;
42 SkRegion region;
rmistry@google.comae933ce2012-08-23 18:19:56 +000043 path.moveTo(25071800.f, -141823808.f);
reed@android.comc6459962009-08-25 19:15:31 +000044 path.lineTo(25075500.f, -141824000.f);
45 path.lineTo(25075400.f, -141827712.f);
46 path.lineTo(25071810.f, -141827600.f);
47 path.close();
48
49 SkIRect bounds;
50 path.getBounds().round(&bounds);
51 SkRegion clip(bounds);
halcanary@google.com9acb8cd2014-01-10 14:53:49 +000052 return region.setPath(path, clip); // <-- !! DOWN !!
reed@android.comc6459962009-08-25 19:15:31 +000053}
54
reed@android.com755dd472009-08-20 21:29:45 +000055static SkBitmap make_bitmap() {
reed@google.com0a6151d2013-10-10 14:44:56 +000056 SkPMColor c[256];
reed@android.com755dd472009-08-20 21:29:45 +000057 for (int i = 0; i < 256; i++) {
reed@android.comcafc9f92009-08-22 03:44:57 +000058 c[i] = SkPackARGB32(0xFF, i, 0, 0);
reed@android.com755dd472009-08-20 21:29:45 +000059 }
reedc5e15a12014-09-29 12:10:27 -070060 SkColorTable* ctable = new SkColorTable(c, 256);
reed@google.com0a6151d2013-10-10 14:44:56 +000061
62 SkBitmap bm;
reedc5e15a12014-09-29 12:10:27 -070063 bm.allocPixels(SkImageInfo::Make(256, 32, kIndex_8_SkColorType, kPremul_SkAlphaType),
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000064 NULL, ctable);
reed@android.com755dd472009-08-20 21:29:45 +000065 ctable->unref();
66
67 bm.lockPixels();
68 for (int y = 0; y < bm.height(); y++) {
69 uint8_t* p = bm.getAddr8(0, y);
70 for (int x = 0; x < 256; x++) {
71 p[x] = x;
72 }
73 }
74 bm.unlockPixels();
75 return bm;
76}
77
reed@google.com0faac1e2011-05-11 05:58:58 +000078class DitherBitmapView : public SampleView {
reed@android.com755dd472009-08-20 21:29:45 +000079 SkBitmap fBM8;
80 SkBitmap fBM32;
halcanary@google.com9acb8cd2014-01-10 14:53:49 +000081 bool fResult;
reed@android.com755dd472009-08-20 21:29:45 +000082public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000083 DitherBitmapView() {
halcanary@google.com9acb8cd2014-01-10 14:53:49 +000084 fResult = test_pathregion();
reed@android.com755dd472009-08-20 21:29:45 +000085 fBM8 = make_bitmap();
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000086 fBM8.copyTo(&fBM32, kN32_SkColorType);
rmistry@google.comae933ce2012-08-23 18:19:56 +000087
reed@google.com0faac1e2011-05-11 05:58:58 +000088 this->setBGColor(0xFFDDDDDD);
reed@android.com755dd472009-08-20 21:29:45 +000089 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000090
reed@android.com755dd472009-08-20 21:29:45 +000091protected:
92 // overrides from SkEventSink
93 virtual bool onQuery(SkEvent* evt) {
94 if (SampleCode::TitleQ(*evt)) {
95 SampleCode::TitleR(evt, "DitherBitmap");
96 return true;
97 }
98 return this->INHERITED::onQuery(evt);
99 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
reed@android.comcafc9f92009-08-22 03:44:57 +0000101 static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
102 SkAutoLockPixels alp(*bm); // needed for ctable
reed@google.com383a6972013-10-21 14:00:07 +0000103 bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
reed@android.comcafc9f92009-08-22 03:44:57 +0000104 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
reed@android.com755dd472009-08-20 21:29:45 +0000106 static void draw2(SkCanvas* canvas, const SkBitmap& bm) {
107 SkPaint paint;
reed@android.comcafc9f92009-08-22 03:44:57 +0000108 SkBitmap bitmap(bm);
109
110 setBitmapOpaque(&bitmap, false);
111 paint.setDither(false);
112 canvas->drawBitmap(bitmap, 0, 0, &paint);
reed@android.com755dd472009-08-20 21:29:45 +0000113 paint.setDither(true);
reed@android.comcafc9f92009-08-22 03:44:57 +0000114 canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint);
115
116 setBitmapOpaque(&bitmap, true);
117 SkScalar x = SkIntToScalar(bm.width() + 10);
118 paint.setDither(false);
119 canvas->drawBitmap(bitmap, x, 0, &paint);
120 paint.setDither(true);
121 canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint);
reed@android.com755dd472009-08-20 21:29:45 +0000122 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
reed@google.com0faac1e2011-05-11 05:58:58 +0000124 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com755dd472009-08-20 21:29:45 +0000125 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
126
127 draw2(canvas, fBM8);
128 canvas->translate(0, SkIntToScalar(fBM8.height() *3));
129 draw2(canvas, fBM32);
reed@android.com3c9b2a42009-08-27 19:28:37 +0000130
131 canvas->translate(0, SkIntToScalar(fBM8.height() *3));
132 draw_gradient(canvas);
halcanary@google.com9acb8cd2014-01-10 14:53:49 +0000133
134 char resultTrue[] = "SkRegion::setPath returned true";
135 char resultFalse[] = "SkRegion::setPath returned false";
136 SkPaint p;
137 if (fResult)
138 canvas->drawText(resultTrue, sizeof(resultTrue) - 1, 0, 50, p);
139 else
140 canvas->drawText(resultFalse, sizeof(resultFalse) - 1, 0, 50, p);
reed@android.com755dd472009-08-20 21:29:45 +0000141 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000142
reed@android.com755dd472009-08-20 21:29:45 +0000143private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000144 typedef SampleView INHERITED;
reed@android.com755dd472009-08-20 21:29:45 +0000145};
146
147//////////////////////////////////////////////////////////////////////////////
148
149static SkView* MyFactory() { return new DitherBitmapView; }
150static SkViewRegister reg(MyFactory);