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