blob: d5c2add4d4e8c780688782d9e0388bd9c2b6de6d [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 "SkGraphics.h"
12#include "SkRandom.h"
13
14#include <pthread.h>
15
reed@google.com961ddb02011-05-05 14:03:48 +000016static void call_measure() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000017 SkPaint paint;
18 uint16_t text[32];
19 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +000020
reed@android.com8a1c16f2008-12-17 15:59:43 +000021 paint.setAntiAlias(true);
22 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
23 for (int j = 0; j < SK_ARRAY_COUNT(text); j++)
24 text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32);
rmistry@google.comae933ce2012-08-23 18:19:56 +000025
reed@google.com961ddb02011-05-05 14:03:48 +000026 for (int i = 9; i < 36; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 SkPaint::FontMetrics m;
rmistry@google.comae933ce2012-08-23 18:19:56 +000028
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 paint.setTextSize(SkIntToScalar(i));
30 paint.getFontMetrics(&m);
31 paint.measureText(text, sizeof(text));
32 }
33}
34
reed@google.com961ddb02011-05-05 14:03:48 +000035static void call_draw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 SkPaint paint;
37 uint16_t text[32];
38 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +000039
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 paint.setAntiAlias(true);
41 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
42 for (int j = 0; j < SK_ARRAY_COUNT(text); j++)
43 text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32);
rmistry@google.comae933ce2012-08-23 18:19:56 +000044
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 SkScalar x = SkIntToScalar(10);
46 SkScalar y = SkIntToScalar(20);
rmistry@google.comae933ce2012-08-23 18:19:56 +000047
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 canvas->drawColor(SK_ColorWHITE);
49 for (int i = 9; i < 36; i++)
50 {
51 SkPaint::FontMetrics m;
rmistry@google.comae933ce2012-08-23 18:19:56 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 paint.setTextSize(SkIntToScalar(i));
54 paint.getFontMetrics(&m);
55 canvas->drawText(text, sizeof(text), x, y, paint);
56 y += m.fDescent - m.fAscent;
57 }
58}
59
60static bool gDone;
61
reed@google.com961ddb02011-05-05 14:03:48 +000062static void* measure_proc(void* context) {
63 while (!gDone) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 call_measure();
65 }
66 return NULL;
67}
68
reed@google.com961ddb02011-05-05 14:03:48 +000069static void* draw_proc(void* context) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 SkBitmap* bm = (SkBitmap*)context;
71 SkCanvas canvas(*bm);
72
reed@google.com961ddb02011-05-05 14:03:48 +000073 while (!gDone) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 call_draw(&canvas);
75 }
76 return NULL;
77}
78
reed@google.com961ddb02011-05-05 14:03:48 +000079class FontCacheView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000080public:
81 enum { N = 4 };
rmistry@google.comae933ce2012-08-23 18:19:56 +000082
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 pthread_t fMThreads[N];
84 pthread_t fDThreads[N];
85 SkBitmap fBitmaps[N];
86
rmistry@google.comae933ce2012-08-23 18:19:56 +000087 FontCacheView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 gDone = false;
reed@google.com961ddb02011-05-05 14:03:48 +000089 for (int i = 0; i < N; i++) {
reed@android.com7d970c72010-04-22 16:07:49 +000090 int status;
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
reed@android.com7d970c72010-04-22 16:07:49 +000092 status = pthread_create(&fMThreads[i], NULL, measure_proc, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 SkASSERT(0 == status);
94
95 fBitmaps[i].setConfig(SkBitmap::kRGB_565_Config, 320, 240);
96 fBitmaps[i].allocPixels();
reed@android.com7d970c72010-04-22 16:07:49 +000097 status = pthread_create(&fDThreads[i], NULL, draw_proc, &fBitmaps[i]);
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 SkASSERT(0 == status);
99 }
reed@google.com961ddb02011-05-05 14:03:48 +0000100 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
reed@google.com961ddb02011-05-05 14:03:48 +0000103 virtual ~FontCacheView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 gDone = true;
reed@google.com961ddb02011-05-05 14:03:48 +0000105 for (int i = 0; i < N; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 void* ret;
107 int status = pthread_join(fMThreads[i], &ret);
108 SkASSERT(0 == status);
109 status = pthread_join(fDThreads[i], &ret);
110 SkASSERT(0 == status);
111 }
112 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114protected:
115 // overrides from SkEventSink
reed@google.com961ddb02011-05-05 14:03:48 +0000116 virtual bool onQuery(SkEvent* evt) {
117 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 SampleCode::TitleR(evt, "FontCache");
119 return true;
120 }
121 return this->INHERITED::onQuery(evt);
122 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
reed@google.com961ddb02011-05-05 14:03:48 +0000124 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 SkScalar x = 0;
126 SkScalar y = 0;
reed@google.com961ddb02011-05-05 14:03:48 +0000127 for (int i = 0; i < N; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 canvas->drawBitmap(fBitmaps[i], x, y);
129 x += SkIntToScalar(fBitmaps[i].width());
130 }
131 this->inval(NULL);
132 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134private:
reed@google.com961ddb02011-05-05 14:03:48 +0000135 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136};
137
138//////////////////////////////////////////////////////////////////////////////
139
140static SkView* MyFactory() { return new FontCacheView; }
141static SkViewRegister reg(MyFactory);
142