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 "third_party/skia/include/core/SkRefCnt.h" | |
11 #include "ui/android/resources/crushed_sprite_resource.h" | |
12 #include "ui/android/resources/resource_manager.h" | |
13 #include "ui/gfx/canvas.h" | |
14 #include "ui/gfx/skia_util.h" | |
15 | |
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_) { | |
37 // Get resource and setup variables. | |
38 ui::CrushedSpriteResource* resource = | |
39 resource_manager->GetCrushedSpriteResource( | |
40 bitmap_res_id, | |
41 metadata_res_id); | |
42 if (frame_count_ == -1) { | |
43 frame_count_ = resource->GetFrameCount(); | |
44 sprite_frame = completion_percentage * (frame_count_ - 1); | |
45 } | |
46 | |
47 // Reset the previous_frame if the animation is being re-run. | |
48 if (previous_frame_ > sprite_frame) { | |
49 previous_frame_ = -1; | |
50 } | |
51 | |
52 // Set up an SkCanvas backed by an SkBitmap to draw into. | |
53 SkBitmap bitmap; | |
54 bitmap.allocN32Pixels(resource->GetUnscaledSpriteSize().width(), | |
55 resource->GetUnscaledSpriteSize().height()); | |
56 SkCanvas canvas(bitmap); | |
57 | |
58 if (previous_frame_ == -1 || | |
59 sprite_frame == resource->GetFrameCount() - 1) { | |
60 // The newly allocated pixels for the SkBitmap need to be cleared if this | |
61 // is the first frame being drawn or the last frame. See crbug.com/549453. | |
62 canvas.clear(SK_ColorTRANSPARENT); | |
63 } | |
64 | |
65 // If this isn't the first or last frame, draw the previous frame(s). | |
66 // Note(twellington): This assumes that the last frame in the crushed sprite | |
67 // animation does not require any previous frames drawn before it. This code | |
68 // needs to be updated if crushed sprites are added for which this | |
69 // assumption does not hold. | |
70 if (sprite_frame != 0 && sprite_frame != resource->GetFrameCount() - 1) { | |
71 // Draw the previous frame. | |
72 if (previous_frame_ != -1) { | |
73 canvas.drawBitmap(previous_frame_bitmap_, 0, 0, nullptr); | |
74 } | |
75 | |
76 // Draw any skipped frames. | |
77 for (int i = previous_frame_ + 1; i < sprite_frame; ++i) { | |
78 DrawRectanglesForFrame(resource, i, &canvas); | |
79 } | |
80 } | |
81 | |
82 // Draw the current frame. | |
83 DrawRectanglesForFrame(resource, sprite_frame, &canvas); | |
84 | |
85 // Set the bitmap on layer_. | |
86 bitmap.setImmutable(); | |
87 layer_->SetBitmap(bitmap); | |
88 | |
89 // Set bounds to scale the layer. | |
90 layer_->SetBounds(resource->GetScaledSpriteSize()); | |
91 | |
92 // Evict the crushed sprite bitmap from memory if this is the last frame. | |
93 if (sprite_frame == frame_count_ - 1) { | |
94 resource->EvictBitmapFromMemory(); | |
95 } | |
96 | |
97 // Update previous_frame_* variables. | |
98 previous_frame_bitmap_ = bitmap; | |
99 previous_frame_ = sprite_frame; | |
100 } | |
101 } | |
102 | |
103 void CrushedSpriteLayer::DrawRectanglesForFrame( | |
104 ui::CrushedSpriteResource* resource, | |
105 int frame, | |
106 SkCanvas* canvas) { | |
107 ui::CrushedSpriteResource::FrameSrcDstRects src_dst_rects = | |
108 resource->GetRectanglesForFrame(frame); | |
109 for (const auto& rect : src_dst_rects) { | |
110 canvas->drawBitmapRect(resource->GetBitmap(), | |
111 gfx::RectToSkRect(rect.first), | |
112 gfx::RectToSkRect(rect.second), | |
113 nullptr); | |
114 } | |
115 } | |
116 | |
117 CrushedSpriteLayer::CrushedSpriteLayer() | |
118 : layer_(cc::UIResourceLayer::Create()), | |
119 frame_count_(-1), | |
120 previous_frame_(-1) { | |
121 layer_->SetIsDrawable(true); | |
122 } | |
123 | |
124 | |
125 CrushedSpriteLayer::~CrushedSpriteLayer() { | |
126 } | |
127 | |
128 } // namespace android | |
OLD | NEW |