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