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