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