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