OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/lazy_pixel_ref_utils.h" | 5 #include "skia/ext/discardable_pixel_ref_utils.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "skia/ext/lazy_pixel_ref.h" | |
10 #include "third_party/skia/include/core/SkBitmapDevice.h" | 9 #include "third_party/skia/include/core/SkBitmapDevice.h" |
11 #include "third_party/skia/include/core/SkCanvas.h" | 10 #include "third_party/skia/include/core/SkCanvas.h" |
12 #include "third_party/skia/include/core/SkData.h" | 11 #include "third_party/skia/include/core/SkData.h" |
13 #include "third_party/skia/include/core/SkDraw.h" | 12 #include "third_party/skia/include/core/SkDraw.h" |
14 #include "third_party/skia/include/core/SkPixelRef.h" | 13 #include "third_party/skia/include/core/SkPixelRef.h" |
15 #include "third_party/skia/include/core/SkRRect.h" | 14 #include "third_party/skia/include/core/SkRRect.h" |
16 #include "third_party/skia/include/core/SkRect.h" | 15 #include "third_party/skia/include/core/SkRect.h" |
17 #include "third_party/skia/include/core/SkShader.h" | 16 #include "third_party/skia/include/core/SkShader.h" |
18 #include "third_party/skia/src/core/SkRasterClip.h" | 17 #include "third_party/skia/src/core/SkRasterClip.h" |
19 | 18 |
20 namespace skia { | 19 namespace skia { |
21 | 20 |
22 namespace { | 21 namespace { |
23 | 22 |
24 // URI label for a lazily decoded SkPixelRef. | 23 // URI label for a discardable SkPixelRef. |
25 const char kLabelLazyDecoded[] = "lazy"; | 24 const char kLabelDiscardable[] = "discardable"; |
26 | 25 |
27 class LazyPixelRefSet { | 26 class DiscardablePixelRefSet { |
28 public: | 27 public: |
29 LazyPixelRefSet( | 28 DiscardablePixelRefSet( |
30 std::vector<LazyPixelRefUtils::PositionLazyPixelRef>* pixel_refs) | 29 std::vector<DiscardablePixelRefUtils::PositionPixelRef>* pixel_refs) |
31 : pixel_refs_(pixel_refs) {} | 30 : pixel_refs_(pixel_refs) {} |
32 | 31 |
33 void Add(SkPixelRef* pixel_ref, const SkRect& rect) { | 32 void Add(SkPixelRef* pixel_ref, const SkRect& rect) { |
34 // Only save lazy pixel refs. | 33 // Only save discardable pixel refs. |
35 if (pixel_ref->getURI() && | 34 if (pixel_ref->getURI() && |
36 !strcmp(pixel_ref->getURI(), kLabelLazyDecoded)) { | 35 !strcmp(pixel_ref->getURI(), kLabelDiscardable)) { |
37 LazyPixelRefUtils::PositionLazyPixelRef position_pixel_ref; | 36 DiscardablePixelRefUtils::PositionPixelRef position_pixel_ref; |
38 position_pixel_ref.lazy_pixel_ref = | 37 position_pixel_ref.pixel_ref = pixel_ref; |
39 static_cast<skia::LazyPixelRef*>(pixel_ref); | |
40 position_pixel_ref.pixel_ref_rect = rect; | 38 position_pixel_ref.pixel_ref_rect = rect; |
41 pixel_refs_->push_back(position_pixel_ref); | 39 pixel_refs_->push_back(position_pixel_ref); |
42 } | 40 } |
43 } | 41 } |
44 | 42 |
45 private: | 43 private: |
46 std::vector<LazyPixelRefUtils::PositionLazyPixelRef>* pixel_refs_; | 44 std::vector<DiscardablePixelRefUtils::PositionPixelRef>* pixel_refs_; |
47 }; | 45 }; |
48 | 46 |
49 class GatherPixelRefDevice : public SkBitmapDevice { | 47 class GatherPixelRefDevice : public SkBitmapDevice { |
50 public: | 48 public: |
51 GatherPixelRefDevice(const SkBitmap& bm, LazyPixelRefSet* lazy_pixel_ref_set) | 49 GatherPixelRefDevice(const SkBitmap& bm, |
52 : SkBitmapDevice(bm), lazy_pixel_ref_set_(lazy_pixel_ref_set) {} | 50 DiscardablePixelRefSet* pixel_ref_set) |
| 51 : SkBitmapDevice(bm), pixel_ref_set_(pixel_ref_set) {} |
53 | 52 |
54 virtual void clear(SkColor color) SK_OVERRIDE {} | 53 virtual void clear(SkColor color) SK_OVERRIDE {} |
55 virtual void writePixels(const SkBitmap& bitmap, | 54 virtual void writePixels(const SkBitmap& bitmap, |
56 int x, | 55 int x, |
57 int y, | 56 int y, |
58 SkCanvas::Config8888 config8888) SK_OVERRIDE {} | 57 SkCanvas::Config8888 config8888) SK_OVERRIDE {} |
59 | 58 |
60 virtual void drawPaint(const SkDraw& draw, const SkPaint& paint) SK_OVERRIDE { | 59 virtual void drawPaint(const SkDraw& draw, const SkPaint& paint) SK_OVERRIDE { |
61 SkBitmap bitmap; | 60 SkBitmap bitmap; |
62 if (GetBitmapFromPaint(paint, &bitmap)) { | 61 if (GetBitmapFromPaint(paint, &bitmap)) { |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 | 319 |
321 protected: | 320 protected: |
322 virtual bool onReadPixels(const SkBitmap& bitmap, | 321 virtual bool onReadPixels(const SkBitmap& bitmap, |
323 int x, | 322 int x, |
324 int y, | 323 int y, |
325 SkCanvas::Config8888 config8888) SK_OVERRIDE { | 324 SkCanvas::Config8888 config8888) SK_OVERRIDE { |
326 return false; | 325 return false; |
327 } | 326 } |
328 | 327 |
329 private: | 328 private: |
330 LazyPixelRefSet* lazy_pixel_ref_set_; | 329 DiscardablePixelRefSet* pixel_ref_set_; |
331 | 330 |
332 void AddBitmap(const SkBitmap& bm, const SkRect& rect) { | 331 void AddBitmap(const SkBitmap& bm, const SkRect& rect) { |
333 SkRect canvas_rect = SkRect::MakeWH(width(), height()); | 332 SkRect canvas_rect = SkRect::MakeWH(width(), height()); |
334 SkRect paint_rect = SkRect::MakeEmpty(); | 333 SkRect paint_rect = SkRect::MakeEmpty(); |
335 paint_rect.intersect(rect, canvas_rect); | 334 paint_rect.intersect(rect, canvas_rect); |
336 lazy_pixel_ref_set_->Add(bm.pixelRef(), paint_rect); | 335 pixel_ref_set_->Add(bm.pixelRef(), paint_rect); |
337 } | 336 } |
338 | 337 |
339 bool GetBitmapFromPaint(const SkPaint& paint, SkBitmap* bm) { | 338 bool GetBitmapFromPaint(const SkPaint& paint, SkBitmap* bm) { |
340 SkShader* shader = paint.getShader(); | 339 SkShader* shader = paint.getShader(); |
341 if (shader) { | 340 if (shader) { |
342 // Check whether the shader is a gradient in order to prevent generation | 341 // Check whether the shader is a gradient in order to prevent generation |
343 // of bitmaps from gradient shaders, which implement asABitmap. | 342 // of bitmaps from gradient shaders, which implement asABitmap. |
344 if (SkShader::kNone_GradientType == shader->asAGradient(NULL)) | 343 if (SkShader::kNone_GradientType == shader->asAGradient(NULL)) |
345 return shader->asABitmap(bm, NULL, NULL); | 344 return shader->asABitmap(bm, NULL, NULL); |
346 } | 345 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 return this->updateClipConservativelyUsingBounds( | 381 return this->updateClipConservativelyUsingBounds( |
383 rrect.getBounds(), op, false); | 382 rrect.getBounds(), op, false); |
384 } | 383 } |
385 | 384 |
386 private: | 385 private: |
387 typedef SkCanvas INHERITED; | 386 typedef SkCanvas INHERITED; |
388 }; | 387 }; |
389 | 388 |
390 } // namespace | 389 } // namespace |
391 | 390 |
392 void LazyPixelRefUtils::GatherPixelRefs( | 391 void DiscardablePixelRefUtils::GatherPixelRefs( |
393 SkPicture* picture, | 392 SkPicture* picture, |
394 std::vector<PositionLazyPixelRef>* lazy_pixel_refs) { | 393 std::vector<PositionPixelRef>* pixel_refs) { |
395 lazy_pixel_refs->clear(); | 394 pixel_refs->clear(); |
396 LazyPixelRefSet pixel_ref_set(lazy_pixel_refs); | 395 DiscardablePixelRefSet pixel_ref_set(pixel_refs); |
397 | 396 |
398 SkBitmap empty_bitmap; | 397 SkBitmap empty_bitmap; |
399 empty_bitmap.setConfig( | 398 empty_bitmap.setConfig( |
400 SkBitmap::kNo_Config, picture->width(), picture->height()); | 399 SkBitmap::kNo_Config, picture->width(), picture->height()); |
401 | 400 |
402 GatherPixelRefDevice device(empty_bitmap, &pixel_ref_set); | 401 GatherPixelRefDevice device(empty_bitmap, &pixel_ref_set); |
403 NoSaveLayerCanvas canvas(&device); | 402 NoSaveLayerCanvas canvas(&device); |
404 | 403 |
405 canvas.clipRect(SkRect::MakeWH(picture->width(), picture->height()), | 404 canvas.clipRect(SkRect::MakeWH(picture->width(), picture->height()), |
406 SkRegion::kIntersect_Op, | 405 SkRegion::kIntersect_Op, |
407 false); | 406 false); |
408 canvas.drawPicture(*picture); | 407 canvas.drawPicture(*picture); |
409 } | 408 } |
410 | 409 |
411 } // namespace skia | 410 } // namespace skia |
OLD | NEW |