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