blob: c727f114c8dba5cc81d9644272def032089c3c66 [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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04007#include "Sample.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkCanvas.h"
9#include "SkGradientShader.h"
10#include "SkPath.h"
11#include "SkRegion.h"
12#include "SkShader.h"
Hal Canaryea60b952018-08-21 11:45:46 -040013#include "SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "Sk1DPathEffect.h"
15#include "SkCornerPathEffect.h"
16#include "SkPathMeasure.h"
17#include "SkRandom.h"
18#include "SkColorPriv.h"
19#include "SkColorFilter.h"
20#include "SkDither.h"
Matt Sarett68b8e3d2017-04-28 11:15:22 -040021#include "sk_tool_utils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022
23static void draw_sweep(SkCanvas* c, int width, int height, SkScalar angle) {
24 SkRect r;
25 SkPaint p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000026
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 p.setAntiAlias(true);
28// p.setDither(true);
29 p.setStrokeWidth(SkIntToScalar(width/10));
30 p.setStyle(SkPaint::kStroke_Style);
31
32 r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
rmistry@google.comae933ce2012-08-23 18:19:56 +000033
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 // SkColor colors[] = { SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN, SK_ColorCYAN };
35 SkColor colors[] = { 0x4c737373, 0x4c737373, 0xffffd300 };
reed8a21c9f2016-03-08 18:50:00 -080036 p.setShader(SkGradientShader::MakeSweep(r.centerX(), r.centerY(),
37 colors, nullptr, SK_ARRAY_COUNT(colors)));
rmistry@google.comae933ce2012-08-23 18:19:56 +000038
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 SkAutoCanvasRestore acr(c, true);
bungeman7438bfc2016-07-12 15:01:19 -070040 c->rotate(angle, r.centerX(), r.centerY());
reed@android.com8a1c16f2008-12-17 15:59:43 +000041
42 SkRect bounds = r;
43 r.inset(p.getStrokeWidth(), p.getStrokeWidth());
44 SkRect innerBounds = r;
45
46 if (true) {
47 c->drawOval(r, p);
48 } else {
49 SkScalar x = r.centerX();
50 SkScalar y = r.centerY();
51 SkScalar radius = r.width() / 2;
52 SkScalar thickness = p.getStrokeWidth();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000053 SkScalar sweep = 360.0f;
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 SkPath path;
rmistry@google.comae933ce2012-08-23 18:19:56 +000055
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 path.moveTo(x + radius, y);
57 // outer top
58 path.lineTo(x + radius + thickness, y);
59 // outer arc
60 path.arcTo(bounds, 0, sweep, false);
61 // inner arc
62 path.arcTo(innerBounds, sweep, -sweep, false);
63 path.close();
64 }
65}
66
reed@google.com961ddb02011-05-05 14:03:48 +000067static void make_bm(SkBitmap* bm) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000068 bm->allocN32Pixels(100, 100);
junov@google.comdbfac8a2012-12-06 21:47:40 +000069 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000070
71 SkCanvas c(*bm);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 draw_sweep(&c, bm->width(), bm->height(), 0);
73}
74
reed@google.com261b8e22011-04-14 17:53:24 +000075static void pre_dither(const SkBitmap& bm) {
reed@google.com261b8e22011-04-14 17:53:24 +000076 for (int y = 0; y < bm.height(); y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 DITHER_4444_SCAN(y);
rmistry@google.comae933ce2012-08-23 18:19:56 +000078
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 SkPMColor* p = bm.getAddr32(0, y);
reed@google.com261b8e22011-04-14 17:53:24 +000080 for (int x = 0; x < bm.width(); x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 SkPMColor c = *p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000082
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 unsigned a = SkGetPackedA32(c);
84 unsigned r = SkGetPackedR32(c);
85 unsigned g = SkGetPackedG32(c);
86 unsigned b = SkGetPackedB32(c);
rmistry@google.comae933ce2012-08-23 18:19:56 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 unsigned d = DITHER_VALUE(x);
89
90 a = SkDITHER_A32To4444(a, d);
91 r = SkDITHER_R32To4444(r, d);
92 g = SkDITHER_G32To4444(g, d);
93 b = SkDITHER_B32To4444(b, d);
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 a = SkA4444ToA32(a);
96 r = SkR4444ToR32(r);
97 g = SkG4444ToG32(g);
98 b = SkB4444ToB32(b);
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 *p++ = SkPackARGB32(a, r, g, b);
101 }
102 }
103}
104
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400105class DitherView : public Sample {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106public:
107 SkBitmap fBM, fBMPreDither, fBM16;
108 SkScalar fAngle;
109
Cary Clark58627cb2018-04-10 09:16:41 -0400110protected:
111 void onOnceBeforeDraw() override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 make_bm(&fBM);
113 make_bm(&fBMPreDither);
114 pre_dither(fBMPreDither);
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400115 sk_tool_utils::copy_to(&fBM16, kARGB_4444_SkColorType, fBM);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 fAngle = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000118
reed@google.com961ddb02011-05-05 14:03:48 +0000119 this->setBGColor(0xFF181818);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 }
121
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400122 bool onQuery(Sample::Event* evt) override {
123 if (Sample::TitleQ(*evt)) {
124 Sample::TitleR(evt, "Dither");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 return true;
126 }
127 return this->INHERITED::onQuery(evt);
128 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
Cary Clark58627cb2018-04-10 09:16:41 -0400130 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 SkPaint paint;
132 SkScalar x = SkIntToScalar(10);
133 SkScalar y = SkIntToScalar(10);
134 const SkScalar DX = SkIntToScalar(fBM.width() + 10);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000135
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 if (true) {
139 canvas->drawBitmap(fBM, x, y, &paint);
140 x += DX;
141 paint.setDither(true);
142 canvas->drawBitmap(fBM, x, y, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 x += DX;
145 paint.setDither(false);
146 canvas->drawBitmap(fBMPreDither, x, y, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 x += DX;
149 canvas->drawBitmap(fBM16, x, y, &paint);
150 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 canvas->translate(DX, DX*2);
153 draw_sweep(canvas, fBM.width(), fBM.height(), fAngle);
154 canvas->translate(DX, 0);
155 draw_sweep(canvas, fBM.width()>>1, fBM.height()>>1, fAngle);
156 canvas->translate(DX, 0);
157 draw_sweep(canvas, fBM.width()>>2, fBM.height()>>2, fAngle);
158
159 fAngle += SK_Scalar1/2;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 }
161
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400163 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164};
165
166//////////////////////////////////////////////////////////////////////////////
167
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400168DEF_SAMPLE( return new DitherView(); )