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