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