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