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