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