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