| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/compositor/layer/crushed_sprite_layer.h" | 5 #include "chrome/browser/android/compositor/layer/crushed_sprite_layer.h" |
| 6 | 6 |
| 7 #include "cc/layers/layer.h" | 7 #include "cc/layers/layer.h" |
| 8 #include "cc/layers/ui_resource_layer.h" | 8 #include "cc/layers/ui_resource_layer.h" |
| 9 #include "content/public/browser/android/compositor.h" | 9 #include "content/public/browser/android/compositor.h" |
| 10 #include "third_party/skia/include/core/SkRefCnt.h" | 10 #include "third_party/skia/include/core/SkRefCnt.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 // Reset the previous_frame if the animation is being re-run. | 47 // Reset the previous_frame if the animation is being re-run. |
| 48 if (previous_frame_ > sprite_frame) { | 48 if (previous_frame_ > sprite_frame) { |
| 49 previous_frame_ = -1; | 49 previous_frame_ = -1; |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Set up an SkCanvas backed by an SkBitmap to draw into. | 52 // Set up an SkCanvas backed by an SkBitmap to draw into. |
| 53 SkBitmap bitmap; | 53 SkBitmap bitmap; |
| 54 bitmap.allocN32Pixels(resource->GetUnscaledSpriteSize().width(), | 54 bitmap.allocN32Pixels(resource->GetUnscaledSpriteSize().width(), |
| 55 resource->GetUnscaledSpriteSize().height()); | 55 resource->GetUnscaledSpriteSize().height()); |
| 56 sk_sp<SkCanvas> canvas = sk_make_sp<SkCanvas>(bitmap); | 56 SkCanvas canvas(bitmap); |
| 57 | 57 |
| 58 if (previous_frame_ == -1 || | 58 if (previous_frame_ == -1 || |
| 59 sprite_frame == resource->GetFrameCount() - 1) { | 59 sprite_frame == resource->GetFrameCount() - 1) { |
| 60 // The newly allocated pixels for the SkBitmap need to be cleared if this | 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. | 61 // is the first frame being drawn or the last frame. See crbug.com/549453. |
| 62 canvas->clear(SK_ColorTRANSPARENT); | 62 canvas.clear(SK_ColorTRANSPARENT); |
| 63 } | 63 } |
| 64 | 64 |
| 65 // If this isn't the first or last frame, draw the previous frame(s). | 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 | 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 | 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 | 68 // needs to be updated if crushed sprites are added for which this |
| 69 // assumption does not hold. | 69 // assumption does not hold. |
| 70 if (sprite_frame != 0 && sprite_frame != resource->GetFrameCount() - 1) { | 70 if (sprite_frame != 0 && sprite_frame != resource->GetFrameCount() - 1) { |
| 71 // Draw the previous frame. | 71 // Draw the previous frame. |
| 72 if (previous_frame_ != -1) { | 72 if (previous_frame_ != -1) { |
| 73 canvas->drawBitmap(previous_frame_bitmap_, 0, 0, nullptr); | 73 canvas.drawBitmap(previous_frame_bitmap_, 0, 0, nullptr); |
| 74 } | 74 } |
| 75 | 75 |
| 76 // Draw any skipped frames. | 76 // Draw any skipped frames. |
| 77 for (int i = previous_frame_ + 1; i < sprite_frame; ++i) { | 77 for (int i = previous_frame_ + 1; i < sprite_frame; ++i) { |
| 78 DrawRectanglesForFrame(resource, i, canvas); | 78 DrawRectanglesForFrame(resource, i, &canvas); |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 | 81 |
| 82 // Draw the current frame. | 82 // Draw the current frame. |
| 83 DrawRectanglesForFrame(resource, sprite_frame, canvas); | 83 DrawRectanglesForFrame(resource, sprite_frame, &canvas); |
| 84 | 84 |
| 85 // Set the bitmap on layer_. | 85 // Set the bitmap on layer_. |
| 86 bitmap.setImmutable(); | 86 bitmap.setImmutable(); |
| 87 layer_->SetBitmap(bitmap); | 87 layer_->SetBitmap(bitmap); |
| 88 | 88 |
| 89 // Set bounds to scale the layer. | 89 // Set bounds to scale the layer. |
| 90 layer_->SetBounds(resource->GetScaledSpriteSize()); | 90 layer_->SetBounds(resource->GetScaledSpriteSize()); |
| 91 | 91 |
| 92 // Evict the crushed sprite bitmap from memory if this is the last frame. | 92 // Evict the crushed sprite bitmap from memory if this is the last frame. |
| 93 if (sprite_frame == frame_count_ - 1) { | 93 if (sprite_frame == frame_count_ - 1) { |
| 94 resource->EvictBitmapFromMemory(); | 94 resource->EvictBitmapFromMemory(); |
| 95 } | 95 } |
| 96 | 96 |
| 97 // Update previous_frame_* variables. | 97 // Update previous_frame_* variables. |
| 98 previous_frame_bitmap_ = bitmap; | 98 previous_frame_bitmap_ = bitmap; |
| 99 previous_frame_ = sprite_frame; | 99 previous_frame_ = sprite_frame; |
| 100 } | 100 } |
| 101 } | 101 } |
| 102 | 102 |
| 103 void CrushedSpriteLayer::DrawRectanglesForFrame( | 103 void CrushedSpriteLayer::DrawRectanglesForFrame( |
| 104 ui::CrushedSpriteResource* resource, | 104 ui::CrushedSpriteResource* resource, |
| 105 int frame, | 105 int frame, |
| 106 sk_sp<SkCanvas> canvas) { | 106 SkCanvas* canvas) { |
| 107 ui::CrushedSpriteResource::FrameSrcDstRects src_dst_rects = | 107 ui::CrushedSpriteResource::FrameSrcDstRects src_dst_rects = |
| 108 resource->GetRectanglesForFrame(frame); | 108 resource->GetRectanglesForFrame(frame); |
| 109 for (const auto& rect : src_dst_rects) { | 109 for (const auto& rect : src_dst_rects) { |
| 110 canvas->drawBitmapRect(resource->GetBitmap(), | 110 canvas->drawBitmapRect(resource->GetBitmap(), |
| 111 gfx::RectToSkRect(rect.first), | 111 gfx::RectToSkRect(rect.first), |
| 112 gfx::RectToSkRect(rect.second), | 112 gfx::RectToSkRect(rect.second), |
| 113 nullptr); | 113 nullptr); |
| 114 } | 114 } |
| 115 } | 115 } |
| 116 | 116 |
| 117 CrushedSpriteLayer::CrushedSpriteLayer() | 117 CrushedSpriteLayer::CrushedSpriteLayer() |
| 118 : layer_(cc::UIResourceLayer::Create()), | 118 : layer_(cc::UIResourceLayer::Create()), |
| 119 frame_count_(-1), | 119 frame_count_(-1), |
| 120 previous_frame_(-1) { | 120 previous_frame_(-1) { |
| 121 layer_->SetIsDrawable(true); | 121 layer_->SetIsDrawable(true); |
| 122 } | 122 } |
| 123 | 123 |
| 124 | 124 |
| 125 CrushedSpriteLayer::~CrushedSpriteLayer() { | 125 CrushedSpriteLayer::~CrushedSpriteLayer() { |
| 126 } | 126 } |
| 127 | 127 |
| 128 } // namespace android | 128 } // namespace android |
| OLD | NEW |