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