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 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 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 62 static void* measure_proc(void* context) { 63 while (!gDone) { 64 call_measure(); 65 } 66 return NULL; 67 } 68 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 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].setConfig(SkBitmap::kRGB_565_Config, 320, 240); 96 fBitmaps[i].allocPixels(); 97 status = pthread_create(&fDThreads[i], NULL, draw_proc, &fBitmaps[i]); 98 SkASSERT(0 == status); 99 } 100 this->setBGColor(0xFFDDDDDD); 101 } 102 103 virtual ~FontCacheView() { 104 gDone = true; 105 for (int i = 0; i < N; i++) { 106 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 } 113 114 protected: 115 // overrides from SkEventSink 116 virtual bool onQuery(SkEvent* evt) { 117 if (SampleCode::TitleQ(*evt)) { 118 SampleCode::TitleR(evt, "FontCache"); 119 return true; 120 } 121 return this->INHERITED::onQuery(evt); 122 } 123 124 virtual void onDrawContent(SkCanvas* canvas) { 125 SkScalar x = 0; 126 SkScalar y = 0; 127 for (int i = 0; i < N; i++) { 128 canvas->drawBitmap(fBitmaps[i], x, y); 129 x += SkIntToScalar(fBitmaps[i].width()); 130 } 131 this->inval(NULL); 132 } 133 134 private: 135 typedef SampleView INHERITED; 136 }; 137 138 ////////////////////////////////////////////////////////////////////////////// 139 140 static SkView* MyFactory() { return new FontCacheView; } 141 static SkViewRegister reg(MyFactory); 142 143