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 return make_scoped_refptr(new CrushedSpriteLayer()); | |
21 } | |
22 | |
23 scoped_refptr<cc::Layer> CrushedSpriteLayer::layer() { | |
24 return layer_; | |
25 } | |
26 | |
27 void CrushedSpriteLayer::DrawSpriteFrame( | |
28 ui::ResourceManager* resource_manager, | |
29 int bitmap_res_id, | |
30 int metadata_res_id, | |
31 float completion_percentage) { | |
32 DCHECK(completion_percentage >= 0.f && completion_percentage <= 1.f); | |
33 | |
34 int sprite_frame = completion_percentage * (frame_count_ - 1); | |
35 | |
36 if (frame_count_ == -1 || sprite_frame != previous_frame_) { | |
David Trainor- moved to gerrit
2015/10/27 21:00:42
Do we need frame_count_ == -1 if previous_frame wi
Theresa
2015/10/27 21:24:49
Maybe.. if the completion percentage starts at .5
| |
37 // Get resource and setup variables. | |
38 ui::CrushedSpriteResource* resource = | |
39 resource_manager->GetCrushedSpriteResource( | |
40 bitmap_res_id, | |
41 metadata_res_id); | |
42 frame_count_ = resource->GetFrameCount(); | |
David Trainor- moved to gerrit
2015/10/27 21:00:42
Move up above the other sprite_frame
Theresa
2015/10/27 21:24:49
It's here on purpose. If we're redisplaying the la
| |
43 int sprite_frame = completion_percentage * (frame_count_ - 1); | |
David Trainor- moved to gerrit
2015/10/27 21:00:43
redundant?
Theresa
2015/10/27 21:24:49
If frame_count_ != -1, yes. I'll surround this lin
Theresa
2015/10/28 02:01:53
Done.
| |
44 | |
45 | |
46 // Reset the previous_frame if the animation is being re-run. | |
47 if (previous_frame_ > sprite_frame) { | |
48 previous_frame_ = 0; | |
49 } | |
50 | |
51 // Set up an SkCanvas backed by an SkBitmap to draw into. | |
52 SkBitmap bitmap; | |
53 bitmap.allocN32Pixels(resource->GetSpriteSize().width(), | |
54 resource->GetSpriteSize().height()); | |
55 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(new SkCanvas(bitmap)); | |
56 | |
57 // If this isn't the first or last frame, draw the previous frame(s). | |
58 // Note(twellington): This assumes that the last frame in the crushed sprite | |
59 // animation does not require any previous frames drawn before it. This code | |
60 // needs to be updated if crushed sprites are added for which this | |
61 // assumption does not hold. | |
62 if (sprite_frame != 0 && sprite_frame != resource->GetFrameCount() - 1) { | |
63 // Draw the previous frame. | |
64 canvas->drawBitmap(previous_frame_bitmap_, 0, 0, nullptr); | |
65 | |
66 // Draw any skipped frames. | |
67 for (int i = previous_frame_ + 1; i < sprite_frame; ++i) { | |
68 DrawRectanglesForFrame(resource, i, canvas); | |
69 } | |
70 } | |
71 | |
72 // Draw the current frame. | |
73 DrawRectanglesForFrame(resource, sprite_frame, canvas); | |
74 | |
75 // Set the bitmap on layer_. | |
76 bitmap.setImmutable(); | |
77 layer_->SetBitmap(bitmap); | |
78 | |
79 // Evict the crushed sprite bitmap from memory if this is the last frame. | |
80 if (sprite_frame == frame_count_ - 1) { | |
81 resource->GetBitmap().empty(); | |
David Trainor- moved to gerrit
2015/10/27 21:00:42
Doesn't this just return if it's empty? Also do w
Theresa
2015/10/27 21:24:49
Discussed offline, I'll use .reset() and add a met
Theresa
2015/10/28 02:01:53
Done.
| |
82 } | |
83 | |
84 // Update previous_frame_* variables. | |
85 previous_frame_bitmap_ = bitmap; | |
86 previous_frame_ = sprite_frame; | |
87 } | |
88 } | |
89 | |
90 void CrushedSpriteLayer::DrawRectanglesForFrame( | |
91 ui::CrushedSpriteResource* resource, | |
92 int frame, | |
93 skia::RefPtr<SkCanvas> canvas) { | |
94 ui::CrushedSpriteResource::FrameSrcDstRects src_dst_rects = | |
95 resource->GetRectanglesForFrame(frame); | |
96 for (const auto& rect : src_dst_rects) { | |
97 canvas->drawBitmapRect(resource->GetBitmap(), | |
98 gfx::RectToSkRect(rect.first), | |
99 gfx::RectToSkRect(rect.second), | |
100 nullptr); | |
101 } | |
102 } | |
103 | |
104 CrushedSpriteLayer::CrushedSpriteLayer() | |
105 : layer_( | |
106 cc::UIResourceLayer::Create(content::Compositor::LayerSettings())), | |
107 frame_count_(-1), | |
108 previous_frame_(-1) { | |
109 layer_->SetIsDrawable(true); | |
110 } | |
111 | |
112 | |
113 CrushedSpriteLayer::~CrushedSpriteLayer() { | |
114 } | |
115 | |
116 } // namespace android | |
117 } // namespace chrome | |
OLD | NEW |