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