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