blob: 666d894e0415d85c0c22e423fe242ddcea4bb794 [file] [log] [blame]
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001
2/*
3 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10
11#include "GrBatchedTextContext.h"
12#include "GrContext.h"
13#include "GrDrawTarget.h"
14#include "GrIndexBuffer.h"
15#include "GrTextContext.h"
16
17
18GrBatchedTextContext::GrBatchedTextContext() {
19}
20
21GrBatchedTextContext::~GrBatchedTextContext() {
22}
23
24void GrBatchedTextContext::init(GrContext* context,
25 const GrPaint& grPaint,
26 const GrMatrix* extMatrix) {
27 this->INHERITED::init(context, grPaint, extMatrix);
28 fGrPaint = grPaint;
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000029 fDrawTarget = NULL;
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000030
31 fMaxVertices = 0;
32 fCurrTexture = NULL;
33 fCurrVertex = 0;
34}
35
36void GrBatchedTextContext::finish() {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000037 GrAssert(fDrawTarget);
38 if (fDrawTarget) {
39 fDrawTarget->drawState()->disableStages();
40 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000041 fDrawTarget = NULL;
42
43 this->INHERITED::finish();
44}
45
46void GrBatchedTextContext::reset() {
47 GrAssert(this->isValid());
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000048 GrAssert(fDrawTarget);
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000049 fDrawTarget->resetVertexSource();
50 fMaxVertices = 0;
51 fCurrVertex = 0;
52 fCurrTexture->unref();
53 fCurrTexture = NULL;
54}
55
56void GrBatchedTextContext::prepareForGlyph(GrTexture* texture) {
57 GrAssert(this->isValid());
58 GrAssert(texture);
59 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
60 this->flush();
61 fCurrTexture = texture;
62 fCurrTexture->ref();
63 }
64}
65
66void GrBatchedTextContext::setupVertexBuff(void** vertexBuff,
67 GrVertexLayout vertexLayout) {
68 GrAssert(this->isValid());
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000069 GrAssert(fDrawTarget);
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000070 if (NULL == *vertexBuff) {
71 // If we need to reserve vertices allow the draw target to suggest
72 // a number of verts to reserve and whether to perform a flush.
73 fMaxVertices = kMinRequestedVerts;
74 bool flush = fDrawTarget->geometryHints(vertexLayout,
75 &fMaxVertices,
76 NULL);
77 if (flush) {
78 this->flush();
bsalomon@google.com193395c2012-03-30 17:35:12 +000079 fContext->flush();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000080 fDrawTarget = fContext->getTextTarget(fGrPaint);
81 fMaxVertices = kDefaultRequestedVerts;
82 // ignore return, no point in flushing again.
83 fDrawTarget->geometryHints(vertexLayout,
84 &fMaxVertices,
85 NULL);
86 }
87
88 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
89 if (fMaxVertices < kMinRequestedVerts) {
90 fMaxVertices = kDefaultRequestedVerts;
91 } else if (fMaxVertices > maxQuadVertices) {
92 // don't exceed the limit of the index buffer
93 fMaxVertices = maxQuadVertices;
94 }
95 bool success = fDrawTarget->reserveVertexAndIndexSpace(
96 vertexLayout,
97 fMaxVertices,
98 0,
99 vertexBuff,
100 NULL);
101 GrAlwaysAssert(success);
102 }
103}