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