blob: 2f3de35fd6e61469a109b37d280960d1648c7b11 [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"
Mike Reed267be7f2017-02-13 09:32:54 -050013#include "SkRegion.h"
reedfb8c1fc2015-08-04 18:44:56 -070014#include "SkUtils.h"
bungemand3ebb482015-08-05 13:57:49 -070015#include "SkView.h"
Matt Sarett68b8e3d2017-04-28 11:15:22 -040016#include "sk_tool_utils.h"
reed@android.com755dd472009-08-20 21:29:45 +000017
reed@android.com3c9b2a42009-08-27 19:28:37 +000018static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
19 canvas->drawRect(r, p);
20
21 SkPaint frame(p);
halcanary96fcdcc2015-08-27 07:41:13 -070022 frame.setShader(nullptr);
reed@android.com3c9b2a42009-08-27 19:28:37 +000023 frame.setStyle(SkPaint::kStroke_Style);
24 canvas->drawRect(r, frame);
25}
26
27static void draw_gradient(SkCanvas* canvas) {
28 SkRect r = { 0, 0, SkIntToScalar(256), SkIntToScalar(32) };
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000029 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } };
reed@android.com3c9b2a42009-08-27 19:28:37 +000030 SkColor colors[] = { 0xFF000000, 0xFFFF0000 };
reed@android.com3c9b2a42009-08-27 19:28:37 +000031 SkPaint p;
reed8a21c9f2016-03-08 18:50:00 -080032 p.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode));
reed@android.com3c9b2a42009-08-27 19:28:37 +000033 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 }
reed@google.com0a6151d2013-10-10 14:44:56 +000060 SkBitmap bm;
reedc5e15a12014-09-29 12:10:27 -070061 bm.allocPixels(SkImageInfo::Make(256, 32, kIndex_8_SkColorType, kPremul_SkAlphaType),
Mike Reed6b3155c2017-04-03 14:41:44 -040062 SkColorTable::Make(c, 256));
reed@android.com755dd472009-08-20 21:29:45 +000063
reed@android.com755dd472009-08-20 21:29:45 +000064 for (int y = 0; y < bm.height(); y++) {
65 uint8_t* p = bm.getAddr8(0, y);
66 for (int x = 0; x < 256; x++) {
67 p[x] = x;
68 }
69 }
reed@android.com755dd472009-08-20 21:29:45 +000070 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;
halcanary@google.com9acb8cd2014-01-10 14:53:49 +000076 bool fResult;
reed@android.com755dd472009-08-20 21:29:45 +000077public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000078 DitherBitmapView() {
halcanary@google.com9acb8cd2014-01-10 14:53:49 +000079 fResult = test_pathregion();
reed@android.com755dd472009-08-20 21:29:45 +000080 fBM8 = make_bitmap();
Matt Sarett68b8e3d2017-04-28 11:15:22 -040081 sk_tool_utils::copy_to(&fBM32, kN32_SkColorType, fBM8);
rmistry@google.comae933ce2012-08-23 18:19:56 +000082
reed@google.com0faac1e2011-05-11 05:58:58 +000083 this->setBGColor(0xFFDDDDDD);
reed@android.com755dd472009-08-20 21:29:45 +000084 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000085
reed@android.com755dd472009-08-20 21:29:45 +000086protected:
87 // overrides from SkEventSink
88 virtual bool onQuery(SkEvent* evt) {
89 if (SampleCode::TitleQ(*evt)) {
90 SampleCode::TitleR(evt, "DitherBitmap");
91 return true;
92 }
93 return this->INHERITED::onQuery(evt);
94 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000095
reed@android.comcafc9f92009-08-22 03:44:57 +000096 static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
reed@google.com383a6972013-10-21 14:00:07 +000097 bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
reed@android.comcafc9f92009-08-22 03:44:57 +000098 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
reed@android.com755dd472009-08-20 21:29:45 +0000100 static void draw2(SkCanvas* canvas, const SkBitmap& bm) {
101 SkPaint paint;
reed@android.comcafc9f92009-08-22 03:44:57 +0000102 SkBitmap bitmap(bm);
103
104 setBitmapOpaque(&bitmap, false);
105 paint.setDither(false);
106 canvas->drawBitmap(bitmap, 0, 0, &paint);
reed@android.com755dd472009-08-20 21:29:45 +0000107 paint.setDither(true);
reed@android.comcafc9f92009-08-22 03:44:57 +0000108 canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint);
109
110 setBitmapOpaque(&bitmap, true);
111 SkScalar x = SkIntToScalar(bm.width() + 10);
112 paint.setDither(false);
113 canvas->drawBitmap(bitmap, x, 0, &paint);
114 paint.setDither(true);
115 canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint);
reed@android.com755dd472009-08-20 21:29:45 +0000116 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000117
reed@google.com0faac1e2011-05-11 05:58:58 +0000118 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com755dd472009-08-20 21:29:45 +0000119 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
120
121 draw2(canvas, fBM8);
122 canvas->translate(0, SkIntToScalar(fBM8.height() *3));
123 draw2(canvas, fBM32);
reed@android.com3c9b2a42009-08-27 19:28:37 +0000124
125 canvas->translate(0, SkIntToScalar(fBM8.height() *3));
126 draw_gradient(canvas);
halcanary@google.com9acb8cd2014-01-10 14:53:49 +0000127
128 char resultTrue[] = "SkRegion::setPath returned true";
129 char resultFalse[] = "SkRegion::setPath returned false";
130 SkPaint p;
131 if (fResult)
132 canvas->drawText(resultTrue, sizeof(resultTrue) - 1, 0, 50, p);
133 else
134 canvas->drawText(resultFalse, sizeof(resultFalse) - 1, 0, 50, p);
reed@android.com755dd472009-08-20 21:29:45 +0000135 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000136
reed@android.com755dd472009-08-20 21:29:45 +0000137private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000138 typedef SampleView INHERITED;
reed@android.com755dd472009-08-20 21:29:45 +0000139};
140
141//////////////////////////////////////////////////////////////////////////////
142
143static SkView* MyFactory() { return new DitherBitmapView; }
144static SkViewRegister reg(MyFactory);