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