blob: 97c95655044234c6ed193a65b9db731e6f729dcc [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00007#include "SampleCode.h"
8#include "SkView.h"
9#include "SkCanvas.h"
10#include "SkGraphics.h"
11#include "SkRandom.h"
12
13#include <pthread.h>
14
reed@google.com961ddb02011-05-05 14:03:48 +000015static void call_measure() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000016 SkPaint paint;
17 uint16_t text[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000018 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +000019
reed@android.com8a1c16f2008-12-17 15:59:43 +000020 paint.setAntiAlias(true);
21 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
22 for (int j = 0; j < SK_ARRAY_COUNT(text); j++)
23 text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32);
rmistry@google.comae933ce2012-08-23 18:19:56 +000024
reed@google.com961ddb02011-05-05 14:03:48 +000025 for (int i = 9; i < 36; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 SkPaint::FontMetrics m;
rmistry@google.comae933ce2012-08-23 18:19:56 +000027
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 paint.setTextSize(SkIntToScalar(i));
29 paint.getFontMetrics(&m);
30 paint.measureText(text, sizeof(text));
31 }
32}
33
reed@google.com961ddb02011-05-05 14:03:48 +000034static void call_draw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 SkPaint paint;
36 uint16_t text[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000037 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +000038
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 paint.setAntiAlias(true);
40 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
41 for (int j = 0; j < SK_ARRAY_COUNT(text); j++)
42 text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32);
rmistry@google.comae933ce2012-08-23 18:19:56 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 SkScalar x = SkIntToScalar(10);
45 SkScalar y = SkIntToScalar(20);
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 canvas->drawColor(SK_ColorWHITE);
48 for (int i = 9; i < 36; i++)
49 {
50 SkPaint::FontMetrics m;
rmistry@google.comae933ce2012-08-23 18:19:56 +000051
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 paint.setTextSize(SkIntToScalar(i));
53 paint.getFontMetrics(&m);
54 canvas->drawText(text, sizeof(text), x, y, paint);
55 y += m.fDescent - m.fAscent;
56 }
57}
58
59static bool gDone;
60
reed@google.com961ddb02011-05-05 14:03:48 +000061static void* measure_proc(void* context) {
62 while (!gDone) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 call_measure();
64 }
halcanary96fcdcc2015-08-27 07:41:13 -070065 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +000066}
67
reed@google.com961ddb02011-05-05 14:03:48 +000068static void* draw_proc(void* context) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 SkBitmap* bm = (SkBitmap*)context;
70 SkCanvas canvas(*bm);
71
reed@google.com961ddb02011-05-05 14:03:48 +000072 while (!gDone) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 call_draw(&canvas);
74 }
halcanary96fcdcc2015-08-27 07:41:13 -070075 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +000076}
77
reed@google.com961ddb02011-05-05 14:03:48 +000078class FontCacheView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079public:
80 enum { N = 4 };
rmistry@google.comae933ce2012-08-23 18:19:56 +000081
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 pthread_t fMThreads[N];
83 pthread_t fDThreads[N];
84 SkBitmap fBitmaps[N];
85
rmistry@google.comae933ce2012-08-23 18:19:56 +000086 FontCacheView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 gDone = false;
reed@google.com961ddb02011-05-05 14:03:48 +000088 for (int i = 0; i < N; i++) {
reed@android.com7d970c72010-04-22 16:07:49 +000089 int status;
rmistry@google.comae933ce2012-08-23 18:19:56 +000090
halcanary96fcdcc2015-08-27 07:41:13 -070091 status = pthread_create(&fMThreads[i], nullptr, measure_proc, nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 SkASSERT(0 == status);
93
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000094 fBitmaps[i].allocPixels(SkImageInfo::Make(320, 240,
95 kRGB_565_SkColorType,
96 kOpaque_SkAlphaType));
halcanary96fcdcc2015-08-27 07:41:13 -070097 status = pthread_create(&fDThreads[i], nullptr, 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 }
halcanary96fcdcc2015-08-27 07:41:13 -0700131 this->inval(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 }
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);