aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrBatchedTextContext.cpp
blob: 9de2badb17c00efdc948ef63c9ea7618b947ea86 (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

/*
 * Copyright 2010 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */



#include "GrBatchedTextContext.h"
#include "GrContext.h"
#include "GrDrawTarget.h"
#include "GrIndexBuffer.h"
#include "GrTextContext.h"


GrBatchedTextContext::GrBatchedTextContext() {
}

GrBatchedTextContext::~GrBatchedTextContext() {
}

void GrBatchedTextContext::init(GrContext* context,
                                const GrPaint& grPaint,
                                const GrMatrix* extMatrix) {
    this->INHERITED::init(context, grPaint, extMatrix);
    fGrPaint = grPaint;
    fDrawTarget = fContext->getTextTarget(fGrPaint);

    fMaxVertices = 0;
    fCurrTexture = NULL;
    fCurrVertex = 0;
}

void GrBatchedTextContext::finish() {
    fDrawTarget = NULL;

    this->INHERITED::finish();
}

void GrBatchedTextContext::reset() {
    GrAssert(this->isValid());
    fDrawTarget->resetVertexSource();
    fMaxVertices = 0;
    fCurrVertex = 0;
    fCurrTexture->unref();
    fCurrTexture = NULL;
}

void GrBatchedTextContext::prepareForGlyph(GrTexture* texture) {
    GrAssert(this->isValid());
    GrAssert(texture);
    if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
        this->flush();
        fCurrTexture = texture;
        fCurrTexture->ref();
    }
}

void GrBatchedTextContext::setupVertexBuff(void** vertexBuff,
                                           GrVertexLayout vertexLayout) {
    GrAssert(this->isValid());
    if (NULL == *vertexBuff) {
        // If we need to reserve vertices allow the draw target to suggest
        // a number of verts to reserve and whether to perform a flush.
        fMaxVertices = kMinRequestedVerts;
        bool flush = fDrawTarget->geometryHints(vertexLayout,
                                                &fMaxVertices,
                                                NULL);
        if (flush) {
            this->flush();
            fContext->flush();
            fDrawTarget = fContext->getTextTarget(fGrPaint);
            fMaxVertices = kDefaultRequestedVerts;
            // ignore return, no point in flushing again.
            fDrawTarget->geometryHints(vertexLayout,
                                       &fMaxVertices,
                                       NULL);
        }

        int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
        if (fMaxVertices < kMinRequestedVerts) {
            fMaxVertices = kDefaultRequestedVerts;
        } else if (fMaxVertices > maxQuadVertices) {
            // don't exceed the limit of the index buffer
            fMaxVertices = maxQuadVertices;
        }
        bool success = fDrawTarget->reserveVertexAndIndexSpace(
                                                   vertexLayout,
                                                   fMaxVertices,
                                                   0,
                                                   vertexBuff,
                                                   NULL);
        GrAlwaysAssert(success);
    }
}