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 "athena/content/content_proxy.h" | 5 #include "athena/content/content_proxy.h" |
6 | 6 |
7 #include "athena/activity/public/activity.h" | 7 #include "athena/activity/public/activity.h" |
8 #include "athena/activity/public/activity_view_model.h" | 8 #include "athena/activity/public/activity_view_model.h" |
| 9 #include "base/bind.h" |
| 10 #include "base/threading/worker_pool.h" |
| 11 #include "content/public/browser/render_view_host.h" |
| 12 #include "content/public/browser/render_widget_host_view.h" |
9 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
10 #include "ui/aura/window.h" | 14 #include "ui/aura/window.h" |
11 #include "ui/views/background.h" | 15 #include "ui/gfx/codec/png_codec.h" |
| 16 #include "ui/gfx/geometry/rect.h" |
| 17 #include "ui/gfx/image/image.h" |
| 18 #include "ui/gfx/image/image_png_rep.h" |
12 #include "ui/views/controls/webview/webview.h" | 19 #include "ui/views/controls/webview/webview.h" |
13 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
14 | 21 |
15 namespace athena { | 22 namespace athena { |
16 | 23 |
| 24 // Encodes an A8 SkBitmap to grayscale PNG in a worker thread. |
| 25 class ProxyImageData : public base::RefCountedThreadSafe<ProxyImageData> { |
| 26 public: |
| 27 ProxyImageData() { |
| 28 } |
| 29 |
| 30 void EncodeImage(const SkBitmap& bitmap, base::Closure callback) { |
| 31 if (!base::WorkerPool::PostTaskAndReply(FROM_HERE, |
| 32 base::Bind(&ProxyImageData::EncodeOnWorker, |
| 33 this, |
| 34 bitmap), |
| 35 callback, |
| 36 true)) { |
| 37 // When coming here, the resulting image will be empty. |
| 38 DCHECK(false) << "Cannot start bitmap encode task."; |
| 39 callback.Run(); |
| 40 } |
| 41 } |
| 42 |
| 43 scoped_refptr<base::RefCountedBytes> data() const { return data_; } |
| 44 |
| 45 private: |
| 46 friend class base::RefCountedThreadSafe<ProxyImageData>; |
| 47 virtual ~ProxyImageData() { |
| 48 } |
| 49 |
| 50 void EncodeOnWorker(const SkBitmap& bitmap) { |
| 51 DCHECK_EQ(bitmap.colorType(), kAlpha_8_SkColorType); |
| 52 // Encode the A8 bitmap to grayscale PNG treating alpha as color intensity. |
| 53 std::vector<unsigned char> data; |
| 54 if (gfx::PNGCodec::EncodeA8SkBitmap(bitmap, &data)) |
| 55 data_ = new base::RefCountedBytes(data); |
| 56 } |
| 57 |
| 58 scoped_refptr<base::RefCountedBytes> data_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(ProxyImageData); |
| 61 }; |
| 62 |
17 ContentProxy::ContentProxy(views::WebView* web_view, Activity* activity) | 63 ContentProxy::ContentProxy(views::WebView* web_view, Activity* activity) |
18 : web_view_(web_view), | 64 : web_view_(web_view), |
19 window_(activity->GetWindow()), | 65 content_visible_(true), |
20 background_color_( | 66 content_loaded_(true), |
21 activity->GetActivityViewModel()->GetRepresentativeColor()), | 67 content_creation_called_(false), |
22 content_loaded_(true) { | 68 proxy_content_to_image_factory_(this) { |
| 69 // Note: The content will be hidden once the image got created. |
23 CreateProxyContent(); | 70 CreateProxyContent(); |
24 HideOriginalContent(); | |
25 } | 71 } |
26 | 72 |
27 ContentProxy::~ContentProxy() { | 73 ContentProxy::~ContentProxy() { |
28 // If we still have a connection to the original Activity, we make it visible | 74 // If we still have a connection to the original Activity, we make it visible |
29 // again. | 75 // again. |
30 ShowOriginalContent(); | 76 ShowOriginalContent(); |
31 // At this point we should either have no view - or the view should not be | |
32 // shown by its parent anymore. | |
33 DCHECK(!proxy_content_.get() || !proxy_content_->parent()); | |
34 proxy_content_.reset(); | |
35 } | 77 } |
36 | 78 |
37 void ContentProxy::ContentWillUnload() { | 79 void ContentProxy::ContentWillUnload() { |
38 content_loaded_ = false; | 80 content_loaded_ = false; |
39 } | 81 } |
40 | 82 |
41 gfx::ImageSkia ContentProxy::GetContentImage() { | 83 gfx::ImageSkia ContentProxy::GetContentImage() { |
42 // For the time being we keep this here and return an empty image. | 84 // While we compress to PNG, we use the original read back. |
43 return image_; | 85 if (!raw_image_.isNull() || !png_data_.get()) |
| 86 return raw_image_; |
| 87 |
| 88 // Otherwise we convert the PNG. |
| 89 std::vector<gfx::ImagePNGRep> image_reps; |
| 90 image_reps.push_back(gfx::ImagePNGRep(png_data_, 0.0f)); |
| 91 return *(gfx::Image(image_reps).ToImageSkia()); |
44 } | 92 } |
45 | 93 |
46 void ContentProxy::EvictContent() { | 94 void ContentProxy::EvictContent() { |
47 HideProxyContent(); | 95 raw_image_ = gfx::ImageSkia(); |
48 CreateSolidProxyContent(); | 96 png_data_->Release(); |
49 ShowProxyContent(); | |
50 } | 97 } |
51 | 98 |
52 void ContentProxy::Reparent(aura::Window* new_parent_window) { | 99 void ContentProxy::OnPreContentDestroyed() { |
53 if (new_parent_window == window_) | |
54 return; | |
55 | |
56 // Since we are breaking now the connection to the old content, we make the | 100 // Since we are breaking now the connection to the old content, we make the |
57 // content visible again before we continue. | 101 // content visible again before we continue. |
58 // Note: Since the owning window is invisible, it does not matter that we | 102 // Note: Since the owning window is invisible, it does not matter that we |
59 // make the web content visible if the window gets destroyed shortly after. | 103 // make the web content visible if the window gets destroyed shortly after. |
60 ShowOriginalContent(); | 104 ShowOriginalContent(); |
61 | 105 |
62 // Transfer the |proxy_content_| to the passed window. | |
63 window_ = new_parent_window; | |
64 web_view_ = NULL; | 106 web_view_ = NULL; |
65 | |
66 // Move the view to the new window and show it there. | |
67 HideOriginalContent(); | |
68 } | 107 } |
69 | 108 |
70 void ContentProxy::ShowOriginalContent() { | 109 void ContentProxy::ShowOriginalContent() { |
71 // Hide our content. | 110 if (web_view_ && !content_visible_) { |
72 HideProxyContent(); | |
73 | |
74 if (web_view_) { | |
75 // Show the original |web_view_| again. | 111 // Show the original |web_view_| again. |
76 web_view_->SetFastResize(false); | 112 web_view_->SetFastResize(false); |
77 // If the content is loaded, we ask it to relayout itself since the | 113 // If the content is loaded, we ask it to relayout itself since the |
78 // dimensions might have changed. If not, we will reload new content and no | 114 // dimensions might have changed. If not, we will reload new content and no |
79 // layout is required for the old content. | 115 // layout is required for the old content. |
80 if (content_loaded_) | 116 if (content_loaded_) |
81 web_view_->Layout(); | 117 web_view_->Layout(); |
82 web_view_->GetWebContents()->GetNativeView()->Show(); | 118 web_view_->GetWebContents()->GetNativeView()->Show(); |
83 web_view_->SetVisible(true); | 119 web_view_->SetVisible(true); |
| 120 content_visible_ = true; |
84 } | 121 } |
85 } | 122 } |
86 | 123 |
87 void ContentProxy::HideOriginalContent() { | 124 void ContentProxy::HideOriginalContent() { |
88 if (web_view_) { | 125 if (web_view_ && content_visible_) { |
89 // Hide the |web_view_|. | 126 // Hide the |web_view_|. |
90 // TODO(skuhne): We might consider removing the view from the window while | 127 // TODO(skuhne): We might consider removing the view from the window while |
91 // it's hidden - it should work the same way as show/hide and does not have | 128 // it's hidden - it should work the same way as show/hide and does not have |
92 // any window re-ordering effect. | 129 // any window re-ordering effect. Furthermore we want possibly to suppress |
| 130 // any resizing of content (not only fast resize) here to avoid jank on |
| 131 // rotation. |
93 web_view_->GetWebContents()->GetNativeView()->Hide(); | 132 web_view_->GetWebContents()->GetNativeView()->Hide(); |
94 web_view_->SetVisible(false); | 133 web_view_->SetVisible(false); |
95 // Don't allow the content to get resized with window size changes. | 134 // Don't allow the content to get resized with window size changes. |
96 web_view_->SetFastResize(true); | 135 web_view_->SetFastResize(true); |
| 136 content_visible_ = false; |
97 } | 137 } |
98 | |
99 // Show our replacement content. | |
100 ShowProxyContent(); | |
101 } | 138 } |
102 | 139 |
103 void ContentProxy::CreateProxyContent() { | 140 void ContentProxy::CreateProxyContent() { |
104 // For the time being we create only a solid color here. | 141 DCHECK(!content_creation_called_); |
105 // TODO(skuhne): Replace with copying the drawn content into |proxy_content_| | 142 content_creation_called_ = true; |
106 // instead. | 143 // Unit tests might not have a |web_view_|. |
107 CreateSolidProxyContent(); | 144 if (!web_view_) |
| 145 return; |
| 146 |
| 147 content::RenderViewHost* host = |
| 148 web_view_->GetWebContents()->GetRenderViewHost(); |
| 149 DCHECK(host && host->GetView()); |
| 150 gfx::Size source = host->GetView()->GetViewBounds().size(); |
| 151 gfx::Size target = gfx::Size(source.width() / 2, source.height() / 2); |
| 152 host->CopyFromBackingStore( |
| 153 gfx::Rect(), |
| 154 target, |
| 155 base::Bind(&ContentProxy::OnContentImageRead, |
| 156 proxy_content_to_image_factory_.GetWeakPtr()), |
| 157 kAlpha_8_SkColorType); |
108 } | 158 } |
109 | 159 |
110 void ContentProxy::CreateSolidProxyContent() { | 160 void ContentProxy::OnContentImageRead(bool success, const SkBitmap& bitmap) { |
111 proxy_content_.reset(new views::View()); | 161 // Now we can hide the content. Note that after hiding we are freeing memory |
112 proxy_content_->set_owned_by_client(); | 162 // and if something goes wrong we will end up with an empty page. |
113 proxy_content_->set_background( | 163 HideOriginalContent(); |
114 views::Background::CreateSolidBackground(background_color_)); | 164 |
| 165 if (!success || bitmap.empty() || bitmap.isNull()) |
| 166 return; |
| 167 |
| 168 // While we are encoding the image, we keep the current image as reference |
| 169 // to have something for the overview mode to grab. Once we have the encoded |
| 170 // PNG, we will get rid of this. |
| 171 raw_image_ = gfx::ImageSkia::CreateFrom1xBitmap(bitmap); |
| 172 |
| 173 scoped_refptr<ProxyImageData> png_image = new ProxyImageData(); |
| 174 png_image->EncodeImage( |
| 175 bitmap, |
| 176 base::Bind(&ContentProxy::OnContentImageEncodeComplete, |
| 177 proxy_content_to_image_factory_.GetWeakPtr(), |
| 178 png_image)); |
115 } | 179 } |
116 | 180 |
117 void ContentProxy::ShowProxyContent() { | 181 void ContentProxy::OnContentImageEncodeComplete( |
118 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window_); | 182 scoped_refptr<ProxyImageData> image) { |
119 DCHECK(widget); | 183 png_data_ = image->data(); |
120 views::View* client_view = widget->client_view(); | |
121 // Place the view in front of all others. | |
122 client_view->AddChildView(proxy_content_.get()); | |
123 proxy_content_->SetSize(client_view->bounds().size()); | |
124 } | |
125 | 184 |
126 void ContentProxy::HideProxyContent() { | 185 // From now on we decode the image as needed to save memory. |
127 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window_); | 186 raw_image_ = gfx::ImageSkia(); |
128 views::View* client_view = widget->client_view(); | |
129 client_view->RemoveChildView(proxy_content_.get()); | |
130 } | 187 } |
131 | 188 |
132 } // namespace athena | 189 } // namespace athena |
OLD | NEW |