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 | |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 10 | #include "GrContext.h" |
| 11 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 12 | #include "effects/GrMorphologyEffect.h" |
| 13 | #include "effects/GrConvolutionEffect.h" |
| 14 | |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 15 | #include "GrBufferAllocPool.h" |
| 16 | #include "GrClipIterator.h" |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 17 | #include "GrGpu.h" |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 18 | #include "GrIndexBuffer.h" |
| 19 | #include "GrInOrderDrawBuffer.h" |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 20 | #include "GrPathRenderer.h" |
tomhudson@google.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 21 | #include "GrPathUtils.h" |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 22 | #include "GrResourceCache.h" |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 23 | #include "GrSoftwarePathRenderer.h" |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 24 | #include "GrStencilBuffer.h" |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 25 | #include "GrTextStrike.h" |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 26 | #include "SkTLazy.h" |
tomhudson@google.com | 0c8d93a | 2011-07-01 17:08:26 +0000 | [diff] [blame] | 27 | #include "SkTrace.h" |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 28 | |
bsalomon@google.com | 3c4d032 | 2012-04-03 18:04:51 +0000 | [diff] [blame] | 29 | #define DEFER_TEXT_RENDERING 1 |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 30 | |
bsalomon@google.com | 3c4d032 | 2012-04-03 18:04:51 +0000 | [diff] [blame] | 31 | #define DEFER_PATHS 1 |
bsalomon@google.com | fb4ce6f | 2012-03-14 13:27:54 +0000 | [diff] [blame] | 32 | |
bsalomon@google.com | 3c4d032 | 2012-04-03 18:04:51 +0000 | [diff] [blame] | 33 | #define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB) |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 34 | |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 35 | #define MAX_BLUR_SIGMA 4.0f |
| 36 | |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 37 | // When we're using coverage AA but the blend is incompatible (given gpu |
| 38 | // limitations) should we disable AA or draw wrong? |
bsalomon@google.com | 950d7a8 | 2011-09-28 15:05:33 +0000 | [diff] [blame] | 39 | #define DISABLE_COVERAGE_AA_FOR_BLEND 1 |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 40 | |
reed@google.com | 4b2d3f3 | 2012-05-15 18:05:50 +0000 | [diff] [blame] | 41 | #if GR_DEBUG |
| 42 | // change this to a 1 to see notifications when partial coverage fails |
| 43 | #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0 |
| 44 | #else |
| 45 | #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0 |
| 46 | #endif |
| 47 | |
bsalomon@google.com | 8ccaddd | 2011-08-09 16:49:03 +0000 | [diff] [blame] | 48 | static const size_t MAX_TEXTURE_CACHE_COUNT = 256; |
| 49 | static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 50 | |
bsalomon@google.com | 6036149 | 2012-03-15 17:47:06 +0000 | [diff] [blame] | 51 | static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 52 | static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4; |
| 53 | |
bsalomon@google.com | fb4ce6f | 2012-03-14 13:27:54 +0000 | [diff] [blame] | 54 | // path rendering is the only thing we defer today that uses non-static indices |
| 55 | static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0; |
| 56 | static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 57 | |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 58 | #define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this) |
| 59 | |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 60 | GrContext* GrContext::Create(GrEngine engine, |
| 61 | GrPlatform3DContext context3D) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 62 | GrContext* ctx = NULL; |
| 63 | GrGpu* fGpu = GrGpu::Create(engine, context3D); |
| 64 | if (NULL != fGpu) { |
| 65 | ctx = new GrContext(fGpu); |
| 66 | fGpu->unref(); |
| 67 | } |
| 68 | return ctx; |
| 69 | } |
| 70 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 71 | GrContext::~GrContext() { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 72 | this->flush(); |
robertphillips@google.com | 5acc0e3 | 2012-05-17 12:01:02 +0000 | [diff] [blame] | 73 | |
| 74 | // Since the gpu can hold scratch textures, give it a chance to let go |
| 75 | // of them before freeing the texture cache |
| 76 | fGpu->purgeResources(); |
| 77 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 78 | delete fTextureCache; |
| 79 | delete fFontCache; |
| 80 | delete fDrawBuffer; |
| 81 | delete fDrawBufferVBAllocPool; |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 82 | delete fDrawBufferIBAllocPool; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 83 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 84 | fAARectRenderer->unref(); |
| 85 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 86 | fGpu->unref(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 87 | GrSafeUnref(fPathRendererChain); |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 88 | GrSafeUnref(fSoftwarePathRenderer); |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 89 | fDrawState->unref(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 90 | } |
| 91 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 92 | void GrContext::contextLost() { |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 93 | contextDestroyed(); |
| 94 | this->setupDrawBuffer(); |
| 95 | } |
| 96 | |
| 97 | void GrContext::contextDestroyed() { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 98 | // abandon first to so destructors |
| 99 | // don't try to free the resources in the API. |
| 100 | fGpu->abandonResources(); |
| 101 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 102 | // a path renderer may be holding onto resources that |
| 103 | // are now unusable |
| 104 | GrSafeSetNull(fPathRendererChain); |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 105 | GrSafeSetNull(fSoftwarePathRenderer); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 106 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 107 | delete fDrawBuffer; |
| 108 | fDrawBuffer = NULL; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 109 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 110 | delete fDrawBufferVBAllocPool; |
| 111 | fDrawBufferVBAllocPool = NULL; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 112 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 113 | delete fDrawBufferIBAllocPool; |
| 114 | fDrawBufferIBAllocPool = NULL; |
| 115 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 116 | fAARectRenderer->reset(); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 117 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 118 | fTextureCache->removeAll(); |
| 119 | fFontCache->freeAll(); |
| 120 | fGpu->markContextDirty(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void GrContext::resetContext() { |
| 124 | fGpu->markContextDirty(); |
| 125 | } |
| 126 | |
| 127 | void GrContext::freeGpuResources() { |
| 128 | this->flush(); |
robertphillips@google.com | ff17584 | 2012-05-14 19:31:39 +0000 | [diff] [blame] | 129 | |
| 130 | fGpu->purgeResources(); |
| 131 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 132 | fAARectRenderer->reset(); |
| 133 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 134 | fTextureCache->removeAll(); |
| 135 | fFontCache->freeAll(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 136 | // a path renderer may be holding onto resources |
| 137 | GrSafeSetNull(fPathRendererChain); |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 138 | GrSafeSetNull(fSoftwarePathRenderer); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 139 | } |
| 140 | |
twiz@google.com | 05e7024 | 2012-01-27 19:12:00 +0000 | [diff] [blame] | 141 | size_t GrContext::getGpuTextureCacheBytes() const { |
| 142 | return fTextureCache->getCachedResourceBytes(); |
| 143 | } |
| 144 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 145 | //////////////////////////////////////////////////////////////////////////////// |
| 146 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 147 | int GrContext::PaintStageVertexLayoutBits( |
| 148 | const GrPaint& paint, |
| 149 | const bool hasTexCoords[GrPaint::kTotalStages]) { |
| 150 | int stageMask = paint.getActiveStageMask(); |
| 151 | int layout = 0; |
| 152 | for (int i = 0; i < GrPaint::kTotalStages; ++i) { |
| 153 | if ((1 << i) & stageMask) { |
| 154 | if (NULL != hasTexCoords && hasTexCoords[i]) { |
| 155 | layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(i, i); |
| 156 | } else { |
| 157 | layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(i); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | return layout; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | //////////////////////////////////////////////////////////////////////////////// |
| 166 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 167 | GrTexture* GrContext::TextureCacheEntry::texture() const { |
| 168 | if (NULL == fEntry) { |
| 169 | return NULL; |
| 170 | } else { |
| 171 | return (GrTexture*) fEntry->resource(); |
| 172 | } |
| 173 | } |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 174 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 175 | namespace { |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 176 | |
| 177 | // we should never have more than one stencil buffer with same combo of |
| 178 | // (width,height,samplecount) |
| 179 | void gen_stencil_key_values(int width, int height, |
| 180 | int sampleCnt, uint32_t v[4]) { |
| 181 | v[0] = width; |
| 182 | v[1] = height; |
| 183 | v[2] = sampleCnt; |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 184 | v[3] = GrResourceKey::kStencilBuffer_TypeBit; |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void gen_stencil_key_values(const GrStencilBuffer* sb, |
| 188 | uint32_t v[4]) { |
| 189 | gen_stencil_key_values(sb->width(), sb->height(), |
| 190 | sb->numSamples(), v); |
| 191 | } |
bsalomon@google.com | 82c7bd8 | 2011-11-09 15:32:29 +0000 | [diff] [blame] | 192 | |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 193 | void scale_rect(SkRect* rect, float xScale, float yScale) { |
robertphillips@google.com | 5af5606 | 2012-04-27 15:39:52 +0000 | [diff] [blame] | 194 | rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale)); |
| 195 | rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale)); |
| 196 | rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale)); |
| 197 | rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale)); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 198 | } |
| 199 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 200 | float adjust_sigma(float sigma, int *scaleFactor, int *radius) { |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 201 | *scaleFactor = 1; |
| 202 | while (sigma > MAX_BLUR_SIGMA) { |
| 203 | *scaleFactor *= 2; |
| 204 | sigma *= 0.5f; |
| 205 | } |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 206 | *radius = static_cast<int>(ceilf(sigma * 3.0f)); |
| 207 | GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 208 | return sigma; |
| 209 | } |
| 210 | |
| 211 | void apply_morphology(GrGpu* gpu, |
| 212 | GrTexture* texture, |
| 213 | const SkRect& rect, |
| 214 | int radius, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 215 | GrContext::MorphologyType morphType, |
| 216 | Gr1DKernelEffect::Direction direction) { |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 217 | |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 218 | GrRenderTarget* target = gpu->drawState()->getRenderTarget(); |
| 219 | GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 220 | GrDrawState* drawState = gpu->drawState(); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 221 | drawState->setRenderTarget(target); |
| 222 | GrMatrix sampleM; |
| 223 | sampleM.setIDiv(texture->width(), texture->height()); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 224 | drawState->sampler(0)->reset(sampleM); |
| 225 | SkAutoTUnref<GrCustomStage> morph( |
| 226 | new GrMorphologyEffect(direction, radius, morphType)); |
| 227 | drawState->sampler(0)->setCustomStage(morph); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 228 | drawState->setTexture(0, texture); |
| 229 | gpu->drawSimpleRect(rect, NULL, 1 << 0); |
| 230 | } |
| 231 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 232 | void convolve_gaussian(GrGpu* gpu, |
| 233 | GrTexture* texture, |
| 234 | const SkRect& rect, |
| 235 | float sigma, |
| 236 | int radius, |
| 237 | Gr1DKernelEffect::Direction direction) { |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 238 | GrRenderTarget* target = gpu->drawState()->getRenderTarget(); |
| 239 | GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 240 | GrDrawState* drawState = gpu->drawState(); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 241 | drawState->setRenderTarget(target); |
| 242 | GrMatrix sampleM; |
| 243 | sampleM.setIDiv(texture->width(), texture->height()); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 244 | drawState->sampler(0)->reset(sampleM); |
| 245 | SkAutoTUnref<GrConvolutionEffect> conv(new |
| 246 | GrConvolutionEffect(direction, radius)); |
| 247 | conv->setGaussianKernel(sigma); |
| 248 | drawState->sampler(0)->setCustomStage(conv); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 249 | drawState->setTexture(0, texture); |
| 250 | gpu->drawSimpleRect(rect, NULL, 1 << 0); |
| 251 | } |
| 252 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 253 | } |
| 254 | |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 255 | GrContext::TextureCacheEntry GrContext::findAndLockTexture( |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 256 | const GrTextureDesc& desc, |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 257 | const GrSamplerState* sampler) { |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 258 | GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 259 | return TextureCacheEntry(fTextureCache->findAndLock(resourceKey, |
| 260 | GrResourceCache::kNested_LockType)); |
| 261 | } |
| 262 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 263 | bool GrContext::isTextureInCache(const GrTextureDesc& desc, |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 264 | const GrSamplerState* sampler) const { |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 265 | GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 266 | return fTextureCache->hasKey(resourceKey); |
| 267 | } |
| 268 | |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 269 | GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) { |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 270 | ASSERT_OWNED_RESOURCE(sb); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 271 | uint32_t v[4]; |
| 272 | gen_stencil_key_values(sb, v); |
| 273 | GrResourceKey resourceKey(v); |
| 274 | return fTextureCache->createAndLock(resourceKey, sb); |
| 275 | } |
| 276 | |
| 277 | GrStencilBuffer* GrContext::findStencilBuffer(int width, int height, |
| 278 | int sampleCnt) { |
| 279 | uint32_t v[4]; |
| 280 | gen_stencil_key_values(width, height, sampleCnt, v); |
| 281 | GrResourceKey resourceKey(v); |
| 282 | GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey, |
| 283 | GrResourceCache::kSingle_LockType); |
| 284 | if (NULL != entry) { |
| 285 | GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource(); |
| 286 | return sb; |
| 287 | } else { |
| 288 | return NULL; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) { |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 293 | ASSERT_OWNED_RESOURCE(sbEntry->resource()); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 294 | fTextureCache->unlock(sbEntry); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static void stretchImage(void* dst, |
| 298 | int dstW, |
| 299 | int dstH, |
| 300 | void* src, |
| 301 | int srcW, |
| 302 | int srcH, |
| 303 | int bpp) { |
| 304 | GrFixed dx = (srcW << 16) / dstW; |
| 305 | GrFixed dy = (srcH << 16) / dstH; |
| 306 | |
| 307 | GrFixed y = dy >> 1; |
| 308 | |
| 309 | int dstXLimit = dstW*bpp; |
| 310 | for (int j = 0; j < dstH; ++j) { |
| 311 | GrFixed x = dx >> 1; |
| 312 | void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp; |
| 313 | void* dstRow = (uint8_t*)dst + j*dstW*bpp; |
| 314 | for (int i = 0; i < dstXLimit; i += bpp) { |
| 315 | memcpy((uint8_t*) dstRow + i, |
| 316 | (uint8_t*) srcRow + (x>>16)*bpp, |
| 317 | bpp); |
| 318 | x += dx; |
| 319 | } |
| 320 | y += dy; |
| 321 | } |
| 322 | } |
| 323 | |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 324 | GrContext::TextureCacheEntry GrContext::createAndLockTexture( |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 325 | const GrSamplerState* sampler, |
| 326 | const GrTextureDesc& desc, |
| 327 | void* srcData, |
| 328 | size_t rowBytes) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 329 | SK_TRACE_EVENT0("GrContext::createAndLockTexture"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 330 | |
| 331 | #if GR_DUMP_TEXTURE_UPLOAD |
| 332 | GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight); |
| 333 | #endif |
| 334 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 335 | TextureCacheEntry entry; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 336 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 337 | GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 338 | desc, false); |
| 339 | |
| 340 | if (GrTexture::NeedsResizing(resourceKey)) { |
| 341 | // The desired texture is NPOT and tiled but that isn't supported by |
| 342 | // the current hardware. Resize the texture to be a POT |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 343 | GrAssert(NULL != sampler); |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 344 | TextureCacheEntry clampEntry = this->findAndLockTexture(desc, |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 345 | NULL); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 346 | |
| 347 | if (NULL == clampEntry.texture()) { |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 348 | clampEntry = this->createAndLockTexture(NULL, desc, |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 349 | srcData, rowBytes); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 350 | GrAssert(NULL != clampEntry.texture()); |
| 351 | if (NULL == clampEntry.texture()) { |
| 352 | return entry; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 355 | GrTextureDesc rtDesc = desc; |
| 356 | rtDesc.fFlags = rtDesc.fFlags | |
| 357 | kRenderTarget_GrTextureFlagBit | |
| 358 | kNoStencil_GrTextureFlagBit; |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 359 | rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64)); |
| 360 | rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64)); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 361 | |
| 362 | GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0); |
| 363 | |
| 364 | if (NULL != texture) { |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 365 | GrDrawTarget::AutoStateRestore asr(fGpu, |
| 366 | GrDrawTarget::kReset_ASRInit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 367 | GrDrawState* drawState = fGpu->drawState(); |
| 368 | drawState->setRenderTarget(texture->asRenderTarget()); |
| 369 | drawState->setTexture(0, clampEntry.texture()); |
bsalomon@google.com | 82c7bd8 | 2011-11-09 15:32:29 +0000 | [diff] [blame] | 370 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 371 | GrSamplerState::Filter filter; |
| 372 | // if filtering is not desired then we want to ensure all |
| 373 | // texels in the resampled image are copies of texels from |
| 374 | // the original. |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 375 | if (GrTexture::NeedsFiltering(resourceKey)) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 376 | filter = GrSamplerState::kBilinear_Filter; |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 377 | } else { |
| 378 | filter = GrSamplerState::kNearest_Filter; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 379 | } |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 380 | drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode, |
| 381 | filter); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 382 | |
| 383 | static const GrVertexLayout layout = |
| 384 | GrDrawTarget::StageTexCoordVertexLayoutBit(0,0); |
| 385 | GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0); |
| 386 | |
| 387 | if (arg.succeeded()) { |
| 388 | GrPoint* verts = (GrPoint*) arg.vertices(); |
| 389 | verts[0].setIRectFan(0, 0, |
| 390 | texture->width(), |
| 391 | texture->height(), |
| 392 | 2*sizeof(GrPoint)); |
| 393 | verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint)); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 394 | fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 395 | 0, 4); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 396 | entry.set(fTextureCache->createAndLock(resourceKey, texture)); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 397 | } |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 398 | texture->releaseRenderTarget(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 399 | } else { |
| 400 | // TODO: Our CPU stretch doesn't filter. But we create separate |
| 401 | // stretched textures when the sampler state is either filtered or |
| 402 | // not. Either implement filtered stretch blit on CPU or just create |
| 403 | // one when FBO case fails. |
| 404 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 405 | rtDesc.fFlags = kNone_GrTextureFlags; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 406 | // no longer need to clamp at min RT size. |
| 407 | rtDesc.fWidth = GrNextPow2(desc.fWidth); |
| 408 | rtDesc.fHeight = GrNextPow2(desc.fHeight); |
bsalomon@google.com | 64c4fe4 | 2011-11-05 14:51:01 +0000 | [diff] [blame] | 409 | int bpp = GrBytesPerPixel(desc.fConfig); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 410 | SkAutoSMalloc<128*128*4> stretchedPixels(bpp * |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 411 | rtDesc.fWidth * |
| 412 | rtDesc.fHeight); |
| 413 | stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight, |
| 414 | srcData, desc.fWidth, desc.fHeight, bpp); |
| 415 | |
| 416 | size_t stretchedRowBytes = rtDesc.fWidth * bpp; |
| 417 | |
| 418 | GrTexture* texture = fGpu->createTexture(rtDesc, |
| 419 | stretchedPixels.get(), |
| 420 | stretchedRowBytes); |
| 421 | GrAssert(NULL != texture); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 422 | entry.set(fTextureCache->createAndLock(resourceKey, texture)); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 423 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 424 | fTextureCache->unlock(clampEntry.cacheEntry()); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 425 | |
| 426 | } else { |
| 427 | GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes); |
| 428 | if (NULL != texture) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 429 | entry.set(fTextureCache->createAndLock(resourceKey, texture)); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | return entry; |
| 433 | } |
| 434 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 435 | GrContext::TextureCacheEntry GrContext::lockScratchTexture( |
| 436 | const GrTextureDesc& inDesc, |
| 437 | ScratchTexMatch match) { |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 438 | GrTextureDesc desc = inDesc; |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 439 | desc.fClientCacheID = kScratch_CacheID; |
| 440 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 441 | if (kExact_ScratchTexMatch != match) { |
| 442 | // bin by pow2 with a reasonable min |
| 443 | static const int MIN_SIZE = 256; |
| 444 | desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth)); |
| 445 | desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight)); |
| 446 | } |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 447 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 448 | GrResourceEntry* entry; |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 449 | int origWidth = desc.fWidth; |
| 450 | int origHeight = desc.fHeight; |
| 451 | bool doubledW = false; |
| 452 | bool doubledH = false; |
| 453 | |
| 454 | do { |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 455 | GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 456 | entry = fTextureCache->findAndLock(key, |
| 457 | GrResourceCache::kNested_LockType); |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 458 | // if we miss, relax the fit of the flags... |
| 459 | // then try doubling width... then height. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 460 | if (NULL != entry || kExact_ScratchTexMatch == match) { |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 461 | break; |
| 462 | } |
| 463 | if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) { |
| 464 | desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit; |
| 465 | } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) { |
| 466 | desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit; |
| 467 | } else if (!doubledW) { |
| 468 | desc.fFlags = inDesc.fFlags; |
| 469 | desc.fWidth *= 2; |
| 470 | doubledW = true; |
| 471 | } else if (!doubledH) { |
| 472 | desc.fFlags = inDesc.fFlags; |
| 473 | desc.fWidth = origWidth; |
| 474 | desc.fHeight *= 2; |
| 475 | doubledH = true; |
| 476 | } else { |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | } while (true); |
| 481 | |
| 482 | if (NULL == entry) { |
| 483 | desc.fFlags = inDesc.fFlags; |
| 484 | desc.fWidth = origWidth; |
| 485 | desc.fHeight = origHeight; |
| 486 | GrTexture* texture = fGpu->createTexture(desc, NULL, 0); |
| 487 | if (NULL != texture) { |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 488 | GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 489 | texture->desc(), |
| 490 | true); |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 491 | entry = fTextureCache->createAndLock(key, texture); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // If the caller gives us the same desc/sampler twice we don't want |
| 496 | // to return the same texture the second time (unless it was previously |
| 497 | // released). So we detach the entry from the cache and reattach at release. |
| 498 | if (NULL != entry) { |
| 499 | fTextureCache->detach(entry); |
| 500 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 501 | return TextureCacheEntry(entry); |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 502 | } |
| 503 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 504 | void GrContext::unlockTexture(TextureCacheEntry entry) { |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 505 | ASSERT_OWNED_RESOURCE(entry.texture()); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 506 | // If this is a scratch texture we detached it from the cache |
| 507 | // while it was locked (to avoid two callers simultaneously getting |
| 508 | // the same texture). |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 509 | if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 510 | fTextureCache->reattachAndUnlock(entry.cacheEntry()); |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 511 | } else { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 512 | fTextureCache->unlock(entry.cacheEntry()); |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 513 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 514 | } |
| 515 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 516 | GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 517 | void* srcData, |
| 518 | size_t rowBytes) { |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 519 | GrTextureDesc descCopy = descIn; |
| 520 | descCopy.fClientCacheID = kUncached_CacheID; |
| 521 | return fGpu->createTexture(descCopy, srcData, rowBytes); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void GrContext::getTextureCacheLimits(int* maxTextures, |
| 525 | size_t* maxTextureBytes) const { |
| 526 | fTextureCache->getLimits(maxTextures, maxTextureBytes); |
| 527 | } |
| 528 | |
| 529 | void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) { |
| 530 | fTextureCache->setLimits(maxTextures, maxTextureBytes); |
| 531 | } |
| 532 | |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 533 | int GrContext::getMaxTextureSize() const { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 534 | return fGpu->getCaps().fMaxTextureSize; |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | int GrContext::getMaxRenderTargetSize() const { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 538 | return fGpu->getCaps().fMaxRenderTargetSize; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /////////////////////////////////////////////////////////////////////////////// |
| 542 | |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 543 | GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) { |
| 544 | return fGpu->createPlatformTexture(desc); |
| 545 | } |
| 546 | |
| 547 | GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) { |
| 548 | return fGpu->createPlatformRenderTarget(desc); |
| 549 | } |
| 550 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 551 | /////////////////////////////////////////////////////////////////////////////// |
| 552 | |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 553 | bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler, |
bsalomon@google.com | 1f221a7 | 2011-08-23 20:54:07 +0000 | [diff] [blame] | 554 | int width, int height) const { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 555 | const GrDrawTarget::Caps& caps = fGpu->getCaps(); |
| 556 | if (!caps.f8BitPaletteSupport) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 557 | return false; |
| 558 | } |
| 559 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 560 | bool isPow2 = GrIsPow2(width) && GrIsPow2(height); |
| 561 | |
| 562 | if (!isPow2) { |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 563 | bool tiled = NULL != sampler && |
| 564 | (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode || |
| 565 | sampler->getWrapY() != GrSamplerState::kClamp_WrapMode); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 566 | if (tiled && !caps.fNPOTTextureTileSupport) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 567 | return false; |
| 568 | } |
| 569 | } |
| 570 | return true; |
| 571 | } |
| 572 | |
| 573 | //////////////////////////////////////////////////////////////////////////////// |
| 574 | |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 575 | const GrClip& GrContext::getClip() const { return fGpu->getClip(); } |
| 576 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 577 | void GrContext::setClip(const GrClip& clip) { |
| 578 | fGpu->setClip(clip); |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 579 | fDrawState->enableState(GrDrawState::kClip_StateBit); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | void GrContext::setClip(const GrIRect& rect) { |
| 583 | GrClip clip; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 584 | clip.setFromIRect(rect); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 585 | fGpu->setClip(clip); |
| 586 | } |
| 587 | |
| 588 | //////////////////////////////////////////////////////////////////////////////// |
| 589 | |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 590 | void GrContext::clear(const GrIRect* rect, const GrColor color) { |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 591 | this->flush(); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 592 | fGpu->clear(rect, color); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | void GrContext::drawPaint(const GrPaint& paint) { |
| 596 | // set rect to be big enough to fill the space, but not super-huge, so we |
| 597 | // don't overflow fixed-point implementations |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 598 | GrRect r; |
| 599 | r.setLTRB(0, 0, |
| 600 | GrIntToScalar(getRenderTarget()->width()), |
| 601 | GrIntToScalar(getRenderTarget()->height())); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 602 | GrMatrix inverse; |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 603 | SkTLazy<GrPaint> tmpPaint; |
| 604 | const GrPaint* p = &paint; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 605 | GrAutoMatrix am; |
| 606 | |
bsalomon@google.com | 4f83be8 | 2011-09-12 13:52:51 +0000 | [diff] [blame] | 607 | // We attempt to map r by the inverse matrix and draw that. mapRect will |
| 608 | // map the four corners and bound them with a new rect. This will not |
| 609 | // produce a correct result for some perspective matrices. |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 610 | if (!this->getMatrix().hasPerspective()) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 611 | if (!fDrawState->getViewInverse(&inverse)) { |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 612 | GrPrintf("Could not invert matrix"); |
| 613 | return; |
| 614 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 615 | inverse.mapRect(&r); |
| 616 | } else { |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 617 | if (paint.getActiveMaskStageMask() || paint.getActiveStageMask()) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 618 | if (!fDrawState->getViewInverse(&inverse)) { |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 619 | GrPrintf("Could not invert matrix"); |
| 620 | return; |
| 621 | } |
| 622 | tmpPaint.set(paint); |
| 623 | tmpPaint.get()->preConcatActiveSamplerMatrices(inverse); |
| 624 | p = tmpPaint.get(); |
| 625 | } |
bsalomon@google.com | 4f83be8 | 2011-09-12 13:52:51 +0000 | [diff] [blame] | 626 | am.set(this, GrMatrix::I()); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 627 | } |
bsalomon@google.com | 4f83be8 | 2011-09-12 13:52:51 +0000 | [diff] [blame] | 628 | // by definition this fills the entire clip, no need for AA |
| 629 | if (paint.fAntiAlias) { |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 630 | if (!tmpPaint.isValid()) { |
| 631 | tmpPaint.set(paint); |
| 632 | p = tmpPaint.get(); |
| 633 | } |
| 634 | GrAssert(p == tmpPaint.get()); |
| 635 | tmpPaint.get()->fAntiAlias = false; |
bsalomon@google.com | 4f83be8 | 2011-09-12 13:52:51 +0000 | [diff] [blame] | 636 | } |
| 637 | this->drawRect(*p, r); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 638 | } |
| 639 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 640 | //////////////////////////////////////////////////////////////////////////////// |
| 641 | |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 642 | namespace { |
| 643 | inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) { |
| 644 | return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage(); |
| 645 | } |
| 646 | } |
| 647 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 648 | //////////////////////////////////////////////////////////////////////////////// |
| 649 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 650 | /* create a triangle strip that strokes the specified triangle. There are 8 |
| 651 | unique vertices, but we repreat the last 2 to close up. Alternatively we |
| 652 | could use an indices array, and then only send 8 verts, but not sure that |
| 653 | would be faster. |
| 654 | */ |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 655 | static void setStrokeRectStrip(GrPoint verts[10], GrRect rect, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 656 | GrScalar width) { |
| 657 | const GrScalar rad = GrScalarHalf(width); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 658 | rect.sort(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 659 | |
| 660 | verts[0].set(rect.fLeft + rad, rect.fTop + rad); |
| 661 | verts[1].set(rect.fLeft - rad, rect.fTop - rad); |
| 662 | verts[2].set(rect.fRight - rad, rect.fTop + rad); |
| 663 | verts[3].set(rect.fRight + rad, rect.fTop - rad); |
| 664 | verts[4].set(rect.fRight - rad, rect.fBottom - rad); |
| 665 | verts[5].set(rect.fRight + rad, rect.fBottom + rad); |
| 666 | verts[6].set(rect.fLeft + rad, rect.fBottom - rad); |
| 667 | verts[7].set(rect.fLeft - rad, rect.fBottom + rad); |
| 668 | verts[8] = verts[0]; |
| 669 | verts[9] = verts[1]; |
| 670 | } |
| 671 | |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 672 | /** |
| 673 | * Returns true if the rects edges are integer-aligned. |
| 674 | */ |
| 675 | static bool isIRect(const GrRect& r) { |
| 676 | return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) && |
| 677 | GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom); |
| 678 | } |
| 679 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 680 | static bool apply_aa_to_rect(GrDrawTarget* target, |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 681 | const GrRect& rect, |
| 682 | GrScalar width, |
| 683 | const GrMatrix* matrix, |
| 684 | GrMatrix* combinedMatrix, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 685 | GrRect* devRect, |
| 686 | bool* useVertexCoverage) { |
bsalomon@google.com | 2eba795 | 2012-01-12 13:47:37 +0000 | [diff] [blame] | 687 | // we use a simple coverage ramp to do aa on axis-aligned rects |
| 688 | // we check if the rect will be axis-aligned, and the rect won't land on |
| 689 | // integer coords. |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 690 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 691 | // we are keeping around the "tweak the alpha" trick because |
| 692 | // it is our only hope for the fixed-pipe implementation. |
| 693 | // In a shader implementation we can give a separate coverage input |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 694 | // TODO: remove this ugliness when we drop the fixed-pipe impl |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 695 | *useVertexCoverage = false; |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 696 | if (!target->canTweakAlphaForCoverage()) { |
bsalomon@google.com | 2eba795 | 2012-01-12 13:47:37 +0000 | [diff] [blame] | 697 | if (disable_coverage_aa_for_blend(target)) { |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 698 | #if GR_DEBUG |
bsalomon@google.com | 2eba795 | 2012-01-12 13:47:37 +0000 | [diff] [blame] | 699 | //GrPrintf("Turning off AA to correctly apply blend.\n"); |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 700 | #endif |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 701 | return false; |
bsalomon@google.com | 2eba795 | 2012-01-12 13:47:37 +0000 | [diff] [blame] | 702 | } else { |
| 703 | *useVertexCoverage = true; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 704 | } |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 705 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 706 | const GrDrawState& drawState = target->getDrawState(); |
| 707 | if (drawState.getRenderTarget()->isMultisampled()) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 708 | return false; |
| 709 | } |
| 710 | |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 711 | if (0 == width && target->willUseHWAALines()) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 712 | return false; |
| 713 | } |
| 714 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 715 | if (!drawState.getViewMatrix().preservesAxisAlignment()) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 716 | return false; |
| 717 | } |
| 718 | |
| 719 | if (NULL != matrix && |
| 720 | !matrix->preservesAxisAlignment()) { |
| 721 | return false; |
| 722 | } |
| 723 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 724 | *combinedMatrix = drawState.getViewMatrix(); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 725 | if (NULL != matrix) { |
| 726 | combinedMatrix->preConcat(*matrix); |
| 727 | GrAssert(combinedMatrix->preservesAxisAlignment()); |
| 728 | } |
| 729 | |
| 730 | combinedMatrix->mapRect(devRect, rect); |
| 731 | devRect->sort(); |
| 732 | |
| 733 | if (width < 0) { |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 734 | return !isIRect(*devRect); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 735 | } else { |
| 736 | return true; |
| 737 | } |
| 738 | } |
| 739 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 740 | void GrContext::drawRect(const GrPaint& paint, |
| 741 | const GrRect& rect, |
| 742 | GrScalar width, |
| 743 | const GrMatrix* matrix) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 744 | SK_TRACE_EVENT0("GrContext::drawRect"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 745 | |
| 746 | GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 747 | int stageMask = paint.getActiveStageMask(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 748 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 749 | GrRect devRect = rect; |
| 750 | GrMatrix combinedMatrix; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 751 | bool useVertexCoverage; |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 752 | bool needAA = paint.fAntiAlias && |
| 753 | !this->getRenderTarget()->isMultisampled(); |
| 754 | bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix, |
| 755 | &combinedMatrix, &devRect, |
| 756 | &useVertexCoverage); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 757 | |
| 758 | if (doAA) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 759 | GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 760 | if (width >= 0) { |
| 761 | GrVec strokeSize;; |
| 762 | if (width > 0) { |
| 763 | strokeSize.set(width, width); |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 764 | combinedMatrix.mapVectors(&strokeSize, 1); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 765 | strokeSize.setAbs(strokeSize); |
| 766 | } else { |
| 767 | strokeSize.set(GR_Scalar1, GR_Scalar1); |
| 768 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 769 | fAARectRenderer->strokeAARect(this->getGpu(), target, devRect, |
| 770 | strokeSize, useVertexCoverage); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 771 | } else { |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 772 | fAARectRenderer->fillAARect(this->getGpu(), target, |
| 773 | devRect, useVertexCoverage); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 774 | } |
| 775 | return; |
| 776 | } |
| 777 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 778 | if (width >= 0) { |
| 779 | // TODO: consider making static vertex buffers for these cases. |
| 780 | // Hairline could be done by just adding closing vertex to |
| 781 | // unitSquareVertexBuffer() |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 782 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL); |
| 783 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 784 | static const int worstCaseVertCount = 10; |
| 785 | GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0); |
| 786 | |
| 787 | if (!geo.succeeded()) { |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 788 | GrPrintf("Failed to get space for vertices!\n"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 789 | return; |
| 790 | } |
| 791 | |
| 792 | GrPrimitiveType primType; |
| 793 | int vertCount; |
| 794 | GrPoint* vertex = geo.positions(); |
| 795 | |
| 796 | if (width > 0) { |
| 797 | vertCount = 10; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 798 | primType = kTriangleStrip_GrPrimitiveType; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 799 | setStrokeRectStrip(vertex, rect, width); |
| 800 | } else { |
| 801 | // hairline |
| 802 | vertCount = 5; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 803 | primType = kLineStrip_GrPrimitiveType; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 804 | vertex[0].set(rect.fLeft, rect.fTop); |
| 805 | vertex[1].set(rect.fRight, rect.fTop); |
| 806 | vertex[2].set(rect.fRight, rect.fBottom); |
| 807 | vertex[3].set(rect.fLeft, rect.fBottom); |
| 808 | vertex[4].set(rect.fLeft, rect.fTop); |
| 809 | } |
| 810 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 811 | GrDrawState::AutoViewMatrixRestore avmr; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 812 | if (NULL != matrix) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 813 | GrDrawState* drawState = target->drawState(); |
| 814 | avmr.set(drawState); |
| 815 | drawState->preConcatViewMatrix(*matrix); |
| 816 | drawState->preConcatSamplerMatrices(stageMask, *matrix); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | target->drawNonIndexed(primType, 0, vertCount); |
| 820 | } else { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 821 | #if GR_STATIC_RECT_VB |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 822 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL); |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 823 | const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer(); |
| 824 | if (NULL == sqVB) { |
| 825 | GrPrintf("Failed to create static rect vb.\n"); |
| 826 | return; |
| 827 | } |
| 828 | target->setVertexSourceToBuffer(layout, sqVB); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 829 | GrDrawState* drawState = target->drawState(); |
| 830 | GrDrawState::AutoViewMatrixRestore avmr(drawState); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 831 | GrMatrix m; |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 832 | m.setAll(rect.width(), 0, rect.fLeft, |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 833 | 0, rect.height(), rect.fTop, |
| 834 | 0, 0, GrMatrix::I()[8]); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 835 | |
| 836 | if (NULL != matrix) { |
| 837 | m.postConcat(*matrix); |
| 838 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 839 | drawState->preConcatViewMatrix(m); |
| 840 | drawState->preConcatSamplerMatrices(stageMask, m); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 841 | |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 842 | target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 843 | #else |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 844 | target->drawSimpleRect(rect, matrix, stageMask); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 845 | #endif |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
| 849 | void GrContext::drawRectToRect(const GrPaint& paint, |
| 850 | const GrRect& dstRect, |
| 851 | const GrRect& srcRect, |
| 852 | const GrMatrix* dstMatrix, |
| 853 | const GrMatrix* srcMatrix) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 854 | SK_TRACE_EVENT0("GrContext::drawRectToRect"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 855 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 856 | // srcRect refers to paint's first texture |
| 857 | if (NULL == paint.getTexture(0)) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 858 | drawRect(paint, dstRect, -1, dstMatrix); |
| 859 | return; |
| 860 | } |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 861 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 862 | GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB); |
| 863 | |
| 864 | #if GR_STATIC_RECT_VB |
| 865 | GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 866 | GrDrawState* drawState = target->drawState(); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 867 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 868 | GrDrawState::AutoViewMatrixRestore avmr(drawState); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 869 | |
| 870 | GrMatrix m; |
| 871 | |
| 872 | m.setAll(dstRect.width(), 0, dstRect.fLeft, |
| 873 | 0, dstRect.height(), dstRect.fTop, |
| 874 | 0, 0, GrMatrix::I()[8]); |
| 875 | if (NULL != dstMatrix) { |
| 876 | m.postConcat(*dstMatrix); |
| 877 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 878 | drawState->preConcatViewMatrix(m); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 879 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 880 | // srcRect refers to first stage |
| 881 | int otherStageMask = paint.getActiveStageMask() & |
| 882 | (~(1 << GrPaint::kFirstTextureStage)); |
| 883 | if (otherStageMask) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 884 | drawState->preConcatSamplerMatrices(otherStageMask, m); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 885 | } |
| 886 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 887 | m.setAll(srcRect.width(), 0, srcRect.fLeft, |
| 888 | 0, srcRect.height(), srcRect.fTop, |
| 889 | 0, 0, GrMatrix::I()[8]); |
| 890 | if (NULL != srcMatrix) { |
| 891 | m.postConcat(*srcMatrix); |
| 892 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 893 | drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 894 | |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 895 | const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer(); |
| 896 | if (NULL == sqVB) { |
| 897 | GrPrintf("Failed to create static rect vb.\n"); |
| 898 | return; |
| 899 | } |
| 900 | target->setVertexSourceToBuffer(layout, sqVB); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 901 | target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 902 | #else |
| 903 | |
| 904 | GrDrawTarget* target; |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 905 | #if BATCH_RECT_TO_RECT |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 906 | target = this->prepareToDraw(paint, kBuffered_DrawCategory); |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 907 | #else |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 908 | target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
| 909 | #endif |
| 910 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 911 | const GrRect* srcRects[GrDrawState::kNumStages] = {NULL}; |
| 912 | const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL}; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 913 | srcRects[0] = &srcRect; |
| 914 | srcMatrices[0] = srcMatrix; |
| 915 | |
| 916 | target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices); |
| 917 | #endif |
| 918 | } |
| 919 | |
| 920 | void GrContext::drawVertices(const GrPaint& paint, |
| 921 | GrPrimitiveType primitiveType, |
| 922 | int vertexCount, |
| 923 | const GrPoint positions[], |
| 924 | const GrPoint texCoords[], |
| 925 | const GrColor colors[], |
| 926 | const uint16_t indices[], |
| 927 | int indexCount) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 928 | SK_TRACE_EVENT0("GrContext::drawVertices"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 929 | |
| 930 | GrDrawTarget::AutoReleaseGeometry geo; |
| 931 | |
| 932 | GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
| 933 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 934 | bool hasTexCoords[GrPaint::kTotalStages] = { |
| 935 | NULL != texCoords, // texCoordSrc provides explicit stage 0 coords |
| 936 | 0 // remaining stages use positions |
| 937 | }; |
| 938 | |
| 939 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 940 | |
| 941 | if (NULL != colors) { |
| 942 | layout |= GrDrawTarget::kColor_VertexLayoutBit; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 943 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 944 | int vertexSize = GrDrawTarget::VertexSize(layout); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 945 | |
| 946 | if (sizeof(GrPoint) != vertexSize) { |
| 947 | if (!geo.set(target, layout, vertexCount, 0)) { |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 948 | GrPrintf("Failed to get space for vertices!\n"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 949 | return; |
| 950 | } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 951 | int texOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 952 | int colorOffset; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 953 | GrDrawTarget::VertexSizeAndOffsetsByIdx(layout, |
| 954 | texOffsets, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 955 | &colorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 956 | NULL, |
| 957 | NULL); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 958 | void* curVertex = geo.vertices(); |
| 959 | |
| 960 | for (int i = 0; i < vertexCount; ++i) { |
| 961 | *((GrPoint*)curVertex) = positions[i]; |
| 962 | |
| 963 | if (texOffsets[0] > 0) { |
| 964 | *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i]; |
| 965 | } |
| 966 | if (colorOffset > 0) { |
| 967 | *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i]; |
| 968 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 969 | curVertex = (void*)((intptr_t)curVertex + vertexSize); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 970 | } |
| 971 | } else { |
| 972 | target->setVertexSourceToArray(layout, positions, vertexCount); |
| 973 | } |
| 974 | |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 975 | // we don't currently apply offscreen AA to this path. Need improved |
| 976 | // management of GrDrawTarget's geometry to avoid copying points per-tile. |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 977 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 978 | if (NULL != indices) { |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 979 | target->setIndexSourceToArray(indices, indexCount); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 980 | target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 981 | } else { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 982 | target->drawNonIndexed(primitiveType, 0, vertexCount); |
| 983 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 984 | } |
| 985 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 986 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 150d284 | 2012-01-12 20:19:56 +0000 | [diff] [blame] | 987 | namespace { |
| 988 | |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 989 | struct CircleVertex { |
| 990 | GrPoint fPos; |
| 991 | GrPoint fCenter; |
| 992 | GrScalar fOuterRadius; |
| 993 | GrScalar fInnerRadius; |
| 994 | }; |
| 995 | |
| 996 | /* Returns true if will map a circle to another circle. This can be true |
| 997 | * if the matrix only includes square-scale, rotation, translation. |
| 998 | */ |
| 999 | inline bool isSimilarityTransformation(const SkMatrix& matrix, |
| 1000 | SkScalar tol = SK_ScalarNearlyZero) { |
| 1001 | if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) { |
| 1002 | return true; |
| 1003 | } |
| 1004 | if (matrix.hasPerspective()) { |
| 1005 | return false; |
| 1006 | } |
| 1007 | |
| 1008 | SkScalar mx = matrix.get(SkMatrix::kMScaleX); |
| 1009 | SkScalar sx = matrix.get(SkMatrix::kMSkewX); |
| 1010 | SkScalar my = matrix.get(SkMatrix::kMScaleY); |
| 1011 | SkScalar sy = matrix.get(SkMatrix::kMSkewY); |
| 1012 | |
| 1013 | if (mx == 0 && sx == 0 && my == 0 && sy == 0) { |
| 1014 | return false; |
| 1015 | } |
| 1016 | |
| 1017 | // it has scales or skews, but it could also be rotation, check it out. |
| 1018 | SkVector vec[2]; |
| 1019 | vec[0].set(mx, sx); |
| 1020 | vec[1].set(sy, my); |
| 1021 | |
| 1022 | return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) && |
| 1023 | SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(), |
| 1024 | SkScalarSquare(tol)); |
| 1025 | } |
| 1026 | |
| 1027 | } |
| 1028 | |
| 1029 | // TODO: strokeWidth can't be larger than zero right now. |
| 1030 | // It will be fixed when drawPath() can handle strokes. |
| 1031 | void GrContext::drawOval(const GrPaint& paint, |
| 1032 | const GrRect& rect, |
| 1033 | SkScalar strokeWidth) { |
| 1034 | DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory : |
| 1035 | kUnbuffered_DrawCategory; |
| 1036 | GrDrawTarget* target = this->prepareToDraw(paint, category); |
| 1037 | GrDrawState* drawState = target->drawState(); |
| 1038 | GrMatrix vm = drawState->getViewMatrix(); |
| 1039 | |
| 1040 | if (!isSimilarityTransformation(vm) || |
| 1041 | !paint.fAntiAlias || |
| 1042 | rect.height() != rect.width()) { |
| 1043 | SkPath path; |
| 1044 | path.addOval(rect); |
| 1045 | GrPathFill fill = (strokeWidth == 0) ? |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1046 | kHairLine_GrPathFill : kWinding_GrPathFill; |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1047 | this->internalDrawPath(paint, path, fill, NULL); |
| 1048 | return; |
| 1049 | } |
| 1050 | |
| 1051 | const GrRenderTarget* rt = drawState->getRenderTarget(); |
| 1052 | if (NULL == rt) { |
| 1053 | return; |
| 1054 | } |
| 1055 | |
| 1056 | GrDrawTarget::AutoDeviceCoordDraw adcd(target, paint.getActiveStageMask()); |
| 1057 | |
| 1058 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL); |
| 1059 | layout |= GrDrawTarget::kEdge_VertexLayoutBit; |
| 1060 | GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout)); |
| 1061 | |
| 1062 | GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY()); |
| 1063 | GrScalar radius = SkScalarHalf(rect.width()); |
| 1064 | |
| 1065 | vm.mapPoints(¢er, 1); |
| 1066 | radius = vm.mapRadius(radius); |
| 1067 | |
| 1068 | GrScalar outerRadius = radius; |
| 1069 | GrScalar innerRadius = 0; |
| 1070 | SkScalar halfWidth = 0; |
| 1071 | if (strokeWidth == 0) { |
| 1072 | halfWidth = SkScalarHalf(SK_Scalar1); |
| 1073 | |
| 1074 | outerRadius += halfWidth; |
| 1075 | innerRadius = SkMaxScalar(0, radius - halfWidth); |
| 1076 | } |
| 1077 | |
| 1078 | GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0); |
| 1079 | if (!geo.succeeded()) { |
| 1080 | GrPrintf("Failed to get space for vertices!\n"); |
| 1081 | return; |
| 1082 | } |
| 1083 | |
| 1084 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices()); |
| 1085 | |
| 1086 | SkScalar L = center.fX - outerRadius; |
| 1087 | SkScalar R = center.fX + outerRadius; |
| 1088 | SkScalar T = center.fY - outerRadius; |
| 1089 | SkScalar B = center.fY + outerRadius; |
| 1090 | |
| 1091 | verts[0].fPos = SkPoint::Make(L, T); |
| 1092 | verts[1].fPos = SkPoint::Make(R, T); |
| 1093 | verts[2].fPos = SkPoint::Make(L, B); |
| 1094 | verts[3].fPos = SkPoint::Make(R, B); |
| 1095 | |
| 1096 | for (int i = 0; i < 4; ++i) { |
| 1097 | // this goes to fragment shader, it should be in y-points-up space. |
| 1098 | verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY); |
| 1099 | |
| 1100 | verts[i].fOuterRadius = outerRadius; |
| 1101 | verts[i].fInnerRadius = innerRadius; |
| 1102 | } |
| 1103 | |
| 1104 | drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1105 | target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4); |
bsalomon@google.com | 150d284 | 2012-01-12 20:19:56 +0000 | [diff] [blame] | 1106 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1107 | |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 1108 | void GrContext::drawPath(const GrPaint& paint, const SkPath& path, |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 1109 | GrPathFill fill, const GrPoint* translate) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1110 | |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1111 | if (path.isEmpty()) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1112 | if (GrIsFillInverted(fill)) { |
| 1113 | this->drawPaint(paint); |
| 1114 | } |
| 1115 | return; |
| 1116 | } |
| 1117 | |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1118 | SkRect ovalRect; |
| 1119 | if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) { |
| 1120 | if (translate) { |
| 1121 | ovalRect.offset(*translate); |
| 1122 | } |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1123 | SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1; |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1124 | this->drawOval(paint, ovalRect, width); |
| 1125 | return; |
| 1126 | } |
| 1127 | |
| 1128 | internalDrawPath(paint, path, fill, translate); |
| 1129 | } |
| 1130 | |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 1131 | void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path, |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1132 | GrPathFill fill, const GrPoint* translate) { |
| 1133 | |
bsalomon@google.com | fb4ce6f | 2012-03-14 13:27:54 +0000 | [diff] [blame] | 1134 | // Note that below we may sw-rasterize the path into a scratch texture. |
| 1135 | // Scratch textures can be recycled after they are returned to the texture |
| 1136 | // cache. This presents a potential hazard for buffered drawing. However, |
| 1137 | // the writePixels that uploads to the scratch will perform a flush so we're |
| 1138 | // OK. |
| 1139 | DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory : |
| 1140 | kUnbuffered_DrawCategory; |
| 1141 | GrDrawTarget* target = this->prepareToDraw(paint, category); |
bsalomon@google.com | 150d284 | 2012-01-12 20:19:56 +0000 | [diff] [blame] | 1142 | GrDrawState::StageMask stageMask = paint.getActiveStageMask(); |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 1143 | |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 1144 | bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled(); |
| 1145 | |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 1146 | // An Assumption here is that path renderer would use some form of tweaking |
| 1147 | // the src color (either the input alpha or in the frag shader) to implement |
| 1148 | // aa. If we have some future driver-mojo path AA that can do the right |
| 1149 | // thing WRT to the blend then we'll need some query on the PR. |
| 1150 | if (disable_coverage_aa_for_blend(target)) { |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 1151 | #if GR_DEBUG |
bsalomon@google.com | 979432b | 2011-11-05 21:38:22 +0000 | [diff] [blame] | 1152 | //GrPrintf("Turning off AA to correctly apply blend.\n"); |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 1153 | #endif |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 1154 | prAA = false; |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 1155 | } |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 1156 | |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1157 | GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1158 | if (NULL == pr) { |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 1159 | #if GR_DEBUG |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1160 | GrPrintf("Unable to find path renderer compatible with path.\n"); |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 1161 | #endif |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1162 | return; |
| 1163 | } |
| 1164 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1165 | pr->drawPath(path, fill, translate, target, stageMask, prAA); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1166 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1167 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1168 | //////////////////////////////////////////////////////////////////////////////// |
| 1169 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1170 | void GrContext::flush(int flagsBitfield) { |
| 1171 | if (kDiscard_FlushBit & flagsBitfield) { |
| 1172 | fDrawBuffer->reset(); |
| 1173 | } else { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1174 | this->flushDrawBuffer(); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1175 | } |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1176 | if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1177 | fGpu->forceRenderTargetFlush(); |
| 1178 | } |
| 1179 | } |
| 1180 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1181 | void GrContext::flushDrawBuffer() { |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 1182 | if (fDrawBuffer) { |
robertphillips@google.com | 58b3818 | 2012-05-03 16:29:41 +0000 | [diff] [blame] | 1183 | // With addition of the AA clip path, flushing the draw buffer can |
| 1184 | // result in the generation of an AA clip mask. During this |
| 1185 | // process the SW path renderer may be invoked which recusively |
| 1186 | // calls this method (via internalWriteTexturePixels) creating |
| 1187 | // infinite recursion |
| 1188 | GrInOrderDrawBuffer* temp = fDrawBuffer; |
| 1189 | fDrawBuffer = NULL; |
| 1190 | |
| 1191 | temp->flushTo(fGpu); |
| 1192 | |
| 1193 | fDrawBuffer = temp; |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 1194 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1197 | void GrContext::internalWriteTexturePixels(GrTexture* texture, |
| 1198 | int left, int top, |
| 1199 | int width, int height, |
| 1200 | GrPixelConfig config, |
| 1201 | const void* buffer, |
| 1202 | size_t rowBytes, |
| 1203 | uint32_t flags) { |
| 1204 | SK_TRACE_EVENT0("GrContext::writeTexturePixels"); |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1205 | ASSERT_OWNED_RESOURCE(texture); |
| 1206 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1207 | if (!(kDontFlush_PixelOpsFlag & flags)) { |
| 1208 | this->flush(); |
| 1209 | } |
| 1210 | // TODO: use scratch texture to perform conversion |
| 1211 | if (GrPixelConfigIsUnpremultiplied(texture->config()) != |
| 1212 | GrPixelConfigIsUnpremultiplied(config)) { |
| 1213 | return; |
| 1214 | } |
| 1215 | |
| 1216 | fGpu->writeTexturePixels(texture, left, top, width, height, |
| 1217 | config, buffer, rowBytes); |
| 1218 | } |
| 1219 | |
| 1220 | bool GrContext::internalReadTexturePixels(GrTexture* texture, |
| 1221 | int left, int top, |
| 1222 | int width, int height, |
| 1223 | GrPixelConfig config, |
| 1224 | void* buffer, |
| 1225 | size_t rowBytes, |
| 1226 | uint32_t flags) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 1227 | SK_TRACE_EVENT0("GrContext::readTexturePixels"); |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1228 | ASSERT_OWNED_RESOURCE(texture); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1229 | |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1230 | // TODO: code read pixels for textures that aren't also rendertargets |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1231 | GrRenderTarget* target = texture->asRenderTarget(); |
| 1232 | if (NULL != target) { |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1233 | return this->internalReadRenderTargetPixels(target, |
| 1234 | left, top, width, height, |
| 1235 | config, buffer, rowBytes, |
| 1236 | flags); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1237 | } else { |
| 1238 | return false; |
| 1239 | } |
| 1240 | } |
| 1241 | |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1242 | #include "SkConfig8888.h" |
| 1243 | |
| 1244 | namespace { |
| 1245 | /** |
| 1246 | * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel |
| 1247 | * formats are representable as Config8888 and so the function returns false |
| 1248 | * if the GrPixelConfig has no equivalent Config8888. |
| 1249 | */ |
| 1250 | bool grconfig_to_config8888(GrPixelConfig config, |
| 1251 | SkCanvas::Config8888* config8888) { |
| 1252 | switch (config) { |
| 1253 | case kRGBA_8888_PM_GrPixelConfig: |
| 1254 | *config8888 = SkCanvas::kRGBA_Premul_Config8888; |
| 1255 | return true; |
| 1256 | case kRGBA_8888_UPM_GrPixelConfig: |
| 1257 | *config8888 = SkCanvas::kRGBA_Unpremul_Config8888; |
| 1258 | return true; |
| 1259 | case kBGRA_8888_PM_GrPixelConfig: |
| 1260 | *config8888 = SkCanvas::kBGRA_Premul_Config8888; |
| 1261 | return true; |
| 1262 | case kBGRA_8888_UPM_GrPixelConfig: |
| 1263 | *config8888 = SkCanvas::kBGRA_Unpremul_Config8888; |
| 1264 | return true; |
| 1265 | default: |
| 1266 | return false; |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1271 | bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target, |
| 1272 | int left, int top, |
| 1273 | int width, int height, |
| 1274 | GrPixelConfig config, |
| 1275 | void* buffer, |
| 1276 | size_t rowBytes, |
| 1277 | uint32_t flags) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 1278 | SK_TRACE_EVENT0("GrContext::readRenderTargetPixels"); |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1279 | ASSERT_OWNED_RESOURCE(target); |
| 1280 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1281 | if (NULL == target) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1282 | target = fDrawState->getRenderTarget(); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1283 | if (NULL == target) { |
| 1284 | return false; |
| 1285 | } |
| 1286 | } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1287 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1288 | if (!(kDontFlush_PixelOpsFlag & flags)) { |
| 1289 | this->flush(); |
| 1290 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1291 | |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1292 | if (!GrPixelConfigIsUnpremultiplied(target->config()) && |
| 1293 | GrPixelConfigIsUnpremultiplied(config) && |
| 1294 | !fGpu->canPreserveReadWriteUnpremulPixels()) { |
| 1295 | SkCanvas::Config8888 srcConfig8888, dstConfig8888; |
| 1296 | if (!grconfig_to_config8888(target->config(), &srcConfig8888) || |
| 1297 | !grconfig_to_config8888(config, &dstConfig8888)) { |
| 1298 | return false; |
| 1299 | } |
| 1300 | // do read back using target's own config |
| 1301 | this->internalReadRenderTargetPixels(target, |
| 1302 | left, top, |
| 1303 | width, height, |
| 1304 | target->config(), |
| 1305 | buffer, rowBytes, |
| 1306 | kDontFlush_PixelOpsFlag); |
| 1307 | // sw convert the pixels to unpremul config |
| 1308 | uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer); |
| 1309 | SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888, |
| 1310 | pixels, rowBytes, srcConfig8888, |
| 1311 | width, height); |
| 1312 | return true; |
| 1313 | } |
| 1314 | |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1315 | GrTexture* src = target->asTexture(); |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1316 | bool swapRAndB = NULL != src && |
| 1317 | fGpu->preferredReadPixelsConfig(config) == |
| 1318 | GrPixelConfigSwapRAndB(config); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1319 | |
| 1320 | bool flipY = NULL != src && |
| 1321 | fGpu->readPixelsWillPayForYFlip(target, left, top, |
| 1322 | width, height, config, |
| 1323 | rowBytes); |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1324 | bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) && |
| 1325 | GrPixelConfigIsUnpremultiplied(config)); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1326 | |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1327 | if (NULL == src && alphaConversion) { |
| 1328 | // we should fallback to cpu conversion here. This could happen when |
| 1329 | // we were given an external render target by the client that is not |
| 1330 | // also a texture (e.g. FBO 0 in GL) |
| 1331 | return false; |
| 1332 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1333 | // 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] | 1334 | GrAutoScratchTexture ast; |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1335 | if (flipY || swapRAndB || alphaConversion) { |
| 1336 | GrAssert(NULL != src); |
| 1337 | if (swapRAndB) { |
| 1338 | config = GrPixelConfigSwapRAndB(config); |
| 1339 | GrAssert(kUnknown_GrPixelConfig != config); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1340 | } |
| 1341 | // Make the scratch a render target because we don't have a robust |
| 1342 | // readTexturePixels as of yet (it calls this function). |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 1343 | GrTextureDesc desc; |
| 1344 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 1345 | desc.fWidth = width; |
| 1346 | desc.fHeight = height; |
| 1347 | desc.fConfig = config; |
bsalomon@google.com | c4ff22a | 2011-11-10 21:56:21 +0000 | [diff] [blame] | 1348 | |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 1349 | // When a full readback is faster than a partial we could always make |
| 1350 | // the scratch exactly match the passed rect. However, if we see many |
| 1351 | // different size rectangles we will trash our texture cache and pay the |
| 1352 | // cost of creating and destroying many textures. So, we only request |
| 1353 | // an exact match when the caller is reading an entire RT. |
| 1354 | ScratchTexMatch match = kApprox_ScratchTexMatch; |
| 1355 | if (0 == left && |
| 1356 | 0 == top && |
| 1357 | target->width() == width && |
| 1358 | target->height() == height && |
| 1359 | fGpu->fullReadPixelsIsFasterThanPartial()) { |
| 1360 | match = kExact_ScratchTexMatch; |
| 1361 | } |
| 1362 | ast.set(this, desc, match); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1363 | GrTexture* texture = ast.texture(); |
| 1364 | if (!texture) { |
| 1365 | return false; |
| 1366 | } |
| 1367 | target = texture->asRenderTarget(); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1368 | GrAssert(NULL != target); |
| 1369 | |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 1370 | GrDrawTarget::AutoStateRestore asr(fGpu, |
| 1371 | GrDrawTarget::kReset_ASRInit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1372 | GrDrawState* drawState = fGpu->drawState(); |
| 1373 | drawState->setRenderTarget(target); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1374 | |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1375 | GrMatrix matrix; |
| 1376 | if (flipY) { |
| 1377 | matrix.setTranslate(SK_Scalar1 * left, |
| 1378 | SK_Scalar1 * (top + height)); |
| 1379 | matrix.set(GrMatrix::kMScaleY, -GR_Scalar1); |
| 1380 | } else { |
| 1381 | matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top); |
| 1382 | } |
| 1383 | matrix.postIDiv(src->width(), src->height()); |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 1384 | drawState->sampler(0)->reset(matrix); |
| 1385 | drawState->sampler(0)->setRAndBSwap(swapRAndB); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1386 | drawState->setTexture(0, src); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1387 | GrRect rect; |
| 1388 | rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height); |
| 1389 | fGpu->drawSimpleRect(rect, NULL, 0x1); |
| 1390 | left = 0; |
| 1391 | top = 0; |
| 1392 | } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1393 | return fGpu->readPixels(target, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1394 | left, top, width, height, |
| 1395 | config, buffer, rowBytes, flipY); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 1398 | void GrContext::resolveRenderTarget(GrRenderTarget* target) { |
| 1399 | GrAssert(target); |
| 1400 | ASSERT_OWNED_RESOURCE(target); |
| 1401 | // In the future we may track whether there are any pending draws to this |
| 1402 | // target. We don't today so we always perform a flush. We don't promise |
| 1403 | // this to our clients, though. |
| 1404 | this->flush(); |
| 1405 | fGpu->resolveRenderTarget(target); |
| 1406 | } |
| 1407 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1408 | void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) { |
| 1409 | if (NULL == src || NULL == dst) { |
| 1410 | return; |
| 1411 | } |
| 1412 | ASSERT_OWNED_RESOURCE(src); |
| 1413 | |
twiz@google.com | 1ac87ff | 2012-04-27 19:39:33 +0000 | [diff] [blame] | 1414 | // Writes pending to the source texture are not tracked, so a flush |
| 1415 | // is required to ensure that the copy captures the most recent contents |
| 1416 | // of the source texture. See similar behaviour in |
| 1417 | // GrContext::resolveRenderTarget. |
| 1418 | this->flush(); |
| 1419 | |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 1420 | GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1421 | GrDrawState* drawState = fGpu->drawState(); |
| 1422 | drawState->setRenderTarget(dst); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1423 | GrMatrix sampleM; |
| 1424 | sampleM.setIDiv(src->width(), src->height()); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1425 | drawState->setTexture(0, src); |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 1426 | drawState->sampler(0)->reset(sampleM); |
bsalomon@google.com | 5db3b6c | 2012-01-12 20:38:57 +0000 | [diff] [blame] | 1427 | SkRect rect = SkRect::MakeXYWH(0, 0, |
| 1428 | SK_Scalar1 * src->width(), |
| 1429 | SK_Scalar1 * src->height()); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1430 | fGpu->drawSimpleRect(rect, NULL, 1 << 0); |
| 1431 | } |
| 1432 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1433 | void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target, |
| 1434 | int left, int top, |
| 1435 | int width, int height, |
| 1436 | GrPixelConfig config, |
| 1437 | const void* buffer, |
| 1438 | size_t rowBytes, |
| 1439 | uint32_t flags) { |
| 1440 | SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels"); |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1441 | ASSERT_OWNED_RESOURCE(target); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1442 | |
| 1443 | if (NULL == target) { |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 1444 | target = fDrawState->getRenderTarget(); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1445 | if (NULL == target) { |
| 1446 | return; |
| 1447 | } |
| 1448 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1449 | |
| 1450 | // TODO: when underlying api has a direct way to do this we should use it |
| 1451 | // (e.g. glDrawPixels on desktop GL). |
| 1452 | |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1453 | // If the RT is also a texture and we don't have to do PM/UPM conversion |
| 1454 | // then take the texture path, which we expect to be at least as fast or |
| 1455 | // faster since it doesn't use an intermediate texture as we do below. |
| 1456 | |
| 1457 | #if !GR_MAC_BUILD |
| 1458 | // At least some drivers on the Mac get confused when glTexImage2D is called |
| 1459 | // on a texture attached to an FBO. The FBO still sees the old image. TODO: |
| 1460 | // determine what OS versions and/or HW is affected. |
| 1461 | if (NULL != target->asTexture() && |
| 1462 | GrPixelConfigIsUnpremultiplied(target->config()) == |
| 1463 | GrPixelConfigIsUnpremultiplied(config)) { |
| 1464 | |
| 1465 | this->internalWriteTexturePixels(target->asTexture(), |
| 1466 | left, top, width, height, |
| 1467 | config, buffer, rowBytes, flags); |
| 1468 | return; |
| 1469 | } |
| 1470 | #endif |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1471 | if (!GrPixelConfigIsUnpremultiplied(target->config()) && |
| 1472 | GrPixelConfigIsUnpremultiplied(config) && |
| 1473 | !fGpu->canPreserveReadWriteUnpremulPixels()) { |
| 1474 | SkCanvas::Config8888 srcConfig8888, dstConfig8888; |
| 1475 | if (!grconfig_to_config8888(config, &srcConfig8888) || |
| 1476 | !grconfig_to_config8888(target->config(), &dstConfig8888)) { |
| 1477 | return; |
| 1478 | } |
| 1479 | // allocate a tmp buffer and sw convert the pixels to premul |
| 1480 | SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height); |
| 1481 | const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer); |
| 1482 | SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888, |
| 1483 | src, rowBytes, srcConfig8888, |
| 1484 | width, height); |
| 1485 | // upload the already premul pixels |
| 1486 | this->internalWriteRenderTargetPixels(target, |
| 1487 | left, top, |
| 1488 | width, height, |
| 1489 | target->config(), |
| 1490 | tmpPixels, 4 * width, flags); |
| 1491 | return; |
| 1492 | } |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1493 | |
| 1494 | bool swapRAndB = fGpu->preferredReadPixelsConfig(config) == |
| 1495 | GrPixelConfigSwapRAndB(config); |
| 1496 | if (swapRAndB) { |
| 1497 | config = GrPixelConfigSwapRAndB(config); |
| 1498 | } |
| 1499 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 1500 | GrTextureDesc desc; |
| 1501 | desc.fWidth = width; |
| 1502 | desc.fHeight = height; |
| 1503 | desc.fConfig = config; |
| 1504 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1505 | GrAutoScratchTexture ast(this, desc); |
| 1506 | GrTexture* texture = ast.texture(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1507 | if (NULL == texture) { |
| 1508 | return; |
| 1509 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1510 | this->internalWriteTexturePixels(texture, 0, 0, width, height, |
| 1511 | config, buffer, rowBytes, flags); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1512 | |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 1513 | GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1514 | GrDrawState* drawState = fGpu->drawState(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1515 | |
| 1516 | GrMatrix matrix; |
| 1517 | matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top)); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1518 | drawState->setViewMatrix(matrix); |
| 1519 | drawState->setRenderTarget(target); |
| 1520 | drawState->setTexture(0, texture); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1521 | |
bsalomon@google.com | 5c63865 | 2011-07-18 19:31:59 +0000 | [diff] [blame] | 1522 | matrix.setIDiv(texture->width(), texture->height()); |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 1523 | drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode, |
| 1524 | GrSamplerState::kNearest_Filter, |
| 1525 | matrix); |
| 1526 | drawState->sampler(0)->setRAndBSwap(swapRAndB); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1527 | |
| 1528 | GrVertexLayout layout = GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0); |
| 1529 | static const int VCOUNT = 4; |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 1530 | // TODO: Use GrGpu::drawRect here |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1531 | GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0); |
| 1532 | if (!geo.succeeded()) { |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 1533 | GrPrintf("Failed to get space for vertices!\n"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1534 | return; |
| 1535 | } |
| 1536 | ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1537 | fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1538 | } |
| 1539 | //////////////////////////////////////////////////////////////////////////////// |
| 1540 | |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1541 | void GrContext::setPaint(const GrPaint& paint) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1542 | |
| 1543 | for (int i = 0; i < GrPaint::kMaxTextures; ++i) { |
| 1544 | int s = i + GrPaint::kFirstTextureStage; |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1545 | fDrawState->setTexture(s, paint.getTexture(i)); |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1546 | ASSERT_OWNED_RESOURCE(paint.getTexture(i)); |
bsalomon@google.com | f864ec4 | 2011-12-12 21:57:03 +0000 | [diff] [blame] | 1547 | if (paint.getTexture(i)) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1548 | *fDrawState->sampler(s) = paint.getTextureSampler(i); |
bsalomon@google.com | f864ec4 | 2011-12-12 21:57:03 +0000 | [diff] [blame] | 1549 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1552 | fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1553 | |
| 1554 | for (int i = 0; i < GrPaint::kMaxMasks; ++i) { |
| 1555 | int s = i + GrPaint::kFirstMaskStage; |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1556 | fDrawState->setTexture(s, paint.getMask(i)); |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1557 | ASSERT_OWNED_RESOURCE(paint.getMask(i)); |
bsalomon@google.com | f864ec4 | 2011-12-12 21:57:03 +0000 | [diff] [blame] | 1558 | if (paint.getMask(i)) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1559 | *fDrawState->sampler(s) = paint.getMaskSampler(i); |
bsalomon@google.com | f864ec4 | 2011-12-12 21:57:03 +0000 | [diff] [blame] | 1560 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1561 | } |
bsalomon@google.com | 26936d0 | 2012-03-19 13:06:19 +0000 | [diff] [blame] | 1562 | |
| 1563 | // disable all stages not accessible via the paint |
| 1564 | for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1565 | fDrawState->setTexture(s, NULL); |
bsalomon@google.com | 26936d0 | 2012-03-19 13:06:19 +0000 | [diff] [blame] | 1566 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1567 | |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1568 | fDrawState->setColor(paint.fColor); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1569 | |
| 1570 | if (paint.fDither) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1571 | fDrawState->enableState(GrDrawState::kDither_StateBit); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1572 | } else { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1573 | fDrawState->disableState(GrDrawState::kDither_StateBit); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1574 | } |
| 1575 | if (paint.fAntiAlias) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1576 | fDrawState->enableState(GrDrawState::kHWAntialias_StateBit); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1577 | } else { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1578 | fDrawState->disableState(GrDrawState::kHWAntialias_StateBit); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1579 | } |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1580 | if (paint.fColorMatrixEnabled) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1581 | fDrawState->enableState(GrDrawState::kColorMatrix_StateBit); |
| 1582 | fDrawState->setColorMatrix(paint.fColorMatrix); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1583 | } else { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1584 | fDrawState->disableState(GrDrawState::kColorMatrix_StateBit); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1585 | } |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1586 | fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff); |
| 1587 | fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode); |
| 1588 | fDrawState->setCoverage(paint.fCoverage); |
reed@google.com | 4b2d3f3 | 2012-05-15 18:05:50 +0000 | [diff] [blame] | 1589 | #if GR_DEBUG_PARTIAL_COVERAGE_CHECK |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 1590 | if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) && |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1591 | !fGpu->canApplyCoverage()) { |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 1592 | GrPrintf("Partial pixel coverage will be incorrectly blended.\n"); |
| 1593 | } |
bsalomon@google.com | 95cd7bd | 2012-03-28 15:35:05 +0000 | [diff] [blame] | 1594 | #endif |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1597 | GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1598 | DrawCategory category) { |
| 1599 | if (category != fLastDrawCategory) { |
bsalomon@google.com | fb4ce6f | 2012-03-14 13:27:54 +0000 | [diff] [blame] | 1600 | this->flushDrawBuffer(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1601 | fLastDrawCategory = category; |
| 1602 | } |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1603 | this->setPaint(paint); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1604 | GrDrawTarget* target = fGpu; |
| 1605 | switch (category) { |
bsalomon@google.com | 193395c | 2012-03-30 17:35:12 +0000 | [diff] [blame] | 1606 | case kUnbuffered_DrawCategory: |
| 1607 | target = fGpu; |
| 1608 | break; |
| 1609 | case kBuffered_DrawCategory: |
| 1610 | target = fDrawBuffer; |
| 1611 | fDrawBuffer->setClip(fGpu->getClip()); |
| 1612 | break; |
| 1613 | default: |
| 1614 | GrCrash("Unexpected DrawCategory."); |
| 1615 | break; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1616 | } |
| 1617 | return target; |
| 1618 | } |
| 1619 | |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1620 | /* |
| 1621 | * This method finds a path renderer that can draw the specified path on |
| 1622 | * the provided target. |
| 1623 | * Due to its expense, the software path renderer has split out so it can |
| 1624 | * can be individually allowed/disallowed via the "allowSW" boolean. |
| 1625 | */ |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 1626 | GrPathRenderer* GrContext::getPathRenderer(const SkPath& path, |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 1627 | GrPathFill fill, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1628 | const GrDrawTarget* target, |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1629 | bool antiAlias, |
| 1630 | bool allowSW) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1631 | if (NULL == fPathRendererChain) { |
| 1632 | fPathRendererChain = |
| 1633 | new GrPathRendererChain(this, GrPathRendererChain::kNone_UsageFlag); |
| 1634 | } |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1635 | |
| 1636 | GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill, |
| 1637 | target, |
| 1638 | antiAlias); |
| 1639 | |
| 1640 | if (NULL == pr && allowSW) { |
| 1641 | if (NULL == fSoftwarePathRenderer) { |
| 1642 | fSoftwarePathRenderer = new GrSoftwarePathRenderer(this); |
| 1643 | } |
| 1644 | |
| 1645 | pr = fSoftwarePathRenderer; |
| 1646 | } |
| 1647 | |
| 1648 | return pr; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1651 | //////////////////////////////////////////////////////////////////////////////// |
| 1652 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1653 | void GrContext::setRenderTarget(GrRenderTarget* target) { |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1654 | ASSERT_OWNED_RESOURCE(target); |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1655 | if (fDrawState->getRenderTarget() != target) { |
bsalomon@google.com | fb4ce6f | 2012-03-14 13:27:54 +0000 | [diff] [blame] | 1656 | this->flush(false); |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1657 | fDrawState->setRenderTarget(target); |
bsalomon@google.com | fb4ce6f | 2012-03-14 13:27:54 +0000 | [diff] [blame] | 1658 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | GrRenderTarget* GrContext::getRenderTarget() { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1662 | return fDrawState->getRenderTarget(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | const GrRenderTarget* GrContext::getRenderTarget() const { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1666 | return fDrawState->getRenderTarget(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 1669 | bool GrContext::isConfigRenderable(GrPixelConfig config) const { |
| 1670 | return fGpu->isConfigRenderable(config); |
| 1671 | } |
| 1672 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1673 | const GrMatrix& GrContext::getMatrix() const { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1674 | return fDrawState->getViewMatrix(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | void GrContext::setMatrix(const GrMatrix& m) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1678 | fDrawState->setViewMatrix(m); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | void GrContext::concatMatrix(const GrMatrix& m) const { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1682 | fDrawState->preConcatViewMatrix(m); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) { |
| 1686 | intptr_t mask = 1 << shift; |
| 1687 | if (pred) { |
| 1688 | bits |= mask; |
| 1689 | } else { |
| 1690 | bits &= ~mask; |
| 1691 | } |
| 1692 | return bits; |
| 1693 | } |
| 1694 | |
bsalomon@google.com | 583a1e3 | 2011-08-17 13:42:46 +0000 | [diff] [blame] | 1695 | GrContext::GrContext(GrGpu* gpu) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1696 | fGpu = gpu; |
| 1697 | fGpu->ref(); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1698 | fGpu->setContext(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1699 | |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1700 | fDrawState = new GrDrawState(); |
| 1701 | fGpu->setDrawState(fDrawState); |
| 1702 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1703 | fPathRendererChain = NULL; |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1704 | fSoftwarePathRenderer = NULL; |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 1705 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1706 | fTextureCache = new GrResourceCache(MAX_TEXTURE_CACHE_COUNT, |
| 1707 | MAX_TEXTURE_CACHE_BYTES); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1708 | fFontCache = new GrFontCache(fGpu); |
| 1709 | |
| 1710 | fLastDrawCategory = kUnbuffered_DrawCategory; |
| 1711 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1712 | fDrawBuffer = NULL; |
| 1713 | fDrawBufferVBAllocPool = NULL; |
| 1714 | fDrawBufferIBAllocPool = NULL; |
| 1715 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 1716 | fAARectRenderer = new GrAARectRenderer; |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1717 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1718 | this->setupDrawBuffer(); |
| 1719 | } |
| 1720 | |
| 1721 | void GrContext::setupDrawBuffer() { |
| 1722 | |
| 1723 | GrAssert(NULL == fDrawBuffer); |
| 1724 | GrAssert(NULL == fDrawBufferVBAllocPool); |
| 1725 | GrAssert(NULL == fDrawBufferIBAllocPool); |
| 1726 | |
bsalomon@google.com | 92edd31 | 2012-04-04 21:40:21 +0000 | [diff] [blame] | 1727 | #if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1728 | fDrawBufferVBAllocPool = |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1729 | new GrVertexBufferAllocPool(fGpu, false, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1730 | DRAW_BUFFER_VBPOOL_BUFFER_SIZE, |
| 1731 | DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS); |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1732 | fDrawBufferIBAllocPool = |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1733 | new GrIndexBufferAllocPool(fGpu, false, |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1734 | DRAW_BUFFER_IBPOOL_BUFFER_SIZE, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1735 | DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS); |
| 1736 | |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 1737 | fDrawBuffer = new GrInOrderDrawBuffer(fGpu, |
| 1738 | fDrawBufferVBAllocPool, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1739 | fDrawBufferIBAllocPool); |
bsalomon@google.com | 3c4d032 | 2012-04-03 18:04:51 +0000 | [diff] [blame] | 1740 | #endif |
| 1741 | |
| 1742 | #if BATCH_RECT_TO_RECT |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1743 | fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer()); |
bsalomon@google.com | 3c4d032 | 2012-04-03 18:04:51 +0000 | [diff] [blame] | 1744 | #endif |
bsalomon@google.com | fb4ce6f | 2012-03-14 13:27:54 +0000 | [diff] [blame] | 1745 | fDrawBuffer->setAutoFlushTarget(fGpu); |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1746 | fDrawBuffer->setDrawState(fDrawState); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1749 | GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1750 | #if DEFER_TEXT_RENDERING |
bsalomon@google.com | 193395c | 2012-03-30 17:35:12 +0000 | [diff] [blame] | 1751 | return prepareToDraw(paint, kBuffered_DrawCategory); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1752 | #else |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1753 | return prepareToDraw(paint, kUnbuffered_DrawCategory); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1754 | #endif |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | const GrIndexBuffer* GrContext::getQuadIndexBuffer() const { |
| 1758 | return fGpu->getQuadIndexBuffer(); |
| 1759 | } |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 1760 | |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1761 | GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture, |
| 1762 | GrAutoScratchTexture* temp1, |
| 1763 | GrAutoScratchTexture* temp2, |
| 1764 | const SkRect& rect, |
| 1765 | float sigmaX, float sigmaY) { |
senorblanco@chromium.org | ceb4414 | 2012-03-05 20:53:36 +0000 | [diff] [blame] | 1766 | ASSERT_OWNED_RESOURCE(srcTexture); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1767 | GrRenderTarget* oldRenderTarget = this->getRenderTarget(); |
| 1768 | GrClip oldClip = this->getClip(); |
| 1769 | GrTexture* origTexture = srcTexture; |
| 1770 | GrAutoMatrix avm(this, GrMatrix::I()); |
| 1771 | SkIRect clearRect; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1772 | int scaleFactorX, radiusX; |
| 1773 | int scaleFactorY, radiusY; |
| 1774 | sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX); |
| 1775 | sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY); |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1776 | |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1777 | SkRect srcRect(rect); |
| 1778 | scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); |
| 1779 | srcRect.roundOut(); |
robertphillips@google.com | 8637a36 | 2012-04-10 18:32:35 +0000 | [diff] [blame] | 1780 | scale_rect(&srcRect, static_cast<float>(scaleFactorX), |
| 1781 | static_cast<float>(scaleFactorY)); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1782 | this->setClip(srcRect); |
| 1783 | |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 1784 | GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() || |
| 1785 | kRGBA_8888_PM_GrPixelConfig == srcTexture->config() || |
| 1786 | kAlpha_8_GrPixelConfig == srcTexture->config()); |
| 1787 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 1788 | GrTextureDesc desc; |
| 1789 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
| 1790 | desc.fWidth = SkScalarFloorToInt(srcRect.width()); |
| 1791 | desc.fHeight = SkScalarFloorToInt(srcRect.height()); |
| 1792 | desc.fConfig = srcTexture->config(); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1793 | |
| 1794 | temp1->set(this, desc); |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 1795 | if (temp2) { |
| 1796 | temp2->set(this, desc); |
| 1797 | } |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1798 | |
| 1799 | GrTexture* dstTexture = temp1->texture(); |
| 1800 | GrPaint paint; |
| 1801 | paint.reset(); |
| 1802 | paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter); |
| 1803 | |
| 1804 | for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { |
| 1805 | paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(), |
| 1806 | srcTexture->height()); |
| 1807 | this->setRenderTarget(dstTexture->asRenderTarget()); |
| 1808 | SkRect dstRect(srcRect); |
| 1809 | scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f, |
| 1810 | i < scaleFactorY ? 0.5f : 1.0f); |
| 1811 | paint.setTexture(0, srcTexture); |
| 1812 | this->drawRectToRect(paint, dstRect, srcRect); |
| 1813 | srcRect = dstRect; |
| 1814 | SkTSwap(srcTexture, dstTexture); |
| 1815 | // If temp2 is non-NULL, don't render back to origTexture |
| 1816 | if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture(); |
| 1817 | } |
| 1818 | |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1819 | SkIRect srcIRect; |
| 1820 | srcRect.roundOut(&srcIRect); |
| 1821 | |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1822 | if (sigmaX > 0.0f) { |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1823 | if (scaleFactorX > 1) { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1824 | // Clear out a radius to the right of the srcRect to prevent the |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1825 | // X convolution from reading garbage. |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1826 | clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1827 | radiusX, srcIRect.height()); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1828 | this->clear(&clearRect, 0x0); |
| 1829 | } |
| 1830 | |
| 1831 | this->setRenderTarget(dstTexture->asRenderTarget()); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1832 | convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX, |
| 1833 | Gr1DKernelEffect::kX_Direction); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1834 | SkTSwap(srcTexture, dstTexture); |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 1835 | if (temp2 && dstTexture == origTexture) { |
| 1836 | dstTexture = temp2->texture(); |
| 1837 | } |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | if (sigmaY > 0.0f) { |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1841 | if (scaleFactorY > 1 || sigmaX > 0.0f) { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1842 | // Clear out a radius below the srcRect to prevent the Y |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1843 | // convolution from reading garbage. |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1844 | clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1845 | srcIRect.width(), radiusY); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1846 | this->clear(&clearRect, 0x0); |
| 1847 | } |
| 1848 | |
| 1849 | this->setRenderTarget(dstTexture->asRenderTarget()); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1850 | convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY, |
| 1851 | Gr1DKernelEffect::kY_Direction); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1852 | SkTSwap(srcTexture, dstTexture); |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 1853 | if (temp2 && dstTexture == origTexture) { |
| 1854 | dstTexture = temp2->texture(); |
| 1855 | } |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | if (scaleFactorX > 1 || scaleFactorY > 1) { |
| 1859 | // Clear one pixel to the right and below, to accommodate bilinear |
| 1860 | // upsampling. |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1861 | clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, |
| 1862 | srcIRect.width() + 1, 1); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1863 | this->clear(&clearRect, 0x0); |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1864 | clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, |
| 1865 | 1, srcIRect.height()); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1866 | this->clear(&clearRect, 0x0); |
| 1867 | // FIXME: This should be mitchell, not bilinear. |
| 1868 | paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter); |
| 1869 | paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(), |
| 1870 | srcTexture->height()); |
| 1871 | this->setRenderTarget(dstTexture->asRenderTarget()); |
| 1872 | paint.setTexture(0, srcTexture); |
| 1873 | SkRect dstRect(srcRect); |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1874 | scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1875 | this->drawRectToRect(paint, dstRect, srcRect); |
| 1876 | srcRect = dstRect; |
| 1877 | SkTSwap(srcTexture, dstTexture); |
| 1878 | } |
| 1879 | this->setRenderTarget(oldRenderTarget); |
| 1880 | this->setClip(oldClip); |
| 1881 | return srcTexture; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 1882 | } |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 1883 | |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1884 | GrTexture* GrContext::applyMorphology(GrTexture* srcTexture, |
| 1885 | const GrRect& rect, |
| 1886 | GrTexture* temp1, GrTexture* temp2, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1887 | MorphologyType morphType, |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1888 | SkISize radius) { |
senorblanco@chromium.org | ceb4414 | 2012-03-05 20:53:36 +0000 | [diff] [blame] | 1889 | ASSERT_OWNED_RESOURCE(srcTexture); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1890 | GrRenderTarget* oldRenderTarget = this->getRenderTarget(); |
| 1891 | GrAutoMatrix avm(this, GrMatrix::I()); |
| 1892 | GrClip oldClip = this->getClip(); |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1893 | this->setClip(GrRect::MakeWH(SkIntToScalar(srcTexture->width()), |
| 1894 | SkIntToScalar(srcTexture->height()))); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1895 | if (radius.fWidth > 0) { |
| 1896 | this->setRenderTarget(temp1->asRenderTarget()); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1897 | apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType, |
| 1898 | Gr1DKernelEffect::kX_Direction); |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1899 | SkIRect clearRect = SkIRect::MakeXYWH( |
| 1900 | SkScalarFloorToInt(rect.fLeft), |
| 1901 | SkScalarFloorToInt(rect.fBottom), |
| 1902 | SkScalarFloorToInt(rect.width()), |
| 1903 | radius.fHeight); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1904 | this->clear(&clearRect, 0x0); |
| 1905 | srcTexture = temp1; |
| 1906 | } |
| 1907 | if (radius.fHeight > 0) { |
| 1908 | this->setRenderTarget(temp2->asRenderTarget()); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1909 | apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType, |
| 1910 | Gr1DKernelEffect::kY_Direction); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1911 | srcTexture = temp2; |
| 1912 | } |
| 1913 | this->setRenderTarget(oldRenderTarget); |
| 1914 | this->setClip(oldClip); |
| 1915 | return srcTexture; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1916 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1917 | |
robertphillips@google.com | 49d9fd5 | 2012-05-23 11:44:08 +0000 | [diff] [blame] | 1918 | void GrContext::postClipPush() { |
| 1919 | fGpu->postClipPush(); |
| 1920 | } |
| 1921 | |
| 1922 | void GrContext::preClipPop() { |
| 1923 | fGpu->preClipPop(); |
| 1924 | }; |
| 1925 | |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1926 | /////////////////////////////////////////////////////////////////////////////// |