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