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; |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame^] | 29 | fDrawTarget = NULL; |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 30 | |
| 31 | fMaxVertices = 0; |
| 32 | fCurrTexture = NULL; |
| 33 | fCurrVertex = 0; |
| 34 | } |
| 35 | |
| 36 | void GrBatchedTextContext::finish() { |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame^] | 37 | GrAssert(fDrawTarget); |
| 38 | if (fDrawTarget) { |
| 39 | fDrawTarget->drawState()->disableStages(); |
| 40 | } |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 41 | fDrawTarget = NULL; |
| 42 | |
| 43 | this->INHERITED::finish(); |
| 44 | } |
| 45 | |
| 46 | void GrBatchedTextContext::reset() { |
| 47 | GrAssert(this->isValid()); |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame^] | 48 | GrAssert(fDrawTarget); |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 49 | fDrawTarget->resetVertexSource(); |
| 50 | fMaxVertices = 0; |
| 51 | fCurrVertex = 0; |
| 52 | fCurrTexture->unref(); |
| 53 | fCurrTexture = NULL; |
| 54 | } |
| 55 | |
| 56 | void 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 | |
| 66 | void GrBatchedTextContext::setupVertexBuff(void** vertexBuff, |
| 67 | GrVertexLayout vertexLayout) { |
| 68 | GrAssert(this->isValid()); |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame^] | 69 | GrAssert(fDrawTarget); |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 70 | 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.com | 193395c | 2012-03-30 17:35:12 +0000 | [diff] [blame] | 79 | fContext->flush(); |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 80 | 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 | } |