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