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