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