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