blob: 75645bd3052c292488d01ad2d9c0561fd21e51c0 [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 "SkGraphics.h"
5#include "SkRandom.h"
6
7#include <pthread.h>
8
9static void call_measure()
10{
11 SkPaint paint;
12 uint16_t text[32];
13 SkRandom rand;
14
15 paint.setAntiAlias(true);
16 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
17 for (int j = 0; j < SK_ARRAY_COUNT(text); j++)
18 text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32);
19
20 for (int i = 9; i < 36; i++)
21 {
22 SkPaint::FontMetrics m;
23
24 paint.setTextSize(SkIntToScalar(i));
25 paint.getFontMetrics(&m);
26 paint.measureText(text, sizeof(text));
27 }
28}
29
30static void call_draw(SkCanvas* canvas)
31{
32 SkPaint paint;
33 uint16_t text[32];
34 SkRandom rand;
35
36 paint.setAntiAlias(true);
37 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
38 for (int j = 0; j < SK_ARRAY_COUNT(text); j++)
39 text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32);
40
41 SkScalar x = SkIntToScalar(10);
42 SkScalar y = SkIntToScalar(20);
43
44 canvas->drawColor(SK_ColorWHITE);
45 for (int i = 9; i < 36; i++)
46 {
47 SkPaint::FontMetrics m;
48
49 paint.setTextSize(SkIntToScalar(i));
50 paint.getFontMetrics(&m);
51 canvas->drawText(text, sizeof(text), x, y, paint);
52 y += m.fDescent - m.fAscent;
53 }
54}
55
56static bool gDone;
57
58static void* measure_proc(void* context)
59{
60 while (!gDone)
61 {
62 call_measure();
63 }
64 return NULL;
65}
66
67static void* draw_proc(void* context)
68{
69 SkBitmap* bm = (SkBitmap*)context;
70 SkCanvas canvas(*bm);
71
72 while (!gDone)
73 {
74 call_draw(&canvas);
75 }
76 return NULL;
77}
78
79class FontCacheView : public SkView {
80public:
81 enum { N = 4 };
82
83 pthread_t fMThreads[N];
84 pthread_t fDThreads[N];
85 SkBitmap fBitmaps[N];
86
87 FontCacheView()
88 {
89 gDone = false;
90 for (int i = 0; i < N; i++)
91 {
reed@android.com7d970c72010-04-22 16:07:49 +000092 int status;
reed@android.com8a1c16f2008-12-17 15:59:43 +000093
reed@android.com7d970c72010-04-22 16:07:49 +000094 status = pthread_create(&fMThreads[i], NULL, measure_proc, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 SkASSERT(0 == status);
96
97 fBitmaps[i].setConfig(SkBitmap::kRGB_565_Config, 320, 240);
98 fBitmaps[i].allocPixels();
reed@android.com7d970c72010-04-22 16:07:49 +000099 status = pthread_create(&fDThreads[i], NULL, draw_proc, &fBitmaps[i]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 SkASSERT(0 == status);
101 }
102 }
103
104 virtual ~FontCacheView()
105 {
106 gDone = true;
107 for (int i = 0; i < N; i++)
108 {
109 void* ret;
110 int status = pthread_join(fMThreads[i], &ret);
111 SkASSERT(0 == status);
112 status = pthread_join(fDThreads[i], &ret);
113 SkASSERT(0 == status);
114 }
115 }
116
117protected:
118 // overrides from SkEventSink
119 virtual bool onQuery(SkEvent* evt)
120 {
121 if (SampleCode::TitleQ(*evt))
122 {
123 SampleCode::TitleR(evt, "FontCache");
124 return true;
125 }
126 return this->INHERITED::onQuery(evt);
127 }
128
129 void drawBG(SkCanvas* canvas)
130 {
131 canvas->drawColor(0xFFDDDDDD);
132// canvas->drawColor(SK_ColorWHITE);
133 }
134
135 virtual void onDraw(SkCanvas* canvas)
136 {
137 this->drawBG(canvas);
138
139 SkScalar x = 0;
140 SkScalar y = 0;
141 for (int i = 0; i < N; i++)
142 {
143 canvas->drawBitmap(fBitmaps[i], x, y);
144 x += SkIntToScalar(fBitmaps[i].width());
145 }
146 this->inval(NULL);
147 }
148
149 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y)
150 {
151 this->inval(NULL);
152 return this->INHERITED::onFindClickHandler(x, y);
153 }
154
155 virtual bool onClick(Click* click)
156 {
157 return this->INHERITED::onClick(click);
158 }
159
160private:
161 typedef SkView INHERITED;
162};
163
164//////////////////////////////////////////////////////////////////////////////
165
166static SkView* MyFactory() { return new FontCacheView; }
167static SkViewRegister reg(MyFactory);
168