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" |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 19 | #include "SkDeviceImageFilterProxy.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | #include "SkDrawProcs.h" |
| 21 | #include "SkGlyphCache.h" |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 22 | #include "SkImageFilter.h" |
reed@google.com | fe62638 | 2011-09-21 13:50:35 +0000 | [diff] [blame] | 23 | #include "SkTLazy.h" |
reed@google.com | c9aa587 | 2011-04-05 21:05:37 +0000 | [diff] [blame] | 24 | #include "SkUtils.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 26 | #define CACHE_COMPATIBLE_DEVICE_TEXTURES 1 |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | |
| 28 | #if 0 |
| 29 | extern bool (*gShouldDrawProc)(); |
| 30 | #define CHECK_SHOULD_DRAW(draw) \ |
| 31 | do { \ |
| 32 | if (gShouldDrawProc && !gShouldDrawProc()) return; \ |
| 33 | this->prepareRenderTarget(draw); \ |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 34 | GrAssert(!fNeedClear) \ |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 35 | } while (0) |
| 36 | #else |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 37 | #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \ |
| 38 | GrAssert(!fNeedClear) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 41 | // we use the same texture slot on GrPaint for bitmaps and shaders |
| 42 | // (since drawBitmap, drawSprite, and drawDevice ignore skia's shader) |
| 43 | enum { |
| 44 | kBitmapTextureIdx = 0, |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 45 | kShaderTextureIdx = 0, |
| 46 | kColorFilterTextureIdx = 1 |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 49 | |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 50 | #define MAX_BLUR_SIGMA 4.0f |
| 51 | // FIXME: This value comes from from SkBlurMaskFilter.cpp. |
| 52 | // Should probably be put in a common header someplace. |
| 53 | #define MAX_BLUR_RADIUS SkIntToScalar(128) |
| 54 | // This constant approximates the scaling done in the software path's |
| 55 | // "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)). |
| 56 | // IMHO, it actually should be 1: we blur "less" than we should do |
| 57 | // according to the CSS and canvas specs, simply because Safari does the same. |
| 58 | // Firefox used to do the same too, until 4.0 where they fixed it. So at some |
| 59 | // point we should probably get rid of these scaling constants and rebaseline |
| 60 | // all the blur tests. |
| 61 | #define BLUR_SIGMA_SCALE 0.6f |
junov@chromium.org | f32a9b6 | 2012-03-16 20:54:17 +0000 | [diff] [blame] | 62 | // This constant represents the screen alignment criterion in texels for |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 63 | // requiring texture domain clamping to prevent color bleeding when drawing |
junov@chromium.org | f32a9b6 | 2012-03-16 20:54:17 +0000 | [diff] [blame] | 64 | // a sub region of a larger source image. |
| 65 | #define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f) |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 66 | |
| 67 | #define DO_DEFERRED_CLEAR \ |
| 68 | do { \ |
| 69 | if (fNeedClear) { \ |
bsalomon@google.com | 730ca3b | 2012-04-03 13:25:12 +0000 | [diff] [blame] | 70 | this->clear(0x0); \ |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 71 | fNeedClear = false; \ |
| 72 | } \ |
| 73 | } while (false) \ |
| 74 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 75 | /////////////////////////////////////////////////////////////////////////////// |
| 76 | |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 77 | #define CHECK_FOR_NODRAW_ANNOTATION(paint) \ |
| 78 | do { if (paint.isNoDrawAnnotation()) { return; } } while (0) |
| 79 | |
| 80 | /////////////////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 83 | class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable { |
| 84 | public: |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 85 | SkAutoCachedTexture() |
| 86 | : fDevice(NULL) |
| 87 | , fTexture(NULL) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 88 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 89 | |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 90 | SkAutoCachedTexture(SkGpuDevice* device, |
| 91 | const SkBitmap& bitmap, |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 92 | const GrTextureParams* params, |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 93 | GrTexture** texture) |
| 94 | : fDevice(NULL) |
| 95 | , fTexture(NULL) { |
| 96 | GrAssert(NULL != texture); |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 97 | *texture = this->set(device, bitmap, params); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 98 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 99 | |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 100 | ~SkAutoCachedTexture() { |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 101 | if (NULL != fTexture) { |
| 102 | GrUnlockCachedBitmapTexture(fTexture); |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 103 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 104 | } |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 105 | |
| 106 | GrTexture* set(SkGpuDevice* device, |
| 107 | const SkBitmap& bitmap, |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 108 | const GrTextureParams* params) { |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 109 | if (NULL != fTexture) { |
| 110 | GrUnlockCachedBitmapTexture(fTexture); |
| 111 | fTexture = NULL; |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 112 | } |
| 113 | fDevice = device; |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 114 | GrTexture* result = (GrTexture*)bitmap.getTexture(); |
| 115 | if (NULL == result) { |
| 116 | // Cannot return the native texture so look it up in our cache |
| 117 | fTexture = GrLockCachedBitmapTexture(device->context(), bitmap, params); |
| 118 | result = fTexture; |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 119 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 120 | return result; |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 121 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 122 | |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 123 | private: |
| 124 | SkGpuDevice* fDevice; |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 125 | GrTexture* fTexture; |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 126 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 127 | |
| 128 | /////////////////////////////////////////////////////////////////////////////// |
| 129 | |
| 130 | bool gDoTraceDraw; |
| 131 | |
| 132 | struct GrSkDrawProcs : public SkDrawProcs { |
| 133 | public: |
| 134 | GrContext* fContext; |
| 135 | GrTextContext* fTextContext; |
| 136 | GrFontScaler* fFontScaler; // cached in the skia glyphcache |
| 137 | }; |
| 138 | |
| 139 | /////////////////////////////////////////////////////////////////////////////// |
| 140 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 141 | static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) { |
| 142 | switch (config) { |
| 143 | case kAlpha_8_GrPixelConfig: |
| 144 | *isOpaque = false; |
| 145 | return SkBitmap::kA8_Config; |
| 146 | case kRGB_565_GrPixelConfig: |
| 147 | *isOpaque = true; |
| 148 | return SkBitmap::kRGB_565_Config; |
| 149 | case kRGBA_4444_GrPixelConfig: |
| 150 | *isOpaque = false; |
| 151 | return SkBitmap::kARGB_4444_Config; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 152 | case kSkia8888_PM_GrPixelConfig: |
| 153 | // we don't currently have a way of knowing whether |
| 154 | // a 8888 is opaque based on the config. |
| 155 | *isOpaque = false; |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 156 | return SkBitmap::kARGB_8888_Config; |
| 157 | default: |
| 158 | *isOpaque = false; |
| 159 | return SkBitmap::kNo_Config; |
| 160 | } |
| 161 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 162 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 163 | static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) { |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 164 | GrPixelConfig config = renderTarget->config(); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 165 | |
| 166 | bool isOpaque; |
| 167 | SkBitmap bitmap; |
| 168 | bitmap.setConfig(grConfig2skConfig(config, &isOpaque), |
| 169 | renderTarget->width(), renderTarget->height()); |
| 170 | bitmap.setIsOpaque(isOpaque); |
| 171 | return bitmap; |
| 172 | } |
| 173 | |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 174 | SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture) |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 175 | : SkDevice(make_bitmap(context, texture->asRenderTarget())) { |
bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 176 | this->initFromRenderTarget(context, texture->asRenderTarget(), false); |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 177 | } |
| 178 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 179 | SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget) |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 180 | : SkDevice(make_bitmap(context, renderTarget)) { |
bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 181 | this->initFromRenderTarget(context, renderTarget, false); |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 182 | } |
| 183 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 184 | void SkGpuDevice::initFromRenderTarget(GrContext* context, |
bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 185 | GrRenderTarget* renderTarget, |
| 186 | bool cached) { |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 187 | fNeedPrepareRenderTarget = false; |
| 188 | fDrawProcs = NULL; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 189 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 190 | fContext = context; |
| 191 | fContext->ref(); |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 192 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 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(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 199 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 200 | // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref |
| 201 | // on the RT but not vice-versa. |
| 202 | // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without |
| 203 | // busting chrome (for a currently unknown reason). |
| 204 | GrSurface* surface = fRenderTarget->asTexture(); |
| 205 | if (NULL == surface) { |
| 206 | surface = fRenderTarget; |
bsalomon@google.com | d9ce125 | 2012-01-24 02:31:42 +0000 | [diff] [blame] | 207 | } |
bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 208 | SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached)); |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 209 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 210 | this->setPixelRef(pr, 0)->unref(); |
| 211 | } |
| 212 | |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 213 | SkGpuDevice::SkGpuDevice(GrContext* context, |
| 214 | SkBitmap::Config config, |
| 215 | int width, |
| 216 | int height) |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 217 | : SkDevice(config, width, height, false /*isOpaque*/) { |
| 218 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 219 | fNeedPrepareRenderTarget = false; |
| 220 | fDrawProcs = NULL; |
| 221 | |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 222 | fContext = context; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 223 | fContext->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 224 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 225 | fRenderTarget = NULL; |
| 226 | fNeedClear = false; |
| 227 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 228 | if (config != SkBitmap::kRGB_565_Config) { |
| 229 | config = SkBitmap::kARGB_8888_Config; |
| 230 | } |
| 231 | SkBitmap bm; |
| 232 | bm.setConfig(config, width, height); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 233 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 234 | GrTextureDesc desc; |
| 235 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 236 | desc.fWidth = width; |
| 237 | desc.fHeight = height; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 238 | desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 239 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 240 | SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0)); |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 241 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 242 | if (NULL != texture) { |
| 243 | fRenderTarget = texture->asRenderTarget(); |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 244 | fRenderTarget->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 245 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 246 | GrAssert(NULL != fRenderTarget); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 247 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 248 | // wrap the bitmap with a pixelref to expose our texture |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 249 | SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture)); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 250 | this->setPixelRef(pr, 0)->unref(); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 251 | } else { |
| 252 | GrPrintf("--- failed to create gpu-offscreen [%d %d]\n", |
| 253 | width, height); |
| 254 | GrAssert(false); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | SkGpuDevice::~SkGpuDevice() { |
| 259 | if (fDrawProcs) { |
| 260 | delete fDrawProcs; |
| 261 | } |
| 262 | |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 263 | // The SkGpuDevice gives the context the render target (e.g., in gainFocus) |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 264 | // This call gives the context a chance to relinquish it |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 265 | fContext->setRenderTarget(NULL); |
| 266 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 267 | SkSafeUnref(fRenderTarget); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 268 | fContext->unref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 269 | } |
| 270 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 271 | /////////////////////////////////////////////////////////////////////////////// |
| 272 | |
| 273 | void SkGpuDevice::makeRenderTargetCurrent() { |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 274 | DO_DEFERRED_CLEAR; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 275 | fContext->setRenderTarget(fRenderTarget); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 276 | fNeedPrepareRenderTarget = true; |
| 277 | } |
| 278 | |
| 279 | /////////////////////////////////////////////////////////////////////////////// |
| 280 | |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 281 | namespace { |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 282 | GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 283 | switch (config8888) { |
| 284 | case SkCanvas::kNative_Premul_Config8888: |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 285 | *flags = 0; |
| 286 | return kSkia8888_GrPixelConfig; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 287 | case SkCanvas::kNative_Unpremul_Config8888: |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 288 | *flags = GrContext::kUnpremul_PixelOpsFlag; |
| 289 | return kSkia8888_PM_GrPixelConfig; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 290 | case SkCanvas::kBGRA_Premul_Config8888: |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 291 | *flags = 0; |
| 292 | return kBGRA_8888_GrPixelConfig; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 293 | case SkCanvas::kBGRA_Unpremul_Config8888: |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 294 | *flags = GrContext::kUnpremul_PixelOpsFlag; |
| 295 | return kBGRA_8888_GrPixelConfig; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 296 | case SkCanvas::kRGBA_Premul_Config8888: |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 297 | *flags = 0; |
| 298 | return kRGBA_8888_GrPixelConfig; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 299 | case SkCanvas::kRGBA_Unpremul_Config8888: |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 300 | *flags = GrContext::kUnpremul_PixelOpsFlag; |
| 301 | return kRGBA_8888_GrPixelConfig; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 302 | default: |
| 303 | GrCrash("Unexpected Config8888."); |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 304 | *flags = 0; // suppress warning |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 305 | return kSkia8888_PM_GrPixelConfig; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 310 | bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap, |
| 311 | int x, int y, |
| 312 | SkCanvas::Config8888 config8888) { |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 313 | DO_DEFERRED_CLEAR; |
bsalomon@google.com | 910267d | 2011-11-02 20:06:25 +0000 | [diff] [blame] | 314 | SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config()); |
| 315 | SkASSERT(!bitmap.isNull()); |
| 316 | SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height()))); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 317 | |
bsalomon@google.com | 910267d | 2011-11-02 20:06:25 +0000 | [diff] [blame] | 318 | SkAutoLockPixels alp(bitmap); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 319 | GrPixelConfig config; |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 320 | uint32_t flags; |
| 321 | config = config8888_to_grconfig_and_flags(config8888, &flags); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 322 | return fContext->readRenderTargetPixels(fRenderTarget, |
| 323 | x, y, |
bsalomon@google.com | 910267d | 2011-11-02 20:06:25 +0000 | [diff] [blame] | 324 | bitmap.width(), |
| 325 | bitmap.height(), |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 326 | config, |
bsalomon@google.com | 910267d | 2011-11-02 20:06:25 +0000 | [diff] [blame] | 327 | bitmap.getPixels(), |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 328 | bitmap.rowBytes(), |
| 329 | flags); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 330 | } |
| 331 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 332 | void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y, |
| 333 | SkCanvas::Config8888 config8888) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 334 | SkAutoLockPixels alp(bitmap); |
| 335 | if (!bitmap.readyToDraw()) { |
| 336 | return; |
| 337 | } |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 338 | |
| 339 | GrPixelConfig config; |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 340 | uint32_t flags; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 341 | if (SkBitmap::kARGB_8888_Config == bitmap.config()) { |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 342 | config = config8888_to_grconfig_and_flags(config8888, &flags); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 343 | } else { |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 344 | flags = 0; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 345 | config= SkBitmapConfig2GrPixelConfig(bitmap.config()); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 346 | } |
| 347 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 348 | fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(), |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 349 | config, bitmap.getPixels(), bitmap.rowBytes(), flags); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 350 | } |
| 351 | |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 352 | namespace { |
| 353 | void purgeClipCB(int genID, void* data) { |
| 354 | GrContext* context = (GrContext*) data; |
| 355 | |
| 356 | if (SkClipStack::kInvalidGenID == genID || |
| 357 | SkClipStack::kEmptyGenID == genID || |
| 358 | SkClipStack::kWideOpenGenID == genID) { |
| 359 | // none of these cases will have a cached clip mask |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | } |
| 364 | }; |
| 365 | |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 366 | void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) { |
| 367 | INHERITED::onAttachToCanvas(canvas); |
| 368 | |
| 369 | // Canvas promises that this ptr is valid until onDetachFromCanvas is called |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 370 | fClipData.fClipStack = canvas->getClipStack(); |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 371 | |
| 372 | fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext); |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void SkGpuDevice::onDetachFromCanvas() { |
| 376 | INHERITED::onDetachFromCanvas(); |
| 377 | |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 378 | // TODO: iterate through the clip stack and clean up any cached clip masks |
| 379 | fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext); |
| 380 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 381 | fClipData.fClipStack = NULL; |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 382 | } |
| 383 | |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 384 | #ifdef SK_DEBUG |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 385 | static void check_bounds(const GrClipData& clipData, |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 386 | const SkRegion& clipRegion, |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 387 | int renderTargetWidth, |
| 388 | int renderTargetHeight) { |
| 389 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 390 | SkIRect devBound; |
| 391 | |
| 392 | devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight); |
| 393 | |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 394 | SkClipStack::BoundsType boundType; |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 395 | SkRect canvTemp; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 396 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 397 | clipData.fClipStack->getBounds(&canvTemp, &boundType); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 398 | if (SkClipStack::kNormal_BoundsType == boundType) { |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 399 | SkIRect devTemp; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 400 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 401 | canvTemp.roundOut(&devTemp); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 402 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 403 | devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 404 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 405 | if (!devBound.intersect(devTemp)) { |
| 406 | devBound.setEmpty(); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
robertphillips@google.com | 768fee8 | 2012-08-02 12:42:43 +0000 | [diff] [blame] | 410 | GrAssert(devBound.contains(clipRegion.getBounds())); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 411 | } |
| 412 | #endif |
| 413 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 414 | /////////////////////////////////////////////////////////////////////////////// |
| 415 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 416 | static void set_matrix_and_clip(GrContext* context, const SkMatrix& matrix, |
| 417 | GrClipData& clipData, |
| 418 | const SkRegion& clipRegion, |
| 419 | const SkIPoint& origin, |
| 420 | int renderTargetWidth, int renderTargetHeight) { |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 421 | context->setMatrix(matrix); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 422 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 423 | clipData.fOrigin = origin; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 424 | |
| 425 | #ifdef SK_DEBUG |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 426 | check_bounds(clipData, clipRegion, |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 427 | renderTargetWidth, renderTargetHeight); |
| 428 | #endif |
| 429 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 430 | context->setClip(&clipData); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 431 | } |
| 432 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 433 | // 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] | 434 | // and not the state from some other canvas/device |
| 435 | void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) { |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 436 | GrAssert(NULL != fClipData.fClipStack); |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 437 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 438 | if (fNeedPrepareRenderTarget || |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 439 | fContext->getRenderTarget() != fRenderTarget) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 440 | |
| 441 | fContext->setRenderTarget(fRenderTarget); |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 442 | SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 443 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 444 | set_matrix_and_clip(fContext, *draw.fMatrix, |
| 445 | fClipData, *draw.fClip, this->getOrigin(), |
| 446 | fRenderTarget->width(), fRenderTarget->height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 447 | fNeedPrepareRenderTarget = false; |
| 448 | } |
| 449 | } |
| 450 | |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 451 | void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip, |
| 452 | const SkClipStack& clipStack) { |
| 453 | this->INHERITED::setMatrixClip(matrix, clip, clipStack); |
| 454 | // 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] | 455 | fNeedPrepareRenderTarget = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 456 | } |
| 457 | |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 458 | void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) { |
| 459 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 460 | GrAssert(NULL != fClipData.fClipStack); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 461 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 462 | fContext->setRenderTarget(fRenderTarget); |
| 463 | |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 464 | this->INHERITED::gainFocus(matrix, clip); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 465 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 466 | set_matrix_and_clip(fContext, matrix, fClipData, clip, this->getOrigin(), |
| 467 | fRenderTarget->width(), fRenderTarget->height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 468 | |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 469 | DO_DEFERRED_CLEAR; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 470 | } |
| 471 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 472 | SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() { |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 473 | DO_DEFERRED_CLEAR; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 474 | return (SkGpuRenderTarget*)fRenderTarget; |
reed@google.com | 75d939b | 2011-12-07 15:07:23 +0000 | [diff] [blame] | 475 | } |
| 476 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 477 | bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) { |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 478 | GrTexture* texture = fRenderTarget->asTexture(); |
| 479 | if (NULL != texture) { |
bsalomon@google.com | 1c31f63 | 2012-07-26 19:39:06 +0000 | [diff] [blame] | 480 | paint->textureSampler(kBitmapTextureIdx)->setCustomStage( |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 481 | SkNEW_ARGS(GrSingleTextureEffect, (texture)))->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(); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 550 | // TODO: SkColorFilter::asCustomStage() |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 551 | if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) { |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 552 | grPaint->fColorMatrixEnabled = false; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 553 | if (!constantColor) { |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 554 | grPaint->fColorFilterColor = SkColor2GrColor(color); |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 555 | grPaint->fColorFilterXfermode = filterMode; |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 556 | } else { |
| 557 | SkColor filtered = colorFilter->filterColor(skPaint.getColor()); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 558 | grPaint->fColor = SkColor2GrColor(filtered); |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 559 | } |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 560 | } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) { |
| 561 | grPaint->fColorMatrixEnabled = true; |
| 562 | memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix)); |
| 563 | grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode; |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 564 | } else if (colorFilter != NULL && colorFilter->asComponentTable(&colorTransformTable)) { |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 565 | grPaint->resetColorFilter(); |
| 566 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 567 | // pass NULL because the color table effect doesn't use tiling or filtering. |
| 568 | GrTexture* texture = act->set(dev, colorTransformTable, NULL); |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 569 | GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx); |
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); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 593 | } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false, |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 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 | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 643 | GrTextureParams params(tileModes, skPaint.isFilterBitmap()); |
| 644 | GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, ¶ms); |
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 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 651 | sampler->reset(); |
| 652 | sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)))->unref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 653 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 654 | // since our texture coords will be in local space, we wack the texture |
| 655 | // matrix to map them back into 0...1 before we load it |
| 656 | SkMatrix localM; |
| 657 | if (shader->getLocalMatrix(&localM)) { |
| 658 | SkMatrix inverse; |
| 659 | if (localM.invert(&inverse)) { |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 660 | matrix->preConcat(inverse); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | if (SkShader::kDefault_BitmapType == bmptype) { |
bsalomon@google.com | 9183216 | 2012-03-08 19:53:02 +0000 | [diff] [blame] | 664 | GrScalar sx = SkFloatToScalar(1.f / bitmap.width()); |
| 665 | GrScalar sy = SkFloatToScalar(1.f / bitmap.height()); |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 666 | matrix->postScale(sx, sy); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 667 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 668 | |
| 669 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 670 | } |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 671 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 672 | |
| 673 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 674 | void SkGpuDevice::clear(SkColor color) { |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 675 | fContext->clear(NULL, color, fRenderTarget); |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 676 | } |
| 677 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 678 | void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) { |
| 679 | CHECK_SHOULD_DRAW(draw); |
| 680 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 681 | GrPaint grPaint; |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 682 | SkAutoCachedTexture textures[GrPaint::kMaxTextures]; |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 683 | if (!skPaint2GrPaintShader(this, |
| 684 | paint, |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 685 | true, |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 686 | textures, |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 687 | &grPaint)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 688 | return; |
| 689 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 690 | |
| 691 | fContext->drawPaint(grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | // must be in SkCanvas::PointMode order |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 695 | static const GrPrimitiveType gPointMode2PrimtiveType[] = { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 696 | kPoints_GrPrimitiveType, |
| 697 | kLines_GrPrimitiveType, |
| 698 | kLineStrip_GrPrimitiveType |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 699 | }; |
| 700 | |
| 701 | void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 702 | size_t count, const SkPoint pts[], const SkPaint& paint) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 703 | CHECK_SHOULD_DRAW(draw); |
| 704 | |
| 705 | SkScalar width = paint.getStrokeWidth(); |
| 706 | if (width < 0) { |
| 707 | return; |
| 708 | } |
| 709 | |
bsalomon@google.com | b702c0f | 2012-06-18 12:52:56 +0000 | [diff] [blame] | 710 | // we only handle hairlines and paints without path effects or mask filters, |
| 711 | // else we let the SkDraw call our drawPath() |
| 712 | if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 713 | draw.drawPoints(mode, count, pts, paint, true); |
| 714 | return; |
| 715 | } |
| 716 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 717 | GrPaint grPaint; |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 718 | SkAutoCachedTexture textures[GrPaint::kMaxTextures]; |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 719 | if (!skPaint2GrPaintShader(this, |
| 720 | paint, |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 721 | true, |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 722 | textures, |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 723 | &grPaint)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 724 | return; |
| 725 | } |
| 726 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 727 | fContext->drawVertices(grPaint, |
| 728 | gPointMode2PrimtiveType[mode], |
| 729 | count, |
| 730 | (GrPoint*)pts, |
| 731 | NULL, |
| 732 | NULL, |
| 733 | NULL, |
| 734 | 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 735 | } |
| 736 | |
reed@google.com | c9aa587 | 2011-04-05 21:05:37 +0000 | [diff] [blame] | 737 | /////////////////////////////////////////////////////////////////////////////// |
| 738 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 739 | void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, |
| 740 | const SkPaint& paint) { |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 741 | CHECK_FOR_NODRAW_ANNOTATION(paint); |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 742 | CHECK_SHOULD_DRAW(draw); |
| 743 | |
bungeman@google.com | 79bd877 | 2011-07-18 15:34:08 +0000 | [diff] [blame] | 744 | bool doStroke = paint.getStyle() != SkPaint::kFill_Style; |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 745 | SkScalar width = paint.getStrokeWidth(); |
| 746 | |
| 747 | /* |
| 748 | We have special code for hairline strokes, miter-strokes, and fills. |
| 749 | Anything else we just call our path code. |
| 750 | */ |
| 751 | bool usePath = doStroke && width > 0 && |
| 752 | paint.getStrokeJoin() != SkPaint::kMiter_Join; |
bsalomon@google.com | 22f42b7 | 2012-03-26 14:36:55 +0000 | [diff] [blame] | 753 | // another two reasons we might need to call drawPath... |
| 754 | if (paint.getMaskFilter() || paint.getPathEffect()) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 755 | usePath = true; |
| 756 | } |
reed@google.com | 67db664 | 2011-05-26 11:46:35 +0000 | [diff] [blame] | 757 | // until we aa rotated rects... |
| 758 | if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) { |
| 759 | usePath = true; |
| 760 | } |
bungeman@google.com | 633722e | 2011-08-09 18:32:51 +0000 | [diff] [blame] | 761 | // small miter limit means right angles show bevel... |
| 762 | if (SkPaint::kMiter_Join == paint.getStrokeJoin() && |
| 763 | paint.getStrokeMiter() < SK_ScalarSqrt2) |
| 764 | { |
| 765 | usePath = true; |
| 766 | } |
bungeman@google.com | 79bd877 | 2011-07-18 15:34:08 +0000 | [diff] [blame] | 767 | // until we can both stroke and fill rectangles |
bungeman@google.com | 79bd877 | 2011-07-18 15:34:08 +0000 | [diff] [blame] | 768 | if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 769 | usePath = true; |
| 770 | } |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 771 | |
| 772 | if (usePath) { |
| 773 | SkPath path; |
| 774 | path.addRect(rect); |
| 775 | this->drawPath(draw, path, paint, NULL, true); |
| 776 | return; |
| 777 | } |
| 778 | |
| 779 | GrPaint grPaint; |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 780 | SkAutoCachedTexture textures[GrPaint::kMaxTextures]; |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 781 | if (!skPaint2GrPaintShader(this, |
| 782 | paint, |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 783 | true, |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 784 | textures, |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 785 | &grPaint)) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 786 | return; |
| 787 | } |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 788 | fContext->drawRect(grPaint, rect, doStroke ? width : -1); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 789 | } |
| 790 | |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 791 | #include "SkMaskFilter.h" |
| 792 | #include "SkBounder.h" |
| 793 | |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 794 | /////////////////////////////////////////////////////////////////////////////// |
| 795 | |
| 796 | // helpers for applying mask filters |
| 797 | namespace { |
| 798 | |
| 799 | GrPathFill skToGrFillType(SkPath::FillType fillType) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 800 | switch (fillType) { |
| 801 | case SkPath::kWinding_FillType: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 802 | return kWinding_GrPathFill; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 803 | case SkPath::kEvenOdd_FillType: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 804 | return kEvenOdd_GrPathFill; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 805 | case SkPath::kInverseWinding_FillType: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 806 | return kInverseWinding_GrPathFill; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 807 | case SkPath::kInverseEvenOdd_FillType: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 808 | return kInverseEvenOdd_GrPathFill; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 809 | default: |
| 810 | SkDebugf("Unsupported path fill type\n"); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 811 | return kHairLine_GrPathFill; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 815 | // We prefer to blur small rect with small radius via CPU. |
| 816 | #define MIN_GPU_BLUR_SIZE SkIntToScalar(64) |
| 817 | #define MIN_GPU_BLUR_RADIUS SkIntToScalar(32) |
| 818 | inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) { |
| 819 | if (rect.width() <= MIN_GPU_BLUR_SIZE && |
| 820 | rect.height() <= MIN_GPU_BLUR_SIZE && |
| 821 | radius <= MIN_GPU_BLUR_RADIUS) { |
| 822 | return true; |
| 823 | } |
| 824 | return false; |
| 825 | } |
| 826 | |
| 827 | bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path, |
| 828 | SkMaskFilter* filter, const SkMatrix& matrix, |
| 829 | const SkRegion& clip, SkBounder* bounder, |
junov@chromium.org | aad7e27 | 2012-04-04 21:01:08 +0000 | [diff] [blame] | 830 | GrPaint* grp, GrPathFill pathFillType) { |
senorblanco@chromium.org | a479fc7 | 2011-07-19 16:40:58 +0000 | [diff] [blame] | 831 | #ifdef SK_DISABLE_GPU_BLUR |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 832 | return false; |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 833 | #endif |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 834 | SkMaskFilter::BlurInfo info; |
| 835 | SkMaskFilter::BlurType blurType = filter->asABlur(&info); |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 836 | if (SkMaskFilter::kNone_BlurType == blurType) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 837 | return false; |
| 838 | } |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 839 | SkScalar radius = info.fIgnoreTransform ? info.fRadius |
| 840 | : matrix.mapRadius(info.fRadius); |
| 841 | radius = SkMinScalar(radius, MAX_BLUR_RADIUS); |
senorblanco@chromium.org | 68c4d12 | 2011-08-01 21:20:31 +0000 | [diff] [blame] | 842 | if (radius <= 0) { |
| 843 | return false; |
| 844 | } |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 845 | |
| 846 | SkRect srcRect = path.getBounds(); |
| 847 | if (shouldDrawBlurWithCPU(srcRect, radius)) { |
| 848 | return false; |
| 849 | } |
| 850 | |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 851 | float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE; |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 852 | float sigma3 = sigma * 3.0f; |
| 853 | |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 854 | SkRect clipRect; |
| 855 | clipRect.set(clip.getBounds()); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 856 | |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 857 | // 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] | 858 | srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3)); |
| 859 | clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3)); |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 860 | srcRect.intersect(clipRect); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 861 | SkRect finalRect = srcRect; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 862 | SkIRect finalIRect; |
| 863 | finalRect.roundOut(&finalIRect); |
| 864 | if (clip.quickReject(finalIRect)) { |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 865 | return true; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 866 | } |
| 867 | if (bounder && !bounder->doIRect(finalIRect)) { |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 868 | return true; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 869 | } |
| 870 | GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop); |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 871 | srcRect.offset(offset); |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 872 | GrTextureDesc desc; |
| 873 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 874 | desc.fWidth = SkScalarCeilToInt(srcRect.width()); |
| 875 | desc.fHeight = SkScalarCeilToInt(srcRect.height()); |
| 876 | // We actually only need A8, but it often isn't supported as a |
| 877 | // render target so default to RGBA_8888 |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 878 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 879 | |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 880 | if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) { |
| 881 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 882 | } |
| 883 | |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 884 | GrAutoScratchTexture pathEntry(context, desc); |
| 885 | GrTexture* pathTexture = pathEntry.texture(); |
| 886 | if (NULL == pathTexture) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 887 | return false; |
| 888 | } |
| 889 | GrRenderTarget* oldRenderTarget = context->getRenderTarget(); |
senorblanco@chromium.org | 42dd0f9 | 2011-07-14 15:29:57 +0000 | [diff] [blame] | 890 | // Once this code moves into GrContext, this should be changed to use |
| 891 | // an AutoClipRestore. |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 892 | const GrClipData* oldClipData = context->getClip(); |
| 893 | |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 894 | context->setRenderTarget(pathTexture->asRenderTarget()); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 895 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 896 | SkClipStack newClipStack(srcRect); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 897 | GrClipData newClipData; |
| 898 | newClipData.fClipStack = &newClipStack; |
| 899 | context->setClip(&newClipData); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 900 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 901 | context->clear(NULL, 0); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 902 | GrPaint tempPaint; |
| 903 | tempPaint.reset(); |
| 904 | |
robertphillips@google.com | fea85ac | 2012-07-11 18:53:23 +0000 | [diff] [blame] | 905 | GrContext::AutoMatrix avm(context, GrMatrix::I()); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 906 | tempPaint.fAntiAlias = grp->fAntiAlias; |
| 907 | if (tempPaint.fAntiAlias) { |
| 908 | // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst |
| 909 | // blend coeff of zero requires dual source blending support in order |
| 910 | // to properly blend partially covered pixels. This means the AA |
| 911 | // code path may not be taken. So we use a dst blend coeff of ISA. We |
| 912 | // could special case AA draws to a dst surface with known alpha=0 to |
| 913 | // 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] | 914 | tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff; |
| 915 | tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff; |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 916 | } |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 917 | // 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] | 918 | context->drawPath(tempPaint, path, pathFillType, &offset); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 919 | |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 920 | // If we're doing a normal blur, we can clobber the pathTexture in the |
| 921 | // gaussianBlur. Otherwise, we need to save it for later compositing. |
| 922 | bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType; |
senorblanco@chromium.org | 1e95d71 | 2012-07-18 19:52:53 +0000 | [diff] [blame] | 923 | SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur( |
| 924 | pathTexture, isNormalBlur, srcRect, sigma, sigma)); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 925 | |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 926 | if (!isNormalBlur) { |
| 927 | GrPaint paint; |
| 928 | paint.reset(); |
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)) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1114 | SkPaint::Style style = doFill ? SkPaint::kFill_Style : |
junov@chromium.org | aad7e27 | 2012-04-04 21:01:08 +0000 | [diff] [blame] | 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, |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1157 | const SkRect& src, |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 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 | |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1167 | SkIRect iSrc; |
| 1168 | src.roundOut(&iSrc); |
| 1169 | |
| 1170 | maxTexTotalTileSize = get_tile_count(iSrc.fLeft, |
| 1171 | iSrc.fTop, |
| 1172 | iSrc.fRight, |
| 1173 | iSrc.fBottom, |
| 1174 | maxTextureSize); |
| 1175 | smallTotalTileSize = get_tile_count(iSrc.fLeft, |
| 1176 | iSrc.fTop, |
| 1177 | iSrc.fRight, |
| 1178 | iSrc.fBottom, |
| 1179 | kSmallTileSize); |
| 1180 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1181 | maxTexTotalTileSize *= maxTextureSize * maxTextureSize; |
| 1182 | smallTotalTileSize *= kSmallTileSize * kSmallTileSize; |
| 1183 | |
| 1184 | if (maxTexTotalTileSize > 2 * smallTotalTileSize) { |
| 1185 | return kSmallTileSize; |
| 1186 | } else { |
| 1187 | return maxTextureSize; |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap, |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 1193 | const GrTextureParams& params, |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1194 | const SkRect* srcRectPtr) const { |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1195 | // if bitmap is explictly texture backed then just use the texture |
| 1196 | if (NULL != bitmap.getTexture()) { |
| 1197 | return false; |
| 1198 | } |
| 1199 | // if it's larger than the max texture size, then we have no choice but |
| 1200 | // tiling |
| 1201 | const int maxTextureSize = fContext->getMaxTextureSize(); |
| 1202 | if (bitmap.width() > maxTextureSize || |
| 1203 | bitmap.height() > maxTextureSize) { |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1204 | return true; |
| 1205 | } |
| 1206 | // if we are going to have to draw the whole thing, then don't tile |
| 1207 | if (NULL == srcRectPtr) { |
| 1208 | return false; |
| 1209 | } |
| 1210 | // 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] | 1211 | if (this->isBitmapInTextureCache(bitmap, params)) { |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1212 | return false; |
| 1213 | } |
| 1214 | |
| 1215 | // At this point we know we could do the draw by uploading the entire bitmap |
| 1216 | // as a texture. However, if the texture would be large compared to the |
| 1217 | // cache size and we don't require most of it for this draw then tile to |
| 1218 | // reduce the amount of upload and cache spill. |
| 1219 | |
| 1220 | // assumption here is that sw bitmap size is a good proxy for its size as |
| 1221 | // a texture |
| 1222 | size_t bmpSize = bitmap.getSize(); |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 1223 | size_t cacheSize; |
| 1224 | fContext->getTextureCacheLimits(NULL, &cacheSize); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1225 | if (bmpSize < cacheSize / 2) { |
| 1226 | return false; |
| 1227 | } |
| 1228 | |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1229 | SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(), |
| 1230 | srcRectPtr->height() / bitmap.height()); |
| 1231 | if (fracUsed <= SK_ScalarHalf) { |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1232 | return true; |
| 1233 | } else { |
| 1234 | return false; |
| 1235 | } |
| 1236 | } |
| 1237 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1238 | void SkGpuDevice::drawBitmap(const SkDraw& draw, |
| 1239 | const SkBitmap& bitmap, |
| 1240 | const SkIRect* srcRectPtr, |
| 1241 | const SkMatrix& m, |
| 1242 | const SkPaint& paint) { |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1243 | |
| 1244 | SkRect tmp; |
| 1245 | SkRect* tmpPtr = NULL; |
| 1246 | |
| 1247 | // convert from SkIRect to SkRect |
| 1248 | if (NULL != srcRectPtr) { |
| 1249 | tmp.set(*srcRectPtr); |
| 1250 | tmpPtr = &tmp; |
| 1251 | } |
| 1252 | |
| 1253 | // We cannot call drawBitmapRect here since 'm' could be anything |
| 1254 | this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint); |
| 1255 | } |
| 1256 | |
| 1257 | void SkGpuDevice::drawBitmapCommon(const SkDraw& draw, |
| 1258 | const SkBitmap& bitmap, |
| 1259 | const SkRect* srcRectPtr, |
| 1260 | const SkMatrix& m, |
| 1261 | const SkPaint& paint) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1262 | CHECK_SHOULD_DRAW(draw); |
| 1263 | |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1264 | SkRect srcRect; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1265 | if (NULL == srcRectPtr) { |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1266 | srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1267 | } else { |
| 1268 | srcRect = *srcRectPtr; |
| 1269 | } |
| 1270 | |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1271 | if (paint.getMaskFilter()){ |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1272 | // Convert the bitmap to a shader so that the rect can be drawn |
| 1273 | // through drawRect, which supports mask filters. |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1274 | SkMatrix newM(m); |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1275 | SkBitmap tmp; // subset of bitmap, if necessary |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1276 | const SkBitmap* bitmapPtr = &bitmap; |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1277 | if (NULL != srcRectPtr) { |
| 1278 | SkIRect iSrc; |
| 1279 | srcRect.roundOut(&iSrc); |
| 1280 | if (!bitmap.extractSubset(&tmp, iSrc)) { |
epoger@google.com | 9ef2d83 | 2011-07-01 21:12:20 +0000 | [diff] [blame] | 1281 | return; // extraction failed |
| 1282 | } |
| 1283 | bitmapPtr = &tmp; |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1284 | srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop)); |
| 1285 | // The source rect has changed so update the matrix |
| 1286 | newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop)); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1287 | } |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1288 | |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1289 | SkPaint paintWithTexture(paint); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1290 | paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr, |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1291 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref(); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1292 | |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1293 | // Transform 'newM' needs to be concatenated to the draw matrix, |
| 1294 | // rather than transforming the primitive directly, so that 'newM' will |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1295 | // also affect the behavior of the mask filter. |
| 1296 | SkMatrix drawMatrix; |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1297 | drawMatrix.setConcat(*draw.fMatrix, newM); |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1298 | SkDraw transformedDraw(draw); |
| 1299 | transformedDraw.fMatrix = &drawMatrix; |
| 1300 | |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1301 | this->drawRect(transformedDraw, srcRect, paintWithTexture); |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1302 | |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1303 | return; |
| 1304 | } |
| 1305 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1306 | GrPaint grPaint; |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 1307 | SkAutoCachedTexture colorLutTexture; |
| 1308 | if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1309 | return; |
| 1310 | } |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1311 | GrTextureParams params; |
| 1312 | params.setBilerp(paint.isFilterBitmap()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1313 | |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1314 | if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) { |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1315 | // take the simple case |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1316 | this->internalDrawBitmap(draw, bitmap, srcRect, m, params, &grPaint); |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1317 | } else { |
| 1318 | this->drawTiledBitmap(draw, bitmap, srcRect, m, params, &grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1319 | } |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1320 | } |
| 1321 | |
| 1322 | // Break 'bitmap' into several tiles to draw it since it has already |
| 1323 | // been determined to be too large to fit in VRAM |
| 1324 | void SkGpuDevice::drawTiledBitmap(const SkDraw& draw, |
| 1325 | const SkBitmap& bitmap, |
| 1326 | const SkRect& srcRect, |
| 1327 | const SkMatrix& m, |
| 1328 | const GrTextureParams& params, |
| 1329 | GrPaint* grPaint) { |
| 1330 | const int maxTextureSize = fContext->getMaxTextureSize(); |
| 1331 | |
| 1332 | int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1333 | |
| 1334 | // undo the translate done by SkCanvas |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1335 | SkScalar DX = SkMaxScalar(0, srcRect.fLeft); |
| 1336 | SkScalar DY = SkMaxScalar(0, srcRect.fTop); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1337 | // compute clip bounds in local coordinates |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1338 | SkRect clipRect; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1339 | { |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1340 | clipRect.set(draw.fClip->getBounds()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1341 | SkMatrix matrix, inverse; |
| 1342 | matrix.setConcat(*draw.fMatrix, m); |
| 1343 | if (!matrix.invert(&inverse)) { |
| 1344 | return; |
| 1345 | } |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1346 | inverse.mapRect(&clipRect); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1347 | // apply the canvas' translate to our local clip |
| 1348 | clipRect.offset(DX, DY); |
| 1349 | } |
| 1350 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1351 | int nx = bitmap.width() / tileSize; |
| 1352 | int ny = bitmap.height() / tileSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1353 | for (int x = 0; x <= nx; x++) { |
| 1354 | for (int y = 0; y <= ny; y++) { |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1355 | SkRect tileR; |
| 1356 | tileR.set(SkIntToScalar(x * tileSize), |
| 1357 | SkIntToScalar(y * tileSize), |
| 1358 | SkIntToScalar((x + 1) * tileSize), |
| 1359 | SkIntToScalar((y + 1) * tileSize)); |
| 1360 | |
| 1361 | if (!SkRect::Intersects(tileR, clipRect)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1362 | continue; |
| 1363 | } |
| 1364 | |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1365 | if (!tileR.intersect(srcRect)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1366 | continue; |
| 1367 | } |
| 1368 | |
| 1369 | SkBitmap tmpB; |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1370 | SkIRect iTileR; |
| 1371 | tileR.roundOut(&iTileR); |
| 1372 | if (bitmap.extractSubset(&tmpB, iTileR)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1373 | // now offset it to make it "local" to our tmp bitmap |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1374 | tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1375 | |
| 1376 | SkMatrix tmpM(m); |
| 1377 | { |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1378 | SkScalar dx = iTileR.fLeft - DX + SkMaxScalar(0, tileR.fLeft); |
| 1379 | SkScalar dy = iTileR.fTop - DY + SkMaxScalar(0, tileR.fTop); |
| 1380 | tmpM.preTranslate(dx, dy); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1381 | } |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1382 | this->internalDrawBitmap(draw, tmpB, tileR, tmpM, params, grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | } |
| 1387 | |
junov@chromium.org | f32a9b6 | 2012-03-16 20:54:17 +0000 | [diff] [blame] | 1388 | namespace { |
| 1389 | |
| 1390 | bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) { |
| 1391 | // detect pixel disalignment |
| 1392 | if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - |
| 1393 | transformedRect.left()) < COLOR_BLEED_TOLERANCE && |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1394 | SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - |
junov@chromium.org | f32a9b6 | 2012-03-16 20:54:17 +0000 | [diff] [blame] | 1395 | transformedRect.top()) < COLOR_BLEED_TOLERANCE && |
| 1396 | SkScalarAbs(transformedRect.width() - srcRect.width()) < |
| 1397 | COLOR_BLEED_TOLERANCE && |
| 1398 | SkScalarAbs(transformedRect.height() - srcRect.height()) < |
| 1399 | COLOR_BLEED_TOLERANCE) { |
| 1400 | return true; |
| 1401 | } |
| 1402 | return false; |
| 1403 | } |
| 1404 | |
| 1405 | bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect, |
| 1406 | const SkMatrix& m) { |
| 1407 | // Only gets called if hasAlignedSamples returned false. |
| 1408 | // So we can assume that sampling is axis aligned but not texel aligned. |
| 1409 | GrAssert(!hasAlignedSamples(srcRect, transformedRect)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1410 | SkRect innerSrcRect(srcRect), innerTransformedRect, |
junov@chromium.org | f32a9b6 | 2012-03-16 20:54:17 +0000 | [diff] [blame] | 1411 | outerTransformedRect(transformedRect); |
| 1412 | innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf); |
| 1413 | m.mapRect(&innerTransformedRect, innerSrcRect); |
| 1414 | |
| 1415 | // The gap between outerTransformedRect and innerTransformedRect |
| 1416 | // represents the projection of the source border area, which is |
| 1417 | // problematic for color bleeding. We must check whether any |
| 1418 | // destination pixels sample the border area. |
| 1419 | outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE); |
| 1420 | innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE); |
| 1421 | SkIRect outer, inner; |
| 1422 | outerTransformedRect.round(&outer); |
| 1423 | innerTransformedRect.round(&inner); |
| 1424 | // If the inner and outer rects round to the same result, it means the |
| 1425 | // border does not overlap any pixel centers. Yay! |
| 1426 | return inner != outer; |
| 1427 | } |
| 1428 | |
| 1429 | } // unnamed namespace |
| 1430 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1431 | /* |
| 1432 | * This is called by drawBitmap(), which has to handle images that may be too |
| 1433 | * large to be represented by a single texture. |
| 1434 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1435 | * internalDrawBitmap assumes that the specified bitmap will fit in a texture |
| 1436 | * 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] | 1437 | */ |
| 1438 | void SkGpuDevice::internalDrawBitmap(const SkDraw& draw, |
| 1439 | const SkBitmap& bitmap, |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1440 | const SkRect& srcRect, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1441 | const SkMatrix& m, |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1442 | const GrTextureParams& params, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1443 | GrPaint* grPaint) { |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1444 | SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() && |
| 1445 | bitmap.height() <= fContext->getMaxTextureSize()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1446 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 1447 | SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1448 | if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 1449 | SkDebugf("nothing to draw\n"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1450 | return; |
| 1451 | } |
| 1452 | |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 1453 | GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1454 | |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 1455 | sampler->matrix()->reset(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1456 | |
| 1457 | GrTexture* texture; |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1458 | SkAutoCachedTexture act(this, bitmap, ¶ms, &texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1459 | if (NULL == texture) { |
| 1460 | return; |
| 1461 | } |
| 1462 | |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1463 | GrRect dstRect(srcRect); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 1464 | GrRect paintRect; |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1465 | SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width())); |
| 1466 | SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height())); |
| 1467 | paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv), |
| 1468 | SkScalarMul(srcRect.fTop, hInv), |
| 1469 | SkScalarMul(srcRect.fRight, wInv), |
| 1470 | SkScalarMul(srcRect.fBottom, hInv)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1471 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1472 | bool needsTextureDomain = false; |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1473 | if (params.isBilerp()) { |
junov@chromium.org | f32a9b6 | 2012-03-16 20:54:17 +0000 | [diff] [blame] | 1474 | // Need texture domain if drawing a sub rect. |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1475 | needsTextureDomain = srcRect.width() < bitmap.width() || |
| 1476 | srcRect.height() < bitmap.height(); |
junov@chromium.org | f32a9b6 | 2012-03-16 20:54:17 +0000 | [diff] [blame] | 1477 | if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) { |
| 1478 | // sampling is axis-aligned |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1479 | GrRect transformedRect; |
junov@chromium.org | f32a9b6 | 2012-03-16 20:54:17 +0000 | [diff] [blame] | 1480 | SkMatrix srcToDeviceMatrix(m); |
| 1481 | srcToDeviceMatrix.postConcat(*draw.fMatrix); |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1482 | srcToDeviceMatrix.mapRect(&transformedRect, srcRect); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1483 | |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1484 | if (hasAlignedSamples(srcRect, transformedRect)) { |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1485 | // We could also turn off filtering here (but we already did a cache lookup with |
| 1486 | // params). |
junov@chromium.org | f32a9b6 | 2012-03-16 20:54:17 +0000 | [diff] [blame] | 1487 | needsTextureDomain = false; |
| 1488 | } else { |
| 1489 | needsTextureDomain = needsTextureDomain && |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1490 | mayColorBleed(srcRect, transformedRect, m); |
junov@chromium.org | f32a9b6 | 2012-03-16 20:54:17 +0000 | [diff] [blame] | 1491 | } |
| 1492 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1493 | } |
junov@chromium.org | f32a9b6 | 2012-03-16 20:54:17 +0000 | [diff] [blame] | 1494 | |
| 1495 | GrRect textureDomain = GrRect::MakeEmpty(); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1496 | SkAutoTUnref<GrCustomStage> stage; |
junov@chromium.org | f32a9b6 | 2012-03-16 20:54:17 +0000 | [diff] [blame] | 1497 | if (needsTextureDomain) { |
| 1498 | // Use a constrained texture domain to avoid color bleeding |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1499 | GrScalar left, top, right, bottom; |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1500 | if (srcRect.width() > GR_Scalar1) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1501 | GrScalar border = GR_ScalarHalf / bitmap.width(); |
| 1502 | left = paintRect.left() + border; |
| 1503 | right = paintRect.right() - border; |
| 1504 | } else { |
| 1505 | left = right = GrScalarHalf(paintRect.left() + paintRect.right()); |
| 1506 | } |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1507 | if (srcRect.height() > GR_Scalar1) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1508 | GrScalar border = GR_ScalarHalf / bitmap.height(); |
| 1509 | top = paintRect.top() + border; |
| 1510 | bottom = paintRect.bottom() - border; |
| 1511 | } else { |
| 1512 | top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom()); |
| 1513 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1514 | textureDomain.setLTRB(left, top, right, bottom); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1515 | stage.reset(SkNEW_ARGS(GrTextureDomainEffect, (texture, textureDomain, params))); |
| 1516 | } else { |
| 1517 | stage.reset(SkNEW_ARGS(GrSingleTextureEffect, (texture, params))); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1518 | } |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1519 | grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(stage); |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 1520 | fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1523 | namespace { |
| 1524 | |
| 1525 | void apply_custom_stage(GrContext* context, |
| 1526 | GrTexture* srcTexture, |
| 1527 | GrTexture* dstTexture, |
| 1528 | const GrRect& rect, |
| 1529 | GrCustomStage* stage) { |
| 1530 | SkASSERT(srcTexture && srcTexture->getContext() == context); |
robertphillips@google.com | fea85ac | 2012-07-11 18:53:23 +0000 | [diff] [blame] | 1531 | GrContext::AutoMatrix avm(context, GrMatrix::I()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1532 | GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget()); |
robertphillips@google.com | 56c79b1 | 2012-07-11 20:57:46 +0000 | [diff] [blame] | 1533 | GrContext::AutoClip acs(context, rect); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1534 | |
| 1535 | GrMatrix sampleM; |
| 1536 | sampleM.setIDiv(srcTexture->width(), srcTexture->height()); |
| 1537 | GrPaint paint; |
| 1538 | paint.reset(); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1539 | paint.textureSampler(0)->reset(sampleM); |
| 1540 | paint.textureSampler(0)->setCustomStage(stage); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1541 | context->drawRect(paint, rect); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | }; |
| 1545 | |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 1546 | static GrTexture* filter_texture(SkDevice* device, GrContext* context, |
| 1547 | GrTexture* texture, SkImageFilter* filter, |
| 1548 | const GrRect& rect) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1549 | GrAssert(filter); |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 1550 | SkDeviceImageFilterProxy proxy(device); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1551 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 1552 | GrTextureDesc desc; |
| 1553 | desc.fFlags = kRenderTarget_GrTextureFlagBit, |
| 1554 | desc.fWidth = SkScalarCeilToInt(rect.width()); |
| 1555 | desc.fHeight = SkScalarCeilToInt(rect.height()); |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1556 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1557 | GrCustomStage* stage; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1558 | |
senorblanco@chromium.org | 302cffb | 2012-08-01 20:16:34 +0000 | [diff] [blame] | 1559 | if (filter->canFilterImageGPU()) { |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 1560 | texture = filter->onFilterImageGPU(&proxy, texture, rect); |
senorblanco@chromium.org | 302cffb | 2012-08-01 20:16:34 +0000 | [diff] [blame] | 1561 | } else if (filter->asNewCustomStage(&stage, texture)) { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1562 | GrAutoScratchTexture dst(context, desc); |
| 1563 | apply_custom_stage(context, texture, dst.texture(), rect, stage); |
| 1564 | texture = dst.detach(); |
| 1565 | stage->unref(); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1566 | } |
| 1567 | return texture; |
| 1568 | } |
| 1569 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1570 | void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
| 1571 | int left, int top, const SkPaint& paint) { |
| 1572 | CHECK_SHOULD_DRAW(draw); |
| 1573 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1574 | SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1575 | if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
| 1576 | return; |
| 1577 | } |
| 1578 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1579 | int w = bitmap.width(); |
| 1580 | int h = bitmap.height(); |
| 1581 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1582 | GrPaint grPaint; |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 1583 | SkAutoCachedTexture colorLutTexture; |
| 1584 | if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1585 | return; |
| 1586 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1587 | |
robertphillips@google.com | fea85ac | 2012-07-11 18:53:23 +0000 | [diff] [blame] | 1588 | GrContext::AutoMatrix avm(fContext, GrMatrix::I()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1589 | |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 1590 | GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1591 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1592 | GrTexture* texture; |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 1593 | sampler->reset(); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1594 | // draw sprite uses the default texture params |
| 1595 | SkAutoCachedTexture act(this, bitmap, NULL, &texture); |
tomhudson@google.com | 1e8f016 | 2012-07-20 16:25:18 +0000 | [diff] [blame] | 1596 | grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS |
| 1597 | (GrSingleTextureEffect, (texture)))->unref(); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1598 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1599 | SkImageFilter* filter = paint.getImageFilter(); |
| 1600 | if (NULL != filter) { |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 1601 | GrTexture* filteredTexture = filter_texture(this, fContext, texture, filter, |
robertphillips@google.com | 8637a36 | 2012-04-10 18:32:35 +0000 | [diff] [blame] | 1602 | GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h))); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1603 | if (filteredTexture) { |
tomhudson@google.com | 1e8f016 | 2012-07-20 16:25:18 +0000 | [diff] [blame] | 1604 | grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS |
| 1605 | (GrSingleTextureEffect, (filteredTexture)))->unref(); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1606 | texture = filteredTexture; |
| 1607 | filteredTexture->unref(); |
| 1608 | } |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1609 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1610 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1611 | fContext->drawRectToRect(grPaint, |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1612 | GrRect::MakeXYWH(GrIntToScalar(left), |
| 1613 | GrIntToScalar(top), |
| 1614 | GrIntToScalar(w), |
| 1615 | GrIntToScalar(h)), |
| 1616 | GrRect::MakeWH(GR_Scalar1 * w / texture->width(), |
| 1617 | GR_Scalar1 * h / texture->height())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1620 | void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap, |
| 1621 | const SkRect* src, const SkRect& dst, |
| 1622 | const SkPaint& paint) { |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1623 | SkMatrix matrix; |
| 1624 | SkRect bitmapBounds, tmpSrc; |
| 1625 | |
| 1626 | bitmapBounds.set(0, 0, |
| 1627 | SkIntToScalar(bitmap.width()), |
| 1628 | SkIntToScalar(bitmap.height())); |
| 1629 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1630 | // Compute matrix from the two rectangles |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1631 | if (NULL != src) { |
| 1632 | tmpSrc = *src; |
| 1633 | } else { |
| 1634 | tmpSrc = bitmapBounds; |
| 1635 | } |
| 1636 | matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); |
| 1637 | |
| 1638 | // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null. |
| 1639 | if (NULL != src) { |
| 1640 | if (!bitmapBounds.contains(tmpSrc)) { |
| 1641 | if (!tmpSrc.intersect(bitmapBounds)) { |
| 1642 | return; // nothing to draw |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1643 | } |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1644 | } |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1645 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 1646 | |
robertphillips@google.com | bac6b05 | 2012-09-28 18:06:49 +0000 | [diff] [blame^] | 1647 | this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 1650 | void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1651 | int x, int y, const SkPaint& paint) { |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 1652 | // clear of the source device must occur before CHECK_SHOULD_DRAW |
| 1653 | SkGpuDevice* dev = static_cast<SkGpuDevice*>(device); |
| 1654 | if (dev->fNeedClear) { |
| 1655 | // TODO: could check here whether we really need to draw at all |
| 1656 | dev->clear(0x0); |
| 1657 | } |
| 1658 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1659 | CHECK_SHOULD_DRAW(draw); |
| 1660 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1661 | GrPaint grPaint; |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 1662 | SkAutoCachedTexture colorLutTexture; |
bsalomon@google.com | 1c31f63 | 2012-07-26 19:39:06 +0000 | [diff] [blame] | 1663 | grPaint.textureSampler(kBitmapTextureIdx)->reset(); |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 1664 | if (!dev->bindDeviceAsTexture(&grPaint) || |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 1665 | !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1666 | return; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1667 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1668 | |
bsalomon@google.com | 1c31f63 | 2012-07-26 19:39:06 +0000 | [diff] [blame] | 1669 | GrTexture* devTex = grPaint.getTextureSampler(kBitmapTextureIdx).getCustomStage()->texture(0); |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1670 | SkASSERT(NULL != devTex); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1671 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1672 | SkImageFilter* filter = paint.getImageFilter(); |
| 1673 | if (NULL != filter) { |
bsalomon@google.com | 1c31f63 | 2012-07-26 19:39:06 +0000 | [diff] [blame] | 1674 | GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()), |
robertphillips@google.com | 8637a36 | 2012-04-10 18:32:35 +0000 | [diff] [blame] | 1675 | SkIntToScalar(devTex->height())); |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 1676 | GrTexture* filteredTexture = filter_texture(this, fContext, devTex, filter, rect); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1677 | if (filteredTexture) { |
tomhudson@google.com | 1e8f016 | 2012-07-20 16:25:18 +0000 | [diff] [blame] | 1678 | grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS |
| 1679 | (GrSingleTextureEffect, (filteredTexture)))->unref(); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1680 | devTex = filteredTexture; |
| 1681 | filteredTexture->unref(); |
| 1682 | } |
| 1683 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1684 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1685 | const SkBitmap& bm = dev->accessBitmap(false); |
| 1686 | int w = bm.width(); |
| 1687 | int h = bm.height(); |
| 1688 | |
robertphillips@google.com | fea85ac | 2012-07-11 18:53:23 +0000 | [diff] [blame] | 1689 | GrContext::AutoMatrix avm(fContext, GrMatrix::I()); |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1690 | GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x), |
| 1691 | GrIntToScalar(y), |
| 1692 | GrIntToScalar(w), |
| 1693 | GrIntToScalar(h)); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1694 | |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1695 | // The device being drawn may not fill up its texture (saveLayer uses |
| 1696 | // the approximate ). |
| 1697 | GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(), |
| 1698 | GR_Scalar1 * h / devTex->height()); |
| 1699 | |
| 1700 | fContext->drawRectToRect(grPaint, dstRect, srcRect); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1703 | bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) { |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 1704 | if (!filter->asNewCustomStage(NULL, NULL) && |
senorblanco@chromium.org | 302cffb | 2012-08-01 20:16:34 +0000 | [diff] [blame] | 1705 | !filter->canFilterImageGPU()) { |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1706 | return false; |
| 1707 | } |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1708 | return true; |
| 1709 | } |
| 1710 | |
| 1711 | bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src, |
| 1712 | const SkMatrix& ctm, |
| 1713 | SkBitmap* result, SkIPoint* offset) { |
| 1714 | // want explicitly our impl, so guard against a subclass of us overriding it |
| 1715 | if (!this->SkGpuDevice::canHandleImageFilter(filter)) { |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1716 | return false; |
| 1717 | } |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1718 | |
| 1719 | SkAutoLockPixels alp(src, !src.getTexture()); |
| 1720 | if (!src.getTexture() && !src.readyToDraw()) { |
| 1721 | return false; |
| 1722 | } |
| 1723 | |
| 1724 | GrPaint paint; |
| 1725 | paint.reset(); |
| 1726 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1727 | GrTexture* texture; |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1728 | // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup |
| 1729 | // must be pushed upstack. |
| 1730 | SkAutoCachedTexture act(this, src, NULL, &texture); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1731 | |
| 1732 | result->setConfig(src.config(), src.width(), src.height()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1733 | GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()), |
robertphillips@google.com | 8637a36 | 2012-04-10 18:32:35 +0000 | [diff] [blame] | 1734 | SkIntToScalar(src.height())); |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 1735 | GrTexture* resultTexture = filter_texture(this, fContext, texture, filter, rect); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1736 | if (resultTexture) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1737 | result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef, |
| 1738 | (resultTexture)))->unref(); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1739 | resultTexture->unref(); |
| 1740 | } |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1741 | return true; |
| 1742 | } |
| 1743 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1744 | /////////////////////////////////////////////////////////////////////////////// |
| 1745 | |
| 1746 | // must be in SkCanvas::VertexMode order |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1747 | static const GrPrimitiveType gVertexMode2PrimitiveType[] = { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1748 | kTriangles_GrPrimitiveType, |
| 1749 | kTriangleStrip_GrPrimitiveType, |
| 1750 | kTriangleFan_GrPrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1751 | }; |
| 1752 | |
| 1753 | void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode, |
| 1754 | int vertexCount, const SkPoint vertices[], |
| 1755 | const SkPoint texs[], const SkColor colors[], |
| 1756 | SkXfermode* xmode, |
| 1757 | const uint16_t indices[], int indexCount, |
| 1758 | const SkPaint& paint) { |
| 1759 | CHECK_SHOULD_DRAW(draw); |
| 1760 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1761 | GrPaint grPaint; |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 1762 | SkAutoCachedTexture textures[GrPaint::kMaxTextures]; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1763 | // we ignore the shader if texs is null. |
| 1764 | if (NULL == texs) { |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 1765 | if (!skPaint2GrPaintNoShader(this, |
| 1766 | paint, |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1767 | false, |
| 1768 | NULL == colors, |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 1769 | &textures[kColorFilterTextureIdx], |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1770 | &grPaint)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1771 | return; |
| 1772 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1773 | } else { |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1774 | if (!skPaint2GrPaintShader(this, |
| 1775 | paint, |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1776 | NULL == colors, |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 1777 | textures, |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1778 | &grPaint)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1779 | return; |
| 1780 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1781 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1782 | |
| 1783 | if (NULL != xmode && NULL != texs && NULL != colors) { |
mike@reedtribe.org | be2aa2a | 2011-11-17 02:32:04 +0000 | [diff] [blame] | 1784 | if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1785 | SkDebugf("Unsupported vertex-color/texture xfer mode.\n"); |
| 1786 | #if 0 |
| 1787 | return |
| 1788 | #endif |
| 1789 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1790 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1791 | |
bsalomon@google.com | 498776a | 2011-08-16 19:20:44 +0000 | [diff] [blame] | 1792 | SkAutoSTMalloc<128, GrColor> convertedColors(0); |
| 1793 | if (NULL != colors) { |
| 1794 | // need to convert byte order and from non-PM to PM |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 1795 | convertedColors.reset(vertexCount); |
bsalomon@google.com | 498776a | 2011-08-16 19:20:44 +0000 | [diff] [blame] | 1796 | for (int i = 0; i < vertexCount; ++i) { |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 1797 | convertedColors[i] = SkColor2GrColor(colors[i]); |
bsalomon@google.com | 498776a | 2011-08-16 19:20:44 +0000 | [diff] [blame] | 1798 | } |
| 1799 | colors = convertedColors.get(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1800 | } |
bsalomon@google.com | 498776a | 2011-08-16 19:20:44 +0000 | [diff] [blame] | 1801 | fContext->drawVertices(grPaint, |
| 1802 | gVertexMode2PrimitiveType[vmode], |
| 1803 | vertexCount, |
| 1804 | (GrPoint*) vertices, |
| 1805 | (GrPoint*) texs, |
| 1806 | colors, |
| 1807 | indices, |
| 1808 | indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | /////////////////////////////////////////////////////////////////////////////// |
| 1812 | |
| 1813 | static void GlyphCacheAuxProc(void* data) { |
reed@google.com | 26344cf | 2012-06-27 18:23:01 +0000 | [diff] [blame] | 1814 | GrFontScaler* scaler = (GrFontScaler*)data; |
| 1815 | SkSafeUnref(scaler); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) { |
| 1819 | void* auxData; |
| 1820 | GrFontScaler* scaler = NULL; |
| 1821 | if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
| 1822 | scaler = (GrFontScaler*)auxData; |
| 1823 | } |
| 1824 | if (NULL == scaler) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1825 | scaler = SkNEW_ARGS(SkGrFontScaler, (cache)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1826 | cache->setAuxProc(GlyphCacheAuxProc, scaler); |
| 1827 | } |
| 1828 | return scaler; |
| 1829 | } |
| 1830 | |
| 1831 | static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state, |
| 1832 | SkFixed fx, SkFixed fy, |
| 1833 | const SkGlyph& glyph) { |
| 1834 | SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0); |
| 1835 | |
bungeman@google.com | 15865a7 | 2012-01-11 16:28:04 +0000 | [diff] [blame] | 1836 | GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1837 | |
| 1838 | if (NULL == procs->fFontScaler) { |
| 1839 | procs->fFontScaler = get_gr_font_scaler(state.fCache); |
| 1840 | } |
reed@google.com | 39ce0ac | 2011-04-08 15:42:19 +0000 | [diff] [blame] | 1841 | |
bungeman@google.com | 15865a7 | 2012-01-11 16:28:04 +0000 | [diff] [blame] | 1842 | procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 1843 | glyph.getSubXFixed(), |
| 1844 | glyph.getSubYFixed()), |
| 1845 | SkFixedFloorToFixed(fx), |
| 1846 | SkFixedFloorToFixed(fy), |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1847 | procs->fFontScaler); |
| 1848 | } |
| 1849 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1850 | SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1851 | |
| 1852 | // deferred allocation |
| 1853 | if (NULL == fDrawProcs) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1854 | fDrawProcs = SkNEW(GrSkDrawProcs); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1855 | fDrawProcs->fD1GProc = SkGPU_Draw1Glyph; |
| 1856 | fDrawProcs->fContext = fContext; |
| 1857 | } |
| 1858 | |
| 1859 | // init our (and GL's) state |
| 1860 | fDrawProcs->fTextContext = context; |
| 1861 | fDrawProcs->fFontScaler = NULL; |
| 1862 | return fDrawProcs; |
| 1863 | } |
| 1864 | |
| 1865 | void SkGpuDevice::drawText(const SkDraw& draw, const void* text, |
| 1866 | size_t byteLength, SkScalar x, SkScalar y, |
| 1867 | const SkPaint& paint) { |
| 1868 | CHECK_SHOULD_DRAW(draw); |
| 1869 | |
tomhudson@google.com | dd5f744 | 2011-08-30 15:13:55 +0000 | [diff] [blame] | 1870 | if (draw.fMatrix->hasPerspective()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1871 | // this guy will just call our drawPath() |
| 1872 | draw.drawText((const char*)text, byteLength, x, y, paint); |
| 1873 | } else { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1874 | SkDraw myDraw(draw); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1875 | |
| 1876 | GrPaint grPaint; |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 1877 | SkAutoCachedTexture textures[GrPaint::kMaxTextures]; |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1878 | if (!skPaint2GrPaintShader(this, |
| 1879 | paint, |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1880 | true, |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 1881 | textures, |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1882 | &grPaint)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1883 | return; |
| 1884 | } |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 1885 | GrTextContext context(fContext, grPaint, draw.fExtMatrix); |
| 1886 | myDraw.fProcs = this->initDrawForText(&context); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1887 | this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint); |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, |
| 1892 | size_t byteLength, const SkScalar pos[], |
| 1893 | SkScalar constY, int scalarsPerPos, |
| 1894 | const SkPaint& paint) { |
| 1895 | CHECK_SHOULD_DRAW(draw); |
| 1896 | |
tomhudson@google.com | dd5f744 | 2011-08-30 15:13:55 +0000 | [diff] [blame] | 1897 | if (draw.fMatrix->hasPerspective()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1898 | // this guy will just call our drawPath() |
| 1899 | draw.drawPosText((const char*)text, byteLength, pos, constY, |
| 1900 | scalarsPerPos, paint); |
| 1901 | } else { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1902 | SkDraw myDraw(draw); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1903 | |
| 1904 | GrPaint grPaint; |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 1905 | SkAutoCachedTexture textures[GrPaint::kMaxTextures]; |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1906 | if (!skPaint2GrPaintShader(this, |
| 1907 | paint, |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1908 | true, |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 1909 | textures, |
bsalomon@google.com | 84405e0 | 2012-03-05 19:57:21 +0000 | [diff] [blame] | 1910 | &grPaint)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1911 | return; |
| 1912 | } |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 1913 | GrTextContext context(fContext, grPaint, draw.fExtMatrix); |
| 1914 | myDraw.fProcs = this->initDrawForText(&context); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1915 | this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY, |
| 1916 | scalarsPerPos, paint); |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text, |
| 1921 | size_t len, const SkPath& path, |
| 1922 | const SkMatrix* m, const SkPaint& paint) { |
| 1923 | CHECK_SHOULD_DRAW(draw); |
| 1924 | |
| 1925 | SkASSERT(draw.fDevice == this); |
| 1926 | draw.drawTextOnPath((const char*)text, len, path, m, paint); |
| 1927 | } |
| 1928 | |
| 1929 | /////////////////////////////////////////////////////////////////////////////// |
| 1930 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1931 | bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) { |
| 1932 | if (!paint.isLCDRenderText()) { |
| 1933 | // we're cool with the paint as is |
| 1934 | return false; |
| 1935 | } |
| 1936 | |
| 1937 | if (paint.getShader() || |
| 1938 | paint.getXfermode() || // unless its srcover |
| 1939 | paint.getMaskFilter() || |
| 1940 | paint.getRasterizer() || |
| 1941 | paint.getColorFilter() || |
| 1942 | paint.getPathEffect() || |
| 1943 | paint.isFakeBoldText() || |
| 1944 | paint.getStyle() != SkPaint::kFill_Style) { |
| 1945 | // turn off lcd |
| 1946 | flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag; |
| 1947 | flags->fHinting = paint.getHinting(); |
| 1948 | return true; |
| 1949 | } |
| 1950 | // we're cool with the paint as is |
| 1951 | return false; |
| 1952 | } |
| 1953 | |
reed@google.com | 75d939b | 2011-12-07 15:07:23 +0000 | [diff] [blame] | 1954 | void SkGpuDevice::flush() { |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 1955 | DO_DEFERRED_CLEAR; |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 1956 | fContext->resolveRenderTarget(fRenderTarget); |
reed@google.com | 75d939b | 2011-12-07 15:07:23 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1959 | /////////////////////////////////////////////////////////////////////////////// |
| 1960 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1961 | bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap, |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 1962 | const GrTextureParams& params) const { |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 1963 | uint64_t key = bitmap.getGenerationID(); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1964 | key |= ((uint64_t) bitmap.pixelRefOffset()) << 32; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1965 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 1966 | GrTextureDesc desc; |
| 1967 | desc.fWidth = bitmap.width(); |
| 1968 | desc.fHeight = bitmap.height(); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 1969 | desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 1970 | |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 1971 | GrCacheData cacheData(key); |
| 1972 | |
| 1973 | return this->context()->isTextureInCache(desc, cacheData, ¶ms); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1977 | SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config, |
| 1978 | int width, int height, |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1979 | bool isOpaque, |
| 1980 | Usage usage) { |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 1981 | GrTextureDesc desc; |
| 1982 | desc.fConfig = fRenderTarget->config(); |
| 1983 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 1984 | desc.fWidth = width; |
| 1985 | desc.fHeight = height; |
| 1986 | desc.fSampleCnt = fRenderTarget->numSamples(); |
| 1987 | |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 1988 | GrTexture* texture; |
| 1989 | SkAutoTUnref<GrTexture> tunref; |
bsalomon@google.com | 1b3ac8b | 2012-04-09 21:40:54 +0000 | [diff] [blame] | 1990 | // Skia's convention is to only clear a device if it is non-opaque. |
| 1991 | bool needClear = !isOpaque; |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 1992 | |
| 1993 | #if CACHE_COMPATIBLE_DEVICE_TEXTURES |
| 1994 | // layers are never draw in repeat modes, so we can request an approx |
| 1995 | // match and ignore any padding. |
| 1996 | GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ? |
| 1997 | GrContext::kApprox_ScratchTexMatch : |
| 1998 | GrContext::kExact_ScratchTexMatch; |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 1999 | texture = fContext->lockScratchTexture(desc, matchType); |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 2000 | #else |
| 2001 | tunref.reset(fContext->createUncachedTexture(desc, NULL, 0)); |
| 2002 | texture = tunref.get(); |
| 2003 | #endif |
| 2004 | if (texture) { |
| 2005 | return SkNEW_ARGS(SkGpuDevice,(fContext, |
| 2006 | texture, |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 2007 | needClear)); |
| 2008 | } else { |
| 2009 | GrPrintf("---- failed to create compatible device texture [%d %d]\n", |
| 2010 | width, height); |
| 2011 | return NULL; |
| 2012 | } |
| 2013 | } |
| 2014 | |
| 2015 | SkGpuDevice::SkGpuDevice(GrContext* context, |
| 2016 | GrTexture* texture, |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 2017 | bool needClear) |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 2018 | : SkDevice(make_bitmap(context, texture->asRenderTarget())) { |
| 2019 | |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 2020 | GrAssert(texture && texture->asRenderTarget()); |
bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 2021 | // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture |
| 2022 | // cache. We pass true for the third argument so that it will get unlocked. |
| 2023 | this->initFromRenderTarget(context, texture->asRenderTarget(), true); |
bsalomon@google.com | 06cd732 | 2012-03-30 18:45:35 +0000 | [diff] [blame] | 2024 | fNeedClear = needClear; |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 2025 | } |