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