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