epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 10 | #include "GrGpu.h" |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 11 | #include "GrBufferAllocPool.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 12 | #include "GrClipIterator.h" |
| 13 | #include "GrIndexBuffer.h" |
bsalomon@google.com | 4043ae2 | 2011-08-02 14:19:11 +0000 | [diff] [blame] | 14 | #include "GrPathRenderer.h" |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 15 | #include "GrGLStencilBuffer.h" |
| 16 | #include "GrVertexBuffer.h" |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 17 | |
| 18 | // probably makes no sense for this to be less than a page |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 19 | static const size_t VERTEX_POOL_VB_SIZE = 1 << 18; |
| 20 | static const int VERTEX_POOL_VB_COUNT = 4; |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 21 | static const size_t INDEX_POOL_IB_SIZE = 1 << 16; |
| 22 | static const int INDEX_POOL_IB_COUNT = 4; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 24 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | |
| 26 | extern void gr_run_unittests(); |
| 27 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 28 | #define DEBUG_INVAL_BUFFER 0xdeadcafe |
| 29 | #define DEBUG_INVAL_START_IDX -1 |
| 30 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 31 | GrGpu::GrGpu() |
| 32 | : f8bitPaletteSupport(false) |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 33 | , fContext(NULL) |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 34 | , fVertexPool(NULL) |
| 35 | , fIndexPool(NULL) |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 36 | , fVertexPoolUseCnt(0) |
| 37 | , fIndexPoolUseCnt(0) |
| 38 | , fGeomPoolStateStack(&fGeoSrcStateStackStorage) |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 39 | , fQuadIndexBuffer(NULL) |
| 40 | , fUnitSquareVertexBuffer(NULL) |
| 41 | , fDefaultPathRenderer(NULL) |
| 42 | , fClientPathRenderer(NULL) |
| 43 | , fContextIsDirty(true) |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 44 | , fResourceHead(NULL) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 45 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | #if GR_DEBUG |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 47 | //gr_run_unittests(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 49 | |
| 50 | fGeomPoolStateStack.push_back(); |
| 51 | #if GR_DEBUG |
| 52 | GeometryPoolState& poolState = fGeomPoolStateStack.back(); |
| 53 | poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 54 | poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX; |
| 55 | poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
| 56 | poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX; |
| 57 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 58 | resetStats(); |
| 59 | } |
| 60 | |
| 61 | GrGpu::~GrGpu() { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 62 | releaseResources(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | } |
| 64 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 65 | void GrGpu::abandonResources() { |
| 66 | |
| 67 | while (NULL != fResourceHead) { |
| 68 | fResourceHead->abandon(); |
| 69 | } |
| 70 | |
| 71 | GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid()); |
| 72 | GrAssert(NULL == fUnitSquareVertexBuffer || |
| 73 | !fUnitSquareVertexBuffer->isValid()); |
| 74 | GrSafeSetNull(fQuadIndexBuffer); |
| 75 | GrSafeSetNull(fUnitSquareVertexBuffer); |
| 76 | delete fVertexPool; |
| 77 | fVertexPool = NULL; |
| 78 | delete fIndexPool; |
| 79 | fIndexPool = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | } |
| 81 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 82 | void GrGpu::releaseResources() { |
| 83 | |
| 84 | while (NULL != fResourceHead) { |
| 85 | fResourceHead->release(); |
| 86 | } |
| 87 | |
| 88 | GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid()); |
| 89 | GrAssert(NULL == fUnitSquareVertexBuffer || |
| 90 | !fUnitSquareVertexBuffer->isValid()); |
| 91 | GrSafeSetNull(fQuadIndexBuffer); |
| 92 | GrSafeSetNull(fUnitSquareVertexBuffer); |
| 93 | delete fVertexPool; |
| 94 | fVertexPool = NULL; |
| 95 | delete fIndexPool; |
| 96 | fIndexPool = NULL; |
| 97 | } |
| 98 | |
| 99 | void GrGpu::insertResource(GrResource* resource) { |
| 100 | GrAssert(NULL != resource); |
| 101 | GrAssert(this == resource->getGpu()); |
| 102 | GrAssert(NULL == resource->fNext); |
| 103 | GrAssert(NULL == resource->fPrevious); |
| 104 | |
| 105 | resource->fNext = fResourceHead; |
| 106 | if (NULL != fResourceHead) { |
| 107 | GrAssert(NULL == fResourceHead->fPrevious); |
| 108 | fResourceHead->fPrevious = resource; |
| 109 | } |
| 110 | fResourceHead = resource; |
| 111 | } |
| 112 | |
| 113 | void GrGpu::removeResource(GrResource* resource) { |
| 114 | GrAssert(NULL != resource); |
| 115 | GrAssert(NULL != fResourceHead); |
| 116 | |
| 117 | if (fResourceHead == resource) { |
| 118 | GrAssert(NULL == resource->fPrevious); |
| 119 | fResourceHead = resource->fNext; |
| 120 | } else { |
| 121 | GrAssert(NULL != fResourceHead); |
| 122 | resource->fPrevious->fNext = resource->fNext; |
| 123 | } |
| 124 | if (NULL != resource->fNext) { |
| 125 | resource->fNext->fPrevious = resource->fPrevious; |
| 126 | } |
| 127 | resource->fNext = NULL; |
| 128 | resource->fPrevious = NULL; |
| 129 | } |
| 130 | |
| 131 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 132 | void GrGpu::unimpl(const char msg[]) { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 133 | #if GR_DEBUG |
| 134 | GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg); |
| 135 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 136 | } |
| 137 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 138 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 140 | GrTexture* GrGpu::createTexture(const GrTextureDesc& desc, |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 141 | const void* srcData, size_t rowBytes) { |
| 142 | this->handleDirtyContext(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 143 | GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes); |
| 144 | if (NULL != tex && |
| 145 | (kRenderTarget_GrTextureFlagBit & desc.fFlags) && |
| 146 | !(kNoStencil_GrTextureFlagBit & desc.fFlags)) { |
| 147 | GrAssert(NULL != tex->asRenderTarget()); |
| 148 | // TODO: defer this and attach dynamically |
| 149 | if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) { |
| 150 | tex->unref(); |
| 151 | return NULL; |
| 152 | } |
| 153 | } |
| 154 | return tex; |
| 155 | } |
| 156 | |
| 157 | bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) { |
| 158 | // TODO: use a cache of stencil buffers rather than create per-rt. |
bsalomon@google.com | edc177d | 2011-08-05 15:46:40 +0000 | [diff] [blame^] | 159 | bool ret = this->createStencilBufferForRenderTarget(rt, rt->width(), |
| 160 | rt->height()); |
| 161 | if (ret) { |
| 162 | // Right now we're clearing the stencil buffer here after it is |
| 163 | // attached to an RT for the first time. When we start matching |
| 164 | // stencil buffers with smaller color targets this will no longer |
| 165 | // be correct because it won't be guaranteed to clear the entire |
| 166 | // sb. |
| 167 | // We used to clear down in the GL subclass using a special purpose |
| 168 | // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported |
| 169 | // FBO status. |
| 170 | GrRenderTarget* oldRT = fCurrDrawState.fRenderTarget; |
| 171 | fCurrDrawState.fRenderTarget = rt; |
| 172 | this->clearStencil(); |
| 173 | fCurrDrawState.fRenderTarget = oldRT; |
| 174 | } |
| 175 | return ret; |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 176 | } |
| 177 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 178 | GrRenderTarget* GrGpu::createRenderTargetFrom3DApiState() { |
| 179 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 180 | return this->onCreateRenderTargetFrom3DApiState(); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 181 | } |
| 182 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 183 | GrResource* GrGpu::createPlatformSurface(const GrPlatformSurfaceDesc& desc) { |
| 184 | this->handleDirtyContext(); |
| 185 | return this->onCreatePlatformSurface(desc); |
| 186 | } |
| 187 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 188 | GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) { |
| 189 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 190 | return this->onCreateVertexBuffer(size, dynamic); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) { |
| 194 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 195 | return this->onCreateIndexBuffer(size, dynamic); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 196 | } |
| 197 | |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 198 | void GrGpu::clear(const GrIRect* rect, GrColor color) { |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 199 | this->handleDirtyContext(); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 200 | this->onClear(rect, color); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void GrGpu::forceRenderTargetFlush() { |
| 204 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 205 | this->onForceRenderTargetFlush(); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 206 | } |
| 207 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 208 | bool GrGpu::readPixels(GrRenderTarget* target, |
| 209 | int left, int top, int width, int height, |
| 210 | GrPixelConfig config, void* buffer) { |
| 211 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 212 | this->handleDirtyContext(); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 213 | return this->onReadPixels(target, left, top, width, height, config, buffer); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | //////////////////////////////////////////////////////////////////////////////// |
| 217 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 218 | static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 219 | |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 220 | GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 221 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 222 | static inline void fill_indices(uint16_t* indices, int quadCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 223 | for (int i = 0; i < quadCount; ++i) { |
| 224 | indices[6 * i + 0] = 4 * i + 0; |
| 225 | indices[6 * i + 1] = 4 * i + 1; |
| 226 | indices[6 * i + 2] = 4 * i + 2; |
| 227 | indices[6 * i + 3] = 4 * i + 0; |
| 228 | indices[6 * i + 4] = 4 * i + 2; |
| 229 | indices[6 * i + 5] = 4 * i + 3; |
| 230 | } |
| 231 | } |
| 232 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 233 | const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 234 | if (NULL == fQuadIndexBuffer) { |
| 235 | static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS; |
| 236 | GrGpu* me = const_cast<GrGpu*>(this); |
| 237 | fQuadIndexBuffer = me->createIndexBuffer(SIZE, false); |
| 238 | if (NULL != fQuadIndexBuffer) { |
| 239 | uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock(); |
| 240 | if (NULL != indices) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 241 | fill_indices(indices, MAX_QUADS); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 242 | fQuadIndexBuffer->unlock(); |
| 243 | } else { |
| 244 | indices = (uint16_t*)GrMalloc(SIZE); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 245 | fill_indices(indices, MAX_QUADS); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 246 | if (!fQuadIndexBuffer->updateData(indices, SIZE)) { |
| 247 | fQuadIndexBuffer->unref(); |
| 248 | fQuadIndexBuffer = NULL; |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 249 | GrCrash("Can't get indices into buffer!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 250 | } |
| 251 | GrFree(indices); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return fQuadIndexBuffer; |
| 257 | } |
| 258 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 259 | const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const { |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 260 | if (NULL == fUnitSquareVertexBuffer) { |
| 261 | |
| 262 | static const GrPoint DATA[] = { |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 263 | { 0, 0 }, |
| 264 | { GR_Scalar1, 0 }, |
| 265 | { GR_Scalar1, GR_Scalar1 }, |
| 266 | { 0, GR_Scalar1 } |
| 267 | #if 0 |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 268 | GrPoint(0, 0), |
| 269 | GrPoint(GR_Scalar1,0), |
| 270 | GrPoint(GR_Scalar1,GR_Scalar1), |
| 271 | GrPoint(0, GR_Scalar1) |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 272 | #endif |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 273 | }; |
| 274 | static const size_t SIZE = sizeof(DATA); |
| 275 | |
| 276 | GrGpu* me = const_cast<GrGpu*>(this); |
| 277 | fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false); |
| 278 | if (NULL != fUnitSquareVertexBuffer) { |
| 279 | if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) { |
| 280 | fUnitSquareVertexBuffer->unref(); |
| 281 | fUnitSquareVertexBuffer = NULL; |
| 282 | GrCrash("Can't get vertices into buffer!"); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return fUnitSquareVertexBuffer; |
| 288 | } |
| 289 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 290 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 291 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 292 | // stencil settings to use when clip is in stencil |
| 293 | const GrStencilSettings GrGpu::gClipStencilSettings = { |
| 294 | kKeep_StencilOp, kKeep_StencilOp, |
| 295 | kKeep_StencilOp, kKeep_StencilOp, |
| 296 | kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc, |
| 297 | 0, 0, |
| 298 | 0, 0, |
| 299 | 0, 0 |
| 300 | }; |
| 301 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 302 | // mapping of clip-respecting stencil funcs to normal stencil funcs |
| 303 | // mapping depends on whether stencil-clipping is in effect. |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 304 | static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = { |
| 305 | {// Stencil-Clipping is DISABLED, effectively always inside the clip |
| 306 | // In the Clip Funcs |
| 307 | kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 308 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 309 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 310 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 311 | // Special in the clip func that forces user's ref to be 0. |
| 312 | kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 313 | // make ref 0 and do normal nequal. |
| 314 | }, |
| 315 | {// Stencil-Clipping is ENABLED |
| 316 | // In the Clip Funcs |
| 317 | kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 318 | // eq stencil clip bit, mask |
| 319 | // out user bits. |
| 320 | |
| 321 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 322 | // add stencil bit to mask and ref |
| 323 | |
| 324 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 325 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 326 | // for both of these we can add |
| 327 | // the clip bit to the mask and |
| 328 | // ref and compare as normal |
| 329 | // Special in the clip func that forces user's ref to be 0. |
| 330 | kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 331 | // make ref have only the clip bit set |
| 332 | // and make comparison be less |
| 333 | // 10..0 < 1..user_bits.. |
| 334 | } |
| 335 | }; |
| 336 | |
| 337 | GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) { |
| 338 | GrAssert(func >= 0); |
| 339 | if (func >= kBasicStencilFuncCount) { |
| 340 | GrAssert(func < kStencilFuncCount); |
| 341 | func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount]; |
| 342 | GrAssert(func >= 0 && func < kBasicStencilFuncCount); |
| 343 | } |
| 344 | return func; |
| 345 | } |
| 346 | |
| 347 | void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func, |
| 348 | bool clipInStencil, |
| 349 | unsigned int clipBit, |
| 350 | unsigned int userBits, |
| 351 | unsigned int* ref, |
| 352 | unsigned int* mask) { |
| 353 | if (func < kBasicStencilFuncCount) { |
| 354 | *mask &= userBits; |
| 355 | *ref &= userBits; |
| 356 | } else { |
| 357 | if (clipInStencil) { |
| 358 | switch (func) { |
| 359 | case kAlwaysIfInClip_StencilFunc: |
| 360 | *mask = clipBit; |
| 361 | *ref = clipBit; |
| 362 | break; |
| 363 | case kEqualIfInClip_StencilFunc: |
| 364 | case kLessIfInClip_StencilFunc: |
| 365 | case kLEqualIfInClip_StencilFunc: |
| 366 | *mask = (*mask & userBits) | clipBit; |
| 367 | *ref = (*ref & userBits) | clipBit; |
| 368 | break; |
| 369 | case kNonZeroIfInClip_StencilFunc: |
| 370 | *mask = (*mask & userBits) | clipBit; |
| 371 | *ref = clipBit; |
| 372 | break; |
| 373 | default: |
| 374 | GrCrash("Unknown stencil func"); |
| 375 | } |
| 376 | } else { |
| 377 | *mask &= userBits; |
| 378 | *ref &= userBits; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | //////////////////////////////////////////////////////////////////////////////// |
| 384 | |
| 385 | #define VISUALIZE_COMPLEX_CLIP 0 |
| 386 | |
| 387 | #if VISUALIZE_COMPLEX_CLIP |
| 388 | #include "GrRandom.h" |
| 389 | GrRandom gRandom; |
| 390 | #define SET_RANDOM_COLOR this->setColor(0xff000000 | gRandom.nextU()); |
| 391 | #else |
| 392 | #define SET_RANDOM_COLOR |
| 393 | #endif |
| 394 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 395 | bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 396 | const GrIRect* r = NULL; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 397 | GrIRect clipRect; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 398 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 399 | // we check this early because we need a valid |
| 400 | // render target to setup stencil clipping |
| 401 | // before even going into flushGraphicsState |
| 402 | if (NULL == fCurrDrawState.fRenderTarget) { |
| 403 | GrAssert(!"No render target bound."); |
| 404 | return false; |
| 405 | } |
| 406 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 407 | if (fCurrDrawState.fFlagBits & kClip_StateBit) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 408 | GrRenderTarget& rt = *fCurrDrawState.fRenderTarget; |
| 409 | |
| 410 | GrRect bounds; |
| 411 | GrRect rtRect; |
| 412 | rtRect.setLTRB(0, 0, |
| 413 | GrIntToScalar(rt.width()), GrIntToScalar(rt.height())); |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 414 | if (fClip.hasConservativeBounds()) { |
| 415 | bounds = fClip.getConservativeBounds(); |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 416 | if (!bounds.intersect(rtRect)) { |
| 417 | bounds.setEmpty(); |
| 418 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 419 | } else { |
| 420 | bounds = rtRect; |
| 421 | } |
| 422 | |
| 423 | bounds.roundOut(&clipRect); |
| 424 | if (clipRect.isEmpty()) { |
| 425 | clipRect.setLTRB(0,0,0,0); |
| 426 | } |
| 427 | r = &clipRect; |
| 428 | |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 429 | // use the stencil clip if we can't represent the clip as a rectangle. |
| 430 | fClipInStencil = !fClip.isRect() && !fClip.isEmpty() && |
| 431 | !bounds.isEmpty(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 432 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 433 | // TODO: dynamically attach a SB when needed. |
| 434 | GrStencilBuffer* stencilBuffer = rt.getStencilBuffer(); |
| 435 | if (fClipInStencil && NULL == stencilBuffer) { |
| 436 | return false; |
| 437 | } |
bsalomon@google.com | a16d650 | 2011-08-02 14:07:52 +0000 | [diff] [blame] | 438 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 439 | if (fClipInStencil && |
| 440 | stencilBuffer->mustRenderClip(fClip, rt.width(), rt.height())) { |
| 441 | |
| 442 | stencilBuffer->setLastClip(fClip, rt.width(), rt.height()); |
| 443 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 444 | // we set the current clip to the bounds so that our recursive |
| 445 | // draws are scissored to them. We use the copy of the complex clip |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 446 | // we just stashed on the SB to render from. We set it back after |
| 447 | // we finish drawing it into the stencil. |
| 448 | const GrClip& clip = stencilBuffer->getLastClip(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 449 | fClip.setFromRect(bounds); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 450 | |
| 451 | AutoStateRestore asr(this); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 452 | AutoGeometryPush agp(this); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 453 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 454 | this->setViewMatrix(GrMatrix::I()); |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 455 | this->clearStencilClip(clipRect); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 456 | this->flushScissor(NULL); |
| 457 | #if !VISUALIZE_COMPLEX_CLIP |
| 458 | this->enableState(kNoColorWrites_StateBit); |
| 459 | #else |
| 460 | this->disableState(kNoColorWrites_StateBit); |
| 461 | #endif |
| 462 | int count = clip.getElementCount(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 463 | int clipBit = stencilBuffer->bits(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 464 | clipBit = (1 << (clipBit-1)); |
| 465 | |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 466 | // often we'll see the first two elements of the clip are |
| 467 | // the full rt size and another element intersected with it. |
| 468 | // We can skip the first full-size rect and save a big rect draw. |
| 469 | int firstElement = 0; |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 470 | if (clip.getElementCount() > 1 && |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 471 | kRect_ClipType == clip.getElementType(0) && |
| 472 | kIntersect_SetOp == clip.getOp(1)&& |
| 473 | clip.getRect(0).contains(bounds)) { |
| 474 | firstElement = 1; |
| 475 | } |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 476 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 477 | // walk through each clip element and perform its set op |
| 478 | // with the existing clip. |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 479 | for (int c = firstElement; c < count; ++c) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 480 | GrPathFill fill; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 481 | bool fillInverted; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 482 | // enabled at bottom of loop |
| 483 | this->disableState(kModifyStencilClip_StateBit); |
| 484 | |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 485 | bool canRenderDirectToStencil; // can the clip element be drawn |
| 486 | // directly to the stencil buffer |
| 487 | // with a non-inverted fill rule |
| 488 | // without extra passes to |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 489 | // resolve in/out status. |
| 490 | |
| 491 | GrPathRenderer* pr = NULL; |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 492 | const GrPath* clipPath = NULL; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 493 | GrPathRenderer::AutoClearPath arp; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 494 | if (kRect_ClipType == clip.getElementType(c)) { |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 495 | canRenderDirectToStencil = true; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 496 | fill = kEvenOdd_PathFill; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 497 | fillInverted = false; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 498 | } else { |
| 499 | fill = clip.getPathFill(c); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 500 | fillInverted = IsFillInverted(fill); |
| 501 | fill = NonInvertedFill(fill); |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 502 | clipPath = &clip.getPath(c); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 503 | pr = this->getClipPathRenderer(*clipPath, fill); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 504 | canRenderDirectToStencil = |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 505 | !pr->requiresStencilPass(this, *clipPath, fill); |
| 506 | arp.set(pr, this, clipPath, fill, NULL); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 507 | } |
| 508 | |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 509 | GrSetOp op = firstElement == c ? kReplace_SetOp : clip.getOp(c); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 510 | int passes; |
| 511 | GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses]; |
| 512 | |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 513 | bool canDrawDirectToClip; // Given the renderer, the element, |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 514 | // fill rule, and set operation can |
| 515 | // we render the element directly to |
| 516 | // stencil bit used for clipping. |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 517 | canDrawDirectToClip = |
| 518 | GrStencilSettings::GetClipPasses(op, |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 519 | canRenderDirectToStencil, |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 520 | clipBit, |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 521 | fillInverted, |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 522 | &passes, stencilSettings); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 523 | |
| 524 | // draw the element to the client stencil bits if necessary |
| 525 | if (!canDrawDirectToClip) { |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 526 | static const GrStencilSettings gDrawToStencil = { |
| 527 | kIncClamp_StencilOp, kIncClamp_StencilOp, |
| 528 | kIncClamp_StencilOp, kIncClamp_StencilOp, |
| 529 | kAlways_StencilFunc, kAlways_StencilFunc, |
| 530 | 0xffffffff, 0xffffffff, |
| 531 | 0x00000000, 0x00000000, |
| 532 | 0xffffffff, 0xffffffff, |
| 533 | }; |
| 534 | SET_RANDOM_COLOR |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 535 | if (kRect_ClipType == clip.getElementType(c)) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 536 | this->setStencil(gDrawToStencil); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 537 | this->drawSimpleRect(clip.getRect(c), NULL, 0); |
| 538 | } else { |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 539 | if (canRenderDirectToStencil) { |
| 540 | this->setStencil(gDrawToStencil); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 541 | pr->drawPath(0); |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 542 | } else { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 543 | pr->drawPathToStencil(); |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 544 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
| 548 | // now we modify the clip bit by rendering either the clip |
| 549 | // element directly or a bounding rect of the entire clip. |
| 550 | this->enableState(kModifyStencilClip_StateBit); |
| 551 | for (int p = 0; p < passes; ++p) { |
| 552 | this->setStencil(stencilSettings[p]); |
| 553 | if (canDrawDirectToClip) { |
| 554 | if (kRect_ClipType == clip.getElementType(c)) { |
| 555 | SET_RANDOM_COLOR |
| 556 | this->drawSimpleRect(clip.getRect(c), NULL, 0); |
| 557 | } else { |
| 558 | SET_RANDOM_COLOR |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 559 | pr->drawPath(0); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 560 | } |
| 561 | } else { |
| 562 | SET_RANDOM_COLOR |
thakis@chromium.org | 441d7da | 2011-06-07 04:03:17 +0000 | [diff] [blame] | 563 | this->drawSimpleRect(bounds, NULL, 0); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 564 | } |
| 565 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 566 | } |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 567 | // restore clip |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 568 | fClip = clip; |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 569 | // recusive draws would have disabled this since they drew with |
| 570 | // the clip bounds as clip. |
| 571 | fClipInStencil = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 572 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 573 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 574 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 575 | // Must flush the scissor after graphics state |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 576 | if (!this->flushGraphicsState(type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 577 | return false; |
| 578 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 579 | this->flushScissor(r); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 580 | return true; |
| 581 | } |
| 582 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 583 | GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path, |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 584 | GrPathFill fill) { |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 585 | if (NULL != fClientPathRenderer && |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 586 | fClientPathRenderer->canDrawPath(path, fill)) { |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 587 | return fClientPathRenderer; |
| 588 | } else { |
| 589 | if (NULL == fDefaultPathRenderer) { |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 590 | fDefaultPathRenderer = |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 591 | new GrDefaultPathRenderer(this->supportsTwoSidedStencil(), |
| 592 | this->supportsStencilWrapOps()); |
| 593 | } |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 594 | GrAssert(fDefaultPathRenderer->canDrawPath(path, fill)); |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 595 | return fDefaultPathRenderer; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 600 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 601 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 602 | void GrGpu::geometrySourceWillPush() { |
| 603 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 604 | if (kArray_GeometrySrcType == geoSrc.fVertexSrc || |
| 605 | kReserved_GeometrySrcType == geoSrc.fVertexSrc) { |
| 606 | this->finalizeReservedVertices(); |
| 607 | } |
| 608 | if (kArray_GeometrySrcType == geoSrc.fIndexSrc || |
| 609 | kReserved_GeometrySrcType == geoSrc.fIndexSrc) { |
| 610 | this->finalizeReservedIndices(); |
| 611 | } |
| 612 | GeometryPoolState& newState = fGeomPoolStateStack.push_back(); |
| 613 | #if GR_DEBUG |
| 614 | newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 615 | newState.fPoolStartVertex = DEBUG_INVAL_START_IDX; |
| 616 | newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
| 617 | newState.fPoolStartIndex = DEBUG_INVAL_START_IDX; |
| 618 | #endif |
| 619 | } |
| 620 | |
| 621 | void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) { |
| 622 | // if popping last entry then pops are unbalanced with pushes |
| 623 | GrAssert(fGeomPoolStateStack.count() > 1); |
| 624 | fGeomPoolStateStack.pop_back(); |
| 625 | } |
| 626 | |
| 627 | void GrGpu::onDrawIndexed(GrPrimitiveType type, |
| 628 | int startVertex, |
| 629 | int startIndex, |
| 630 | int vertexCount, |
| 631 | int indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 632 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 633 | this->handleDirtyContext(); |
| 634 | |
| 635 | if (!this->setupClipAndFlushState(type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 636 | return; |
| 637 | } |
| 638 | |
| 639 | #if GR_COLLECT_STATS |
| 640 | fStats.fVertexCnt += vertexCount; |
| 641 | fStats.fIndexCnt += indexCount; |
| 642 | fStats.fDrawCnt += 1; |
| 643 | #endif |
| 644 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 645 | int sVertex = startVertex; |
| 646 | int sIndex = startIndex; |
| 647 | setupGeometry(&sVertex, &sIndex, vertexCount, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 648 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 649 | this->onGpuDrawIndexed(type, sVertex, sIndex, |
| 650 | vertexCount, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 651 | } |
| 652 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 653 | void GrGpu::onDrawNonIndexed(GrPrimitiveType type, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 654 | int startVertex, |
| 655 | int vertexCount) { |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 656 | this->handleDirtyContext(); |
| 657 | |
| 658 | if (!this->setupClipAndFlushState(type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 659 | return; |
| 660 | } |
| 661 | #if GR_COLLECT_STATS |
| 662 | fStats.fVertexCnt += vertexCount; |
| 663 | fStats.fDrawCnt += 1; |
| 664 | #endif |
| 665 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 666 | int sVertex = startVertex; |
| 667 | setupGeometry(&sVertex, NULL, vertexCount, 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 668 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 669 | this->onGpuDrawNonIndexed(type, sVertex, vertexCount); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | void GrGpu::finalizeReservedVertices() { |
| 673 | GrAssert(NULL != fVertexPool); |
| 674 | fVertexPool->unlock(); |
| 675 | } |
| 676 | |
| 677 | void GrGpu::finalizeReservedIndices() { |
| 678 | GrAssert(NULL != fIndexPool); |
| 679 | fIndexPool->unlock(); |
| 680 | } |
| 681 | |
| 682 | void GrGpu::prepareVertexPool() { |
| 683 | if (NULL == fVertexPool) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 684 | GrAssert(0 == fVertexPoolUseCnt); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 685 | fVertexPool = new GrVertexBufferAllocPool(this, true, |
| 686 | VERTEX_POOL_VB_SIZE, |
bsalomon@google.com | 7a5af8b | 2011-02-18 18:40:42 +0000 | [diff] [blame] | 687 | VERTEX_POOL_VB_COUNT); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 688 | fVertexPool->releaseGpuRef(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 689 | } else if (!fVertexPoolUseCnt) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 690 | // the client doesn't have valid data in the pool |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 691 | fVertexPool->reset(); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | void GrGpu::prepareIndexPool() { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 696 | if (NULL == fIndexPool) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 697 | GrAssert(0 == fIndexPoolUseCnt); |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 698 | fIndexPool = new GrIndexBufferAllocPool(this, true, |
| 699 | INDEX_POOL_IB_SIZE, |
| 700 | INDEX_POOL_IB_COUNT); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 701 | fIndexPool->releaseGpuRef(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 702 | } else if (!fIndexPoolUseCnt) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 703 | // the client doesn't have valid data in the pool |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 704 | fIndexPool->reset(); |
| 705 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 706 | } |
| 707 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 708 | bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout, |
| 709 | int vertexCount, |
| 710 | void** vertices) { |
| 711 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
| 712 | |
| 713 | GrAssert(vertexCount > 0); |
| 714 | GrAssert(NULL != vertices); |
| 715 | |
| 716 | this->prepareVertexPool(); |
| 717 | |
| 718 | *vertices = fVertexPool->makeSpace(vertexLayout, |
| 719 | vertexCount, |
| 720 | &geomPoolState.fPoolVertexBuffer, |
| 721 | &geomPoolState.fPoolStartVertex); |
| 722 | if (NULL == *vertices) { |
| 723 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 724 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 725 | ++fVertexPoolUseCnt; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 726 | return true; |
| 727 | } |
| 728 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 729 | bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) { |
| 730 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
| 731 | |
| 732 | GrAssert(indexCount > 0); |
| 733 | GrAssert(NULL != indices); |
| 734 | |
| 735 | this->prepareIndexPool(); |
| 736 | |
| 737 | *indices = fIndexPool->makeSpace(indexCount, |
| 738 | &geomPoolState.fPoolIndexBuffer, |
| 739 | &geomPoolState.fPoolStartIndex); |
| 740 | if (NULL == *indices) { |
| 741 | return false; |
| 742 | } |
| 743 | ++fIndexPoolUseCnt; |
| 744 | return true; |
| 745 | } |
| 746 | |
| 747 | void GrGpu::releaseReservedVertexSpace() { |
| 748 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 749 | GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc); |
| 750 | size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout); |
| 751 | fVertexPool->putBack(bytes); |
| 752 | --fVertexPoolUseCnt; |
| 753 | } |
| 754 | |
| 755 | void GrGpu::releaseReservedIndexSpace() { |
| 756 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 757 | GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc); |
| 758 | size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t); |
| 759 | fIndexPool->putBack(bytes); |
| 760 | --fIndexPoolUseCnt; |
| 761 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 762 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 763 | void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) { |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 764 | this->prepareVertexPool(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 765 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 766 | #if GR_DEBUG |
| 767 | bool success = |
| 768 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 769 | fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 770 | vertexCount, |
| 771 | vertexArray, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 772 | &geomPoolState.fPoolVertexBuffer, |
| 773 | &geomPoolState.fPoolStartVertex); |
| 774 | ++fVertexPoolUseCnt; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 775 | GR_DEBUGASSERT(success); |
| 776 | } |
| 777 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 778 | void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) { |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 779 | this->prepareIndexPool(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 780 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 781 | #if GR_DEBUG |
| 782 | bool success = |
| 783 | #endif |
| 784 | fIndexPool->appendIndices(indexCount, |
| 785 | indexArray, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 786 | &geomPoolState.fPoolIndexBuffer, |
| 787 | &geomPoolState.fPoolStartIndex); |
| 788 | ++fIndexPoolUseCnt; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 789 | GR_DEBUGASSERT(success); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 790 | } |
| 791 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 792 | void GrGpu::releaseVertexArray() { |
| 793 | // if vertex source was array, we stowed data in the pool |
| 794 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 795 | GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc); |
| 796 | size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout); |
| 797 | fVertexPool->putBack(bytes); |
| 798 | --fVertexPoolUseCnt; |
| 799 | } |
| 800 | |
| 801 | void GrGpu::releaseIndexArray() { |
| 802 | // if index source was array, we stowed data in the pool |
| 803 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 804 | GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc); |
| 805 | size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t); |
| 806 | fIndexPool->putBack(bytes); |
| 807 | --fIndexPoolUseCnt; |
| 808 | } |
| 809 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 810 | //////////////////////////////////////////////////////////////////////////////// |
| 811 | |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 812 | const GrGpuStats& GrGpu::getStats() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 813 | return fStats; |
| 814 | } |
| 815 | |
| 816 | void GrGpu::resetStats() { |
| 817 | memset(&fStats, 0, sizeof(fStats)); |
| 818 | } |
| 819 | |
| 820 | void GrGpu::printStats() const { |
| 821 | if (GR_COLLECT_STATS) { |
| 822 | GrPrintf( |
| 823 | "-v-------------------------GPU STATS----------------------------v-\n" |
| 824 | "Stats collection is: %s\n" |
| 825 | "Draws: %04d, Verts: %04d, Indices: %04d\n" |
| 826 | "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n" |
| 827 | "TexCreates: %04d, RTCreates:%04d\n" |
| 828 | "-^--------------------------------------------------------------^-\n", |
| 829 | (GR_COLLECT_STATS ? "ON" : "OFF"), |
| 830 | fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt, |
| 831 | fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt, |
| 832 | fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 837 | const GrSamplerState GrSamplerState::gClampNoFilter( |
| 838 | GrSamplerState::kClamp_WrapMode, |
| 839 | GrSamplerState::kClamp_WrapMode, |
| 840 | GrSamplerState::kNormal_SampleMode, |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 841 | GrMatrix::I(), |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 842 | GrSamplerState::kNearest_Filter); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 843 | |
| 844 | |
| 845 | |
| 846 | |