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