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::SetCrushedSpriteResource( | |
29 scoped_refptr<ui::CrushedSpriteResource> resource) { | |
30 resource_ = resource; | |
David Trainor- moved to gerrit
2015/10/15 21:04:57
Ideally we wouldn't hold onto the resource here.
Theresa
2015/10/24 00:06:45
I got rid of this method and just pass the resourc
| |
31 } | |
32 | |
33 void CrushedSpriteLayer::DrawSpriteFrame(int sprite_frame) { | |
David Trainor- moved to gerrit
2015/10/15 21:04:57
Should you clamp sprite_frame between 0 and the ma
Theresa
2015/10/24 00:06:45
Done.
| |
34 if (sprite_frame == 0 || sprite_frame != previous_frame_) { | |
35 // Reset the previous_frame if the animation is being re-run. | |
36 if (previous_frame_ > sprite_frame) { | |
37 previous_frame_ = 0; | |
38 } | |
39 | |
40 // If this is the last frame and a bitmap for it has been cached with the | |
41 // resource, set the bitmap on layer_ and return. | |
42 if (sprite_frame == resource_->GetFrameCount() - 1 && | |
43 !resource_->GetBitmapForLastFrame().empty()) { | |
David Trainor- moved to gerrit
2015/10/15 21:04:57
Do we need this? Can't we rely on the sprite_fram
Theresa
2015/10/24 00:06:44
I think you mean rely on previous_frame_bitmap to
David Trainor- moved to gerrit
2015/10/27 15:14:47
I see your point. I'd be okay with sharing it. S
Theresa
2015/10/27 19:48:02
Done.
| |
44 layer_->SetBitmap(resource_->GetBitmapForLastFrame()); | |
45 return; | |
46 } | |
47 | |
48 // Reload the source bitmap if necessary. | |
49 if (resource_->BitmapHasBeenEvictedFromMemory()) { | |
David Trainor- moved to gerrit
2015/10/15 21:04:57
See above. Can probably simplify this a lot and r
Theresa
2015/10/24 00:06:45
I put the logic for evicting the sprite sheet from
David Trainor- moved to gerrit
2015/10/27 15:14:47
Won't this cause reloads during animations if two
Theresa
2015/10/27 19:48:02
As discussed offline, yes, it could cause unnecess
| |
50 resource_manager_->ReloadCrushedSpriteResource( | |
51 resource_->GetBitmapResourceId()); | |
52 } | |
53 | |
54 // Set up an SkCanvas backed by an SkBitmap to draw into. | |
55 SkBitmap* bitmap = new SkBitmap(); | |
56 bitmap->allocN32Pixels(resource_->GetSpriteSize(), | |
57 resource_->GetSpriteSize()); | |
58 SkCanvas* canvas = new SkCanvas(*bitmap); | |
David Trainor- moved to gerrit
2015/10/15 21:04:57
Leaking canvas and bitmap. Use:
// Don't worry,
Theresa
2015/10/24 00:06:44
Done.
| |
59 | |
60 // If this isn't the first or last frame, draw the previous frame(s). | |
61 // Note(twellington): This assumes that the last frame in the crushed sprite | |
62 // animation does not require any previous frames drawn before it. This code | |
63 // needs to be updated if crushed sprites are added for which this | |
64 // assumption does not hold. | |
65 if (sprite_frame != 0 && sprite_frame != resource_->GetFrameCount() - 1) { | |
66 // Draw the previous frame. | |
67 canvas->drawBitmap(previous_frame_bitmap_, 0, 0, nullptr); | |
David Trainor- moved to gerrit
2015/10/15 21:04:57
So... UIResourceBitmap expects an immututable bitm
Theresa
2015/10/24 00:06:45
I'm not sure why it has to be immutable (it just h
David Trainor- moved to gerrit
2015/10/27 15:14:47
I'm not sure if he still works here. I would imag
Theresa
2015/10/27 19:48:02
Acknowledged.
| |
68 | |
69 // Draw any skipped frames. | |
70 for (int i = previous_frame_ + 1; i < sprite_frame; ++i) { | |
71 DrawRectanglesForFrame(i, canvas); | |
72 } | |
73 } | |
74 | |
75 // Draw the current frame. | |
76 DrawRectanglesForFrame(sprite_frame, canvas); | |
77 | |
78 // Set the bitmap on layer_. | |
79 bitmap->setImmutable(); | |
80 layer_->SetBitmap(*bitmap); | |
81 | |
82 // Cache the bitmap on the resource if this is the last frame. | |
83 if (sprite_frame == resource_->GetFrameCount() - 1) { | |
David Trainor- moved to gerrit
2015/10/15 21:04:57
See above, probably don't need.
Theresa
2015/10/27 19:48:02
I left the bitmap memory ejection here but moved t
| |
84 resource_->SetBitmapForLastFrame(*bitmap); | |
85 } | |
86 | |
87 // Update previous_frame_* variables. | |
88 previous_frame_bitmap_ = *bitmap; | |
89 previous_frame_ = sprite_frame; | |
90 } | |
91 } | |
92 | |
93 void CrushedSpriteLayer::DrawRectanglesForFrame(int frame, SkCanvas* canvas) { | |
David Trainor- moved to gerrit
2015/10/15 21:04:57
skia::RefPtr<SkCanvas>
Theresa
2015/10/24 00:06:45
Done.
| |
94 ui::CrushedSpriteResource::FrameSrcDstRects src_dst_rects = | |
95 resource_->GetRectanglesForFrame(frame); | |
96 for (auto rect : src_dst_rects) { | |
David Trainor- moved to gerrit
2015/10/15 21:04:57
auto& so we don't copy the object?
Theresa
2015/10/24 00:06:44
Done.
| |
97 canvas->drawBitmapRect(resource_->GetBitmap(), | |
98 gfx::RectToSkRect(rect.first), | |
99 gfx::RectToSkRect(rect.second), | |
100 nullptr); | |
101 } | |
102 } | |
103 | |
104 CrushedSpriteLayer::CrushedSpriteLayer(ui::ResourceManager* resource_manager) | |
105 : resource_manager_(resource_manager), | |
106 layer_( | |
107 cc::UIResourceLayer::Create(content::Compositor::LayerSettings())), | |
108 previous_frame_(0) { | |
David Trainor- moved to gerrit
2015/10/15 21:04:57
-1 to say that we haven't drawn the first frame?
Theresa
2015/10/24 00:06:44
Done.
| |
109 layer_->SetIsDrawable(true); | |
110 } | |
111 | |
112 | |
113 CrushedSpriteLayer::~CrushedSpriteLayer() { | |
114 } | |
115 | |
116 } // namespace android | |
117 } // namespace chrome | |
OLD | NEW |