Index: chrome/browser/android/compositor/layer/crushed_sprite_layer.cc |
diff --git a/chrome/browser/android/compositor/layer/crushed_sprite_layer.cc b/chrome/browser/android/compositor/layer/crushed_sprite_layer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..be1bfc1936ba5303ada7f475d0505e711cb8a254 |
--- /dev/null |
+++ b/chrome/browser/android/compositor/layer/crushed_sprite_layer.cc |
@@ -0,0 +1,119 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/android/compositor/layer/crushed_sprite_layer.h" |
+ |
+#include "cc/layers/layer.h" |
+#include "cc/layers/picture_layer.h" |
+#include "cc/playback/display_item_list.h" |
+#include "cc/playback/display_item_list_settings.h" |
+#include "cc/playback/drawing_display_item.h" |
+#include "content/public/browser/android/compositor.h" |
+#include "third_party/skia/include/core/SkPictureRecorder.h" |
+#include "ui/gfx/canvas.h" |
+#include "ui/gfx/skia_util.h" |
+ |
+namespace chrome { |
+namespace android { |
+ |
+// static |
+scoped_refptr<CrushedSpriteLayer> CrushedSpriteLayer::Create() { |
+ return make_scoped_refptr(new CrushedSpriteLayer()); |
+} |
+ |
+void CrushedSpriteLayer::PaintContents( |
+ SkCanvas* canvas, |
+ const gfx::Rect& clip, |
+ PaintingControlSetting painting_control) { |
+ gfx::Rect rect(layer_->bounds()); |
+ canvas->clipRect(RectToSkRect(rect)); |
+ |
+ if (paint_previous_frame_) |
+ canvas->drawPicture(previous_frame_.get()); |
+ |
+ if (src_dst_rects_.empty() || src_bitmap_.empty()) |
+ return; |
+ |
+ for (auto rect : src_dst_rects_) { |
+ canvas->drawBitmapRect(src_bitmap_, |
+ gfx::RectToSkRect(rect.first), |
+ gfx::RectToSkRect(rect.second), |
+ nullptr); |
+ } |
+} |
+ |
+scoped_refptr<cc::Layer> CrushedSpriteLayer::layer() { |
+ return layer_; |
+} |
+ |
+void CrushedSpriteLayer::UpdateCrushedSprite( |
+ const SkBitmap& src_bitmap, |
+ const std::vector<std::pair<gfx::Rect, gfx::Rect>>& src_dst_rects, |
+ bool paint_previous_frame) { |
+ src_bitmap_ = src_bitmap; |
+ src_dst_rects_ = src_dst_rects; |
+ paint_previous_frame_ = paint_previous_frame; |
+ layer_->SetNeedsDisplay(); |
+} |
+ |
+scoped_refptr<cc::DisplayItemList> |
+CrushedSpriteLayer::PaintContentsToDisplayList( |
+ const gfx::Rect& clip, |
+ PaintingControlSetting painting_control) { |
+ cc::DisplayItemListSettings settings; |
+ settings.use_cached_picture = true; |
+ scoped_refptr<cc::DisplayItemList> display_list = |
+ cc::DisplayItemList::Create(clip, settings); |
+ |
+ SkPictureRecorder recorder; |
+ SkCanvas* canvas = recorder.beginRecording(gfx::RectToSkRect(clip)); |
+ PaintContents(canvas, clip, painting_control); |
+ previous_frame_ = |
+ skia::AdoptRef(recorder.endRecordingAsPicture()); |
+ auto* item = display_list->CreateAndAppendItem<cc::DrawingDisplayItem>(); |
+ item->SetNew(previous_frame_); |
+ |
+ display_list->Finalize(); |
+ return display_list; |
+} |
+ |
+bool CrushedSpriteLayer::FillsBoundsCompletely() const { |
+ return false; |
+} |
+ |
+size_t CrushedSpriteLayer::GetApproximateUnsharedMemoryUsage() const { |
+ size_t memory_usage = 0; |
+ if (previous_frame_.get()) { |
+ // TODO(twellington): I have a question for reviewiers - I think |
+ // previous_frame_ is shared with the DisplayList when it's passed in |
+ // on line 75 above. Does that seem right? If so, it doesn't need to |
+ // be included in the memory usage. |
pedro (no code reviews)
2015/09/29 01:14:40
Good question. I don't know the answer. Same appli
David Trainor- moved to gerrit
2015/09/29 22:23:09
I would imagine it's asking memory that this class
Theresa
2015/10/01 01:57:31
As discussed offline, I'm going to leave in the sr
|
+ memory_usage += previous_frame_->approximateBytesUsed(); |
+ } |
+ if (!src_bitmap_.isNull()) { |
+ // TODO(twellington): Same question here about shared memory - I think |
+ // the underlying bits for SkPixelRef are shared w/ the UI resource. |
+ memory_usage += src_bitmap_.getSize(); |
+ } |
+ if (!src_dst_rects_.empty()) { |
+ memory_usage += sizeof(std::vector<std::pair<gfx::Rect, gfx::Rect>>); |
+ memory_usage += sizeof(gfx::Rect) * src_dst_rects_.size() * 2; |
+ } |
+ return memory_usage; |
+} |
+ |
+CrushedSpriteLayer::CrushedSpriteLayer() |
+ : layer_(cc::PictureLayer::Create(content::Compositor::LayerSettings(), |
+ this)), |
+ paint_previous_frame_(false) { |
+} |
+ |
+CrushedSpriteLayer::~CrushedSpriteLayer() { |
+ previous_frame_ = NULL; |
David Trainor- moved to gerrit
2015/09/29 22:23:09
I'm not sure this is necessary. It will just run
Theresa
2015/10/01 01:57:31
Done.
|
+ if (!src_bitmap_.isNull()) |
+ src_bitmap_.reset(); |
David Trainor- moved to gerrit
2015/09/29 22:23:09
Also might not be necessary? It looks like the un
Theresa
2015/10/01 01:57:31
I think you're right. I removed this and previous_
|
+} |
+ |
+} // namespace android |
+} // namespace chrome |