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