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