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