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