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