blob: aa314dd58b03545cd291a0cc33817108bd79d691 [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.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "Sk64.h"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000012#include "SkFlattenableBuffers.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkGradientShader.h"
14#include "SkGraphics.h"
15#include "SkImageDecoder.h"
16#include "SkKernel33MaskFilter.h"
17#include "SkPath.h"
18#include "SkRandom.h"
19#include "SkRegion.h"
20#include "SkShader.h"
21#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkColorPriv.h"
23#include "SkColorFilter.h"
24#include "SkTime.h"
25#include "SkTypeface.h"
26#include "SkXfermode.h"
27
28#include "SkStream.h"
29#include "SkXMLParser.h"
30
reed@android.com8a1c16f2008-12-17 15:59:43 +000031class ReduceNoise : public SkKernel33ProcMaskFilter {
32public:
33 ReduceNoise(int percent256) : SkKernel33ProcMaskFilter(percent256) {}
reed@google.com30711b72012-12-18 19:18:39 +000034 virtual uint8_t computeValue(uint8_t* const* srcRows) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 int c = srcRows[1][1];
36 int min = 255, max = 0;
37 for (int i = 0; i < 3; i++)
38 for (int j = 0; j < 3; j++)
39 if (i != 1 || j != 1)
40 {
41 int v = srcRows[i][j];
42 if (max < v)
43 max = v;
44 if (min > v)
45 min = v;
46 }
47 if (c > max) c = max;
48 // if (c < min) c = min;
49 return c;
50 }
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +000051
52#ifdef SK_DEVELOPER
53 virtual void toString(SkString* str) const SK_OVERRIDE {
54 str->append("ReduceNoise: (");
55 this->INHERITED::toString(str);
56 str->append(")");
57 }
58#endif
59
djsollen@google.comba28d032012-03-26 17:57:35 +000060 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(ReduceNoise)
61
reed@android.com8a1c16f2008-12-17 15:59:43 +000062private:
63 ReduceNoise(SkFlattenableReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {}
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +000064
65 typedef SkKernel33ProcMaskFilter INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000066};
67
68class Darken : public SkKernel33ProcMaskFilter {
69public:
70 Darken(int percent256) : SkKernel33ProcMaskFilter(percent256) {}
reed@google.com30711b72012-12-18 19:18:39 +000071 virtual uint8_t computeValue(uint8_t* const* srcRows) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 int c = srcRows[1][1];
73 float f = c / 255.f;
reed@google.com82065d62011-02-07 15:30:46 +000074
reed@google.com9e39bb32011-05-18 12:17:53 +000075 if (c >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 f = sqrtf(f);
reed@google.com9e39bb32011-05-18 12:17:53 +000077 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 f *= f;
79 }
80 SkASSERT(f >= 0 && f <= 1);
81 return (int)(f * 255);
82 }
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +000083
84#ifdef SK_DEVELOPER
skia.committer@gmail.com8eaddb02013-03-19 07:15:10 +000085 virtual void toString(SkString* str) const SK_OVERRIDE {
86 str->append("Darken: (");
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +000087 this->INHERITED::toString(str);
88 str->append(")");
89 }
90#endif
91
djsollen@google.comba28d032012-03-26 17:57:35 +000092 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Darken)
93
reed@android.com8a1c16f2008-12-17 15:59:43 +000094private:
95 Darken(SkFlattenableReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {}
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +000096
97 typedef SkKernel33ProcMaskFilter INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000098};
99
100static SkMaskFilter* makemf() { return new Darken(0x30); }
101
reed@google.com9e39bb32011-05-18 12:17:53 +0000102static void test_breakText() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 SkPaint paint;
104 const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
105 size_t length = strlen(text);
106 SkScalar width = paint.measureText(text, length);
reed@google.com82065d62011-02-07 15:30:46 +0000107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 SkScalar mm = 0;
109 SkScalar nn = 0;
reed@google.com9e39bb32011-05-18 12:17:53 +0000110 for (SkScalar w = 0; w <= width; w += SK_Scalar1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 SkScalar m;
112 size_t n = paint.breakText(text, length, w, &m,
113 SkPaint::kBackward_TextBufferDirection);
reed@google.com82065d62011-02-07 15:30:46 +0000114
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 SkASSERT(n <= length);
116 SkASSERT(m <= width);
reed@google.com82065d62011-02-07 15:30:46 +0000117
reed@google.com9e39bb32011-05-18 12:17:53 +0000118 if (n == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 SkASSERT(m == 0);
reed@google.com9e39bb32011-05-18 12:17:53 +0000120 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 // now assert that we're monotonic
reed@google.com9e39bb32011-05-18 12:17:53 +0000122 if (n == nn) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 SkASSERT(m == mm);
reed@google.com9e39bb32011-05-18 12:17:53 +0000124 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 SkASSERT(n > nn);
126 SkASSERT(m > mm);
127 }
128 }
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000129 nn = SkIntToScalar((unsigned int)n);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 mm = m;
131 }
132
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000133 SkDEBUGCODE(size_t length2 =) paint.breakText(text, length, width, &mm);
reed@google.com261b8e22011-04-14 17:53:24 +0000134 SkASSERT(length2 == length);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 SkASSERT(mm == width);
136}
137
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000138static SkRandom gRand;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139
140class SkPowerMode : public SkXfermode {
141public:
142 SkPowerMode(SkScalar exponent) { this->init(exponent); }
143
reed@google.com9e39bb32011-05-18 12:17:53 +0000144 virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count,
mike@reedtribe.org3af2b8d2012-12-24 18:26:43 +0000145 const SkAlpha aa[]) const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146
147 typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&);
reed@google.com82065d62011-02-07 15:30:46 +0000148
robertphillips@google.comb83b6b42013-01-22 14:32:09 +0000149 SK_DEVELOPER_TO_STRING()
djsollen@google.comba28d032012-03-26 17:57:35 +0000150 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPowerMode)
reed@google.com82065d62011-02-07 15:30:46 +0000151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152private:
153 SkScalar fExp; // user's value
154 uint8_t fTable[256]; // cache
155
156 void init(SkScalar exponent);
djsollen@google.com54924242012-03-29 15:18:04 +0000157 SkPowerMode(SkFlattenableReadBuffer& b) : INHERITED(b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 // read the exponent
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000159 this->init(SkFixedToScalar(b.readFixed()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 }
djsollen@google.com54924242012-03-29 15:18:04 +0000161 virtual void flatten(SkFlattenableWriteBuffer& b) const SK_OVERRIDE {
162 this->INHERITED::flatten(b);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000163 b.writeFixed(SkScalarToFixed(fExp));
djsollen@google.com54924242012-03-29 15:18:04 +0000164 }
reed@google.com82065d62011-02-07 15:30:46 +0000165
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 typedef SkXfermode INHERITED;
167};
168
reed@google.com9e39bb32011-05-18 12:17:53 +0000169void SkPowerMode::init(SkScalar e) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 fExp = e;
171 float ee = SkScalarToFloat(e);
reed@google.com82065d62011-02-07 15:30:46 +0000172
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 printf("------ %g\n", ee);
reed@google.com9e39bb32011-05-18 12:17:53 +0000174 for (int i = 0; i < 256; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 float x = i / 255.f;
176 // printf(" %d %g", i, x);
177 x = powf(x, ee);
178 // printf(" %g", x);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000179 int xx = SkScalarRound(x * 255);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 // printf(" %d\n", xx);
181 fTable[i] = SkToU8(xx);
182 }
183}
184
reed@google.com9e39bb32011-05-18 12:17:53 +0000185void SkPowerMode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
mike@reedtribe.org3af2b8d2012-12-24 18:26:43 +0000186 const SkAlpha aa[]) const {
reed@google.com9e39bb32011-05-18 12:17:53 +0000187 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 SkPMColor c = src[i];
189 int r = SkGetPackedR32(c);
190 int g = SkGetPackedG32(c);
191 int b = SkGetPackedB32(c);
192 r = fTable[r];
193 g = fTable[g];
194 b = fTable[b];
195 dst[i] = SkPack888ToRGB16(r, g, b);
196 }
197}
198
robertphillips@google.comb83b6b42013-01-22 14:32:09 +0000199#ifdef SK_DEVELOPER
200void SkPowerMode::toString(SkString* str) const {
201 str->append("SkPowerMode: exponent ");
202 str->appendScalar(fExp);
203}
204#endif
205
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206static const struct {
207 const char* fName;
208 uint32_t fFlags;
209 bool fFlushCache;
210} gHints[] = {
211 { "Linear", SkPaint::kLinearText_Flag, false },
212 { "Normal", 0, true },
213 { "Subpixel", SkPaint::kSubpixelText_Flag, true }
214};
215
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216static void DrawTheText(SkCanvas* canvas, const char text[], size_t length,
217 SkScalar x, SkScalar y, const SkPaint& paint,
reed@google.com9e39bb32011-05-18 12:17:53 +0000218 SkScalar clickX, SkMaskFilter* mf) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219 SkPaint p(paint);
220
221#if 0
222 canvas->drawText(text, length, x, y, paint);
223#else
224 {
225 SkPoint pts[1000];
226 SkScalar xpos = x;
227 SkASSERT(length <= SK_ARRAY_COUNT(pts));
reed@google.com9e39bb32011-05-18 12:17:53 +0000228 for (size_t i = 0; i < length; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229 pts[i].set(xpos, y), xpos += paint.getTextSize();
reed@google.com9e39bb32011-05-18 12:17:53 +0000230 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231 canvas->drawPosText(text, length, pts, paint);
232 }
233#endif
234
235 p.setSubpixelText(true);
236 x += SkIntToScalar(180);
237 canvas->drawText(text, length, x, y, p);
238
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239#ifdef SK_DEBUG
reed@google.com9e39bb32011-05-18 12:17:53 +0000240 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241 // p.setMaskFilter(mf);
242 p.setSubpixelText(false);
243 p.setLinearText(true);
244 x += SkIntToScalar(180);
245 canvas->drawText(text, length, x, y, p);
246 }
247#endif
248}
249
reed@google.com9e39bb32011-05-18 12:17:53 +0000250class TextSpeedView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000252 TextSpeedView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253 fMF = makemf();
254
255 fHints = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 fClickX = 0;
257
reed@google.com82065d62011-02-07 15:30:46 +0000258 test_breakText();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 }
reed@google.com82065d62011-02-07 15:30:46 +0000260
reed@google.com9e39bb32011-05-18 12:17:53 +0000261 virtual ~TextSpeedView() {
reed@google.com82065d62011-02-07 15:30:46 +0000262 SkSafeUnref(fMF);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 }
264
265protected:
266 // overrides from SkEventSink
reed@google.com9e39bb32011-05-18 12:17:53 +0000267 virtual bool onQuery(SkEvent* evt) {
268 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269 SampleCode::TitleR(evt, "Text");
270 return true;
271 }
272 return this->INHERITED::onQuery(evt);
273 }
reed@google.com82065d62011-02-07 15:30:46 +0000274
reed@google.com9e39bb32011-05-18 12:17:53 +0000275 static void make_textstrip(SkBitmap* bm) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276 bm->setConfig(SkBitmap::kRGB_565_Config, 200, 18);
277 bm->allocPixels();
278 bm->eraseColor(SK_ColorWHITE);
reed@google.com82065d62011-02-07 15:30:46 +0000279
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280 SkCanvas canvas(*bm);
281 SkPaint paint;
282 const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";
reed@google.com82065d62011-02-07 15:30:46 +0000283
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 paint.setFlags(paint.getFlags() | SkPaint::kAntiAlias_Flag
285 | SkPaint::kDevKernText_Flag);
286 paint.setTextSize(SkIntToScalar(14));
287 canvas.drawText(s, strlen(s), SkIntToScalar(8), SkIntToScalar(14), paint);
288 }
reed@google.com82065d62011-02-07 15:30:46 +0000289
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000290 static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291 for (size_t i = 0; i < n; i++)
292 pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480);
293 }
reed@google.com82065d62011-02-07 15:30:46 +0000294
reed@google.com9e39bb32011-05-18 12:17:53 +0000295 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296 SkAutoCanvasRestore restore(canvas, false);
297 {
298 SkRect r;
299 r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20));
300 // canvas->saveLayer(&r, NULL, SkCanvas::kHasAlphaLayer_SaveFlag);
301 }
302
303 SkPaint paint;
304// const uint16_t glyphs[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };
305 int index = fHints % SK_ARRAY_COUNT(gHints);
306 index = 1;
307// const char* style = gHints[index].fName;
reed@google.com82065d62011-02-07 15:30:46 +0000308
reed@android.com8a1c16f2008-12-17 15:59:43 +0000309// canvas->translate(0, SkIntToScalar(50));
310
311 // canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint);
312
reed@android.com04d86c62010-01-25 22:02:44 +0000313 SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf")));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000314 paint.setAntiAlias(true);
315 paint.setFlags(paint.getFlags() | gHints[index].fFlags);
reed@google.com82065d62011-02-07 15:30:46 +0000316
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 SkRect clip;
318 clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155));
reed@google.com82065d62011-02-07 15:30:46 +0000319
reed@android.com8a1c16f2008-12-17 15:59:43 +0000320 const char* text = "Hamburgefons";
321 size_t length = strlen(text);
322
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000323 SkScalar y = SkIntToScalar(0);
324 for (int i = 9; i <= 24; i++) {
325 paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/);
reed@google.com9e39bb32011-05-18 12:17:53 +0000326 for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4;
327 dx += SkIntToScalar(1) /* /4 */) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000328 y += paint.getFontSpacing();
reed@google.com9e39bb32011-05-18 12:17:53 +0000329 DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y,
330 paint, fClickX, fMF);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000331 }
332 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000333 if (gHints[index].fFlushCache) {
334// SkGraphics::SetFontCacheUsed(0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335 }
336 }
reed@google.com82065d62011-02-07 15:30:46 +0000337
reed@google.com4d5c26d2013-01-08 16:17:50 +0000338 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
339 unsigned modi) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340 fClickX = x;
341 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000342 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343 }
reed@google.com82065d62011-02-07 15:30:46 +0000344
reed@google.com9e39bb32011-05-18 12:17:53 +0000345 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 return this->INHERITED::onClick(click);
347 }
reed@google.com82065d62011-02-07 15:30:46 +0000348
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349private:
350 int fHints;
351 SkScalar fClickX;
352 SkMaskFilter* fMF;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353
reed@google.com9e39bb32011-05-18 12:17:53 +0000354 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355};
356
357//////////////////////////////////////////////////////////////////////////////
358
359static SkView* MyFactory() { return new TextSpeedView; }
360static SkViewRegister reg(MyFactory);