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 | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 722 | static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path, |
| 723 | SkMaskFilter* filter, const SkMatrix& matrix, |
| 724 | const SkRegion& clip, SkBounder* bounder, |
| 725 | GrPaint* grp) { |
senorblanco@chromium.org | a479fc7 | 2011-07-19 16:40:58 +0000 | [diff] [blame] | 726 | #ifdef SK_DISABLE_GPU_BLUR |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 727 | return false; |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 728 | #endif |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 729 | SkMaskFilter::BlurInfo info; |
| 730 | SkMaskFilter::BlurType blurType = filter->asABlur(&info); |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 731 | if (SkMaskFilter::kNone_BlurType == blurType) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 732 | return false; |
| 733 | } |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 734 | SkScalar radius = info.fIgnoreTransform ? info.fRadius |
| 735 | : matrix.mapRadius(info.fRadius); |
| 736 | radius = SkMinScalar(radius, MAX_BLUR_RADIUS); |
senorblanco@chromium.org | 68c4d12 | 2011-08-01 21:20:31 +0000 | [diff] [blame] | 737 | if (radius <= 0) { |
| 738 | return false; |
| 739 | } |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 740 | float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE; |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 741 | float sigma3 = sigma * 3.0f; |
| 742 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 743 | SkRect srcRect = path.getBounds(); |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 744 | SkRect clipRect; |
| 745 | clipRect.set(clip.getBounds()); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 746 | |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 747 | // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area. |
| 748 | srcRect.inset(-sigma3, -sigma3); |
| 749 | clipRect.inset(-sigma3, -sigma3); |
| 750 | srcRect.intersect(clipRect); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 751 | SkRect finalRect = srcRect; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 752 | SkIRect finalIRect; |
| 753 | finalRect.roundOut(&finalIRect); |
| 754 | if (clip.quickReject(finalIRect)) { |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 755 | return true; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 756 | } |
| 757 | if (bounder && !bounder->doIRect(finalIRect)) { |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 758 | return true; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 759 | } |
| 760 | GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop); |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 761 | srcRect.offset(offset); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 762 | const GrTextureDesc desc = { |
| 763 | kRenderTarget_GrTextureFlagBit, |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 764 | srcRect.width(), |
| 765 | srcRect.height(), |
| 766 | // We actually only need A8, but it often isn't supported as a |
| 767 | // render target |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 768 | kRGBA_8888_PM_GrPixelConfig, |
| 769 | {0} // samples |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 770 | }; |
| 771 | |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 772 | GrAutoScratchTexture pathEntry(context, desc); |
| 773 | GrTexture* pathTexture = pathEntry.texture(); |
| 774 | if (NULL == pathTexture) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 775 | return false; |
| 776 | } |
| 777 | GrRenderTarget* oldRenderTarget = context->getRenderTarget(); |
senorblanco@chromium.org | 42dd0f9 | 2011-07-14 15:29:57 +0000 | [diff] [blame] | 778 | // Once this code moves into GrContext, this should be changed to use |
| 779 | // an AutoClipRestore. |
| 780 | GrClip oldClip = context->getClip(); |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 781 | context->setRenderTarget(pathTexture->asRenderTarget()); |
senorblanco@chromium.org | 42dd0f9 | 2011-07-14 15:29:57 +0000 | [diff] [blame] | 782 | context->setClip(srcRect); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 783 | context->clear(NULL, 0); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 784 | GrPaint tempPaint; |
| 785 | tempPaint.reset(); |
| 786 | |
| 787 | GrAutoMatrix avm(context, GrMatrix::I()); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 788 | tempPaint.fAntiAlias = grp->fAntiAlias; |
| 789 | if (tempPaint.fAntiAlias) { |
| 790 | // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst |
| 791 | // blend coeff of zero requires dual source blending support in order |
| 792 | // to properly blend partially covered pixels. This means the AA |
| 793 | // code path may not be taken. So we use a dst blend coeff of ISA. We |
| 794 | // could special case AA draws to a dst surface with known alpha=0 to |
| 795 | // use a zero dst coeff when dual source blending isn't available. |
| 796 | tempPaint.fSrcBlendCoeff = kOne_BlendCoeff; |
| 797 | tempPaint.fDstBlendCoeff = kISC_BlendCoeff; |
| 798 | } |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 799 | // 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] | 800 | context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 801 | |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 802 | GrAutoScratchTexture temp1, temp2; |
| 803 | // If we're doing a normal blur, we can clobber the pathTexture in the |
| 804 | // gaussianBlur. Otherwise, we need to save it for later compositing. |
| 805 | bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType; |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame^] | 806 | GrTexture* blurTexture = context->gaussianBlur(pathTexture, |
| 807 | &temp1, |
| 808 | isNormalBlur ? NULL : &temp2, |
| 809 | srcRect, sigma, sigma); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 810 | |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 811 | if (!isNormalBlur) { |
| 812 | GrPaint paint; |
| 813 | paint.reset(); |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 814 | paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter); |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 815 | paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(), |
| 816 | pathTexture->height()); |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 817 | // Blend pathTexture over blurTexture. |
| 818 | context->setRenderTarget(blurTexture->asRenderTarget()); |
| 819 | paint.setTexture(0, pathTexture); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 820 | if (SkMaskFilter::kInner_BlurType == blurType) { |
| 821 | // inner: dst = dst * src |
| 822 | paint.fSrcBlendCoeff = kDC_BlendCoeff; |
| 823 | paint.fDstBlendCoeff = kZero_BlendCoeff; |
| 824 | } else if (SkMaskFilter::kSolid_BlurType == blurType) { |
| 825 | // solid: dst = src + dst - src * dst |
| 826 | // = (1 - dst) * src + 1 * dst |
| 827 | paint.fSrcBlendCoeff = kIDC_BlendCoeff; |
| 828 | paint.fDstBlendCoeff = kOne_BlendCoeff; |
| 829 | } else if (SkMaskFilter::kOuter_BlurType == blurType) { |
| 830 | // outer: dst = dst * (1 - src) |
| 831 | // = 0 * src + (1 - src) * dst |
| 832 | paint.fSrcBlendCoeff = kZero_BlendCoeff; |
| 833 | paint.fDstBlendCoeff = kISC_BlendCoeff; |
| 834 | } |
| 835 | context->drawRect(paint, srcRect); |
| 836 | } |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 837 | context->setRenderTarget(oldRenderTarget); |
senorblanco@chromium.org | 42dd0f9 | 2011-07-14 15:29:57 +0000 | [diff] [blame] | 838 | context->setClip(oldClip); |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 839 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 840 | if (grp->hasTextureOrMask()) { |
| 841 | GrMatrix inverse; |
| 842 | if (!matrix.invert(&inverse)) { |
| 843 | return false; |
| 844 | } |
| 845 | grp->preConcatActiveSamplerMatrices(inverse); |
| 846 | } |
| 847 | |
| 848 | static const int MASK_IDX = GrPaint::kMaxMasks - 1; |
| 849 | // we assume the last mask index is available for use |
| 850 | GrAssert(NULL == grp->getMask(MASK_IDX)); |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 851 | grp->setMask(MASK_IDX, blurTexture); |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 852 | grp->maskSampler(MASK_IDX)->reset(); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 853 | |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 854 | grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft, |
| 855 | -finalRect.fTop); |
| 856 | grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(), |
| 857 | blurTexture->height()); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 858 | context->drawRect(*grp, finalRect); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 859 | return true; |
| 860 | } |
| 861 | |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 862 | static bool drawWithMaskFilter(GrContext* context, const SkPath& path, |
| 863 | SkMaskFilter* filter, const SkMatrix& matrix, |
| 864 | const SkRegion& clip, SkBounder* bounder, |
| 865 | GrPaint* grp) { |
| 866 | SkMask srcM, dstM; |
| 867 | |
| 868 | if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM, |
| 869 | SkMask::kComputeBoundsAndRenderImage_CreateMode)) { |
| 870 | return false; |
| 871 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 872 | SkAutoMaskFreeImage autoSrc(srcM.fImage); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 873 | |
| 874 | if (!filter->filterMask(&dstM, srcM, matrix, NULL)) { |
| 875 | return false; |
| 876 | } |
| 877 | // 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] | 878 | SkAutoMaskFreeImage autoDst(dstM.fImage); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 879 | |
| 880 | if (clip.quickReject(dstM.fBounds)) { |
| 881 | return false; |
| 882 | } |
| 883 | if (bounder && !bounder->doIRect(dstM.fBounds)) { |
| 884 | return false; |
| 885 | } |
| 886 | |
| 887 | // we now have a device-aligned 8bit mask in dstM, ready to be drawn using |
| 888 | // the current clip (and identity matrix) and grpaint settings |
| 889 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 890 | // used to compute inverse view, if necessary |
| 891 | GrMatrix ivm = context->getMatrix(); |
| 892 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 893 | GrAutoMatrix avm(context, GrMatrix::I()); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 894 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 895 | const GrTextureDesc desc = { |
| 896 | kNone_GrTextureFlags, |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 897 | dstM.fBounds.width(), |
| 898 | dstM.fBounds.height(), |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 899 | kAlpha_8_GrPixelConfig, |
| 900 | {0}, // samples |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 901 | }; |
| 902 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 903 | GrAutoScratchTexture ast(context, desc); |
| 904 | GrTexture* texture = ast.texture(); |
bsalomon@google.com | eb2aa1d | 2011-07-14 15:45:19 +0000 | [diff] [blame] | 905 | |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 906 | if (NULL == texture) { |
| 907 | return false; |
| 908 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 909 | texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
bsalomon@google.com | eb2aa1d | 2011-07-14 15:45:19 +0000 | [diff] [blame] | 910 | dstM.fImage, dstM.fRowBytes); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 911 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 912 | if (grp->hasTextureOrMask() && ivm.invert(&ivm)) { |
| 913 | grp->preConcatActiveSamplerMatrices(ivm); |
| 914 | } |
| 915 | |
| 916 | static const int MASK_IDX = GrPaint::kMaxMasks - 1; |
| 917 | // we assume the last mask index is available for use |
| 918 | GrAssert(NULL == grp->getMask(MASK_IDX)); |
| 919 | grp->setMask(MASK_IDX, texture); |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 920 | grp->maskSampler(MASK_IDX)->reset(); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 921 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 922 | GrRect d; |
| 923 | d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft), |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 924 | GrIntToScalar(dstM.fBounds.fTop), |
| 925 | GrIntToScalar(dstM.fBounds.fRight), |
| 926 | GrIntToScalar(dstM.fBounds.fBottom)); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 927 | |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 928 | GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix(); |
| 929 | m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1, |
| 930 | -dstM.fBounds.fTop*SK_Scalar1); |
| 931 | m->postIDiv(texture->width(), texture->height()); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 932 | context->drawRect(*grp, d); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 933 | return true; |
| 934 | } |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 935 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 936 | void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath, |
bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 937 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 938 | bool pathIsMutable) { |
| 939 | CHECK_SHOULD_DRAW(draw); |
| 940 | |
reed@google.com | fe62638 | 2011-09-21 13:50:35 +0000 | [diff] [blame] | 941 | bool doFill = true; |
bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 942 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 943 | GrPaint grPaint; |
| 944 | SkAutoCachedTexture act; |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 945 | if (!skPaint2GrPaintShader(this, |
| 946 | paint, |
| 947 | *draw.fMatrix, |
| 948 | true, |
| 949 | &act, |
| 950 | &grPaint)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 951 | return; |
| 952 | } |
| 953 | |
bsalomon@google.com | 8c0a0d3 | 2012-03-05 16:01:18 +0000 | [diff] [blame] | 954 | // can we cheat, and threat a thin stroke as a hairline w/ coverage |
| 955 | // if we can, we draw lots faster (raster device does this same test) |
| 956 | SkScalar hairlineCoverage; |
| 957 | if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) { |
| 958 | doFill = false; |
| 959 | grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage * |
| 960 | grPaint.fCoverage); |
| 961 | } |
bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 962 | |
reed@google.com | fe62638 | 2011-09-21 13:50:35 +0000 | [diff] [blame] | 963 | // If we have a prematrix, apply it to the path, optimizing for the case |
| 964 | // where the original path can in fact be modified in place (even though |
| 965 | // its parameter type is const). |
| 966 | SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath); |
| 967 | SkPath tmpPath; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 968 | |
| 969 | if (prePathMatrix) { |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 970 | SkPath* result = pathPtr; |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 971 | |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 972 | if (!pathIsMutable) { |
| 973 | result = &tmpPath; |
| 974 | pathIsMutable = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 975 | } |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 976 | // should I push prePathMatrix on our MV stack temporarily, instead |
| 977 | // of applying it here? See SkDraw.cpp |
| 978 | pathPtr->transform(*prePathMatrix, result); |
| 979 | pathPtr = result; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 980 | } |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 981 | // at this point we're done with prePathMatrix |
| 982 | SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 983 | |
bsalomon@google.com | 8b58c4d | 2012-02-13 14:49:09 +0000 | [diff] [blame] | 984 | if (paint.getPathEffect() || |
| 985 | (doFill && paint.getStyle() != SkPaint::kFill_Style)) { |
reed@google.com | fe62638 | 2011-09-21 13:50:35 +0000 | [diff] [blame] | 986 | // it is safe to use tmpPath here, even if we already used it for the |
| 987 | // prepathmatrix, since getFillPath can take the same object for its |
| 988 | // input and output safely. |
bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 989 | doFill = paint.getFillPath(*pathPtr, &tmpPath); |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 990 | pathPtr = &tmpPath; |
| 991 | } |
| 992 | |
bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 993 | if (paint.getMaskFilter()) { |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 994 | // avoid possibly allocating a new path in transform if we can |
| 995 | SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath; |
| 996 | |
| 997 | // transform the path into device space |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 998 | pathPtr->transform(*draw.fMatrix, devPathPtr); |
bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 999 | if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(), |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 1000 | *draw.fMatrix, *draw.fClip, draw.fBounder, |
| 1001 | &grPaint)) { |
bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 1002 | drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(), |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1003 | *draw.fMatrix, *draw.fClip, draw.fBounder, |
| 1004 | &grPaint); |
| 1005 | } |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 1006 | return; |
| 1007 | } |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 1008 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1009 | GrPathFill fill = kHairLine_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1010 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 1011 | if (doFill) { |
| 1012 | switch (pathPtr->getFillType()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1013 | case SkPath::kWinding_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1014 | fill = kWinding_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1015 | break; |
| 1016 | case SkPath::kEvenOdd_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1017 | fill = kEvenOdd_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1018 | break; |
| 1019 | case SkPath::kInverseWinding_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1020 | fill = kInverseWinding_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1021 | break; |
| 1022 | case SkPath::kInverseEvenOdd_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1023 | fill = kInverseEvenOdd_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1024 | break; |
| 1025 | default: |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1026 | SkDebugf("Unsupported path fill type\n"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1027 | return; |
| 1028 | } |
| 1029 | } |
| 1030 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 1031 | fContext->drawPath(grPaint, *pathPtr, fill); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1034 | namespace { |
| 1035 | |
| 1036 | inline int get_tile_count(int l, int t, int r, int b, int tileSize) { |
| 1037 | int tilesX = (r / tileSize) - (l / tileSize) + 1; |
| 1038 | int tilesY = (b / tileSize) - (t / tileSize) + 1; |
| 1039 | return tilesX * tilesY; |
| 1040 | } |
| 1041 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1042 | inline int determine_tile_size(const SkBitmap& bitmap, |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1043 | const SkIRect* srcRectPtr, |
| 1044 | int maxTextureSize) { |
| 1045 | static const int kSmallTileSize = 1 << 10; |
| 1046 | if (maxTextureSize <= kSmallTileSize) { |
| 1047 | return maxTextureSize; |
| 1048 | } |
| 1049 | |
| 1050 | size_t maxTexTotalTileSize; |
| 1051 | size_t smallTotalTileSize; |
| 1052 | |
| 1053 | if (NULL == srcRectPtr) { |
| 1054 | int w = bitmap.width(); |
| 1055 | int h = bitmap.height(); |
| 1056 | maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize); |
| 1057 | smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize); |
| 1058 | } else { |
| 1059 | maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft, |
| 1060 | srcRectPtr->fTop, |
| 1061 | srcRectPtr->fRight, |
| 1062 | srcRectPtr->fBottom, |
| 1063 | maxTextureSize); |
| 1064 | smallTotalTileSize = get_tile_count(srcRectPtr->fLeft, |
| 1065 | srcRectPtr->fTop, |
| 1066 | srcRectPtr->fRight, |
| 1067 | srcRectPtr->fBottom, |
| 1068 | kSmallTileSize); |
| 1069 | } |
| 1070 | maxTexTotalTileSize *= maxTextureSize * maxTextureSize; |
| 1071 | smallTotalTileSize *= kSmallTileSize * kSmallTileSize; |
| 1072 | |
| 1073 | if (maxTexTotalTileSize > 2 * smallTotalTileSize) { |
| 1074 | return kSmallTileSize; |
| 1075 | } else { |
| 1076 | return maxTextureSize; |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap, |
| 1082 | const GrSamplerState& sampler, |
| 1083 | const SkIRect* srcRectPtr, |
| 1084 | int* tileSize) const { |
| 1085 | SkASSERT(NULL != tileSize); |
| 1086 | |
| 1087 | // if bitmap is explictly texture backed then just use the texture |
| 1088 | if (NULL != bitmap.getTexture()) { |
| 1089 | return false; |
| 1090 | } |
| 1091 | // if it's larger than the max texture size, then we have no choice but |
| 1092 | // tiling |
| 1093 | const int maxTextureSize = fContext->getMaxTextureSize(); |
| 1094 | if (bitmap.width() > maxTextureSize || |
| 1095 | bitmap.height() > maxTextureSize) { |
| 1096 | *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize); |
| 1097 | return true; |
| 1098 | } |
| 1099 | // if we are going to have to draw the whole thing, then don't tile |
| 1100 | if (NULL == srcRectPtr) { |
| 1101 | return false; |
| 1102 | } |
| 1103 | // if the entire texture is already in our cache then no reason to tile it |
| 1104 | if (this->isBitmapInTextureCache(bitmap, sampler)) { |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
| 1108 | // At this point we know we could do the draw by uploading the entire bitmap |
| 1109 | // as a texture. However, if the texture would be large compared to the |
| 1110 | // cache size and we don't require most of it for this draw then tile to |
| 1111 | // reduce the amount of upload and cache spill. |
| 1112 | |
| 1113 | // assumption here is that sw bitmap size is a good proxy for its size as |
| 1114 | // a texture |
| 1115 | size_t bmpSize = bitmap.getSize(); |
| 1116 | size_t cacheSize; |
| 1117 | fContext->getTextureCacheLimits(NULL, &cacheSize); |
| 1118 | if (bmpSize < cacheSize / 2) { |
| 1119 | return false; |
| 1120 | } |
| 1121 | |
| 1122 | SkFixed fracUsed = |
| 1123 | SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(), |
| 1124 | (srcRectPtr->height() << 16) / bitmap.height()); |
| 1125 | if (fracUsed <= SK_FixedHalf) { |
| 1126 | *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize); |
| 1127 | return true; |
| 1128 | } else { |
| 1129 | return false; |
| 1130 | } |
| 1131 | } |
| 1132 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1133 | void SkGpuDevice::drawBitmap(const SkDraw& draw, |
| 1134 | const SkBitmap& bitmap, |
| 1135 | const SkIRect* srcRectPtr, |
| 1136 | const SkMatrix& m, |
| 1137 | const SkPaint& paint) { |
| 1138 | CHECK_SHOULD_DRAW(draw); |
| 1139 | |
| 1140 | SkIRect srcRect; |
| 1141 | if (NULL == srcRectPtr) { |
| 1142 | srcRect.set(0, 0, bitmap.width(), bitmap.height()); |
| 1143 | } else { |
| 1144 | srcRect = *srcRectPtr; |
| 1145 | } |
| 1146 | |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1147 | if (paint.getMaskFilter()){ |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1148 | // Convert the bitmap to a shader so that the rect can be drawn |
| 1149 | // through drawRect, which supports mask filters. |
| 1150 | SkBitmap tmp; // subset of bitmap, if necessary |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1151 | const SkBitmap* bitmapPtr = &bitmap; |
epoger@google.com | 9ef2d83 | 2011-07-01 21:12:20 +0000 | [diff] [blame] | 1152 | if (srcRectPtr) { |
| 1153 | if (!bitmap.extractSubset(&tmp, srcRect)) { |
| 1154 | return; // extraction failed |
| 1155 | } |
| 1156 | bitmapPtr = &tmp; |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1157 | srcRect.set(0,0, srcRect.width(), srcRect.height()); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1158 | } |
| 1159 | SkPaint paintWithTexture(paint); |
| 1160 | paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr, |
| 1161 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref(); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1162 | SkRect ScalarRect; |
epoger@google.com | 9ef2d83 | 2011-07-01 21:12:20 +0000 | [diff] [blame] | 1163 | ScalarRect.set(srcRect); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1164 | |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1165 | // Transform 'm' needs to be concatenated to the draw matrix, |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1166 | // rather than transforming the primitive directly, so that 'm' will |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1167 | // also affect the behavior of the mask filter. |
| 1168 | SkMatrix drawMatrix; |
| 1169 | drawMatrix.setConcat(*draw.fMatrix, m); |
| 1170 | SkDraw transformedDraw(draw); |
| 1171 | transformedDraw.fMatrix = &drawMatrix; |
| 1172 | |
| 1173 | this->drawRect(transformedDraw, ScalarRect, paintWithTexture); |
| 1174 | |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1175 | return; |
| 1176 | } |
| 1177 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1178 | GrPaint grPaint; |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1179 | if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1180 | return; |
| 1181 | } |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 1182 | GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1183 | if (paint.isFilterBitmap()) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1184 | sampler->setFilter(GrSamplerState::kBilinear_Filter); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1185 | } else { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1186 | sampler->setFilter(GrSamplerState::kNearest_Filter); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1187 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1188 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1189 | int tileSize; |
| 1190 | if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) { |
| 1191 | // take the simple case |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1192 | this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1193 | return; |
| 1194 | } |
| 1195 | |
| 1196 | // undo the translate done by SkCanvas |
| 1197 | int DX = SkMax32(0, srcRect.fLeft); |
| 1198 | int DY = SkMax32(0, srcRect.fTop); |
| 1199 | // compute clip bounds in local coordinates |
| 1200 | SkIRect clipRect; |
| 1201 | { |
| 1202 | SkRect r; |
| 1203 | r.set(draw.fClip->getBounds()); |
| 1204 | SkMatrix matrix, inverse; |
| 1205 | matrix.setConcat(*draw.fMatrix, m); |
| 1206 | if (!matrix.invert(&inverse)) { |
| 1207 | return; |
| 1208 | } |
| 1209 | inverse.mapRect(&r); |
| 1210 | r.roundOut(&clipRect); |
| 1211 | // apply the canvas' translate to our local clip |
| 1212 | clipRect.offset(DX, DY); |
| 1213 | } |
| 1214 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1215 | int nx = bitmap.width() / tileSize; |
| 1216 | int ny = bitmap.height() / tileSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1217 | for (int x = 0; x <= nx; x++) { |
| 1218 | for (int y = 0; y <= ny; y++) { |
| 1219 | SkIRect tileR; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1220 | tileR.set(x * tileSize, y * tileSize, |
| 1221 | (x + 1) * tileSize, (y + 1) * tileSize); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1222 | if (!SkIRect::Intersects(tileR, clipRect)) { |
| 1223 | continue; |
| 1224 | } |
| 1225 | |
| 1226 | SkIRect srcR = tileR; |
| 1227 | if (!srcR.intersect(srcRect)) { |
| 1228 | continue; |
| 1229 | } |
| 1230 | |
| 1231 | SkBitmap tmpB; |
| 1232 | if (bitmap.extractSubset(&tmpB, tileR)) { |
| 1233 | // now offset it to make it "local" to our tmp bitmap |
| 1234 | srcR.offset(-tileR.fLeft, -tileR.fTop); |
| 1235 | |
| 1236 | SkMatrix tmpM(m); |
| 1237 | { |
| 1238 | int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft); |
| 1239 | int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop); |
| 1240 | tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy)); |
| 1241 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1242 | this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | /* |
| 1249 | * This is called by drawBitmap(), which has to handle images that may be too |
| 1250 | * large to be represented by a single texture. |
| 1251 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1252 | * internalDrawBitmap assumes that the specified bitmap will fit in a texture |
| 1253 | * 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] | 1254 | */ |
| 1255 | void SkGpuDevice::internalDrawBitmap(const SkDraw& draw, |
| 1256 | const SkBitmap& bitmap, |
| 1257 | const SkIRect& srcRect, |
| 1258 | const SkMatrix& m, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1259 | GrPaint* grPaint) { |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1260 | SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() && |
| 1261 | bitmap.height() <= fContext->getMaxTextureSize()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1262 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 1263 | SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1264 | if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 1265 | SkDebugf("nothing to draw\n"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1266 | return; |
| 1267 | } |
| 1268 | |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 1269 | GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1270 | |
| 1271 | sampler->setWrapX(GrSamplerState::kClamp_WrapMode); |
| 1272 | sampler->setWrapY(GrSamplerState::kClamp_WrapMode); |
| 1273 | sampler->setSampleMode(GrSamplerState::kNormal_SampleMode); |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 1274 | sampler->matrix()->reset(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1275 | |
| 1276 | GrTexture* texture; |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 1277 | SkAutoCachedTexture act(this, bitmap, sampler, &texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1278 | if (NULL == texture) { |
| 1279 | return; |
| 1280 | } |
| 1281 | |
bsalomon@google.com | 452943d | 2011-10-31 17:37:14 +0000 | [diff] [blame] | 1282 | grPaint->setTexture(kBitmapTextureIdx, texture); |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 1283 | |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 1284 | GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()), |
| 1285 | GrIntToScalar(srcRect.height())); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 1286 | GrRect paintRect; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1287 | paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()), |
| 1288 | GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()), |
| 1289 | GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()), |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 1290 | GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1291 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1292 | if (GrSamplerState::kNearest_Filter != sampler->getFilter() && |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1293 | (srcRect.width() < bitmap.width() || |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1294 | srcRect.height() < bitmap.height())) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1295 | // If drawing a subrect of the bitmap and filtering is enabled, |
| 1296 | // use a constrained texture domain to avoid color bleeding |
| 1297 | GrScalar left, top, right, bottom; |
| 1298 | if (srcRect.width() > 1) { |
| 1299 | GrScalar border = GR_ScalarHalf / bitmap.width(); |
| 1300 | left = paintRect.left() + border; |
| 1301 | right = paintRect.right() - border; |
| 1302 | } else { |
| 1303 | left = right = GrScalarHalf(paintRect.left() + paintRect.right()); |
| 1304 | } |
| 1305 | if (srcRect.height() > 1) { |
| 1306 | GrScalar border = GR_ScalarHalf / bitmap.height(); |
| 1307 | top = paintRect.top() + border; |
| 1308 | bottom = paintRect.bottom() - border; |
| 1309 | } else { |
| 1310 | top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom()); |
| 1311 | } |
| 1312 | GrRect textureDomain; |
| 1313 | textureDomain.setLTRB(left, top, right, bottom); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1314 | sampler->setTextureDomain(textureDomain); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 1317 | fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
| 1321 | int left, int top, const SkPaint& paint) { |
| 1322 | CHECK_SHOULD_DRAW(draw); |
| 1323 | |
| 1324 | SkAutoLockPixels alp(bitmap); |
| 1325 | if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
| 1326 | return; |
| 1327 | } |
| 1328 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1329 | int w = bitmap.width(); |
| 1330 | int h = bitmap.height(); |
| 1331 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1332 | GrPaint grPaint; |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1333 | if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1334 | return; |
| 1335 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1336 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1337 | GrAutoMatrix avm(fContext, GrMatrix::I()); |
| 1338 | |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 1339 | GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1340 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1341 | GrTexture* texture; |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 1342 | sampler->reset(); |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 1343 | SkAutoCachedTexture act(this, bitmap, sampler, &texture); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1344 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1345 | SkImageFilter* imageFilter = paint.getImageFilter(); |
| 1346 | SkSize blurSize; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 1347 | SkISize radius; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1348 | if (NULL != imageFilter && imageFilter->asABlur(&blurSize)) { |
| 1349 | GrAutoScratchTexture temp1, temp2; |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame^] | 1350 | GrTexture* blurTexture = fContext->gaussianBlur(texture, &temp1, &temp2, |
| 1351 | GrRect::MakeWH(w, h), |
| 1352 | blurSize.width(), |
| 1353 | blurSize.height()); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1354 | texture = blurTexture; |
| 1355 | grPaint.setTexture(kBitmapTextureIdx, texture); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 1356 | } else if (NULL != imageFilter && imageFilter->asADilate(&radius)) { |
| 1357 | const GrTextureDesc desc = { |
| 1358 | kRenderTarget_GrTextureFlagBit, |
| 1359 | w, |
| 1360 | h, |
| 1361 | kRGBA_8888_PM_GrPixelConfig, |
| 1362 | {0} // samples |
| 1363 | }; |
| 1364 | GrAutoScratchTexture temp1(fContext, desc), temp2(fContext, desc); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame^] | 1365 | texture = fContext->applyMorphology(texture, GrRect::MakeWH(w, h), |
| 1366 | temp1.texture(), temp2.texture(), |
| 1367 | GrSamplerState::kDilate_Filter, |
| 1368 | radius); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 1369 | grPaint.setTexture(kBitmapTextureIdx, texture); |
| 1370 | } else if (NULL != imageFilter && imageFilter->asAnErode(&radius)) { |
| 1371 | const GrTextureDesc desc = { |
| 1372 | kRenderTarget_GrTextureFlagBit, |
| 1373 | w, |
| 1374 | h, |
| 1375 | kRGBA_8888_PM_GrPixelConfig, |
| 1376 | {0} // samples |
| 1377 | }; |
| 1378 | GrAutoScratchTexture temp1(fContext, desc), temp2(fContext, desc); |
senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame^] | 1379 | texture = fContext->applyMorphology(texture, GrRect::MakeWH(w, h), |
| 1380 | temp1.texture(), temp2.texture(), |
| 1381 | GrSamplerState::kErode_Filter, |
| 1382 | radius); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 1383 | grPaint.setTexture(kBitmapTextureIdx, texture); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1384 | } else { |
| 1385 | grPaint.setTexture(kBitmapTextureIdx, texture); |
| 1386 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1387 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1388 | fContext->drawRectToRect(grPaint, |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1389 | GrRect::MakeXYWH(GrIntToScalar(left), |
| 1390 | GrIntToScalar(top), |
| 1391 | GrIntToScalar(w), |
| 1392 | GrIntToScalar(h)), |
| 1393 | GrRect::MakeWH(GR_Scalar1 * w / texture->width(), |
| 1394 | GR_Scalar1 * h / texture->height())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev, |
| 1398 | int x, int y, const SkPaint& paint) { |
| 1399 | CHECK_SHOULD_DRAW(draw); |
| 1400 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1401 | GrPaint grPaint; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 1402 | if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) || |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1403 | !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1404 | return; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1405 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1406 | |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1407 | GrTexture* devTex = grPaint.getTexture(0); |
| 1408 | SkASSERT(NULL != devTex); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1409 | |
| 1410 | const SkBitmap& bm = dev->accessBitmap(false); |
| 1411 | int w = bm.width(); |
| 1412 | int h = bm.height(); |
| 1413 | |
| 1414 | GrAutoMatrix avm(fContext, GrMatrix::I()); |
| 1415 | |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 1416 | grPaint.textureSampler(kBitmapTextureIdx)->reset(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1417 | |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1418 | GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x), |
| 1419 | GrIntToScalar(y), |
| 1420 | GrIntToScalar(w), |
| 1421 | GrIntToScalar(h)); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1422 | |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1423 | // The device being drawn may not fill up its texture (saveLayer uses |
| 1424 | // the approximate ). |
| 1425 | GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(), |
| 1426 | GR_Scalar1 * h / devTex->height()); |
| 1427 | |
| 1428 | fContext->drawRectToRect(grPaint, dstRect, srcRect); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1431 | bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src, |
| 1432 | const SkMatrix& ctm, |
| 1433 | SkBitmap* result, SkIPoint* offset) { |
| 1434 | SkSize size; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 1435 | SkISize radius; |
| 1436 | if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) { |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1437 | return false; |
| 1438 | } |
| 1439 | SkDevice* dev = this->createCompatibleDevice(SkBitmap::kARGB_8888_Config, |
| 1440 | src.width(), |
| 1441 | src.height(), |
| 1442 | false); |
| 1443 | if (NULL == dev) { |
| 1444 | return false; |
| 1445 | } |
| 1446 | SkAutoUnref aur(dev); |
| 1447 | SkCanvas canvas(dev); |
| 1448 | SkPaint paint; |
| 1449 | paint.setImageFilter(filter); |
| 1450 | canvas.drawSprite(src, 0, 0, &paint); |
| 1451 | *result = dev->accessBitmap(false); |
| 1452 | return true; |
| 1453 | } |
| 1454 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1455 | /////////////////////////////////////////////////////////////////////////////// |
| 1456 | |
| 1457 | // must be in SkCanvas::VertexMode order |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1458 | static const GrPrimitiveType gVertexMode2PrimitiveType[] = { |
| 1459 | kTriangles_PrimitiveType, |
| 1460 | kTriangleStrip_PrimitiveType, |
| 1461 | kTriangleFan_PrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1462 | }; |
| 1463 | |
| 1464 | void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode, |
| 1465 | int vertexCount, const SkPoint vertices[], |
| 1466 | const SkPoint texs[], const SkColor colors[], |
| 1467 | SkXfermode* xmode, |
| 1468 | const uint16_t indices[], int indexCount, |
| 1469 | const SkPaint& paint) { |
| 1470 | CHECK_SHOULD_DRAW(draw); |
| 1471 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1472 | GrPaint grPaint; |
| 1473 | SkAutoCachedTexture act; |
| 1474 | // we ignore the shader if texs is null. |
| 1475 | if (NULL == texs) { |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1476 | if (!skPaint2GrPaintNoShader(paint, |
| 1477 | false, |
| 1478 | NULL == colors, |
| 1479 | &grPaint)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1480 | return; |
| 1481 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1482 | } else { |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1483 | if (!skPaint2GrPaintShader(this, |
| 1484 | paint, |
| 1485 | *draw.fMatrix, |
| 1486 | NULL == colors, |
| 1487 | &act, |
| 1488 | &grPaint)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1489 | return; |
| 1490 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1491 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1492 | |
| 1493 | if (NULL != xmode && NULL != texs && NULL != colors) { |
mike@reedtribe.org | be2aa2a | 2011-11-17 02:32:04 +0000 | [diff] [blame] | 1494 | if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1495 | SkDebugf("Unsupported vertex-color/texture xfer mode.\n"); |
| 1496 | #if 0 |
| 1497 | return |
| 1498 | #endif |
| 1499 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1500 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1501 | |
bsalomon@google.com | 498776a | 2011-08-16 19:20:44 +0000 | [diff] [blame] | 1502 | SkAutoSTMalloc<128, GrColor> convertedColors(0); |
| 1503 | if (NULL != colors) { |
| 1504 | // need to convert byte order and from non-PM to PM |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 1505 | convertedColors.reset(vertexCount); |
bsalomon@google.com | 498776a | 2011-08-16 19:20:44 +0000 | [diff] [blame] | 1506 | for (int i = 0; i < vertexCount; ++i) { |
| 1507 | convertedColors[i] = SkGr::SkColor2GrColor(colors[i]); |
| 1508 | } |
| 1509 | colors = convertedColors.get(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1510 | } |
bsalomon@google.com | 498776a | 2011-08-16 19:20:44 +0000 | [diff] [blame] | 1511 | fContext->drawVertices(grPaint, |
| 1512 | gVertexMode2PrimitiveType[vmode], |
| 1513 | vertexCount, |
| 1514 | (GrPoint*) vertices, |
| 1515 | (GrPoint*) texs, |
| 1516 | colors, |
| 1517 | indices, |
| 1518 | indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | /////////////////////////////////////////////////////////////////////////////// |
| 1522 | |
| 1523 | static void GlyphCacheAuxProc(void* data) { |
| 1524 | delete (GrFontScaler*)data; |
| 1525 | } |
| 1526 | |
| 1527 | static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) { |
| 1528 | void* auxData; |
| 1529 | GrFontScaler* scaler = NULL; |
| 1530 | if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
| 1531 | scaler = (GrFontScaler*)auxData; |
| 1532 | } |
| 1533 | if (NULL == scaler) { |
| 1534 | scaler = new SkGrFontScaler(cache); |
| 1535 | cache->setAuxProc(GlyphCacheAuxProc, scaler); |
| 1536 | } |
| 1537 | return scaler; |
| 1538 | } |
| 1539 | |
| 1540 | static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state, |
| 1541 | SkFixed fx, SkFixed fy, |
| 1542 | const SkGlyph& glyph) { |
| 1543 | SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0); |
| 1544 | |
bungeman@google.com | 15865a7 | 2012-01-11 16:28:04 +0000 | [diff] [blame] | 1545 | GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1546 | |
| 1547 | if (NULL == procs->fFontScaler) { |
| 1548 | procs->fFontScaler = get_gr_font_scaler(state.fCache); |
| 1549 | } |
reed@google.com | 39ce0ac | 2011-04-08 15:42:19 +0000 | [diff] [blame] | 1550 | |
bungeman@google.com | 15865a7 | 2012-01-11 16:28:04 +0000 | [diff] [blame] | 1551 | procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 1552 | glyph.getSubXFixed(), |
| 1553 | glyph.getSubYFixed()), |
| 1554 | SkFixedFloorToFixed(fx), |
| 1555 | SkFixedFloorToFixed(fy), |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1556 | procs->fFontScaler); |
| 1557 | } |
| 1558 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1559 | SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1560 | |
| 1561 | // deferred allocation |
| 1562 | if (NULL == fDrawProcs) { |
| 1563 | fDrawProcs = new GrSkDrawProcs; |
| 1564 | fDrawProcs->fD1GProc = SkGPU_Draw1Glyph; |
| 1565 | fDrawProcs->fContext = fContext; |
| 1566 | } |
| 1567 | |
| 1568 | // init our (and GL's) state |
| 1569 | fDrawProcs->fTextContext = context; |
| 1570 | fDrawProcs->fFontScaler = NULL; |
| 1571 | return fDrawProcs; |
| 1572 | } |
| 1573 | |
| 1574 | void SkGpuDevice::drawText(const SkDraw& draw, const void* text, |
| 1575 | size_t byteLength, SkScalar x, SkScalar y, |
| 1576 | const SkPaint& paint) { |
| 1577 | CHECK_SHOULD_DRAW(draw); |
| 1578 | |
tomhudson@google.com | dd5f744 | 2011-08-30 15:13:55 +0000 | [diff] [blame] | 1579 | if (draw.fMatrix->hasPerspective()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1580 | // this guy will just call our drawPath() |
| 1581 | draw.drawText((const char*)text, byteLength, x, y, paint); |
| 1582 | } else { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1583 | SkDraw myDraw(draw); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1584 | |
| 1585 | GrPaint grPaint; |
| 1586 | SkAutoCachedTexture act; |
| 1587 | |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1588 | if (!skPaint2GrPaintShader(this, |
| 1589 | paint, |
| 1590 | *draw.fMatrix, |
| 1591 | true, |
| 1592 | &act, |
| 1593 | &grPaint)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1594 | return; |
| 1595 | } |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 1596 | GrTextContext context(fContext, grPaint, draw.fExtMatrix); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1597 | myDraw.fProcs = this->initDrawForText(&context); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1598 | this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint); |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, |
| 1603 | size_t byteLength, const SkScalar pos[], |
| 1604 | SkScalar constY, int scalarsPerPos, |
| 1605 | const SkPaint& paint) { |
| 1606 | CHECK_SHOULD_DRAW(draw); |
| 1607 | |
tomhudson@google.com | dd5f744 | 2011-08-30 15:13:55 +0000 | [diff] [blame] | 1608 | if (draw.fMatrix->hasPerspective()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1609 | // this guy will just call our drawPath() |
| 1610 | draw.drawPosText((const char*)text, byteLength, pos, constY, |
| 1611 | scalarsPerPos, paint); |
| 1612 | } else { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1613 | SkDraw myDraw(draw); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1614 | |
| 1615 | GrPaint grPaint; |
| 1616 | SkAutoCachedTexture act; |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1617 | if (!skPaint2GrPaintShader(this, |
| 1618 | paint, |
| 1619 | *draw.fMatrix, |
| 1620 | true, |
| 1621 | &act, |
| 1622 | &grPaint)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1623 | return; |
| 1624 | } |
| 1625 | |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 1626 | GrTextContext context(fContext, grPaint, draw.fExtMatrix); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1627 | myDraw.fProcs = this->initDrawForText(&context); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1628 | this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY, |
| 1629 | scalarsPerPos, paint); |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text, |
| 1634 | size_t len, const SkPath& path, |
| 1635 | const SkMatrix* m, const SkPaint& paint) { |
| 1636 | CHECK_SHOULD_DRAW(draw); |
| 1637 | |
| 1638 | SkASSERT(draw.fDevice == this); |
| 1639 | draw.drawTextOnPath((const char*)text, len, path, m, paint); |
| 1640 | } |
| 1641 | |
| 1642 | /////////////////////////////////////////////////////////////////////////////// |
| 1643 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1644 | bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) { |
| 1645 | if (!paint.isLCDRenderText()) { |
| 1646 | // we're cool with the paint as is |
| 1647 | return false; |
| 1648 | } |
| 1649 | |
| 1650 | if (paint.getShader() || |
| 1651 | paint.getXfermode() || // unless its srcover |
| 1652 | paint.getMaskFilter() || |
| 1653 | paint.getRasterizer() || |
| 1654 | paint.getColorFilter() || |
| 1655 | paint.getPathEffect() || |
| 1656 | paint.isFakeBoldText() || |
| 1657 | paint.getStyle() != SkPaint::kFill_Style) { |
| 1658 | // turn off lcd |
| 1659 | flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag; |
| 1660 | flags->fHinting = paint.getHinting(); |
| 1661 | return true; |
| 1662 | } |
| 1663 | // we're cool with the paint as is |
| 1664 | return false; |
| 1665 | } |
| 1666 | |
reed@google.com | 75d939b | 2011-12-07 15:07:23 +0000 | [diff] [blame] | 1667 | void SkGpuDevice::flush() { |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 1668 | fContext->resolveRenderTarget(fRenderTarget); |
reed@google.com | 75d939b | 2011-12-07 15:07:23 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1671 | /////////////////////////////////////////////////////////////////////////////// |
| 1672 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1673 | SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap, |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 1674 | const GrSamplerState* sampler, |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1675 | TexType type) { |
| 1676 | GrContext::TextureCacheEntry entry; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1677 | GrContext* ctx = this->context(); |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 1678 | |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1679 | if (kBitmap_TexType != type) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 1680 | const GrTextureDesc desc = { |
| 1681 | kRenderTarget_GrTextureFlagBit, |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 1682 | bitmap.width(), |
| 1683 | bitmap.height(), |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 1684 | SkGr::Bitmap2PixelConfig(bitmap), |
| 1685 | {0} // samples |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 1686 | }; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1687 | GrContext::ScratchTexMatch match; |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1688 | if (kSaveLayerDeviceRenderTarget_TexType == type) { |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1689 | // we know layers will only be drawn through drawDevice. |
| 1690 | // drawDevice has been made to work with content embedded in a |
| 1691 | // larger texture so its okay to use the approximate version. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1692 | match = GrContext::kApprox_ScratchTexMatch; |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1693 | } else { |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1694 | SkASSERT(kDeviceRenderTarget_TexType == type); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1695 | match = GrContext::kExact_ScratchTexMatch; |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1696 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1697 | entry = ctx->lockScratchTexture(desc, match); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1698 | } else { |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1699 | if (!bitmap.isVolatile()) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1700 | GrContext::TextureKey key = bitmap.getGenerationID(); |
| 1701 | key |= ((uint64_t) bitmap.pixelRefOffset()) << 32; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1702 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1703 | entry = ctx->findAndLockTexture(key, bitmap.width(), |
| 1704 | bitmap.height(), sampler); |
| 1705 | if (NULL == entry.texture()) { |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 1706 | entry = sk_gr_create_bitmap_texture(ctx, key, sampler, |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1707 | bitmap); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1708 | } |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1709 | } else { |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 1710 | entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, |
| 1711 | sampler, bitmap); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1712 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1713 | if (NULL == entry.texture()) { |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1714 | GrPrintf("---- failed to create texture for cache [%d %d]\n", |
| 1715 | bitmap.width(), bitmap.height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1716 | } |
| 1717 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1718 | return entry; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1721 | void SkGpuDevice::unlockCachedTexture(TexCache cache) { |
| 1722 | this->context()->unlockTexture(cache); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1725 | bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap, |
| 1726 | const GrSamplerState& sampler) const { |
| 1727 | GrContext::TextureKey key = bitmap.getGenerationID(); |
| 1728 | key |= ((uint64_t) bitmap.pixelRefOffset()) << 32; |
| 1729 | return this->context()->isTextureInCache(key, bitmap.width(), |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 1730 | bitmap.height(), &sampler); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1731 | |
| 1732 | } |
| 1733 | |
| 1734 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1735 | SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config, |
| 1736 | int width, int height, |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1737 | bool isOpaque, |
| 1738 | Usage usage) { |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1739 | return SkNEW_ARGS(SkGpuDevice,(this->context(), config, |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1740 | width, height, usage)); |
| 1741 | } |
| 1742 | |