aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/TextBlobCacheTest.cpp
blob: b15af9083ab7dd36214a00c60ad2e877e04aaab1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "sk_tool_utils.h"
#include "SkCanvas.h"
#include "SkPaint.h"
#include "SkPoint.h"
#include "SkTextBlob.h"
#include "SkFontMgr.h"
#include "SkGraphics.h"
#include "SkSurface.h"
#include "SkTypeface.h"

#ifdef SK_BUILD_FOR_WIN
    #include "SkTypeface_win.h"
#endif

#include "Test.h"

#if SK_SUPPORT_GPU
#include "GrContextFactory.h"

struct TextBlobWrapper {
    explicit TextBlobWrapper(const SkTextBlob* blob) : fBlob(SkRef(blob)) {}
    TextBlobWrapper(const TextBlobWrapper& blob) : fBlob(SkRef(blob.fBlob.get())) {}

    SkAutoTUnref<const SkTextBlob> fBlob;
};

static void draw(SkCanvas* canvas, int redraw, const SkTArray<TextBlobWrapper>& blobs) {
    for (int r = 0; r < redraw; r++) {
        for (int i = 0; i < blobs.count(); i++) {
            SkPaint paint;
            canvas->drawTextBlob(blobs[i].fBlob.get(), 0, 0, paint);
        }
    }
}

// limit this just so we don't take too long to draw
#define MAX_TOTAL_TEXT 4096
#define MAX_CHAR 256
#define MAX_FAMILIES 5

// This test hammers the GPU textblobcache and font atlas
DEF_TEST(TextBlobCache, reporter) {
    SkAutoTDelete<GrContextFactory> grFactory(SkNEW(GrContextFactory));

    // setup surface
    uint32_t flags = 0;
    SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);

    GrContext* ctx = grFactory->get(GrContextFactory::kNative_GLContextType);
    SkImageInfo info = SkImageInfo::Make(1024, 768, kN32_SkColorType, kPremul_SkAlphaType);
    SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info,
                                                               0, &props));
    REPORTER_ASSERT(reporter, surface);
    if (!surface) {
        return;
    }

    SkCanvas* canvas = surface->getCanvas();

    SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());

    int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);

    // make a ton of text
    uint16_t text[MAX_TOTAL_TEXT];
    for (int i = 0; i < MAX_TOTAL_TEXT; i++) {
        text[i] = i % MAX_CHAR;
    }

    // generate textblobs
    SkTArray<TextBlobWrapper> blobs;
    for (int i = 0; i < count; i++) {
        SkPaint paint;
        paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
        paint.setTextSize(256); // draw big glyphs to really stress the atlas

        SkString familyName;
        fm->getFamilyName(i, &familyName);
        SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
        for (int j = 0; j < set->count(); ++j) {
            SkFontStyle fs;
            set->getStyle(j, &fs, NULL);

            SkSafeUnref(paint.setTypeface(set->createTypeface(j)));

            SkTextBlobBuilder builder;
            for (int aa = 0; aa < 2; aa++) {
                for (int subpixel = 0; subpixel < 2; subpixel++) {
                    for (int lcd = 0; lcd < 2; lcd++) {
                        paint.setAntiAlias(SkToBool(aa));
                        paint.setSubpixelText(SkToBool(subpixel));
                        paint.setLCDRenderText(SkToBool(lcd));
                        const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint,
                                                                                   MAX_TOTAL_TEXT,
                                                                                   0, 0,
                                                                                   NULL);
                        memcpy(run.glyphs, text, MAX_TOTAL_TEXT * sizeof(uint16_t));
                    }
                }
            }
            SkNEW_APPEND_TO_TARRAY(&blobs, TextBlobWrapper, (builder.build()));
        }
    }

    // test redraw
    draw(canvas, 2, blobs);

    // test draw after free
    ctx->freeGpuResources();
    draw(canvas, 1, blobs);

    // test draw after abandon
    ctx->abandonContext();
    draw(canvas, 1, blobs);
}
#endif