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