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