OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/android/compositor/layer/crushed_sprite_layer.h" | |
6 | |
7 #include "cc/layers/layer.h" | |
8 #include "cc/layers/ui_resource_layer.h" | |
9 #include "content/public/browser/android/compositor.h" | |
10 #include "ui/android/resources/crushed_sprite_resource.h" | |
11 #include "ui/android/resources/resource_manager.h" | |
12 #include "ui/gfx/canvas.h" | |
13 #include "ui/gfx/skia_util.h" | |
14 | |
15 namespace chrome { | |
16 namespace android { | |
17 | |
18 // static | |
19 scoped_refptr<CrushedSpriteLayer> CrushedSpriteLayer::Create( | |
20 ui::ResourceManager* resource_manager) { | |
21 return make_scoped_refptr(new CrushedSpriteLayer(resource_manager)); | |
22 } | |
23 | |
24 scoped_refptr<cc::Layer> CrushedSpriteLayer::layer() { | |
25 return layer_; | |
26 } | |
27 | |
28 void CrushedSpriteLayer::DrawSpriteFrame( | |
29 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.
| |
30 int sprite_frame) { | |
31 DCHECK(sprite_frame >= 0 && sprite_frame < resource->GetFrameCount()); | |
32 if (sprite_frame != previous_frame_) { | |
33 // Reset the previous_frame if the animation is being re-run. | |
34 if (previous_frame_ > sprite_frame) { | |
35 previous_frame_ = 0; | |
36 } | |
37 | |
38 // If this is the last frame and a bitmap for it has been cached with the | |
39 // resource, set the bitmap on layer_ and return. | |
40 if (sprite_frame == resource->GetFrameCount() - 1 && | |
41 !resource->GetBitmapForLastFrame().empty()) { | |
42 layer_->SetBitmap(resource->GetBitmapForLastFrame()); | |
43 return; | |
44 } | |
45 | |
46 // Reload the source bitmap if necessary. | |
47 if (resource->BitmapHasBeenEvictedFromMemory()) { | |
48 resource_manager_->ReloadCrushedSpriteResource( | |
49 resource->GetBitmapResourceId()); | |
50 } | |
51 | |
52 // Set up an SkCanvas backed by an SkBitmap to draw into. | |
53 SkBitmap bitmap; | |
54 bitmap.allocN32Pixels(resource->GetSpriteSize().width(), | |
55 resource->GetSpriteSize().height()); | |
56 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(new SkCanvas(bitmap)); | |
57 | |
58 // If this isn't the first or last frame, draw the previous frame(s). | |
59 // Note(twellington): This assumes that the last frame in the crushed sprite | |
60 // animation does not require any previous frames drawn before it. This code | |
61 // needs to be updated if crushed sprites are added for which this | |
62 // assumption does not hold. | |
63 if (sprite_frame != 0 && sprite_frame != resource->GetFrameCount() - 1) { | |
64 // Draw the previous frame. | |
65 canvas->drawBitmap(previous_frame_bitmap_, 0, 0, nullptr); | |
66 | |
67 // Draw any skipped frames. | |
68 for (int i = previous_frame_ + 1; i < sprite_frame; ++i) { | |
69 DrawRectanglesForFrame(resource, i, canvas); | |
70 } | |
71 } | |
72 | |
73 // Draw the current frame. | |
74 DrawRectanglesForFrame(resource, sprite_frame, canvas); | |
75 | |
76 // Set the bitmap on layer_. | |
77 bitmap.setImmutable(); | |
78 layer_->SetBitmap(bitmap); | |
79 | |
80 // Cache the bitmap on the resource if this is the last frame. | |
81 if (sprite_frame == resource->GetFrameCount() - 1) { | |
82 resource->SetBitmapForLastFrame(bitmap); | |
83 } | |
84 | |
85 // Update previous_frame_* variables. | |
86 previous_frame_bitmap_ = bitmap; | |
87 previous_frame_ = sprite_frame; | |
88 } | |
89 } | |
90 | |
91 void CrushedSpriteLayer::DrawRectanglesForFrame( | |
92 scoped_refptr<ui::CrushedSpriteResource> resource, | |
93 int frame, | |
94 skia::RefPtr<SkCanvas> canvas) { | |
95 ui::CrushedSpriteResource::FrameSrcDstRects src_dst_rects = | |
96 resource->GetRectanglesForFrame(frame); | |
97 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.
| |
98 canvas->drawBitmapRect(resource->GetBitmap(), | |
99 gfx::RectToSkRect(rect.first), | |
100 gfx::RectToSkRect(rect.second), | |
101 nullptr); | |
102 } | |
103 } | |
104 | |
105 CrushedSpriteLayer::CrushedSpriteLayer(ui::ResourceManager* resource_manager) | |
106 : resource_manager_(resource_manager), | |
107 layer_( | |
108 cc::UIResourceLayer::Create(content::Compositor::LayerSettings())), | |
109 previous_frame_(-1) { | |
110 layer_->SetIsDrawable(true); | |
111 } | |
112 | |
113 | |
114 CrushedSpriteLayer::~CrushedSpriteLayer() { | |
115 } | |
116 | |
117 } // namespace android | |
118 } // namespace chrome | |
OLD | NEW |