Chromium Code Reviews| Index: gm/fontcache.cpp |
| diff --git a/gm/fontcache.cpp b/gm/fontcache.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f4a5b3cfd3dde8ee4f442ad241b8570ea6a9210d |
| --- /dev/null |
| +++ b/gm/fontcache.cpp |
| @@ -0,0 +1,116 @@ |
| +/* |
| + * Copyright 2013 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "gm.h" |
| +#include "SkCanvas.h" |
| +#include "SkGraphics.h" |
| +#include "SkTypeface.h" |
| + |
| +// GM to stress the GPU font cache |
| + |
| +const char* gFamilyNames[] = { |
| + "sans-serif", "serif", "monospace" |
| +}; |
| + |
| +const SkTypeface::Style gStyles[] = { |
| + SkTypeface::kNormal, SkTypeface::kItalic |
| +}; |
| + |
|
robertphillips
2013/10/09 15:23:49
Is "20" missing on purpose?
|
| +const int gTextSizes[] = { |
| + 10, 12, 14, 16, 18, 22, 24, 26, 28, 30 |
| +}; |
| + |
|
robertphillips
2013/10/09 15:23:49
DrawString?
|
| +static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x, |
| + SkScalar y, const SkPaint& paint) { |
| + canvas->drawText(text.c_str(), text.size(), x, y, paint); |
| + return x + paint.measureText(text.c_str(), text.size()); |
| +} |
| + |
| +class FontCacheGM : public skiagm::GM { |
| +public: |
| + FontCacheGM() { |
|
robertphillips
2013/10/09 15:23:49
Is this init expensive? Should it be moved to onOn
|
| + fTypefaceCount = 0; |
|
robertphillips
2013/10/09 15:23:49
Where does 16*1024*1024 come from?
|
| + SkGraphics::SetFontCacheLimit(16 * 1024 * 1024); |
|
robertphillips
2013/10/09 15:23:49
should "unsigned long" just be size_t?
|
| + for (unsigned long i = 0; i < SK_ARRAY_COUNT(gFamilyNames); ++i) { |
| + for (unsigned long j = 0; j < SK_ARRAY_COUNT(gStyles); ++j) { |
| + fTypefaces[fTypefaceCount++] = SkTypeface::CreateFromName(gFamilyNames[i], |
| + gStyles[j]); |
| + } |
| + } |
| + fName.set("fontcache"); |
| + } |
| + |
|
robertphillips
2013/10/09 15:23:49
virtual?
|
| + ~FontCacheGM() { |
| + for (size_t i = 0; i < fTypefaceCount; ++i) { |
| + SkSafeUnref(fTypefaces[i]); |
| + } |
| + } |
| + |
| +protected: |
|
robertphillips
2013/10/09 15:23:49
override
|
| + virtual SkString onShortName() { |
| + return fName; |
| + } |
| + |
|
robertphillips
2013/10/09 15:23:49
override
|
| + virtual SkISize onISize() { |
| + return SkISize::Make(640, 320); |
| + } |
| + |
| + virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| + SkScalar y = 32; |
| + SkPaint paint; |
| + paint.setAntiAlias(true); |
| + paint.setLCDRenderText(true); |
| + paint.setSubpixelText(true); |
| + |
| + SkString text("Ham"); |
| + |
| + // draw some initial text to partially fill the GPU cache |
| + for (size_t i = 0; i < 2; ++i) { |
| + paint.setTypeface(fTypefaces[i]); |
| + SkScalar x = 20; |
| + |
| + for (size_t j = 0; j < SK_ARRAY_COUNT(gTextSizes); ++j) { |
| + paint.setTextSize(gTextSizes[j]); |
| + x = drawString(canvas, text, x, y, paint) + 20; |
| + } |
| + y += 32; |
| + } |
| + |
| + // force a flush |
| + canvas->flush(); |
| + |
| + // draw again, and more to overflow the cache |
| + for (size_t i = 0; i < fTypefaceCount; ++i) { |
| + paint.setTypeface(fTypefaces[i]); |
| + SkScalar x = 20; |
| + |
| + for (size_t j = 0; j < SK_ARRAY_COUNT(gTextSizes); ++j) { |
| + paint.setTextSize(gTextSizes[j]); |
| + x = drawString(canvas, text, x, y, paint) + 20; |
| + } |
| + y += 32; |
| + } |
| + |
| + } |
| + |
| + virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| + // this GM is meant only for the GPU |
| + return kGPUOnly_Flag; |
| + } |
| + |
| +private: |
| + SkTypeface* fTypefaces[SK_ARRAY_COUNT(gFamilyNames)*SK_ARRAY_COUNT(gStyles)]; |
|
robertphillips
2013/10/09 15:23:49
Technically I don't think fTypefaceCount needs to
|
| + size_t fTypefaceCount; |
| + SkString fName; |
| + typedef GM INHERITED; |
| +}; |
| + |
| + |
| +////////////////////////////////////////////////////////////////////////////// |
| + |
| +DEF_GM( return SkNEW(FontCacheGM); ) |
| + |