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() |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 34 | : fContext(NULL) |
bsalomon@google.com | 979432b | 2011-11-05 21:38:22 +0000 | [diff] [blame] | 35 | , fResetTimestamp(kExpiredTimestamp+1) |
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) |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 40 | , fQuadIndexBuffer(NULL) |
| 41 | , fUnitSquareVertexBuffer(NULL) |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 42 | , fPathRendererChain(NULL) |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 43 | , fContextIsDirty(true) |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 44 | , fResourceHead(NULL) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 45 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | #if GR_DEBUG |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 47 | //gr_run_unittests(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 49 | |
| 50 | fGeomPoolStateStack.push_back(); |
| 51 | #if GR_DEBUG |
| 52 | GeometryPoolState& poolState = fGeomPoolStateStack.back(); |
| 53 | poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 54 | poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX; |
| 55 | poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
| 56 | poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX; |
| 57 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 58 | resetStats(); |
| 59 | } |
| 60 | |
| 61 | GrGpu::~GrGpu() { |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 62 | this->releaseResources(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | } |
| 64 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 65 | void GrGpu::abandonResources() { |
| 66 | |
| 67 | while (NULL != fResourceHead) { |
| 68 | fResourceHead->abandon(); |
| 69 | } |
| 70 | |
| 71 | GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid()); |
| 72 | GrAssert(NULL == fUnitSquareVertexBuffer || |
| 73 | !fUnitSquareVertexBuffer->isValid()); |
| 74 | GrSafeSetNull(fQuadIndexBuffer); |
| 75 | GrSafeSetNull(fUnitSquareVertexBuffer); |
| 76 | delete fVertexPool; |
| 77 | fVertexPool = NULL; |
| 78 | delete fIndexPool; |
| 79 | fIndexPool = NULL; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 80 | // in case path renderer has any GrResources, start from scratch |
| 81 | GrSafeSetNull(fPathRendererChain); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 82 | } |
| 83 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 84 | void GrGpu::releaseResources() { |
| 85 | |
| 86 | while (NULL != fResourceHead) { |
| 87 | fResourceHead->release(); |
| 88 | } |
| 89 | |
| 90 | GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid()); |
| 91 | GrAssert(NULL == fUnitSquareVertexBuffer || |
| 92 | !fUnitSquareVertexBuffer->isValid()); |
| 93 | GrSafeSetNull(fQuadIndexBuffer); |
| 94 | GrSafeSetNull(fUnitSquareVertexBuffer); |
| 95 | delete fVertexPool; |
| 96 | fVertexPool = NULL; |
| 97 | delete fIndexPool; |
| 98 | fIndexPool = NULL; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 99 | // in case path renderer has any GrResources, start from scratch |
| 100 | GrSafeSetNull(fPathRendererChain); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 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 | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame^] | 198 | GrTexture* GrGpu::createPlatformTexture(const GrPlatformTextureDesc& desc) { |
| 199 | this->handleDirtyContext(); |
| 200 | GrTexture* tex = this->onCreatePlatformTexture(desc); |
| 201 | // TODO: defer this and attach dynamically |
| 202 | GrRenderTarget* tgt = tex->asRenderTarget(); |
| 203 | if (NULL != tgt && |
| 204 | !this->attachStencilBufferToRenderTarget(tgt)) { |
| 205 | tex->unref(); |
| 206 | return NULL; |
| 207 | } else { |
| 208 | return tex; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | GrRenderTarget* GrGpu::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) { |
| 213 | this->handleDirtyContext(); |
| 214 | return this->onCreatePlatformRenderTarget(desc); |
| 215 | } |
| 216 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 217 | GrResource* GrGpu::createPlatformSurface(const GrPlatformSurfaceDesc& desc) { |
| 218 | this->handleDirtyContext(); |
| 219 | return this->onCreatePlatformSurface(desc); |
| 220 | } |
| 221 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 222 | GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) { |
| 223 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 224 | return this->onCreateVertexBuffer(size, dynamic); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) { |
| 228 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 229 | return this->onCreateIndexBuffer(size, dynamic); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 230 | } |
| 231 | |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 232 | void GrGpu::clear(const GrIRect* rect, GrColor color) { |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 233 | this->handleDirtyContext(); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 234 | this->onClear(rect, color); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void GrGpu::forceRenderTargetFlush() { |
| 238 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 239 | this->onForceRenderTargetFlush(); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 240 | } |
| 241 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 242 | bool GrGpu::readPixels(GrRenderTarget* target, |
| 243 | int left, int top, int width, int height, |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 244 | GrPixelConfig config, void* buffer, |
| 245 | size_t rowBytes) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 246 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 247 | this->handleDirtyContext(); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 248 | return this->onReadPixels(target, left, top, width, height, |
| 249 | config, buffer, rowBytes); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | //////////////////////////////////////////////////////////////////////////////// |
| 253 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 254 | 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] | 255 | |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 256 | GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 257 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 258 | static inline void fill_indices(uint16_t* indices, int quadCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 259 | for (int i = 0; i < quadCount; ++i) { |
| 260 | indices[6 * i + 0] = 4 * i + 0; |
| 261 | indices[6 * i + 1] = 4 * i + 1; |
| 262 | indices[6 * i + 2] = 4 * i + 2; |
| 263 | indices[6 * i + 3] = 4 * i + 0; |
| 264 | indices[6 * i + 4] = 4 * i + 2; |
| 265 | indices[6 * i + 5] = 4 * i + 3; |
| 266 | } |
| 267 | } |
| 268 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 269 | const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 270 | if (NULL == fQuadIndexBuffer) { |
| 271 | static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS; |
| 272 | GrGpu* me = const_cast<GrGpu*>(this); |
| 273 | fQuadIndexBuffer = me->createIndexBuffer(SIZE, false); |
| 274 | if (NULL != fQuadIndexBuffer) { |
| 275 | uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock(); |
| 276 | if (NULL != indices) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 277 | fill_indices(indices, MAX_QUADS); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 278 | fQuadIndexBuffer->unlock(); |
| 279 | } else { |
| 280 | indices = (uint16_t*)GrMalloc(SIZE); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 281 | fill_indices(indices, MAX_QUADS); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 282 | if (!fQuadIndexBuffer->updateData(indices, SIZE)) { |
| 283 | fQuadIndexBuffer->unref(); |
| 284 | fQuadIndexBuffer = NULL; |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 285 | GrCrash("Can't get indices into buffer!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 286 | } |
| 287 | GrFree(indices); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return fQuadIndexBuffer; |
| 293 | } |
| 294 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 295 | const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const { |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 296 | if (NULL == fUnitSquareVertexBuffer) { |
| 297 | |
| 298 | static const GrPoint DATA[] = { |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 299 | { 0, 0 }, |
| 300 | { GR_Scalar1, 0 }, |
| 301 | { GR_Scalar1, GR_Scalar1 }, |
| 302 | { 0, GR_Scalar1 } |
| 303 | #if 0 |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 304 | GrPoint(0, 0), |
| 305 | GrPoint(GR_Scalar1,0), |
| 306 | GrPoint(GR_Scalar1,GR_Scalar1), |
| 307 | GrPoint(0, GR_Scalar1) |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 308 | #endif |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 309 | }; |
| 310 | static const size_t SIZE = sizeof(DATA); |
| 311 | |
| 312 | GrGpu* me = const_cast<GrGpu*>(this); |
| 313 | fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false); |
| 314 | if (NULL != fUnitSquareVertexBuffer) { |
| 315 | if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) { |
| 316 | fUnitSquareVertexBuffer->unref(); |
| 317 | fUnitSquareVertexBuffer = NULL; |
| 318 | GrCrash("Can't get vertices into buffer!"); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return fUnitSquareVertexBuffer; |
| 324 | } |
| 325 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 326 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 327 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 328 | // stencil settings to use when clip is in stencil |
| 329 | const GrStencilSettings GrGpu::gClipStencilSettings = { |
| 330 | kKeep_StencilOp, kKeep_StencilOp, |
| 331 | kKeep_StencilOp, kKeep_StencilOp, |
| 332 | kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc, |
| 333 | 0, 0, |
| 334 | 0, 0, |
| 335 | 0, 0 |
| 336 | }; |
| 337 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 338 | // mapping of clip-respecting stencil funcs to normal stencil funcs |
| 339 | // mapping depends on whether stencil-clipping is in effect. |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 340 | static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = { |
| 341 | {// Stencil-Clipping is DISABLED, effectively always inside the clip |
| 342 | // In the Clip Funcs |
| 343 | kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 344 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 345 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 346 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 347 | // Special in the clip func that forces user's ref to be 0. |
| 348 | kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 349 | // make ref 0 and do normal nequal. |
| 350 | }, |
| 351 | {// Stencil-Clipping is ENABLED |
| 352 | // In the Clip Funcs |
| 353 | kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 354 | // eq stencil clip bit, mask |
| 355 | // out user bits. |
| 356 | |
| 357 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 358 | // add stencil bit to mask and ref |
| 359 | |
| 360 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 361 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 362 | // for both of these we can add |
| 363 | // the clip bit to the mask and |
| 364 | // ref and compare as normal |
| 365 | // Special in the clip func that forces user's ref to be 0. |
| 366 | kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 367 | // make ref have only the clip bit set |
| 368 | // and make comparison be less |
| 369 | // 10..0 < 1..user_bits.. |
| 370 | } |
| 371 | }; |
| 372 | |
| 373 | GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) { |
| 374 | GrAssert(func >= 0); |
| 375 | if (func >= kBasicStencilFuncCount) { |
| 376 | GrAssert(func < kStencilFuncCount); |
| 377 | func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount]; |
| 378 | GrAssert(func >= 0 && func < kBasicStencilFuncCount); |
| 379 | } |
| 380 | return func; |
| 381 | } |
| 382 | |
| 383 | void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func, |
| 384 | bool clipInStencil, |
| 385 | unsigned int clipBit, |
| 386 | unsigned int userBits, |
| 387 | unsigned int* ref, |
| 388 | unsigned int* mask) { |
| 389 | if (func < kBasicStencilFuncCount) { |
| 390 | *mask &= userBits; |
| 391 | *ref &= userBits; |
| 392 | } else { |
| 393 | if (clipInStencil) { |
| 394 | switch (func) { |
| 395 | case kAlwaysIfInClip_StencilFunc: |
| 396 | *mask = clipBit; |
| 397 | *ref = clipBit; |
| 398 | break; |
| 399 | case kEqualIfInClip_StencilFunc: |
| 400 | case kLessIfInClip_StencilFunc: |
| 401 | case kLEqualIfInClip_StencilFunc: |
| 402 | *mask = (*mask & userBits) | clipBit; |
| 403 | *ref = (*ref & userBits) | clipBit; |
| 404 | break; |
| 405 | case kNonZeroIfInClip_StencilFunc: |
| 406 | *mask = (*mask & userBits) | clipBit; |
| 407 | *ref = clipBit; |
| 408 | break; |
| 409 | default: |
| 410 | GrCrash("Unknown stencil func"); |
| 411 | } |
| 412 | } else { |
| 413 | *mask &= userBits; |
| 414 | *ref &= userBits; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | //////////////////////////////////////////////////////////////////////////////// |
| 420 | |
| 421 | #define VISUALIZE_COMPLEX_CLIP 0 |
| 422 | |
| 423 | #if VISUALIZE_COMPLEX_CLIP |
| 424 | #include "GrRandom.h" |
| 425 | GrRandom gRandom; |
| 426 | #define SET_RANDOM_COLOR this->setColor(0xff000000 | gRandom.nextU()); |
| 427 | #else |
| 428 | #define SET_RANDOM_COLOR |
| 429 | #endif |
| 430 | |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 431 | namespace { |
| 432 | // determines how many elements at the head of the clip can be skipped and |
| 433 | // whether the initial clear should be to the inside- or outside-the-clip value, |
| 434 | // and what op should be used to draw the first element that isn't skipped. |
| 435 | int process_initial_clip_elements(const GrClip& clip, |
| 436 | bool* clearToInside, |
| 437 | GrSetOp* startOp) { |
| 438 | |
| 439 | // logically before the first element of the clip stack is |
| 440 | // processed the clip is entirely open. However, depending on the |
| 441 | // first set op we may prefer to clear to 0 for performance. We may |
| 442 | // also be able to skip the initial clip paths/rects. We loop until |
| 443 | // we cannot skip an element. |
| 444 | int curr; |
| 445 | bool done = false; |
| 446 | *clearToInside = true; |
| 447 | int count = clip.getElementCount(); |
| 448 | |
| 449 | for (curr = 0; curr < count && !done; ++curr) { |
| 450 | switch (clip.getOp(curr)) { |
| 451 | case kReplace_SetOp: |
| 452 | // replace ignores everything previous |
| 453 | *startOp = kReplace_SetOp; |
| 454 | *clearToInside = false; |
| 455 | done = true; |
| 456 | break; |
| 457 | case kIntersect_SetOp: |
| 458 | // if everything is initially clearToInside then intersect is |
| 459 | // same as clear to 0 and treat as a replace. Otherwise, |
| 460 | // set stays empty. |
| 461 | if (*clearToInside) { |
| 462 | *startOp = kReplace_SetOp; |
| 463 | *clearToInside = false; |
| 464 | done = true; |
| 465 | } |
| 466 | break; |
| 467 | // we can skip a leading union. |
| 468 | case kUnion_SetOp: |
| 469 | // if everything is initially outside then union is |
| 470 | // same as replace. Otherwise, every pixel is still |
| 471 | // clearToInside |
| 472 | if (!*clearToInside) { |
| 473 | *startOp = kReplace_SetOp; |
| 474 | done = true; |
| 475 | } |
| 476 | break; |
| 477 | case kXor_SetOp: |
| 478 | // xor is same as difference or replace both of which |
| 479 | // can be 1-pass instead of 2 for xor. |
| 480 | if (*clearToInside) { |
| 481 | *startOp = kDifference_SetOp; |
| 482 | } else { |
| 483 | *startOp = kReplace_SetOp; |
| 484 | } |
| 485 | done = true; |
| 486 | break; |
| 487 | case kDifference_SetOp: |
| 488 | // if all pixels are clearToInside then we have to process the |
| 489 | // difference, otherwise it has no effect and all pixels |
| 490 | // remain outside. |
| 491 | if (*clearToInside) { |
| 492 | *startOp = kDifference_SetOp; |
| 493 | done = true; |
| 494 | } |
| 495 | break; |
| 496 | case kReverseDifference_SetOp: |
| 497 | // if all pixels are clearToInside then reverse difference |
| 498 | // produces empty set. Otherise it is same as replace |
| 499 | if (*clearToInside) { |
| 500 | *clearToInside = false; |
| 501 | } else { |
| 502 | *startOp = kReplace_SetOp; |
| 503 | done = true; |
| 504 | } |
| 505 | break; |
bsalomon@google.com | 2ec7280 | 2011-09-21 21:46:03 +0000 | [diff] [blame] | 506 | default: |
| 507 | GrCrash("Unknown set op."); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | return done ? curr-1 : count; |
| 511 | } |
| 512 | } |
| 513 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 514 | bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 515 | const GrIRect* r = NULL; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 516 | GrIRect clipRect; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 517 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 518 | // we check this early because we need a valid |
| 519 | // render target to setup stencil clipping |
| 520 | // before even going into flushGraphicsState |
| 521 | if (NULL == fCurrDrawState.fRenderTarget) { |
| 522 | GrAssert(!"No render target bound."); |
| 523 | return false; |
| 524 | } |
| 525 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 526 | if (fCurrDrawState.fFlagBits & kClip_StateBit) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 527 | GrRenderTarget& rt = *fCurrDrawState.fRenderTarget; |
| 528 | |
| 529 | GrRect bounds; |
| 530 | GrRect rtRect; |
| 531 | rtRect.setLTRB(0, 0, |
| 532 | GrIntToScalar(rt.width()), GrIntToScalar(rt.height())); |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 533 | if (fClip.hasConservativeBounds()) { |
| 534 | bounds = fClip.getConservativeBounds(); |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 535 | if (!bounds.intersect(rtRect)) { |
| 536 | bounds.setEmpty(); |
| 537 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 538 | } else { |
| 539 | bounds = rtRect; |
| 540 | } |
| 541 | |
| 542 | bounds.roundOut(&clipRect); |
| 543 | if (clipRect.isEmpty()) { |
| 544 | clipRect.setLTRB(0,0,0,0); |
| 545 | } |
| 546 | r = &clipRect; |
| 547 | |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 548 | // use the stencil clip if we can't represent the clip as a rectangle. |
| 549 | fClipInStencil = !fClip.isRect() && !fClip.isEmpty() && |
| 550 | !bounds.isEmpty(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 551 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 552 | // TODO: dynamically attach a SB when needed. |
| 553 | GrStencilBuffer* stencilBuffer = rt.getStencilBuffer(); |
| 554 | if (fClipInStencil && NULL == stencilBuffer) { |
| 555 | return false; |
| 556 | } |
bsalomon@google.com | a16d650 | 2011-08-02 14:07:52 +0000 | [diff] [blame] | 557 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 558 | if (fClipInStencil && |
| 559 | stencilBuffer->mustRenderClip(fClip, rt.width(), rt.height())) { |
| 560 | |
| 561 | stencilBuffer->setLastClip(fClip, rt.width(), rt.height()); |
| 562 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 563 | // we set the current clip to the bounds so that our recursive |
| 564 | // 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] | 565 | // we just stashed on the SB to render from. We set it back after |
| 566 | // we finish drawing it into the stencil. |
| 567 | const GrClip& clip = stencilBuffer->getLastClip(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 568 | fClip.setFromRect(bounds); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 569 | |
| 570 | AutoStateRestore asr(this); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 571 | AutoGeometryPush agp(this); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 572 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 573 | this->setViewMatrix(GrMatrix::I()); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 574 | this->flushScissor(NULL); |
| 575 | #if !VISUALIZE_COMPLEX_CLIP |
| 576 | this->enableState(kNoColorWrites_StateBit); |
| 577 | #else |
| 578 | this->disableState(kNoColorWrites_StateBit); |
| 579 | #endif |
| 580 | int count = clip.getElementCount(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 581 | int clipBit = stencilBuffer->bits(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 582 | clipBit = (1 << (clipBit-1)); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 583 | |
| 584 | bool clearToInside; |
bsalomon@google.com | 2ec7280 | 2011-09-21 21:46:03 +0000 | [diff] [blame] | 585 | GrSetOp startOp = kReplace_SetOp; // suppress warning |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 586 | int start = process_initial_clip_elements(clip, &clearToInside, |
| 587 | &startOp); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 588 | |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 589 | this->clearStencilClip(clipRect, clearToInside); |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 590 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 591 | // walk through each clip element and perform its set op |
| 592 | // with the existing clip. |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 593 | for (int c = start; c < count; ++c) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 594 | GrPathFill fill; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 595 | bool fillInverted; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 596 | // enabled at bottom of loop |
| 597 | this->disableState(kModifyStencilClip_StateBit); |
| 598 | |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 599 | bool canRenderDirectToStencil; // can the clip element be drawn |
| 600 | // directly to the stencil buffer |
| 601 | // with a non-inverted fill rule |
| 602 | // without extra passes to |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 603 | // resolve in/out status. |
| 604 | |
| 605 | GrPathRenderer* pr = NULL; |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 606 | const GrPath* clipPath = NULL; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 607 | GrPathRenderer::AutoClearPath arp; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 608 | if (kRect_ClipType == clip.getElementType(c)) { |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 609 | canRenderDirectToStencil = true; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 610 | fill = kEvenOdd_PathFill; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 611 | fillInverted = false; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 612 | } else { |
| 613 | fill = clip.getPathFill(c); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 614 | fillInverted = GrIsFillInverted(fill); |
| 615 | fill = GrNonInvertedFill(fill); |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 616 | clipPath = &clip.getPath(c); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 617 | pr = this->getClipPathRenderer(*clipPath, fill); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 618 | if (NULL == pr) { |
| 619 | fClipInStencil = false; |
| 620 | fClip = clip; |
| 621 | return false; |
| 622 | } |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 623 | canRenderDirectToStencil = |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 624 | !pr->requiresStencilPass(this, *clipPath, fill); |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 625 | arp.set(pr, this, clipPath, fill, false, NULL); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 626 | } |
| 627 | |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 628 | GrSetOp op = (c == start) ? startOp : clip.getOp(c); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 629 | int passes; |
| 630 | GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses]; |
| 631 | |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 632 | bool canDrawDirectToClip; // Given the renderer, the element, |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 633 | // fill rule, and set operation can |
| 634 | // we render the element directly to |
| 635 | // stencil bit used for clipping. |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 636 | canDrawDirectToClip = |
| 637 | GrStencilSettings::GetClipPasses(op, |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 638 | canRenderDirectToStencil, |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 639 | clipBit, |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 640 | fillInverted, |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 641 | &passes, stencilSettings); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 642 | |
| 643 | // draw the element to the client stencil bits if necessary |
| 644 | if (!canDrawDirectToClip) { |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 645 | static const GrStencilSettings gDrawToStencil = { |
| 646 | kIncClamp_StencilOp, kIncClamp_StencilOp, |
| 647 | kIncClamp_StencilOp, kIncClamp_StencilOp, |
| 648 | kAlways_StencilFunc, kAlways_StencilFunc, |
| 649 | 0xffffffff, 0xffffffff, |
| 650 | 0x00000000, 0x00000000, |
| 651 | 0xffffffff, 0xffffffff, |
| 652 | }; |
| 653 | SET_RANDOM_COLOR |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 654 | if (kRect_ClipType == clip.getElementType(c)) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 655 | this->setStencil(gDrawToStencil); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 656 | this->drawSimpleRect(clip.getRect(c), NULL, 0); |
| 657 | } else { |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 658 | if (canRenderDirectToStencil) { |
| 659 | this->setStencil(gDrawToStencil); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 660 | pr->drawPath(0); |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 661 | } else { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 662 | pr->drawPathToStencil(); |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 663 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | // now we modify the clip bit by rendering either the clip |
| 668 | // element directly or a bounding rect of the entire clip. |
| 669 | this->enableState(kModifyStencilClip_StateBit); |
| 670 | for (int p = 0; p < passes; ++p) { |
| 671 | this->setStencil(stencilSettings[p]); |
| 672 | if (canDrawDirectToClip) { |
| 673 | if (kRect_ClipType == clip.getElementType(c)) { |
| 674 | SET_RANDOM_COLOR |
| 675 | this->drawSimpleRect(clip.getRect(c), NULL, 0); |
| 676 | } else { |
| 677 | SET_RANDOM_COLOR |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 678 | pr->drawPath(0); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 679 | } |
| 680 | } else { |
| 681 | SET_RANDOM_COLOR |
thakis@chromium.org | 441d7da | 2011-06-07 04:03:17 +0000 | [diff] [blame] | 682 | this->drawSimpleRect(bounds, NULL, 0); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 683 | } |
| 684 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 685 | } |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 686 | // restore clip |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 687 | fClip = clip; |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 688 | // recusive draws would have disabled this since they drew with |
| 689 | // the clip bounds as clip. |
| 690 | fClipInStencil = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 691 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 692 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 693 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 694 | // Must flush the scissor after graphics state |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 695 | if (!this->flushGraphicsState(type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 696 | return false; |
| 697 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 698 | this->flushScissor(r); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 699 | return true; |
| 700 | } |
| 701 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 702 | GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path, |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 703 | GrPathFill fill) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 704 | if (NULL == fPathRendererChain) { |
| 705 | fPathRendererChain = |
| 706 | new GrPathRendererChain(this->getContext(), |
| 707 | GrPathRendererChain::kNonAAOnly_UsageFlag); |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 708 | } |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 709 | return fPathRendererChain->getPathRenderer(this->getCaps(), |
| 710 | path, fill, false); |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 714 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 715 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 716 | void GrGpu::geometrySourceWillPush() { |
| 717 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 718 | if (kArray_GeometrySrcType == geoSrc.fVertexSrc || |
| 719 | kReserved_GeometrySrcType == geoSrc.fVertexSrc) { |
| 720 | this->finalizeReservedVertices(); |
| 721 | } |
| 722 | if (kArray_GeometrySrcType == geoSrc.fIndexSrc || |
| 723 | kReserved_GeometrySrcType == geoSrc.fIndexSrc) { |
| 724 | this->finalizeReservedIndices(); |
| 725 | } |
| 726 | GeometryPoolState& newState = fGeomPoolStateStack.push_back(); |
| 727 | #if GR_DEBUG |
| 728 | newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 729 | newState.fPoolStartVertex = DEBUG_INVAL_START_IDX; |
| 730 | newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
| 731 | newState.fPoolStartIndex = DEBUG_INVAL_START_IDX; |
| 732 | #endif |
| 733 | } |
| 734 | |
| 735 | void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) { |
| 736 | // if popping last entry then pops are unbalanced with pushes |
| 737 | GrAssert(fGeomPoolStateStack.count() > 1); |
| 738 | fGeomPoolStateStack.pop_back(); |
| 739 | } |
| 740 | |
| 741 | void GrGpu::onDrawIndexed(GrPrimitiveType type, |
| 742 | int startVertex, |
| 743 | int startIndex, |
| 744 | int vertexCount, |
| 745 | int indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 746 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 747 | this->handleDirtyContext(); |
| 748 | |
| 749 | if (!this->setupClipAndFlushState(type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 750 | return; |
| 751 | } |
| 752 | |
| 753 | #if GR_COLLECT_STATS |
| 754 | fStats.fVertexCnt += vertexCount; |
| 755 | fStats.fIndexCnt += indexCount; |
| 756 | fStats.fDrawCnt += 1; |
| 757 | #endif |
| 758 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 759 | int sVertex = startVertex; |
| 760 | int sIndex = startIndex; |
| 761 | setupGeometry(&sVertex, &sIndex, vertexCount, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 762 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 763 | this->onGpuDrawIndexed(type, sVertex, sIndex, |
| 764 | vertexCount, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 765 | } |
| 766 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 767 | void GrGpu::onDrawNonIndexed(GrPrimitiveType type, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 768 | int startVertex, |
| 769 | int vertexCount) { |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 770 | this->handleDirtyContext(); |
| 771 | |
| 772 | if (!this->setupClipAndFlushState(type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 773 | return; |
| 774 | } |
| 775 | #if GR_COLLECT_STATS |
| 776 | fStats.fVertexCnt += vertexCount; |
| 777 | fStats.fDrawCnt += 1; |
| 778 | #endif |
| 779 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 780 | int sVertex = startVertex; |
| 781 | setupGeometry(&sVertex, NULL, vertexCount, 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 782 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 783 | this->onGpuDrawNonIndexed(type, sVertex, vertexCount); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | void GrGpu::finalizeReservedVertices() { |
| 787 | GrAssert(NULL != fVertexPool); |
| 788 | fVertexPool->unlock(); |
| 789 | } |
| 790 | |
| 791 | void GrGpu::finalizeReservedIndices() { |
| 792 | GrAssert(NULL != fIndexPool); |
| 793 | fIndexPool->unlock(); |
| 794 | } |
| 795 | |
| 796 | void GrGpu::prepareVertexPool() { |
| 797 | if (NULL == fVertexPool) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 798 | GrAssert(0 == fVertexPoolUseCnt); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 799 | fVertexPool = new GrVertexBufferAllocPool(this, true, |
| 800 | VERTEX_POOL_VB_SIZE, |
bsalomon@google.com | 7a5af8b | 2011-02-18 18:40:42 +0000 | [diff] [blame] | 801 | VERTEX_POOL_VB_COUNT); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 802 | fVertexPool->releaseGpuRef(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 803 | } else if (!fVertexPoolUseCnt) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 804 | // the client doesn't have valid data in the pool |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 805 | fVertexPool->reset(); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | void GrGpu::prepareIndexPool() { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 810 | if (NULL == fIndexPool) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 811 | GrAssert(0 == fIndexPoolUseCnt); |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 812 | fIndexPool = new GrIndexBufferAllocPool(this, true, |
| 813 | INDEX_POOL_IB_SIZE, |
| 814 | INDEX_POOL_IB_COUNT); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 815 | fIndexPool->releaseGpuRef(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 816 | } else if (!fIndexPoolUseCnt) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 817 | // the client doesn't have valid data in the pool |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 818 | fIndexPool->reset(); |
| 819 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 820 | } |
| 821 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 822 | bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout, |
| 823 | int vertexCount, |
| 824 | void** vertices) { |
| 825 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
| 826 | |
| 827 | GrAssert(vertexCount > 0); |
| 828 | GrAssert(NULL != vertices); |
| 829 | |
| 830 | this->prepareVertexPool(); |
| 831 | |
| 832 | *vertices = fVertexPool->makeSpace(vertexLayout, |
| 833 | vertexCount, |
| 834 | &geomPoolState.fPoolVertexBuffer, |
| 835 | &geomPoolState.fPoolStartVertex); |
| 836 | if (NULL == *vertices) { |
| 837 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 838 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 839 | ++fVertexPoolUseCnt; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 840 | return true; |
| 841 | } |
| 842 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 843 | bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) { |
| 844 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
| 845 | |
| 846 | GrAssert(indexCount > 0); |
| 847 | GrAssert(NULL != indices); |
| 848 | |
| 849 | this->prepareIndexPool(); |
| 850 | |
| 851 | *indices = fIndexPool->makeSpace(indexCount, |
| 852 | &geomPoolState.fPoolIndexBuffer, |
| 853 | &geomPoolState.fPoolStartIndex); |
| 854 | if (NULL == *indices) { |
| 855 | return false; |
| 856 | } |
| 857 | ++fIndexPoolUseCnt; |
| 858 | return true; |
| 859 | } |
| 860 | |
| 861 | void GrGpu::releaseReservedVertexSpace() { |
| 862 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 863 | GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc); |
| 864 | size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout); |
| 865 | fVertexPool->putBack(bytes); |
| 866 | --fVertexPoolUseCnt; |
| 867 | } |
| 868 | |
| 869 | void GrGpu::releaseReservedIndexSpace() { |
| 870 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 871 | GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc); |
| 872 | size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t); |
| 873 | fIndexPool->putBack(bytes); |
| 874 | --fIndexPoolUseCnt; |
| 875 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 876 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 877 | void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) { |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 878 | this->prepareVertexPool(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 879 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 880 | #if GR_DEBUG |
| 881 | bool success = |
| 882 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 883 | fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 884 | vertexCount, |
| 885 | vertexArray, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 886 | &geomPoolState.fPoolVertexBuffer, |
| 887 | &geomPoolState.fPoolStartVertex); |
| 888 | ++fVertexPoolUseCnt; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 889 | GR_DEBUGASSERT(success); |
| 890 | } |
| 891 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 892 | void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) { |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 893 | this->prepareIndexPool(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 894 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 895 | #if GR_DEBUG |
| 896 | bool success = |
| 897 | #endif |
| 898 | fIndexPool->appendIndices(indexCount, |
| 899 | indexArray, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 900 | &geomPoolState.fPoolIndexBuffer, |
| 901 | &geomPoolState.fPoolStartIndex); |
| 902 | ++fIndexPoolUseCnt; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 903 | GR_DEBUGASSERT(success); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 904 | } |
| 905 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 906 | void GrGpu::releaseVertexArray() { |
| 907 | // if vertex source was array, we stowed data in the pool |
| 908 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 909 | GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc); |
| 910 | size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout); |
| 911 | fVertexPool->putBack(bytes); |
| 912 | --fVertexPoolUseCnt; |
| 913 | } |
| 914 | |
| 915 | void GrGpu::releaseIndexArray() { |
| 916 | // if index source was array, we stowed data in the pool |
| 917 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 918 | GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc); |
| 919 | size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t); |
| 920 | fIndexPool->putBack(bytes); |
| 921 | --fIndexPoolUseCnt; |
| 922 | } |
| 923 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 924 | //////////////////////////////////////////////////////////////////////////////// |
| 925 | |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 926 | const GrGpuStats& GrGpu::getStats() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 927 | return fStats; |
| 928 | } |
| 929 | |
| 930 | void GrGpu::resetStats() { |
| 931 | memset(&fStats, 0, sizeof(fStats)); |
| 932 | } |
| 933 | |
| 934 | void GrGpu::printStats() const { |
| 935 | if (GR_COLLECT_STATS) { |
| 936 | GrPrintf( |
| 937 | "-v-------------------------GPU STATS----------------------------v-\n" |
| 938 | "Stats collection is: %s\n" |
| 939 | "Draws: %04d, Verts: %04d, Indices: %04d\n" |
| 940 | "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n" |
| 941 | "TexCreates: %04d, RTCreates:%04d\n" |
| 942 | "-^--------------------------------------------------------------^-\n", |
| 943 | (GR_COLLECT_STATS ? "ON" : "OFF"), |
| 944 | fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt, |
| 945 | fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt, |
| 946 | fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 951 | const GrSamplerState GrSamplerState::gClampNoFilter( |
| 952 | GrSamplerState::kClamp_WrapMode, |
| 953 | GrSamplerState::kClamp_WrapMode, |
| 954 | GrSamplerState::kNormal_SampleMode, |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 955 | GrMatrix::I(), |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 956 | GrSamplerState::kNearest_Filter); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 957 | |
| 958 | |
| 959 | |
| 960 | |