blob: b15af9083ab7dd36214a00c60ad2e877e04aaab1 [file] [log] [blame]
joshualitte49109f2015-07-17 12:47:39 -07001/*
2 * Copyright 2015 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 "sk_tool_utils.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkPoint.h"
12#include "SkTextBlob.h"
13#include "SkFontMgr.h"
14#include "SkGraphics.h"
15#include "SkSurface.h"
16#include "SkTypeface.h"
17
18#ifdef SK_BUILD_FOR_WIN
19 #include "SkTypeface_win.h"
20#endif
21
22#include "Test.h"
23
24#if SK_SUPPORT_GPU
25#include "GrContextFactory.h"
26
27struct TextBlobWrapper {
28 explicit TextBlobWrapper(const SkTextBlob* blob) : fBlob(SkRef(blob)) {}
29 TextBlobWrapper(const TextBlobWrapper& blob) : fBlob(SkRef(blob.fBlob.get())) {}
30
31 SkAutoTUnref<const SkTextBlob> fBlob;
32};
33
34static void draw(SkCanvas* canvas, int redraw, const SkTArray<TextBlobWrapper>& blobs) {
35 for (int r = 0; r < redraw; r++) {
36 for (int i = 0; i < blobs.count(); i++) {
37 SkPaint paint;
38 canvas->drawTextBlob(blobs[i].fBlob.get(), 0, 0, paint);
39 }
40 }
41}
42
43// limit this just so we don't take too long to draw
44#define MAX_TOTAL_TEXT 4096
45#define MAX_CHAR 256
46#define MAX_FAMILIES 5
47
48// This test hammers the GPU textblobcache and font atlas
49DEF_TEST(TextBlobCache, reporter) {
50 SkAutoTDelete<GrContextFactory> grFactory(SkNEW(GrContextFactory));
51
52 // setup surface
53 uint32_t flags = 0;
54 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
55
56 GrContext* ctx = grFactory->get(GrContextFactory::kNative_GLContextType);
57 SkImageInfo info = SkImageInfo::Make(1024, 768, kN32_SkColorType, kPremul_SkAlphaType);
58 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info,
59 0, &props));
60 REPORTER_ASSERT(reporter, surface);
61 if (!surface) {
62 return;
63 }
64
65 SkCanvas* canvas = surface->getCanvas();
66
67 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
68
69 int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);
70
71 // make a ton of text
72 uint16_t text[MAX_TOTAL_TEXT];
73 for (int i = 0; i < MAX_TOTAL_TEXT; i++) {
74 text[i] = i % MAX_CHAR;
75 }
76
77 // generate textblobs
78 SkTArray<TextBlobWrapper> blobs;
79 for (int i = 0; i < count; i++) {
80 SkPaint paint;
81 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
82 paint.setTextSize(256); // draw big glyphs to really stress the atlas
83
84 SkString familyName;
85 fm->getFamilyName(i, &familyName);
86 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
87 for (int j = 0; j < set->count(); ++j) {
88 SkFontStyle fs;
89 set->getStyle(j, &fs, NULL);
90
91 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
92
93 SkTextBlobBuilder builder;
94 for (int aa = 0; aa < 2; aa++) {
95 for (int subpixel = 0; subpixel < 2; subpixel++) {
96 for (int lcd = 0; lcd < 2; lcd++) {
97 paint.setAntiAlias(SkToBool(aa));
98 paint.setSubpixelText(SkToBool(subpixel));
99 paint.setLCDRenderText(SkToBool(lcd));
100 const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint,
101 MAX_TOTAL_TEXT,
102 0, 0,
103 NULL);
104 memcpy(run.glyphs, text, MAX_TOTAL_TEXT * sizeof(uint16_t));
105 }
106 }
107 }
108 SkNEW_APPEND_TO_TARRAY(&blobs, TextBlobWrapper, (builder.build()));
109 }
110 }
111
112 // test redraw
113 draw(canvas, 2, blobs);
114
115 // test draw after free
116 ctx->freeGpuResources();
117 draw(canvas, 1, blobs);
118
119 // test draw after abandon
120 ctx->abandonContext();
121 draw(canvas, 1, blobs);
122}
123#endif