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