OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "skia/ext/pixel_ref_utils.h" | 5 #include "skia/ext/pixel_ref_utils.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "third_party/skia/include/core/SkBitmapDevice.h" | 9 #include "third_party/skia/include/core/SkBitmapDevice.h" |
10 #include "third_party/skia/include/core/SkCanvas.h" | 10 #include "third_party/skia/include/core/SkCanvas.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 | 93 |
94 GatherPixelRefDevice::drawRect(draw, bounds, paint); | 94 GatherPixelRefDevice::drawRect(draw, bounds, paint); |
95 } | 95 } |
96 void drawRect(const SkDraw& draw, | 96 void drawRect(const SkDraw& draw, |
97 const SkRect& rect, | 97 const SkRect& rect, |
98 const SkPaint& paint) override { | 98 const SkPaint& paint) override { |
99 SkBitmap bitmap; | 99 SkBitmap bitmap; |
100 if (GetBitmapFromPaint(paint, &bitmap)) { | 100 if (GetBitmapFromPaint(paint, &bitmap)) { |
101 SkRect mapped_rect; | 101 SkRect mapped_rect; |
102 draw.fMatrix->mapRect(&mapped_rect, rect); | 102 draw.fMatrix->mapRect(&mapped_rect, rect); |
103 if (mapped_rect.intersect(SkRect::Make(draw.fRC->getBounds()))) { | 103 if (mapped_rect.intersects(SkRect::Make(draw.fRC->getBounds()))) { |
104 AddBitmap(bitmap, mapped_rect, *draw.fMatrix, paint.getFilterQuality()); | 104 AddBitmap(bitmap, mapped_rect, *draw.fMatrix, paint.getFilterQuality()); |
105 } | 105 } |
106 } | 106 } |
107 } | 107 } |
108 void drawOval(const SkDraw& draw, | 108 void drawOval(const SkDraw& draw, |
109 const SkRect& rect, | 109 const SkRect& rect, |
110 const SkPaint& paint) override { | 110 const SkPaint& paint) override { |
111 GatherPixelRefDevice::drawRect(draw, rect, paint); | 111 GatherPixelRefDevice::drawRect(draw, rect, paint); |
112 } | 112 } |
113 void drawRRect(const SkDraw& draw, | 113 void drawRRect(const SkDraw& draw, |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 } | 348 } |
349 | 349 |
350 private: | 350 private: |
351 DiscardablePixelRefSet* pixel_ref_set_; | 351 DiscardablePixelRefSet* pixel_ref_set_; |
352 | 352 |
353 void AddBitmap(const SkBitmap& bm, | 353 void AddBitmap(const SkBitmap& bm, |
354 const SkRect& rect, | 354 const SkRect& rect, |
355 const SkMatrix& matrix, | 355 const SkMatrix& matrix, |
356 SkFilterQuality filter_quality) { | 356 SkFilterQuality filter_quality) { |
357 SkRect canvas_rect = SkRect::MakeWH(width(), height()); | 357 SkRect canvas_rect = SkRect::MakeWH(width(), height()); |
358 SkRect paint_rect = SkRect::MakeEmpty(); | 358 if (rect.intersects(canvas_rect)) { |
359 if (paint_rect.intersect(rect, canvas_rect)) { | 359 pixel_ref_set_->Add(bm.pixelRef(), rect, matrix, filter_quality); |
360 pixel_ref_set_->Add(bm.pixelRef(), paint_rect, matrix, | |
361 filter_quality); | |
362 } | 360 } |
363 } | 361 } |
364 | 362 |
365 bool GetBitmapFromPaint(const SkPaint& paint, SkBitmap* bm) { | 363 bool GetBitmapFromPaint(const SkPaint& paint, SkBitmap* bm) { |
366 SkShader* shader = paint.getShader(); | 364 SkShader* shader = paint.getShader(); |
367 if (shader) { | 365 if (shader) { |
368 // Check whether the shader is a gradient in order to prevent generation | 366 // Check whether the shader is a gradient in order to prevent generation |
369 // of bitmaps from gradient shaders, which implement asABitmap. | 367 // of bitmaps from gradient shaders, which implement asABitmap. |
370 if (SkShader::kNone_GradientType == shader->asAGradient(NULL)) | 368 if (SkShader::kNone_GradientType == shader->asAGradient(NULL)) |
371 return shader->asABitmap(bm, NULL, NULL); | 369 return shader->asABitmap(bm, NULL, NULL); |
372 } | 370 } |
373 return false; | 371 return false; |
374 } | 372 } |
375 }; | 373 }; |
376 | 374 |
377 } // namespace | 375 } // namespace |
378 | 376 |
379 void PixelRefUtils::GatherDiscardablePixelRefs( | 377 void PixelRefUtils::GatherDiscardablePixelRefs( |
380 SkPicture* picture, | 378 SkPicture* picture, |
381 std::vector<PositionPixelRef>* pixel_refs) { | 379 std::vector<PositionPixelRef>* pixel_refs) { |
382 pixel_refs->clear(); | 380 pixel_refs->clear(); |
383 DiscardablePixelRefSet pixel_ref_set(pixel_refs); | 381 DiscardablePixelRefSet pixel_ref_set(pixel_refs); |
384 | 382 |
385 SkRect picture_bounds = picture->cullRect(); | 383 SkRect picture_bounds = picture->cullRect(); |
386 SkIRect picture_ibounds = picture_bounds.roundOut(); | 384 SkIRect picture_ibounds = picture_bounds.roundOut(); |
387 SkBitmap empty_bitmap; | 385 SkBitmap empty_bitmap; |
388 empty_bitmap.setInfo(SkImageInfo::MakeUnknown(picture_ibounds.width(), | 386 // Use right/bottom as the size so that we don't need a translate and, as a |
389 picture_ibounds.height())); | 387 // result, the information is returned relative to the picture's origin. |
| 388 empty_bitmap.setInfo(SkImageInfo::MakeUnknown(picture_ibounds.right(), |
| 389 picture_ibounds.bottom())); |
390 | 390 |
391 GatherPixelRefDevice device(empty_bitmap, &pixel_ref_set); | 391 GatherPixelRefDevice device(empty_bitmap, &pixel_ref_set); |
392 SkNoSaveLayerCanvas canvas(&device); | 392 SkNoSaveLayerCanvas canvas(&device); |
393 | 393 |
394 // Draw the picture pinned against our top/left corner. | |
395 canvas.translate(-picture_bounds.left(), -picture_bounds.top()); | |
396 canvas.drawPicture(picture); | 394 canvas.drawPicture(picture); |
397 } | 395 } |
398 | 396 |
399 } // namespace skia | 397 } // namespace skia |
OLD | NEW |