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