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