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