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