OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/bind.h" |
| 6 #include "base/location.h" |
5 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/thread_task_runner_handle.h" |
6 #include "chrome/browser/android/thumbnail/thumbnail.h" | 9 #include "chrome/browser/android/thumbnail/thumbnail.h" |
7 #include "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
8 #include "third_party/skia/include/core/SkCanvas.h" | 11 #include "third_party/skia/include/core/SkCanvas.h" |
9 #include "ui/android/resources/ui_resource_provider.h" | 12 #include "ui/android/resources/ui_resource_provider.h" |
10 #include "ui/gfx/geometry/size_conversions.h" | 13 #include "ui/gfx/geometry/size_conversions.h" |
11 | 14 |
12 namespace { | 15 namespace { |
13 | 16 |
14 SkBitmap CreateSmallHolderBitmap() { | 17 SkBitmap CreateSmallHolderBitmap() { |
15 SkBitmap small_bitmap; | 18 SkBitmap small_bitmap; |
(...skipping 22 matching lines...) Expand all Loading... |
38 float scale, | 41 float scale, |
39 ui::UIResourceProvider* ui_resource_provider, | 42 ui::UIResourceProvider* ui_resource_provider, |
40 ThumbnailDelegate* thumbnail_delegate) | 43 ThumbnailDelegate* thumbnail_delegate) |
41 : tab_id_(tab_id), | 44 : tab_id_(tab_id), |
42 time_stamp_(time_stamp), | 45 time_stamp_(time_stamp), |
43 scale_(scale), | 46 scale_(scale), |
44 bitmap_(gfx::Size(1, 1), true), | 47 bitmap_(gfx::Size(1, 1), true), |
45 ui_resource_id_(0), | 48 ui_resource_id_(0), |
46 retrieved_(false), | 49 retrieved_(false), |
47 ui_resource_provider_(ui_resource_provider), | 50 ui_resource_provider_(ui_resource_provider), |
48 thumbnail_delegate_(thumbnail_delegate) { | 51 thumbnail_delegate_(thumbnail_delegate), |
| 52 weak_factory_(this) { |
49 } | 53 } |
50 | 54 |
51 Thumbnail::~Thumbnail() { | 55 Thumbnail::~Thumbnail() { |
52 ClearUIResourceId(); | 56 ClearUIResourceId(); |
53 } | 57 } |
54 | 58 |
55 void Thumbnail::SetBitmap(const SkBitmap& bitmap) { | 59 void Thumbnail::SetBitmap(const SkBitmap& bitmap) { |
56 DCHECK(!bitmap.empty()); | 60 DCHECK(!bitmap.empty()); |
57 retrieved_ = false; | 61 retrieved_ = false; |
58 ClearUIResourceId(); | 62 ClearUIResourceId(); |
(...skipping 17 matching lines...) Expand all Loading... |
76 } | 80 } |
77 | 81 |
78 void Thumbnail::CreateUIResource() { | 82 void Thumbnail::CreateUIResource() { |
79 DCHECK(ui_resource_provider_); | 83 DCHECK(ui_resource_provider_); |
80 if (!ui_resource_id_) | 84 if (!ui_resource_id_) |
81 ui_resource_id_ = ui_resource_provider_->CreateUIResource(this); | 85 ui_resource_id_ = ui_resource_provider_->CreateUIResource(this); |
82 } | 86 } |
83 | 87 |
84 cc::UIResourceBitmap Thumbnail::GetBitmap(cc::UIResourceId uid, | 88 cc::UIResourceBitmap Thumbnail::GetBitmap(cc::UIResourceId uid, |
85 bool resource_lost) { | 89 bool resource_lost) { |
86 if (retrieved_) | 90 if (retrieved_) { |
| 91 // InvalidateCachedThumbnail() causes |this| to be deleted, so |
| 92 // don't delete the resource while LayerTeeHost calls into |this| |
| 93 // to avoid reentry there. |
| 94 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 95 FROM_HERE, |
| 96 base::Bind(&Thumbnail::DoInvalidate, weak_factory_.GetWeakPtr())); |
87 return bitmap_; | 97 return bitmap_; |
| 98 } |
88 | 99 |
89 retrieved_ = true; | 100 retrieved_ = true; |
90 | 101 |
91 cc::UIResourceBitmap old_bitmap(bitmap_); | 102 cc::UIResourceBitmap old_bitmap(bitmap_); |
92 // Return a place holder for all other calls to GetBitmap. | 103 // Return a place holder for all other calls to GetBitmap. |
93 bitmap_ = cc::UIResourceBitmap(CreateSmallHolderBitmap()); | 104 bitmap_ = cc::UIResourceBitmap(CreateSmallHolderBitmap()); |
94 return old_bitmap; | 105 return old_bitmap; |
95 } | 106 } |
96 | 107 |
97 void Thumbnail::UIResourceIsInvalid() { | 108 void Thumbnail::DoInvalidate() { |
98 ui_resource_id_ = 0; | |
99 if (thumbnail_delegate_) | 109 if (thumbnail_delegate_) |
100 thumbnail_delegate_->InvalidateCachedThumbnail(this); | 110 thumbnail_delegate_->InvalidateCachedThumbnail(this); |
101 } | 111 } |
102 | 112 |
103 void Thumbnail::ClearUIResourceId() { | 113 void Thumbnail::ClearUIResourceId() { |
104 if (ui_resource_id_ && ui_resource_provider_) | 114 if (ui_resource_id_ && ui_resource_provider_) |
105 ui_resource_provider_->DeleteUIResource(ui_resource_id_); | 115 ui_resource_provider_->DeleteUIResource(ui_resource_id_); |
106 ui_resource_id_ = 0; | 116 ui_resource_id_ = 0; |
107 } | 117 } |
OLD | NEW |