OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/frame_host/navigation_entry_screenshot_manager.h" | 5 #include "content/browser/frame_host/navigation_entry_screenshot_manager.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/threading/worker_pool.h" | 8 #include "base/threading/worker_pool.h" |
9 #include "content/browser/frame_host/navigation_controller_impl.h" | 9 #include "content/browser/frame_host/navigation_controller_impl.h" |
10 #include "content/browser/frame_host/navigation_entry_impl.h" | 10 #include "content/browser/frame_host/navigation_entry_impl.h" |
11 #include "content/browser/renderer_host/render_view_host_impl.h" | 11 #include "content/browser/renderer_host/render_view_host_impl.h" |
12 #include "content/public/browser/overscroll_configuration.h" | 12 #include "content/public/browser/overscroll_configuration.h" |
13 #include "content/public/browser/render_widget_host.h" | 13 #include "content/public/browser/render_widget_host.h" |
14 #include "content/public/browser/render_widget_host_view.h" | 14 #include "content/public/browser/render_widget_host_view.h" |
15 #include "content/public/common/content_switches.h" | 15 #include "content/public/common/content_switches.h" |
16 #include "third_party/skia/include/core/SkCanvas.h" | |
17 #include "third_party/skia/include/core/SkPaint.h" | |
18 #include "third_party/skia/include/effects/SkLumaColorFilter.h" | |
19 #include "ui/gfx/codec/png_codec.h" | 16 #include "ui/gfx/codec/png_codec.h" |
20 | 17 |
21 namespace { | 18 namespace { |
22 | 19 |
23 // Minimum delay between taking screenshots. | 20 // Minimum delay between taking screenshots. |
24 const int kMinScreenshotIntervalMS = 1000; | 21 const int kMinScreenshotIntervalMS = 1000; |
25 | 22 |
26 } | 23 } |
27 | 24 |
28 namespace content { | 25 namespace content { |
(...skipping 17 matching lines...) Expand all Loading... | |
46 | 43 |
47 scoped_refptr<base::RefCountedBytes> data() const { return data_; } | 44 scoped_refptr<base::RefCountedBytes> data() const { return data_; } |
48 | 45 |
49 private: | 46 private: |
50 friend class base::RefCountedThreadSafe<ScreenshotData>; | 47 friend class base::RefCountedThreadSafe<ScreenshotData>; |
51 virtual ~ScreenshotData() { | 48 virtual ~ScreenshotData() { |
52 } | 49 } |
53 | 50 |
54 void EncodeOnWorker(const SkBitmap& bitmap) { | 51 void EncodeOnWorker(const SkBitmap& bitmap) { |
55 std::vector<unsigned char> data; | 52 std::vector<unsigned char> data; |
56 // Paint |bitmap| to a kA8_Config SkBitmap | 53 // Encode the A8 bitmap to grayscale PNG treating alpha as color intensity. |
danakj
2014/07/10 19:13:02
you can dcheck the bitmap config here if you like?
mfomitchev
2014/07/11 19:46:07
Done. I did it in the other place because the stac
| |
57 SkBitmap a8Bitmap; | 54 if (gfx::PNGCodec::EncodeA8SkBitmap(bitmap, &data)) |
58 a8Bitmap.setConfig(SkBitmap::kA8_Config, | |
59 bitmap.width(), | |
60 bitmap.height(), | |
61 0); | |
62 a8Bitmap.allocPixels(); | |
63 SkCanvas canvas(a8Bitmap); | |
64 SkPaint paint; | |
65 SkColorFilter* filter = SkLumaColorFilter::Create(); | |
66 paint.setColorFilter(filter); | |
67 filter->unref(); | |
68 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint); | |
69 // Encode the a8Bitmap to grayscale PNG treating alpha as color intensity | |
70 if (gfx::PNGCodec::EncodeA8SkBitmap(a8Bitmap, &data)) | |
71 data_ = new base::RefCountedBytes(data); | 55 data_ = new base::RefCountedBytes(data); |
72 } | 56 } |
73 | 57 |
74 scoped_refptr<base::RefCountedBytes> data_; | 58 scoped_refptr<base::RefCountedBytes> data_; |
75 | 59 |
76 DISALLOW_COPY_AND_ASSIGN(ScreenshotData); | 60 DISALLOW_COPY_AND_ASSIGN(ScreenshotData); |
77 }; | 61 }; |
78 | 62 |
79 NavigationEntryScreenshotManager::NavigationEntryScreenshotManager( | 63 NavigationEntryScreenshotManager::NavigationEntryScreenshotManager( |
80 NavigationControllerImpl* owner) | 64 NavigationControllerImpl* owner) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 owner_->GetEntryAtIndex(i))); | 111 owner_->GetEntryAtIndex(i))); |
128 } | 112 } |
129 DCHECK_EQ(GetScreenshotCount(), 0); | 113 DCHECK_EQ(GetScreenshotCount(), 0); |
130 } | 114 } |
131 | 115 |
132 void NavigationEntryScreenshotManager::TakeScreenshotImpl( | 116 void NavigationEntryScreenshotManager::TakeScreenshotImpl( |
133 RenderViewHost* host, | 117 RenderViewHost* host, |
134 NavigationEntryImpl* entry) { | 118 NavigationEntryImpl* entry) { |
135 DCHECK(host && host->GetView()); | 119 DCHECK(host && host->GetView()); |
136 DCHECK(entry); | 120 DCHECK(entry); |
137 SkBitmap::Config preferred_format = host->PreferredReadbackFormat(); | |
138 host->CopyFromBackingStore( | 121 host->CopyFromBackingStore( |
139 gfx::Rect(), | 122 gfx::Rect(), |
140 host->GetView()->GetViewBounds().size(), | 123 host->GetView()->GetViewBounds().size(), |
141 base::Bind(&NavigationEntryScreenshotManager::OnScreenshotTaken, | 124 base::Bind(&NavigationEntryScreenshotManager::OnScreenshotTaken, |
142 screenshot_factory_.GetWeakPtr(), | 125 screenshot_factory_.GetWeakPtr(), |
143 entry->GetUniqueID()), | 126 entry->GetUniqueID()), |
144 preferred_format); | 127 SkBitmap::kA8_Config); |
145 } | 128 } |
146 | 129 |
147 void NavigationEntryScreenshotManager::SetMinScreenshotIntervalMS( | 130 void NavigationEntryScreenshotManager::SetMinScreenshotIntervalMS( |
148 int interval_ms) { | 131 int interval_ms) { |
149 DCHECK_GE(interval_ms, 0); | 132 DCHECK_GE(interval_ms, 0); |
150 min_screenshot_interval_ms_ = interval_ms; | 133 min_screenshot_interval_ms_ = interval_ms; |
151 } | 134 } |
152 | 135 |
153 void NavigationEntryScreenshotManager::OnScreenshotTaken(int unique_id, | 136 void NavigationEntryScreenshotManager::OnScreenshotTaken(int unique_id, |
154 bool success, | 137 bool success, |
155 const SkBitmap& bitmap) { | 138 const SkBitmap& bitmap) { |
139 LOG(ERROR) << "OnScreenshotTaken"; | |
140 | |
156 NavigationEntryImpl* entry = NULL; | 141 NavigationEntryImpl* entry = NULL; |
157 int entry_count = owner_->GetEntryCount(); | 142 int entry_count = owner_->GetEntryCount(); |
158 for (int i = 0; i < entry_count; ++i) { | 143 for (int i = 0; i < entry_count; ++i) { |
159 NavigationEntry* iter = owner_->GetEntryAtIndex(i); | 144 NavigationEntry* iter = owner_->GetEntryAtIndex(i); |
160 if (iter->GetUniqueID() == unique_id) { | 145 if (iter->GetUniqueID() == unique_id) { |
161 entry = NavigationEntryImpl::FromNavigationEntry(iter); | 146 entry = NavigationEntryImpl::FromNavigationEntry(iter); |
162 break; | 147 break; |
163 } | 148 } |
164 } | 149 } |
165 | 150 |
166 if (!entry) { | 151 if (!entry) { |
167 LOG(ERROR) << "Invalid entry with unique id: " << unique_id; | 152 LOG(ERROR) << "Invalid entry with unique id: " << unique_id; |
168 return; | 153 return; |
169 } | 154 } |
170 | 155 |
171 if (!success || bitmap.empty() || bitmap.isNull()) { | 156 if (!success || bitmap.empty() || bitmap.isNull()) { |
172 if (!ClearScreenshot(entry)) | 157 if (!ClearScreenshot(entry)) |
173 OnScreenshotSet(entry); | 158 OnScreenshotSet(entry); |
174 return; | 159 return; |
175 } | 160 } |
176 | 161 |
162 CHECK(bitmap.getConfig() == SkBitmap::kA8_Config) | |
danakj
2014/07/10 19:13:02
DCHECK_EQ, then you don't need the extra << either
mfomitchev
2014/07/11 19:46:07
Done.
| |
163 << "Incorrect screenshot format: " << bitmap.getConfig(); | |
164 | |
177 scoped_refptr<ScreenshotData> screenshot = new ScreenshotData(); | 165 scoped_refptr<ScreenshotData> screenshot = new ScreenshotData(); |
178 screenshot->EncodeScreenshot( | 166 screenshot->EncodeScreenshot( |
179 bitmap, | 167 bitmap, |
180 base::Bind(&NavigationEntryScreenshotManager::OnScreenshotEncodeComplete, | 168 base::Bind(&NavigationEntryScreenshotManager::OnScreenshotEncodeComplete, |
181 screenshot_factory_.GetWeakPtr(), | 169 screenshot_factory_.GetWeakPtr(), |
182 unique_id, | 170 unique_id, |
183 screenshot)); | 171 screenshot)); |
184 } | 172 } |
185 | 173 |
186 int NavigationEntryScreenshotManager::GetScreenshotCount() const { | 174 int NavigationEntryScreenshotManager::GetScreenshotCount() const { |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
287 owner_->GetEntryAtIndex(forward)); | 275 owner_->GetEntryAtIndex(forward)); |
288 if (ClearScreenshot(entry)) | 276 if (ClearScreenshot(entry)) |
289 --screenshot_count; | 277 --screenshot_count; |
290 ++forward; | 278 ++forward; |
291 } | 279 } |
292 CHECK_GE(screenshot_count, 0); | 280 CHECK_GE(screenshot_count, 0); |
293 CHECK_LE(screenshot_count, kMaxScreenshots); | 281 CHECK_LE(screenshot_count, kMaxScreenshots); |
294 } | 282 } |
295 | 283 |
296 } // namespace content | 284 } // namespace content |
OLD | NEW |