| 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/render_widget_host.h" | 12 #include "content/public/browser/render_widget_host.h" |
| 13 #include "content/public/browser/render_widget_host_view.h" | 13 #include "content/public/browser/render_widget_host_view.h" |
| 14 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
| 15 #include "third_party/skia/include/core/SkCanvas.h" |
| 16 #include "third_party/skia/include/core/SkPaint.h" |
| 17 #include "third_party/skia/include/effects/SkLumaColorFilter.h" |
| 15 #include "ui/gfx/codec/png_codec.h" | 18 #include "ui/gfx/codec/png_codec.h" |
| 16 | 19 |
| 17 namespace { | 20 namespace { |
| 18 | 21 |
| 19 // Minimum delay between taking screenshots. | 22 // Minimum delay between taking screenshots. |
| 20 const int kMinScreenshotIntervalMS = 1000; | 23 const int kMinScreenshotIntervalMS = 1000; |
| 21 | 24 |
| 22 } | 25 } |
| 23 | 26 |
| 24 namespace content { | 27 namespace content { |
| 25 | 28 |
| 26 // Encodes an SkBitmap to PNG data in a worker thread. | 29 // Converts SkBitmap to grayscale and encodes to PNG data in a worker thread. |
| 27 class ScreenshotData : public base::RefCountedThreadSafe<ScreenshotData> { | 30 class ScreenshotData : public base::RefCountedThreadSafe<ScreenshotData> { |
| 28 public: | 31 public: |
| 29 ScreenshotData() { | 32 ScreenshotData() { |
| 30 } | 33 } |
| 31 | 34 |
| 32 void EncodeScreenshot(const SkBitmap& bitmap, base::Closure callback) { | 35 void EncodeScreenshot(const SkBitmap& bitmap, base::Closure callback) { |
| 33 if (!base::WorkerPool::PostTaskAndReply(FROM_HERE, | 36 if (!base::WorkerPool::PostTaskAndReply(FROM_HERE, |
| 34 base::Bind(&ScreenshotData::EncodeOnWorker, | 37 base::Bind(&ScreenshotData::EncodeOnWorker, |
| 35 this, | 38 this, |
| 36 bitmap), | 39 bitmap), |
| 37 callback, | 40 callback, |
| 38 true)) { | 41 true)) { |
| 39 callback.Run(); | 42 callback.Run(); |
| 40 } | 43 } |
| 41 } | 44 } |
| 42 | 45 |
| 43 scoped_refptr<base::RefCountedBytes> data() const { return data_; } | 46 scoped_refptr<base::RefCountedBytes> data() const { return data_; } |
| 44 | 47 |
| 45 private: | 48 private: |
| 46 friend class base::RefCountedThreadSafe<ScreenshotData>; | 49 friend class base::RefCountedThreadSafe<ScreenshotData>; |
| 47 virtual ~ScreenshotData() { | 50 virtual ~ScreenshotData() { |
| 48 } | 51 } |
| 49 | 52 |
| 50 void EncodeOnWorker(const SkBitmap& bitmap) { | 53 void EncodeOnWorker(const SkBitmap& bitmap) { |
| 51 std::vector<unsigned char> data; | 54 std::vector<unsigned char> data; |
| 52 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &data)) | 55 // Paint |bitmap| to a kA8_Config SkBitmap |
| 56 SkBitmap a8Bitmap; |
| 57 a8Bitmap.setConfig(SkBitmap::kA8_Config, |
| 58 bitmap.width(), |
| 59 bitmap.height(), |
| 60 0); |
| 61 a8Bitmap.allocPixels(); |
| 62 SkCanvas canvas(a8Bitmap); |
| 63 SkPaint paint; |
| 64 SkColorFilter* filter = SkLumaColorFilter::Create(); |
| 65 paint.setColorFilter(filter); |
| 66 filter->unref(); |
| 67 canvas.drawBitmap(bitmap, SK_Scalar1, SK_Scalar1, &paint); |
| 68 // Encode the a8Bitmap to grayscale PNG treating alpha as color intensity |
| 69 if (gfx::PNGCodec::EncodeA8SkBitmap(a8Bitmap, &data)) |
| 53 data_ = new base::RefCountedBytes(data); | 70 data_ = new base::RefCountedBytes(data); |
| 54 } | 71 } |
| 55 | 72 |
| 56 scoped_refptr<base::RefCountedBytes> data_; | 73 scoped_refptr<base::RefCountedBytes> data_; |
| 57 | 74 |
| 58 DISALLOW_COPY_AND_ASSIGN(ScreenshotData); | 75 DISALLOW_COPY_AND_ASSIGN(ScreenshotData); |
| 59 }; | 76 }; |
| 60 | 77 |
| 61 NavigationEntryScreenshotManager::NavigationEntryScreenshotManager( | 78 NavigationEntryScreenshotManager::NavigationEntryScreenshotManager( |
| 62 NavigationControllerImpl* owner) | 79 NavigationControllerImpl* owner) |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 owner_->GetEntryAtIndex(forward)); | 284 owner_->GetEntryAtIndex(forward)); |
| 268 if (ClearScreenshot(entry)) | 285 if (ClearScreenshot(entry)) |
| 269 --screenshot_count; | 286 --screenshot_count; |
| 270 ++forward; | 287 ++forward; |
| 271 } | 288 } |
| 272 CHECK_GE(screenshot_count, 0); | 289 CHECK_GE(screenshot_count, 0); |
| 273 CHECK_LE(screenshot_count, kMaxScreenshots); | 290 CHECK_LE(screenshot_count, kMaxScreenshots); |
| 274 } | 291 } |
| 275 | 292 |
| 276 } // namespace content | 293 } // namespace content |
| OLD | NEW |