bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1 | /* |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 2 | Copyright 2011 Google Inc. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 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 | |
| 17 | #include "GrContext.h" |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 18 | #include "GrGpu.h" |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 19 | #include "GrTextureCache.h" |
| 20 | #include "GrTextStrike.h" |
| 21 | #include "GrMemory.h" |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 22 | #include "GrClipIterator.h" |
| 23 | #include "GrIndexBuffer.h" |
| 24 | #include "GrInOrderDrawBuffer.h" |
| 25 | #include "GrBufferAllocPool.h" |
| 26 | #include "GrPathRenderer.h" |
| 27 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 28 | #define ENABLE_OFFSCREEN_AA 0 |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 29 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 30 | #define DEFER_TEXT_RENDERING 1 |
| 31 | |
| 32 | #define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB) |
| 33 | |
| 34 | static const size_t MAX_TEXTURE_CACHE_COUNT = 128; |
| 35 | static const size_t MAX_TEXTURE_CACHE_BYTES = 8 * 1024 * 1024; |
| 36 | |
| 37 | static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 18; |
| 38 | static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4; |
| 39 | |
| 40 | // We are currently only batching Text and drawRectToRect, both |
| 41 | // of which use the quad index buffer. |
| 42 | static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 0; |
| 43 | static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 0; |
| 44 | |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 45 | GrContext* GrContext::Create(GrEngine engine, |
| 46 | GrPlatform3DContext context3D) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 47 | GrContext* ctx = NULL; |
| 48 | GrGpu* fGpu = GrGpu::Create(engine, context3D); |
| 49 | if (NULL != fGpu) { |
| 50 | ctx = new GrContext(fGpu); |
| 51 | fGpu->unref(); |
| 52 | } |
| 53 | return ctx; |
| 54 | } |
| 55 | |
| 56 | GrContext* GrContext::CreateGLShaderContext() { |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 57 | return GrContext::Create(kOpenGL_Shaders_GrEngine, NULL); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | GrContext::~GrContext() { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 61 | this->flush(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 62 | delete fTextureCache; |
| 63 | delete fFontCache; |
| 64 | delete fDrawBuffer; |
| 65 | delete fDrawBufferVBAllocPool; |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 66 | delete fDrawBufferIBAllocPool; |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 67 | GrSafeUnref(fCustomPathRenderer); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 68 | GrSafeUnref(fAAFillRectIndexBuffer); |
| 69 | GrSafeUnref(fAAStrokeRectIndexBuffer); |
| 70 | fGpu->unref(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 71 | } |
| 72 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 73 | void GrContext::contextLost() { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 74 | // abandon first to so destructors |
| 75 | // don't try to free the resources in the API. |
| 76 | fGpu->abandonResources(); |
| 77 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 78 | delete fDrawBuffer; |
| 79 | fDrawBuffer = NULL; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 80 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 81 | delete fDrawBufferVBAllocPool; |
| 82 | fDrawBufferVBAllocPool = NULL; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 83 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 84 | delete fDrawBufferIBAllocPool; |
| 85 | fDrawBufferIBAllocPool = NULL; |
| 86 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 87 | GrSafeSetNull(fAAFillRectIndexBuffer); |
| 88 | GrSafeSetNull(fAAStrokeRectIndexBuffer); |
| 89 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 90 | fTextureCache->removeAll(); |
| 91 | fFontCache->freeAll(); |
| 92 | fGpu->markContextDirty(); |
| 93 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 94 | this->setupDrawBuffer(); |
| 95 | } |
| 96 | |
| 97 | void GrContext::resetContext() { |
| 98 | fGpu->markContextDirty(); |
| 99 | } |
| 100 | |
| 101 | void GrContext::freeGpuResources() { |
| 102 | this->flush(); |
| 103 | fTextureCache->removeAll(); |
| 104 | fFontCache->freeAll(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 105 | } |
| 106 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 107 | //////////////////////////////////////////////////////////////////////////////// |
| 108 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 109 | int GrContext::PaintStageVertexLayoutBits( |
| 110 | const GrPaint& paint, |
| 111 | const bool hasTexCoords[GrPaint::kTotalStages]) { |
| 112 | int stageMask = paint.getActiveStageMask(); |
| 113 | int layout = 0; |
| 114 | for (int i = 0; i < GrPaint::kTotalStages; ++i) { |
| 115 | if ((1 << i) & stageMask) { |
| 116 | if (NULL != hasTexCoords && hasTexCoords[i]) { |
| 117 | layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(i, i); |
| 118 | } else { |
| 119 | layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(i); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | return layout; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | //////////////////////////////////////////////////////////////////////////////// |
| 128 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 129 | enum { |
| 130 | kNPOTBit = 0x1, |
| 131 | kFilterBit = 0x2, |
| 132 | kKeylessBit = 0x4, |
| 133 | }; |
| 134 | |
| 135 | bool GrContext::finalizeTextureKey(GrTextureKey* key, |
| 136 | const GrSamplerState& sampler, |
| 137 | bool keyless) const { |
| 138 | uint32_t bits = 0; |
| 139 | uint16_t width = key->width(); |
| 140 | uint16_t height = key->height(); |
| 141 | |
| 142 | if (!fGpu->npotTextureTileSupport()) { |
| 143 | bool isPow2 = GrIsPow2(width) && GrIsPow2(height); |
| 144 | |
| 145 | bool tiled = (sampler.getWrapX() != GrSamplerState::kClamp_WrapMode) || |
| 146 | (sampler.getWrapY() != GrSamplerState::kClamp_WrapMode); |
| 147 | |
| 148 | if (tiled && !isPow2) { |
| 149 | bits |= kNPOTBit; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 150 | if (GrSamplerState::kNearest_Filter != sampler.getFilter()) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 151 | bits |= kFilterBit; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (keyless) { |
| 157 | bits |= kKeylessBit; |
| 158 | } |
| 159 | key->finalize(bits); |
| 160 | return 0 != bits; |
| 161 | } |
| 162 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 163 | GrTextureEntry* GrContext::findAndLockTexture(GrTextureKey* key, |
| 164 | const GrSamplerState& sampler) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 165 | finalizeTextureKey(key, sampler, false); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 166 | return fTextureCache->findAndLock(*key); |
| 167 | } |
| 168 | |
| 169 | static void stretchImage(void* dst, |
| 170 | int dstW, |
| 171 | int dstH, |
| 172 | void* src, |
| 173 | int srcW, |
| 174 | int srcH, |
| 175 | int bpp) { |
| 176 | GrFixed dx = (srcW << 16) / dstW; |
| 177 | GrFixed dy = (srcH << 16) / dstH; |
| 178 | |
| 179 | GrFixed y = dy >> 1; |
| 180 | |
| 181 | int dstXLimit = dstW*bpp; |
| 182 | for (int j = 0; j < dstH; ++j) { |
| 183 | GrFixed x = dx >> 1; |
| 184 | void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp; |
| 185 | void* dstRow = (uint8_t*)dst + j*dstW*bpp; |
| 186 | for (int i = 0; i < dstXLimit; i += bpp) { |
| 187 | memcpy((uint8_t*) dstRow + i, |
| 188 | (uint8_t*) srcRow + (x>>16)*bpp, |
| 189 | bpp); |
| 190 | x += dx; |
| 191 | } |
| 192 | y += dy; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | GrTextureEntry* GrContext::createAndLockTexture(GrTextureKey* key, |
| 197 | const GrSamplerState& sampler, |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 198 | const GrTextureDesc& desc, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 199 | void* srcData, size_t rowBytes) { |
| 200 | GrAssert(key->width() == desc.fWidth); |
| 201 | GrAssert(key->height() == desc.fHeight); |
| 202 | |
| 203 | #if GR_DUMP_TEXTURE_UPLOAD |
| 204 | GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight); |
| 205 | #endif |
| 206 | |
| 207 | GrTextureEntry* entry = NULL; |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 208 | bool special = finalizeTextureKey(key, sampler, false); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 209 | if (special) { |
| 210 | GrTextureEntry* clampEntry; |
| 211 | GrTextureKey clampKey(*key); |
| 212 | clampEntry = findAndLockTexture(&clampKey, GrSamplerState::ClampNoFilter()); |
| 213 | |
| 214 | if (NULL == clampEntry) { |
| 215 | clampEntry = createAndLockTexture(&clampKey, |
| 216 | GrSamplerState::ClampNoFilter(), |
| 217 | desc, srcData, rowBytes); |
| 218 | GrAssert(NULL != clampEntry); |
| 219 | if (NULL == clampEntry) { |
| 220 | return NULL; |
| 221 | } |
| 222 | } |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 223 | GrTextureDesc rtDesc = desc; |
| 224 | rtDesc.fFlags = rtDesc.fFlags | |
| 225 | kRenderTarget_GrTextureFlagBit | |
| 226 | kNoStencil_GrTextureFlagBit; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 227 | rtDesc.fWidth = GrNextPow2(GrMax<int>(desc.fWidth, |
| 228 | fGpu->minRenderTargetWidth())); |
| 229 | rtDesc.fHeight = GrNextPow2(GrMax<int>(desc.fHeight, |
| 230 | fGpu->minRenderTargetHeight())); |
| 231 | |
| 232 | GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0); |
| 233 | |
| 234 | if (NULL != texture) { |
| 235 | GrDrawTarget::AutoStateRestore asr(fGpu); |
| 236 | fGpu->setRenderTarget(texture->asRenderTarget()); |
| 237 | fGpu->setTexture(0, clampEntry->texture()); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 238 | fGpu->disableStencil(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 239 | fGpu->setViewMatrix(GrMatrix::I()); |
| 240 | fGpu->setAlpha(0xff); |
| 241 | fGpu->setBlendFunc(kOne_BlendCoeff, kZero_BlendCoeff); |
| 242 | fGpu->disableState(GrDrawTarget::kDither_StateBit | |
| 243 | GrDrawTarget::kClip_StateBit | |
| 244 | GrDrawTarget::kAntialias_StateBit); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 245 | GrSamplerState::Filter filter; |
| 246 | // if filtering is not desired then we want to ensure all |
| 247 | // texels in the resampled image are copies of texels from |
| 248 | // the original. |
| 249 | if (GrSamplerState::kNearest_Filter == sampler.getFilter()) { |
| 250 | filter = GrSamplerState::kNearest_Filter; |
| 251 | } else { |
| 252 | filter = GrSamplerState::kBilinear_Filter; |
| 253 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 254 | GrSamplerState stretchSampler(GrSamplerState::kClamp_WrapMode, |
| 255 | GrSamplerState::kClamp_WrapMode, |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 256 | filter); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 257 | fGpu->setSamplerState(0, stretchSampler); |
| 258 | |
| 259 | static const GrVertexLayout layout = |
| 260 | GrDrawTarget::StageTexCoordVertexLayoutBit(0,0); |
| 261 | GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0); |
| 262 | |
| 263 | if (arg.succeeded()) { |
| 264 | GrPoint* verts = (GrPoint*) arg.vertices(); |
| 265 | verts[0].setIRectFan(0, 0, |
| 266 | texture->width(), |
| 267 | texture->height(), |
| 268 | 2*sizeof(GrPoint)); |
| 269 | verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint)); |
| 270 | fGpu->drawNonIndexed(kTriangleFan_PrimitiveType, |
| 271 | 0, 4); |
| 272 | entry = fTextureCache->createAndLock(*key, texture); |
| 273 | } |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 274 | texture->releaseRenderTarget(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 275 | } else { |
| 276 | // TODO: Our CPU stretch doesn't filter. But we create separate |
| 277 | // stretched textures when the sampler state is either filtered or |
| 278 | // not. Either implement filtered stretch blit on CPU or just create |
| 279 | // one when FBO case fails. |
| 280 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 281 | rtDesc.fFlags = kNone_GrTextureFlags; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 282 | // no longer need to clamp at min RT size. |
| 283 | rtDesc.fWidth = GrNextPow2(desc.fWidth); |
| 284 | rtDesc.fHeight = GrNextPow2(desc.fHeight); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 285 | int bpp = GrBytesPerPixel(desc.fFormat); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 286 | GrAutoSMalloc<128*128*4> stretchedPixels(bpp * |
| 287 | rtDesc.fWidth * |
| 288 | rtDesc.fHeight); |
| 289 | stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight, |
| 290 | srcData, desc.fWidth, desc.fHeight, bpp); |
| 291 | |
| 292 | size_t stretchedRowBytes = rtDesc.fWidth * bpp; |
| 293 | |
| 294 | GrTexture* texture = fGpu->createTexture(rtDesc, |
| 295 | stretchedPixels.get(), |
| 296 | stretchedRowBytes); |
| 297 | GrAssert(NULL != texture); |
| 298 | entry = fTextureCache->createAndLock(*key, texture); |
| 299 | } |
| 300 | fTextureCache->unlock(clampEntry); |
| 301 | |
| 302 | } else { |
| 303 | GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes); |
| 304 | if (NULL != texture) { |
| 305 | entry = fTextureCache->createAndLock(*key, texture); |
| 306 | } else { |
| 307 | entry = NULL; |
| 308 | } |
| 309 | } |
| 310 | return entry; |
| 311 | } |
| 312 | |
bsalomon@google.com | a39f404 | 2011-04-26 13:18:16 +0000 | [diff] [blame] | 313 | GrTextureEntry* GrContext::lockKeylessTexture(const GrTextureDesc& desc) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 314 | uint32_t p0 = desc.fFormat; |
| 315 | uint32_t p1 = (desc.fAALevel << 16) | desc.fFlags; |
| 316 | GrTextureKey key(p0, p1, desc.fWidth, desc.fHeight); |
bsalomon@google.com | a39f404 | 2011-04-26 13:18:16 +0000 | [diff] [blame] | 317 | this->finalizeTextureKey(&key, GrSamplerState::ClampNoFilter(), true); |
| 318 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 319 | GrTextureEntry* entry = fTextureCache->findAndLock(key); |
| 320 | if (NULL == entry) { |
| 321 | GrTexture* texture = fGpu->createTexture(desc, NULL, 0); |
| 322 | if (NULL != texture) { |
| 323 | entry = fTextureCache->createAndLock(key, texture); |
| 324 | } |
| 325 | } |
| 326 | // If the caller gives us the same desc/sampler twice we don't want |
| 327 | // to return the same texture the second time (unless it was previously |
| 328 | // released). So we detach the entry from the cache and reattach at release. |
| 329 | if (NULL != entry) { |
| 330 | fTextureCache->detach(entry); |
| 331 | } |
| 332 | return entry; |
| 333 | } |
| 334 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 335 | void GrContext::unlockTexture(GrTextureEntry* entry) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 336 | if (kKeylessBit & entry->key().getPrivateBits()) { |
| 337 | fTextureCache->reattachAndUnlock(entry); |
| 338 | } else { |
| 339 | fTextureCache->unlock(entry); |
| 340 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 341 | } |
| 342 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 343 | GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& desc, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 344 | void* srcData, |
| 345 | size_t rowBytes) { |
| 346 | return fGpu->createTexture(desc, srcData, rowBytes); |
| 347 | } |
| 348 | |
| 349 | void GrContext::getTextureCacheLimits(int* maxTextures, |
| 350 | size_t* maxTextureBytes) const { |
| 351 | fTextureCache->getLimits(maxTextures, maxTextureBytes); |
| 352 | } |
| 353 | |
| 354 | void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) { |
| 355 | fTextureCache->setLimits(maxTextures, maxTextureBytes); |
| 356 | } |
| 357 | |
| 358 | int GrContext::getMaxTextureDimension() { |
| 359 | return fGpu->maxTextureDimension(); |
| 360 | } |
| 361 | |
| 362 | /////////////////////////////////////////////////////////////////////////////// |
| 363 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 364 | GrResource* GrContext::createPlatformSurface(const GrPlatformSurfaceDesc& desc) { |
| 365 | // validate flags here so that GrGpu subclasses don't have to check |
| 366 | if (kTexture_GrPlatformSurfaceType == desc.fSurfaceType && |
| 367 | 0 != desc.fRenderTargetFlags) { |
| 368 | return NULL; |
| 369 | } |
| 370 | if (!(kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) && |
| 371 | (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags)) { |
| 372 | return NULL; |
| 373 | } |
| 374 | if (kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType && |
| 375 | (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) && |
| 376 | !(kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags)) { |
| 377 | return NULL; |
| 378 | } |
| 379 | return fGpu->createPlatformSurface(desc); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 380 | } |
| 381 | |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 382 | GrRenderTarget* GrContext::createPlatformRenderTarget(intptr_t platformRenderTarget, |
| 383 | int stencilBits, |
| 384 | bool isMultisampled, |
| 385 | int width, int height) { |
| 386 | #if GR_DEBUG |
| 387 | GrPrintf("Using deprecated createPlatformRenderTarget API."); |
| 388 | #endif |
| 389 | return fGpu->createPlatformRenderTarget(platformRenderTarget, |
| 390 | stencilBits, isMultisampled, |
| 391 | width, height); |
| 392 | } |
| 393 | |
| 394 | GrRenderTarget* GrContext::createRenderTargetFrom3DApiState() { |
| 395 | #if GR_DEBUG |
| 396 | GrPrintf("Using deprecated createRenderTargetFrom3DApiState API."); |
| 397 | #endif |
| 398 | return fGpu->createRenderTargetFrom3DApiState(); |
| 399 | } |
| 400 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 401 | /////////////////////////////////////////////////////////////////////////////// |
| 402 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 403 | bool GrContext::supportsIndex8PixelConfig(const GrSamplerState& sampler, |
| 404 | int width, int height) { |
| 405 | if (!fGpu->supports8BitPalette()) { |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | bool isPow2 = GrIsPow2(width) && GrIsPow2(height); |
| 411 | |
| 412 | if (!isPow2) { |
| 413 | if (!fGpu->npotTextureSupport()) { |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | bool tiled = sampler.getWrapX() != GrSamplerState::kClamp_WrapMode || |
| 418 | sampler.getWrapY() != GrSamplerState::kClamp_WrapMode; |
| 419 | if (tiled && !fGpu->npotTextureTileSupport()) { |
| 420 | return false; |
| 421 | } |
| 422 | } |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | //////////////////////////////////////////////////////////////////////////////// |
| 427 | |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 428 | const GrClip& GrContext::getClip() const { return fGpu->getClip(); } |
| 429 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 430 | void GrContext::setClip(const GrClip& clip) { |
| 431 | fGpu->setClip(clip); |
| 432 | fGpu->enableState(GrDrawTarget::kClip_StateBit); |
| 433 | } |
| 434 | |
| 435 | void GrContext::setClip(const GrIRect& rect) { |
| 436 | GrClip clip; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 437 | clip.setFromIRect(rect); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 438 | fGpu->setClip(clip); |
| 439 | } |
| 440 | |
| 441 | //////////////////////////////////////////////////////////////////////////////// |
| 442 | |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 443 | void GrContext::clear(const GrIRect* rect, const GrColor color) { |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 444 | this->flush(); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 445 | fGpu->clear(rect, color); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | void GrContext::drawPaint(const GrPaint& paint) { |
| 449 | // set rect to be big enough to fill the space, but not super-huge, so we |
| 450 | // don't overflow fixed-point implementations |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 451 | GrRect r; |
| 452 | r.setLTRB(0, 0, |
| 453 | GrIntToScalar(getRenderTarget()->width()), |
| 454 | GrIntToScalar(getRenderTarget()->height())); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 455 | GrMatrix inverse; |
| 456 | if (fGpu->getViewInverse(&inverse)) { |
| 457 | inverse.mapRect(&r); |
| 458 | } else { |
| 459 | GrPrintf("---- fGpu->getViewInverse failed\n"); |
| 460 | } |
| 461 | this->drawRect(paint, r); |
| 462 | } |
| 463 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 464 | //////////////////////////////////////////////////////////////////////////////// |
| 465 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 466 | bool GrContext::doOffscreenAA(GrDrawTarget* target, |
| 467 | const GrPaint& paint, |
| 468 | bool isLines) const { |
| 469 | #if !ENABLE_OFFSCREEN_AA |
| 470 | return false; |
| 471 | #else |
| 472 | if (!paint.fAntiAlias) { |
| 473 | return false; |
| 474 | } |
| 475 | if (isLines && fGpu->supportsAALines()) { |
| 476 | return false; |
| 477 | } |
| 478 | if (target->getRenderTarget()->isMultisampled()) { |
| 479 | return false; |
| 480 | } |
| 481 | // we have to be sure that the blend equation is expressible |
| 482 | // as simple src / dst coeffecients when the source |
| 483 | // is already modulated by the coverage fraction. |
| 484 | // We could use dual-source blending to get the correct per-pixel |
| 485 | // dst coeffecient for the remaining cases. |
| 486 | if (kISC_BlendCoeff != paint.fDstBlendCoeff && |
| 487 | kOne_BlendCoeff != paint.fDstBlendCoeff && |
| 488 | kISA_BlendCoeff != paint.fDstBlendCoeff) { |
| 489 | return false; |
| 490 | } |
| 491 | return true; |
| 492 | #endif |
| 493 | } |
| 494 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 495 | bool GrContext::setupOffscreenAAPass1(GrDrawTarget* target, |
| 496 | bool requireStencil, |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 497 | const GrIRect& boundRect, |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 498 | OffscreenRecord* record) { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 499 | GrAssert(ENABLE_OFFSCREEN_AA); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 500 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 501 | GrAssert(NULL == record->fEntry0); |
| 502 | GrAssert(NULL == record->fEntry1); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 503 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 504 | int boundW = boundRect.width(); |
| 505 | int boundH = boundRect.height(); |
| 506 | int size = GrMax(64, (int)GrNextPow2(GrMax(boundW, boundH))); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 507 | |
| 508 | GrTextureDesc desc; |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 509 | if (requireStencil) { |
| 510 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 511 | } else { |
| 512 | desc.fFlags = kRenderTarget_GrTextureFlagBit | |
| 513 | kNoStencil_GrTextureFlagBit; |
| 514 | } |
| 515 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 516 | desc.fFormat = kRGBA_8888_GrPixelConfig; |
| 517 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 518 | int scale; |
| 519 | // Using MSAA seems to be slower for some yet unknown reason. |
| 520 | if (false && fGpu->supportsFullsceneAA()) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 521 | record->fDownsample = OffscreenRecord::kFSAA_Downsample; |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 522 | scale = GR_Scalar1; |
| 523 | desc.fAALevel = kMed_GrAALevel; |
| 524 | } else { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 525 | record->fDownsample = (fGpu->supports4x4DownsampleFilter()) ? |
| 526 | OffscreenRecord::k4x4SinglePass_Downsample : |
| 527 | OffscreenRecord::k4x4TwoPass_Downsample; |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 528 | scale = 4; |
| 529 | desc.fAALevel = kNone_GrAALevel; |
| 530 | } |
| 531 | |
| 532 | desc.fWidth = scale * size; |
| 533 | desc.fHeight = scale * size; |
| 534 | |
| 535 | record->fEntry0 = this->lockKeylessTexture(desc); |
| 536 | |
| 537 | if (NULL == record->fEntry0) { |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 538 | return false; |
| 539 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 540 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 541 | if (OffscreenRecord::k4x4TwoPass_Downsample == record->fDownsample) { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 542 | desc.fWidth /= 2; |
| 543 | desc.fHeight /= 2; |
| 544 | record->fEntry1 = this->lockKeylessTexture(desc); |
| 545 | if (NULL == record->fEntry1) { |
| 546 | this->unlockTexture(record->fEntry0); |
| 547 | record->fEntry0 = NULL; |
| 548 | return false; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | GrRenderTarget* offRT0 = record->fEntry0->texture()->asRenderTarget(); |
| 553 | GrAssert(NULL != offRT0); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 554 | |
| 555 | target->saveCurrentDrawState(&record->fSavedState); |
| 556 | |
| 557 | GrPaint tempPaint; |
| 558 | tempPaint.reset(); |
| 559 | SetPaint(tempPaint, target); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 560 | target->setRenderTarget(offRT0); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 561 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 562 | GrMatrix transM; |
| 563 | transM.setTranslate(-boundRect.fLeft, -boundRect.fTop); |
| 564 | target->postConcatViewMatrix(transM); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 565 | GrMatrix scaleM; |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 566 | scaleM.setScale(scale * GR_Scalar1, scale * GR_Scalar1); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 567 | target->postConcatViewMatrix(scaleM); |
| 568 | |
| 569 | // clip gets applied in second pass |
| 570 | target->disableState(GrDrawTarget::kClip_StateBit); |
| 571 | |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 572 | GrIRect clear = SkIRect::MakeWH(scale * boundW, scale * boundH); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 573 | target->clear(&clear, 0x0); |
| 574 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 575 | return true; |
| 576 | } |
| 577 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 578 | void GrContext::offscreenAAPass2(GrDrawTarget* target, |
| 579 | const GrPaint& paint, |
| 580 | const GrIRect& boundRect, |
| 581 | OffscreenRecord* record) { |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 582 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 583 | GrAssert(NULL != record->fEntry0); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 584 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 585 | GrSamplerState::Filter filter; |
| 586 | if (OffscreenRecord::k4x4SinglePass_Downsample == record->fDownsample) { |
| 587 | filter = GrSamplerState::k4x4Downsample_Filter; |
| 588 | } else { |
| 589 | filter = GrSamplerState::kBilinear_Filter; |
| 590 | } |
| 591 | |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 592 | GrMatrix sampleM; |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 593 | GrSamplerState sampler(GrSamplerState::kClamp_WrapMode, |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 594 | GrSamplerState::kClamp_WrapMode, filter); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 595 | |
| 596 | GrTexture* src = record->fEntry0->texture(); |
| 597 | int scale; |
| 598 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 599 | enum { |
| 600 | kOffscreenStage = GrPaint::kTotalStages, |
| 601 | }; |
| 602 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 603 | if (OffscreenRecord::k4x4TwoPass_Downsample == record->fDownsample) { |
| 604 | GrAssert(NULL != record->fEntry1); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 605 | scale = 2; |
| 606 | GrRenderTarget* dst = record->fEntry1->texture()->asRenderTarget(); |
| 607 | |
| 608 | // Do 2x2 downsample from first to second |
| 609 | target->setTexture(kOffscreenStage, src); |
| 610 | target->setRenderTarget(dst); |
| 611 | target->setViewMatrix(GrMatrix::I()); |
| 612 | sampleM.setScale(scale * GR_Scalar1 / src->width(), |
| 613 | scale * GR_Scalar1 / src->height()); |
| 614 | sampler.setMatrix(sampleM); |
| 615 | target->setSamplerState(kOffscreenStage, sampler); |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 616 | GrRect rect = SkRect::MakeWH(scale * boundRect.width(), |
| 617 | scale * boundRect.height()); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 618 | target->drawSimpleRect(rect, NULL, 1 << kOffscreenStage); |
| 619 | |
| 620 | src = record->fEntry1->texture(); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 621 | } else if (OffscreenRecord::kFSAA_Downsample == record->fDownsample) { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 622 | scale = 1; |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 623 | GrIRect rect = SkIRect::MakeWH(boundRect.width(), boundRect.height()); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 624 | src->asRenderTarget()->overrideResolveRect(rect); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 625 | } else { |
| 626 | GrAssert(OffscreenRecord::k4x4SinglePass_Downsample == |
| 627 | record->fDownsample); |
| 628 | scale = 4; |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | // setup for draw back to main RT |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 632 | int stageMask = paint.getActiveStageMask(); |
| 633 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 634 | target->restoreDrawState(record->fSavedState); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 635 | |
| 636 | if (stageMask) { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 637 | GrMatrix invVM; |
| 638 | if (target->getViewInverse(&invVM)) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 639 | target->preConcatSamplerMatrices(stageMask, invVM); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | target->setViewMatrix(GrMatrix::I()); |
| 643 | |
| 644 | target->setTexture(kOffscreenStage, src); |
| 645 | sampleM.setScale(scale * GR_Scalar1 / src->width(), |
| 646 | scale * GR_Scalar1 / src->height()); |
| 647 | sampler.setMatrix(sampleM); |
| 648 | sampleM.setTranslate(-boundRect.fLeft, -boundRect.fTop); |
| 649 | sampler.preConcatMatrix(sampleM); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 650 | target->setSamplerState(kOffscreenStage, sampler); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 651 | |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 652 | GrRect dstRect; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 653 | int stages = (1 << kOffscreenStage) | stageMask; |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 654 | dstRect.set(boundRect); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 655 | target->drawSimpleRect(dstRect, NULL, stages); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 656 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 657 | this->unlockTexture(record->fEntry0); |
| 658 | record->fEntry0 = NULL; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 659 | if (NULL != record->fEntry1) { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 660 | this->unlockTexture(record->fEntry1); |
| 661 | record->fEntry1 = NULL; |
| 662 | } |
| 663 | target->restoreDrawState(record->fSavedState); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | //////////////////////////////////////////////////////////////////////////////// |
| 667 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 668 | /* create a triangle strip that strokes the specified triangle. There are 8 |
| 669 | unique vertices, but we repreat the last 2 to close up. Alternatively we |
| 670 | could use an indices array, and then only send 8 verts, but not sure that |
| 671 | would be faster. |
| 672 | */ |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 673 | static void setStrokeRectStrip(GrPoint verts[10], GrRect rect, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 674 | GrScalar width) { |
| 675 | const GrScalar rad = GrScalarHalf(width); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 676 | rect.sort(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 677 | |
| 678 | verts[0].set(rect.fLeft + rad, rect.fTop + rad); |
| 679 | verts[1].set(rect.fLeft - rad, rect.fTop - rad); |
| 680 | verts[2].set(rect.fRight - rad, rect.fTop + rad); |
| 681 | verts[3].set(rect.fRight + rad, rect.fTop - rad); |
| 682 | verts[4].set(rect.fRight - rad, rect.fBottom - rad); |
| 683 | verts[5].set(rect.fRight + rad, rect.fBottom + rad); |
| 684 | verts[6].set(rect.fLeft + rad, rect.fBottom - rad); |
| 685 | verts[7].set(rect.fLeft - rad, rect.fBottom + rad); |
| 686 | verts[8] = verts[0]; |
| 687 | verts[9] = verts[1]; |
| 688 | } |
| 689 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 690 | static GrColor getColorForMesh(const GrPaint& paint) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 691 | // FIXME: This was copied from SkGpuDevice, seems like |
| 692 | // we should have already smeared a in caller if that |
| 693 | // is what is desired. |
| 694 | if (paint.hasTexture()) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 695 | unsigned a = GrColorUnpackA(paint.fColor); |
| 696 | return GrColorPackRGBA(a, a, a, a); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 697 | } else { |
| 698 | return paint.fColor; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
| 702 | static void setInsetFan(GrPoint* pts, size_t stride, |
| 703 | const GrRect& r, GrScalar dx, GrScalar dy) { |
| 704 | pts->setRectFan(r.fLeft + dx, r.fTop + dy, r.fRight - dx, r.fBottom - dy, stride); |
| 705 | } |
| 706 | |
| 707 | static const uint16_t gFillAARectIdx[] = { |
| 708 | 0, 1, 5, 5, 4, 0, |
| 709 | 1, 2, 6, 6, 5, 1, |
| 710 | 2, 3, 7, 7, 6, 2, |
| 711 | 3, 0, 4, 4, 7, 3, |
| 712 | 4, 5, 6, 6, 7, 4, |
| 713 | }; |
| 714 | |
| 715 | int GrContext::aaFillRectIndexCount() const { |
| 716 | return GR_ARRAY_COUNT(gFillAARectIdx); |
| 717 | } |
| 718 | |
| 719 | GrIndexBuffer* GrContext::aaFillRectIndexBuffer() { |
| 720 | if (NULL == fAAFillRectIndexBuffer) { |
| 721 | fAAFillRectIndexBuffer = fGpu->createIndexBuffer(sizeof(gFillAARectIdx), |
| 722 | false); |
| 723 | GrAssert(NULL != fAAFillRectIndexBuffer); |
| 724 | #if GR_DEBUG |
| 725 | bool updated = |
| 726 | #endif |
| 727 | fAAFillRectIndexBuffer->updateData(gFillAARectIdx, |
| 728 | sizeof(gFillAARectIdx)); |
| 729 | GR_DEBUGASSERT(updated); |
| 730 | } |
| 731 | return fAAFillRectIndexBuffer; |
| 732 | } |
| 733 | |
| 734 | static const uint16_t gStrokeAARectIdx[] = { |
| 735 | 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, |
| 736 | 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, |
| 737 | 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, |
| 738 | 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, |
| 739 | |
| 740 | 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, |
| 741 | 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, |
| 742 | 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, |
| 743 | 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, |
| 744 | |
| 745 | 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8, |
| 746 | 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8, |
| 747 | 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8, |
| 748 | 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8, |
| 749 | }; |
| 750 | |
| 751 | int GrContext::aaStrokeRectIndexCount() const { |
| 752 | return GR_ARRAY_COUNT(gStrokeAARectIdx); |
| 753 | } |
| 754 | |
| 755 | GrIndexBuffer* GrContext::aaStrokeRectIndexBuffer() { |
| 756 | if (NULL == fAAStrokeRectIndexBuffer) { |
| 757 | fAAStrokeRectIndexBuffer = fGpu->createIndexBuffer(sizeof(gStrokeAARectIdx), |
| 758 | false); |
| 759 | GrAssert(NULL != fAAStrokeRectIndexBuffer); |
| 760 | #if GR_DEBUG |
| 761 | bool updated = |
| 762 | #endif |
| 763 | fAAStrokeRectIndexBuffer->updateData(gStrokeAARectIdx, |
| 764 | sizeof(gStrokeAARectIdx)); |
| 765 | GR_DEBUGASSERT(updated); |
| 766 | } |
| 767 | return fAAStrokeRectIndexBuffer; |
| 768 | } |
| 769 | |
| 770 | void GrContext::fillAARect(GrDrawTarget* target, |
| 771 | const GrPaint& paint, |
| 772 | const GrRect& devRect) { |
| 773 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 774 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL) | |
| 775 | GrDrawTarget::kColor_VertexLayoutBit; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 776 | |
| 777 | size_t vsize = GrDrawTarget::VertexSize(layout); |
| 778 | |
| 779 | GrDrawTarget::AutoReleaseGeometry geo(target, layout, 8, 0); |
| 780 | |
| 781 | intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices()); |
| 782 | |
| 783 | GrPoint* fan0Pos = reinterpret_cast<GrPoint*>(verts); |
| 784 | GrPoint* fan1Pos = reinterpret_cast<GrPoint*>(verts + 4 * vsize); |
| 785 | |
| 786 | setInsetFan(fan0Pos, vsize, devRect, -GR_ScalarHalf, -GR_ScalarHalf); |
| 787 | setInsetFan(fan1Pos, vsize, devRect, GR_ScalarHalf, GR_ScalarHalf); |
| 788 | |
| 789 | verts += sizeof(GrPoint); |
| 790 | for (int i = 0; i < 4; ++i) { |
| 791 | *reinterpret_cast<GrColor*>(verts + i * vsize) = 0; |
| 792 | } |
| 793 | |
| 794 | GrColor innerColor = getColorForMesh(paint); |
| 795 | verts += 4 * vsize; |
| 796 | for (int i = 0; i < 4; ++i) { |
| 797 | *reinterpret_cast<GrColor*>(verts + i * vsize) = innerColor; |
| 798 | } |
| 799 | |
| 800 | target->setIndexSourceToBuffer(this->aaFillRectIndexBuffer()); |
| 801 | |
| 802 | target->drawIndexed(kTriangles_PrimitiveType, 0, |
| 803 | 0, 8, this->aaFillRectIndexCount()); |
| 804 | } |
| 805 | |
| 806 | void GrContext::strokeAARect(GrDrawTarget* target, const GrPaint& paint, |
| 807 | const GrRect& devRect, const GrVec& devStrokeSize) { |
| 808 | const GrScalar& dx = devStrokeSize.fX; |
| 809 | const GrScalar& dy = devStrokeSize.fY; |
| 810 | const GrScalar rx = GrMul(dx, GR_ScalarHalf); |
| 811 | const GrScalar ry = GrMul(dy, GR_ScalarHalf); |
| 812 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 813 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL) | |
| 814 | GrDrawTarget::kColor_VertexLayoutBit; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 815 | |
| 816 | GrScalar spare; |
| 817 | { |
| 818 | GrScalar w = devRect.width() - dx; |
| 819 | GrScalar h = devRect.height() - dy; |
| 820 | spare = GrMin(w, h); |
| 821 | } |
| 822 | |
| 823 | if (spare <= 0) { |
| 824 | GrRect r(devRect); |
| 825 | r.inset(-rx, -ry); |
| 826 | fillAARect(target, paint, r); |
| 827 | return; |
| 828 | } |
| 829 | |
| 830 | size_t vsize = GrDrawTarget::VertexSize(layout); |
| 831 | |
| 832 | GrDrawTarget::AutoReleaseGeometry geo(target, layout, 16, 0); |
| 833 | |
| 834 | intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices()); |
| 835 | |
| 836 | GrPoint* fan0Pos = reinterpret_cast<GrPoint*>(verts); |
| 837 | GrPoint* fan1Pos = reinterpret_cast<GrPoint*>(verts + 4 * vsize); |
| 838 | GrPoint* fan2Pos = reinterpret_cast<GrPoint*>(verts + 8 * vsize); |
| 839 | GrPoint* fan3Pos = reinterpret_cast<GrPoint*>(verts + 12 * vsize); |
| 840 | |
| 841 | setInsetFan(fan0Pos, vsize, devRect, -rx - GR_ScalarHalf, -ry - GR_ScalarHalf); |
| 842 | setInsetFan(fan1Pos, vsize, devRect, -rx + GR_ScalarHalf, -ry + GR_ScalarHalf); |
| 843 | setInsetFan(fan2Pos, vsize, devRect, rx - GR_ScalarHalf, ry - GR_ScalarHalf); |
| 844 | setInsetFan(fan3Pos, vsize, devRect, rx + GR_ScalarHalf, ry + GR_ScalarHalf); |
| 845 | |
| 846 | verts += sizeof(GrPoint); |
| 847 | for (int i = 0; i < 4; ++i) { |
| 848 | *reinterpret_cast<GrColor*>(verts + i * vsize) = 0; |
| 849 | } |
| 850 | |
| 851 | GrColor innerColor = getColorForMesh(paint); |
| 852 | verts += 4 * vsize; |
| 853 | for (int i = 0; i < 8; ++i) { |
| 854 | *reinterpret_cast<GrColor*>(verts + i * vsize) = innerColor; |
| 855 | } |
| 856 | |
| 857 | verts += 8 * vsize; |
| 858 | for (int i = 0; i < 8; ++i) { |
| 859 | *reinterpret_cast<GrColor*>(verts + i * vsize) = 0; |
| 860 | } |
| 861 | |
| 862 | target->setIndexSourceToBuffer(aaStrokeRectIndexBuffer()); |
| 863 | target->drawIndexed(kTriangles_PrimitiveType, |
| 864 | 0, 0, 16, aaStrokeRectIndexCount()); |
| 865 | } |
| 866 | |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 867 | /** |
| 868 | * Returns true if the rects edges are integer-aligned. |
| 869 | */ |
| 870 | static bool isIRect(const GrRect& r) { |
| 871 | return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) && |
| 872 | GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom); |
| 873 | } |
| 874 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 875 | static bool apply_aa_to_rect(GrDrawTarget* target, |
| 876 | GrGpu* gpu, |
| 877 | const GrPaint& paint, |
| 878 | const GrRect& rect, |
| 879 | GrScalar width, |
| 880 | const GrMatrix* matrix, |
| 881 | GrMatrix* combinedMatrix, |
| 882 | GrRect* devRect) { |
| 883 | // we use a simple alpha ramp to do aa on axis-aligned rects |
| 884 | // do AA with alpha ramp if the caller requested AA, the rect |
| 885 | // will be axis-aligned,the render target is not |
| 886 | // multisampled, and the rect won't land on integer coords. |
| 887 | |
| 888 | if (!paint.fAntiAlias) { |
| 889 | return false; |
| 890 | } |
| 891 | |
| 892 | if (target->getRenderTarget()->isMultisampled()) { |
| 893 | return false; |
| 894 | } |
| 895 | |
| 896 | if (0 == width && gpu->supportsAALines()) { |
| 897 | return false; |
| 898 | } |
| 899 | |
| 900 | if (!target->getViewMatrix().preservesAxisAlignment()) { |
| 901 | return false; |
| 902 | } |
| 903 | |
| 904 | if (NULL != matrix && |
| 905 | !matrix->preservesAxisAlignment()) { |
| 906 | return false; |
| 907 | } |
| 908 | |
| 909 | *combinedMatrix = target->getViewMatrix(); |
| 910 | if (NULL != matrix) { |
| 911 | combinedMatrix->preConcat(*matrix); |
| 912 | GrAssert(combinedMatrix->preservesAxisAlignment()); |
| 913 | } |
| 914 | |
| 915 | combinedMatrix->mapRect(devRect, rect); |
| 916 | devRect->sort(); |
| 917 | |
| 918 | if (width < 0) { |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 919 | return !isIRect(*devRect); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 920 | } else { |
| 921 | return true; |
| 922 | } |
| 923 | } |
| 924 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 925 | void GrContext::drawRect(const GrPaint& paint, |
| 926 | const GrRect& rect, |
| 927 | GrScalar width, |
| 928 | const GrMatrix* matrix) { |
| 929 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 930 | |
| 931 | GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 932 | int stageMask = paint.getActiveStageMask(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 933 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 934 | GrRect devRect = rect; |
| 935 | GrMatrix combinedMatrix; |
| 936 | bool doAA = apply_aa_to_rect(target, fGpu, paint, rect, width, matrix, |
| 937 | &combinedMatrix, &devRect); |
| 938 | |
| 939 | if (doAA) { |
| 940 | GrDrawTarget::AutoViewMatrixRestore avm(target); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 941 | if (stageMask) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 942 | GrMatrix inv; |
| 943 | if (combinedMatrix.invert(&inv)) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 944 | target->preConcatSamplerMatrices(stageMask, inv); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | target->setViewMatrix(GrMatrix::I()); |
| 948 | if (width >= 0) { |
| 949 | GrVec strokeSize;; |
| 950 | if (width > 0) { |
| 951 | strokeSize.set(width, width); |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 952 | combinedMatrix.mapVectors(&strokeSize, 1); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 953 | strokeSize.setAbs(strokeSize); |
| 954 | } else { |
| 955 | strokeSize.set(GR_Scalar1, GR_Scalar1); |
| 956 | } |
| 957 | strokeAARect(target, paint, devRect, strokeSize); |
| 958 | } else { |
| 959 | fillAARect(target, paint, devRect); |
| 960 | } |
| 961 | return; |
| 962 | } |
| 963 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 964 | if (width >= 0) { |
| 965 | // TODO: consider making static vertex buffers for these cases. |
| 966 | // Hairline could be done by just adding closing vertex to |
| 967 | // unitSquareVertexBuffer() |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 968 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL); |
| 969 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 970 | static const int worstCaseVertCount = 10; |
| 971 | GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0); |
| 972 | |
| 973 | if (!geo.succeeded()) { |
| 974 | return; |
| 975 | } |
| 976 | |
| 977 | GrPrimitiveType primType; |
| 978 | int vertCount; |
| 979 | GrPoint* vertex = geo.positions(); |
| 980 | |
| 981 | if (width > 0) { |
| 982 | vertCount = 10; |
| 983 | primType = kTriangleStrip_PrimitiveType; |
| 984 | setStrokeRectStrip(vertex, rect, width); |
| 985 | } else { |
| 986 | // hairline |
| 987 | vertCount = 5; |
| 988 | primType = kLineStrip_PrimitiveType; |
| 989 | vertex[0].set(rect.fLeft, rect.fTop); |
| 990 | vertex[1].set(rect.fRight, rect.fTop); |
| 991 | vertex[2].set(rect.fRight, rect.fBottom); |
| 992 | vertex[3].set(rect.fLeft, rect.fBottom); |
| 993 | vertex[4].set(rect.fLeft, rect.fTop); |
| 994 | } |
| 995 | |
| 996 | GrDrawTarget::AutoViewMatrixRestore avmr; |
| 997 | if (NULL != matrix) { |
| 998 | avmr.set(target); |
| 999 | target->preConcatViewMatrix(*matrix); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1000 | target->preConcatSamplerMatrices(stageMask, *matrix); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | target->drawNonIndexed(primType, 0, vertCount); |
| 1004 | } else { |
| 1005 | #if GR_STATIC_RECT_VB |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1006 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL); |
| 1007 | |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1008 | target->setVertexSourceToBuffer(layout, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1009 | fGpu->getUnitSquareVertexBuffer()); |
| 1010 | GrDrawTarget::AutoViewMatrixRestore avmr(target); |
| 1011 | GrMatrix m; |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1012 | m.setAll(rect.width(), 0, rect.fLeft, |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 1013 | 0, rect.height(), rect.fTop, |
| 1014 | 0, 0, GrMatrix::I()[8]); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1015 | |
| 1016 | if (NULL != matrix) { |
| 1017 | m.postConcat(*matrix); |
| 1018 | } |
| 1019 | |
| 1020 | target->preConcatViewMatrix(m); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1021 | target->preConcatSamplerMatrices(stageMask, m); |
| 1022 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1023 | target->drawNonIndexed(kTriangleFan_PrimitiveType, 0, 4); |
| 1024 | #else |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1025 | target->drawSimpleRect(rect, matrix, stageMask); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1026 | #endif |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | void GrContext::drawRectToRect(const GrPaint& paint, |
| 1031 | const GrRect& dstRect, |
| 1032 | const GrRect& srcRect, |
| 1033 | const GrMatrix* dstMatrix, |
| 1034 | const GrMatrix* srcMatrix) { |
| 1035 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1036 | // srcRect refers to paint's first texture |
| 1037 | if (NULL == paint.getTexture(0)) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1038 | drawRect(paint, dstRect, -1, dstMatrix); |
| 1039 | return; |
| 1040 | } |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1041 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1042 | GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB); |
| 1043 | |
| 1044 | #if GR_STATIC_RECT_VB |
| 1045 | GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1046 | |
| 1047 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1048 | GrDrawTarget::AutoViewMatrixRestore avmr(target); |
| 1049 | |
| 1050 | GrMatrix m; |
| 1051 | |
| 1052 | m.setAll(dstRect.width(), 0, dstRect.fLeft, |
| 1053 | 0, dstRect.height(), dstRect.fTop, |
| 1054 | 0, 0, GrMatrix::I()[8]); |
| 1055 | if (NULL != dstMatrix) { |
| 1056 | m.postConcat(*dstMatrix); |
| 1057 | } |
| 1058 | target->preConcatViewMatrix(m); |
| 1059 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1060 | // srcRect refers to first stage |
| 1061 | int otherStageMask = paint.getActiveStageMask() & |
| 1062 | (~(1 << GrPaint::kFirstTextureStage)); |
| 1063 | if (otherStageMask) { |
| 1064 | target->preConcatSamplerMatrices(otherStageMask, m); |
| 1065 | } |
| 1066 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1067 | m.setAll(srcRect.width(), 0, srcRect.fLeft, |
| 1068 | 0, srcRect.height(), srcRect.fTop, |
| 1069 | 0, 0, GrMatrix::I()[8]); |
| 1070 | if (NULL != srcMatrix) { |
| 1071 | m.postConcat(*srcMatrix); |
| 1072 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1073 | target->preConcatSamplerMatrix(GrPaint::kFirstTextureStage, m); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1074 | |
| 1075 | target->setVertexSourceToBuffer(layout, fGpu->getUnitSquareVertexBuffer()); |
| 1076 | target->drawNonIndexed(kTriangleFan_PrimitiveType, 0, 4); |
| 1077 | #else |
| 1078 | |
| 1079 | GrDrawTarget* target; |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1080 | #if BATCH_RECT_TO_RECT |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1081 | target = this->prepareToDraw(paint, kBuffered_DrawCategory); |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1082 | #else |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1083 | target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
| 1084 | #endif |
| 1085 | |
| 1086 | const GrRect* srcRects[GrDrawTarget::kNumStages] = {NULL}; |
| 1087 | const GrMatrix* srcMatrices[GrDrawTarget::kNumStages] = {NULL}; |
| 1088 | srcRects[0] = &srcRect; |
| 1089 | srcMatrices[0] = srcMatrix; |
| 1090 | |
| 1091 | target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices); |
| 1092 | #endif |
| 1093 | } |
| 1094 | |
| 1095 | void GrContext::drawVertices(const GrPaint& paint, |
| 1096 | GrPrimitiveType primitiveType, |
| 1097 | int vertexCount, |
| 1098 | const GrPoint positions[], |
| 1099 | const GrPoint texCoords[], |
| 1100 | const GrColor colors[], |
| 1101 | const uint16_t indices[], |
| 1102 | int indexCount) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1103 | |
| 1104 | GrDrawTarget::AutoReleaseGeometry geo; |
| 1105 | |
| 1106 | GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
| 1107 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1108 | bool hasTexCoords[GrPaint::kTotalStages] = { |
| 1109 | NULL != texCoords, // texCoordSrc provides explicit stage 0 coords |
| 1110 | 0 // remaining stages use positions |
| 1111 | }; |
| 1112 | |
| 1113 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1114 | |
| 1115 | if (NULL != colors) { |
| 1116 | layout |= GrDrawTarget::kColor_VertexLayoutBit; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1117 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1118 | int vertexSize = GrDrawTarget::VertexSize(layout); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1119 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1120 | bool doAA = false; |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1121 | OffscreenRecord record; |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1122 | GrIRect bounds; |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1123 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1124 | if (sizeof(GrPoint) != vertexSize) { |
| 1125 | if (!geo.set(target, layout, vertexCount, 0)) { |
| 1126 | GrPrintf("Failed to get space for vertices!"); |
| 1127 | return; |
| 1128 | } |
| 1129 | int texOffsets[GrDrawTarget::kMaxTexCoords]; |
| 1130 | int colorOffset; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1131 | GrDrawTarget::VertexSizeAndOffsetsByIdx(layout, |
| 1132 | texOffsets, |
| 1133 | &colorOffset); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1134 | void* curVertex = geo.vertices(); |
| 1135 | |
| 1136 | for (int i = 0; i < vertexCount; ++i) { |
| 1137 | *((GrPoint*)curVertex) = positions[i]; |
| 1138 | |
| 1139 | if (texOffsets[0] > 0) { |
| 1140 | *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i]; |
| 1141 | } |
| 1142 | if (colorOffset > 0) { |
| 1143 | *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i]; |
| 1144 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1145 | curVertex = (void*)((intptr_t)curVertex + vertexSize); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1146 | } |
| 1147 | } else { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1148 | // we don't do offscreen AA when we have per-vertex tex coords or colors |
| 1149 | if (this->doOffscreenAA(target, paint, GrIsPrimTypeLines(primitiveType))) { |
| 1150 | GrRect b; |
| 1151 | b.setBounds(positions, vertexCount); |
| 1152 | target->getViewMatrix().mapRect(&b); |
| 1153 | b.roundOut(&bounds); |
| 1154 | |
| 1155 | if (this->setupOffscreenAAPass1(target, false, bounds, &record)) { |
| 1156 | doAA = true; |
| 1157 | } |
| 1158 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1159 | target->setVertexSourceToArray(layout, positions, vertexCount); |
| 1160 | } |
| 1161 | |
| 1162 | if (NULL != indices) { |
| 1163 | target->setIndexSourceToArray(indices, indexCount); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1166 | if (NULL != indices) { |
| 1167 | target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1168 | } else { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1169 | target->drawNonIndexed(primitiveType, 0, vertexCount); |
| 1170 | } |
| 1171 | |
| 1172 | if (doAA) { |
| 1173 | this->offscreenAAPass2(target, paint, bounds, &record); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 1178 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1179 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 1180 | void GrContext::drawPath(const GrPaint& paint, const GrPath& path, |
| 1181 | GrPathFill fill, const GrPoint* translate) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1182 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1183 | GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 1184 | GrPathRenderer* pr = this->getPathRenderer(target, path, fill); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1185 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1186 | if (!IsFillInverted(fill) && // will be relaxed soon |
| 1187 | !pr->supportsAA(target, path, fill) && |
| 1188 | this->doOffscreenAA(target, paint, kHairLine_PathFill == fill)) { |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 1189 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1190 | OffscreenRecord record; |
| 1191 | bool needsStencil = pr->requiresStencilPass(target, path, fill); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1192 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1193 | // compute bounds as intersection of rt size, clip, and path |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 1194 | GrIRect bound = SkIRect::MakeWH(target->getRenderTarget()->width(), |
| 1195 | target->getRenderTarget()->height()); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1196 | if (target->getClip().hasConservativeBounds()) { |
| 1197 | GrIRect clipIBounds; |
| 1198 | target->getClip().getConservativeBounds().roundOut(&clipIBounds); |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 1199 | if (!bound.intersect(clipIBounds)) { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1200 | return; |
| 1201 | } |
| 1202 | } |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 1203 | GrRect pathBounds = path.getBounds(); |
| 1204 | if (!pathBounds.isEmpty()) { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1205 | GrIRect pathIBounds; |
| 1206 | target->getViewMatrix().mapRect(&pathBounds, pathBounds); |
| 1207 | pathBounds.roundOut(&pathIBounds); |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 1208 | if (!bound.intersect(pathIBounds)) { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1209 | return; |
| 1210 | } |
| 1211 | } |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 1212 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1213 | if (this->setupOffscreenAAPass1(target, needsStencil, bound, &record)) { |
| 1214 | pr->drawPath(target, 0, path, fill, translate); |
| 1215 | this->offscreenAAPass2(target, paint, bound, &record); |
| 1216 | return; |
| 1217 | } |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 1218 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1219 | GrDrawTarget::StageBitfield enabledStages = paint.getActiveStageMask(); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 1220 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 1221 | pr->drawPath(target, enabledStages, path, fill, translate); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1222 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1223 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1224 | //////////////////////////////////////////////////////////////////////////////// |
| 1225 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1226 | void GrContext::flush(int flagsBitfield) { |
| 1227 | if (kDiscard_FlushBit & flagsBitfield) { |
| 1228 | fDrawBuffer->reset(); |
| 1229 | } else { |
| 1230 | flushDrawBuffer(); |
| 1231 | } |
| 1232 | |
| 1233 | if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1234 | fGpu->forceRenderTargetFlush(); |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | void GrContext::flushText() { |
| 1239 | if (kText_DrawCategory == fLastDrawCategory) { |
| 1240 | flushDrawBuffer(); |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | void GrContext::flushDrawBuffer() { |
| 1245 | #if BATCH_RECT_TO_RECT || DEFER_TEXT_RENDERING |
| 1246 | fDrawBuffer->playback(fGpu); |
| 1247 | fDrawBuffer->reset(); |
| 1248 | #endif |
| 1249 | } |
| 1250 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1251 | bool GrContext::readTexturePixels(GrTexture* texture, |
| 1252 | int left, int top, int width, int height, |
| 1253 | GrPixelConfig config, void* buffer) { |
| 1254 | |
| 1255 | // TODO: code read pixels for textures that aren't rendertargets |
| 1256 | |
| 1257 | this->flush(); |
| 1258 | GrRenderTarget* target = texture->asRenderTarget(); |
| 1259 | if (NULL != target) { |
| 1260 | return fGpu->readPixels(target, |
| 1261 | left, top, width, height, |
| 1262 | config, buffer); |
| 1263 | } else { |
| 1264 | return false; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | bool GrContext::readRenderTargetPixels(GrRenderTarget* target, |
| 1269 | int left, int top, int width, int height, |
| 1270 | GrPixelConfig config, void* buffer) { |
| 1271 | uint32_t flushFlags = 0; |
| 1272 | if (NULL == target) { |
| 1273 | flushFlags |= GrContext::kForceCurrentRenderTarget_FlushBit; |
| 1274 | } |
| 1275 | |
| 1276 | this->flush(flushFlags); |
| 1277 | return fGpu->readPixels(target, |
| 1278 | left, top, width, height, |
| 1279 | config, buffer); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | void GrContext::writePixels(int left, int top, int width, int height, |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1283 | GrPixelConfig config, const void* buffer, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1284 | size_t stride) { |
| 1285 | |
| 1286 | // TODO: when underlying api has a direct way to do this we should use it |
| 1287 | // (e.g. glDrawPixels on desktop GL). |
| 1288 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 1289 | const GrTextureDesc desc = { |
| 1290 | kNone_GrTextureFlags, kNone_GrAALevel, width, height, config |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1291 | }; |
| 1292 | GrTexture* texture = fGpu->createTexture(desc, buffer, stride); |
| 1293 | if (NULL == texture) { |
| 1294 | return; |
| 1295 | } |
| 1296 | |
| 1297 | this->flush(true); |
| 1298 | |
| 1299 | GrAutoUnref aur(texture); |
| 1300 | GrDrawTarget::AutoStateRestore asr(fGpu); |
| 1301 | |
| 1302 | GrMatrix matrix; |
| 1303 | matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top)); |
| 1304 | fGpu->setViewMatrix(matrix); |
| 1305 | |
| 1306 | fGpu->disableState(GrDrawTarget::kClip_StateBit); |
| 1307 | fGpu->setAlpha(0xFF); |
| 1308 | fGpu->setBlendFunc(kOne_BlendCoeff, |
| 1309 | kZero_BlendCoeff); |
| 1310 | fGpu->setTexture(0, texture); |
| 1311 | |
| 1312 | GrSamplerState sampler; |
| 1313 | sampler.setClampNoFilter(); |
| 1314 | matrix.setScale(GR_Scalar1 / width, GR_Scalar1 / height); |
| 1315 | sampler.setMatrix(matrix); |
| 1316 | fGpu->setSamplerState(0, sampler); |
| 1317 | |
| 1318 | GrVertexLayout layout = GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0); |
| 1319 | static const int VCOUNT = 4; |
| 1320 | |
| 1321 | GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0); |
| 1322 | if (!geo.succeeded()) { |
| 1323 | return; |
| 1324 | } |
| 1325 | ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height); |
| 1326 | fGpu->drawNonIndexed(kTriangleFan_PrimitiveType, 0, VCOUNT); |
| 1327 | } |
| 1328 | //////////////////////////////////////////////////////////////////////////////// |
| 1329 | |
| 1330 | void GrContext::SetPaint(const GrPaint& paint, GrDrawTarget* target) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame^] | 1331 | |
| 1332 | for (int i = 0; i < GrPaint::kMaxTextures; ++i) { |
| 1333 | int s = i + GrPaint::kFirstTextureStage; |
| 1334 | target->setTexture(s, paint.getTexture(i)); |
| 1335 | target->setSamplerState(s, *paint.getTextureSampler(i)); |
| 1336 | } |
| 1337 | |
| 1338 | target->setFirstCoverageStage(GrPaint::kFirstMaskStage); |
| 1339 | |
| 1340 | for (int i = 0; i < GrPaint::kMaxMasks; ++i) { |
| 1341 | int s = i + GrPaint::kFirstMaskStage; |
| 1342 | target->setTexture(s, paint.getMask(i)); |
| 1343 | target->setSamplerState(s, *paint.getMaskSampler(i)); |
| 1344 | } |
| 1345 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1346 | target->setColor(paint.fColor); |
| 1347 | |
| 1348 | if (paint.fDither) { |
| 1349 | target->enableState(GrDrawTarget::kDither_StateBit); |
| 1350 | } else { |
| 1351 | target->disableState(GrDrawTarget::kDither_StateBit); |
| 1352 | } |
| 1353 | if (paint.fAntiAlias) { |
| 1354 | target->enableState(GrDrawTarget::kAntialias_StateBit); |
| 1355 | } else { |
| 1356 | target->disableState(GrDrawTarget::kAntialias_StateBit); |
| 1357 | } |
| 1358 | target->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 1359 | target->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1362 | GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1363 | DrawCategory category) { |
| 1364 | if (category != fLastDrawCategory) { |
| 1365 | flushDrawBuffer(); |
| 1366 | fLastDrawCategory = category; |
| 1367 | } |
| 1368 | SetPaint(paint, fGpu); |
| 1369 | GrDrawTarget* target = fGpu; |
| 1370 | switch (category) { |
| 1371 | case kText_DrawCategory: |
| 1372 | #if DEFER_TEXT_RENDERING |
| 1373 | target = fDrawBuffer; |
| 1374 | fDrawBuffer->initializeDrawStateAndClip(*fGpu); |
| 1375 | #else |
| 1376 | target = fGpu; |
| 1377 | #endif |
| 1378 | break; |
| 1379 | case kUnbuffered_DrawCategory: |
| 1380 | target = fGpu; |
| 1381 | break; |
| 1382 | case kBuffered_DrawCategory: |
| 1383 | target = fDrawBuffer; |
| 1384 | fDrawBuffer->initializeDrawStateAndClip(*fGpu); |
| 1385 | break; |
| 1386 | } |
| 1387 | return target; |
| 1388 | } |
| 1389 | |
| 1390 | //////////////////////////////////////////////////////////////////////////////// |
| 1391 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1392 | void GrContext::setRenderTarget(GrRenderTarget* target) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1393 | this->flush(false); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1394 | fGpu->setRenderTarget(target); |
| 1395 | } |
| 1396 | |
| 1397 | GrRenderTarget* GrContext::getRenderTarget() { |
| 1398 | return fGpu->getRenderTarget(); |
| 1399 | } |
| 1400 | |
| 1401 | const GrRenderTarget* GrContext::getRenderTarget() const { |
| 1402 | return fGpu->getRenderTarget(); |
| 1403 | } |
| 1404 | |
| 1405 | const GrMatrix& GrContext::getMatrix() const { |
| 1406 | return fGpu->getViewMatrix(); |
| 1407 | } |
| 1408 | |
| 1409 | void GrContext::setMatrix(const GrMatrix& m) { |
| 1410 | fGpu->setViewMatrix(m); |
| 1411 | } |
| 1412 | |
| 1413 | void GrContext::concatMatrix(const GrMatrix& m) const { |
| 1414 | fGpu->preConcatViewMatrix(m); |
| 1415 | } |
| 1416 | |
| 1417 | static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) { |
| 1418 | intptr_t mask = 1 << shift; |
| 1419 | if (pred) { |
| 1420 | bits |= mask; |
| 1421 | } else { |
| 1422 | bits &= ~mask; |
| 1423 | } |
| 1424 | return bits; |
| 1425 | } |
| 1426 | |
| 1427 | void GrContext::resetStats() { |
| 1428 | fGpu->resetStats(); |
| 1429 | } |
| 1430 | |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 1431 | const GrGpuStats& GrContext::getStats() const { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1432 | return fGpu->getStats(); |
| 1433 | } |
| 1434 | |
| 1435 | void GrContext::printStats() const { |
| 1436 | fGpu->printStats(); |
| 1437 | } |
| 1438 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 1439 | GrContext::GrContext(GrGpu* gpu) : |
| 1440 | fDefaultPathRenderer(gpu->supportsTwoSidedStencil(), |
| 1441 | gpu->supportsStencilWrapOps()) { |
| 1442 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1443 | fGpu = gpu; |
| 1444 | fGpu->ref(); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1445 | fGpu->setContext(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1446 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 1447 | fCustomPathRenderer = GrPathRenderer::CreatePathRenderer(); |
| 1448 | fGpu->setClipPathRenderer(fCustomPathRenderer); |
| 1449 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1450 | fTextureCache = new GrTextureCache(MAX_TEXTURE_CACHE_COUNT, |
| 1451 | MAX_TEXTURE_CACHE_BYTES); |
| 1452 | fFontCache = new GrFontCache(fGpu); |
| 1453 | |
| 1454 | fLastDrawCategory = kUnbuffered_DrawCategory; |
| 1455 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1456 | fDrawBuffer = NULL; |
| 1457 | fDrawBufferVBAllocPool = NULL; |
| 1458 | fDrawBufferIBAllocPool = NULL; |
| 1459 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 1460 | fAAFillRectIndexBuffer = NULL; |
| 1461 | fAAStrokeRectIndexBuffer = NULL; |
| 1462 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1463 | this->setupDrawBuffer(); |
| 1464 | } |
| 1465 | |
| 1466 | void GrContext::setupDrawBuffer() { |
| 1467 | |
| 1468 | GrAssert(NULL == fDrawBuffer); |
| 1469 | GrAssert(NULL == fDrawBufferVBAllocPool); |
| 1470 | GrAssert(NULL == fDrawBufferIBAllocPool); |
| 1471 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1472 | #if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1473 | fDrawBufferVBAllocPool = |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1474 | new GrVertexBufferAllocPool(fGpu, false, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1475 | DRAW_BUFFER_VBPOOL_BUFFER_SIZE, |
| 1476 | DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS); |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1477 | fDrawBufferIBAllocPool = |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1478 | new GrIndexBufferAllocPool(fGpu, false, |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1479 | DRAW_BUFFER_IBPOOL_BUFFER_SIZE, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1480 | DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS); |
| 1481 | |
| 1482 | fDrawBuffer = new GrInOrderDrawBuffer(fDrawBufferVBAllocPool, |
| 1483 | fDrawBufferIBAllocPool); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1484 | #endif |
| 1485 | |
| 1486 | #if BATCH_RECT_TO_RECT |
| 1487 | fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer()); |
| 1488 | #endif |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1491 | GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) { |
| 1492 | GrDrawTarget* target; |
| 1493 | #if DEFER_TEXT_RENDERING |
| 1494 | target = prepareToDraw(paint, kText_DrawCategory); |
| 1495 | #else |
| 1496 | target = prepareToDraw(paint, kUnbuffered_DrawCategory); |
| 1497 | #endif |
| 1498 | SetPaint(paint, target); |
| 1499 | return target; |
| 1500 | } |
| 1501 | |
| 1502 | const GrIndexBuffer* GrContext::getQuadIndexBuffer() const { |
| 1503 | return fGpu->getQuadIndexBuffer(); |
| 1504 | } |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 1505 | |
| 1506 | GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target, |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 1507 | const GrPath& path, |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 1508 | GrPathFill fill) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1509 | if (NULL != fCustomPathRenderer && |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 1510 | fCustomPathRenderer->canDrawPath(target, path, fill)) { |
| 1511 | return fCustomPathRenderer; |
| 1512 | } else { |
| 1513 | GrAssert(fDefaultPathRenderer.canDrawPath(target, path, fill)); |
| 1514 | return &fDefaultPathRenderer; |
| 1515 | } |
| 1516 | } |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 1517 | |