epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 16 | static void call_measure() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | SkPaint paint; |
| 18 | uint16_t text[32]; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 19 | SkRandom rand; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 20 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 25 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 26 | for (int i = 9; i < 36; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | SkPaint::FontMetrics m; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 28 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | paint.setTextSize(SkIntToScalar(i)); |
| 30 | paint.getFontMetrics(&m); |
| 31 | paint.measureText(text, sizeof(text)); |
| 32 | } |
| 33 | } |
| 34 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 35 | static void call_draw(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | SkPaint paint; |
| 37 | uint16_t text[32]; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 38 | SkRandom rand; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 39 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 44 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | SkScalar x = SkIntToScalar(10); |
| 46 | SkScalar y = SkIntToScalar(20); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 47 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | canvas->drawColor(SK_ColorWHITE); |
| 49 | for (int i = 9; i < 36; i++) |
| 50 | { |
| 51 | SkPaint::FontMetrics m; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 52 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 62 | static void* measure_proc(void* context) { |
| 63 | while (!gDone) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | call_measure(); |
| 65 | } |
| 66 | return NULL; |
| 67 | } |
| 68 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 69 | static void* draw_proc(void* context) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | SkBitmap* bm = (SkBitmap*)context; |
| 71 | SkCanvas canvas(*bm); |
| 72 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 73 | while (!gDone) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | call_draw(&canvas); |
| 75 | } |
| 76 | return NULL; |
| 77 | } |
| 78 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 79 | class FontCacheView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | public: |
| 81 | enum { N = 4 }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 82 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | pthread_t fMThreads[N]; |
| 84 | pthread_t fDThreads[N]; |
| 85 | SkBitmap fBitmaps[N]; |
| 86 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 87 | FontCacheView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | gDone = false; |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 89 | for (int i = 0; i < N; i++) { |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 90 | int status; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 91 | |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 92 | status = pthread_create(&fMThreads[i], NULL, measure_proc, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | SkASSERT(0 == status); |
| 94 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 95 | fBitmaps[i].allocPixels(SkImageInfo::Make(320, 240, |
| 96 | kRGB_565_SkColorType, |
| 97 | kOpaque_SkAlphaType)); |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 98 | status = pthread_create(&fDThreads[i], NULL, draw_proc, &fBitmaps[i]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | SkASSERT(0 == status); |
| 100 | } |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 101 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 103 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 104 | virtual ~FontCacheView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | gDone = true; |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 106 | for (int i = 0; i < N; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 114 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | protected: |
| 116 | // overrides from SkEventSink |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 117 | virtual bool onQuery(SkEvent* evt) { |
| 118 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | SampleCode::TitleR(evt, "FontCache"); |
| 120 | return true; |
| 121 | } |
| 122 | return this->INHERITED::onQuery(evt); |
| 123 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 124 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 125 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | SkScalar x = 0; |
| 127 | SkScalar y = 0; |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 128 | for (int i = 0; i < N; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | canvas->drawBitmap(fBitmaps[i], x, y); |
| 130 | x += SkIntToScalar(fBitmaps[i].width()); |
| 131 | } |
| 132 | this->inval(NULL); |
| 133 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 134 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | private: |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 136 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | ////////////////////////////////////////////////////////////////////////////// |
| 140 | |
| 141 | static SkView* MyFactory() { return new FontCacheView; } |
| 142 | static SkViewRegister reg(MyFactory); |