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