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 | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 418 | namespace { |
| 419 | // determines how many elements at the head of the clip can be skipped and |
| 420 | // whether the initial clear should be to the inside- or outside-the-clip value, |
| 421 | // and what op should be used to draw the first element that isn't skipped. |
| 422 | int process_initial_clip_elements(const GrClip& clip, |
| 423 | bool* clearToInside, |
| 424 | GrSetOp* startOp) { |
| 425 | |
| 426 | // logically before the first element of the clip stack is |
| 427 | // processed the clip is entirely open. However, depending on the |
| 428 | // first set op we may prefer to clear to 0 for performance. We may |
| 429 | // also be able to skip the initial clip paths/rects. We loop until |
| 430 | // we cannot skip an element. |
| 431 | int curr; |
| 432 | bool done = false; |
| 433 | *clearToInside = true; |
| 434 | int count = clip.getElementCount(); |
| 435 | |
| 436 | for (curr = 0; curr < count && !done; ++curr) { |
| 437 | switch (clip.getOp(curr)) { |
| 438 | case kReplace_SetOp: |
| 439 | // replace ignores everything previous |
| 440 | *startOp = kReplace_SetOp; |
| 441 | *clearToInside = false; |
| 442 | done = true; |
| 443 | break; |
| 444 | case kIntersect_SetOp: |
| 445 | // if everything is initially clearToInside then intersect is |
| 446 | // same as clear to 0 and treat as a replace. Otherwise, |
| 447 | // set stays empty. |
| 448 | if (*clearToInside) { |
| 449 | *startOp = kReplace_SetOp; |
| 450 | *clearToInside = false; |
| 451 | done = true; |
| 452 | } |
| 453 | break; |
| 454 | // we can skip a leading union. |
| 455 | case kUnion_SetOp: |
| 456 | // if everything is initially outside then union is |
| 457 | // same as replace. Otherwise, every pixel is still |
| 458 | // clearToInside |
| 459 | if (!*clearToInside) { |
| 460 | *startOp = kReplace_SetOp; |
| 461 | done = true; |
| 462 | } |
| 463 | break; |
| 464 | case kXor_SetOp: |
| 465 | // xor is same as difference or replace both of which |
| 466 | // can be 1-pass instead of 2 for xor. |
| 467 | if (*clearToInside) { |
| 468 | *startOp = kDifference_SetOp; |
| 469 | } else { |
| 470 | *startOp = kReplace_SetOp; |
| 471 | } |
| 472 | done = true; |
| 473 | break; |
| 474 | case kDifference_SetOp: |
| 475 | // if all pixels are clearToInside then we have to process the |
| 476 | // difference, otherwise it has no effect and all pixels |
| 477 | // remain outside. |
| 478 | if (*clearToInside) { |
| 479 | *startOp = kDifference_SetOp; |
| 480 | done = true; |
| 481 | } |
| 482 | break; |
| 483 | case kReverseDifference_SetOp: |
| 484 | // if all pixels are clearToInside then reverse difference |
| 485 | // produces empty set. Otherise it is same as replace |
| 486 | if (*clearToInside) { |
| 487 | *clearToInside = false; |
| 488 | } else { |
| 489 | *startOp = kReplace_SetOp; |
| 490 | done = true; |
| 491 | } |
| 492 | break; |
bsalomon@google.com | 2ec7280 | 2011-09-21 21:46:03 +0000 | [diff] [blame^] | 493 | default: |
| 494 | GrCrash("Unknown set op."); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | return done ? curr-1 : count; |
| 498 | } |
| 499 | } |
| 500 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 501 | bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 502 | const GrIRect* r = NULL; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 503 | GrIRect clipRect; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 504 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 505 | // we check this early because we need a valid |
| 506 | // render target to setup stencil clipping |
| 507 | // before even going into flushGraphicsState |
| 508 | if (NULL == fCurrDrawState.fRenderTarget) { |
| 509 | GrAssert(!"No render target bound."); |
| 510 | return false; |
| 511 | } |
| 512 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 513 | if (fCurrDrawState.fFlagBits & kClip_StateBit) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 514 | GrRenderTarget& rt = *fCurrDrawState.fRenderTarget; |
| 515 | |
| 516 | GrRect bounds; |
| 517 | GrRect rtRect; |
| 518 | rtRect.setLTRB(0, 0, |
| 519 | GrIntToScalar(rt.width()), GrIntToScalar(rt.height())); |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 520 | if (fClip.hasConservativeBounds()) { |
| 521 | bounds = fClip.getConservativeBounds(); |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 522 | if (!bounds.intersect(rtRect)) { |
| 523 | bounds.setEmpty(); |
| 524 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 525 | } else { |
| 526 | bounds = rtRect; |
| 527 | } |
| 528 | |
| 529 | bounds.roundOut(&clipRect); |
| 530 | if (clipRect.isEmpty()) { |
| 531 | clipRect.setLTRB(0,0,0,0); |
| 532 | } |
| 533 | r = &clipRect; |
| 534 | |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 535 | // use the stencil clip if we can't represent the clip as a rectangle. |
| 536 | fClipInStencil = !fClip.isRect() && !fClip.isEmpty() && |
| 537 | !bounds.isEmpty(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 538 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 539 | // TODO: dynamically attach a SB when needed. |
| 540 | GrStencilBuffer* stencilBuffer = rt.getStencilBuffer(); |
| 541 | if (fClipInStencil && NULL == stencilBuffer) { |
| 542 | return false; |
| 543 | } |
bsalomon@google.com | a16d650 | 2011-08-02 14:07:52 +0000 | [diff] [blame] | 544 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 545 | if (fClipInStencil && |
| 546 | stencilBuffer->mustRenderClip(fClip, rt.width(), rt.height())) { |
| 547 | |
| 548 | stencilBuffer->setLastClip(fClip, rt.width(), rt.height()); |
| 549 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 550 | // we set the current clip to the bounds so that our recursive |
| 551 | // 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] | 552 | // we just stashed on the SB to render from. We set it back after |
| 553 | // we finish drawing it into the stencil. |
| 554 | const GrClip& clip = stencilBuffer->getLastClip(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 555 | fClip.setFromRect(bounds); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 556 | |
| 557 | AutoStateRestore asr(this); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 558 | AutoGeometryPush agp(this); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 559 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 560 | this->setViewMatrix(GrMatrix::I()); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 561 | this->flushScissor(NULL); |
| 562 | #if !VISUALIZE_COMPLEX_CLIP |
| 563 | this->enableState(kNoColorWrites_StateBit); |
| 564 | #else |
| 565 | this->disableState(kNoColorWrites_StateBit); |
| 566 | #endif |
| 567 | int count = clip.getElementCount(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 568 | int clipBit = stencilBuffer->bits(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 569 | clipBit = (1 << (clipBit-1)); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 570 | |
| 571 | bool clearToInside; |
bsalomon@google.com | 2ec7280 | 2011-09-21 21:46:03 +0000 | [diff] [blame^] | 572 | GrSetOp startOp = kReplace_SetOp; // suppress warning |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 573 | int start = process_initial_clip_elements(clip, &clearToInside, |
| 574 | &startOp); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 575 | |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 576 | this->clearStencilClip(clipRect, clearToInside); |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 577 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 578 | // walk through each clip element and perform its set op |
| 579 | // with the existing clip. |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 580 | for (int c = start; c < count; ++c) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 581 | GrPathFill fill; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 582 | bool fillInverted; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 583 | // enabled at bottom of loop |
| 584 | this->disableState(kModifyStencilClip_StateBit); |
| 585 | |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 586 | bool canRenderDirectToStencil; // can the clip element be drawn |
| 587 | // directly to the stencil buffer |
| 588 | // with a non-inverted fill rule |
| 589 | // without extra passes to |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 590 | // resolve in/out status. |
| 591 | |
| 592 | GrPathRenderer* pr = NULL; |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 593 | const GrPath* clipPath = NULL; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 594 | GrPathRenderer::AutoClearPath arp; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 595 | if (kRect_ClipType == clip.getElementType(c)) { |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 596 | canRenderDirectToStencil = true; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 597 | fill = kEvenOdd_PathFill; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 598 | fillInverted = false; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 599 | } else { |
| 600 | fill = clip.getPathFill(c); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 601 | fillInverted = IsFillInverted(fill); |
| 602 | fill = NonInvertedFill(fill); |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 603 | clipPath = &clip.getPath(c); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 604 | pr = this->getClipPathRenderer(*clipPath, fill); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 605 | if (NULL == pr) { |
| 606 | fClipInStencil = false; |
| 607 | fClip = clip; |
| 608 | return false; |
| 609 | } |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 610 | canRenderDirectToStencil = |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 611 | !pr->requiresStencilPass(this, *clipPath, fill); |
| 612 | arp.set(pr, this, clipPath, fill, NULL); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 613 | } |
| 614 | |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 615 | GrSetOp op = (c == start) ? startOp : clip.getOp(c); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 616 | int passes; |
| 617 | GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses]; |
| 618 | |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 619 | bool canDrawDirectToClip; // Given the renderer, the element, |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 620 | // fill rule, and set operation can |
| 621 | // we render the element directly to |
| 622 | // stencil bit used for clipping. |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 623 | canDrawDirectToClip = |
| 624 | GrStencilSettings::GetClipPasses(op, |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 625 | canRenderDirectToStencil, |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 626 | clipBit, |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 627 | fillInverted, |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 628 | &passes, stencilSettings); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 629 | |
| 630 | // draw the element to the client stencil bits if necessary |
| 631 | if (!canDrawDirectToClip) { |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 632 | static const GrStencilSettings gDrawToStencil = { |
| 633 | kIncClamp_StencilOp, kIncClamp_StencilOp, |
| 634 | kIncClamp_StencilOp, kIncClamp_StencilOp, |
| 635 | kAlways_StencilFunc, kAlways_StencilFunc, |
| 636 | 0xffffffff, 0xffffffff, |
| 637 | 0x00000000, 0x00000000, |
| 638 | 0xffffffff, 0xffffffff, |
| 639 | }; |
| 640 | SET_RANDOM_COLOR |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 641 | if (kRect_ClipType == clip.getElementType(c)) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 642 | this->setStencil(gDrawToStencil); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 643 | this->drawSimpleRect(clip.getRect(c), NULL, 0); |
| 644 | } else { |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 645 | if (canRenderDirectToStencil) { |
| 646 | this->setStencil(gDrawToStencil); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 647 | pr->drawPath(0); |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 648 | } else { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 649 | pr->drawPathToStencil(); |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 650 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
| 654 | // now we modify the clip bit by rendering either the clip |
| 655 | // element directly or a bounding rect of the entire clip. |
| 656 | this->enableState(kModifyStencilClip_StateBit); |
| 657 | for (int p = 0; p < passes; ++p) { |
| 658 | this->setStencil(stencilSettings[p]); |
| 659 | if (canDrawDirectToClip) { |
| 660 | if (kRect_ClipType == clip.getElementType(c)) { |
| 661 | SET_RANDOM_COLOR |
| 662 | this->drawSimpleRect(clip.getRect(c), NULL, 0); |
| 663 | } else { |
| 664 | SET_RANDOM_COLOR |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 665 | pr->drawPath(0); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 666 | } |
| 667 | } else { |
| 668 | SET_RANDOM_COLOR |
thakis@chromium.org | 441d7da | 2011-06-07 04:03:17 +0000 | [diff] [blame] | 669 | this->drawSimpleRect(bounds, NULL, 0); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 672 | } |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 673 | // restore clip |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 674 | fClip = clip; |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 675 | // recusive draws would have disabled this since they drew with |
| 676 | // the clip bounds as clip. |
| 677 | fClipInStencil = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 678 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 679 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 680 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 681 | // Must flush the scissor after graphics state |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 682 | if (!this->flushGraphicsState(type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 683 | return false; |
| 684 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 685 | this->flushScissor(r); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 686 | return true; |
| 687 | } |
| 688 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 689 | GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path, |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 690 | GrPathFill fill) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 691 | if (NULL == fPathRendererChain) { |
| 692 | fPathRendererChain = |
| 693 | new GrPathRendererChain(this->getContext(), |
| 694 | GrPathRendererChain::kNonAAOnly_UsageFlag); |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 695 | } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 696 | return fPathRendererChain->getPathRenderer(this, path, fill); |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 700 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 701 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 702 | void GrGpu::geometrySourceWillPush() { |
| 703 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 704 | if (kArray_GeometrySrcType == geoSrc.fVertexSrc || |
| 705 | kReserved_GeometrySrcType == geoSrc.fVertexSrc) { |
| 706 | this->finalizeReservedVertices(); |
| 707 | } |
| 708 | if (kArray_GeometrySrcType == geoSrc.fIndexSrc || |
| 709 | kReserved_GeometrySrcType == geoSrc.fIndexSrc) { |
| 710 | this->finalizeReservedIndices(); |
| 711 | } |
| 712 | GeometryPoolState& newState = fGeomPoolStateStack.push_back(); |
| 713 | #if GR_DEBUG |
| 714 | newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 715 | newState.fPoolStartVertex = DEBUG_INVAL_START_IDX; |
| 716 | newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
| 717 | newState.fPoolStartIndex = DEBUG_INVAL_START_IDX; |
| 718 | #endif |
| 719 | } |
| 720 | |
| 721 | void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) { |
| 722 | // if popping last entry then pops are unbalanced with pushes |
| 723 | GrAssert(fGeomPoolStateStack.count() > 1); |
| 724 | fGeomPoolStateStack.pop_back(); |
| 725 | } |
| 726 | |
| 727 | void GrGpu::onDrawIndexed(GrPrimitiveType type, |
| 728 | int startVertex, |
| 729 | int startIndex, |
| 730 | int vertexCount, |
| 731 | int indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 732 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 733 | this->handleDirtyContext(); |
| 734 | |
| 735 | if (!this->setupClipAndFlushState(type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 736 | return; |
| 737 | } |
| 738 | |
| 739 | #if GR_COLLECT_STATS |
| 740 | fStats.fVertexCnt += vertexCount; |
| 741 | fStats.fIndexCnt += indexCount; |
| 742 | fStats.fDrawCnt += 1; |
| 743 | #endif |
| 744 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 745 | int sVertex = startVertex; |
| 746 | int sIndex = startIndex; |
| 747 | setupGeometry(&sVertex, &sIndex, vertexCount, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 748 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 749 | this->onGpuDrawIndexed(type, sVertex, sIndex, |
| 750 | vertexCount, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 751 | } |
| 752 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 753 | void GrGpu::onDrawNonIndexed(GrPrimitiveType type, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 754 | int startVertex, |
| 755 | int vertexCount) { |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 756 | this->handleDirtyContext(); |
| 757 | |
| 758 | if (!this->setupClipAndFlushState(type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 759 | return; |
| 760 | } |
| 761 | #if GR_COLLECT_STATS |
| 762 | fStats.fVertexCnt += vertexCount; |
| 763 | fStats.fDrawCnt += 1; |
| 764 | #endif |
| 765 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 766 | int sVertex = startVertex; |
| 767 | setupGeometry(&sVertex, NULL, vertexCount, 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 768 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 769 | this->onGpuDrawNonIndexed(type, sVertex, vertexCount); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | void GrGpu::finalizeReservedVertices() { |
| 773 | GrAssert(NULL != fVertexPool); |
| 774 | fVertexPool->unlock(); |
| 775 | } |
| 776 | |
| 777 | void GrGpu::finalizeReservedIndices() { |
| 778 | GrAssert(NULL != fIndexPool); |
| 779 | fIndexPool->unlock(); |
| 780 | } |
| 781 | |
| 782 | void GrGpu::prepareVertexPool() { |
| 783 | if (NULL == fVertexPool) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 784 | GrAssert(0 == fVertexPoolUseCnt); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 785 | fVertexPool = new GrVertexBufferAllocPool(this, true, |
| 786 | VERTEX_POOL_VB_SIZE, |
bsalomon@google.com | 7a5af8b | 2011-02-18 18:40:42 +0000 | [diff] [blame] | 787 | VERTEX_POOL_VB_COUNT); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 788 | fVertexPool->releaseGpuRef(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 789 | } else if (!fVertexPoolUseCnt) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 790 | // the client doesn't have valid data in the pool |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 791 | fVertexPool->reset(); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | void GrGpu::prepareIndexPool() { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 796 | if (NULL == fIndexPool) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 797 | GrAssert(0 == fIndexPoolUseCnt); |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 798 | fIndexPool = new GrIndexBufferAllocPool(this, true, |
| 799 | INDEX_POOL_IB_SIZE, |
| 800 | INDEX_POOL_IB_COUNT); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 801 | fIndexPool->releaseGpuRef(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 802 | } else if (!fIndexPoolUseCnt) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 803 | // the client doesn't have valid data in the pool |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 804 | fIndexPool->reset(); |
| 805 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 806 | } |
| 807 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 808 | bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout, |
| 809 | int vertexCount, |
| 810 | void** vertices) { |
| 811 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
| 812 | |
| 813 | GrAssert(vertexCount > 0); |
| 814 | GrAssert(NULL != vertices); |
| 815 | |
| 816 | this->prepareVertexPool(); |
| 817 | |
| 818 | *vertices = fVertexPool->makeSpace(vertexLayout, |
| 819 | vertexCount, |
| 820 | &geomPoolState.fPoolVertexBuffer, |
| 821 | &geomPoolState.fPoolStartVertex); |
| 822 | if (NULL == *vertices) { |
| 823 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 824 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 825 | ++fVertexPoolUseCnt; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 826 | return true; |
| 827 | } |
| 828 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 829 | bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) { |
| 830 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
| 831 | |
| 832 | GrAssert(indexCount > 0); |
| 833 | GrAssert(NULL != indices); |
| 834 | |
| 835 | this->prepareIndexPool(); |
| 836 | |
| 837 | *indices = fIndexPool->makeSpace(indexCount, |
| 838 | &geomPoolState.fPoolIndexBuffer, |
| 839 | &geomPoolState.fPoolStartIndex); |
| 840 | if (NULL == *indices) { |
| 841 | return false; |
| 842 | } |
| 843 | ++fIndexPoolUseCnt; |
| 844 | return true; |
| 845 | } |
| 846 | |
| 847 | void GrGpu::releaseReservedVertexSpace() { |
| 848 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 849 | GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc); |
| 850 | size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout); |
| 851 | fVertexPool->putBack(bytes); |
| 852 | --fVertexPoolUseCnt; |
| 853 | } |
| 854 | |
| 855 | void GrGpu::releaseReservedIndexSpace() { |
| 856 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 857 | GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc); |
| 858 | size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t); |
| 859 | fIndexPool->putBack(bytes); |
| 860 | --fIndexPoolUseCnt; |
| 861 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 862 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 863 | void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) { |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 864 | this->prepareVertexPool(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 865 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 866 | #if GR_DEBUG |
| 867 | bool success = |
| 868 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 869 | fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 870 | vertexCount, |
| 871 | vertexArray, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 872 | &geomPoolState.fPoolVertexBuffer, |
| 873 | &geomPoolState.fPoolStartVertex); |
| 874 | ++fVertexPoolUseCnt; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 875 | GR_DEBUGASSERT(success); |
| 876 | } |
| 877 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 878 | void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) { |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 879 | this->prepareIndexPool(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 880 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 881 | #if GR_DEBUG |
| 882 | bool success = |
| 883 | #endif |
| 884 | fIndexPool->appendIndices(indexCount, |
| 885 | indexArray, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 886 | &geomPoolState.fPoolIndexBuffer, |
| 887 | &geomPoolState.fPoolStartIndex); |
| 888 | ++fIndexPoolUseCnt; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 889 | GR_DEBUGASSERT(success); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 890 | } |
| 891 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 892 | void GrGpu::releaseVertexArray() { |
| 893 | // if vertex source was array, we stowed data in the pool |
| 894 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 895 | GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc); |
| 896 | size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout); |
| 897 | fVertexPool->putBack(bytes); |
| 898 | --fVertexPoolUseCnt; |
| 899 | } |
| 900 | |
| 901 | void GrGpu::releaseIndexArray() { |
| 902 | // if index source was array, we stowed data in the pool |
| 903 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 904 | GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc); |
| 905 | size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t); |
| 906 | fIndexPool->putBack(bytes); |
| 907 | --fIndexPoolUseCnt; |
| 908 | } |
| 909 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 910 | //////////////////////////////////////////////////////////////////////////////// |
| 911 | |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 912 | const GrGpuStats& GrGpu::getStats() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 913 | return fStats; |
| 914 | } |
| 915 | |
| 916 | void GrGpu::resetStats() { |
| 917 | memset(&fStats, 0, sizeof(fStats)); |
| 918 | } |
| 919 | |
| 920 | void GrGpu::printStats() const { |
| 921 | if (GR_COLLECT_STATS) { |
| 922 | GrPrintf( |
| 923 | "-v-------------------------GPU STATS----------------------------v-\n" |
| 924 | "Stats collection is: %s\n" |
| 925 | "Draws: %04d, Verts: %04d, Indices: %04d\n" |
| 926 | "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n" |
| 927 | "TexCreates: %04d, RTCreates:%04d\n" |
| 928 | "-^--------------------------------------------------------------^-\n", |
| 929 | (GR_COLLECT_STATS ? "ON" : "OFF"), |
| 930 | fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt, |
| 931 | fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt, |
| 932 | fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 937 | const GrSamplerState GrSamplerState::gClampNoFilter( |
| 938 | GrSamplerState::kClamp_WrapMode, |
| 939 | GrSamplerState::kClamp_WrapMode, |
| 940 | GrSamplerState::kNormal_SampleMode, |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 941 | GrMatrix::I(), |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 942 | GrSamplerState::kNearest_Filter); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 943 | |
| 944 | |
| 945 | |
| 946 | |