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