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