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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 | 92 |
93 GatherPixelRefDevice::drawRect(draw, bounds, paint); | 93 GatherPixelRefDevice::drawRect(draw, bounds, paint); |
94 } | 94 } |
95 void drawRect(const SkDraw& draw, | 95 void drawRect(const SkDraw& draw, |
96 const SkRect& rect, | 96 const SkRect& rect, |
97 const SkPaint& paint) override { | 97 const SkPaint& paint) override { |
98 SkBitmap bitmap; | 98 SkBitmap bitmap; |
99 if (GetBitmapFromPaint(paint, &bitmap)) { | 99 if (GetBitmapFromPaint(paint, &bitmap)) { |
100 SkRect mapped_rect; | 100 SkRect mapped_rect; |
101 draw.fMatrix->mapRect(&mapped_rect, rect); | 101 draw.fMatrix->mapRect(&mapped_rect, rect); |
102 if (mapped_rect.intersect(SkRect::Make(draw.fRC->getBounds()))) { | 102 if (mapped_rect.intersects(SkRect::Make(draw.fRC->getBounds()))) { |
103 AddBitmap(bitmap, mapped_rect, *draw.fMatrix, paint.getFilterQuality()); | 103 AddBitmap(bitmap, mapped_rect, *draw.fMatrix, paint.getFilterQuality()); |
104 } | 104 } |
105 } | 105 } |
106 } | 106 } |
107 void drawOval(const SkDraw& draw, | 107 void drawOval(const SkDraw& draw, |
108 const SkRect& rect, | 108 const SkRect& rect, |
109 const SkPaint& paint) override { | 109 const SkPaint& paint) override { |
110 GatherPixelRefDevice::drawRect(draw, rect, paint); | 110 GatherPixelRefDevice::drawRect(draw, rect, paint); |
111 } | 111 } |
112 void drawRRect(const SkDraw& draw, | 112 void drawRRect(const SkDraw& draw, |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
334 } | 334 } |
335 | 335 |
336 private: | 336 private: |
337 DiscardablePixelRefSet* pixel_ref_set_; | 337 DiscardablePixelRefSet* pixel_ref_set_; |
338 | 338 |
339 void AddBitmap(const SkBitmap& bm, | 339 void AddBitmap(const SkBitmap& bm, |
340 const SkRect& rect, | 340 const SkRect& rect, |
341 const SkMatrix& matrix, | 341 const SkMatrix& matrix, |
342 SkFilterQuality filter_quality) { | 342 SkFilterQuality filter_quality) { |
343 SkRect canvas_rect = SkRect::MakeWH(width(), height()); | 343 SkRect canvas_rect = SkRect::MakeWH(width(), height()); |
344 SkRect paint_rect = SkRect::MakeEmpty(); | 344 if (rect.intersects(canvas_rect)) { |
345 if (paint_rect.intersect(rect, canvas_rect)) { | 345 pixel_ref_set_->Add(bm.pixelRef(), rect, matrix, filter_quality); |
346 pixel_ref_set_->Add(bm.pixelRef(), paint_rect, matrix, | |
347 filter_quality); | |
348 } | 346 } |
349 } | 347 } |
350 | 348 |
351 bool GetBitmapFromPaint(const SkPaint& paint, SkBitmap* bm) { | 349 bool GetBitmapFromPaint(const SkPaint& paint, SkBitmap* bm) { |
352 SkShader* shader = paint.getShader(); | 350 SkShader* shader = paint.getShader(); |
353 if (shader) { | 351 if (shader) { |
354 // Check whether the shader is a gradient in order to prevent generation | 352 // Check whether the shader is a gradient in order to prevent generation |
355 // of bitmaps from gradient shaders, which implement asABitmap. | 353 // of bitmaps from gradient shaders, which implement asABitmap. |
356 if (SkShader::kNone_GradientType == shader->asAGradient(NULL)) | 354 if (SkShader::kNone_GradientType == shader->asAGradient(NULL)) |
357 return shader->asABitmap(bm, NULL, NULL); | 355 return shader->asABitmap(bm, NULL, NULL); |
358 } | 356 } |
359 return false; | 357 return false; |
360 } | 358 } |
361 }; | 359 }; |
362 | 360 |
363 } // namespace | 361 } // namespace |
364 | 362 |
365 void PixelRefUtils::GatherDiscardablePixelRefs( | 363 void PixelRefUtils::GatherDiscardablePixelRefs( |
366 SkPicture* picture, | 364 SkPicture* picture, |
367 std::vector<PositionPixelRef>* pixel_refs) { | 365 std::vector<PositionPixelRef>* pixel_refs) { |
368 pixel_refs->clear(); | 366 pixel_refs->clear(); |
369 DiscardablePixelRefSet pixel_ref_set(pixel_refs); | 367 DiscardablePixelRefSet pixel_ref_set(pixel_refs); |
370 | 368 |
371 SkRect picture_bounds = picture->cullRect(); | 369 SkRect picture_bounds = picture->cullRect(); |
372 SkIRect picture_ibounds = picture_bounds.roundOut(); | 370 SkIRect picture_ibounds = picture_bounds.roundOut(); |
373 SkBitmap empty_bitmap; | 371 SkBitmap empty_bitmap; |
374 empty_bitmap.setInfo(SkImageInfo::MakeUnknown(picture_ibounds.width(), | 372 empty_bitmap.setInfo(SkImageInfo::MakeUnknown(picture_ibounds.right(), |
weiliangc
2015/08/07 22:56:00
Just to double check, this won't need more memory
vmpstr
2015/08/10 17:36:05
It shouldn't, since we don't allocate any memory f
| |
375 picture_ibounds.height())); | 373 picture_ibounds.bottom())); |
376 | 374 |
377 GatherPixelRefDevice device(empty_bitmap, &pixel_ref_set); | 375 GatherPixelRefDevice device(empty_bitmap, &pixel_ref_set); |
378 SkNoSaveLayerCanvas canvas(&device); | 376 SkNoSaveLayerCanvas canvas(&device); |
379 | 377 |
380 // Draw the picture pinned against our top/left corner. | |
381 canvas.translate(-picture_bounds.left(), -picture_bounds.top()); | |
382 canvas.drawPicture(picture); | 378 canvas.drawPicture(picture); |
383 } | 379 } |
384 | 380 |
385 } // namespace skia | 381 } // namespace skia |
OLD | NEW |