blob: 1c6596258b5b4bd34f1aec39a295ec14169592a3 [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) {
joshualitt404d9d62015-07-22 11:00:32 -070035 int yOffset = 0;
joshualitte49109f2015-07-17 12:47:39 -070036 for (int r = 0; r < redraw; r++) {
37 for (int i = 0; i < blobs.count(); i++) {
joshualitt404d9d62015-07-22 11:00:32 -070038 const SkTextBlob* blob = blobs[i].fBlob.get();
39 const SkRect& bounds = blob->bounds();
40 yOffset += SkScalarCeilToInt(bounds.height());
joshualitte49109f2015-07-17 12:47:39 -070041 SkPaint paint;
joshualitt404d9d62015-07-22 11:00:32 -070042 canvas->drawTextBlob(blob, 0, SkIntToScalar(yOffset), paint);
joshualitte49109f2015-07-17 12:47:39 -070043 }
44 }
45}
46
47// limit this just so we don't take too long to draw
48#define MAX_TOTAL_TEXT 4096
49#define MAX_CHAR 256
50#define MAX_FAMILIES 5
51
52// This test hammers the GPU textblobcache and font atlas
joshualitt404d9d62015-07-22 11:00:32 -070053DEF_GPUTEST(TextBlobCache, reporter, factory) {
joshualitte49109f2015-07-17 12:47:39 -070054 // setup surface
55 uint32_t flags = 0;
56 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
57
joshualitt404d9d62015-07-22 11:00:32 -070058 GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType);
joshualitte49109f2015-07-17 12:47:39 -070059 SkImageInfo info = SkImageInfo::Make(1024, 768, kN32_SkColorType, kPremul_SkAlphaType);
60 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info,
61 0, &props));
62 REPORTER_ASSERT(reporter, surface);
63 if (!surface) {
64 return;
65 }
66
67 SkCanvas* canvas = surface->getCanvas();
68
69 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
70
71 int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);
72
73 // make a ton of text
74 uint16_t text[MAX_TOTAL_TEXT];
75 for (int i = 0; i < MAX_TOTAL_TEXT; i++) {
76 text[i] = i % MAX_CHAR;
77 }
78
79 // generate textblobs
80 SkTArray<TextBlobWrapper> blobs;
81 for (int i = 0; i < count; i++) {
82 SkPaint paint;
83 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
84 paint.setTextSize(256); // draw big glyphs to really stress the atlas
85
86 SkString familyName;
87 fm->getFamilyName(i, &familyName);
88 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
89 for (int j = 0; j < set->count(); ++j) {
90 SkFontStyle fs;
91 set->getStyle(j, &fs, NULL);
92
93 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
94
95 SkTextBlobBuilder builder;
96 for (int aa = 0; aa < 2; aa++) {
97 for (int subpixel = 0; subpixel < 2; subpixel++) {
98 for (int lcd = 0; lcd < 2; lcd++) {
99 paint.setAntiAlias(SkToBool(aa));
100 paint.setSubpixelText(SkToBool(subpixel));
101 paint.setLCDRenderText(SkToBool(lcd));
102 const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint,
103 MAX_TOTAL_TEXT,
104 0, 0,
105 NULL);
106 memcpy(run.glyphs, text, MAX_TOTAL_TEXT * sizeof(uint16_t));
107 }
108 }
109 }
110 SkNEW_APPEND_TO_TARRAY(&blobs, TextBlobWrapper, (builder.build()));
111 }
112 }
113
114 // test redraw
115 draw(canvas, 2, blobs);
116
117 // test draw after free
118 ctx->freeGpuResources();
119 draw(canvas, 1, blobs);
120
121 // test draw after abandon
122 ctx->abandonContext();
123 draw(canvas, 1, blobs);
124}
125#endif