bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 1 | |
| 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 | |
| 18 | GrBatchedTextContext::GrBatchedTextContext() { |
| 19 | } |
| 20 | |
| 21 | GrBatchedTextContext::~GrBatchedTextContext() { |
| 22 | } |
| 23 | |
| 24 | void GrBatchedTextContext::init(GrContext* context, |
| 25 | const GrPaint& grPaint, |
| 26 | const GrMatrix* extMatrix) { |
| 27 | this->INHERITED::init(context, grPaint, extMatrix); |
| 28 | fGrPaint = grPaint; |
| 29 | fDrawTarget = fContext->getTextTarget(fGrPaint); |
| 30 | |
| 31 | fMaxVertices = 0; |
| 32 | fCurrTexture = NULL; |
| 33 | fCurrVertex = 0; |
| 34 | } |
| 35 | |
| 36 | void GrBatchedTextContext::finish() { |
| 37 | fDrawTarget = NULL; |
| 38 | |
| 39 | this->INHERITED::finish(); |
| 40 | } |
| 41 | |
| 42 | void GrBatchedTextContext::reset() { |
| 43 | GrAssert(this->isValid()); |
| 44 | fDrawTarget->resetVertexSource(); |
| 45 | fMaxVertices = 0; |
| 46 | fCurrVertex = 0; |
| 47 | fCurrTexture->unref(); |
| 48 | fCurrTexture = NULL; |
| 49 | } |
| 50 | |
| 51 | void GrBatchedTextContext::prepareForGlyph(GrTexture* texture) { |
| 52 | GrAssert(this->isValid()); |
| 53 | GrAssert(texture); |
| 54 | if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) { |
| 55 | this->flush(); |
| 56 | fCurrTexture = texture; |
| 57 | fCurrTexture->ref(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void GrBatchedTextContext::setupVertexBuff(void** vertexBuff, |
| 62 | GrVertexLayout vertexLayout) { |
| 63 | GrAssert(this->isValid()); |
| 64 | if (NULL == *vertexBuff) { |
| 65 | // If we need to reserve vertices allow the draw target to suggest |
| 66 | // a number of verts to reserve and whether to perform a flush. |
| 67 | fMaxVertices = kMinRequestedVerts; |
| 68 | bool flush = fDrawTarget->geometryHints(vertexLayout, |
| 69 | &fMaxVertices, |
| 70 | NULL); |
| 71 | if (flush) { |
| 72 | this->flush(); |
bsalomon@google.com | 193395c | 2012-03-30 17:35:12 +0000 | [diff] [blame^] | 73 | fContext->flush(); |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 74 | fDrawTarget = fContext->getTextTarget(fGrPaint); |
| 75 | fMaxVertices = kDefaultRequestedVerts; |
| 76 | // ignore return, no point in flushing again. |
| 77 | fDrawTarget->geometryHints(vertexLayout, |
| 78 | &fMaxVertices, |
| 79 | NULL); |
| 80 | } |
| 81 | |
| 82 | int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads(); |
| 83 | if (fMaxVertices < kMinRequestedVerts) { |
| 84 | fMaxVertices = kDefaultRequestedVerts; |
| 85 | } else if (fMaxVertices > maxQuadVertices) { |
| 86 | // don't exceed the limit of the index buffer |
| 87 | fMaxVertices = maxQuadVertices; |
| 88 | } |
| 89 | bool success = fDrawTarget->reserveVertexAndIndexSpace( |
| 90 | vertexLayout, |
| 91 | fMaxVertices, |
| 92 | 0, |
| 93 | vertexBuff, |
| 94 | NULL); |
| 95 | GrAlwaysAssert(success); |
| 96 | } |
| 97 | } |