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