1 /*
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 */
7 #include "Sample.h"
8 #include "SkCanvas.h"
9 #include "SkGraphics.h"
10 #include "SkRandom.h"
11
12 #include <pthread.h>
13
call_measure()14 static void call_measure() {
15 SkPaint paint;
16 uint16_t text[32];
17 SkRandom rand;
18
19 paint.setAntiAlias(true);
20 paint.setTextEncoding(kUTF16_SkTextEncoding);
21 for (int j = 0; j < SK_ARRAY_COUNT(text); j++)
22 text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32);
23
24 for (int i = 9; i < 36; i++) {
25 SkFontMetrics m;
26
27 paint.setTextSize(SkIntToScalar(i));
28 paint.getFontMetrics(&m);
29 paint.measureText(text, sizeof(text));
30 }
31 }
32
call_draw(SkCanvas * canvas)33 static void call_draw(SkCanvas* canvas) {
34 SkPaint paint;
35 uint16_t text[32];
36 SkRandom rand;
37
38 paint.setAntiAlias(true);
39 paint.setTextEncoding(kUTF16_SkTextEncoding);
40 for (int j = 0; j < SK_ARRAY_COUNT(text); j++)
41 text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32);
42
43 SkScalar x = SkIntToScalar(10);
44 SkScalar y = SkIntToScalar(20);
45
46 canvas->drawColor(SK_ColorWHITE);
47 for (int i = 9; i < 36; i++)
48 {
49 SkFontMetrics m;
50
51 paint.setTextSize(SkIntToScalar(i));
52 paint.getFontMetrics(&m);
53 canvas->drawText(text, sizeof(text), x, y, paint);
54 y += m.fDescent - m.fAscent;
55 }
56 }
57
58 static bool gDone;
59
measure_proc(void * context)60 static void* measure_proc(void* context) {
61 while (!gDone) {
62 call_measure();
63 }
64 return nullptr;
65 }
66
draw_proc(void * context)67 static void* draw_proc(void* context) {
68 SkBitmap* bm = (SkBitmap*)context;
69 SkCanvas canvas(*bm);
70
71 while (!gDone) {
72 call_draw(&canvas);
73 }
74 return nullptr;
75 }
76
77 class FontCacheView : public Sample {
78 public:
79 enum { N = 4 };
80
81 pthread_t fMThreads[N];
82 pthread_t fDThreads[N];
83 SkBitmap fBitmaps[N];
84
FontCacheView()85 FontCacheView() {
86 gDone = false;
87 for (int i = 0; i < N; i++) {
88 int status;
89
90 status = pthread_create(&fMThreads[i], nullptr, measure_proc, nullptr);
91 SkASSERT(0 == status);
92
93 fBitmaps[i].allocPixels(SkImageInfo::Make(320, 240,
94 kRGB_565_SkColorType,
95 kOpaque_SkAlphaType));
96 status = pthread_create(&fDThreads[i], nullptr, draw_proc, &fBitmaps[i]);
97 SkASSERT(0 == status);
98 }
99 this->setBGColor(0xFFDDDDDD);
100 }
101
~FontCacheView()102 virtual ~FontCacheView() {
103 gDone = true;
104 for (int i = 0; i < N; i++) {
105 void* ret;
106 int status = pthread_join(fMThreads[i], &ret);
107 SkASSERT(0 == status);
108 status = pthread_join(fDThreads[i], &ret);
109 SkASSERT(0 == status);
110 }
111 }
112
113 protected:
onQuery(Sample::Event * evt)114 virtual bool onQuery(Sample::Event* evt) {
115 if (Sample::TitleQ(*evt)) {
116 Sample::TitleR(evt, "FontCache");
117 return true;
118 }
119 return this->INHERITED::onQuery(evt);
120 }
121
onDrawContent(SkCanvas * canvas)122 virtual void onDrawContent(SkCanvas* canvas) {
123 SkScalar x = 0;
124 SkScalar y = 0;
125 for (int i = 0; i < N; i++) {
126 canvas->drawBitmap(fBitmaps[i], x, y);
127 x += SkIntToScalar(fBitmaps[i].width());
128 }
129 this->inval(nullptr);
130 }
131
132 private:
133 typedef Sample INHERITED;
134 };
135
136 //////////////////////////////////////////////////////////////////////////////
137
MyFactory()138 static Sample* MyFactory() { return new FontCacheView; }
139 static SampleRegister reg(MyFactory);
140