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