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" |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 13 | #include "GrContext.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | #include "GrIndexBuffer.h" |
tomhudson@google.com | dd182cb | 2012-02-10 21:01:00 +0000 | [diff] [blame] | 15 | #include "GrStencilBuffer.h" |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 16 | #include "GrVertexBuffer.h" |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 17 | |
| 18 | // 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] | 19 | static const size_t VERTEX_POOL_VB_SIZE = 1 << 18; |
| 20 | static const int VERTEX_POOL_VB_COUNT = 4; |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 21 | static const size_t INDEX_POOL_IB_SIZE = 1 << 16; |
| 22 | static const int INDEX_POOL_IB_COUNT = 4; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 24 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | |
| 26 | extern void gr_run_unittests(); |
| 27 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 28 | #define DEBUG_INVAL_BUFFER 0xdeadcafe |
| 29 | #define DEBUG_INVAL_START_IDX -1 |
| 30 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 31 | GrGpu::GrGpu() |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 32 | : fClipMaskManager(this) |
| 33 | , fContext(NULL) |
bsalomon@google.com | 979432b | 2011-11-05 21:38:22 +0000 | [diff] [blame] | 34 | , fResetTimestamp(kExpiredTimestamp+1) |
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 | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 41 | , fContextIsDirty(true) |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 42 | , fResourceHead(NULL) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 43 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | #if GR_DEBUG |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 45 | //gr_run_unittests(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 47 | |
| 48 | fGeomPoolStateStack.push_back(); |
| 49 | #if GR_DEBUG |
| 50 | GeometryPoolState& poolState = fGeomPoolStateStack.back(); |
| 51 | poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 52 | poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX; |
| 53 | poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
| 54 | poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX; |
| 55 | #endif |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 56 | |
| 57 | for (int i = 0; i < kGrPixelConfigCount; ++i) { |
| 58 | fConfigRenderSupport[i] = false; |
| 59 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | GrGpu::~GrGpu() { |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 63 | this->releaseResources(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 64 | } |
| 65 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 66 | void GrGpu::abandonResources() { |
| 67 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 68 | fClipMaskManager.releaseResources(); |
| 69 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 70 | while (NULL != fResourceHead) { |
| 71 | fResourceHead->abandon(); |
| 72 | } |
| 73 | |
| 74 | GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid()); |
| 75 | GrAssert(NULL == fUnitSquareVertexBuffer || |
| 76 | !fUnitSquareVertexBuffer->isValid()); |
| 77 | GrSafeSetNull(fQuadIndexBuffer); |
| 78 | GrSafeSetNull(fUnitSquareVertexBuffer); |
| 79 | delete fVertexPool; |
| 80 | fVertexPool = NULL; |
| 81 | delete fIndexPool; |
| 82 | fIndexPool = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 83 | } |
| 84 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 85 | void GrGpu::releaseResources() { |
| 86 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 87 | fClipMaskManager.releaseResources(); |
| 88 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 89 | while (NULL != fResourceHead) { |
| 90 | fResourceHead->release(); |
| 91 | } |
| 92 | |
| 93 | GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid()); |
| 94 | GrAssert(NULL == fUnitSquareVertexBuffer || |
| 95 | !fUnitSquareVertexBuffer->isValid()); |
| 96 | GrSafeSetNull(fQuadIndexBuffer); |
| 97 | GrSafeSetNull(fUnitSquareVertexBuffer); |
| 98 | delete fVertexPool; |
| 99 | fVertexPool = NULL; |
| 100 | delete fIndexPool; |
| 101 | fIndexPool = NULL; |
| 102 | } |
| 103 | |
| 104 | void GrGpu::insertResource(GrResource* resource) { |
| 105 | GrAssert(NULL != resource); |
| 106 | GrAssert(this == resource->getGpu()); |
| 107 | GrAssert(NULL == resource->fNext); |
| 108 | GrAssert(NULL == resource->fPrevious); |
| 109 | |
| 110 | resource->fNext = fResourceHead; |
| 111 | if (NULL != fResourceHead) { |
| 112 | GrAssert(NULL == fResourceHead->fPrevious); |
| 113 | fResourceHead->fPrevious = resource; |
| 114 | } |
| 115 | fResourceHead = resource; |
| 116 | } |
| 117 | |
| 118 | void GrGpu::removeResource(GrResource* resource) { |
| 119 | GrAssert(NULL != resource); |
| 120 | GrAssert(NULL != fResourceHead); |
| 121 | |
| 122 | if (fResourceHead == resource) { |
| 123 | GrAssert(NULL == resource->fPrevious); |
| 124 | fResourceHead = resource->fNext; |
| 125 | } else { |
| 126 | GrAssert(NULL != fResourceHead); |
| 127 | resource->fPrevious->fNext = resource->fNext; |
| 128 | } |
| 129 | if (NULL != resource->fNext) { |
| 130 | resource->fNext->fPrevious = resource->fPrevious; |
| 131 | } |
| 132 | resource->fNext = NULL; |
| 133 | resource->fPrevious = NULL; |
| 134 | } |
| 135 | |
| 136 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | void GrGpu::unimpl(const char msg[]) { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 138 | #if GR_DEBUG |
| 139 | GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg); |
| 140 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 141 | } |
| 142 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 143 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 144 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 145 | GrTexture* GrGpu::createTexture(const GrTextureDesc& desc, |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 146 | const void* srcData, size_t rowBytes) { |
| 147 | this->handleDirtyContext(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 148 | GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes); |
| 149 | if (NULL != tex && |
| 150 | (kRenderTarget_GrTextureFlagBit & desc.fFlags) && |
| 151 | !(kNoStencil_GrTextureFlagBit & desc.fFlags)) { |
| 152 | GrAssert(NULL != tex->asRenderTarget()); |
| 153 | // TODO: defer this and attach dynamically |
| 154 | if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) { |
| 155 | tex->unref(); |
| 156 | return NULL; |
| 157 | } |
| 158 | } |
| 159 | return tex; |
| 160 | } |
| 161 | |
| 162 | bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) { |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 163 | GrAssert(NULL == rt->getStencilBuffer()); |
| 164 | GrStencilBuffer* sb = |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 165 | this->getContext()->findStencilBuffer(rt->width(), |
| 166 | rt->height(), |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 167 | rt->numSamples()); |
| 168 | if (NULL != sb) { |
| 169 | rt->setStencilBuffer(sb); |
| 170 | bool attached = this->attachStencilBufferToRenderTarget(sb, rt); |
| 171 | if (!attached) { |
| 172 | rt->setStencilBuffer(NULL); |
| 173 | } |
| 174 | return attached; |
| 175 | } |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 176 | if (this->createStencilBufferForRenderTarget(rt, |
| 177 | rt->width(), rt->height())) { |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 178 | rt->getStencilBuffer()->ref(); |
| 179 | rt->getStencilBuffer()->transferToCacheAndLock(); |
| 180 | |
bsalomon@google.com | edc177d | 2011-08-05 15:46:40 +0000 | [diff] [blame] | 181 | // Right now we're clearing the stencil buffer here after it is |
| 182 | // attached to an RT for the first time. When we start matching |
| 183 | // stencil buffers with smaller color targets this will no longer |
| 184 | // be correct because it won't be guaranteed to clear the entire |
| 185 | // sb. |
| 186 | // We used to clear down in the GL subclass using a special purpose |
| 187 | // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported |
| 188 | // FBO status. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 189 | GrDrawState::AutoRenderTargetRestore artr(this->drawState(), rt); |
bsalomon@google.com | edc177d | 2011-08-05 15:46:40 +0000 | [diff] [blame] | 190 | this->clearStencil(); |
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 | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 197 | GrTexture* GrGpu::createPlatformTexture(const GrPlatformTextureDesc& desc) { |
| 198 | this->handleDirtyContext(); |
| 199 | GrTexture* tex = this->onCreatePlatformTexture(desc); |
bsalomon@google.com | a14dd6d | 2012-01-03 21:08:12 +0000 | [diff] [blame] | 200 | if (NULL == tex) { |
| 201 | return NULL; |
| 202 | } |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 203 | // TODO: defer this and attach dynamically |
| 204 | GrRenderTarget* tgt = tex->asRenderTarget(); |
| 205 | if (NULL != tgt && |
| 206 | !this->attachStencilBufferToRenderTarget(tgt)) { |
| 207 | tex->unref(); |
| 208 | return NULL; |
| 209 | } else { |
| 210 | return tex; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | GrRenderTarget* GrGpu::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) { |
| 215 | this->handleDirtyContext(); |
| 216 | return this->onCreatePlatformRenderTarget(desc); |
| 217 | } |
| 218 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 219 | GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) { |
| 220 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 221 | return this->onCreateVertexBuffer(size, dynamic); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) { |
| 225 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 226 | return this->onCreateIndexBuffer(size, dynamic); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 227 | } |
| 228 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 229 | GrPath* GrGpu::createPath(const SkPath& path) { |
| 230 | GrAssert(fCaps.fPathStencilingSupport); |
| 231 | this->handleDirtyContext(); |
| 232 | return this->onCreatePath(path); |
| 233 | } |
| 234 | |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 235 | void GrGpu::clear(const GrIRect* rect, |
| 236 | GrColor color, |
| 237 | GrRenderTarget* renderTarget) { |
| 238 | GrRenderTarget* oldRT = NULL; |
| 239 | if (NULL != renderTarget && |
| 240 | renderTarget != this->drawState()->getRenderTarget()) { |
| 241 | oldRT = this->drawState()->getRenderTarget(); |
| 242 | this->drawState()->setRenderTarget(renderTarget); |
| 243 | } |
| 244 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 245 | if (NULL == this->getDrawState().getRenderTarget()) { |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 246 | return; |
| 247 | } |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 248 | this->handleDirtyContext(); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 249 | this->onClear(rect, color); |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 250 | |
| 251 | if (NULL != oldRT) { |
| 252 | this->drawState()->setRenderTarget(oldRT); |
| 253 | } |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void GrGpu::forceRenderTargetFlush() { |
| 257 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 258 | this->onForceRenderTargetFlush(); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 259 | } |
| 260 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 261 | bool GrGpu::readPixels(GrRenderTarget* target, |
| 262 | int left, int top, int width, int height, |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 263 | GrPixelConfig config, void* buffer, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 264 | size_t rowBytes, bool invertY) { |
| 265 | GrAssert(GrPixelConfigIsUnpremultiplied(config) == |
| 266 | GrPixelConfigIsUnpremultiplied(target->config())); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 267 | this->handleDirtyContext(); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 268 | return this->onReadPixels(target, left, top, width, height, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 269 | config, buffer, rowBytes, invertY); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 270 | } |
| 271 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 272 | void GrGpu::writeTexturePixels(GrTexture* texture, |
| 273 | int left, int top, int width, int height, |
| 274 | GrPixelConfig config, const void* buffer, |
| 275 | size_t rowBytes) { |
| 276 | GrAssert(GrPixelConfigIsUnpremultiplied(config) == |
| 277 | GrPixelConfigIsUnpremultiplied(texture->config())); |
| 278 | this->handleDirtyContext(); |
| 279 | this->onWriteTexturePixels(texture, left, top, width, height, |
| 280 | config, buffer, rowBytes); |
| 281 | } |
| 282 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 283 | void GrGpu::resolveRenderTarget(GrRenderTarget* target) { |
| 284 | GrAssert(target); |
| 285 | this->handleDirtyContext(); |
| 286 | this->onResolveRenderTarget(target); |
| 287 | } |
| 288 | |
| 289 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 290 | //////////////////////////////////////////////////////////////////////////////// |
| 291 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 292 | 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] | 293 | |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 294 | GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 295 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 296 | static inline void fill_indices(uint16_t* indices, int quadCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 297 | for (int i = 0; i < quadCount; ++i) { |
| 298 | indices[6 * i + 0] = 4 * i + 0; |
| 299 | indices[6 * i + 1] = 4 * i + 1; |
| 300 | indices[6 * i + 2] = 4 * i + 2; |
| 301 | indices[6 * i + 3] = 4 * i + 0; |
| 302 | indices[6 * i + 4] = 4 * i + 2; |
| 303 | indices[6 * i + 5] = 4 * i + 3; |
| 304 | } |
| 305 | } |
| 306 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 307 | const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 308 | if (NULL == fQuadIndexBuffer) { |
| 309 | static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS; |
| 310 | GrGpu* me = const_cast<GrGpu*>(this); |
| 311 | fQuadIndexBuffer = me->createIndexBuffer(SIZE, false); |
| 312 | if (NULL != fQuadIndexBuffer) { |
| 313 | uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock(); |
| 314 | if (NULL != indices) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 315 | fill_indices(indices, MAX_QUADS); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 316 | fQuadIndexBuffer->unlock(); |
| 317 | } else { |
| 318 | indices = (uint16_t*)GrMalloc(SIZE); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 319 | fill_indices(indices, MAX_QUADS); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 320 | if (!fQuadIndexBuffer->updateData(indices, SIZE)) { |
| 321 | fQuadIndexBuffer->unref(); |
| 322 | fQuadIndexBuffer = NULL; |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 323 | GrCrash("Can't get indices into buffer!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 324 | } |
| 325 | GrFree(indices); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return fQuadIndexBuffer; |
| 331 | } |
| 332 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 333 | const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const { |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 334 | if (NULL == fUnitSquareVertexBuffer) { |
| 335 | |
| 336 | static const GrPoint DATA[] = { |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 337 | { 0, 0 }, |
| 338 | { GR_Scalar1, 0 }, |
| 339 | { GR_Scalar1, GR_Scalar1 }, |
| 340 | { 0, GR_Scalar1 } |
| 341 | #if 0 |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 342 | GrPoint(0, 0), |
| 343 | GrPoint(GR_Scalar1,0), |
| 344 | GrPoint(GR_Scalar1,GR_Scalar1), |
| 345 | GrPoint(0, GR_Scalar1) |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 346 | #endif |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 347 | }; |
| 348 | static const size_t SIZE = sizeof(DATA); |
| 349 | |
| 350 | GrGpu* me = const_cast<GrGpu*>(this); |
| 351 | fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false); |
| 352 | if (NULL != fUnitSquareVertexBuffer) { |
| 353 | if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) { |
| 354 | fUnitSquareVertexBuffer->unref(); |
| 355 | fUnitSquareVertexBuffer = NULL; |
| 356 | GrCrash("Can't get vertices into buffer!"); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return fUnitSquareVertexBuffer; |
| 362 | } |
| 363 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 364 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 365 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 366 | bool GrGpu::setupClipAndFlushState(DrawType type) { |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 367 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 368 | if (!fClipMaskManager.setupClipping(fClip)) { |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 369 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 370 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 371 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 372 | if (!this->flushGraphicsState(type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 373 | return false; |
| 374 | } |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 375 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 376 | return true; |
| 377 | } |
| 378 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 379 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 380 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 381 | void GrGpu::geometrySourceWillPush() { |
| 382 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 383 | if (kArray_GeometrySrcType == geoSrc.fVertexSrc || |
| 384 | kReserved_GeometrySrcType == geoSrc.fVertexSrc) { |
| 385 | this->finalizeReservedVertices(); |
| 386 | } |
| 387 | if (kArray_GeometrySrcType == geoSrc.fIndexSrc || |
| 388 | kReserved_GeometrySrcType == geoSrc.fIndexSrc) { |
| 389 | this->finalizeReservedIndices(); |
| 390 | } |
| 391 | GeometryPoolState& newState = fGeomPoolStateStack.push_back(); |
| 392 | #if GR_DEBUG |
| 393 | newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 394 | newState.fPoolStartVertex = DEBUG_INVAL_START_IDX; |
| 395 | newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
| 396 | newState.fPoolStartIndex = DEBUG_INVAL_START_IDX; |
| 397 | #endif |
| 398 | } |
| 399 | |
| 400 | void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) { |
| 401 | // if popping last entry then pops are unbalanced with pushes |
| 402 | GrAssert(fGeomPoolStateStack.count() > 1); |
| 403 | fGeomPoolStateStack.pop_back(); |
| 404 | } |
| 405 | |
| 406 | void GrGpu::onDrawIndexed(GrPrimitiveType type, |
| 407 | int startVertex, |
| 408 | int startIndex, |
| 409 | int vertexCount, |
| 410 | int indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 411 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 412 | this->handleDirtyContext(); |
| 413 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 414 | if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 418 | int sVertex = startVertex; |
| 419 | int sIndex = startIndex; |
| 420 | setupGeometry(&sVertex, &sIndex, vertexCount, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 421 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 422 | this->onGpuDrawIndexed(type, sVertex, sIndex, |
| 423 | vertexCount, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 424 | } |
| 425 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 426 | void GrGpu::onDrawNonIndexed(GrPrimitiveType type, |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 427 | int startVertex, |
| 428 | int vertexCount) { |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 429 | this->handleDirtyContext(); |
| 430 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 431 | if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 432 | return; |
| 433 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 434 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 435 | int sVertex = startVertex; |
| 436 | setupGeometry(&sVertex, NULL, vertexCount, 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 437 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 438 | this->onGpuDrawNonIndexed(type, sVertex, vertexCount); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 439 | } |
| 440 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 441 | void GrGpu::onStencilPath(const GrPath* path, GrPathFill fill) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 442 | this->handleDirtyContext(); |
| 443 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 444 | // TODO: make this more effecient (don't copy and copy back) |
| 445 | GrAutoTRestore<GrStencilSettings> asr(this->drawState()->stencil()); |
| 446 | |
| 447 | this->setStencilPathSettings(*path, fill, this->drawState()->stencil()); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 448 | if (!this->setupClipAndFlushState(kStencilPath_DrawType)) { |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | this->onGpuStencilPath(path, fill); |
| 453 | } |
| 454 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 455 | void GrGpu::finalizeReservedVertices() { |
| 456 | GrAssert(NULL != fVertexPool); |
| 457 | fVertexPool->unlock(); |
| 458 | } |
| 459 | |
| 460 | void GrGpu::finalizeReservedIndices() { |
| 461 | GrAssert(NULL != fIndexPool); |
| 462 | fIndexPool->unlock(); |
| 463 | } |
| 464 | |
| 465 | void GrGpu::prepareVertexPool() { |
| 466 | if (NULL == fVertexPool) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 467 | GrAssert(0 == fVertexPoolUseCnt); |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 468 | fVertexPool = SkNEW_ARGS(GrVertexBufferAllocPool, (this, true, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 469 | VERTEX_POOL_VB_SIZE, |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 470 | VERTEX_POOL_VB_COUNT)); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 471 | fVertexPool->releaseGpuRef(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 472 | } else if (!fVertexPoolUseCnt) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 473 | // the client doesn't have valid data in the pool |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 474 | fVertexPool->reset(); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | void GrGpu::prepareIndexPool() { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 479 | if (NULL == fIndexPool) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 480 | GrAssert(0 == fIndexPoolUseCnt); |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 481 | fIndexPool = SkNEW_ARGS(GrIndexBufferAllocPool, (this, true, |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 482 | INDEX_POOL_IB_SIZE, |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 483 | INDEX_POOL_IB_COUNT)); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 484 | fIndexPool->releaseGpuRef(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 485 | } else if (!fIndexPoolUseCnt) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 486 | // the client doesn't have valid data in the pool |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 487 | fIndexPool->reset(); |
| 488 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 489 | } |
| 490 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 491 | bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout, |
| 492 | int vertexCount, |
| 493 | void** vertices) { |
| 494 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
| 495 | |
| 496 | GrAssert(vertexCount > 0); |
| 497 | GrAssert(NULL != vertices); |
| 498 | |
| 499 | this->prepareVertexPool(); |
| 500 | |
| 501 | *vertices = fVertexPool->makeSpace(vertexLayout, |
| 502 | vertexCount, |
| 503 | &geomPoolState.fPoolVertexBuffer, |
| 504 | &geomPoolState.fPoolStartVertex); |
| 505 | if (NULL == *vertices) { |
| 506 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 507 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 508 | ++fVertexPoolUseCnt; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 509 | return true; |
| 510 | } |
| 511 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 512 | bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) { |
| 513 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
| 514 | |
| 515 | GrAssert(indexCount > 0); |
| 516 | GrAssert(NULL != indices); |
| 517 | |
| 518 | this->prepareIndexPool(); |
| 519 | |
| 520 | *indices = fIndexPool->makeSpace(indexCount, |
| 521 | &geomPoolState.fPoolIndexBuffer, |
| 522 | &geomPoolState.fPoolStartIndex); |
| 523 | if (NULL == *indices) { |
| 524 | return false; |
| 525 | } |
| 526 | ++fIndexPoolUseCnt; |
| 527 | return true; |
| 528 | } |
| 529 | |
| 530 | void GrGpu::releaseReservedVertexSpace() { |
| 531 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 532 | GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc); |
| 533 | size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout); |
| 534 | fVertexPool->putBack(bytes); |
| 535 | --fVertexPoolUseCnt; |
| 536 | } |
| 537 | |
| 538 | void GrGpu::releaseReservedIndexSpace() { |
| 539 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 540 | GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc); |
| 541 | size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t); |
| 542 | fIndexPool->putBack(bytes); |
| 543 | --fIndexPoolUseCnt; |
| 544 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 545 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 546 | void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) { |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 547 | this->prepareVertexPool(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 548 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 549 | #if GR_DEBUG |
| 550 | bool success = |
| 551 | #endif |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 552 | fVertexPool->appendVertices(this->getVertexLayout(), |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 553 | vertexCount, |
| 554 | vertexArray, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 555 | &geomPoolState.fPoolVertexBuffer, |
| 556 | &geomPoolState.fPoolStartVertex); |
| 557 | ++fVertexPoolUseCnt; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 558 | GR_DEBUGASSERT(success); |
| 559 | } |
| 560 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 561 | void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) { |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 562 | this->prepareIndexPool(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 563 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 564 | #if GR_DEBUG |
| 565 | bool success = |
| 566 | #endif |
| 567 | fIndexPool->appendIndices(indexCount, |
| 568 | indexArray, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 569 | &geomPoolState.fPoolIndexBuffer, |
| 570 | &geomPoolState.fPoolStartIndex); |
| 571 | ++fIndexPoolUseCnt; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 572 | GR_DEBUGASSERT(success); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 573 | } |
| 574 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 575 | void GrGpu::releaseVertexArray() { |
| 576 | // if vertex source was array, we stowed data in the pool |
| 577 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 578 | GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc); |
| 579 | size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout); |
| 580 | fVertexPool->putBack(bytes); |
| 581 | --fVertexPoolUseCnt; |
| 582 | } |
| 583 | |
| 584 | void GrGpu::releaseIndexArray() { |
| 585 | // if index source was array, we stowed data in the pool |
| 586 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 587 | GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc); |
| 588 | size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t); |
| 589 | fIndexPool->putBack(bytes); |
| 590 | --fIndexPoolUseCnt; |
| 591 | } |
| 592 | |