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