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 "SkCanvas.h" | 8 #include "SkCanvas.h" |
9 #include "SkCanvasPriv.h" | 9 #include "SkCanvasPriv.h" |
10 #include "SkBitmapDevice.h" | 10 #include "SkBitmapDevice.h" |
| 11 #include "SkColorFilter.h" |
11 #include "SkDeviceImageFilterProxy.h" | 12 #include "SkDeviceImageFilterProxy.h" |
12 #include "SkDraw.h" | 13 #include "SkDraw.h" |
13 #include "SkDrawable.h" | 14 #include "SkDrawable.h" |
14 #include "SkDrawFilter.h" | 15 #include "SkDrawFilter.h" |
15 #include "SkDrawLooper.h" | 16 #include "SkDrawLooper.h" |
16 #include "SkErrorInternals.h" | 17 #include "SkErrorInternals.h" |
17 #include "SkImage.h" | 18 #include "SkImage.h" |
18 #include "SkMetaData.h" | 19 #include "SkMetaData.h" |
19 #include "SkPathOps.h" | 20 #include "SkPathOps.h" |
20 #include "SkPatchUtils.h" | 21 #include "SkPatchUtils.h" |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 SkCanvas* fCanvas; | 277 SkCanvas* fCanvas; |
277 const DeviceCM* fCurrLayer; | 278 const DeviceCM* fCurrLayer; |
278 const SkPaint* fPaint; // May be null. | 279 const SkPaint* fPaint; // May be null. |
279 SkBool8 fSkipEmptyClips; | 280 SkBool8 fSkipEmptyClips; |
280 | 281 |
281 typedef SkDraw INHERITED; | 282 typedef SkDraw INHERITED; |
282 }; | 283 }; |
283 | 284 |
284 ///////////////////////////////////////////////////////////////////////////// | 285 ///////////////////////////////////////////////////////////////////////////// |
285 | 286 |
| 287 static SkPaint* set_if_needed(SkLazyPaint* lazy, const SkPaint& orig) { |
| 288 return lazy->isValid() ? lazy->get() : lazy->set(orig); |
| 289 } |
| 290 |
| 291 /** |
| 292 * If the paint has an imagefilter, but it can be simplified to just a colorfil
ter, return that |
| 293 * colorfilter, else return NULL. |
| 294 */ |
| 295 static SkColorFilter* image_to_color_filter(const SkPaint& paint) { |
| 296 SkImageFilter* imgf = paint.getImageFilter(); |
| 297 if (!imgf) { |
| 298 return NULL; |
| 299 } |
| 300 |
| 301 SkColorFilter* imgCF; |
| 302 if (!imgf->asAColorFilter(&imgCF)) { |
| 303 return NULL; |
| 304 } |
| 305 |
| 306 SkColorFilter* paintCF = paint.getColorFilter(); |
| 307 if (NULL == paintCF) { |
| 308 // there is no existing paint colorfilter, so we can just return the ima
gefilter's |
| 309 return imgCF; |
| 310 } |
| 311 |
| 312 // The paint has both a colorfilter and an imagefilter. |
| 313 SkAutoTUnref<SkColorFilter> autoImgCF(imgCF); |
| 314 return SkColorFilter::CreateComposeFilter(imgCF, paintCF); |
| 315 } |
| 316 |
286 class AutoDrawLooper { | 317 class AutoDrawLooper { |
287 public: | 318 public: |
288 AutoDrawLooper(SkCanvas* canvas, const SkSurfaceProps& props, const SkPaint&
paint, | 319 AutoDrawLooper(SkCanvas* canvas, const SkSurfaceProps& props, const SkPaint&
paint, |
289 bool skipLayerForImageFilter = false, | 320 bool skipLayerForImageFilter = false, |
290 const SkRect* bounds = NULL) : fOrigPaint(paint) { | 321 const SkRect* bounds = NULL) : fOrigPaint(paint) { |
291 fCanvas = canvas; | 322 fCanvas = canvas; |
292 fFilter = canvas->getDrawFilter(); | 323 fFilter = canvas->getDrawFilter(); |
293 fPaint = &fOrigPaint; | 324 fPaint = &fOrigPaint; |
294 fSaveCount = canvas->getSaveCount(); | 325 fSaveCount = canvas->getSaveCount(); |
295 fDoClearImageFilter = false; | 326 fDoClearImageFilter = false; |
296 fDone = false; | 327 fDone = false; |
297 | 328 |
298 if (!skipLayerForImageFilter && fOrigPaint.getImageFilter()) { | 329 SkColorFilter* simplifiedCF = image_to_color_filter(fOrigPaint); |
| 330 if (simplifiedCF) { |
| 331 SkPaint* paint = set_if_needed(&fLazyPaintInit, fOrigPaint); |
| 332 paint->setColorFilter(simplifiedCF)->unref(); |
| 333 paint->setImageFilter(NULL); |
| 334 fPaint = paint; |
| 335 } |
| 336 |
| 337 if (!skipLayerForImageFilter && fPaint->getImageFilter()) { |
299 SkPaint tmp; | 338 SkPaint tmp; |
300 tmp.setImageFilter(fOrigPaint.getImageFilter()); | 339 tmp.setImageFilter(fPaint->getImageFilter()); |
301 (void)canvas->internalSaveLayer(bounds, &tmp, SkCanvas::kARGB_ClipLa
yer_SaveFlag, | 340 (void)canvas->internalSaveLayer(bounds, &tmp, SkCanvas::kARGB_ClipLa
yer_SaveFlag, |
302 true, SkCanvas::kFullLayer_SaveLayer
Strategy); | 341 true, SkCanvas::kFullLayer_SaveLayer
Strategy); |
303 // we'll clear the imageFilter for the actual draws in next(), so | 342 // we'll clear the imageFilter for the actual draws in next(), so |
304 // it will only be applied during the restore(). | 343 // it will only be applied during the restore(). |
305 fDoClearImageFilter = true; | 344 fDoClearImageFilter = true; |
306 } | 345 } |
307 | 346 |
308 if (SkDrawLooper* looper = paint.getLooper()) { | 347 if (SkDrawLooper* looper = paint.getLooper()) { |
309 void* buffer = fLooperContextAllocator.reserveT<SkDrawLooper::Contex
t>( | 348 void* buffer = fLooperContextAllocator.reserveT<SkDrawLooper::Contex
t>( |
310 looper->contextSize()); | 349 looper->contextSize()); |
311 fLooperContext = looper->createContext(canvas, buffer); | 350 fLooperContext = looper->createContext(canvas, buffer); |
312 fIsSimple = false; | 351 fIsSimple = false; |
313 } else { | 352 } else { |
314 fLooperContext = NULL; | 353 fLooperContext = NULL; |
315 // can we be marked as simple? | 354 // can we be marked as simple? |
316 fIsSimple = !fFilter && !fDoClearImageFilter; | 355 fIsSimple = !fFilter && !fDoClearImageFilter; |
317 } | 356 } |
318 | 357 |
319 uint32_t oldFlags = paint.getFlags(); | 358 uint32_t oldFlags = paint.getFlags(); |
320 fNewPaintFlags = filter_paint_flags(props, oldFlags); | 359 fNewPaintFlags = filter_paint_flags(props, oldFlags); |
321 if (fIsSimple && (fNewPaintFlags != oldFlags)) { | 360 if (fIsSimple && (fNewPaintFlags != oldFlags)) { |
322 SkPaint* paint = fLazyPaint.set(fOrigPaint); | 361 SkPaint* paint = set_if_needed(&fLazyPaintInit, fOrigPaint); |
323 paint->setFlags(fNewPaintFlags); | 362 paint->setFlags(fNewPaintFlags); |
324 fPaint = paint; | 363 fPaint = paint; |
325 // if we're not simple, doNext() will take care of calling setFlags(
) | 364 // if we're not simple, doNext() will take care of calling setFlags(
) |
326 } | 365 } |
327 } | 366 } |
328 | 367 |
329 ~AutoDrawLooper() { | 368 ~AutoDrawLooper() { |
330 if (fDoClearImageFilter) { | 369 if (fDoClearImageFilter) { |
331 fCanvas->internalRestore(); | 370 fCanvas->internalRestore(); |
332 } | 371 } |
(...skipping 10 matching lines...) Expand all Loading... |
343 return false; | 382 return false; |
344 } else if (fIsSimple) { | 383 } else if (fIsSimple) { |
345 fDone = true; | 384 fDone = true; |
346 return !fPaint->nothingToDraw(); | 385 return !fPaint->nothingToDraw(); |
347 } else { | 386 } else { |
348 return this->doNext(drawType); | 387 return this->doNext(drawType); |
349 } | 388 } |
350 } | 389 } |
351 | 390 |
352 private: | 391 private: |
353 SkLazyPaint fLazyPaint; | 392 SkLazyPaint fLazyPaintInit, fLazyPaintPerNext; |
354 SkCanvas* fCanvas; | 393 SkCanvas* fCanvas; |
355 const SkPaint& fOrigPaint; | 394 const SkPaint& fOrigPaint; |
356 SkDrawFilter* fFilter; | 395 SkDrawFilter* fFilter; |
357 const SkPaint* fPaint; | 396 const SkPaint* fPaint; |
358 int fSaveCount; | 397 int fSaveCount; |
359 uint32_t fNewPaintFlags; | 398 uint32_t fNewPaintFlags; |
360 bool fDoClearImageFilter; | 399 bool fDoClearImageFilter; |
361 bool fDone; | 400 bool fDone; |
362 bool fIsSimple; | 401 bool fIsSimple; |
363 SkDrawLooper::Context* fLooperContext; | 402 SkDrawLooper::Context* fLooperContext; |
364 SkSmallAllocator<1, 32> fLooperContextAllocator; | 403 SkSmallAllocator<1, 32> fLooperContextAllocator; |
365 | 404 |
366 bool doNext(SkDrawFilter::Type drawType); | 405 bool doNext(SkDrawFilter::Type drawType); |
367 }; | 406 }; |
368 | 407 |
369 bool AutoDrawLooper::doNext(SkDrawFilter::Type drawType) { | 408 bool AutoDrawLooper::doNext(SkDrawFilter::Type drawType) { |
370 fPaint = NULL; | 409 fPaint = NULL; |
371 SkASSERT(!fIsSimple); | 410 SkASSERT(!fIsSimple); |
372 SkASSERT(fLooperContext || fFilter || fDoClearImageFilter); | 411 SkASSERT(fLooperContext || fFilter || fDoClearImageFilter); |
373 | 412 |
374 SkPaint* paint = fLazyPaint.set(fOrigPaint); | 413 SkPaint* paint = fLazyPaintPerNext.set(fLazyPaintInit.isValid() ? |
| 414 *fLazyPaintInit.get() : fOrigPaint); |
375 paint->setFlags(fNewPaintFlags); | 415 paint->setFlags(fNewPaintFlags); |
376 | 416 |
377 if (fDoClearImageFilter) { | 417 if (fDoClearImageFilter) { |
378 paint->setImageFilter(NULL); | 418 paint->setImageFilter(NULL); |
379 } | 419 } |
380 | 420 |
381 if (fLooperContext && !fLooperContext->next(fCanvas, paint)) { | 421 if (fLooperContext && !fLooperContext->next(fCanvas, paint)) { |
382 fDone = true; | 422 fDone = true; |
383 return false; | 423 return false; |
384 } | 424 } |
(...skipping 2138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2523 } | 2563 } |
2524 | 2564 |
2525 if (matrix) { | 2565 if (matrix) { |
2526 canvas->concat(*matrix); | 2566 canvas->concat(*matrix); |
2527 } | 2567 } |
2528 } | 2568 } |
2529 | 2569 |
2530 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { | 2570 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { |
2531 fCanvas->restoreToCount(fSaveCount); | 2571 fCanvas->restoreToCount(fSaveCount); |
2532 } | 2572 } |
OLD | NEW |