reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | #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 | |
| 9 | static void call_measure() |
| 10 | { |
| 11 | SkPaint paint; |
| 12 | uint16_t text[32]; |
| 13 | SkRandom rand; |
| 14 | |
| 15 | paint.setAntiAlias(true); |
| 16 | paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); |
| 17 | for (int j = 0; j < SK_ARRAY_COUNT(text); j++) |
| 18 | text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32); |
| 19 | |
| 20 | for (int i = 9; i < 36; i++) |
| 21 | { |
| 22 | SkPaint::FontMetrics m; |
| 23 | |
| 24 | paint.setTextSize(SkIntToScalar(i)); |
| 25 | paint.getFontMetrics(&m); |
| 26 | paint.measureText(text, sizeof(text)); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | static void call_draw(SkCanvas* canvas) |
| 31 | { |
| 32 | SkPaint paint; |
| 33 | uint16_t text[32]; |
| 34 | SkRandom rand; |
| 35 | |
| 36 | paint.setAntiAlias(true); |
| 37 | paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); |
| 38 | for (int j = 0; j < SK_ARRAY_COUNT(text); j++) |
| 39 | text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32); |
| 40 | |
| 41 | SkScalar x = SkIntToScalar(10); |
| 42 | SkScalar y = SkIntToScalar(20); |
| 43 | |
| 44 | canvas->drawColor(SK_ColorWHITE); |
| 45 | for (int i = 9; i < 36; i++) |
| 46 | { |
| 47 | SkPaint::FontMetrics m; |
| 48 | |
| 49 | paint.setTextSize(SkIntToScalar(i)); |
| 50 | paint.getFontMetrics(&m); |
| 51 | canvas->drawText(text, sizeof(text), x, y, paint); |
| 52 | y += m.fDescent - m.fAscent; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static bool gDone; |
| 57 | |
| 58 | static void* measure_proc(void* context) |
| 59 | { |
| 60 | while (!gDone) |
| 61 | { |
| 62 | call_measure(); |
| 63 | } |
| 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | static void* draw_proc(void* context) |
| 68 | { |
| 69 | SkBitmap* bm = (SkBitmap*)context; |
| 70 | SkCanvas canvas(*bm); |
| 71 | |
| 72 | while (!gDone) |
| 73 | { |
| 74 | call_draw(&canvas); |
| 75 | } |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | class FontCacheView : public SkView { |
| 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 | { |
| 89 | gDone = false; |
| 90 | for (int i = 0; i < N; i++) |
| 91 | { |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 92 | int status; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 94 | status = pthread_create(&fMThreads[i], NULL, measure_proc, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | SkASSERT(0 == status); |
| 96 | |
| 97 | fBitmaps[i].setConfig(SkBitmap::kRGB_565_Config, 320, 240); |
| 98 | fBitmaps[i].allocPixels(); |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 99 | status = pthread_create(&fDThreads[i], NULL, draw_proc, &fBitmaps[i]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | SkASSERT(0 == status); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | virtual ~FontCacheView() |
| 105 | { |
| 106 | gDone = true; |
| 107 | for (int i = 0; i < N; i++) |
| 108 | { |
| 109 | void* ret; |
| 110 | int status = pthread_join(fMThreads[i], &ret); |
| 111 | SkASSERT(0 == status); |
| 112 | status = pthread_join(fDThreads[i], &ret); |
| 113 | SkASSERT(0 == status); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | protected: |
| 118 | // overrides from SkEventSink |
| 119 | virtual bool onQuery(SkEvent* evt) |
| 120 | { |
| 121 | if (SampleCode::TitleQ(*evt)) |
| 122 | { |
| 123 | SampleCode::TitleR(evt, "FontCache"); |
| 124 | return true; |
| 125 | } |
| 126 | return this->INHERITED::onQuery(evt); |
| 127 | } |
| 128 | |
| 129 | void drawBG(SkCanvas* canvas) |
| 130 | { |
| 131 | canvas->drawColor(0xFFDDDDDD); |
| 132 | // canvas->drawColor(SK_ColorWHITE); |
| 133 | } |
| 134 | |
| 135 | virtual void onDraw(SkCanvas* canvas) |
| 136 | { |
| 137 | this->drawBG(canvas); |
| 138 | |
| 139 | SkScalar x = 0; |
| 140 | SkScalar y = 0; |
| 141 | for (int i = 0; i < N; i++) |
| 142 | { |
| 143 | canvas->drawBitmap(fBitmaps[i], x, y); |
| 144 | x += SkIntToScalar(fBitmaps[i].width()); |
| 145 | } |
| 146 | this->inval(NULL); |
| 147 | } |
| 148 | |
| 149 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) |
| 150 | { |
| 151 | this->inval(NULL); |
| 152 | return this->INHERITED::onFindClickHandler(x, y); |
| 153 | } |
| 154 | |
| 155 | virtual bool onClick(Click* click) |
| 156 | { |
| 157 | return this->INHERITED::onClick(click); |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | typedef SkView INHERITED; |
| 162 | }; |
| 163 | |
| 164 | ////////////////////////////////////////////////////////////////////////////// |
| 165 | |
| 166 | static SkView* MyFactory() { return new FontCacheView; } |
| 167 | static SkViewRegister reg(MyFactory); |
| 168 | |