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 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 621 | //////////////////////////////////////////////////////////////////////////////// |
| 622 | |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 623 | void GrContext::clear(const GrIRect* rect, |
| 624 | const GrColor color, |
| 625 | GrRenderTarget* target) { |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 626 | this->flush(); |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 627 | fGpu->clear(rect, color, target); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void GrContext::drawPaint(const GrPaint& paint) { |
| 631 | // set rect to be big enough to fill the space, but not super-huge, so we |
| 632 | // don't overflow fixed-point implementations |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 633 | GrRect r; |
| 634 | r.setLTRB(0, 0, |
| 635 | GrIntToScalar(getRenderTarget()->width()), |
| 636 | GrIntToScalar(getRenderTarget()->height())); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 637 | GrMatrix inverse; |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 638 | SkTLazy<GrPaint> tmpPaint; |
| 639 | const GrPaint* p = &paint; |
robertphillips@google.com | fea85ac | 2012-07-11 18:53:23 +0000 | [diff] [blame] | 640 | AutoMatrix am; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 641 | |
bsalomon@google.com | 4f83be8 | 2011-09-12 13:52:51 +0000 | [diff] [blame] | 642 | // We attempt to map r by the inverse matrix and draw that. mapRect will |
| 643 | // map the four corners and bound them with a new rect. This will not |
| 644 | // produce a correct result for some perspective matrices. |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 645 | if (!this->getMatrix().hasPerspective()) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 646 | if (!fDrawState->getViewInverse(&inverse)) { |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 647 | GrPrintf("Could not invert matrix"); |
| 648 | return; |
| 649 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 650 | inverse.mapRect(&r); |
| 651 | } else { |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 652 | if (paint.getActiveMaskStageMask() || paint.getActiveStageMask()) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 653 | if (!fDrawState->getViewInverse(&inverse)) { |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 654 | GrPrintf("Could not invert matrix"); |
| 655 | return; |
| 656 | } |
| 657 | tmpPaint.set(paint); |
| 658 | tmpPaint.get()->preConcatActiveSamplerMatrices(inverse); |
| 659 | p = tmpPaint.get(); |
| 660 | } |
bsalomon@google.com | 4f83be8 | 2011-09-12 13:52:51 +0000 | [diff] [blame] | 661 | am.set(this, GrMatrix::I()); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 662 | } |
bsalomon@google.com | 4f83be8 | 2011-09-12 13:52:51 +0000 | [diff] [blame] | 663 | // by definition this fills the entire clip, no need for AA |
| 664 | if (paint.fAntiAlias) { |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 665 | if (!tmpPaint.isValid()) { |
| 666 | tmpPaint.set(paint); |
| 667 | p = tmpPaint.get(); |
| 668 | } |
| 669 | GrAssert(p == tmpPaint.get()); |
| 670 | tmpPaint.get()->fAntiAlias = false; |
bsalomon@google.com | 4f83be8 | 2011-09-12 13:52:51 +0000 | [diff] [blame] | 671 | } |
| 672 | this->drawRect(*p, r); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 673 | } |
| 674 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 675 | //////////////////////////////////////////////////////////////////////////////// |
| 676 | |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 677 | namespace { |
| 678 | inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) { |
| 679 | return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage(); |
| 680 | } |
| 681 | } |
| 682 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 683 | //////////////////////////////////////////////////////////////////////////////// |
| 684 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 685 | /* create a triangle strip that strokes the specified triangle. There are 8 |
| 686 | unique vertices, but we repreat the last 2 to close up. Alternatively we |
| 687 | could use an indices array, and then only send 8 verts, but not sure that |
| 688 | would be faster. |
| 689 | */ |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 690 | static void setStrokeRectStrip(GrPoint verts[10], GrRect rect, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 691 | GrScalar width) { |
| 692 | const GrScalar rad = GrScalarHalf(width); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 693 | rect.sort(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 694 | |
| 695 | verts[0].set(rect.fLeft + rad, rect.fTop + rad); |
| 696 | verts[1].set(rect.fLeft - rad, rect.fTop - rad); |
| 697 | verts[2].set(rect.fRight - rad, rect.fTop + rad); |
| 698 | verts[3].set(rect.fRight + rad, rect.fTop - rad); |
| 699 | verts[4].set(rect.fRight - rad, rect.fBottom - rad); |
| 700 | verts[5].set(rect.fRight + rad, rect.fBottom + rad); |
| 701 | verts[6].set(rect.fLeft + rad, rect.fBottom - rad); |
| 702 | verts[7].set(rect.fLeft - rad, rect.fBottom + rad); |
| 703 | verts[8] = verts[0]; |
| 704 | verts[9] = verts[1]; |
| 705 | } |
| 706 | |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 707 | /** |
| 708 | * Returns true if the rects edges are integer-aligned. |
| 709 | */ |
| 710 | static bool isIRect(const GrRect& r) { |
| 711 | return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) && |
| 712 | GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom); |
| 713 | } |
| 714 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 715 | static bool apply_aa_to_rect(GrDrawTarget* target, |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 716 | const GrRect& rect, |
| 717 | GrScalar width, |
| 718 | const GrMatrix* matrix, |
| 719 | GrMatrix* combinedMatrix, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 720 | GrRect* devRect, |
| 721 | bool* useVertexCoverage) { |
bsalomon@google.com | 2eba795 | 2012-01-12 13:47:37 +0000 | [diff] [blame] | 722 | // we use a simple coverage ramp to do aa on axis-aligned rects |
| 723 | // we check if the rect will be axis-aligned, and the rect won't land on |
| 724 | // integer coords. |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 725 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 726 | // we are keeping around the "tweak the alpha" trick because |
| 727 | // it is our only hope for the fixed-pipe implementation. |
| 728 | // In a shader implementation we can give a separate coverage input |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 729 | // TODO: remove this ugliness when we drop the fixed-pipe impl |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 730 | *useVertexCoverage = false; |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 731 | if (!target->canTweakAlphaForCoverage()) { |
bsalomon@google.com | 2eba795 | 2012-01-12 13:47:37 +0000 | [diff] [blame] | 732 | if (disable_coverage_aa_for_blend(target)) { |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 733 | #if GR_DEBUG |
bsalomon@google.com | 2eba795 | 2012-01-12 13:47:37 +0000 | [diff] [blame] | 734 | //GrPrintf("Turning off AA to correctly apply blend.\n"); |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 735 | #endif |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 736 | return false; |
bsalomon@google.com | 2eba795 | 2012-01-12 13:47:37 +0000 | [diff] [blame] | 737 | } else { |
| 738 | *useVertexCoverage = true; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 739 | } |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 740 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 741 | const GrDrawState& drawState = target->getDrawState(); |
| 742 | if (drawState.getRenderTarget()->isMultisampled()) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 743 | return false; |
| 744 | } |
| 745 | |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 746 | if (0 == width && target->willUseHWAALines()) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 747 | return false; |
| 748 | } |
| 749 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 750 | if (!drawState.getViewMatrix().preservesAxisAlignment()) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 751 | return false; |
| 752 | } |
| 753 | |
| 754 | if (NULL != matrix && |
| 755 | !matrix->preservesAxisAlignment()) { |
| 756 | return false; |
| 757 | } |
| 758 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 759 | *combinedMatrix = drawState.getViewMatrix(); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 760 | if (NULL != matrix) { |
| 761 | combinedMatrix->preConcat(*matrix); |
| 762 | GrAssert(combinedMatrix->preservesAxisAlignment()); |
| 763 | } |
| 764 | |
| 765 | combinedMatrix->mapRect(devRect, rect); |
| 766 | devRect->sort(); |
| 767 | |
| 768 | if (width < 0) { |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 769 | return !isIRect(*devRect); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 770 | } else { |
| 771 | return true; |
| 772 | } |
| 773 | } |
| 774 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 775 | void GrContext::drawRect(const GrPaint& paint, |
| 776 | const GrRect& rect, |
| 777 | GrScalar width, |
| 778 | const GrMatrix* matrix) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 779 | SK_TRACE_EVENT0("GrContext::drawRect"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 780 | |
| 781 | GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 782 | GrDrawState::AutoStageDisable atr(fDrawState); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 783 | int stageMask = paint.getActiveStageMask(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 784 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 785 | GrRect devRect = rect; |
| 786 | GrMatrix combinedMatrix; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 787 | bool useVertexCoverage; |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 788 | bool needAA = paint.fAntiAlias && |
| 789 | !this->getRenderTarget()->isMultisampled(); |
| 790 | bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix, |
| 791 | &combinedMatrix, &devRect, |
| 792 | &useVertexCoverage); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 793 | |
| 794 | if (doAA) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 795 | GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 796 | if (width >= 0) { |
| 797 | GrVec strokeSize;; |
| 798 | if (width > 0) { |
| 799 | strokeSize.set(width, width); |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 800 | combinedMatrix.mapVectors(&strokeSize, 1); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 801 | strokeSize.setAbs(strokeSize); |
| 802 | } else { |
| 803 | strokeSize.set(GR_Scalar1, GR_Scalar1); |
| 804 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 805 | fAARectRenderer->strokeAARect(this->getGpu(), target, devRect, |
| 806 | strokeSize, useVertexCoverage); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 807 | } else { |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 808 | fAARectRenderer->fillAARect(this->getGpu(), target, |
| 809 | devRect, useVertexCoverage); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 810 | } |
| 811 | return; |
| 812 | } |
| 813 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 814 | if (width >= 0) { |
| 815 | // TODO: consider making static vertex buffers for these cases. |
| 816 | // Hairline could be done by just adding closing vertex to |
| 817 | // unitSquareVertexBuffer() |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 818 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL); |
| 819 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 820 | static const int worstCaseVertCount = 10; |
| 821 | GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0); |
| 822 | |
| 823 | if (!geo.succeeded()) { |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 824 | GrPrintf("Failed to get space for vertices!\n"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 825 | return; |
| 826 | } |
| 827 | |
| 828 | GrPrimitiveType primType; |
| 829 | int vertCount; |
| 830 | GrPoint* vertex = geo.positions(); |
| 831 | |
| 832 | if (width > 0) { |
| 833 | vertCount = 10; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 834 | primType = kTriangleStrip_GrPrimitiveType; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 835 | setStrokeRectStrip(vertex, rect, width); |
| 836 | } else { |
| 837 | // hairline |
| 838 | vertCount = 5; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 839 | primType = kLineStrip_GrPrimitiveType; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 840 | vertex[0].set(rect.fLeft, rect.fTop); |
| 841 | vertex[1].set(rect.fRight, rect.fTop); |
| 842 | vertex[2].set(rect.fRight, rect.fBottom); |
| 843 | vertex[3].set(rect.fLeft, rect.fBottom); |
| 844 | vertex[4].set(rect.fLeft, rect.fTop); |
| 845 | } |
| 846 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 847 | GrDrawState::AutoViewMatrixRestore avmr; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 848 | if (NULL != matrix) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 849 | GrDrawState* drawState = target->drawState(); |
| 850 | avmr.set(drawState); |
| 851 | drawState->preConcatViewMatrix(*matrix); |
| 852 | drawState->preConcatSamplerMatrices(stageMask, *matrix); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | target->drawNonIndexed(primType, 0, vertCount); |
| 856 | } else { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 857 | #if GR_STATIC_RECT_VB |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 858 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL); |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 859 | const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer(); |
| 860 | if (NULL == sqVB) { |
| 861 | GrPrintf("Failed to create static rect vb.\n"); |
| 862 | return; |
| 863 | } |
| 864 | target->setVertexSourceToBuffer(layout, sqVB); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 865 | GrDrawState* drawState = target->drawState(); |
| 866 | GrDrawState::AutoViewMatrixRestore avmr(drawState); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 867 | GrMatrix m; |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 868 | m.setAll(rect.width(), 0, rect.fLeft, |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 869 | 0, rect.height(), rect.fTop, |
| 870 | 0, 0, GrMatrix::I()[8]); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 871 | |
| 872 | if (NULL != matrix) { |
| 873 | m.postConcat(*matrix); |
| 874 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 875 | drawState->preConcatViewMatrix(m); |
| 876 | drawState->preConcatSamplerMatrices(stageMask, m); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 877 | |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 878 | target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 879 | #else |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 880 | target->drawSimpleRect(rect, matrix, stageMask); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 881 | #endif |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | |
| 885 | void GrContext::drawRectToRect(const GrPaint& paint, |
| 886 | const GrRect& dstRect, |
| 887 | const GrRect& srcRect, |
| 888 | const GrMatrix* dstMatrix, |
| 889 | const GrMatrix* srcMatrix) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 890 | SK_TRACE_EVENT0("GrContext::drawRectToRect"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 891 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 892 | // srcRect refers to paint's first texture |
tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 893 | if (!paint.isTextureStageEnabled(0)) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 894 | drawRect(paint, dstRect, -1, dstMatrix); |
| 895 | return; |
| 896 | } |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 897 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 898 | GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB); |
| 899 | |
| 900 | #if GR_STATIC_RECT_VB |
| 901 | GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 902 | GrDrawState::AutoStageDisable atr(fDrawState); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 903 | GrDrawState* drawState = target->drawState(); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 904 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 905 | GrDrawState::AutoViewMatrixRestore avmr(drawState); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 906 | |
| 907 | GrMatrix m; |
| 908 | |
| 909 | m.setAll(dstRect.width(), 0, dstRect.fLeft, |
| 910 | 0, dstRect.height(), dstRect.fTop, |
| 911 | 0, 0, GrMatrix::I()[8]); |
| 912 | if (NULL != dstMatrix) { |
| 913 | m.postConcat(*dstMatrix); |
| 914 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 915 | drawState->preConcatViewMatrix(m); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 916 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 917 | // srcRect refers to first stage |
| 918 | int otherStageMask = paint.getActiveStageMask() & |
| 919 | (~(1 << GrPaint::kFirstTextureStage)); |
| 920 | if (otherStageMask) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 921 | drawState->preConcatSamplerMatrices(otherStageMask, m); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 922 | } |
| 923 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 924 | m.setAll(srcRect.width(), 0, srcRect.fLeft, |
| 925 | 0, srcRect.height(), srcRect.fTop, |
| 926 | 0, 0, GrMatrix::I()[8]); |
| 927 | if (NULL != srcMatrix) { |
| 928 | m.postConcat(*srcMatrix); |
| 929 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 930 | drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 931 | |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 932 | const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer(); |
| 933 | if (NULL == sqVB) { |
| 934 | GrPrintf("Failed to create static rect vb.\n"); |
| 935 | return; |
| 936 | } |
| 937 | target->setVertexSourceToBuffer(layout, sqVB); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 938 | target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 939 | #else |
| 940 | |
| 941 | GrDrawTarget* target; |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 942 | #if BATCH_RECT_TO_RECT |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 943 | target = this->prepareToDraw(paint, kBuffered_DrawCategory); |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 944 | #else |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 945 | target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
| 946 | #endif |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 947 | GrDrawState::AutoStageDisable atr(fDrawState); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 948 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 949 | const GrRect* srcRects[GrDrawState::kNumStages] = {NULL}; |
| 950 | const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL}; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 951 | srcRects[0] = &srcRect; |
| 952 | srcMatrices[0] = srcMatrix; |
| 953 | |
| 954 | target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices); |
| 955 | #endif |
| 956 | } |
| 957 | |
| 958 | void GrContext::drawVertices(const GrPaint& paint, |
| 959 | GrPrimitiveType primitiveType, |
| 960 | int vertexCount, |
| 961 | const GrPoint positions[], |
| 962 | const GrPoint texCoords[], |
| 963 | const GrColor colors[], |
| 964 | const uint16_t indices[], |
| 965 | int indexCount) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 966 | SK_TRACE_EVENT0("GrContext::drawVertices"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 967 | |
| 968 | GrDrawTarget::AutoReleaseGeometry geo; |
| 969 | |
| 970 | GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 971 | GrDrawState::AutoStageDisable atr(fDrawState); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 972 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 973 | bool hasTexCoords[GrPaint::kTotalStages] = { |
| 974 | NULL != texCoords, // texCoordSrc provides explicit stage 0 coords |
| 975 | 0 // remaining stages use positions |
| 976 | }; |
| 977 | |
| 978 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 979 | |
| 980 | if (NULL != colors) { |
| 981 | layout |= GrDrawTarget::kColor_VertexLayoutBit; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 982 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 983 | int vertexSize = GrDrawTarget::VertexSize(layout); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 984 | |
| 985 | if (sizeof(GrPoint) != vertexSize) { |
| 986 | if (!geo.set(target, layout, vertexCount, 0)) { |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 987 | GrPrintf("Failed to get space for vertices!\n"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 988 | return; |
| 989 | } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 990 | int texOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 991 | int colorOffset; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 992 | GrDrawTarget::VertexSizeAndOffsetsByIdx(layout, |
| 993 | texOffsets, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 994 | &colorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 995 | NULL, |
| 996 | NULL); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 997 | void* curVertex = geo.vertices(); |
| 998 | |
| 999 | for (int i = 0; i < vertexCount; ++i) { |
| 1000 | *((GrPoint*)curVertex) = positions[i]; |
| 1001 | |
| 1002 | if (texOffsets[0] > 0) { |
| 1003 | *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i]; |
| 1004 | } |
| 1005 | if (colorOffset > 0) { |
| 1006 | *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i]; |
| 1007 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1008 | curVertex = (void*)((intptr_t)curVertex + vertexSize); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } else { |
| 1011 | target->setVertexSourceToArray(layout, positions, vertexCount); |
| 1012 | } |
| 1013 | |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1014 | // we don't currently apply offscreen AA to this path. Need improved |
| 1015 | // management of GrDrawTarget's geometry to avoid copying points per-tile. |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1016 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1017 | if (NULL != indices) { |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1018 | target->setIndexSourceToArray(indices, indexCount); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1019 | target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1020 | } else { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1021 | target->drawNonIndexed(primitiveType, 0, vertexCount); |
| 1022 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 1025 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 150d284 | 2012-01-12 20:19:56 +0000 | [diff] [blame] | 1026 | namespace { |
| 1027 | |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1028 | struct CircleVertex { |
| 1029 | GrPoint fPos; |
| 1030 | GrPoint fCenter; |
| 1031 | GrScalar fOuterRadius; |
| 1032 | GrScalar fInnerRadius; |
| 1033 | }; |
| 1034 | |
| 1035 | /* Returns true if will map a circle to another circle. This can be true |
| 1036 | * if the matrix only includes square-scale, rotation, translation. |
| 1037 | */ |
| 1038 | inline bool isSimilarityTransformation(const SkMatrix& matrix, |
| 1039 | SkScalar tol = SK_ScalarNearlyZero) { |
| 1040 | if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) { |
| 1041 | return true; |
| 1042 | } |
| 1043 | if (matrix.hasPerspective()) { |
| 1044 | return false; |
| 1045 | } |
| 1046 | |
| 1047 | SkScalar mx = matrix.get(SkMatrix::kMScaleX); |
| 1048 | SkScalar sx = matrix.get(SkMatrix::kMSkewX); |
| 1049 | SkScalar my = matrix.get(SkMatrix::kMScaleY); |
| 1050 | SkScalar sy = matrix.get(SkMatrix::kMSkewY); |
| 1051 | |
| 1052 | if (mx == 0 && sx == 0 && my == 0 && sy == 0) { |
| 1053 | return false; |
| 1054 | } |
| 1055 | |
| 1056 | // it has scales or skews, but it could also be rotation, check it out. |
| 1057 | SkVector vec[2]; |
| 1058 | vec[0].set(mx, sx); |
| 1059 | vec[1].set(sy, my); |
| 1060 | |
| 1061 | return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) && |
| 1062 | SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(), |
| 1063 | SkScalarSquare(tol)); |
| 1064 | } |
| 1065 | |
| 1066 | } |
| 1067 | |
| 1068 | // TODO: strokeWidth can't be larger than zero right now. |
| 1069 | // It will be fixed when drawPath() can handle strokes. |
| 1070 | void GrContext::drawOval(const GrPaint& paint, |
| 1071 | const GrRect& rect, |
| 1072 | SkScalar strokeWidth) { |
| 1073 | DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory : |
| 1074 | kUnbuffered_DrawCategory; |
| 1075 | GrDrawTarget* target = this->prepareToDraw(paint, category); |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 1076 | GrDrawState::AutoStageDisable atr(fDrawState); |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1077 | GrDrawState* drawState = target->drawState(); |
| 1078 | GrMatrix vm = drawState->getViewMatrix(); |
| 1079 | |
| 1080 | if (!isSimilarityTransformation(vm) || |
| 1081 | !paint.fAntiAlias || |
| 1082 | rect.height() != rect.width()) { |
| 1083 | SkPath path; |
| 1084 | path.addOval(rect); |
| 1085 | GrPathFill fill = (strokeWidth == 0) ? |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1086 | kHairLine_GrPathFill : kWinding_GrPathFill; |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1087 | this->internalDrawPath(paint, path, fill, NULL); |
| 1088 | return; |
| 1089 | } |
| 1090 | |
| 1091 | const GrRenderTarget* rt = drawState->getRenderTarget(); |
| 1092 | if (NULL == rt) { |
| 1093 | return; |
| 1094 | } |
| 1095 | |
| 1096 | GrDrawTarget::AutoDeviceCoordDraw adcd(target, paint.getActiveStageMask()); |
| 1097 | |
| 1098 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL); |
| 1099 | layout |= GrDrawTarget::kEdge_VertexLayoutBit; |
| 1100 | GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout)); |
| 1101 | |
| 1102 | GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY()); |
| 1103 | GrScalar radius = SkScalarHalf(rect.width()); |
| 1104 | |
| 1105 | vm.mapPoints(¢er, 1); |
| 1106 | radius = vm.mapRadius(radius); |
| 1107 | |
| 1108 | GrScalar outerRadius = radius; |
| 1109 | GrScalar innerRadius = 0; |
| 1110 | SkScalar halfWidth = 0; |
| 1111 | if (strokeWidth == 0) { |
| 1112 | halfWidth = SkScalarHalf(SK_Scalar1); |
| 1113 | |
| 1114 | outerRadius += halfWidth; |
| 1115 | innerRadius = SkMaxScalar(0, radius - halfWidth); |
| 1116 | } |
| 1117 | |
| 1118 | GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0); |
| 1119 | if (!geo.succeeded()) { |
| 1120 | GrPrintf("Failed to get space for vertices!\n"); |
| 1121 | return; |
| 1122 | } |
| 1123 | |
| 1124 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices()); |
| 1125 | |
robertphillips@google.com | a0a66c1 | 2012-06-22 13:14:29 +0000 | [diff] [blame] | 1126 | // The fragment shader will extend the radius out half a pixel |
| 1127 | // to antialias. Expand the drawn rect here so all the pixels |
| 1128 | // will be captured. |
| 1129 | SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f); |
| 1130 | SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f); |
| 1131 | SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f); |
| 1132 | SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f); |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1133 | |
| 1134 | verts[0].fPos = SkPoint::Make(L, T); |
| 1135 | verts[1].fPos = SkPoint::Make(R, T); |
| 1136 | verts[2].fPos = SkPoint::Make(L, B); |
| 1137 | verts[3].fPos = SkPoint::Make(R, B); |
| 1138 | |
| 1139 | for (int i = 0; i < 4; ++i) { |
| 1140 | // this goes to fragment shader, it should be in y-points-up space. |
| 1141 | verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY); |
| 1142 | |
| 1143 | verts[i].fOuterRadius = outerRadius; |
| 1144 | verts[i].fInnerRadius = innerRadius; |
| 1145 | } |
| 1146 | |
| 1147 | drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1148 | target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4); |
bsalomon@google.com | 150d284 | 2012-01-12 20:19:56 +0000 | [diff] [blame] | 1149 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1150 | |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 1151 | void GrContext::drawPath(const GrPaint& paint, const SkPath& path, |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 1152 | GrPathFill fill, const GrPoint* translate) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1153 | |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1154 | if (path.isEmpty()) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1155 | if (GrIsFillInverted(fill)) { |
| 1156 | this->drawPaint(paint); |
| 1157 | } |
| 1158 | return; |
| 1159 | } |
| 1160 | |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1161 | SkRect ovalRect; |
| 1162 | if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) { |
| 1163 | if (translate) { |
| 1164 | ovalRect.offset(*translate); |
| 1165 | } |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1166 | SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1; |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1167 | this->drawOval(paint, ovalRect, width); |
| 1168 | return; |
| 1169 | } |
| 1170 | |
| 1171 | internalDrawPath(paint, path, fill, translate); |
| 1172 | } |
| 1173 | |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 1174 | void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path, |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1175 | GrPathFill fill, const GrPoint* translate) { |
| 1176 | |
bsalomon@google.com | fb4ce6f | 2012-03-14 13:27:54 +0000 | [diff] [blame] | 1177 | // Note that below we may sw-rasterize the path into a scratch texture. |
| 1178 | // Scratch textures can be recycled after they are returned to the texture |
| 1179 | // cache. This presents a potential hazard for buffered drawing. However, |
| 1180 | // the writePixels that uploads to the scratch will perform a flush so we're |
| 1181 | // OK. |
| 1182 | DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory : |
| 1183 | kUnbuffered_DrawCategory; |
| 1184 | GrDrawTarget* target = this->prepareToDraw(paint, category); |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 1185 | GrDrawState::AutoStageDisable atr(fDrawState); |
bsalomon@google.com | 150d284 | 2012-01-12 20:19:56 +0000 | [diff] [blame] | 1186 | GrDrawState::StageMask stageMask = paint.getActiveStageMask(); |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 1187 | |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 1188 | bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled(); |
| 1189 | |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 1190 | // An Assumption here is that path renderer would use some form of tweaking |
| 1191 | // the src color (either the input alpha or in the frag shader) to implement |
| 1192 | // aa. If we have some future driver-mojo path AA that can do the right |
| 1193 | // thing WRT to the blend then we'll need some query on the PR. |
| 1194 | if (disable_coverage_aa_for_blend(target)) { |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 1195 | #if GR_DEBUG |
bsalomon@google.com | 979432b | 2011-11-05 21:38:22 +0000 | [diff] [blame] | 1196 | //GrPrintf("Turning off AA to correctly apply blend.\n"); |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 1197 | #endif |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 1198 | prAA = false; |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 1199 | } |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 1200 | |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1201 | GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1202 | if (NULL == pr) { |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 1203 | #if GR_DEBUG |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1204 | GrPrintf("Unable to find path renderer compatible with path.\n"); |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 1205 | #endif |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1206 | return; |
| 1207 | } |
| 1208 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1209 | pr->drawPath(path, fill, translate, target, stageMask, prAA); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1210 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1211 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1212 | //////////////////////////////////////////////////////////////////////////////// |
| 1213 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1214 | void GrContext::flush(int flagsBitfield) { |
| 1215 | if (kDiscard_FlushBit & flagsBitfield) { |
| 1216 | fDrawBuffer->reset(); |
| 1217 | } else { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1218 | this->flushDrawBuffer(); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1219 | } |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1220 | if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1221 | fGpu->forceRenderTargetFlush(); |
| 1222 | } |
| 1223 | } |
| 1224 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1225 | void GrContext::flushDrawBuffer() { |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 1226 | if (fDrawBuffer) { |
robertphillips@google.com | 58b3818 | 2012-05-03 16:29:41 +0000 | [diff] [blame] | 1227 | // With addition of the AA clip path, flushing the draw buffer can |
| 1228 | // result in the generation of an AA clip mask. During this |
| 1229 | // process the SW path renderer may be invoked which recusively |
| 1230 | // calls this method (via internalWriteTexturePixels) creating |
| 1231 | // infinite recursion |
| 1232 | GrInOrderDrawBuffer* temp = fDrawBuffer; |
| 1233 | fDrawBuffer = NULL; |
| 1234 | |
| 1235 | temp->flushTo(fGpu); |
| 1236 | |
| 1237 | fDrawBuffer = temp; |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 1238 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1241 | void GrContext::internalWriteTexturePixels(GrTexture* texture, |
| 1242 | int left, int top, |
| 1243 | int width, int height, |
| 1244 | GrPixelConfig config, |
| 1245 | const void* buffer, |
| 1246 | size_t rowBytes, |
| 1247 | uint32_t flags) { |
| 1248 | SK_TRACE_EVENT0("GrContext::writeTexturePixels"); |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1249 | ASSERT_OWNED_RESOURCE(texture); |
| 1250 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1251 | if (!(kDontFlush_PixelOpsFlag & flags)) { |
| 1252 | this->flush(); |
| 1253 | } |
| 1254 | // TODO: use scratch texture to perform conversion |
| 1255 | if (GrPixelConfigIsUnpremultiplied(texture->config()) != |
| 1256 | GrPixelConfigIsUnpremultiplied(config)) { |
| 1257 | return; |
| 1258 | } |
| 1259 | |
| 1260 | fGpu->writeTexturePixels(texture, left, top, width, height, |
| 1261 | config, buffer, rowBytes); |
| 1262 | } |
| 1263 | |
| 1264 | bool GrContext::internalReadTexturePixels(GrTexture* texture, |
| 1265 | int left, int top, |
| 1266 | int width, int height, |
| 1267 | GrPixelConfig config, |
| 1268 | void* buffer, |
| 1269 | size_t rowBytes, |
| 1270 | uint32_t flags) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 1271 | SK_TRACE_EVENT0("GrContext::readTexturePixels"); |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1272 | ASSERT_OWNED_RESOURCE(texture); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1273 | |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1274 | // TODO: code read pixels for textures that aren't also rendertargets |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1275 | GrRenderTarget* target = texture->asRenderTarget(); |
| 1276 | if (NULL != target) { |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1277 | return this->internalReadRenderTargetPixels(target, |
| 1278 | left, top, width, height, |
| 1279 | config, buffer, rowBytes, |
| 1280 | flags); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1281 | } else { |
| 1282 | return false; |
| 1283 | } |
| 1284 | } |
| 1285 | |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1286 | #include "SkConfig8888.h" |
| 1287 | |
| 1288 | namespace { |
| 1289 | /** |
| 1290 | * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel |
| 1291 | * formats are representable as Config8888 and so the function returns false |
| 1292 | * if the GrPixelConfig has no equivalent Config8888. |
| 1293 | */ |
| 1294 | bool grconfig_to_config8888(GrPixelConfig config, |
| 1295 | SkCanvas::Config8888* config8888) { |
| 1296 | switch (config) { |
| 1297 | case kRGBA_8888_PM_GrPixelConfig: |
| 1298 | *config8888 = SkCanvas::kRGBA_Premul_Config8888; |
| 1299 | return true; |
| 1300 | case kRGBA_8888_UPM_GrPixelConfig: |
| 1301 | *config8888 = SkCanvas::kRGBA_Unpremul_Config8888; |
| 1302 | return true; |
| 1303 | case kBGRA_8888_PM_GrPixelConfig: |
| 1304 | *config8888 = SkCanvas::kBGRA_Premul_Config8888; |
| 1305 | return true; |
| 1306 | case kBGRA_8888_UPM_GrPixelConfig: |
| 1307 | *config8888 = SkCanvas::kBGRA_Unpremul_Config8888; |
| 1308 | return true; |
| 1309 | default: |
| 1310 | return false; |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1315 | bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target, |
| 1316 | int left, int top, |
| 1317 | int width, int height, |
| 1318 | GrPixelConfig config, |
| 1319 | void* buffer, |
| 1320 | size_t rowBytes, |
| 1321 | uint32_t flags) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 1322 | SK_TRACE_EVENT0("GrContext::readRenderTargetPixels"); |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1323 | ASSERT_OWNED_RESOURCE(target); |
| 1324 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1325 | if (NULL == target) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1326 | target = fDrawState->getRenderTarget(); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1327 | if (NULL == target) { |
| 1328 | return false; |
| 1329 | } |
| 1330 | } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1331 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1332 | if (!(kDontFlush_PixelOpsFlag & flags)) { |
| 1333 | this->flush(); |
| 1334 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1335 | |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1336 | if (!GrPixelConfigIsUnpremultiplied(target->config()) && |
| 1337 | GrPixelConfigIsUnpremultiplied(config) && |
| 1338 | !fGpu->canPreserveReadWriteUnpremulPixels()) { |
| 1339 | SkCanvas::Config8888 srcConfig8888, dstConfig8888; |
| 1340 | if (!grconfig_to_config8888(target->config(), &srcConfig8888) || |
| 1341 | !grconfig_to_config8888(config, &dstConfig8888)) { |
| 1342 | return false; |
| 1343 | } |
| 1344 | // do read back using target's own config |
| 1345 | this->internalReadRenderTargetPixels(target, |
| 1346 | left, top, |
| 1347 | width, height, |
| 1348 | target->config(), |
| 1349 | buffer, rowBytes, |
| 1350 | kDontFlush_PixelOpsFlag); |
| 1351 | // sw convert the pixels to unpremul config |
| 1352 | uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer); |
| 1353 | SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888, |
| 1354 | pixels, rowBytes, srcConfig8888, |
| 1355 | width, height); |
| 1356 | return true; |
| 1357 | } |
| 1358 | |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1359 | GrTexture* src = target->asTexture(); |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1360 | bool swapRAndB = NULL != src && |
| 1361 | fGpu->preferredReadPixelsConfig(config) == |
| 1362 | GrPixelConfigSwapRAndB(config); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1363 | |
| 1364 | bool flipY = NULL != src && |
| 1365 | fGpu->readPixelsWillPayForYFlip(target, left, top, |
| 1366 | width, height, config, |
| 1367 | rowBytes); |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1368 | bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) && |
| 1369 | GrPixelConfigIsUnpremultiplied(config)); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1370 | |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1371 | if (NULL == src && alphaConversion) { |
| 1372 | // we should fallback to cpu conversion here. This could happen when |
| 1373 | // we were given an external render target by the client that is not |
| 1374 | // also a texture (e.g. FBO 0 in GL) |
| 1375 | return false; |
| 1376 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1377 | // 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] | 1378 | GrAutoScratchTexture ast; |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1379 | if (flipY || swapRAndB || alphaConversion) { |
| 1380 | GrAssert(NULL != src); |
| 1381 | if (swapRAndB) { |
| 1382 | config = GrPixelConfigSwapRAndB(config); |
| 1383 | GrAssert(kUnknown_GrPixelConfig != config); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1384 | } |
| 1385 | // Make the scratch a render target because we don't have a robust |
| 1386 | // readTexturePixels as of yet (it calls this function). |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 1387 | GrTextureDesc desc; |
| 1388 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 1389 | desc.fWidth = width; |
| 1390 | desc.fHeight = height; |
| 1391 | desc.fConfig = config; |
bsalomon@google.com | c4ff22a | 2011-11-10 21:56:21 +0000 | [diff] [blame] | 1392 | |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 1393 | // When a full readback is faster than a partial we could always make |
| 1394 | // the scratch exactly match the passed rect. However, if we see many |
| 1395 | // different size rectangles we will trash our texture cache and pay the |
| 1396 | // cost of creating and destroying many textures. So, we only request |
| 1397 | // an exact match when the caller is reading an entire RT. |
| 1398 | ScratchTexMatch match = kApprox_ScratchTexMatch; |
| 1399 | if (0 == left && |
| 1400 | 0 == top && |
| 1401 | target->width() == width && |
| 1402 | target->height() == height && |
| 1403 | fGpu->fullReadPixelsIsFasterThanPartial()) { |
| 1404 | match = kExact_ScratchTexMatch; |
| 1405 | } |
| 1406 | ast.set(this, desc, match); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1407 | GrTexture* texture = ast.texture(); |
| 1408 | if (!texture) { |
| 1409 | return false; |
| 1410 | } |
| 1411 | target = texture->asRenderTarget(); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1412 | GrAssert(NULL != target); |
| 1413 | |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 1414 | GrDrawTarget::AutoStateRestore asr(fGpu, |
| 1415 | GrDrawTarget::kReset_ASRInit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1416 | GrDrawState* drawState = fGpu->drawState(); |
| 1417 | drawState->setRenderTarget(target); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1418 | |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1419 | GrMatrix matrix; |
| 1420 | if (flipY) { |
| 1421 | matrix.setTranslate(SK_Scalar1 * left, |
| 1422 | SK_Scalar1 * (top + height)); |
| 1423 | matrix.set(GrMatrix::kMScaleY, -GR_Scalar1); |
| 1424 | } else { |
| 1425 | matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top); |
| 1426 | } |
| 1427 | matrix.postIDiv(src->width(), src->height()); |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 1428 | drawState->sampler(0)->reset(matrix); |
| 1429 | drawState->sampler(0)->setRAndBSwap(swapRAndB); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1430 | drawState->setTexture(0, src); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1431 | GrRect rect; |
| 1432 | rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height); |
| 1433 | fGpu->drawSimpleRect(rect, NULL, 0x1); |
| 1434 | left = 0; |
| 1435 | top = 0; |
| 1436 | } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1437 | return fGpu->readPixels(target, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1438 | left, top, width, height, |
| 1439 | config, buffer, rowBytes, flipY); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 1442 | void GrContext::resolveRenderTarget(GrRenderTarget* target) { |
| 1443 | GrAssert(target); |
| 1444 | ASSERT_OWNED_RESOURCE(target); |
| 1445 | // In the future we may track whether there are any pending draws to this |
| 1446 | // target. We don't today so we always perform a flush. We don't promise |
| 1447 | // this to our clients, though. |
| 1448 | this->flush(); |
| 1449 | fGpu->resolveRenderTarget(target); |
| 1450 | } |
| 1451 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1452 | void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) { |
| 1453 | if (NULL == src || NULL == dst) { |
| 1454 | return; |
| 1455 | } |
| 1456 | ASSERT_OWNED_RESOURCE(src); |
| 1457 | |
twiz@google.com | 1ac87ff | 2012-04-27 19:39:33 +0000 | [diff] [blame] | 1458 | // Writes pending to the source texture are not tracked, so a flush |
| 1459 | // is required to ensure that the copy captures the most recent contents |
| 1460 | // of the source texture. See similar behaviour in |
| 1461 | // GrContext::resolveRenderTarget. |
| 1462 | this->flush(); |
| 1463 | |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 1464 | GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1465 | GrDrawState* drawState = fGpu->drawState(); |
| 1466 | drawState->setRenderTarget(dst); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1467 | GrMatrix sampleM; |
| 1468 | sampleM.setIDiv(src->width(), src->height()); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1469 | drawState->setTexture(0, src); |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 1470 | drawState->sampler(0)->reset(sampleM); |
bsalomon@google.com | 5db3b6c | 2012-01-12 20:38:57 +0000 | [diff] [blame] | 1471 | SkRect rect = SkRect::MakeXYWH(0, 0, |
| 1472 | SK_Scalar1 * src->width(), |
| 1473 | SK_Scalar1 * src->height()); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1474 | fGpu->drawSimpleRect(rect, NULL, 1 << 0); |
| 1475 | } |
| 1476 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1477 | void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target, |
| 1478 | int left, int top, |
| 1479 | int width, int height, |
| 1480 | GrPixelConfig config, |
| 1481 | const void* buffer, |
| 1482 | size_t rowBytes, |
| 1483 | uint32_t flags) { |
| 1484 | SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels"); |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1485 | ASSERT_OWNED_RESOURCE(target); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1486 | |
| 1487 | if (NULL == target) { |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 1488 | target = fDrawState->getRenderTarget(); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1489 | if (NULL == target) { |
| 1490 | return; |
| 1491 | } |
| 1492 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1493 | |
| 1494 | // TODO: when underlying api has a direct way to do this we should use it |
| 1495 | // (e.g. glDrawPixels on desktop GL). |
| 1496 | |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1497 | // If the RT is also a texture and we don't have to do PM/UPM conversion |
| 1498 | // then take the texture path, which we expect to be at least as fast or |
| 1499 | // faster since it doesn't use an intermediate texture as we do below. |
| 1500 | |
| 1501 | #if !GR_MAC_BUILD |
| 1502 | // At least some drivers on the Mac get confused when glTexImage2D is called |
| 1503 | // on a texture attached to an FBO. The FBO still sees the old image. TODO: |
| 1504 | // determine what OS versions and/or HW is affected. |
| 1505 | if (NULL != target->asTexture() && |
| 1506 | GrPixelConfigIsUnpremultiplied(target->config()) == |
| 1507 | GrPixelConfigIsUnpremultiplied(config)) { |
| 1508 | |
| 1509 | this->internalWriteTexturePixels(target->asTexture(), |
| 1510 | left, top, width, height, |
| 1511 | config, buffer, rowBytes, flags); |
| 1512 | return; |
| 1513 | } |
| 1514 | #endif |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1515 | if (!GrPixelConfigIsUnpremultiplied(target->config()) && |
| 1516 | GrPixelConfigIsUnpremultiplied(config) && |
| 1517 | !fGpu->canPreserveReadWriteUnpremulPixels()) { |
| 1518 | SkCanvas::Config8888 srcConfig8888, dstConfig8888; |
| 1519 | if (!grconfig_to_config8888(config, &srcConfig8888) || |
| 1520 | !grconfig_to_config8888(target->config(), &dstConfig8888)) { |
| 1521 | return; |
| 1522 | } |
| 1523 | // allocate a tmp buffer and sw convert the pixels to premul |
| 1524 | SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height); |
| 1525 | const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer); |
| 1526 | SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888, |
| 1527 | src, rowBytes, srcConfig8888, |
| 1528 | width, height); |
| 1529 | // upload the already premul pixels |
| 1530 | this->internalWriteRenderTargetPixels(target, |
| 1531 | left, top, |
| 1532 | width, height, |
| 1533 | target->config(), |
| 1534 | tmpPixels, 4 * width, flags); |
| 1535 | return; |
| 1536 | } |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1537 | |
| 1538 | bool swapRAndB = fGpu->preferredReadPixelsConfig(config) == |
| 1539 | GrPixelConfigSwapRAndB(config); |
| 1540 | if (swapRAndB) { |
| 1541 | config = GrPixelConfigSwapRAndB(config); |
| 1542 | } |
| 1543 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 1544 | GrTextureDesc desc; |
| 1545 | desc.fWidth = width; |
| 1546 | desc.fHeight = height; |
| 1547 | desc.fConfig = config; |
| 1548 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1549 | GrAutoScratchTexture ast(this, desc); |
| 1550 | GrTexture* texture = ast.texture(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1551 | if (NULL == texture) { |
| 1552 | return; |
| 1553 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1554 | this->internalWriteTexturePixels(texture, 0, 0, width, height, |
| 1555 | config, buffer, rowBytes, flags); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1556 | |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 1557 | GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1558 | GrDrawState* drawState = fGpu->drawState(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1559 | |
| 1560 | GrMatrix matrix; |
| 1561 | matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top)); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1562 | drawState->setViewMatrix(matrix); |
| 1563 | drawState->setRenderTarget(target); |
| 1564 | drawState->setTexture(0, texture); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1565 | |
bsalomon@google.com | 5c63865 | 2011-07-18 19:31:59 +0000 | [diff] [blame] | 1566 | matrix.setIDiv(texture->width(), texture->height()); |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 1567 | drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode, |
| 1568 | GrSamplerState::kNearest_Filter, |
| 1569 | matrix); |
| 1570 | drawState->sampler(0)->setRAndBSwap(swapRAndB); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1571 | |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 1572 | static const GrVertexLayout layout = 0; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1573 | static const int VCOUNT = 4; |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 1574 | // TODO: Use GrGpu::drawRect here |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1575 | GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0); |
| 1576 | if (!geo.succeeded()) { |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 1577 | GrPrintf("Failed to get space for vertices!\n"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1578 | return; |
| 1579 | } |
| 1580 | ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1581 | fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1582 | } |
| 1583 | //////////////////////////////////////////////////////////////////////////////// |
| 1584 | |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1585 | void GrContext::setPaint(const GrPaint& paint) { |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 1586 | GrAssert(fDrawState->stagesDisabled()); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1587 | |
| 1588 | for (int i = 0; i < GrPaint::kMaxTextures; ++i) { |
| 1589 | int s = i + GrPaint::kFirstTextureStage; |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1590 | ASSERT_OWNED_RESOURCE(paint.getTexture(i)); |
tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 1591 | if (paint.isTextureStageEnabled(i)) { |
| 1592 | fDrawState->setTexture(s, paint.getTexture(i)); |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1593 | *fDrawState->sampler(s) = paint.getTextureSampler(i); |
bsalomon@google.com | f864ec4 | 2011-12-12 21:57:03 +0000 | [diff] [blame] | 1594 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1597 | fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1598 | |
| 1599 | for (int i = 0; i < GrPaint::kMaxMasks; ++i) { |
| 1600 | int s = i + GrPaint::kFirstMaskStage; |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1601 | ASSERT_OWNED_RESOURCE(paint.getMask(i)); |
tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 1602 | if (paint.isMaskStageEnabled(i)) { |
| 1603 | fDrawState->setTexture(s, paint.getMask(i)); |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1604 | *fDrawState->sampler(s) = paint.getMaskSampler(i); |
bsalomon@google.com | f864ec4 | 2011-12-12 21:57:03 +0000 | [diff] [blame] | 1605 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1606 | } |
bsalomon@google.com | 26936d0 | 2012-03-19 13:06:19 +0000 | [diff] [blame] | 1607 | |
| 1608 | // disable all stages not accessible via the paint |
| 1609 | for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) { |
tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 1610 | fDrawState->disableStage(s); |
bsalomon@google.com | 26936d0 | 2012-03-19 13:06:19 +0000 | [diff] [blame] | 1611 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1612 | |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1613 | fDrawState->setColor(paint.fColor); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1614 | |
| 1615 | if (paint.fDither) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1616 | fDrawState->enableState(GrDrawState::kDither_StateBit); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1617 | } else { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1618 | fDrawState->disableState(GrDrawState::kDither_StateBit); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1619 | } |
| 1620 | if (paint.fAntiAlias) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1621 | fDrawState->enableState(GrDrawState::kHWAntialias_StateBit); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1622 | } else { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1623 | fDrawState->disableState(GrDrawState::kHWAntialias_StateBit); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1624 | } |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1625 | if (paint.fColorMatrixEnabled) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1626 | fDrawState->enableState(GrDrawState::kColorMatrix_StateBit); |
| 1627 | fDrawState->setColorMatrix(paint.fColorMatrix); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1628 | } else { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1629 | fDrawState->disableState(GrDrawState::kColorMatrix_StateBit); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1630 | } |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1631 | fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff); |
| 1632 | fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode); |
| 1633 | fDrawState->setCoverage(paint.fCoverage); |
reed@google.com | 4b2d3f3 | 2012-05-15 18:05:50 +0000 | [diff] [blame] | 1634 | #if GR_DEBUG_PARTIAL_COVERAGE_CHECK |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 1635 | if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) && |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1636 | !fGpu->canApplyCoverage()) { |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 1637 | GrPrintf("Partial pixel coverage will be incorrectly blended.\n"); |
| 1638 | } |
bsalomon@google.com | 95cd7bd | 2012-03-28 15:35:05 +0000 | [diff] [blame] | 1639 | #endif |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1642 | GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1643 | DrawCategory category) { |
| 1644 | if (category != fLastDrawCategory) { |
bsalomon@google.com | fb4ce6f | 2012-03-14 13:27:54 +0000 | [diff] [blame] | 1645 | this->flushDrawBuffer(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1646 | fLastDrawCategory = category; |
| 1647 | } |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1648 | this->setPaint(paint); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1649 | GrDrawTarget* target = fGpu; |
| 1650 | switch (category) { |
bsalomon@google.com | 193395c | 2012-03-30 17:35:12 +0000 | [diff] [blame] | 1651 | case kUnbuffered_DrawCategory: |
| 1652 | target = fGpu; |
| 1653 | break; |
| 1654 | case kBuffered_DrawCategory: |
| 1655 | target = fDrawBuffer; |
| 1656 | fDrawBuffer->setClip(fGpu->getClip()); |
| 1657 | break; |
| 1658 | default: |
| 1659 | GrCrash("Unexpected DrawCategory."); |
| 1660 | break; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1661 | } |
| 1662 | return target; |
| 1663 | } |
| 1664 | |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1665 | /* |
| 1666 | * This method finds a path renderer that can draw the specified path on |
| 1667 | * the provided target. |
| 1668 | * Due to its expense, the software path renderer has split out so it can |
| 1669 | * can be individually allowed/disallowed via the "allowSW" boolean. |
| 1670 | */ |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 1671 | GrPathRenderer* GrContext::getPathRenderer(const SkPath& path, |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 1672 | GrPathFill fill, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1673 | const GrDrawTarget* target, |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1674 | bool antiAlias, |
| 1675 | bool allowSW) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1676 | if (NULL == fPathRendererChain) { |
| 1677 | fPathRendererChain = |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1678 | SkNEW_ARGS(GrPathRendererChain, |
| 1679 | (this, GrPathRendererChain::kNone_UsageFlag)); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1680 | } |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1681 | |
| 1682 | GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill, |
| 1683 | target, |
| 1684 | antiAlias); |
| 1685 | |
| 1686 | if (NULL == pr && allowSW) { |
| 1687 | if (NULL == fSoftwarePathRenderer) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1688 | fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this)); |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | pr = fSoftwarePathRenderer; |
| 1692 | } |
| 1693 | |
| 1694 | return pr; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1697 | //////////////////////////////////////////////////////////////////////////////// |
| 1698 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1699 | void GrContext::setRenderTarget(GrRenderTarget* target) { |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1700 | ASSERT_OWNED_RESOURCE(target); |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1701 | if (fDrawState->getRenderTarget() != target) { |
bsalomon@google.com | fb4ce6f | 2012-03-14 13:27:54 +0000 | [diff] [blame] | 1702 | this->flush(false); |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1703 | fDrawState->setRenderTarget(target); |
bsalomon@google.com | fb4ce6f | 2012-03-14 13:27:54 +0000 | [diff] [blame] | 1704 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | GrRenderTarget* GrContext::getRenderTarget() { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1708 | return fDrawState->getRenderTarget(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | const GrRenderTarget* GrContext::getRenderTarget() const { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1712 | return fDrawState->getRenderTarget(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 1715 | bool GrContext::isConfigRenderable(GrPixelConfig config) const { |
| 1716 | return fGpu->isConfigRenderable(config); |
| 1717 | } |
| 1718 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1719 | const GrMatrix& GrContext::getMatrix() const { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1720 | return fDrawState->getViewMatrix(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | void GrContext::setMatrix(const GrMatrix& m) { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1724 | fDrawState->setViewMatrix(m); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | void GrContext::concatMatrix(const GrMatrix& m) const { |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1728 | fDrawState->preConcatViewMatrix(m); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) { |
| 1732 | intptr_t mask = 1 << shift; |
| 1733 | if (pred) { |
| 1734 | bits |= mask; |
| 1735 | } else { |
| 1736 | bits &= ~mask; |
| 1737 | } |
| 1738 | return bits; |
| 1739 | } |
| 1740 | |
bsalomon@google.com | 583a1e3 | 2011-08-17 13:42:46 +0000 | [diff] [blame] | 1741 | GrContext::GrContext(GrGpu* gpu) { |
bsalomon@google.com | c0af317 | 2012-06-15 14:10:09 +0000 | [diff] [blame] | 1742 | ++THREAD_INSTANCE_COUNT; |
| 1743 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1744 | fGpu = gpu; |
| 1745 | fGpu->ref(); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1746 | fGpu->setContext(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1747 | |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1748 | fDrawState = SkNEW(GrDrawState); |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1749 | fGpu->setDrawState(fDrawState); |
| 1750 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1751 | fPathRendererChain = NULL; |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1752 | fSoftwarePathRenderer = NULL; |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 1753 | |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1754 | fTextureCache = SkNEW_ARGS(GrResourceCache, |
| 1755 | (MAX_TEXTURE_CACHE_COUNT, |
| 1756 | MAX_TEXTURE_CACHE_BYTES)); |
| 1757 | fFontCache = SkNEW_ARGS(GrFontCache, (fGpu)); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1758 | |
| 1759 | fLastDrawCategory = kUnbuffered_DrawCategory; |
| 1760 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1761 | fDrawBuffer = NULL; |
| 1762 | fDrawBufferVBAllocPool = NULL; |
| 1763 | fDrawBufferIBAllocPool = NULL; |
| 1764 | |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1765 | fAARectRenderer = SkNEW(GrAARectRenderer); |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1766 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1767 | this->setupDrawBuffer(); |
| 1768 | } |
| 1769 | |
| 1770 | void GrContext::setupDrawBuffer() { |
| 1771 | |
| 1772 | GrAssert(NULL == fDrawBuffer); |
| 1773 | GrAssert(NULL == fDrawBufferVBAllocPool); |
| 1774 | GrAssert(NULL == fDrawBufferIBAllocPool); |
| 1775 | |
bsalomon@google.com | 92edd31 | 2012-04-04 21:40:21 +0000 | [diff] [blame] | 1776 | #if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1777 | fDrawBufferVBAllocPool = |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1778 | SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1779 | DRAW_BUFFER_VBPOOL_BUFFER_SIZE, |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1780 | DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS)); |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1781 | fDrawBufferIBAllocPool = |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1782 | SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false, |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1783 | DRAW_BUFFER_IBPOOL_BUFFER_SIZE, |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1784 | DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS)); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1785 | |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1786 | fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu, |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 1787 | fDrawBufferVBAllocPool, |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1788 | fDrawBufferIBAllocPool)); |
bsalomon@google.com | 3c4d032 | 2012-04-03 18:04:51 +0000 | [diff] [blame] | 1789 | #endif |
| 1790 | |
| 1791 | #if BATCH_RECT_TO_RECT |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1792 | fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer()); |
bsalomon@google.com | 3c4d032 | 2012-04-03 18:04:51 +0000 | [diff] [blame] | 1793 | #endif |
bsalomon@google.com | 1015e03 | 2012-06-25 18:41:04 +0000 | [diff] [blame] | 1794 | if (fDrawBuffer) { |
| 1795 | fDrawBuffer->setAutoFlushTarget(fGpu); |
| 1796 | fDrawBuffer->setDrawState(fDrawState); |
| 1797 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1800 | GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1801 | #if DEFER_TEXT_RENDERING |
bsalomon@google.com | 193395c | 2012-03-30 17:35:12 +0000 | [diff] [blame] | 1802 | return prepareToDraw(paint, kBuffered_DrawCategory); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1803 | #else |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 1804 | return prepareToDraw(paint, kUnbuffered_DrawCategory); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1805 | #endif |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | const GrIndexBuffer* GrContext::getQuadIndexBuffer() const { |
| 1809 | return fGpu->getQuadIndexBuffer(); |
| 1810 | } |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 1811 | |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1812 | GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture, |
| 1813 | GrAutoScratchTexture* temp1, |
| 1814 | GrAutoScratchTexture* temp2, |
| 1815 | const SkRect& rect, |
| 1816 | float sigmaX, float sigmaY) { |
senorblanco@chromium.org | ceb4414 | 2012-03-05 20:53:36 +0000 | [diff] [blame] | 1817 | ASSERT_OWNED_RESOURCE(srcTexture); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1818 | GrRenderTarget* oldRenderTarget = this->getRenderTarget(); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1819 | GrTexture* origTexture = srcTexture; |
robertphillips@google.com | fea85ac | 2012-07-11 18:53:23 +0000 | [diff] [blame] | 1820 | AutoMatrix avm(this, GrMatrix::I()); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1821 | SkIRect clearRect; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1822 | int scaleFactorX, radiusX; |
| 1823 | int scaleFactorY, radiusY; |
| 1824 | sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX); |
| 1825 | sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY); |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1826 | |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1827 | SkRect srcRect(rect); |
| 1828 | scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); |
| 1829 | srcRect.roundOut(); |
robertphillips@google.com | 8637a36 | 2012-04-10 18:32:35 +0000 | [diff] [blame] | 1830 | scale_rect(&srcRect, static_cast<float>(scaleFactorX), |
| 1831 | static_cast<float>(scaleFactorY)); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 1832 | |
robertphillips@google.com | 56c79b1 | 2012-07-11 20:57:46 +0000 | [diff] [blame] | 1833 | AutoClip acs(this, srcRect); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1834 | |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 1835 | GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() || |
| 1836 | kRGBA_8888_PM_GrPixelConfig == srcTexture->config() || |
| 1837 | kAlpha_8_GrPixelConfig == srcTexture->config()); |
| 1838 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 1839 | GrTextureDesc desc; |
| 1840 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
| 1841 | desc.fWidth = SkScalarFloorToInt(srcRect.width()); |
| 1842 | desc.fHeight = SkScalarFloorToInt(srcRect.height()); |
| 1843 | desc.fConfig = srcTexture->config(); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1844 | |
| 1845 | temp1->set(this, desc); |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 1846 | if (temp2) { |
| 1847 | temp2->set(this, desc); |
| 1848 | } |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1849 | |
| 1850 | GrTexture* dstTexture = temp1->texture(); |
| 1851 | GrPaint paint; |
| 1852 | paint.reset(); |
| 1853 | paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter); |
| 1854 | |
| 1855 | for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { |
| 1856 | paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(), |
| 1857 | srcTexture->height()); |
| 1858 | this->setRenderTarget(dstTexture->asRenderTarget()); |
| 1859 | SkRect dstRect(srcRect); |
| 1860 | scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f, |
| 1861 | i < scaleFactorY ? 0.5f : 1.0f); |
| 1862 | paint.setTexture(0, srcTexture); |
| 1863 | this->drawRectToRect(paint, dstRect, srcRect); |
| 1864 | srcRect = dstRect; |
| 1865 | SkTSwap(srcTexture, dstTexture); |
| 1866 | // If temp2 is non-NULL, don't render back to origTexture |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 1867 | if (temp2 && dstTexture == origTexture) { |
| 1868 | dstTexture = temp2->texture(); |
| 1869 | } |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1872 | SkIRect srcIRect; |
| 1873 | srcRect.roundOut(&srcIRect); |
| 1874 | |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1875 | if (sigmaX > 0.0f) { |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1876 | if (scaleFactorX > 1) { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1877 | // 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] | 1878 | // X convolution from reading garbage. |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1879 | clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1880 | radiusX, srcIRect.height()); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1881 | this->clear(&clearRect, 0x0); |
| 1882 | } |
| 1883 | |
| 1884 | this->setRenderTarget(dstTexture->asRenderTarget()); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1885 | convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX, |
| 1886 | Gr1DKernelEffect::kX_Direction); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1887 | SkTSwap(srcTexture, dstTexture); |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 1888 | if (temp2 && dstTexture == origTexture) { |
| 1889 | dstTexture = temp2->texture(); |
| 1890 | } |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | if (sigmaY > 0.0f) { |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1894 | if (scaleFactorY > 1 || sigmaX > 0.0f) { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1895 | // Clear out a radius below the srcRect to prevent the Y |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1896 | // convolution from reading garbage. |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1897 | clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1898 | srcIRect.width(), radiusY); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1899 | this->clear(&clearRect, 0x0); |
| 1900 | } |
| 1901 | |
| 1902 | this->setRenderTarget(dstTexture->asRenderTarget()); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1903 | convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY, |
| 1904 | Gr1DKernelEffect::kY_Direction); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1905 | SkTSwap(srcTexture, dstTexture); |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 1906 | if (temp2 && dstTexture == origTexture) { |
| 1907 | dstTexture = temp2->texture(); |
| 1908 | } |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | if (scaleFactorX > 1 || scaleFactorY > 1) { |
| 1912 | // Clear one pixel to the right and below, to accommodate bilinear |
| 1913 | // upsampling. |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1914 | clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, |
| 1915 | srcIRect.width() + 1, 1); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1916 | this->clear(&clearRect, 0x0); |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1917 | clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, |
| 1918 | 1, srcIRect.height()); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1919 | this->clear(&clearRect, 0x0); |
| 1920 | // FIXME: This should be mitchell, not bilinear. |
| 1921 | paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter); |
| 1922 | paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(), |
| 1923 | srcTexture->height()); |
| 1924 | this->setRenderTarget(dstTexture->asRenderTarget()); |
| 1925 | paint.setTexture(0, srcTexture); |
| 1926 | SkRect dstRect(srcRect); |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1927 | scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1928 | this->drawRectToRect(paint, dstRect, srcRect); |
| 1929 | srcRect = dstRect; |
| 1930 | SkTSwap(srcTexture, dstTexture); |
| 1931 | } |
| 1932 | this->setRenderTarget(oldRenderTarget); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1933 | return srcTexture; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 1934 | } |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 1935 | |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1936 | GrTexture* GrContext::applyMorphology(GrTexture* srcTexture, |
| 1937 | const GrRect& rect, |
| 1938 | GrTexture* temp1, GrTexture* temp2, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1939 | MorphologyType morphType, |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1940 | SkISize radius) { |
senorblanco@chromium.org | ceb4414 | 2012-03-05 20:53:36 +0000 | [diff] [blame] | 1941 | ASSERT_OWNED_RESOURCE(srcTexture); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1942 | GrRenderTarget* oldRenderTarget = this->getRenderTarget(); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 1943 | |
robertphillips@google.com | 56c79b1 | 2012-07-11 20:57:46 +0000 | [diff] [blame] | 1944 | AutoMatrix avm(this, GrMatrix::I()); |
| 1945 | |
| 1946 | AutoClip acs(this, GrRect::MakeWH(SkIntToScalar(srcTexture->width()), |
| 1947 | SkIntToScalar(srcTexture->height()))); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 1948 | |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1949 | if (radius.fWidth > 0) { |
| 1950 | this->setRenderTarget(temp1->asRenderTarget()); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1951 | apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType, |
| 1952 | Gr1DKernelEffect::kX_Direction); |
robertphillips@google.com | 7a39633 | 2012-05-10 15:11:27 +0000 | [diff] [blame] | 1953 | SkIRect clearRect = SkIRect::MakeXYWH( |
| 1954 | SkScalarFloorToInt(rect.fLeft), |
| 1955 | SkScalarFloorToInt(rect.fBottom), |
| 1956 | SkScalarFloorToInt(rect.width()), |
| 1957 | radius.fHeight); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1958 | this->clear(&clearRect, 0x0); |
| 1959 | srcTexture = temp1; |
| 1960 | } |
| 1961 | if (radius.fHeight > 0) { |
| 1962 | this->setRenderTarget(temp2->asRenderTarget()); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 1963 | apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType, |
| 1964 | Gr1DKernelEffect::kY_Direction); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1965 | srcTexture = temp2; |
| 1966 | } |
| 1967 | this->setRenderTarget(oldRenderTarget); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 1968 | return srcTexture; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1969 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1970 | |
| 1971 | /////////////////////////////////////////////////////////////////////////////// |