blob: 34241c5e025f1bc78af1d78b2f5994655e5a5679 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
bungemand3ebb482015-08-05 13:57:49 -07007
reed@android.com755dd472009-08-20 21:29:45 +00008#include "SampleCode.h"
bungemand3ebb482015-08-05 13:57:49 -07009#include "SkCanvas.h"
reed@android.com755dd472009-08-20 21:29:45 +000010#include "SkColorPriv.h"
reed@android.com3c9b2a42009-08-27 19:28:37 +000011#include "SkGradientShader.h"
bungemand3ebb482015-08-05 13:57:49 -070012#include "SkPath.h"
reedfb8c1fc2015-08-04 18:44:56 -070013#include "SkUtils.h"
bungemand3ebb482015-08-05 13:57:49 -070014#include "SkView.h"
reed@android.com755dd472009-08-20 21:29:45 +000015
reed@android.com3c9b2a42009-08-27 19:28:37 +000016static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
17 canvas->drawRect(r, p);
18
19 SkPaint frame(p);
halcanary96fcdcc2015-08-27 07:41:13 -070020 frame.setShader(nullptr);
reed@android.com3c9b2a42009-08-27 19:28:37 +000021 frame.setStyle(SkPaint::kStroke_Style);
22 canvas->drawRect(r, frame);
23}
24
25static void draw_gradient(SkCanvas* canvas) {
26 SkRect r = { 0, 0, SkIntToScalar(256), SkIntToScalar(32) };
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000027 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } };
reed@android.com3c9b2a42009-08-27 19:28:37 +000028 SkColor colors[] = { 0xFF000000, 0xFFFF0000 };
reed@android.com3c9b2a42009-08-27 19:28:37 +000029 SkPaint p;
reed8a21c9f2016-03-08 18:50:00 -080030 p.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode));
reed@android.com3c9b2a42009-08-27 19:28:37 +000031 draw_rect(canvas, r, p);
32
33 canvas->translate(0, SkIntToScalar(40));
34 p.setDither(true);
35 draw_rect(canvas, r, p);
36}
37
halcanary@google.com9acb8cd2014-01-10 14:53:49 +000038static bool test_pathregion() {
reed@android.comc6459962009-08-25 19:15:31 +000039 SkPath path;
40 SkRegion region;
rmistry@google.comae933ce2012-08-23 18:19:56 +000041 path.moveTo(25071800.f, -141823808.f);
reed@android.comc6459962009-08-25 19:15:31 +000042 path.lineTo(25075500.f, -141824000.f);
43 path.lineTo(25075400.f, -141827712.f);
44 path.lineTo(25071810.f, -141827600.f);
45 path.close();
46
47 SkIRect bounds;
48 path.getBounds().round(&bounds);
49 SkRegion clip(bounds);
halcanary@google.com9acb8cd2014-01-10 14:53:49 +000050 return region.setPath(path, clip); // <-- !! DOWN !!
reed@android.comc6459962009-08-25 19:15:31 +000051}
52
reed@android.com755dd472009-08-20 21:29:45 +000053static SkBitmap make_bitmap() {
reed@google.com0a6151d2013-10-10 14:44:56 +000054 SkPMColor c[256];
reed@android.com755dd472009-08-20 21:29:45 +000055 for (int i = 0; i < 256; i++) {
reed@android.comcafc9f92009-08-22 03:44:57 +000056 c[i] = SkPackARGB32(0xFF, i, 0, 0);
reed@android.com755dd472009-08-20 21:29:45 +000057 }
reedc5e15a12014-09-29 12:10:27 -070058 SkColorTable* ctable = new SkColorTable(c, 256);
reed@google.com0a6151d2013-10-10 14:44:56 +000059
60 SkBitmap bm;
reedc5e15a12014-09-29 12:10:27 -070061 bm.allocPixels(SkImageInfo::Make(256, 32, kIndex_8_SkColorType, kPremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -070062 nullptr, ctable);
reed@android.com755dd472009-08-20 21:29:45 +000063 ctable->unref();
64
65 bm.lockPixels();
66 for (int y = 0; y < bm.height(); y++) {
67 uint8_t* p = bm.getAddr8(0, y);
68 for (int x = 0; x < 256; x++) {
69 p[x] = x;
70 }
71 }
72 bm.unlockPixels();
73 return bm;
74}
75
reed@google.com0faac1e2011-05-11 05:58:58 +000076class DitherBitmapView : public SampleView {
reed@android.com755dd472009-08-20 21:29:45 +000077 SkBitmap fBM8;
78 SkBitmap fBM32;
halcanary@google.com9acb8cd2014-01-10 14:53:49 +000079 bool fResult;
reed@android.com755dd472009-08-20 21:29:45 +000080public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000081 DitherBitmapView() {
halcanary@google.com9acb8cd2014-01-10 14:53:49 +000082 fResult = test_pathregion();
reed@android.com755dd472009-08-20 21:29:45 +000083 fBM8 = make_bitmap();
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000084 fBM8.copyTo(&fBM32, kN32_SkColorType);
rmistry@google.comae933ce2012-08-23 18:19:56 +000085
reed@google.com0faac1e2011-05-11 05:58:58 +000086 this->setBGColor(0xFFDDDDDD);
reed@android.com755dd472009-08-20 21:29:45 +000087 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
reed@android.com755dd472009-08-20 21:29:45 +000089protected:
90 // overrides from SkEventSink
91 virtual bool onQuery(SkEvent* evt) {
92 if (SampleCode::TitleQ(*evt)) {
93 SampleCode::TitleR(evt, "DitherBitmap");
94 return true;
95 }
96 return this->INHERITED::onQuery(evt);
97 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000098
reed@android.comcafc9f92009-08-22 03:44:57 +000099 static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
100 SkAutoLockPixels alp(*bm); // needed for ctable
reed@google.com383a6972013-10-21 14:00:07 +0000101 bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
reed@android.comcafc9f92009-08-22 03:44:57 +0000102 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000103
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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000121
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);
halcanary@google.com9acb8cd2014-01-10 14:53:49 +0000131
132 char resultTrue[] = "SkRegion::setPath returned true";
133 char resultFalse[] = "SkRegion::setPath returned false";
134 SkPaint p;
135 if (fResult)
136 canvas->drawText(resultTrue, sizeof(resultTrue) - 1, 0, 50, p);
137 else
138 canvas->drawText(resultFalse, sizeof(resultFalse) - 1, 0, 50, p);
reed@android.com755dd472009-08-20 21:29:45 +0000139 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000140
reed@android.com755dd472009-08-20 21:29:45 +0000141private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000142 typedef SampleView INHERITED;
reed@android.com755dd472009-08-20 21:29:45 +0000143};
144
145//////////////////////////////////////////////////////////////////////////////
146
147static SkView* MyFactory() { return new DitherBitmapView; }
148static SkViewRegister reg(MyFactory);