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