Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2008 The Android Open Source Project | 2 * Copyright 2008 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkCanvasPriv.h" | 10 #include "SkCanvasPriv.h" |
| 11 #include "SkClipStack.h" | 11 #include "SkClipStack.h" |
| 12 #include "SkColorFilter.h" | 12 #include "SkColorFilter.h" |
| 13 #include "SkDevice.h" | 13 #include "SkDevice.h" |
| 14 #include "SkDraw.h" | 14 #include "SkDraw.h" |
| 15 #include "SkDrawable.h" | 15 #include "SkDrawable.h" |
| 16 #include "SkDrawFilter.h" | 16 #include "SkDrawFilter.h" |
| 17 #include "SkDrawLooper.h" | 17 #include "SkDrawLooper.h" |
| 18 #include "SkErrorInternals.h" | 18 #include "SkErrorInternals.h" |
| 19 #include "SkImage.h" | 19 #include "SkImage.h" |
| 20 #include "SkImage_Base.h" | |
| 20 #include "SkMetaData.h" | 21 #include "SkMetaData.h" |
| 21 #include "SkNinePatchIter.h" | 22 #include "SkNinePatchIter.h" |
| 22 #include "SkPaintPriv.h" | 23 #include "SkPaintPriv.h" |
| 23 #include "SkPatchUtils.h" | 24 #include "SkPatchUtils.h" |
| 24 #include "SkPicture.h" | 25 #include "SkPicture.h" |
| 25 #include "SkRasterClip.h" | 26 #include "SkRasterClip.h" |
| 26 #include "SkReadPixelsRec.h" | 27 #include "SkReadPixelsRec.h" |
| 27 #include "SkRRect.h" | 28 #include "SkRRect.h" |
| 28 #include "SkSmallAllocator.h" | 29 #include "SkSmallAllocator.h" |
| 29 #include "SkSurface_Base.h" | 30 #include "SkSurface_Base.h" |
| (...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 585 // call this after any possible paint modifiers | 586 // call this after any possible paint modifiers |
| 586 if (fPaint->nothingToDraw()) { | 587 if (fPaint->nothingToDraw()) { |
| 587 fPaint = nullptr; | 588 fPaint = nullptr; |
| 588 return false; | 589 return false; |
| 589 } | 590 } |
| 590 return true; | 591 return true; |
| 591 } | 592 } |
| 592 | 593 |
| 593 ////////// macros to place around the internal draw calls ////////////////// | 594 ////////// macros to place around the internal draw calls ////////////////// |
| 594 | 595 |
| 596 #define LOOPER_BEGIN_DRAWBITMAP(paint, skipLayerForFilter, bounds) \ | |
| 597 this->predrawNotify(); \ | |
| 598 AutoDrawLooper looper(this, fProps, paint, skipLayerForFilter, bounds); \ | |
| 599 while (looper.next(SkDrawFilter::kBitmap_Type)) { \ | |
| 600 SkDrawIter iter(this); | |
| 601 | |
| 602 | |
| 595 #define LOOPER_BEGIN_DRAWDEVICE(paint, type) \ | 603 #define LOOPER_BEGIN_DRAWDEVICE(paint, type) \ |
| 596 this->predrawNotify(); \ | 604 this->predrawNotify(); \ |
| 597 AutoDrawLooper looper(this, fProps, paint, true); \ | 605 AutoDrawLooper looper(this, fProps, paint, true); \ |
| 598 while (looper.next(type)) { \ | 606 while (looper.next(type)) { \ |
| 599 SkDrawIter iter(this); | 607 SkDrawIter iter(this); |
| 600 | 608 |
| 601 #define LOOPER_BEGIN(paint, type, bounds) \ | 609 #define LOOPER_BEGIN(paint, type, bounds) \ |
| 602 this->predrawNotify(); \ | 610 this->predrawNotify(); \ |
| 603 AutoDrawLooper looper(this, fProps, paint, false, bounds); \ | 611 AutoDrawLooper looper(this, fProps, paint, false, bounds); \ |
| 604 while (looper.next(type)) { \ | 612 while (looper.next(type)) { \ |
| (...skipping 792 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1397 SkDEBUGCODE(bitmap.validate();) | 1405 SkDEBUGCODE(bitmap.validate();) |
| 1398 | 1406 |
| 1399 SkPaint tmp; | 1407 SkPaint tmp; |
| 1400 if (nullptr == paint) { | 1408 if (nullptr == paint) { |
| 1401 paint = &tmp; | 1409 paint = &tmp; |
| 1402 } | 1410 } |
| 1403 | 1411 |
| 1404 LOOPER_BEGIN_DRAWDEVICE(*paint, SkDrawFilter::kBitmap_Type) | 1412 LOOPER_BEGIN_DRAWDEVICE(*paint, SkDrawFilter::kBitmap_Type) |
| 1405 | 1413 |
| 1406 while (iter.next()) { | 1414 while (iter.next()) { |
| 1407 paint = &looper.paint(); | 1415 const SkIPoint pos = { x - iter.getX(), y - iter.getY() }; |
| 1408 SkImageFilter* filter = paint->getImageFilter(); | 1416 iter.fDevice->drawBitmapAsSprite(iter, bitmap, pos.x(), pos.y(), looper. paint()); |
| 1409 SkIPoint pos = { x - iter.getX(), y - iter.getY() }; | |
| 1410 if (filter && !iter.fDevice->canHandleImageFilter(filter)) { | |
| 1411 SkImageFilter::DeviceProxy proxy(iter.fDevice); | |
| 1412 SkBitmap dst; | |
| 1413 SkIPoint offset = SkIPoint::Make(0, 0); | |
| 1414 SkMatrix matrix = *iter.fMatrix; | |
| 1415 matrix.postTranslate(SkIntToScalar(-pos.x()), SkIntToScalar(-pos.y() )); | |
| 1416 const SkIRect clipBounds = bitmap.bounds(); | |
| 1417 SkAutoTUnref<SkImageFilter::Cache> cache(iter.fDevice->getImageFilte rCache()); | |
| 1418 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), | |
| 1419 SkImageFilter::kApprox_SizeConstraint); | |
| 1420 if (filter->filterImage(&proxy, bitmap, ctx, &dst, &offset)) { | |
| 1421 SkPaint tmpUnfiltered(*paint); | |
| 1422 tmpUnfiltered.setImageFilter(nullptr); | |
| 1423 iter.fDevice->drawSprite(iter, dst, pos.x() + offset.x(), pos.y( ) + offset.y(), | |
| 1424 tmpUnfiltered); | |
| 1425 } | |
| 1426 } else { | |
| 1427 iter.fDevice->drawSprite(iter, bitmap, pos.x(), pos.y(), *paint); | |
| 1428 } | |
| 1429 } | 1417 } |
| 1430 LOOPER_END | 1418 LOOPER_END |
| 1431 } | 1419 } |
| 1432 | 1420 |
| 1433 ///////////////////////////////////////////////////////////////////////////// | 1421 ///////////////////////////////////////////////////////////////////////////// |
| 1434 void SkCanvas::translate(SkScalar dx, SkScalar dy) { | 1422 void SkCanvas::translate(SkScalar dx, SkScalar dy) { |
| 1435 SkMatrix m; | 1423 SkMatrix m; |
| 1436 m.setTranslate(dx, dy); | 1424 m.setTranslate(dx, dy); |
| 1437 this->concat(m); | 1425 this->concat(m); |
| 1438 } | 1426 } |
| (...skipping 751 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2190 | 2178 |
| 2191 LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, bounds) | 2179 LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, bounds) |
| 2192 | 2180 |
| 2193 while (iter.next()) { | 2181 while (iter.next()) { |
| 2194 iter.fDevice->drawPath(iter, path, looper.paint()); | 2182 iter.fDevice->drawPath(iter, path, looper.paint()); |
| 2195 } | 2183 } |
| 2196 | 2184 |
| 2197 LOOPER_END | 2185 LOOPER_END |
| 2198 } | 2186 } |
| 2199 | 2187 |
| 2188 #include "SkMatrixUtils.h" | |
|
Stephen White
2015/12/04 21:41:08
Nit: move this #include to the top-of-file?
reed1
2015/12/04 21:52:11
Good catch.
| |
| 2189 | |
| 2190 bool SkCanvas::canDrawBitmapAsSprite(SkScalar x, SkScalar y, int w, int h, const SkPaint& paint) { | |
| 2191 #ifdef SK_SUPPORT_LEGACY_LAYER_BITMAP_IMAGEFILTERS | |
| 2192 return false; | |
| 2193 #endif | |
| 2194 | |
| 2195 if (!paint.getImageFilter()) { | |
| 2196 return false; | |
| 2197 } | |
| 2198 | |
| 2199 const SkMatrix& ctm = this->getTotalMatrix(); | |
| 2200 const unsigned kSubpixelBits = 0; // matching SkDraw::drawBitmap() | |
| 2201 if (!SkTreatAsSprite(ctm, w, h, kSubpixelBits)) { | |
| 2202 return false; | |
| 2203 } | |
| 2204 | |
| 2205 // Currently we can only use the filterSprite code if we are clipped to the bitmap's bounds. | |
| 2206 // Once we can filter and the filter will return a result larger than itself , we should be | |
| 2207 // able to remove this constraint. | |
| 2208 // skbug.com/4526 | |
| 2209 // | |
| 2210 SkPoint pt; | |
| 2211 ctm.mapXY(x, y, &pt); | |
| 2212 SkIRect ir = SkIRect::MakeXYWH(SkScalarRoundToInt(pt.x()), SkScalarRoundToIn t(pt.y()), w, h); | |
| 2213 return ir.contains(fMCRec->fRasterClip.getBounds()); | |
| 2214 } | |
| 2215 | |
| 2200 void SkCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const S kPaint* paint) { | 2216 void SkCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const S kPaint* paint) { |
| 2201 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawImage()"); | 2217 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawImage()"); |
| 2202 SkRect bounds = SkRect::MakeXYWH(x, y, | 2218 SkRect bounds = SkRect::MakeXYWH(x, y, |
| 2203 SkIntToScalar(image->width()), SkIntToScala r(image->height())); | 2219 SkIntToScalar(image->width()), SkIntToScala r(image->height())); |
| 2204 if (nullptr == paint || paint->canComputeFastBounds()) { | 2220 if (nullptr == paint || paint->canComputeFastBounds()) { |
| 2205 SkRect tmp = bounds; | 2221 SkRect tmp = bounds; |
| 2206 if (paint) { | 2222 if (paint) { |
| 2207 paint->computeFastBounds(tmp, &tmp); | 2223 paint->computeFastBounds(tmp, &tmp); |
| 2208 } | 2224 } |
| 2209 if (this->quickReject(tmp)) { | 2225 if (this->quickReject(tmp)) { |
| 2210 return; | 2226 return; |
| 2211 } | 2227 } |
| 2212 } | 2228 } |
| 2213 | 2229 |
| 2214 SkLazyPaint lazy; | 2230 SkLazyPaint lazy; |
| 2215 if (nullptr == paint) { | 2231 if (nullptr == paint) { |
| 2216 paint = lazy.init(); | 2232 paint = lazy.init(); |
| 2217 } | 2233 } |
| 2218 | 2234 |
| 2219 LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, &bounds) | 2235 const bool drawAsSprite = this->canDrawBitmapAsSprite(x, y, image->width(), image->height(), |
| 2220 | 2236 *paint); |
| 2237 LOOPER_BEGIN_DRAWBITMAP(*paint, drawAsSprite, &bounds) | |
| 2238 | |
| 2221 while (iter.next()) { | 2239 while (iter.next()) { |
| 2222 iter.fDevice->drawImage(iter, image, x, y, looper.paint()); | 2240 const SkPaint& pnt = looper.paint(); |
| 2241 if (drawAsSprite && pnt.getImageFilter()) { | |
| 2242 SkBitmap bitmap; | |
| 2243 if (as_IB(image)->asBitmapForImageFilters(&bitmap)) { | |
| 2244 SkPoint pt; | |
| 2245 iter.fMatrix->mapXY(x, y, &pt); | |
| 2246 iter.fDevice->drawBitmapAsSprite(iter, bitmap, | |
| 2247 SkScalarRoundToInt(pt.fX), | |
| 2248 SkScalarRoundToInt(pt.fY), pnt) ; | |
| 2249 } | |
| 2250 } else { | |
| 2251 iter.fDevice->drawImage(iter, image, x, y, pnt); | |
| 2252 } | |
| 2223 } | 2253 } |
| 2224 | 2254 |
| 2225 LOOPER_END | 2255 LOOPER_END |
| 2226 } | 2256 } |
| 2227 | 2257 |
| 2228 void SkCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const Sk Rect& dst, | 2258 void SkCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const Sk Rect& dst, |
| 2229 const SkPaint* paint, SrcRectConstraint constrain t) { | 2259 const SkPaint* paint, SrcRectConstraint constrain t) { |
| 2230 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawImageRect()"); | 2260 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawImageRect()"); |
| 2231 SkRect storage; | 2261 SkRect storage; |
| 2232 const SkRect* bounds = &dst; | 2262 const SkRect* bounds = &dst; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2274 if (paint->canComputeFastBounds()) { | 2304 if (paint->canComputeFastBounds()) { |
| 2275 bitmap.getBounds(&storage); | 2305 bitmap.getBounds(&storage); |
| 2276 matrix.mapRect(&storage); | 2306 matrix.mapRect(&storage); |
| 2277 SkRect tmp = storage; | 2307 SkRect tmp = storage; |
| 2278 if (this->quickReject(paint->computeFastBounds(tmp, &tmp))) { | 2308 if (this->quickReject(paint->computeFastBounds(tmp, &tmp))) { |
| 2279 return; | 2309 return; |
| 2280 } | 2310 } |
| 2281 bounds = &storage; | 2311 bounds = &storage; |
| 2282 } | 2312 } |
| 2283 | 2313 |
| 2284 LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, bounds) | 2314 const bool drawAsSprite = bounds && this->canDrawBitmapAsSprite(x, y, bitmap .width(), |
| 2315 bitmap.heigh t(), *paint); | |
| 2316 LOOPER_BEGIN_DRAWBITMAP(*paint, drawAsSprite, bounds) | |
| 2285 | 2317 |
| 2286 while (iter.next()) { | 2318 while (iter.next()) { |
| 2287 iter.fDevice->drawBitmap(iter, bitmap, matrix, looper.paint()); | 2319 const SkPaint& pnt = looper.paint(); |
| 2320 if (drawAsSprite && pnt.getImageFilter()) { | |
| 2321 SkPoint pt; | |
| 2322 iter.fMatrix->mapXY(x, y, &pt); | |
| 2323 iter.fDevice->drawBitmapAsSprite(iter, bitmap, | |
| 2324 SkScalarRoundToInt(pt.fX), | |
| 2325 SkScalarRoundToInt(pt.fY), pnt); | |
| 2326 } else { | |
| 2327 iter.fDevice->drawBitmap(iter, bitmap, matrix, looper.paint()); | |
| 2328 } | |
| 2288 } | 2329 } |
| 2289 | 2330 |
| 2290 LOOPER_END | 2331 LOOPER_END |
| 2291 } | 2332 } |
| 2292 | 2333 |
| 2293 // this one is non-virtual, so it can be called safely by other canvas apis | 2334 // this one is non-virtual, so it can be called safely by other canvas apis |
| 2294 void SkCanvas::internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, | 2335 void SkCanvas::internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, |
| 2295 const SkRect& dst, const SkPaint* paint, | 2336 const SkRect& dst, const SkPaint* paint, |
| 2296 SrcRectConstraint constraint) { | 2337 SrcRectConstraint constraint) { |
| 2297 if (bitmap.drawsNothing() || dst.isEmpty()) { | 2338 if (bitmap.drawsNothing() || dst.isEmpty()) { |
| 2298 return; | 2339 return; |
| 2299 } | 2340 } |
| (...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2963 } | 3004 } |
| 2964 | 3005 |
| 2965 if (matrix) { | 3006 if (matrix) { |
| 2966 canvas->concat(*matrix); | 3007 canvas->concat(*matrix); |
| 2967 } | 3008 } |
| 2968 } | 3009 } |
| 2969 | 3010 |
| 2970 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { | 3011 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { |
| 2971 fCanvas->restoreToCount(fSaveCount); | 3012 fCanvas->restoreToCount(fSaveCount); |
| 2972 } | 3013 } |
| OLD | NEW |