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