epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #include "GrContext.h" |
| 12 | #include "GrTextContext.h" |
| 13 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | #include "SkGpuDevice.h" |
| 15 | #include "SkGrTexturePixelRef.h" |
| 16 | |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 17 | #include "SkColorFilter.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | #include "SkDrawProcs.h" |
| 19 | #include "SkGlyphCache.h" |
reed@google.com | c9aa587 | 2011-04-05 21:05:37 +0000 | [diff] [blame] | 20 | #include "SkUtils.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | |
| 22 | #define CACHE_LAYER_TEXTURES 1 |
| 23 | |
| 24 | #if 0 |
| 25 | extern bool (*gShouldDrawProc)(); |
| 26 | #define CHECK_SHOULD_DRAW(draw) \ |
| 27 | do { \ |
| 28 | if (gShouldDrawProc && !gShouldDrawProc()) return; \ |
| 29 | this->prepareRenderTarget(draw); \ |
| 30 | } while (0) |
| 31 | #else |
| 32 | #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw) |
| 33 | #endif |
| 34 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 35 | // we use the same texture slot on GrPaint for bitmaps and shaders |
| 36 | // (since drawBitmap, drawSprite, and drawDevice ignore skia's shader) |
| 37 | enum { |
| 38 | kBitmapTextureIdx = 0, |
| 39 | kShaderTextureIdx = 0 |
| 40 | }; |
| 41 | |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 42 | |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 43 | #define MAX_BLUR_SIGMA 4.0f |
| 44 | // FIXME: This value comes from from SkBlurMaskFilter.cpp. |
| 45 | // Should probably be put in a common header someplace. |
| 46 | #define MAX_BLUR_RADIUS SkIntToScalar(128) |
| 47 | // This constant approximates the scaling done in the software path's |
| 48 | // "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)). |
| 49 | // IMHO, it actually should be 1: we blur "less" than we should do |
| 50 | // according to the CSS and canvas specs, simply because Safari does the same. |
| 51 | // Firefox used to do the same too, until 4.0 where they fixed it. So at some |
| 52 | // point we should probably get rid of these scaling constants and rebaseline |
| 53 | // all the blur tests. |
| 54 | #define BLUR_SIGMA_SCALE 0.6f |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 55 | /////////////////////////////////////////////////////////////////////////////// |
| 56 | |
| 57 | SkGpuDevice::SkAutoCachedTexture:: |
| 58 | SkAutoCachedTexture(SkGpuDevice* device, |
| 59 | const SkBitmap& bitmap, |
| 60 | const GrSamplerState& sampler, |
| 61 | GrTexture** texture) { |
| 62 | GrAssert(texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | *texture = this->set(device, bitmap, sampler); |
| 64 | } |
| 65 | |
| 66 | SkGpuDevice::SkAutoCachedTexture::SkAutoCachedTexture() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | GrTexture* SkGpuDevice::SkAutoCachedTexture::set(SkGpuDevice* device, |
| 70 | const SkBitmap& bitmap, |
| 71 | const GrSamplerState& sampler) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 72 | if (fTex.texture()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 73 | fDevice->unlockCachedTexture(fTex); |
| 74 | } |
| 75 | fDevice = device; |
| 76 | GrTexture* texture = (GrTexture*)bitmap.getTexture(); |
| 77 | if (texture) { |
| 78 | // return the native texture |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 79 | fTex.reset(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | } else { |
| 81 | // look it up in our cache |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 82 | fTex = device->lockCachedTexture(bitmap, sampler); |
| 83 | texture = fTex.texture(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | } |
| 85 | return texture; |
| 86 | } |
| 87 | |
| 88 | SkGpuDevice::SkAutoCachedTexture::~SkAutoCachedTexture() { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 89 | if (fTex.texture()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 90 | fDevice->unlockCachedTexture(fTex); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /////////////////////////////////////////////////////////////////////////////// |
| 95 | |
| 96 | bool gDoTraceDraw; |
| 97 | |
| 98 | struct GrSkDrawProcs : public SkDrawProcs { |
| 99 | public: |
| 100 | GrContext* fContext; |
| 101 | GrTextContext* fTextContext; |
| 102 | GrFontScaler* fFontScaler; // cached in the skia glyphcache |
| 103 | }; |
| 104 | |
| 105 | /////////////////////////////////////////////////////////////////////////////// |
| 106 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 107 | GrRenderTarget* SkGpuDevice::Current3DApiRenderTarget() { |
| 108 | return (GrRenderTarget*) -1; |
| 109 | } |
| 110 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 111 | static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) { |
| 112 | switch (config) { |
| 113 | case kAlpha_8_GrPixelConfig: |
| 114 | *isOpaque = false; |
| 115 | return SkBitmap::kA8_Config; |
| 116 | case kRGB_565_GrPixelConfig: |
| 117 | *isOpaque = true; |
| 118 | return SkBitmap::kRGB_565_Config; |
| 119 | case kRGBA_4444_GrPixelConfig: |
| 120 | *isOpaque = false; |
| 121 | return SkBitmap::kARGB_4444_Config; |
| 122 | case kRGBA_8888_GrPixelConfig: |
| 123 | case kRGBX_8888_GrPixelConfig: |
| 124 | *isOpaque = (kRGBX_8888_GrPixelConfig == config); |
| 125 | return SkBitmap::kARGB_8888_Config; |
| 126 | default: |
| 127 | *isOpaque = false; |
| 128 | return SkBitmap::kNo_Config; |
| 129 | } |
| 130 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 131 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 132 | static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) { |
bsalomon@google.com | a44f700 | 2011-08-09 15:30:41 +0000 | [diff] [blame] | 133 | SkAutoTUnref<GrRenderTarget> rtunref; |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 134 | if (SkGpuDevice::Current3DApiRenderTarget() == renderTarget) { |
| 135 | renderTarget = context->createRenderTargetFrom3DApiState(); |
bsalomon@google.com | a44f700 | 2011-08-09 15:30:41 +0000 | [diff] [blame] | 136 | rtunref.reset(renderTarget); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 137 | } |
| 138 | GrTexture* texture = renderTarget->asTexture(); |
| 139 | GrPixelConfig config = texture ? texture->config() : kRGBA_8888_GrPixelConfig; |
| 140 | |
| 141 | bool isOpaque; |
| 142 | SkBitmap bitmap; |
| 143 | bitmap.setConfig(grConfig2skConfig(config, &isOpaque), |
| 144 | renderTarget->width(), renderTarget->height()); |
| 145 | bitmap.setIsOpaque(isOpaque); |
| 146 | return bitmap; |
| 147 | } |
| 148 | |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 149 | SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture) |
| 150 | : SkDevice(make_bitmap(context, texture->asRenderTarget())) { |
| 151 | this->initFromRenderTarget(context, texture->asRenderTarget()); |
| 152 | } |
| 153 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 154 | SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget) |
| 155 | : SkDevice(make_bitmap(context, renderTarget)) { |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 156 | this->initFromRenderTarget(context, renderTarget); |
| 157 | } |
| 158 | |
| 159 | void SkGpuDevice::initFromRenderTarget(GrContext* context, |
| 160 | GrRenderTarget* renderTarget) { |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 161 | fNeedPrepareRenderTarget = false; |
| 162 | fDrawProcs = NULL; |
| 163 | |
| 164 | fContext = context; |
| 165 | fContext->ref(); |
| 166 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 167 | fTexture = NULL; |
| 168 | fRenderTarget = NULL; |
| 169 | fNeedClear = false; |
| 170 | |
| 171 | if (Current3DApiRenderTarget() == renderTarget) { |
| 172 | fRenderTarget = fContext->createRenderTargetFrom3DApiState(); |
| 173 | } else { |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 174 | GrAssert(NULL != renderTarget); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 175 | fRenderTarget = renderTarget; |
| 176 | fRenderTarget->ref(); |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 177 | // if this RT is also a texture, hold a ref on it |
| 178 | fTexture = fRenderTarget->asTexture(); |
| 179 | SkSafeRef(fTexture); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | SkGrRenderTargetPixelRef* pr = new SkGrRenderTargetPixelRef(fRenderTarget); |
| 183 | this->setPixelRef(pr, 0)->unref(); |
| 184 | } |
| 185 | |
| 186 | SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width, |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 187 | int height, Usage usage) |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 188 | : SkDevice(config, width, height, false /*isOpaque*/) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 189 | fNeedPrepareRenderTarget = false; |
| 190 | fDrawProcs = NULL; |
| 191 | |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 192 | fContext = context; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 193 | fContext->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 194 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 195 | fTexture = NULL; |
| 196 | fRenderTarget = NULL; |
| 197 | fNeedClear = false; |
| 198 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 199 | if (config != SkBitmap::kRGB_565_Config) { |
| 200 | config = SkBitmap::kARGB_8888_Config; |
| 201 | } |
| 202 | SkBitmap bm; |
| 203 | bm.setConfig(config, width, height); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 204 | |
| 205 | #if CACHE_LAYER_TEXTURES |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 206 | TexType type = (kSaveLayer_Usage == usage) ? |
| 207 | kSaveLayerDeviceRenderTarget_TexType : |
| 208 | kDeviceRenderTarget_TexType; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 209 | fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNoFilter(), type); |
| 210 | fTexture = fCache.texture(); |
| 211 | if (fTexture) { |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 212 | SkASSERT(NULL != fTexture->asRenderTarget()); |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 213 | // hold a ref directly on fTexture (even though fCache has one) to match |
| 214 | // other constructor paths. Simplifies cleanup. |
| 215 | fTexture->ref(); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 216 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 217 | #else |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 218 | const GrTextureDesc desc = { |
| 219 | kRenderTarget_GrTextureFlagBit, |
| 220 | kNone_GrAALevel, |
| 221 | width, |
| 222 | height, |
| 223 | SkGr::Bitmap2PixelConfig(bm) |
| 224 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 225 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 226 | fTexture = fContext->createUncachedTexture(desc, NULL, 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 227 | #endif |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 228 | if (NULL != fTexture) { |
| 229 | fRenderTarget = fTexture->asRenderTarget(); |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 230 | fRenderTarget->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 231 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 232 | GrAssert(NULL != fRenderTarget); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 233 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 234 | // we defer the actual clear until our gainFocus() |
| 235 | fNeedClear = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 236 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 237 | // wrap the bitmap with a pixelref to expose our texture |
| 238 | SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 239 | this->setPixelRef(pr, 0)->unref(); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 240 | } else { |
| 241 | GrPrintf("--- failed to create gpu-offscreen [%d %d]\n", |
| 242 | width, height); |
| 243 | GrAssert(false); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | SkGpuDevice::~SkGpuDevice() { |
| 248 | if (fDrawProcs) { |
| 249 | delete fDrawProcs; |
| 250 | } |
| 251 | |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 252 | SkSafeUnref(fTexture); |
| 253 | SkSafeUnref(fRenderTarget); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 254 | if (fCache.texture()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 255 | GrAssert(NULL != fTexture); |
| 256 | GrAssert(fRenderTarget == fTexture->asRenderTarget()); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 257 | fContext->unlockTexture(fCache); |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 258 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 259 | fContext->unref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 260 | } |
| 261 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 262 | /////////////////////////////////////////////////////////////////////////////// |
| 263 | |
| 264 | void SkGpuDevice::makeRenderTargetCurrent() { |
| 265 | fContext->setRenderTarget(fRenderTarget); |
| 266 | fContext->flush(true); |
| 267 | fNeedPrepareRenderTarget = true; |
| 268 | } |
| 269 | |
| 270 | /////////////////////////////////////////////////////////////////////////////// |
| 271 | |
| 272 | bool SkGpuDevice::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) { |
| 273 | SkIRect bounds; |
| 274 | bounds.set(0, 0, this->width(), this->height()); |
| 275 | if (!bounds.intersect(srcRect)) { |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | const int w = bounds.width(); |
| 280 | const int h = bounds.height(); |
| 281 | SkBitmap tmp; |
| 282 | // note we explicitly specify our rowBytes to be snug (no gap between rows) |
| 283 | tmp.setConfig(SkBitmap::kARGB_8888_Config, w, h, w * 4); |
| 284 | if (!tmp.allocPixels()) { |
| 285 | return false; |
| 286 | } |
| 287 | |
Scroggo | 813c33c | 2011-04-07 20:56:21 +0000 | [diff] [blame] | 288 | tmp.lockPixels(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 289 | |
Scroggo | eb17603 | 2011-04-07 21:11:49 +0000 | [diff] [blame] | 290 | bool read = fContext->readRenderTargetPixels(fRenderTarget, |
| 291 | bounds.fLeft, bounds.fTop, |
| 292 | bounds.width(), bounds.height(), |
| 293 | kRGBA_8888_GrPixelConfig, |
| 294 | tmp.getPixels()); |
Scroggo | 813c33c | 2011-04-07 20:56:21 +0000 | [diff] [blame] | 295 | tmp.unlockPixels(); |
| 296 | if (!read) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 297 | return false; |
| 298 | } |
| 299 | |
| 300 | tmp.swap(*bitmap); |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) { |
| 305 | SkAutoLockPixels alp(bitmap); |
| 306 | if (!bitmap.readyToDraw()) { |
| 307 | return; |
| 308 | } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 309 | GrPixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(), |
| 310 | bitmap.isOpaque()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 311 | fContext->setRenderTarget(fRenderTarget); |
| 312 | // we aren't setting the clip or matrix, so mark as dirty |
| 313 | // we don't need to set them for this call and don't have them anyway |
| 314 | fNeedPrepareRenderTarget = true; |
| 315 | |
| 316 | fContext->writePixels(x, y, bitmap.width(), bitmap.height(), |
| 317 | config, bitmap.getPixels(), bitmap.rowBytes()); |
| 318 | } |
| 319 | |
| 320 | /////////////////////////////////////////////////////////////////////////////// |
| 321 | |
| 322 | static void convert_matrixclip(GrContext* context, const SkMatrix& matrix, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 323 | const SkClipStack& clipStack, |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 324 | const SkRegion& clipRegion, |
| 325 | const SkIPoint& origin) { |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 326 | context->setMatrix(matrix); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 327 | |
| 328 | SkGrClipIterator iter; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 329 | iter.reset(clipStack); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 330 | const SkIRect& skBounds = clipRegion.getBounds(); |
| 331 | GrRect bounds; |
| 332 | bounds.setLTRB(GrIntToScalar(skBounds.fLeft), |
| 333 | GrIntToScalar(skBounds.fTop), |
| 334 | GrIntToScalar(skBounds.fRight), |
| 335 | GrIntToScalar(skBounds.fBottom)); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 336 | GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()), |
| 337 | &bounds); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 338 | context->setClip(grc); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // call this ever each draw call, to ensure that the context reflects our state, |
| 342 | // and not the state from some other canvas/device |
| 343 | void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) { |
| 344 | if (fNeedPrepareRenderTarget || |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 345 | fContext->getRenderTarget() != fRenderTarget) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 346 | |
| 347 | fContext->setRenderTarget(fRenderTarget); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 348 | SkASSERT(draw.fClipStack); |
| 349 | convert_matrixclip(fContext, *draw.fMatrix, |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 350 | *draw.fClipStack, *draw.fClip, this->getOrigin()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 351 | fNeedPrepareRenderTarget = false; |
| 352 | } |
| 353 | } |
| 354 | |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 355 | void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip, |
| 356 | const SkClipStack& clipStack) { |
| 357 | this->INHERITED::setMatrixClip(matrix, clip, clipStack); |
bsalomon@google.com | a7bf6e2 | 2011-04-11 19:20:46 +0000 | [diff] [blame] | 358 | // We don't need to set them now because the context may not reflect this device. |
| 359 | fNeedPrepareRenderTarget = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 363 | const SkRegion& clip, const SkClipStack& clipStack) { |
| 364 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 365 | fContext->setRenderTarget(fRenderTarget); |
| 366 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 367 | this->INHERITED::gainFocus(canvas, matrix, clip, clipStack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 368 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 369 | convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 370 | |
| 371 | if (fNeedClear) { |
bsalomon@google.com | 31a5840 | 2011-04-27 21:00:02 +0000 | [diff] [blame] | 372 | fContext->clear(NULL, 0x0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 373 | fNeedClear = false; |
| 374 | } |
| 375 | } |
| 376 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 377 | bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 378 | if (NULL != fTexture) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 379 | paint->setTexture(kBitmapTextureIdx, fTexture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 380 | return true; |
| 381 | } |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | /////////////////////////////////////////////////////////////////////////////// |
| 386 | |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 387 | SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch); |
| 388 | SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch); |
| 389 | SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch); |
| 390 | SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch); |
| 391 | SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4, |
| 392 | shader_type_mismatch); |
| 393 | SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 394 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 395 | static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = { |
| 396 | (GrSamplerState::SampleMode) -1, // kNone_BitmapType |
| 397 | GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType |
| 398 | GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType |
| 399 | GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType |
| 400 | GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType |
| 401 | }; |
| 402 | |
| 403 | bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint, |
| 404 | bool justAlpha, |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 405 | GrPaint* grPaint, |
| 406 | bool constantColor) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 407 | |
| 408 | grPaint->fDither = skPaint.isDither(); |
| 409 | grPaint->fAntiAlias = skPaint.isAntiAlias(); |
| 410 | |
| 411 | SkXfermode::Coeff sm = SkXfermode::kOne_Coeff; |
| 412 | SkXfermode::Coeff dm = SkXfermode::kISA_Coeff; |
| 413 | |
| 414 | SkXfermode* mode = skPaint.getXfermode(); |
| 415 | if (mode) { |
| 416 | if (!mode->asCoeff(&sm, &dm)) { |
reed@google.com | 1a2e8d2 | 2011-01-21 22:08:29 +0000 | [diff] [blame] | 417 | SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");) |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 418 | #if 0 |
| 419 | return false; |
| 420 | #endif |
| 421 | } |
| 422 | } |
| 423 | grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm); |
| 424 | grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm); |
| 425 | |
| 426 | if (justAlpha) { |
| 427 | uint8_t alpha = skPaint.getAlpha(); |
| 428 | grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha); |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 429 | // justAlpha is currently set to true only if there is a texture, |
| 430 | // so constantColor should not also be true. |
| 431 | GrAssert(!constantColor); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 432 | } else { |
| 433 | grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor()); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 434 | grPaint->setTexture(kShaderTextureIdx, NULL); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 435 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 436 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 437 | SkColor color; |
| 438 | SkXfermode::Mode filterMode; |
| 439 | if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) { |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 440 | if (!constantColor) { |
| 441 | grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color); |
| 442 | grPaint->fColorFilterXfermode = filterMode; |
| 443 | return true; |
| 444 | } |
| 445 | SkColor filtered = colorFilter->filterColor(skPaint.getColor()); |
| 446 | grPaint->fColor = SkGr::SkColor2GrColor(filtered); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 447 | } |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 448 | grPaint->resetColorFilter(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 449 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 450 | } |
| 451 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 452 | bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint, |
| 453 | SkAutoCachedTexture* act, |
| 454 | const SkMatrix& ctm, |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 455 | GrPaint* grPaint, |
| 456 | bool constantColor) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 457 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 458 | SkASSERT(NULL != act); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 459 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 460 | SkShader* shader = skPaint.getShader(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 461 | if (NULL == shader) { |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 462 | return this->skPaint2GrPaintNoShader(skPaint, |
| 463 | false, |
| 464 | grPaint, |
| 465 | constantColor); |
| 466 | } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 467 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 468 | } |
| 469 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 470 | SkBitmap bitmap; |
| 471 | SkMatrix matrix; |
| 472 | SkShader::TileMode tileModes[2]; |
| 473 | SkScalar twoPointParams[3]; |
| 474 | SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix, |
| 475 | tileModes, twoPointParams); |
| 476 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 477 | GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype]; |
| 478 | if (-1 == sampleMode) { |
reed@google.com | 2be9e8b | 2011-07-06 21:18:09 +0000 | [diff] [blame] | 479 | SkShader::GradientInfo info; |
| 480 | SkColor color; |
| 481 | |
| 482 | info.fColors = &color; |
| 483 | info.fColorOffsets = NULL; |
| 484 | info.fColorCount = 1; |
| 485 | if (SkShader::kColor_GradientType == shader->asAGradient(&info)) { |
| 486 | SkPaint copy(skPaint); |
| 487 | copy.setShader(NULL); |
bsalomon@google.com | cd9cfd7 | 2011-07-08 16:55:04 +0000 | [diff] [blame] | 488 | // modulate the paint alpha by the shader's solid color alpha |
| 489 | U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha()); |
| 490 | copy.setColor(SkColorSetA(color, newA)); |
reed@google.com | 2be9e8b | 2011-07-06 21:18:09 +0000 | [diff] [blame] | 491 | return this->skPaint2GrPaintNoShader(copy, |
| 492 | false, |
| 493 | grPaint, |
| 494 | constantColor); |
| 495 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 496 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 497 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 498 | GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx); |
| 499 | sampler->setSampleMode(sampleMode); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 500 | if (skPaint.isFilterBitmap()) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 501 | sampler->setFilter(GrSamplerState::kBilinear_Filter); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 502 | } else { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 503 | sampler->setFilter(GrSamplerState::kNearest_Filter); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 504 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 505 | sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0])); |
| 506 | sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1])); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 507 | if (GrSamplerState::kRadial2_SampleMode == sampleMode) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 508 | sampler->setRadial2Params(twoPointParams[0], |
| 509 | twoPointParams[1], |
| 510 | twoPointParams[2] < 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 511 | } |
| 512 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 513 | GrTexture* texture = act->set(this, bitmap, *sampler); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 514 | if (NULL == texture) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 515 | SkDebugf("Couldn't convert bitmap to texture.\n"); |
| 516 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 517 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 518 | grPaint->setTexture(kShaderTextureIdx, texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 519 | |
| 520 | // since our texture coords will be in local space, we wack the texture |
| 521 | // matrix to map them back into 0...1 before we load it |
| 522 | SkMatrix localM; |
| 523 | if (shader->getLocalMatrix(&localM)) { |
| 524 | SkMatrix inverse; |
| 525 | if (localM.invert(&inverse)) { |
| 526 | matrix.preConcat(inverse); |
| 527 | } |
| 528 | } |
| 529 | if (SkShader::kDefault_BitmapType == bmptype) { |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 530 | GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width()); |
| 531 | GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 532 | matrix.postScale(sx, sy); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 533 | } else if (SkShader::kRadial_BitmapType == bmptype) { |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 534 | GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 535 | matrix.postScale(s, s); |
| 536 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 537 | sampler->setMatrix(matrix); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 538 | |
| 539 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 543 | |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 544 | void SkGpuDevice::clear(SkColor color) { |
bsalomon@google.com | 31a5840 | 2011-04-27 21:00:02 +0000 | [diff] [blame] | 545 | fContext->clear(NULL, color); |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 546 | } |
| 547 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 548 | void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) { |
| 549 | CHECK_SHOULD_DRAW(draw); |
| 550 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 551 | GrPaint grPaint; |
| 552 | SkAutoCachedTexture act; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 553 | if (!this->skPaint2GrPaintShader(paint, |
| 554 | &act, |
| 555 | *draw.fMatrix, |
| 556 | &grPaint, |
| 557 | true)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 558 | return; |
| 559 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 560 | |
| 561 | fContext->drawPaint(grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | // must be in SkCanvas::PointMode order |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 565 | static const GrPrimitiveType gPointMode2PrimtiveType[] = { |
| 566 | kPoints_PrimitiveType, |
| 567 | kLines_PrimitiveType, |
| 568 | kLineStrip_PrimitiveType |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 569 | }; |
| 570 | |
| 571 | void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 572 | size_t count, const SkPoint pts[], const SkPaint& paint) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 573 | CHECK_SHOULD_DRAW(draw); |
| 574 | |
| 575 | SkScalar width = paint.getStrokeWidth(); |
| 576 | if (width < 0) { |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | // we only handle hairlines here, else we let the SkDraw call our drawPath() |
| 581 | if (width > 0) { |
| 582 | draw.drawPoints(mode, count, pts, paint, true); |
| 583 | return; |
| 584 | } |
| 585 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 586 | GrPaint grPaint; |
| 587 | SkAutoCachedTexture act; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 588 | if (!this->skPaint2GrPaintShader(paint, |
| 589 | &act, |
| 590 | *draw.fMatrix, |
| 591 | &grPaint, |
| 592 | true)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 593 | return; |
| 594 | } |
| 595 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 596 | fContext->drawVertices(grPaint, |
| 597 | gPointMode2PrimtiveType[mode], |
| 598 | count, |
| 599 | (GrPoint*)pts, |
| 600 | NULL, |
| 601 | NULL, |
| 602 | NULL, |
| 603 | 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 604 | } |
| 605 | |
reed@google.com | c9aa587 | 2011-04-05 21:05:37 +0000 | [diff] [blame] | 606 | /////////////////////////////////////////////////////////////////////////////// |
| 607 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 608 | void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, |
| 609 | const SkPaint& paint) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 610 | CHECK_SHOULD_DRAW(draw); |
| 611 | |
bungeman@google.com | 79bd877 | 2011-07-18 15:34:08 +0000 | [diff] [blame] | 612 | bool doStroke = paint.getStyle() != SkPaint::kFill_Style; |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 613 | SkScalar width = paint.getStrokeWidth(); |
| 614 | |
| 615 | /* |
| 616 | We have special code for hairline strokes, miter-strokes, and fills. |
| 617 | Anything else we just call our path code. |
| 618 | */ |
| 619 | bool usePath = doStroke && width > 0 && |
| 620 | paint.getStrokeJoin() != SkPaint::kMiter_Join; |
| 621 | // another reason we might need to call drawPath... |
| 622 | if (paint.getMaskFilter()) { |
| 623 | usePath = true; |
| 624 | } |
reed@google.com | 67db664 | 2011-05-26 11:46:35 +0000 | [diff] [blame] | 625 | // until we aa rotated rects... |
| 626 | if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) { |
| 627 | usePath = true; |
| 628 | } |
bungeman@google.com | 633722e | 2011-08-09 18:32:51 +0000 | [diff] [blame] | 629 | // small miter limit means right angles show bevel... |
| 630 | if (SkPaint::kMiter_Join == paint.getStrokeJoin() && |
| 631 | paint.getStrokeMiter() < SK_ScalarSqrt2) |
| 632 | { |
| 633 | usePath = true; |
| 634 | } |
bungeman@google.com | 79bd877 | 2011-07-18 15:34:08 +0000 | [diff] [blame] | 635 | // until we can both stroke and fill rectangles |
bungeman@google.com | 79bd877 | 2011-07-18 15:34:08 +0000 | [diff] [blame] | 636 | if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 637 | usePath = true; |
| 638 | } |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 639 | |
| 640 | if (usePath) { |
| 641 | SkPath path; |
| 642 | path.addRect(rect); |
| 643 | this->drawPath(draw, path, paint, NULL, true); |
| 644 | return; |
| 645 | } |
| 646 | |
| 647 | GrPaint grPaint; |
| 648 | SkAutoCachedTexture act; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 649 | if (!this->skPaint2GrPaintShader(paint, |
| 650 | &act, |
| 651 | *draw.fMatrix, |
| 652 | &grPaint, |
| 653 | true)) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 654 | return; |
| 655 | } |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 656 | fContext->drawRect(grPaint, rect, doStroke ? width : -1); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 657 | } |
| 658 | |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 659 | #include "SkMaskFilter.h" |
| 660 | #include "SkBounder.h" |
| 661 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 662 | static GrPathFill skToGrFillType(SkPath::FillType fillType) { |
| 663 | switch (fillType) { |
| 664 | case SkPath::kWinding_FillType: |
| 665 | return kWinding_PathFill; |
| 666 | case SkPath::kEvenOdd_FillType: |
| 667 | return kEvenOdd_PathFill; |
| 668 | case SkPath::kInverseWinding_FillType: |
| 669 | return kInverseWinding_PathFill; |
| 670 | case SkPath::kInverseEvenOdd_FillType: |
| 671 | return kInverseEvenOdd_PathFill; |
| 672 | default: |
| 673 | SkDebugf("Unsupported path fill type\n"); |
| 674 | return kHairLine_PathFill; |
| 675 | } |
| 676 | } |
| 677 | |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 678 | static void buildKernel(float sigma, float* kernel, int kernelWidth) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 679 | int halfWidth = (kernelWidth - 1) / 2; |
| 680 | float sum = 0.0f; |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 681 | float denom = 1.0f / (2.0f * sigma * sigma); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 682 | for (int i = 0; i < kernelWidth; ++i) { |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 683 | float x = static_cast<float>(i - halfWidth); |
| 684 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 685 | // is dropped here, since we renormalize the kernel below. |
| 686 | kernel[i] = sk_float_exp(- x * x * denom); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 687 | sum += kernel[i]; |
| 688 | } |
| 689 | // Normalize the kernel |
| 690 | float scale = 1.0f / sum; |
| 691 | for (int i = 0; i < kernelWidth; ++i) |
| 692 | kernel[i] *= scale; |
| 693 | } |
| 694 | |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 695 | static void scaleRect(SkRect* rect, float scale) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 696 | rect->fLeft *= scale; |
| 697 | rect->fTop *= scale; |
| 698 | rect->fRight *= scale; |
| 699 | rect->fBottom *= scale; |
| 700 | } |
| 701 | |
| 702 | static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path, |
| 703 | SkMaskFilter* filter, const SkMatrix& matrix, |
| 704 | const SkRegion& clip, SkBounder* bounder, |
| 705 | GrPaint* grp) { |
senorblanco@chromium.org | a479fc7 | 2011-07-19 16:40:58 +0000 | [diff] [blame] | 706 | #ifdef SK_DISABLE_GPU_BLUR |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 707 | return false; |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 708 | #endif |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 709 | SkMaskFilter::BlurInfo info; |
| 710 | SkMaskFilter::BlurType blurType = filter->asABlur(&info); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 711 | if (SkMaskFilter::kNone_BlurType == blurType) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 712 | return false; |
| 713 | } |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 714 | SkScalar radius = info.fIgnoreTransform ? info.fRadius |
| 715 | : matrix.mapRadius(info.fRadius); |
| 716 | radius = SkMinScalar(radius, MAX_BLUR_RADIUS); |
senorblanco@chromium.org | 68c4d12 | 2011-08-01 21:20:31 +0000 | [diff] [blame] | 717 | if (radius <= 0) { |
| 718 | return false; |
| 719 | } |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 720 | float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 721 | SkRect srcRect = path.getBounds(); |
| 722 | |
| 723 | int scaleFactor = 1; |
| 724 | |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 725 | while (sigma > MAX_BLUR_SIGMA) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 726 | scaleFactor *= 2; |
| 727 | sigma *= 0.5f; |
| 728 | } |
senorblanco@chromium.org | 4a947d2 | 2011-07-18 21:48:35 +0000 | [diff] [blame] | 729 | int halfWidth = static_cast<int>(ceilf(sigma * 3.0f)); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 730 | int kernelWidth = halfWidth * 2 + 1; |
| 731 | |
senorblanco@chromium.org | 68c4d12 | 2011-08-01 21:20:31 +0000 | [diff] [blame] | 732 | float invScale = 1.0f / scaleFactor; |
| 733 | scaleRect(&srcRect, invScale); |
| 734 | srcRect.roundOut(); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 735 | srcRect.inset(-halfWidth, -halfWidth); |
| 736 | |
senorblanco@chromium.org | 68c4d12 | 2011-08-01 21:20:31 +0000 | [diff] [blame] | 737 | SkRect clipBounds; |
| 738 | clipBounds.set(clip.getBounds()); |
| 739 | scaleRect(&clipBounds, invScale); |
| 740 | clipBounds.roundOut(); |
| 741 | clipBounds.inset(-halfWidth, -halfWidth); |
| 742 | |
| 743 | srcRect.intersect(clipBounds); |
| 744 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 745 | scaleRect(&srcRect, scaleFactor); |
| 746 | SkRect finalRect = srcRect; |
| 747 | |
| 748 | SkIRect finalIRect; |
| 749 | finalRect.roundOut(&finalIRect); |
| 750 | if (clip.quickReject(finalIRect)) { |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 751 | return true; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 752 | } |
| 753 | if (bounder && !bounder->doIRect(finalIRect)) { |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 754 | return true; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 755 | } |
| 756 | GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop); |
| 757 | srcRect.offset(-srcRect.fLeft, -srcRect.fTop); |
| 758 | const GrTextureDesc desc = { |
| 759 | kRenderTarget_GrTextureFlagBit, |
| 760 | kNone_GrAALevel, |
| 761 | srcRect.width(), |
| 762 | srcRect.height(), |
| 763 | // We actually only need A8, but it often isn't supported as a |
| 764 | // render target |
| 765 | kRGBA_8888_GrPixelConfig |
| 766 | }; |
| 767 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 768 | GrAutoScratchTexture srcEntry(context, desc); |
| 769 | GrAutoScratchTexture dstEntry(context, desc); |
| 770 | if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 771 | return false; |
| 772 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 773 | GrTexture* srcTexture = srcEntry.texture(); |
| 774 | GrTexture* dstTexture = dstEntry.texture(); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 775 | if (NULL == srcTexture || NULL == dstTexture) { |
| 776 | return false; |
| 777 | } |
| 778 | GrRenderTarget* oldRenderTarget = context->getRenderTarget(); |
senorblanco@chromium.org | 42dd0f9 | 2011-07-14 15:29:57 +0000 | [diff] [blame] | 779 | // Once this code moves into GrContext, this should be changed to use |
| 780 | // an AutoClipRestore. |
| 781 | GrClip oldClip = context->getClip(); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 782 | context->setRenderTarget(dstTexture->asRenderTarget()); |
senorblanco@chromium.org | 42dd0f9 | 2011-07-14 15:29:57 +0000 | [diff] [blame] | 783 | context->setClip(srcRect); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 784 | context->clear(NULL, 0); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 785 | GrPaint tempPaint; |
| 786 | tempPaint.reset(); |
| 787 | |
| 788 | GrAutoMatrix avm(context, GrMatrix::I()); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 789 | tempPaint.fAntiAlias = grp->fAntiAlias; |
| 790 | if (tempPaint.fAntiAlias) { |
| 791 | // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst |
| 792 | // blend coeff of zero requires dual source blending support in order |
| 793 | // to properly blend partially covered pixels. This means the AA |
| 794 | // code path may not be taken. So we use a dst blend coeff of ISA. We |
| 795 | // could special case AA draws to a dst surface with known alpha=0 to |
| 796 | // use a zero dst coeff when dual source blending isn't available. |
| 797 | tempPaint.fSrcBlendCoeff = kOne_BlendCoeff; |
| 798 | tempPaint.fDstBlendCoeff = kISC_BlendCoeff; |
| 799 | } |
| 800 | // Draw hard shadow to dstTexture with path topleft at origin 0,0. |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 801 | context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 802 | SkTSwap(srcTexture, dstTexture); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 803 | |
| 804 | GrMatrix sampleM; |
senorblanco@chromium.org | 422b67d | 2011-07-19 21:22:13 +0000 | [diff] [blame] | 805 | sampleM.setIDiv(srcTexture->width(), srcTexture->height()); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 806 | GrPaint paint; |
| 807 | paint.reset(); |
| 808 | paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter); |
| 809 | paint.getTextureSampler(0)->setMatrix(sampleM); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 810 | GrAutoScratchTexture origEntry; |
| 811 | |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 812 | if (blurType != SkMaskFilter::kNormal_BlurType) { |
| 813 | // Stash away a copy of the unblurred image. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 814 | origEntry.set(context, desc); |
| 815 | if (NULL == origEntry.texture()) { |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 816 | return false; |
| 817 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 818 | context->setRenderTarget(origEntry.texture()->asRenderTarget()); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 819 | paint.setTexture(0, srcTexture); |
| 820 | context->drawRect(paint, srcRect); |
| 821 | } |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 822 | for (int i = 1; i < scaleFactor; i *= 2) { |
senorblanco@chromium.org | 422b67d | 2011-07-19 21:22:13 +0000 | [diff] [blame] | 823 | sampleM.setIDiv(srcTexture->width(), srcTexture->height()); |
| 824 | paint.getTextureSampler(0)->setMatrix(sampleM); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 825 | context->setRenderTarget(dstTexture->asRenderTarget()); |
| 826 | SkRect dstRect(srcRect); |
| 827 | scaleRect(&dstRect, 0.5f); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 828 | paint.setTexture(0, srcTexture); |
| 829 | context->drawRectToRect(paint, dstRect, srcRect); |
| 830 | srcRect = dstRect; |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 831 | SkTSwap(srcTexture, dstTexture); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | SkAutoTMalloc<float> kernelStorage(kernelWidth); |
| 835 | float* kernel = kernelStorage.get(); |
| 836 | buildKernel(sigma, kernel, kernelWidth); |
| 837 | |
senorblanco@chromium.org | 2ce9a04 | 2011-07-22 15:31:14 +0000 | [diff] [blame] | 838 | // Clear out a halfWidth to the right of the srcRect to prevent the |
| 839 | // X convolution from reading garbage. |
| 840 | SkIRect clearRect = SkIRect::MakeXYWH( |
| 841 | srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height()); |
| 842 | context->clear(&clearRect, 0x0); |
| 843 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 844 | context->setRenderTarget(dstTexture->asRenderTarget()); |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 845 | context->convolveInX(srcTexture, srcRect, kernel, kernelWidth); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 846 | SkTSwap(srcTexture, dstTexture); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 847 | |
senorblanco@chromium.org | 2ce9a04 | 2011-07-22 15:31:14 +0000 | [diff] [blame] | 848 | // Clear out a halfWidth below the srcRect to prevent the Y |
| 849 | // convolution from reading garbage. |
| 850 | clearRect = SkIRect::MakeXYWH( |
| 851 | srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth); |
| 852 | context->clear(&clearRect, 0x0); |
| 853 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 854 | context->setRenderTarget(dstTexture->asRenderTarget()); |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 855 | context->convolveInY(srcTexture, srcRect, kernel, kernelWidth); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 856 | SkTSwap(srcTexture, dstTexture); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 857 | |
senorblanco@chromium.org | 2ce9a04 | 2011-07-22 15:31:14 +0000 | [diff] [blame] | 858 | // Clear one pixel to the right and below, to accommodate bilinear |
| 859 | // upsampling. |
| 860 | clearRect = SkIRect::MakeXYWH( |
| 861 | srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1); |
| 862 | context->clear(&clearRect, 0x0); |
| 863 | clearRect = SkIRect::MakeXYWH( |
| 864 | srcRect.fRight, srcRect.fTop, 1, srcRect.height()); |
| 865 | context->clear(&clearRect, 0x0); |
| 866 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 867 | if (scaleFactor > 1) { |
| 868 | // FIXME: This should be mitchell, not bilinear. |
| 869 | paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter); |
senorblanco@chromium.org | 422b67d | 2011-07-19 21:22:13 +0000 | [diff] [blame] | 870 | sampleM.setIDiv(srcTexture->width(), srcTexture->height()); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 871 | paint.getTextureSampler(0)->setMatrix(sampleM); |
| 872 | context->setRenderTarget(dstTexture->asRenderTarget()); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 873 | paint.setTexture(0, srcTexture); |
| 874 | SkRect dstRect(srcRect); |
| 875 | scaleRect(&dstRect, scaleFactor); |
| 876 | context->drawRectToRect(paint, dstRect, srcRect); |
| 877 | srcRect = dstRect; |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 878 | SkTSwap(srcTexture, dstTexture); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 879 | } |
| 880 | |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 881 | if (blurType != SkMaskFilter::kNormal_BlurType) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 882 | GrTexture* origTexture = origEntry.texture(); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 883 | paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter); |
senorblanco@chromium.org | 422b67d | 2011-07-19 21:22:13 +0000 | [diff] [blame] | 884 | sampleM.setIDiv(origTexture->width(), origTexture->height()); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 885 | paint.getTextureSampler(0)->setMatrix(sampleM); |
| 886 | // Blend origTexture over srcTexture. |
| 887 | context->setRenderTarget(srcTexture->asRenderTarget()); |
| 888 | paint.setTexture(0, origTexture); |
| 889 | if (SkMaskFilter::kInner_BlurType == blurType) { |
| 890 | // inner: dst = dst * src |
| 891 | paint.fSrcBlendCoeff = kDC_BlendCoeff; |
| 892 | paint.fDstBlendCoeff = kZero_BlendCoeff; |
| 893 | } else if (SkMaskFilter::kSolid_BlurType == blurType) { |
| 894 | // solid: dst = src + dst - src * dst |
| 895 | // = (1 - dst) * src + 1 * dst |
| 896 | paint.fSrcBlendCoeff = kIDC_BlendCoeff; |
| 897 | paint.fDstBlendCoeff = kOne_BlendCoeff; |
| 898 | } else if (SkMaskFilter::kOuter_BlurType == blurType) { |
| 899 | // outer: dst = dst * (1 - src) |
| 900 | // = 0 * src + (1 - src) * dst |
| 901 | paint.fSrcBlendCoeff = kZero_BlendCoeff; |
| 902 | paint.fDstBlendCoeff = kISC_BlendCoeff; |
| 903 | } |
| 904 | context->drawRect(paint, srcRect); |
| 905 | } |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 906 | context->setRenderTarget(oldRenderTarget); |
senorblanco@chromium.org | 42dd0f9 | 2011-07-14 15:29:57 +0000 | [diff] [blame] | 907 | context->setClip(oldClip); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 908 | |
| 909 | if (grp->hasTextureOrMask()) { |
| 910 | GrMatrix inverse; |
| 911 | if (!matrix.invert(&inverse)) { |
| 912 | return false; |
| 913 | } |
| 914 | grp->preConcatActiveSamplerMatrices(inverse); |
| 915 | } |
| 916 | |
| 917 | static const int MASK_IDX = GrPaint::kMaxMasks - 1; |
| 918 | // we assume the last mask index is available for use |
| 919 | GrAssert(NULL == grp->getMask(MASK_IDX)); |
| 920 | grp->setMask(MASK_IDX, srcTexture); |
| 921 | grp->getMaskSampler(MASK_IDX)->setClampNoFilter(); |
| 922 | |
| 923 | GrMatrix m; |
| 924 | m.setTranslate(-finalRect.fLeft, -finalRect.fTop); |
| 925 | m.postIDiv(srcTexture->width(), srcTexture->height()); |
| 926 | grp->getMaskSampler(MASK_IDX)->setMatrix(m); |
| 927 | context->drawRect(*grp, finalRect); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 928 | return true; |
| 929 | } |
| 930 | |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 931 | static bool drawWithMaskFilter(GrContext* context, const SkPath& path, |
| 932 | SkMaskFilter* filter, const SkMatrix& matrix, |
| 933 | const SkRegion& clip, SkBounder* bounder, |
| 934 | GrPaint* grp) { |
| 935 | SkMask srcM, dstM; |
| 936 | |
| 937 | if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM, |
| 938 | SkMask::kComputeBoundsAndRenderImage_CreateMode)) { |
| 939 | return false; |
| 940 | } |
| 941 | |
| 942 | SkAutoMaskImage autoSrc(&srcM, false); |
| 943 | |
| 944 | if (!filter->filterMask(&dstM, srcM, matrix, NULL)) { |
| 945 | return false; |
| 946 | } |
| 947 | // this will free-up dstM when we're done (allocated in filterMask()) |
| 948 | SkAutoMaskImage autoDst(&dstM, false); |
| 949 | |
| 950 | if (clip.quickReject(dstM.fBounds)) { |
| 951 | return false; |
| 952 | } |
| 953 | if (bounder && !bounder->doIRect(dstM.fBounds)) { |
| 954 | return false; |
| 955 | } |
| 956 | |
| 957 | // we now have a device-aligned 8bit mask in dstM, ready to be drawn using |
| 958 | // the current clip (and identity matrix) and grpaint settings |
| 959 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 960 | // used to compute inverse view, if necessary |
| 961 | GrMatrix ivm = context->getMatrix(); |
| 962 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 963 | GrAutoMatrix avm(context, GrMatrix::I()); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 964 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 965 | const GrTextureDesc desc = { |
| 966 | kNone_GrTextureFlags, |
| 967 | kNone_GrAALevel, |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 968 | dstM.fBounds.width(), |
| 969 | dstM.fBounds.height(), |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 970 | kAlpha_8_GrPixelConfig |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 971 | }; |
| 972 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 973 | GrAutoScratchTexture ast(context, desc); |
| 974 | GrTexture* texture = ast.texture(); |
bsalomon@google.com | eb2aa1d | 2011-07-14 15:45:19 +0000 | [diff] [blame] | 975 | |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 976 | if (NULL == texture) { |
| 977 | return false; |
| 978 | } |
bsalomon@google.com | eb2aa1d | 2011-07-14 15:45:19 +0000 | [diff] [blame] | 979 | texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight, |
| 980 | dstM.fImage, dstM.fRowBytes); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 981 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 982 | if (grp->hasTextureOrMask() && ivm.invert(&ivm)) { |
| 983 | grp->preConcatActiveSamplerMatrices(ivm); |
| 984 | } |
| 985 | |
| 986 | static const int MASK_IDX = GrPaint::kMaxMasks - 1; |
| 987 | // we assume the last mask index is available for use |
| 988 | GrAssert(NULL == grp->getMask(MASK_IDX)); |
| 989 | grp->setMask(MASK_IDX, texture); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 990 | grp->getMaskSampler(MASK_IDX)->setClampNoFilter(); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 991 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 992 | GrRect d; |
| 993 | d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft), |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 994 | GrIntToScalar(dstM.fBounds.fTop), |
| 995 | GrIntToScalar(dstM.fBounds.fRight), |
| 996 | GrIntToScalar(dstM.fBounds.fBottom)); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 997 | |
| 998 | GrMatrix m; |
| 999 | m.setTranslate(-dstM.fBounds.fLeft, -dstM.fBounds.fTop); |
bsalomon@google.com | eb2aa1d | 2011-07-14 15:45:19 +0000 | [diff] [blame] | 1000 | m.postIDiv(texture->width(), texture->height()); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1001 | grp->getMaskSampler(MASK_IDX)->setMatrix(m); |
| 1002 | |
| 1003 | context->drawRect(*grp, d); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 1004 | return true; |
| 1005 | } |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 1006 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 1007 | void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1008 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 1009 | bool pathIsMutable) { |
| 1010 | CHECK_SHOULD_DRAW(draw); |
| 1011 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1012 | GrPaint grPaint; |
| 1013 | SkAutoCachedTexture act; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1014 | if (!this->skPaint2GrPaintShader(paint, |
| 1015 | &act, |
| 1016 | *draw.fMatrix, |
| 1017 | &grPaint, |
| 1018 | true)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1019 | return; |
| 1020 | } |
| 1021 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 1022 | // BEGIN lift from SkDraw::drawPath() |
| 1023 | |
| 1024 | SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath); |
| 1025 | bool doFill = true; |
| 1026 | SkPath tmpPath; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1027 | |
| 1028 | if (prePathMatrix) { |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 1029 | SkPath* result = pathPtr; |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 1030 | |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 1031 | if (!pathIsMutable) { |
| 1032 | result = &tmpPath; |
| 1033 | pathIsMutable = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1034 | } |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 1035 | // should I push prePathMatrix on our MV stack temporarily, instead |
| 1036 | // of applying it here? See SkDraw.cpp |
| 1037 | pathPtr->transform(*prePathMatrix, result); |
| 1038 | pathPtr = result; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1039 | } |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 1040 | // at this point we're done with prePathMatrix |
| 1041 | SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1042 | |
bsalomon@google.com | 04de782 | 2011-03-25 18:04:43 +0000 | [diff] [blame] | 1043 | // This "if" is not part of the SkDraw::drawPath() lift. |
| 1044 | // When we get a 1.0 wide stroke we hairline stroke it instead of creating |
| 1045 | // a new stroked-path. This is motivated by canvas2D sites that draw |
| 1046 | // lines as 1.0 wide stroked paths. We can consider doing an alpha-modulated- |
| 1047 | // hairline for width < 1.0 when AA is enabled. |
| 1048 | static const int gMatrixMask = ~(SkMatrix::kIdentity_Mask | |
| 1049 | SkMatrix::kTranslate_Mask); |
| 1050 | if (!paint.getPathEffect() && |
| 1051 | SkPaint::kStroke_Style == paint.getStyle() && |
| 1052 | !(draw.fMatrix->getType() & gMatrixMask) && |
| 1053 | SK_Scalar1 == paint.getStrokeWidth()) { |
| 1054 | doFill = false; |
| 1055 | } |
| 1056 | |
| 1057 | if (doFill && (paint.getPathEffect() || |
| 1058 | paint.getStyle() != SkPaint::kFill_Style)) { |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 1059 | doFill = paint.getFillPath(*pathPtr, &tmpPath); |
| 1060 | pathPtr = &tmpPath; |
| 1061 | } |
| 1062 | |
| 1063 | // END lift from SkDraw::drawPath() |
| 1064 | |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 1065 | if (paint.getMaskFilter()) { |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 1066 | // avoid possibly allocating a new path in transform if we can |
| 1067 | SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath; |
| 1068 | |
| 1069 | // transform the path into device space |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 1070 | pathPtr->transform(*draw.fMatrix, devPathPtr); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 1071 | if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(), |
| 1072 | *draw.fMatrix, *draw.fClip, draw.fBounder, |
| 1073 | &grPaint)) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1074 | drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(), |
| 1075 | *draw.fMatrix, *draw.fClip, draw.fBounder, |
| 1076 | &grPaint); |
| 1077 | } |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 1078 | return; |
| 1079 | } |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 1080 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1081 | GrPathFill fill = kHairLine_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1082 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 1083 | if (doFill) { |
| 1084 | switch (pathPtr->getFillType()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1085 | case SkPath::kWinding_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1086 | fill = kWinding_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1087 | break; |
| 1088 | case SkPath::kEvenOdd_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1089 | fill = kEvenOdd_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1090 | break; |
| 1091 | case SkPath::kInverseWinding_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1092 | fill = kInverseWinding_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1093 | break; |
| 1094 | case SkPath::kInverseEvenOdd_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1095 | fill = kInverseEvenOdd_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1096 | break; |
| 1097 | default: |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1098 | SkDebugf("Unsupported path fill type\n"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1099 | return; |
| 1100 | } |
| 1101 | } |
| 1102 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 1103 | fContext->drawPath(grPaint, *pathPtr, fill); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1106 | void SkGpuDevice::drawBitmap(const SkDraw& draw, |
| 1107 | const SkBitmap& bitmap, |
| 1108 | const SkIRect* srcRectPtr, |
| 1109 | const SkMatrix& m, |
| 1110 | const SkPaint& paint) { |
| 1111 | CHECK_SHOULD_DRAW(draw); |
| 1112 | |
| 1113 | SkIRect srcRect; |
| 1114 | if (NULL == srcRectPtr) { |
| 1115 | srcRect.set(0, 0, bitmap.width(), bitmap.height()); |
| 1116 | } else { |
| 1117 | srcRect = *srcRectPtr; |
| 1118 | } |
| 1119 | |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1120 | if (paint.getMaskFilter()){ |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1121 | // Convert the bitmap to a shader so that the rect can be drawn |
| 1122 | // through drawRect, which supports mask filters. |
| 1123 | SkBitmap tmp; // subset of bitmap, if necessary |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1124 | const SkBitmap* bitmapPtr = &bitmap; |
epoger@google.com | 9ef2d83 | 2011-07-01 21:12:20 +0000 | [diff] [blame] | 1125 | if (srcRectPtr) { |
| 1126 | if (!bitmap.extractSubset(&tmp, srcRect)) { |
| 1127 | return; // extraction failed |
| 1128 | } |
| 1129 | bitmapPtr = &tmp; |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1130 | srcRect.set(0,0, srcRect.width(), srcRect.height()); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1131 | } |
| 1132 | SkPaint paintWithTexture(paint); |
| 1133 | paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr, |
| 1134 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref(); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1135 | SkRect ScalarRect; |
epoger@google.com | 9ef2d83 | 2011-07-01 21:12:20 +0000 | [diff] [blame] | 1136 | ScalarRect.set(srcRect); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1137 | |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1138 | // Transform 'm' needs to be concatenated to the draw matrix, |
| 1139 | // rather than transforming the primitive directly, so that 'm' will |
| 1140 | // also affect the behavior of the mask filter. |
| 1141 | SkMatrix drawMatrix; |
| 1142 | drawMatrix.setConcat(*draw.fMatrix, m); |
| 1143 | SkDraw transformedDraw(draw); |
| 1144 | transformedDraw.fMatrix = &drawMatrix; |
| 1145 | |
| 1146 | this->drawRect(transformedDraw, ScalarRect, paintWithTexture); |
| 1147 | |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1148 | return; |
| 1149 | } |
| 1150 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1151 | GrPaint grPaint; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1152 | if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1153 | return; |
| 1154 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1155 | GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1156 | if (paint.isFilterBitmap()) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1157 | sampler->setFilter(GrSamplerState::kBilinear_Filter); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1158 | } else { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1159 | sampler->setFilter(GrSamplerState::kNearest_Filter); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1160 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1161 | |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1162 | const int maxTextureSize = fContext->getMaxTextureSize(); |
| 1163 | if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize && |
| 1164 | bitmap.height() <= maxTextureSize)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1165 | // take the fast case |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1166 | this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1167 | return; |
| 1168 | } |
| 1169 | |
| 1170 | // undo the translate done by SkCanvas |
| 1171 | int DX = SkMax32(0, srcRect.fLeft); |
| 1172 | int DY = SkMax32(0, srcRect.fTop); |
| 1173 | // compute clip bounds in local coordinates |
| 1174 | SkIRect clipRect; |
| 1175 | { |
| 1176 | SkRect r; |
| 1177 | r.set(draw.fClip->getBounds()); |
| 1178 | SkMatrix matrix, inverse; |
| 1179 | matrix.setConcat(*draw.fMatrix, m); |
| 1180 | if (!matrix.invert(&inverse)) { |
| 1181 | return; |
| 1182 | } |
| 1183 | inverse.mapRect(&r); |
| 1184 | r.roundOut(&clipRect); |
| 1185 | // apply the canvas' translate to our local clip |
| 1186 | clipRect.offset(DX, DY); |
| 1187 | } |
| 1188 | |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1189 | int nx = bitmap.width() / maxTextureSize; |
| 1190 | int ny = bitmap.height() / maxTextureSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1191 | for (int x = 0; x <= nx; x++) { |
| 1192 | for (int y = 0; y <= ny; y++) { |
| 1193 | SkIRect tileR; |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1194 | tileR.set(x * maxTextureSize, y * maxTextureSize, |
| 1195 | (x + 1) * maxTextureSize, (y + 1) * maxTextureSize); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1196 | if (!SkIRect::Intersects(tileR, clipRect)) { |
| 1197 | continue; |
| 1198 | } |
| 1199 | |
| 1200 | SkIRect srcR = tileR; |
| 1201 | if (!srcR.intersect(srcRect)) { |
| 1202 | continue; |
| 1203 | } |
| 1204 | |
| 1205 | SkBitmap tmpB; |
| 1206 | if (bitmap.extractSubset(&tmpB, tileR)) { |
| 1207 | // now offset it to make it "local" to our tmp bitmap |
| 1208 | srcR.offset(-tileR.fLeft, -tileR.fTop); |
| 1209 | |
| 1210 | SkMatrix tmpM(m); |
| 1211 | { |
| 1212 | int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft); |
| 1213 | int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop); |
| 1214 | tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy)); |
| 1215 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1216 | this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1217 | } |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | /* |
| 1223 | * This is called by drawBitmap(), which has to handle images that may be too |
| 1224 | * large to be represented by a single texture. |
| 1225 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1226 | * internalDrawBitmap assumes that the specified bitmap will fit in a texture |
| 1227 | * and that non-texture portion of the GrPaint has already been setup. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1228 | */ |
| 1229 | void SkGpuDevice::internalDrawBitmap(const SkDraw& draw, |
| 1230 | const SkBitmap& bitmap, |
| 1231 | const SkIRect& srcRect, |
| 1232 | const SkMatrix& m, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1233 | GrPaint* grPaint) { |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1234 | SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() && |
| 1235 | bitmap.height() <= fContext->getMaxTextureSize()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1236 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 1237 | SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1238 | if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 1239 | SkDebugf("nothing to draw\n"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1240 | return; |
| 1241 | } |
| 1242 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1243 | GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx); |
| 1244 | |
| 1245 | sampler->setWrapX(GrSamplerState::kClamp_WrapMode); |
| 1246 | sampler->setWrapY(GrSamplerState::kClamp_WrapMode); |
| 1247 | sampler->setSampleMode(GrSamplerState::kNormal_SampleMode); |
| 1248 | sampler->setMatrix(GrMatrix::I()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1249 | |
| 1250 | GrTexture* texture; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1251 | SkAutoCachedTexture act(this, bitmap, *sampler, &texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1252 | if (NULL == texture) { |
| 1253 | return; |
| 1254 | } |
| 1255 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1256 | grPaint->setTexture(kShaderTextureIdx, texture); |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 1257 | |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 1258 | GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()), |
| 1259 | GrIntToScalar(srcRect.height())); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 1260 | GrRect paintRect; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1261 | paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()), |
| 1262 | GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()), |
| 1263 | GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()), |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 1264 | GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1265 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1266 | if (GrSamplerState::kNearest_Filter != sampler->getFilter() && |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1267 | (srcRect.width() < bitmap.width() || |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1268 | srcRect.height() < bitmap.height())) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1269 | // If drawing a subrect of the bitmap and filtering is enabled, |
| 1270 | // use a constrained texture domain to avoid color bleeding |
| 1271 | GrScalar left, top, right, bottom; |
| 1272 | if (srcRect.width() > 1) { |
| 1273 | GrScalar border = GR_ScalarHalf / bitmap.width(); |
| 1274 | left = paintRect.left() + border; |
| 1275 | right = paintRect.right() - border; |
| 1276 | } else { |
| 1277 | left = right = GrScalarHalf(paintRect.left() + paintRect.right()); |
| 1278 | } |
| 1279 | if (srcRect.height() > 1) { |
| 1280 | GrScalar border = GR_ScalarHalf / bitmap.height(); |
| 1281 | top = paintRect.top() + border; |
| 1282 | bottom = paintRect.bottom() - border; |
| 1283 | } else { |
| 1284 | top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom()); |
| 1285 | } |
| 1286 | GrRect textureDomain; |
| 1287 | textureDomain.setLTRB(left, top, right, bottom); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1288 | sampler->setTextureDomain(textureDomain); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 1291 | fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
| 1295 | int left, int top, const SkPaint& paint) { |
| 1296 | CHECK_SHOULD_DRAW(draw); |
| 1297 | |
| 1298 | SkAutoLockPixels alp(bitmap); |
| 1299 | if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
| 1300 | return; |
| 1301 | } |
| 1302 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1303 | GrPaint grPaint; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1304 | if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1305 | return; |
| 1306 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1307 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1308 | GrAutoMatrix avm(fContext, GrMatrix::I()); |
| 1309 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1310 | GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1311 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1312 | GrTexture* texture; |
| 1313 | sampler->setClampNoFilter(); |
| 1314 | SkAutoCachedTexture act(this, bitmap, *sampler, &texture); |
| 1315 | |
| 1316 | grPaint.setTexture(kBitmapTextureIdx, texture); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1317 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1318 | fContext->drawRectToRect(grPaint, |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 1319 | GrRect::MakeXYWH(GrIntToScalar(left), |
| 1320 | GrIntToScalar(top), |
| 1321 | GrIntToScalar(bitmap.width()), |
| 1322 | GrIntToScalar(bitmap.height())), |
| 1323 | GrRect::MakeWH(GR_Scalar1, GR_Scalar1)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev, |
| 1327 | int x, int y, const SkPaint& paint) { |
| 1328 | CHECK_SHOULD_DRAW(draw); |
| 1329 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1330 | GrPaint grPaint; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 1331 | if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) || |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1332 | !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1333 | return; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1334 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1335 | |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1336 | GrTexture* devTex = grPaint.getTexture(0); |
| 1337 | SkASSERT(NULL != devTex); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1338 | |
| 1339 | const SkBitmap& bm = dev->accessBitmap(false); |
| 1340 | int w = bm.width(); |
| 1341 | int h = bm.height(); |
| 1342 | |
| 1343 | GrAutoMatrix avm(fContext, GrMatrix::I()); |
| 1344 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1345 | grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1346 | |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1347 | GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x), |
| 1348 | GrIntToScalar(y), |
| 1349 | GrIntToScalar(w), |
| 1350 | GrIntToScalar(h)); |
| 1351 | // The device being drawn may not fill up its texture (saveLayer uses |
| 1352 | // the approximate ). |
| 1353 | GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(), |
| 1354 | GR_Scalar1 * h / devTex->height()); |
| 1355 | |
| 1356 | fContext->drawRectToRect(grPaint, dstRect, srcRect); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | /////////////////////////////////////////////////////////////////////////////// |
| 1360 | |
| 1361 | // must be in SkCanvas::VertexMode order |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1362 | static const GrPrimitiveType gVertexMode2PrimitiveType[] = { |
| 1363 | kTriangles_PrimitiveType, |
| 1364 | kTriangleStrip_PrimitiveType, |
| 1365 | kTriangleFan_PrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1366 | }; |
| 1367 | |
| 1368 | void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode, |
| 1369 | int vertexCount, const SkPoint vertices[], |
| 1370 | const SkPoint texs[], const SkColor colors[], |
| 1371 | SkXfermode* xmode, |
| 1372 | const uint16_t indices[], int indexCount, |
| 1373 | const SkPaint& paint) { |
| 1374 | CHECK_SHOULD_DRAW(draw); |
| 1375 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1376 | GrPaint grPaint; |
| 1377 | SkAutoCachedTexture act; |
| 1378 | // we ignore the shader if texs is null. |
| 1379 | if (NULL == texs) { |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1380 | if (!this->skPaint2GrPaintNoShader(paint, |
| 1381 | false, |
| 1382 | &grPaint, |
| 1383 | NULL == colors)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1384 | return; |
| 1385 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1386 | } else { |
reed@google.com | 1a2e8d2 | 2011-01-21 22:08:29 +0000 | [diff] [blame] | 1387 | if (!this->skPaint2GrPaintShader(paint, &act, |
| 1388 | *draw.fMatrix, |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1389 | &grPaint, |
| 1390 | NULL == colors)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1391 | return; |
| 1392 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1393 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1394 | |
| 1395 | if (NULL != xmode && NULL != texs && NULL != colors) { |
| 1396 | SkXfermode::Mode mode; |
| 1397 | if (!SkXfermode::IsMode(xmode, &mode) || |
| 1398 | SkXfermode::kMultiply_Mode != mode) { |
| 1399 | SkDebugf("Unsupported vertex-color/texture xfer mode.\n"); |
| 1400 | #if 0 |
| 1401 | return |
| 1402 | #endif |
| 1403 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1404 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1405 | |
bsalomon@google.com | 498776a | 2011-08-16 19:20:44 +0000 | [diff] [blame^] | 1406 | SkAutoSTMalloc<128, GrColor> convertedColors(0); |
| 1407 | if (NULL != colors) { |
| 1408 | // need to convert byte order and from non-PM to PM |
| 1409 | convertedColors.realloc(vertexCount); |
| 1410 | for (int i = 0; i < vertexCount; ++i) { |
| 1411 | convertedColors[i] = SkGr::SkColor2GrColor(colors[i]); |
| 1412 | } |
| 1413 | colors = convertedColors.get(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1414 | } |
bsalomon@google.com | 498776a | 2011-08-16 19:20:44 +0000 | [diff] [blame^] | 1415 | fContext->drawVertices(grPaint, |
| 1416 | gVertexMode2PrimitiveType[vmode], |
| 1417 | vertexCount, |
| 1418 | (GrPoint*) vertices, |
| 1419 | (GrPoint*) texs, |
| 1420 | colors, |
| 1421 | indices, |
| 1422 | indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | /////////////////////////////////////////////////////////////////////////////// |
| 1426 | |
| 1427 | static void GlyphCacheAuxProc(void* data) { |
| 1428 | delete (GrFontScaler*)data; |
| 1429 | } |
| 1430 | |
| 1431 | static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) { |
| 1432 | void* auxData; |
| 1433 | GrFontScaler* scaler = NULL; |
| 1434 | if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
| 1435 | scaler = (GrFontScaler*)auxData; |
| 1436 | } |
| 1437 | if (NULL == scaler) { |
| 1438 | scaler = new SkGrFontScaler(cache); |
| 1439 | cache->setAuxProc(GlyphCacheAuxProc, scaler); |
| 1440 | } |
| 1441 | return scaler; |
| 1442 | } |
| 1443 | |
| 1444 | static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state, |
| 1445 | SkFixed fx, SkFixed fy, |
| 1446 | const SkGlyph& glyph) { |
| 1447 | SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0); |
| 1448 | |
| 1449 | GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs; |
| 1450 | |
| 1451 | if (NULL == procs->fFontScaler) { |
| 1452 | procs->fFontScaler = get_gr_font_scaler(state.fCache); |
| 1453 | } |
reed@google.com | 39ce0ac | 2011-04-08 15:42:19 +0000 | [diff] [blame] | 1454 | |
| 1455 | /* |
reed@google.com | 3b139f5 | 2011-06-07 17:56:25 +0000 | [diff] [blame] | 1456 | * What should we do with fy? (assuming horizontal/latin text) |
reed@google.com | 39ce0ac | 2011-04-08 15:42:19 +0000 | [diff] [blame] | 1457 | * |
reed@google.com | 3b139f5 | 2011-06-07 17:56:25 +0000 | [diff] [blame] | 1458 | * The raster code calls SkFixedFloorToFixed on it, as it does with fx. |
| 1459 | * It calls that rather than round, because our caller has already added |
| 1460 | * SK_FixedHalf, so that calling floor gives us the rounded integer. |
| 1461 | * |
| 1462 | * Test code between raster and gpu (they should draw the same) |
| 1463 | * |
| 1464 | * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint); |
| 1465 | * |
| 1466 | * Perhaps we should only perform this integralization if there is no |
| 1467 | * fExtMatrix... |
reed@google.com | 39ce0ac | 2011-04-08 15:42:19 +0000 | [diff] [blame] | 1468 | */ |
reed@google.com | 3b139f5 | 2011-06-07 17:56:25 +0000 | [diff] [blame] | 1469 | fy = SkFixedFloorToFixed(fy); |
| 1470 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1471 | procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0), |
reed@google.com | 3b139f5 | 2011-06-07 17:56:25 +0000 | [diff] [blame] | 1472 | SkFixedFloorToFixed(fx), fy, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1473 | procs->fFontScaler); |
| 1474 | } |
| 1475 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1476 | SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1477 | |
| 1478 | // deferred allocation |
| 1479 | if (NULL == fDrawProcs) { |
| 1480 | fDrawProcs = new GrSkDrawProcs; |
| 1481 | fDrawProcs->fD1GProc = SkGPU_Draw1Glyph; |
| 1482 | fDrawProcs->fContext = fContext; |
| 1483 | } |
| 1484 | |
| 1485 | // init our (and GL's) state |
| 1486 | fDrawProcs->fTextContext = context; |
| 1487 | fDrawProcs->fFontScaler = NULL; |
| 1488 | return fDrawProcs; |
| 1489 | } |
| 1490 | |
| 1491 | void SkGpuDevice::drawText(const SkDraw& draw, const void* text, |
| 1492 | size_t byteLength, SkScalar x, SkScalar y, |
| 1493 | const SkPaint& paint) { |
| 1494 | CHECK_SHOULD_DRAW(draw); |
| 1495 | |
| 1496 | if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) { |
| 1497 | // this guy will just call our drawPath() |
| 1498 | draw.drawText((const char*)text, byteLength, x, y, paint); |
| 1499 | } else { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1500 | SkDraw myDraw(draw); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1501 | |
| 1502 | GrPaint grPaint; |
| 1503 | SkAutoCachedTexture act; |
| 1504 | |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1505 | if (!this->skPaint2GrPaintShader(paint, |
| 1506 | &act, |
| 1507 | *draw.fMatrix, |
| 1508 | &grPaint, |
| 1509 | true)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1510 | return; |
| 1511 | } |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 1512 | GrTextContext context(fContext, grPaint, draw.fExtMatrix); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1513 | myDraw.fProcs = this->initDrawForText(&context); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1514 | this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint); |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, |
| 1519 | size_t byteLength, const SkScalar pos[], |
| 1520 | SkScalar constY, int scalarsPerPos, |
| 1521 | const SkPaint& paint) { |
| 1522 | CHECK_SHOULD_DRAW(draw); |
| 1523 | |
| 1524 | if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) { |
| 1525 | // this guy will just call our drawPath() |
| 1526 | draw.drawPosText((const char*)text, byteLength, pos, constY, |
| 1527 | scalarsPerPos, paint); |
| 1528 | } else { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1529 | SkDraw myDraw(draw); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1530 | |
| 1531 | GrPaint grPaint; |
| 1532 | SkAutoCachedTexture act; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1533 | if (!this->skPaint2GrPaintShader(paint, |
| 1534 | &act, |
| 1535 | *draw.fMatrix, |
| 1536 | &grPaint, |
| 1537 | true)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1538 | return; |
| 1539 | } |
| 1540 | |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 1541 | GrTextContext context(fContext, grPaint, draw.fExtMatrix); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1542 | myDraw.fProcs = this->initDrawForText(&context); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1543 | this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY, |
| 1544 | scalarsPerPos, paint); |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text, |
| 1549 | size_t len, const SkPath& path, |
| 1550 | const SkMatrix* m, const SkPaint& paint) { |
| 1551 | CHECK_SHOULD_DRAW(draw); |
| 1552 | |
| 1553 | SkASSERT(draw.fDevice == this); |
| 1554 | draw.drawTextOnPath((const char*)text, len, path, m, paint); |
| 1555 | } |
| 1556 | |
| 1557 | /////////////////////////////////////////////////////////////////////////////// |
| 1558 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1559 | bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) { |
| 1560 | if (!paint.isLCDRenderText()) { |
| 1561 | // we're cool with the paint as is |
| 1562 | return false; |
| 1563 | } |
| 1564 | |
| 1565 | if (paint.getShader() || |
| 1566 | paint.getXfermode() || // unless its srcover |
| 1567 | paint.getMaskFilter() || |
| 1568 | paint.getRasterizer() || |
| 1569 | paint.getColorFilter() || |
| 1570 | paint.getPathEffect() || |
| 1571 | paint.isFakeBoldText() || |
| 1572 | paint.getStyle() != SkPaint::kFill_Style) { |
| 1573 | // turn off lcd |
| 1574 | flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag; |
| 1575 | flags->fHinting = paint.getHinting(); |
| 1576 | return true; |
| 1577 | } |
| 1578 | // we're cool with the paint as is |
| 1579 | return false; |
| 1580 | } |
| 1581 | |
| 1582 | /////////////////////////////////////////////////////////////////////////////// |
| 1583 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1584 | SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap, |
| 1585 | const GrSamplerState& sampler, |
| 1586 | TexType type) { |
| 1587 | GrContext::TextureCacheEntry entry; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1588 | GrContext* ctx = this->context(); |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 1589 | |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1590 | if (kBitmap_TexType != type) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 1591 | const GrTextureDesc desc = { |
| 1592 | kRenderTarget_GrTextureFlagBit, |
| 1593 | kNone_GrAALevel, |
| 1594 | bitmap.width(), |
| 1595 | bitmap.height(), |
| 1596 | SkGr::Bitmap2PixelConfig(bitmap) |
| 1597 | }; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1598 | GrContext::ScratchTexMatch match; |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1599 | if (kSaveLayerDeviceRenderTarget_TexType == type) { |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1600 | // we know layers will only be drawn through drawDevice. |
| 1601 | // drawDevice has been made to work with content embedded in a |
| 1602 | // larger texture so its okay to use the approximate version. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1603 | match = GrContext::kApprox_ScratchTexMatch; |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1604 | } else { |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1605 | SkASSERT(kDeviceRenderTarget_TexType == type); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1606 | match = GrContext::kExact_ScratchTexMatch; |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1607 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1608 | entry = ctx->lockScratchTexture(desc, match); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1609 | } else { |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1610 | if (!bitmap.isVolatile()) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1611 | GrContext::TextureKey key = bitmap.getGenerationID(); |
| 1612 | key |= ((uint64_t) bitmap.pixelRefOffset()) << 32; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1613 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1614 | entry = ctx->findAndLockTexture(key, bitmap.width(), |
| 1615 | bitmap.height(), sampler); |
| 1616 | if (NULL == entry.texture()) { |
| 1617 | entry = sk_gr_create_bitmap_texture(ctx, key, sampler, |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1618 | bitmap); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1619 | } |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1620 | } else { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1621 | entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1622 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1623 | if (NULL == entry.texture()) { |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1624 | GrPrintf("---- failed to create texture for cache [%d %d]\n", |
| 1625 | bitmap.width(), bitmap.height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1626 | } |
| 1627 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1628 | return entry; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1631 | void SkGpuDevice::unlockCachedTexture(TexCache cache) { |
| 1632 | this->context()->unlockTexture(cache); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1635 | SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config, |
| 1636 | int width, int height, |
| 1637 | bool isOpaque, |
| 1638 | Usage usage) { |
| 1639 | return SkNEW_ARGS(SkGpuDevice,(this->context(), config, |
| 1640 | width, height, usage)); |
| 1641 | } |
| 1642 | |