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