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