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