reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | #include "GrGpu.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | #include "GrTextStrike.h" |
| 19 | #include "GrTextureCache.h" |
| 20 | #include "GrClipIterator.h" |
| 21 | #include "GrIndexBuffer.h" |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 22 | #include "GrVertexBuffer.h" |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 23 | #include "GrBufferAllocPool.h" |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 24 | #include "GrPathRenderer.h" |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 25 | |
| 26 | // 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] | 27 | static const size_t VERTEX_POOL_VB_SIZE = 1 << 18; |
| 28 | static const int VERTEX_POOL_VB_COUNT = 4; |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame^] | 29 | static const size_t INDEX_POOL_IB_SIZE = 1 << 16; |
| 30 | static const int INDEX_POOL_IB_COUNT = 4; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 31 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 32 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | |
| 34 | extern void gr_run_unittests(); |
| 35 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 36 | #define DEBUG_INVAL_BUFFER 0xdeadcafe |
| 37 | #define DEBUG_INVAL_START_IDX -1 |
| 38 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 39 | GrGpu::GrGpu() |
| 40 | : f8bitPaletteSupport(false) |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 41 | , fContext(NULL) |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 42 | , fVertexPool(NULL) |
| 43 | , fIndexPool(NULL) |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 44 | , fVertexPoolUseCnt(0) |
| 45 | , fIndexPoolUseCnt(0) |
| 46 | , fGeomPoolStateStack(&fGeoSrcStateStackStorage) |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 47 | , fQuadIndexBuffer(NULL) |
| 48 | , fUnitSquareVertexBuffer(NULL) |
| 49 | , fDefaultPathRenderer(NULL) |
| 50 | , fClientPathRenderer(NULL) |
| 51 | , fContextIsDirty(true) |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 52 | , fResourceHead(NULL) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 53 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 54 | #if GR_DEBUG |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 55 | //gr_run_unittests(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 57 | |
| 58 | fGeomPoolStateStack.push_back(); |
| 59 | #if GR_DEBUG |
| 60 | GeometryPoolState& poolState = fGeomPoolStateStack.back(); |
| 61 | poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 62 | poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX; |
| 63 | poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
| 64 | poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX; |
| 65 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 66 | resetStats(); |
| 67 | } |
| 68 | |
| 69 | GrGpu::~GrGpu() { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 70 | releaseResources(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | } |
| 72 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 73 | void GrGpu::abandonResources() { |
| 74 | |
| 75 | while (NULL != fResourceHead) { |
| 76 | fResourceHead->abandon(); |
| 77 | } |
| 78 | |
| 79 | GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid()); |
| 80 | GrAssert(NULL == fUnitSquareVertexBuffer || |
| 81 | !fUnitSquareVertexBuffer->isValid()); |
| 82 | GrSafeSetNull(fQuadIndexBuffer); |
| 83 | GrSafeSetNull(fUnitSquareVertexBuffer); |
| 84 | delete fVertexPool; |
| 85 | fVertexPool = NULL; |
| 86 | delete fIndexPool; |
| 87 | fIndexPool = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 88 | } |
| 89 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 90 | void GrGpu::releaseResources() { |
| 91 | |
| 92 | while (NULL != fResourceHead) { |
| 93 | fResourceHead->release(); |
| 94 | } |
| 95 | |
| 96 | GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid()); |
| 97 | GrAssert(NULL == fUnitSquareVertexBuffer || |
| 98 | !fUnitSquareVertexBuffer->isValid()); |
| 99 | GrSafeSetNull(fQuadIndexBuffer); |
| 100 | GrSafeSetNull(fUnitSquareVertexBuffer); |
| 101 | delete fVertexPool; |
| 102 | fVertexPool = NULL; |
| 103 | delete fIndexPool; |
| 104 | fIndexPool = NULL; |
| 105 | } |
| 106 | |
| 107 | void GrGpu::insertResource(GrResource* resource) { |
| 108 | GrAssert(NULL != resource); |
| 109 | GrAssert(this == resource->getGpu()); |
| 110 | GrAssert(NULL == resource->fNext); |
| 111 | GrAssert(NULL == resource->fPrevious); |
| 112 | |
| 113 | resource->fNext = fResourceHead; |
| 114 | if (NULL != fResourceHead) { |
| 115 | GrAssert(NULL == fResourceHead->fPrevious); |
| 116 | fResourceHead->fPrevious = resource; |
| 117 | } |
| 118 | fResourceHead = resource; |
| 119 | } |
| 120 | |
| 121 | void GrGpu::removeResource(GrResource* resource) { |
| 122 | GrAssert(NULL != resource); |
| 123 | GrAssert(NULL != fResourceHead); |
| 124 | |
| 125 | if (fResourceHead == resource) { |
| 126 | GrAssert(NULL == resource->fPrevious); |
| 127 | fResourceHead = resource->fNext; |
| 128 | } else { |
| 129 | GrAssert(NULL != fResourceHead); |
| 130 | resource->fPrevious->fNext = resource->fNext; |
| 131 | } |
| 132 | if (NULL != resource->fNext) { |
| 133 | resource->fNext->fPrevious = resource->fPrevious; |
| 134 | } |
| 135 | resource->fNext = NULL; |
| 136 | resource->fPrevious = NULL; |
| 137 | } |
| 138 | |
| 139 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 140 | void GrGpu::unimpl(const char msg[]) { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 141 | #if GR_DEBUG |
| 142 | GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg); |
| 143 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 144 | } |
| 145 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 146 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 147 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 148 | GrTexture* GrGpu::createTexture(const GrTextureDesc& desc, |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 149 | const void* srcData, size_t rowBytes) { |
| 150 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 151 | return this->onCreateTexture(desc, srcData, rowBytes); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 152 | } |
| 153 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 154 | GrRenderTarget* GrGpu::createRenderTargetFrom3DApiState() { |
| 155 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 156 | return this->onCreateRenderTargetFrom3DApiState(); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 157 | } |
| 158 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 159 | GrResource* GrGpu::createPlatformSurface(const GrPlatformSurfaceDesc& desc) { |
| 160 | this->handleDirtyContext(); |
| 161 | return this->onCreatePlatformSurface(desc); |
| 162 | } |
| 163 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 164 | GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) { |
| 165 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 166 | return this->onCreateVertexBuffer(size, dynamic); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) { |
| 170 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 171 | return this->onCreateIndexBuffer(size, dynamic); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 172 | } |
| 173 | |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 174 | void GrGpu::clear(const GrIRect* rect, GrColor color) { |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 175 | this->handleDirtyContext(); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 176 | this->onClear(rect, color); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void GrGpu::forceRenderTargetFlush() { |
| 180 | this->handleDirtyContext(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 181 | this->onForceRenderTargetFlush(); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 182 | } |
| 183 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 184 | bool GrGpu::readPixels(GrRenderTarget* target, |
| 185 | int left, int top, int width, int height, |
| 186 | GrPixelConfig config, void* buffer) { |
| 187 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 188 | this->handleDirtyContext(); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 189 | return this->onReadPixels(target, left, top, width, height, config, buffer); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | //////////////////////////////////////////////////////////////////////////////// |
| 193 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 194 | 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] | 195 | |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 196 | GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 197 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 198 | static inline void fill_indices(uint16_t* indices, int quadCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 199 | for (int i = 0; i < quadCount; ++i) { |
| 200 | indices[6 * i + 0] = 4 * i + 0; |
| 201 | indices[6 * i + 1] = 4 * i + 1; |
| 202 | indices[6 * i + 2] = 4 * i + 2; |
| 203 | indices[6 * i + 3] = 4 * i + 0; |
| 204 | indices[6 * i + 4] = 4 * i + 2; |
| 205 | indices[6 * i + 5] = 4 * i + 3; |
| 206 | } |
| 207 | } |
| 208 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 209 | const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 210 | if (NULL == fQuadIndexBuffer) { |
| 211 | static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS; |
| 212 | GrGpu* me = const_cast<GrGpu*>(this); |
| 213 | fQuadIndexBuffer = me->createIndexBuffer(SIZE, false); |
| 214 | if (NULL != fQuadIndexBuffer) { |
| 215 | uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock(); |
| 216 | if (NULL != indices) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 217 | fill_indices(indices, MAX_QUADS); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 218 | fQuadIndexBuffer->unlock(); |
| 219 | } else { |
| 220 | indices = (uint16_t*)GrMalloc(SIZE); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 221 | fill_indices(indices, MAX_QUADS); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 222 | if (!fQuadIndexBuffer->updateData(indices, SIZE)) { |
| 223 | fQuadIndexBuffer->unref(); |
| 224 | fQuadIndexBuffer = NULL; |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 225 | GrCrash("Can't get indices into buffer!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 226 | } |
| 227 | GrFree(indices); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return fQuadIndexBuffer; |
| 233 | } |
| 234 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 235 | const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const { |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 236 | if (NULL == fUnitSquareVertexBuffer) { |
| 237 | |
| 238 | static const GrPoint DATA[] = { |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 239 | { 0, 0 }, |
| 240 | { GR_Scalar1, 0 }, |
| 241 | { GR_Scalar1, GR_Scalar1 }, |
| 242 | { 0, GR_Scalar1 } |
| 243 | #if 0 |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 244 | GrPoint(0, 0), |
| 245 | GrPoint(GR_Scalar1,0), |
| 246 | GrPoint(GR_Scalar1,GR_Scalar1), |
| 247 | GrPoint(0, GR_Scalar1) |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 248 | #endif |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 249 | }; |
| 250 | static const size_t SIZE = sizeof(DATA); |
| 251 | |
| 252 | GrGpu* me = const_cast<GrGpu*>(this); |
| 253 | fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false); |
| 254 | if (NULL != fUnitSquareVertexBuffer) { |
| 255 | if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) { |
| 256 | fUnitSquareVertexBuffer->unref(); |
| 257 | fUnitSquareVertexBuffer = NULL; |
| 258 | GrCrash("Can't get vertices into buffer!"); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return fUnitSquareVertexBuffer; |
| 264 | } |
| 265 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 266 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 267 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 268 | void GrGpu::clipWillBeSet(const GrClip& newClip) { |
| 269 | if (newClip != fClip) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 270 | fClipState.fClipIsDirty = true; |
| 271 | } |
| 272 | } |
| 273 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 274 | //////////////////////////////////////////////////////////////////////////////// |
| 275 | |
| 276 | // stencil settings to use when clip is in stencil |
| 277 | const GrStencilSettings GrGpu::gClipStencilSettings = { |
| 278 | kKeep_StencilOp, kKeep_StencilOp, |
| 279 | kKeep_StencilOp, kKeep_StencilOp, |
| 280 | kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc, |
| 281 | 0, 0, |
| 282 | 0, 0, |
| 283 | 0, 0 |
| 284 | }; |
| 285 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 286 | // mapping of clip-respecting stencil funcs to normal stencil funcs |
| 287 | // mapping depends on whether stencil-clipping is in effect. |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 288 | static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = { |
| 289 | {// Stencil-Clipping is DISABLED, effectively always inside the clip |
| 290 | // In the Clip Funcs |
| 291 | kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 292 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 293 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 294 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 295 | // Special in the clip func that forces user's ref to be 0. |
| 296 | kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 297 | // make ref 0 and do normal nequal. |
| 298 | }, |
| 299 | {// Stencil-Clipping is ENABLED |
| 300 | // In the Clip Funcs |
| 301 | kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 302 | // eq stencil clip bit, mask |
| 303 | // out user bits. |
| 304 | |
| 305 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 306 | // add stencil bit to mask and ref |
| 307 | |
| 308 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 309 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 310 | // for both of these we can add |
| 311 | // the clip bit to the mask and |
| 312 | // ref and compare as normal |
| 313 | // Special in the clip func that forces user's ref to be 0. |
| 314 | kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 315 | // make ref have only the clip bit set |
| 316 | // and make comparison be less |
| 317 | // 10..0 < 1..user_bits.. |
| 318 | } |
| 319 | }; |
| 320 | |
| 321 | GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) { |
| 322 | GrAssert(func >= 0); |
| 323 | if (func >= kBasicStencilFuncCount) { |
| 324 | GrAssert(func < kStencilFuncCount); |
| 325 | func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount]; |
| 326 | GrAssert(func >= 0 && func < kBasicStencilFuncCount); |
| 327 | } |
| 328 | return func; |
| 329 | } |
| 330 | |
| 331 | void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func, |
| 332 | bool clipInStencil, |
| 333 | unsigned int clipBit, |
| 334 | unsigned int userBits, |
| 335 | unsigned int* ref, |
| 336 | unsigned int* mask) { |
| 337 | if (func < kBasicStencilFuncCount) { |
| 338 | *mask &= userBits; |
| 339 | *ref &= userBits; |
| 340 | } else { |
| 341 | if (clipInStencil) { |
| 342 | switch (func) { |
| 343 | case kAlwaysIfInClip_StencilFunc: |
| 344 | *mask = clipBit; |
| 345 | *ref = clipBit; |
| 346 | break; |
| 347 | case kEqualIfInClip_StencilFunc: |
| 348 | case kLessIfInClip_StencilFunc: |
| 349 | case kLEqualIfInClip_StencilFunc: |
| 350 | *mask = (*mask & userBits) | clipBit; |
| 351 | *ref = (*ref & userBits) | clipBit; |
| 352 | break; |
| 353 | case kNonZeroIfInClip_StencilFunc: |
| 354 | *mask = (*mask & userBits) | clipBit; |
| 355 | *ref = clipBit; |
| 356 | break; |
| 357 | default: |
| 358 | GrCrash("Unknown stencil func"); |
| 359 | } |
| 360 | } else { |
| 361 | *mask &= userBits; |
| 362 | *ref &= userBits; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | //////////////////////////////////////////////////////////////////////////////// |
| 368 | |
| 369 | #define VISUALIZE_COMPLEX_CLIP 0 |
| 370 | |
| 371 | #if VISUALIZE_COMPLEX_CLIP |
| 372 | #include "GrRandom.h" |
| 373 | GrRandom gRandom; |
| 374 | #define SET_RANDOM_COLOR this->setColor(0xff000000 | gRandom.nextU()); |
| 375 | #else |
| 376 | #define SET_RANDOM_COLOR |
| 377 | #endif |
| 378 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 379 | bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 380 | const GrIRect* r = NULL; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 381 | GrIRect clipRect; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 382 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 383 | // we check this early because we need a valid |
| 384 | // render target to setup stencil clipping |
| 385 | // before even going into flushGraphicsState |
| 386 | if (NULL == fCurrDrawState.fRenderTarget) { |
| 387 | GrAssert(!"No render target bound."); |
| 388 | return false; |
| 389 | } |
| 390 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 391 | if (fCurrDrawState.fFlagBits & kClip_StateBit) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 392 | GrRenderTarget& rt = *fCurrDrawState.fRenderTarget; |
| 393 | |
| 394 | GrRect bounds; |
| 395 | GrRect rtRect; |
| 396 | rtRect.setLTRB(0, 0, |
| 397 | GrIntToScalar(rt.width()), GrIntToScalar(rt.height())); |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 398 | if (fClip.hasConservativeBounds()) { |
| 399 | bounds = fClip.getConservativeBounds(); |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 400 | if (!bounds.intersect(rtRect)) { |
| 401 | bounds.setEmpty(); |
| 402 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 403 | } else { |
| 404 | bounds = rtRect; |
| 405 | } |
| 406 | |
| 407 | bounds.roundOut(&clipRect); |
| 408 | if (clipRect.isEmpty()) { |
| 409 | clipRect.setLTRB(0,0,0,0); |
| 410 | } |
| 411 | r = &clipRect; |
| 412 | |
| 413 | fClipState.fClipInStencil = !fClip.isRect() && |
| 414 | !fClip.isEmpty() && |
| 415 | !bounds.isEmpty(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 416 | |
| 417 | if (fClipState.fClipInStencil && |
| 418 | (fClipState.fClipIsDirty || |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 419 | fClip != rt.fLastStencilClip)) { |
| 420 | |
| 421 | rt.fLastStencilClip = fClip; |
| 422 | // we set the current clip to the bounds so that our recursive |
| 423 | // draws are scissored to them. We use the copy of the complex clip |
| 424 | // in the rt to render |
| 425 | const GrClip& clip = rt.fLastStencilClip; |
| 426 | fClip.setFromRect(bounds); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 427 | |
| 428 | AutoStateRestore asr(this); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 429 | AutoGeometryPush agp(this); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 430 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 431 | this->setViewMatrix(GrMatrix::I()); |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 432 | this->clearStencilClip(clipRect); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 433 | this->flushScissor(NULL); |
| 434 | #if !VISUALIZE_COMPLEX_CLIP |
| 435 | this->enableState(kNoColorWrites_StateBit); |
| 436 | #else |
| 437 | this->disableState(kNoColorWrites_StateBit); |
| 438 | #endif |
| 439 | int count = clip.getElementCount(); |
| 440 | int clipBit = rt.stencilBits(); |
| 441 | clipBit = (1 << (clipBit-1)); |
| 442 | |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 443 | // often we'll see the first two elements of the clip are |
| 444 | // the full rt size and another element intersected with it. |
| 445 | // We can skip the first full-size rect and save a big rect draw. |
| 446 | int firstElement = 0; |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 447 | if (clip.getElementCount() > 1 && |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 448 | kRect_ClipType == clip.getElementType(0) && |
| 449 | kIntersect_SetOp == clip.getOp(1)&& |
| 450 | clip.getRect(0).contains(bounds)) { |
| 451 | firstElement = 1; |
| 452 | } |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 453 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 454 | // walk through each clip element and perform its set op |
| 455 | // with the existing clip. |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 456 | for (int c = firstElement; c < count; ++c) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 457 | GrPathFill fill; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 458 | bool fillInverted; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 459 | // enabled at bottom of loop |
| 460 | this->disableState(kModifyStencilClip_StateBit); |
| 461 | |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 462 | bool canRenderDirectToStencil; // can the clip element be drawn |
| 463 | // directly to the stencil buffer |
| 464 | // with a non-inverted fill rule |
| 465 | // without extra passes to |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 466 | // resolve in/out status. |
| 467 | |
| 468 | GrPathRenderer* pr = NULL; |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 469 | const GrPath* clipPath = NULL; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 470 | GrPathRenderer::AutoClearPath arp; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 471 | if (kRect_ClipType == clip.getElementType(c)) { |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 472 | canRenderDirectToStencil = true; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 473 | fill = kEvenOdd_PathFill; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 474 | fillInverted = false; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 475 | } else { |
| 476 | fill = clip.getPathFill(c); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 477 | fillInverted = IsFillInverted(fill); |
| 478 | fill = NonInvertedFill(fill); |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 479 | clipPath = &clip.getPath(c); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 480 | pr = this->getClipPathRenderer(*clipPath, fill); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 481 | canRenderDirectToStencil = |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 482 | !pr->requiresStencilPass(this, *clipPath, fill); |
| 483 | arp.set(pr, this, clipPath, fill, NULL); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 484 | } |
| 485 | |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 486 | GrSetOp op = firstElement == c ? kReplace_SetOp : clip.getOp(c); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 487 | int passes; |
| 488 | GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses]; |
| 489 | |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 490 | bool canDrawDirectToClip; // Given the renderer, the element, |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 491 | // fill rule, and set operation can |
| 492 | // we render the element directly to |
| 493 | // stencil bit used for clipping. |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 494 | canDrawDirectToClip = |
| 495 | GrStencilSettings::GetClipPasses(op, |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 496 | canRenderDirectToStencil, |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 497 | clipBit, |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 498 | fillInverted, |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 499 | &passes, stencilSettings); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 500 | |
| 501 | // draw the element to the client stencil bits if necessary |
| 502 | if (!canDrawDirectToClip) { |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 503 | static const GrStencilSettings gDrawToStencil = { |
| 504 | kIncClamp_StencilOp, kIncClamp_StencilOp, |
| 505 | kIncClamp_StencilOp, kIncClamp_StencilOp, |
| 506 | kAlways_StencilFunc, kAlways_StencilFunc, |
| 507 | 0xffffffff, 0xffffffff, |
| 508 | 0x00000000, 0x00000000, |
| 509 | 0xffffffff, 0xffffffff, |
| 510 | }; |
| 511 | SET_RANDOM_COLOR |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 512 | if (kRect_ClipType == clip.getElementType(c)) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 513 | this->setStencil(gDrawToStencil); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 514 | this->drawSimpleRect(clip.getRect(c), NULL, 0); |
| 515 | } else { |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 516 | if (canRenderDirectToStencil) { |
| 517 | this->setStencil(gDrawToStencil); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 518 | pr->drawPath(0); |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 519 | } else { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 520 | pr->drawPathToStencil(); |
bsalomon@google.com | 7f5875d | 2011-03-24 16:55:45 +0000 | [diff] [blame] | 521 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
| 525 | // now we modify the clip bit by rendering either the clip |
| 526 | // element directly or a bounding rect of the entire clip. |
| 527 | this->enableState(kModifyStencilClip_StateBit); |
| 528 | for (int p = 0; p < passes; ++p) { |
| 529 | this->setStencil(stencilSettings[p]); |
| 530 | if (canDrawDirectToClip) { |
| 531 | if (kRect_ClipType == clip.getElementType(c)) { |
| 532 | SET_RANDOM_COLOR |
| 533 | this->drawSimpleRect(clip.getRect(c), NULL, 0); |
| 534 | } else { |
| 535 | SET_RANDOM_COLOR |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 536 | pr->drawPath(0); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 537 | } |
| 538 | } else { |
| 539 | SET_RANDOM_COLOR |
thakis@chromium.org | 441d7da | 2011-06-07 04:03:17 +0000 | [diff] [blame] | 540 | this->drawSimpleRect(bounds, NULL, 0); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 543 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 544 | fClip = clip; |
| 545 | // recusive draws would have disabled this. |
| 546 | fClipState.fClipInStencil = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 547 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 548 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 549 | fClipState.fClipIsDirty = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 550 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 551 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 552 | // Must flush the scissor after graphics state |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 553 | if (!this->flushGraphicsState(type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 554 | return false; |
| 555 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 556 | this->flushScissor(r); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 557 | return true; |
| 558 | } |
| 559 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 560 | GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path, |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 561 | GrPathFill fill) { |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 562 | if (NULL != fClientPathRenderer && |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 563 | fClientPathRenderer->canDrawPath(path, fill)) { |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 564 | return fClientPathRenderer; |
| 565 | } else { |
| 566 | if (NULL == fDefaultPathRenderer) { |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 567 | fDefaultPathRenderer = |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 568 | new GrDefaultPathRenderer(this->supportsTwoSidedStencil(), |
| 569 | this->supportsStencilWrapOps()); |
| 570 | } |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 571 | GrAssert(fDefaultPathRenderer->canDrawPath(path, fill)); |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 572 | return fDefaultPathRenderer; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 577 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 578 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 579 | void GrGpu::geometrySourceWillPush() { |
| 580 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 581 | if (kArray_GeometrySrcType == geoSrc.fVertexSrc || |
| 582 | kReserved_GeometrySrcType == geoSrc.fVertexSrc) { |
| 583 | this->finalizeReservedVertices(); |
| 584 | } |
| 585 | if (kArray_GeometrySrcType == geoSrc.fIndexSrc || |
| 586 | kReserved_GeometrySrcType == geoSrc.fIndexSrc) { |
| 587 | this->finalizeReservedIndices(); |
| 588 | } |
| 589 | GeometryPoolState& newState = fGeomPoolStateStack.push_back(); |
| 590 | #if GR_DEBUG |
| 591 | newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 592 | newState.fPoolStartVertex = DEBUG_INVAL_START_IDX; |
| 593 | newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
| 594 | newState.fPoolStartIndex = DEBUG_INVAL_START_IDX; |
| 595 | #endif |
| 596 | } |
| 597 | |
| 598 | void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) { |
| 599 | // if popping last entry then pops are unbalanced with pushes |
| 600 | GrAssert(fGeomPoolStateStack.count() > 1); |
| 601 | fGeomPoolStateStack.pop_back(); |
| 602 | } |
| 603 | |
| 604 | void GrGpu::onDrawIndexed(GrPrimitiveType type, |
| 605 | int startVertex, |
| 606 | int startIndex, |
| 607 | int vertexCount, |
| 608 | int indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 609 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 610 | this->handleDirtyContext(); |
| 611 | |
| 612 | if (!this->setupClipAndFlushState(type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 613 | return; |
| 614 | } |
| 615 | |
| 616 | #if GR_COLLECT_STATS |
| 617 | fStats.fVertexCnt += vertexCount; |
| 618 | fStats.fIndexCnt += indexCount; |
| 619 | fStats.fDrawCnt += 1; |
| 620 | #endif |
| 621 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 622 | int sVertex = startVertex; |
| 623 | int sIndex = startIndex; |
| 624 | setupGeometry(&sVertex, &sIndex, vertexCount, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 625 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 626 | this->onGpuDrawIndexed(type, sVertex, sIndex, |
| 627 | vertexCount, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 628 | } |
| 629 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 630 | void GrGpu::onDrawNonIndexed(GrPrimitiveType type, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 631 | int startVertex, |
| 632 | int vertexCount) { |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 633 | this->handleDirtyContext(); |
| 634 | |
| 635 | if (!this->setupClipAndFlushState(type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 636 | return; |
| 637 | } |
| 638 | #if GR_COLLECT_STATS |
| 639 | fStats.fVertexCnt += vertexCount; |
| 640 | fStats.fDrawCnt += 1; |
| 641 | #endif |
| 642 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 643 | int sVertex = startVertex; |
| 644 | setupGeometry(&sVertex, NULL, vertexCount, 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 645 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 646 | this->onGpuDrawNonIndexed(type, sVertex, vertexCount); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | void GrGpu::finalizeReservedVertices() { |
| 650 | GrAssert(NULL != fVertexPool); |
| 651 | fVertexPool->unlock(); |
| 652 | } |
| 653 | |
| 654 | void GrGpu::finalizeReservedIndices() { |
| 655 | GrAssert(NULL != fIndexPool); |
| 656 | fIndexPool->unlock(); |
| 657 | } |
| 658 | |
| 659 | void GrGpu::prepareVertexPool() { |
| 660 | if (NULL == fVertexPool) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 661 | GrAssert(0 == fVertexPoolUseCnt); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 662 | fVertexPool = new GrVertexBufferAllocPool(this, true, |
| 663 | VERTEX_POOL_VB_SIZE, |
bsalomon@google.com | 7a5af8b | 2011-02-18 18:40:42 +0000 | [diff] [blame] | 664 | VERTEX_POOL_VB_COUNT); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 665 | fVertexPool->releaseGpuRef(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 666 | } else if (!fVertexPoolUseCnt) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 667 | // the client doesn't have valid data in the pool |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 668 | fVertexPool->reset(); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | void GrGpu::prepareIndexPool() { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 673 | if (NULL == fIndexPool) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 674 | GrAssert(0 == fIndexPoolUseCnt); |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame^] | 675 | fIndexPool = new GrIndexBufferAllocPool(this, true, |
| 676 | INDEX_POOL_IB_SIZE, |
| 677 | INDEX_POOL_IB_COUNT); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 678 | fIndexPool->releaseGpuRef(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 679 | } else if (!fIndexPoolUseCnt) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 680 | // the client doesn't have valid data in the pool |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 681 | fIndexPool->reset(); |
| 682 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 683 | } |
| 684 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 685 | bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout, |
| 686 | int vertexCount, |
| 687 | void** vertices) { |
| 688 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
| 689 | |
| 690 | GrAssert(vertexCount > 0); |
| 691 | GrAssert(NULL != vertices); |
| 692 | |
| 693 | this->prepareVertexPool(); |
| 694 | |
| 695 | *vertices = fVertexPool->makeSpace(vertexLayout, |
| 696 | vertexCount, |
| 697 | &geomPoolState.fPoolVertexBuffer, |
| 698 | &geomPoolState.fPoolStartVertex); |
| 699 | if (NULL == *vertices) { |
| 700 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 701 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 702 | ++fVertexPoolUseCnt; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 703 | return true; |
| 704 | } |
| 705 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 706 | bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) { |
| 707 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
| 708 | |
| 709 | GrAssert(indexCount > 0); |
| 710 | GrAssert(NULL != indices); |
| 711 | |
| 712 | this->prepareIndexPool(); |
| 713 | |
| 714 | *indices = fIndexPool->makeSpace(indexCount, |
| 715 | &geomPoolState.fPoolIndexBuffer, |
| 716 | &geomPoolState.fPoolStartIndex); |
| 717 | if (NULL == *indices) { |
| 718 | return false; |
| 719 | } |
| 720 | ++fIndexPoolUseCnt; |
| 721 | return true; |
| 722 | } |
| 723 | |
| 724 | void GrGpu::releaseReservedVertexSpace() { |
| 725 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 726 | GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc); |
| 727 | size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout); |
| 728 | fVertexPool->putBack(bytes); |
| 729 | --fVertexPoolUseCnt; |
| 730 | } |
| 731 | |
| 732 | void GrGpu::releaseReservedIndexSpace() { |
| 733 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 734 | GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc); |
| 735 | size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t); |
| 736 | fIndexPool->putBack(bytes); |
| 737 | --fIndexPoolUseCnt; |
| 738 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 739 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 740 | void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) { |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 741 | this->prepareVertexPool(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 742 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 743 | #if GR_DEBUG |
| 744 | bool success = |
| 745 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 746 | fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 747 | vertexCount, |
| 748 | vertexArray, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 749 | &geomPoolState.fPoolVertexBuffer, |
| 750 | &geomPoolState.fPoolStartVertex); |
| 751 | ++fVertexPoolUseCnt; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 752 | GR_DEBUGASSERT(success); |
| 753 | } |
| 754 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 755 | void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) { |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 756 | this->prepareIndexPool(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 757 | GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 758 | #if GR_DEBUG |
| 759 | bool success = |
| 760 | #endif |
| 761 | fIndexPool->appendIndices(indexCount, |
| 762 | indexArray, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 763 | &geomPoolState.fPoolIndexBuffer, |
| 764 | &geomPoolState.fPoolStartIndex); |
| 765 | ++fIndexPoolUseCnt; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 766 | GR_DEBUGASSERT(success); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 767 | } |
| 768 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 769 | void GrGpu::releaseVertexArray() { |
| 770 | // if vertex source was array, we stowed data in the pool |
| 771 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 772 | GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc); |
| 773 | size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout); |
| 774 | fVertexPool->putBack(bytes); |
| 775 | --fVertexPoolUseCnt; |
| 776 | } |
| 777 | |
| 778 | void GrGpu::releaseIndexArray() { |
| 779 | // if index source was array, we stowed data in the pool |
| 780 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 781 | GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc); |
| 782 | size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t); |
| 783 | fIndexPool->putBack(bytes); |
| 784 | --fIndexPoolUseCnt; |
| 785 | } |
| 786 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 787 | //////////////////////////////////////////////////////////////////////////////// |
| 788 | |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 789 | const GrGpuStats& GrGpu::getStats() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 790 | return fStats; |
| 791 | } |
| 792 | |
| 793 | void GrGpu::resetStats() { |
| 794 | memset(&fStats, 0, sizeof(fStats)); |
| 795 | } |
| 796 | |
| 797 | void GrGpu::printStats() const { |
| 798 | if (GR_COLLECT_STATS) { |
| 799 | GrPrintf( |
| 800 | "-v-------------------------GPU STATS----------------------------v-\n" |
| 801 | "Stats collection is: %s\n" |
| 802 | "Draws: %04d, Verts: %04d, Indices: %04d\n" |
| 803 | "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n" |
| 804 | "TexCreates: %04d, RTCreates:%04d\n" |
| 805 | "-^--------------------------------------------------------------^-\n", |
| 806 | (GR_COLLECT_STATS ? "ON" : "OFF"), |
| 807 | fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt, |
| 808 | fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt, |
| 809 | fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 814 | const GrSamplerState GrSamplerState::gClampNoFilter( |
| 815 | GrSamplerState::kClamp_WrapMode, |
| 816 | GrSamplerState::kClamp_WrapMode, |
| 817 | GrSamplerState::kNormal_SampleMode, |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 818 | GrMatrix::I(), |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 819 | GrSamplerState::kNearest_Filter); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 820 | |
| 821 | |
| 822 | |
| 823 | |