Chromium Code Reviews| 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..b946862fca09448c933d5e327313836198abaeaf |
| --- /dev/null |
| +++ b/chrome/browser/android/compositor/layer/crushed_sprite_layer.cc |
| @@ -0,0 +1,118 @@ |
| +// 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/ui_resource_layer.h" |
| +#include "content/public/browser/android/compositor.h" |
| +#include "ui/android/resources/crushed_sprite_resource.h" |
| +#include "ui/android/resources/resource_manager.h" |
| +#include "ui/gfx/canvas.h" |
| +#include "ui/gfx/skia_util.h" |
| + |
| +namespace chrome { |
| +namespace android { |
| + |
| +// static |
| +scoped_refptr<CrushedSpriteLayer> CrushedSpriteLayer::Create( |
| + ui::ResourceManager* resource_manager) { |
| + return make_scoped_refptr(new CrushedSpriteLayer(resource_manager)); |
| +} |
| + |
| +scoped_refptr<cc::Layer> CrushedSpriteLayer::layer() { |
| + return layer_; |
| +} |
| + |
| +void CrushedSpriteLayer::DrawSpriteFrame( |
| + scoped_refptr<ui::CrushedSpriteResource> resource, |
|
David Trainor- moved to gerrit
2015/10/27 15:14:47
Do we still need scoped_refptr<> if the resource i
Theresa
2015/10/27 19:48:03
Nope. Removed the ref counting.
|
| + int sprite_frame) { |
| + DCHECK(sprite_frame >= 0 && sprite_frame < resource->GetFrameCount()); |
| + if (sprite_frame != previous_frame_) { |
| + // Reset the previous_frame if the animation is being re-run. |
| + if (previous_frame_ > sprite_frame) { |
| + previous_frame_ = 0; |
| + } |
| + |
| + // If this is the last frame and a bitmap for it has been cached with the |
| + // resource, set the bitmap on layer_ and return. |
| + if (sprite_frame == resource->GetFrameCount() - 1 && |
| + !resource->GetBitmapForLastFrame().empty()) { |
| + layer_->SetBitmap(resource->GetBitmapForLastFrame()); |
| + return; |
| + } |
| + |
| + // Reload the source bitmap if necessary. |
| + if (resource->BitmapHasBeenEvictedFromMemory()) { |
| + resource_manager_->ReloadCrushedSpriteResource( |
| + resource->GetBitmapResourceId()); |
| + } |
| + |
| + // Set up an SkCanvas backed by an SkBitmap to draw into. |
| + SkBitmap bitmap; |
| + bitmap.allocN32Pixels(resource->GetSpriteSize().width(), |
| + resource->GetSpriteSize().height()); |
| + skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(new SkCanvas(bitmap)); |
| + |
| + // If this isn't the first or last frame, draw the previous frame(s). |
| + // Note(twellington): This assumes that the last frame in the crushed sprite |
| + // animation does not require any previous frames drawn before it. This code |
| + // needs to be updated if crushed sprites are added for which this |
| + // assumption does not hold. |
| + if (sprite_frame != 0 && sprite_frame != resource->GetFrameCount() - 1) { |
| + // Draw the previous frame. |
| + canvas->drawBitmap(previous_frame_bitmap_, 0, 0, nullptr); |
| + |
| + // Draw any skipped frames. |
| + for (int i = previous_frame_ + 1; i < sprite_frame; ++i) { |
| + DrawRectanglesForFrame(resource, i, canvas); |
| + } |
| + } |
| + |
| + // Draw the current frame. |
| + DrawRectanglesForFrame(resource, sprite_frame, canvas); |
| + |
| + // Set the bitmap on layer_. |
| + bitmap.setImmutable(); |
| + layer_->SetBitmap(bitmap); |
| + |
| + // Cache the bitmap on the resource if this is the last frame. |
| + if (sprite_frame == resource->GetFrameCount() - 1) { |
| + resource->SetBitmapForLastFrame(bitmap); |
| + } |
| + |
| + // Update previous_frame_* variables. |
| + previous_frame_bitmap_ = bitmap; |
| + previous_frame_ = sprite_frame; |
| + } |
| +} |
| + |
| +void CrushedSpriteLayer::DrawRectanglesForFrame( |
| + scoped_refptr<ui::CrushedSpriteResource> resource, |
| + int frame, |
| + skia::RefPtr<SkCanvas> canvas) { |
| + ui::CrushedSpriteResource::FrameSrcDstRects src_dst_rects = |
| + resource->GetRectanglesForFrame(frame); |
| + for (auto& rect : src_dst_rects) { |
|
David Trainor- moved to gerrit
2015/10/27 15:14:47
Actually... const auto& :). Sorry!
Theresa
2015/10/27 19:48:02
Done.
|
| + canvas->drawBitmapRect(resource->GetBitmap(), |
| + gfx::RectToSkRect(rect.first), |
| + gfx::RectToSkRect(rect.second), |
| + nullptr); |
| + } |
| +} |
| + |
| +CrushedSpriteLayer::CrushedSpriteLayer(ui::ResourceManager* resource_manager) |
| + : resource_manager_(resource_manager), |
| + layer_( |
| + cc::UIResourceLayer::Create(content::Compositor::LayerSettings())), |
| + previous_frame_(-1) { |
| + layer_->SetIsDrawable(true); |
| +} |
| + |
| + |
| +CrushedSpriteLayer::~CrushedSpriteLayer() { |
| +} |
| + |
| +} // namespace android |
| +} // namespace chrome |