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