commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
| 7 | |
| 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkGraphics.h" |
| 11 | #include "SkTypeface.h" |
| 12 | |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 13 | // GM to stress the GPU font cache |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 14 | |
| 15 | const char* gFamilyNames[] = { |
jvanverth | 3f54175 | 2014-09-03 08:44:59 -0700 | [diff] [blame] | 16 | "sans-serif", "serif" |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 17 | }; |
| 18 | |
| 19 | const SkTypeface::Style gStyles[] = { |
jvanverth | 3f54175 | 2014-09-03 08:44:59 -0700 | [diff] [blame] | 20 | SkTypeface::kNormal, SkTypeface::kItalic, SkTypeface::kBold |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | const SkScalar gTextSizes[] = { |
jvanverth | 3f54175 | 2014-09-03 08:44:59 -0700 | [diff] [blame] | 24 | 192, 194, 196, 198, 200, 202, 204, 206 |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | #define TYPEFACE_COUNT (SK_ARRAY_COUNT(gFamilyNames)*SK_ARRAY_COUNT(gStyles)) |
| 28 | |
| 29 | static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x, |
| 30 | SkScalar y, const SkPaint& paint) { |
| 31 | canvas->drawText(text.c_str(), text.size(), x, y, paint); |
| 32 | return x + paint.measureText(text.c_str(), text.size()); |
| 33 | } |
| 34 | |
| 35 | class FontCacheGM : public skiagm::GM { |
| 36 | public: |
| 37 | FontCacheGM() { |
| 38 | for (size_t i = 0; i < TYPEFACE_COUNT; ++i) { |
| 39 | fTypefaces[i] = NULL; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | virtual ~FontCacheGM() { |
| 44 | for (size_t i = 0; i < TYPEFACE_COUNT; ++i) { |
| 45 | SkSafeUnref(fTypefaces[i]); |
| 46 | } |
| 47 | } |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 48 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 49 | protected: |
| 50 | virtual SkString onShortName() SK_OVERRIDE { |
jvanverth@google.com | 73d13d7 | 2013-10-09 18:53:35 +0000 | [diff] [blame] | 51 | return SkString("fontcache"); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | virtual SkISize onISize() SK_OVERRIDE { |
jvanverth | 3f54175 | 2014-09-03 08:44:59 -0700 | [diff] [blame] | 55 | return SkISize::Make(1280, 640); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 59 | int typefaceCount = 0; |
| 60 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFamilyNames); ++i) { |
| 61 | for (size_t j = 0; j < SK_ARRAY_COUNT(gStyles); ++j) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 62 | fTypefaces[typefaceCount++] = sk_tool_utils::create_portable_typeface(gFamilyNames[i], |
| 63 | gStyles[j]); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | } |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 67 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 68 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 69 | SkScalar y = 32; |
| 70 | SkPaint paint; |
| 71 | paint.setAntiAlias(true); |
| 72 | paint.setLCDRenderText(true); |
| 73 | paint.setSubpixelText(true); |
| 74 | |
jvanverth | 3f54175 | 2014-09-03 08:44:59 -0700 | [diff] [blame] | 75 | SkString text("H"); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 76 | |
jvanverth | 3f54175 | 2014-09-03 08:44:59 -0700 | [diff] [blame] | 77 | // draw enough to overflow the cache |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 78 | for (size_t i = 0; i < TYPEFACE_COUNT; ++i) { |
| 79 | paint.setTypeface(fTypefaces[i]); |
| 80 | SkScalar x = 20; |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 81 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 82 | for (size_t j = 0; j < SK_ARRAY_COUNT(gTextSizes); ++j) { |
| 83 | paint.setTextSize(gTextSizes[j]); |
jvanverth | 3f54175 | 2014-09-03 08:44:59 -0700 | [diff] [blame] | 84 | x = draw_string(canvas, text, x, y, paint) + 10; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 85 | } |
jvanverth | 3f54175 | 2014-09-03 08:44:59 -0700 | [diff] [blame] | 86 | y += 128; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 87 | } |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 88 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 92 | // this GM is meant only for the GPU |
| 93 | return kGPUOnly_Flag; |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | SkTypeface* fTypefaces[TYPEFACE_COUNT]; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 98 | typedef GM INHERITED; |
| 99 | }; |
| 100 | |
| 101 | |
| 102 | ////////////////////////////////////////////////////////////////////////////// |
| 103 | |
| 104 | DEF_GM( return SkNEW(FontCacheGM); ) |