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