blob: b1bb6cbc9f37be04fcd326c26f9b28348e131f50 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "Sk64.h"
5#include "SkGradientShader.h"
6#include "SkGraphics.h"
7#include "SkImageDecoder.h"
8#include "SkKernel33MaskFilter.h"
9#include "SkPath.h"
10#include "SkRandom.h"
11#include "SkRegion.h"
12#include "SkShader.h"
13#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkColorPriv.h"
15#include "SkColorFilter.h"
16#include "SkTime.h"
17#include "SkTypeface.h"
18#include "SkXfermode.h"
19
20#include "SkStream.h"
21#include "SkXMLParser.h"
22
23static const int gKernel[3][3] = {
24// { -1, -2, -1 }, { -2, 12, -2 }, { -1, -2, -1 }
25 { 1, 2, 1 }, { 2, 64-12, 2 }, { 1, 2, 1 }
26};
27static const int gShift = 6;
28
29class ReduceNoise : public SkKernel33ProcMaskFilter {
30public:
31 ReduceNoise(int percent256) : SkKernel33ProcMaskFilter(percent256) {}
32 virtual uint8_t computeValue(uint8_t* const* srcRows)
33 {
34 int c = srcRows[1][1];
35 int min = 255, max = 0;
36 for (int i = 0; i < 3; i++)
37 for (int j = 0; j < 3; j++)
38 if (i != 1 || j != 1)
39 {
40 int v = srcRows[i][j];
41 if (max < v)
42 max = v;
43 if (min > v)
44 min = v;
45 }
46 if (c > max) c = max;
47 // if (c < min) c = min;
48 return c;
49 }
50 virtual Factory getFactory() { return Create; }
51private:
52 ReduceNoise(SkFlattenableReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {}
53 static SkFlattenable* Create(SkFlattenableReadBuffer& rb)
54 {
55 return new ReduceNoise(rb);
56 }
57};
58
59class Darken : public SkKernel33ProcMaskFilter {
60public:
61 Darken(int percent256) : SkKernel33ProcMaskFilter(percent256) {}
62 virtual uint8_t computeValue(uint8_t* const* srcRows)
63 {
64 int c = srcRows[1][1];
65 float f = c / 255.f;
reed@google.com82065d62011-02-07 15:30:46 +000066
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 if (c >= 0)
68 {
69 f = sqrtf(f);
70 }
71 else
72 {
73 f *= f;
74 }
75 SkASSERT(f >= 0 && f <= 1);
76 return (int)(f * 255);
77 }
78 virtual Factory getFactory() { return Create; }
79private:
80 Darken(SkFlattenableReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {}
81 static SkFlattenable* Create(SkFlattenableReadBuffer& rb)
82 {
83 return new Darken(rb);
84 }
85};
86
87static SkMaskFilter* makemf() { return new Darken(0x30); }
88
89//#ifdef TEST_CLICKX
90
91static void test_typefaceCache()
92{
reed@android.com44a63122009-05-30 02:40:28 +000093#ifdef ANDROID
reed@android.com069b8272009-03-04 15:31:48 +000094 SkTypeface* t0 = SkTypeface::CreateFromName("sans-serif",
95 SkTypeface::kNormal);
96 SkTypeface* t1 = SkTypeface::CreateFromName(NULL, SkTypeface::kNormal);
97 SkTypeface* t2 = SkTypeface::CreateFromName("arial", SkTypeface::kNormal);
98 SkTypeface* t3 = SkTypeface::CreateFromName("helvetica", SkTypeface::kItalic);
reed@google.com82065d62011-02-07 15:30:46 +000099
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 SkASSERT(t0 == t1);
101 SkASSERT(t0 == t2);
102 SkASSERT(t0 == t3);
reed@android.comda342a82008-12-23 18:37:08 +0000103#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104}
105
106static void test_breakText()
107{
108 SkPaint paint;
109 const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
110 size_t length = strlen(text);
111 SkScalar width = paint.measureText(text, length);
reed@google.com82065d62011-02-07 15:30:46 +0000112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 SkScalar mm = 0;
114 SkScalar nn = 0;
115 for (SkScalar w = 0; w <= width; w += SK_Scalar1)
116 {
117 SkScalar m;
118 size_t n = paint.breakText(text, length, w, &m,
119 SkPaint::kBackward_TextBufferDirection);
reed@google.com82065d62011-02-07 15:30:46 +0000120
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 SkASSERT(n <= length);
122 SkASSERT(m <= width);
reed@google.com82065d62011-02-07 15:30:46 +0000123
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 if (n == 0)
125 SkASSERT(m == 0);
126 else
127 {
128 // now assert that we're monotonic
129 if (n == nn)
130 SkASSERT(m == mm);
131 else
132 {
133 SkASSERT(n > nn);
134 SkASSERT(m > mm);
135 }
136 }
137 nn = n;
138 mm = m;
139 }
140
141 nn = paint.breakText(text, length, width, &mm);
142 SkASSERT(nn == length);
143 SkASSERT(mm == width);
144}
145
146static SkRandom gRand;
147
148class SkPowerMode : public SkXfermode {
149public:
150 SkPowerMode(SkScalar exponent) { this->init(exponent); }
151
152 virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count, const SkAlpha aa[]);
153
154 typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&);
reed@google.com82065d62011-02-07 15:30:46 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 // overrides for SkFlattenable
157 virtual Factory getFactory() { return Create; }
158 virtual void flatten(SkFlattenableWriteBuffer& b)
159 {
160 // this->INHERITED::flatten(b); How can we know if this is legal????
161 b.write32(SkScalarToFixed(fExp));
162 }
reed@google.com82065d62011-02-07 15:30:46 +0000163
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164private:
165 SkScalar fExp; // user's value
166 uint8_t fTable[256]; // cache
167
168 void init(SkScalar exponent);
169 SkPowerMode(SkFlattenableReadBuffer& b) : SkXfermode(b)
170 {
171 // read the exponent
172 this->init(SkFixedToScalar(b.readS32()));
173 }
174 static SkFlattenable* Create(SkFlattenableReadBuffer& b)
175 {
176 return SkNEW_ARGS(SkPowerMode, (b));
177 }
reed@google.com82065d62011-02-07 15:30:46 +0000178
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 typedef SkXfermode INHERITED;
180};
181
182void SkPowerMode::init(SkScalar e)
183{
184 fExp = e;
185 float ee = SkScalarToFloat(e);
reed@google.com82065d62011-02-07 15:30:46 +0000186
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 printf("------ %g\n", ee);
188 for (int i = 0; i < 256; i++)
189 {
190 float x = i / 255.f;
191 // printf(" %d %g", i, x);
192 x = powf(x, ee);
193 // printf(" %g", x);
194 int xx = SkScalarRound(SkFloatToScalar(x * 255));
195 // printf(" %d\n", xx);
196 fTable[i] = SkToU8(xx);
197 }
198}
199
200void SkPowerMode::xfer16(uint16_t dst[], const SkPMColor src[], int count, const SkAlpha aa[])
201{
202 for (int i = 0; i < count; i++)
203 {
204 SkPMColor c = src[i];
205 int r = SkGetPackedR32(c);
206 int g = SkGetPackedG32(c);
207 int b = SkGetPackedB32(c);
208 r = fTable[r];
209 g = fTable[g];
210 b = fTable[b];
211 dst[i] = SkPack888ToRGB16(r, g, b);
212 }
213}
214
215static const struct {
216 const char* fName;
217 uint32_t fFlags;
218 bool fFlushCache;
219} gHints[] = {
220 { "Linear", SkPaint::kLinearText_Flag, false },
221 { "Normal", 0, true },
222 { "Subpixel", SkPaint::kSubpixelText_Flag, true }
223};
224
225#ifdef SK_DEBUG
226 #define REPEAT_COUNT 1
227#else
reed@android.comf2b98d62010-12-20 18:26:13 +0000228 #define REPEAT_COUNT 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229#endif
230
231static int count_char_points(const SkPaint& paint, char c)
232{
233 SkPath path;
reed@google.com82065d62011-02-07 15:30:46 +0000234
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 paint.getTextPath(&c, 1, 0, 0, &path);
236 return path.getPoints(NULL, 0);
237}
238
239static int gOld, gNew, gCount;
240
241static void dump(int c, int oldc, int newc)
242{
243 if (oldc != newc)
244 {
245 gOld += oldc;
246 gNew += newc;
247 gCount += 1;
248 printf("char %c: old = %3d, new = %3d, reduction %g%%\n", c, oldc, newc, 100. * (oldc - newc) / oldc);
249 }
250}
251
252static void tab(int n)
253{
254// printf("[%d] ", n); return;
255 SkASSERT(n >= 0);
256 for (int i = 0; i < n; i++)
257 printf(" ");
258}
259
260#if 0
261#include "badrects.cpp"
262
263static void make_badrgn(SkRegion* rgn, int insetAmount)
264{
265 SkRect16 r, bounds;
266 int i;
reed@google.com82065d62011-02-07 15:30:46 +0000267
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268 rgn->setEmpty();
269 bounds.setEmpty();
270
271 for (i = 0; i < SK_ARRAY_COUNT(badrects); i++)
272 {
273 SkASSERT(badrects[i].width > 0 && badrects[i].height > 0);
274
275 r.set(badrects[i].x, badrects[i].y, badrects[i].x + badrects[i].width, badrects[i].y + badrects[i].height);
276 r.inset(insetAmount, insetAmount);
277 rgn->op(r, SkRegion::kUnion_Op);
278 bounds.join(r);
279 }
280 SkASSERT(bounds == rgn->getBounds());
281
282 for (i = 0; i < SK_ARRAY_COUNT(badrects); i++)
283 {
reed@google.com82065d62011-02-07 15:30:46 +0000284 r.set(badrects[i].x, badrects[i].y, badrects[i].x + badrects[i].width, badrects[i].y + badrects[i].height);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 SkASSERT(rgn->contains(r));
286 }
287}
288#endif
289
290static void draw_rgn(const SkRegion& rgn, SkCanvas* canvas, const SkPaint& paint)
291{
292 SkRect r;
293 SkRegion::Iterator iter(rgn);
reed@google.com82065d62011-02-07 15:30:46 +0000294
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 for (; !iter.done(); iter.next())
296 {
297 r.set(iter.rect());
298 canvas->drawRect(r, paint);
299 }
300}
301
302static void test_break(SkCanvas* canvas, const char text[], size_t length,
303 SkScalar x, SkScalar y, const SkPaint& paint,
304 SkScalar clickX)
305{
306 SkPaint linePaint;
reed@google.com82065d62011-02-07 15:30:46 +0000307
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308 linePaint.setAntiAlias(true);
reed@google.com82065d62011-02-07 15:30:46 +0000309
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310 SkScalar measured;
reed@google.com82065d62011-02-07 15:30:46 +0000311
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312 if (paint.breakText(text, length, clickX - x, &measured, SkPaint::kForward_TextBufferDirection))
313 {
314 linePaint.setColor(SK_ColorRED);
315 canvas->drawLine(x, y, x + measured, y, linePaint);
316 }
317
318 x += paint.measureText(text, length);
319 if (paint.breakText(text, length, x - clickX, &measured, SkPaint::kBackward_TextBufferDirection))
320 {
321 linePaint.setColor(SK_ColorBLUE);
322 canvas->drawLine(x - measured, y, x, y, linePaint);
323 }
324}
325
326static void test_poly()
327{
328 static const SkPoint dst[] = {
329 SkIntToScalar(2), SkIntToScalar(1),
330 SkIntToScalar(5), SkIntToScalar(1),
331 SkIntToScalar(5), SkIntToScalar(3),
332 SkIntToScalar(2), SkIntToScalar(3)
333 };
reed@google.com82065d62011-02-07 15:30:46 +0000334
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335 static const SkPoint src[] = {
336 SkIntToScalar(0), SkIntToScalar(0),
337 SkIntToScalar(1), SkIntToScalar(0),
338 SkIntToScalar(1), SkIntToScalar(1),
339 SkIntToScalar(0), SkIntToScalar(1)
340 };
reed@google.com82065d62011-02-07 15:30:46 +0000341
reed@android.com8a1c16f2008-12-17 15:59:43 +0000342 SkMatrix matrix;
reed@google.com82065d62011-02-07 15:30:46 +0000343
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344 if (matrix.setPolyToPoly(src, dst, 4))
345 {
346 SkPoint pt = { SK_Scalar1/2, SK_Scalar1/2 };
reed@google.com82065d62011-02-07 15:30:46 +0000347 matrix.mapPoints(&pt, 1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348 printf("---- x = %g y = %g\n", SkScalarToFloat(pt.fX), SkScalarToFloat(pt.fY));
349 }
350 else
351 printf("---- setPolyToPoly failed\n");
352}
353
354#include "SkColorShader.h"
355
356static void DrawTheText(SkCanvas* canvas, const char text[], size_t length,
357 SkScalar x, SkScalar y, const SkPaint& paint,
358 SkScalar clickX, SkMaskFilter* mf)
359{
360 SkPaint p(paint);
361
362#if 0
363 canvas->drawText(text, length, x, y, paint);
364#else
365 {
366 SkPoint pts[1000];
367 SkScalar xpos = x;
368 SkASSERT(length <= SK_ARRAY_COUNT(pts));
369 for (size_t i = 0; i < length; i++)
370 pts[i].set(xpos, y), xpos += paint.getTextSize();
371 canvas->drawPosText(text, length, pts, paint);
372 }
373#endif
374
375 p.setSubpixelText(true);
376 x += SkIntToScalar(180);
377 canvas->drawText(text, length, x, y, p);
378
379#ifdef TEST_CLICKX
380 test_break(canvas, text, length, x, y, p, clickX);
381#endif
382
383#ifdef SK_DEBUG
384 if (false)
385 {
386 SkColorShader shader;
387 p.setShader(&shader);
388 x += SkIntToScalar(180);
389 canvas->drawText(text, length, x, y, p);
390 p.setShader(NULL);
391 }
392
393 if (true)
394 {
395 // p.setMaskFilter(mf);
396 p.setSubpixelText(false);
397 p.setLinearText(true);
398 x += SkIntToScalar(180);
399 canvas->drawText(text, length, x, y, p);
400 }
401#endif
402}
403
404class TextSpeedView : public SkView {
405public:
406 TextSpeedView()
407 {
408 fMF = makemf();
409
410 fHints = 0;
411
412 if (false)
413 {
414 static const char extra[] = { '.', ',', ':', ';', '!' };
415 SkPaint paint, paint2;
416
reed@android.com069b8272009-03-04 15:31:48 +0000417 paint2.setTypeface(SkTypeface::CreateFromName(NULL,
418 SkTypeface::kItalic))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000419
420 for (int i = 0; i < 26; i++)
421 ::dump('a' + i, count_char_points(paint, 'a' + i), count_char_points(paint2, 'a' + i));
422 for (int j = 0; j < SK_ARRAY_COUNT(extra); j++)
423 ::dump(extra[j], count_char_points(paint, extra[j]), count_char_points(paint2, extra[j]));
424
425 printf("--- ave reduction = %g%%\n", 100. * (gOld - gNew) / gOld);
426 }
reed@google.com82065d62011-02-07 15:30:46 +0000427
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428 if (true)
429 {
430 SkPoint pts[] = { SkIntToScalar(20), 0, SkIntToScalar(256+20), 0 };
431 SkColor colors[] = { SkColorSetARGB(0, 255, 255, 255), SkColorSetARGB(255, 255, 255, 255) };
432 fGradient = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
433 }
reed@google.com82065d62011-02-07 15:30:46 +0000434
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435 fClickX = 0;
436
reed@google.com82065d62011-02-07 15:30:46 +0000437 test_breakText();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438 test_typefaceCache();
reed@android.com6b82d1a2009-06-03 02:35:01 +0000439// test_poly();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440 }
reed@google.com82065d62011-02-07 15:30:46 +0000441
reed@android.com8a1c16f2008-12-17 15:59:43 +0000442 virtual ~TextSpeedView()
443 {
444 fGradient->unref();
reed@google.com82065d62011-02-07 15:30:46 +0000445 SkSafeUnref(fMF);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446 }
447
448protected:
449 // overrides from SkEventSink
450 virtual bool onQuery(SkEvent* evt)
451 {
452 if (SampleCode::TitleQ(*evt))
453 {
454 SampleCode::TitleR(evt, "Text");
455 return true;
456 }
457 return this->INHERITED::onQuery(evt);
458 }
reed@google.com82065d62011-02-07 15:30:46 +0000459
reed@android.com8a1c16f2008-12-17 15:59:43 +0000460 void drawBG(SkCanvas* canvas)
461 {
462// canvas->drawColor(0xFFDDDDDD);
463 canvas->drawColor(SK_ColorWHITE);
464 // canvas->drawColor(SK_ColorBLACK);
465 }
reed@google.com82065d62011-02-07 15:30:46 +0000466
reed@android.com8a1c16f2008-12-17 15:59:43 +0000467 static void make_textstrip(SkBitmap* bm)
468 {
469 bm->setConfig(SkBitmap::kRGB_565_Config, 200, 18);
470 bm->allocPixels();
471 bm->eraseColor(SK_ColorWHITE);
reed@google.com82065d62011-02-07 15:30:46 +0000472
reed@android.com8a1c16f2008-12-17 15:59:43 +0000473 SkCanvas canvas(*bm);
474 SkPaint paint;
475 const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";
reed@google.com82065d62011-02-07 15:30:46 +0000476
reed@android.com8a1c16f2008-12-17 15:59:43 +0000477 paint.setFlags(paint.getFlags() | SkPaint::kAntiAlias_Flag
478 | SkPaint::kDevKernText_Flag);
479 paint.setTextSize(SkIntToScalar(14));
480 canvas.drawText(s, strlen(s), SkIntToScalar(8), SkIntToScalar(14), paint);
481 }
reed@google.com82065d62011-02-07 15:30:46 +0000482
reed@android.com8a1c16f2008-12-17 15:59:43 +0000483 static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand)
484 {
485 for (size_t i = 0; i < n; i++)
486 pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480);
487 }
reed@google.com82065d62011-02-07 15:30:46 +0000488
reed@android.com8a1c16f2008-12-17 15:59:43 +0000489 virtual void onDraw(SkCanvas* canvas)
490 {
reed@android.comcb342352010-07-22 18:27:53 +0000491 inval(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000492 if (false)
493 {
494 canvas->translate(SkIntToScalar(480), 0);
495 canvas->rotate(SkIntToScalar(90));
496 }
reed@google.com82065d62011-02-07 15:30:46 +0000497
reed@android.com8a1c16f2008-12-17 15:59:43 +0000498 this->drawBG(canvas);
reed@google.com82065d62011-02-07 15:30:46 +0000499
reed@android.com8a1c16f2008-12-17 15:59:43 +0000500 if (false)
501 {
502 SkPaint p;
reed@google.com82065d62011-02-07 15:30:46 +0000503
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504 p.setAntiAlias(true);
505 p.setSubpixelText(true);
506 // p.setLinearText(true);
reed@google.com82065d62011-02-07 15:30:46 +0000507
reed@android.com8a1c16f2008-12-17 15:59:43 +0000508 SkScalar size = SkIntToScalar(6);
509 SkMSec dur = 0;
510 const int LOOP = 16;
511 const int TIMES = 10;
reed@google.com82065d62011-02-07 15:30:46 +0000512
reed@android.com8a1c16f2008-12-17 15:59:43 +0000513 for (int times = 0; times < TIMES; times++)
514 {
515 SkMSec now = SkTime::GetMSecs();
516 for (int loop = 0; loop < LOOP; loop++)
517 {
518 p.setTextSize(size);
519 size += SK_Scalar1/5;
520 canvas->drawText("Hamburgefons", 12, SkIntToScalar(10), SkIntToScalar(50), p);
521 }
522 dur += SkTime::GetMSecs() - now;
523 SkGraphics::SetFontCacheUsed(0);
524 }
reed@google.com82065d62011-02-07 15:30:46 +0000525
reed@android.com8a1c16f2008-12-17 15:59:43 +0000526 printf("----- duration = %g\n", dur * 1.0 / TIMES);
527 this->inval(NULL);
528 return;
529 }
reed@google.com82065d62011-02-07 15:30:46 +0000530
reed@android.com8a1c16f2008-12-17 15:59:43 +0000531 if (false)
532 {
533 SkPaint p;
534 p.setAntiAlias(true);
535 for (int i = 6; i <= 36; i++)
536 {
537 SkRect r;
538 SkPaint::FontMetrics m;
539 p.setTextSize(SkIntToScalar(i));
540 p.getFontMetrics(&m);
541 int ascent = SkScalarRound(m.fAscent);
542 int descent = SkScalarRound(m.fDescent);
543 for (uint8_t c = ' '; c <= 127; c++)
544 {
545 p.getTextWidths(&c, 1, NULL, &r);
546 if (SkScalarRound(r.fTop) < ascent)
547 printf("PS %d --- %c [%d] top=%g, ascent=%g ymax=%g\n", i, c, c,
548 SkScalarToFloat(r.fTop), SkScalarToFloat(m.fAscent), SkScalarToFloat(m.fTop));
549 if (SkScalarRound(r.fBottom) > descent)
550 printf("PS %d --- %c [%d] bottom=%g, descent=%g ymin=%g\n", i, c, c,
551 SkScalarToFloat(r.fBottom), SkScalarToFloat(m.fDescent), SkScalarToFloat(m.fBottom));
552 }
553 }
554 }
reed@google.com82065d62011-02-07 15:30:46 +0000555
reed@android.com8a1c16f2008-12-17 15:59:43 +0000556 if (false)
557 {
558 SkPaint p;
559 p.setShader(fGradient);
560
561#ifdef SK_RELEASE
562 SkMSec now = SkTime::GetMSecs();
563 for (int i = 0; i < 100; i++)
564#endif
565 canvas->drawPaint(p);
566#ifdef SK_RELEASE
567 printf("----- %d ms\n", SkTime::GetMSecs() - now);
568 this->inval(NULL);
569#endif
570 return;
571 }
reed@google.com82065d62011-02-07 15:30:46 +0000572
reed@android.com8a1c16f2008-12-17 15:59:43 +0000573 if (false)
574 {
575 SkBitmap bm;
reed@google.com82065d62011-02-07 15:30:46 +0000576
reed@android.com8a1c16f2008-12-17 15:59:43 +0000577 make_textstrip(&bm);
578 canvas->translate(0, SkIntToScalar(50));
579 for (int i = 0; i < 10; i++)
580 {
581 float gamma = 1 + i * 0.2f;
582 SkPowerMode mode(SkFloatToScalar(1 / gamma));
583 SkPaint p;
584 p.setXfermode(&mode);
reed@google.com82065d62011-02-07 15:30:46 +0000585
reed@android.com8a1c16f2008-12-17 15:59:43 +0000586 canvas->drawBitmap(bm, 0, SkIntToScalar(i) * bm.height(), &p);
587 }
588 return;
589 }
reed@google.com82065d62011-02-07 15:30:46 +0000590
reed@android.com8a1c16f2008-12-17 15:59:43 +0000591 if (false)
592 {
593 SkPaint paint;
reed@google.com82065d62011-02-07 15:30:46 +0000594
reed@android.com8a1c16f2008-12-17 15:59:43 +0000595 paint.setAntiAlias(true);
596 paint.setDevKernText(true);
597 SkMSec now = SkTime::GetMSecs();
598 for (int i = 0; i < 1000000; i++)
599 {
600 paint.measureText("Hamburgefons", 15, NULL, NULL);
601 }
602 printf("--------- measure %d\n", SkTime::GetMSecs() - now);
603 this->inval(NULL);
604 return;
605 }
606
607 if (false)
608 {
609 SkRegion rgn;
610 SkPath path;
611 SkPaint paint;
reed@google.com82065d62011-02-07 15:30:46 +0000612
reed@android.com8a1c16f2008-12-17 15:59:43 +0000613 // make_badrgn(&rgn, -2);
reed@google.com82065d62011-02-07 15:30:46 +0000614
reed@android.com8a1c16f2008-12-17 15:59:43 +0000615 if (false)
616 {
617 paint.setColor(SK_ColorBLUE);
618 canvas->drawIRect(rgn.getBounds(), paint);
619 }
620 paint.setColor(SK_ColorRED);
621 draw_rgn(rgn, canvas, paint);
reed@google.com82065d62011-02-07 15:30:46 +0000622
reed@android.com8a1c16f2008-12-17 15:59:43 +0000623 rgn.getBoundaryPath(&path);
624 paint.setARGB(0x80, 0, 0, 0xFF);
625 canvas->drawPath(path, paint);
626 return;
627 }
628
629 if (false)
630 {
631 SkRect r = { SkIntToScalar(50), SkIntToScalar(50), SkIntToScalar(300), SkIntToScalar(300) };
632 SkPaint p;
reed@google.com82065d62011-02-07 15:30:46 +0000633
reed@android.com8a1c16f2008-12-17 15:59:43 +0000634 p.setStyle(SkPaint::kStroke_Style);
635 p.setAlpha(0x80);
636 p.setStrokeWidth(SkIntToScalar(20));
637 canvas->drawRect(r, p);
638 }
reed@google.com82065d62011-02-07 15:30:46 +0000639
reed@android.com8a1c16f2008-12-17 15:59:43 +0000640 if (false)
641 {
642 SkPaint p;
643 SkRect r = { SkIntToScalar(100), SkIntToScalar(100), SkIntToScalar(104), SkIntToScalar(104) };
644 // r.offset(SK_ScalarHalf, SK_ScalarHalf);
645 p.setStyle(SkPaint::kStroke_Style);
646 p.setStrokeWidth(SK_Scalar1*2);
647 // p.setAntiAliasOn(true);
648 canvas->drawRect(r, p);
649 return;
650 }
reed@google.com82065d62011-02-07 15:30:46 +0000651
reed@android.com8a1c16f2008-12-17 15:59:43 +0000652 if (false)
653 {
654 Sk64 aa, bb;
655 int64_t a = (int64_t)6062080 * -30596;
656 int64_t b = (int64_t)4816896 * 57957;
657 aa.setMul(6062080, -30596);
658 bb.setMul(4816896, 57957);
659
660 a += b;
661 b = a >> 16;
662
663// SkFixed c = aa.addGetFixed(bb);
reed@google.com82065d62011-02-07 15:30:46 +0000664
reed@android.com8a1c16f2008-12-17 15:59:43 +0000665 printf("%d %d\n", (int)a, a >> 32);
reed@google.com82065d62011-02-07 15:30:46 +0000666
reed@android.com8a1c16f2008-12-17 15:59:43 +0000667 SkBitmap bm;
668 SkPaint paint;
669 SkScalar scale = SkFloatToScalar(0.5625f);
670 SkScalar x = SkIntToScalar(100);
671 SkScalar y = SkIntToScalar(100);
reed@google.com82065d62011-02-07 15:30:46 +0000672
reed@android.com8a1c16f2008-12-17 15:59:43 +0000673 //paint.setFilterType(SkPaint::kBilinear_FilterType);
reed@google.com82065d62011-02-07 15:30:46 +0000674
reed@android.com8a1c16f2008-12-17 15:59:43 +0000675 SkImageDecoder::DecodeFile("/app_web_browser.png", &bm);
reed@google.com82065d62011-02-07 15:30:46 +0000676
reed@android.com8a1c16f2008-12-17 15:59:43 +0000677 // canvas->drawBitmap(bm, x, y, paint);
678 x += SkIntToScalar(100);
679 canvas->save();
680 canvas->translate(x, y);
681 canvas->scale(SkIntToScalar(2)/1, SkIntToScalar(2)/1);
682 canvas->translate(-x, -y);
683 canvas->drawBitmap(bm, x, y, &paint);
684 canvas->restore();
685 x += SkIntToScalar(100);
686 canvas->save();
687 canvas->translate(x, y);
688 canvas->scale(scale, scale);
689 canvas->translate(-x, -y);
690 // canvas->drawBitmap(bm, x, y, paint);
691 canvas->restore();
692 return;
693 }
reed@google.com82065d62011-02-07 15:30:46 +0000694
reed@android.com8a1c16f2008-12-17 15:59:43 +0000695 SkAutoCanvasRestore restore(canvas, false);
696 {
697 SkRect r;
698 r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20));
699 // canvas->saveLayer(&r, NULL, SkCanvas::kHasAlphaLayer_SaveFlag);
700 }
701
702 SkPaint paint;
703// const uint16_t glyphs[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };
704 int index = fHints % SK_ARRAY_COUNT(gHints);
705 index = 1;
706// const char* style = gHints[index].fName;
reed@google.com82065d62011-02-07 15:30:46 +0000707
reed@android.com8a1c16f2008-12-17 15:59:43 +0000708// canvas->translate(0, SkIntToScalar(50));
709
710 // canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint);
711
reed@android.com04d86c62010-01-25 22:02:44 +0000712 SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf")));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000713 paint.setAntiAlias(true);
714 paint.setFlags(paint.getFlags() | gHints[index].fFlags);
reed@google.com82065d62011-02-07 15:30:46 +0000715
reed@android.com8a1c16f2008-12-17 15:59:43 +0000716 SkMSec now = 0;
717 if (REPEAT_COUNT > 1)
718 now = SkTime::GetMSecs();
719
720 SkRect clip;
721 clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155));
reed@google.com82065d62011-02-07 15:30:46 +0000722
reed@android.com8a1c16f2008-12-17 15:59:43 +0000723 if (0) {
724 canvas->clipRect(clip);
725 }
726
727 if (0) {
reed@google.com82065d62011-02-07 15:30:46 +0000728 SkPath clipPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000729 clipPath.addOval(clip);
730 canvas->clipPath(clipPath);
731 }
732
733 const char* text = "Hamburgefons";
734 size_t length = strlen(text);
735
736#ifdef TEST_CLICKX
737 {
738 SkPaint p;
reed@google.com82065d62011-02-07 15:30:46 +0000739
reed@android.com8a1c16f2008-12-17 15:59:43 +0000740 p.setColor(SK_ColorGREEN);
741 p.setAntiAlias(true);
742 canvas->drawLine(fClickX, 0, fClickX, SkIntToScalar(1000), p);
743 }
744#endif
745
746 for (int j = 0; j < REPEAT_COUNT; j++)
747 {
748 SkScalar y = SkIntToScalar(0);
749 for (int i = 9; i <= 24; i++) {
750 paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/);
751 for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4; dx += SkIntToScalar(1) /* /4 */)
752 {
753 y += paint.getFontSpacing();
754 DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y, paint, fClickX, fMF);
755 }
756 }
757 if (gHints[index].fFlushCache) {
reed@android.comcb342352010-07-22 18:27:53 +0000758// SkGraphics::SetFontCacheUsed(0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000759 }
760 }
reed@google.com82065d62011-02-07 15:30:46 +0000761
reed@android.com8a1c16f2008-12-17 15:59:43 +0000762 if (REPEAT_COUNT > 1)
763 {
764 printf("--------- FPS = %g\n", REPEAT_COUNT * 1000. / (SkTime::GetMSecs() - now));
765 this->inval(NULL);
766 }
767 }
reed@google.com82065d62011-02-07 15:30:46 +0000768
769 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000770 {
771 fClickX = x;
772 this->inval(NULL);
773 return this->INHERITED::onFindClickHandler(x, y);
774 }
reed@google.com82065d62011-02-07 15:30:46 +0000775
776 virtual bool onClick(Click* click)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000777 {
778 return this->INHERITED::onClick(click);
779 }
reed@google.com82065d62011-02-07 15:30:46 +0000780
reed@android.com8a1c16f2008-12-17 15:59:43 +0000781private:
782 int fHints;
783 SkScalar fClickX;
784 SkMaskFilter* fMF;
785 SkShader* fGradient;
786
787 typedef SkView INHERITED;
788};
789
790//////////////////////////////////////////////////////////////////////////////
791
792static SkView* MyFactory() { return new TextSpeedView; }
793static SkViewRegister reg(MyFactory);
794