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