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