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