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