blob: 0b8187a0203da428074e82af9d128e7da6636d43 [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
reed@google.com961ddb02011-05-05 14:03:48 +00009static void call_measure() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000010 SkPaint paint;
11 uint16_t text[32];
12 SkRandom rand;
13
14 paint.setAntiAlias(true);
15 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
16 for (int j = 0; j < SK_ARRAY_COUNT(text); j++)
17 text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32);
18
reed@google.com961ddb02011-05-05 14:03:48 +000019 for (int i = 9; i < 36; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000020 SkPaint::FontMetrics m;
21
22 paint.setTextSize(SkIntToScalar(i));
23 paint.getFontMetrics(&m);
24 paint.measureText(text, sizeof(text));
25 }
26}
27
reed@google.com961ddb02011-05-05 14:03:48 +000028static void call_draw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 SkPaint paint;
30 uint16_t text[32];
31 SkRandom rand;
32
33 paint.setAntiAlias(true);
34 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
35 for (int j = 0; j < SK_ARRAY_COUNT(text); j++)
36 text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32);
37
38 SkScalar x = SkIntToScalar(10);
39 SkScalar y = SkIntToScalar(20);
40
41 canvas->drawColor(SK_ColorWHITE);
42 for (int i = 9; i < 36; i++)
43 {
44 SkPaint::FontMetrics m;
45
46 paint.setTextSize(SkIntToScalar(i));
47 paint.getFontMetrics(&m);
48 canvas->drawText(text, sizeof(text), x, y, paint);
49 y += m.fDescent - m.fAscent;
50 }
51}
52
53static bool gDone;
54
reed@google.com961ddb02011-05-05 14:03:48 +000055static void* measure_proc(void* context) {
56 while (!gDone) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 call_measure();
58 }
59 return NULL;
60}
61
reed@google.com961ddb02011-05-05 14:03:48 +000062static void* draw_proc(void* context) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 SkBitmap* bm = (SkBitmap*)context;
64 SkCanvas canvas(*bm);
65
reed@google.com961ddb02011-05-05 14:03:48 +000066 while (!gDone) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 call_draw(&canvas);
68 }
69 return NULL;
70}
71
reed@google.com961ddb02011-05-05 14:03:48 +000072class FontCacheView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073public:
74 enum { N = 4 };
75
76 pthread_t fMThreads[N];
77 pthread_t fDThreads[N];
78 SkBitmap fBitmaps[N];
79
reed@google.com961ddb02011-05-05 14:03:48 +000080 FontCacheView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 gDone = false;
reed@google.com961ddb02011-05-05 14:03:48 +000082 for (int i = 0; i < N; i++) {
reed@android.com7d970c72010-04-22 16:07:49 +000083 int status;
reed@android.com8a1c16f2008-12-17 15:59:43 +000084
reed@android.com7d970c72010-04-22 16:07:49 +000085 status = pthread_create(&fMThreads[i], NULL, measure_proc, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 SkASSERT(0 == status);
87
88 fBitmaps[i].setConfig(SkBitmap::kRGB_565_Config, 320, 240);
89 fBitmaps[i].allocPixels();
reed@android.com7d970c72010-04-22 16:07:49 +000090 status = pthread_create(&fDThreads[i], NULL, draw_proc, &fBitmaps[i]);
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 SkASSERT(0 == status);
92 }
reed@google.com961ddb02011-05-05 14:03:48 +000093 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 }
95
reed@google.com961ddb02011-05-05 14:03:48 +000096 virtual ~FontCacheView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 gDone = true;
reed@google.com961ddb02011-05-05 14:03:48 +000098 for (int i = 0; i < N; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 void* ret;
100 int status = pthread_join(fMThreads[i], &ret);
101 SkASSERT(0 == status);
102 status = pthread_join(fDThreads[i], &ret);
103 SkASSERT(0 == status);
104 }
105 }
106
107protected:
108 // overrides from SkEventSink
reed@google.com961ddb02011-05-05 14:03:48 +0000109 virtual bool onQuery(SkEvent* evt) {
110 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 SampleCode::TitleR(evt, "FontCache");
112 return true;
113 }
114 return this->INHERITED::onQuery(evt);
115 }
116
reed@google.com961ddb02011-05-05 14:03:48 +0000117 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 SkScalar x = 0;
119 SkScalar y = 0;
reed@google.com961ddb02011-05-05 14:03:48 +0000120 for (int i = 0; i < N; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 canvas->drawBitmap(fBitmaps[i], x, y);
122 x += SkIntToScalar(fBitmaps[i].width());
123 }
124 this->inval(NULL);
125 }
126
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127private:
reed@google.com961ddb02011-05-05 14:03:48 +0000128 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129};
130
131//////////////////////////////////////////////////////////////////////////////
132
133static SkView* MyFactory() { return new FontCacheView; }
134static SkViewRegister reg(MyFactory);
135