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