| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/snapshot/snapshot.h" | 5 #include "ui/snapshot/snapshot.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/logging.h" | |
| 10 #include "base/numerics/safe_conversions.h" | |
| 11 #include "base/task_runner_util.h" | 9 #include "base/task_runner_util.h" |
| 12 #include "cc/output/copy_output_request.h" | 10 #include "cc/output/copy_output_request.h" |
| 13 #include "cc/output/copy_output_result.h" | 11 #include "cc/output/copy_output_result.h" |
| 14 #include "skia/ext/image_operations.h" | |
| 15 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "third_party/skia/include/core/SkPixelRef.h" | |
| 17 #include "ui/aura/window.h" | 13 #include "ui/aura/window.h" |
| 18 #include "ui/aura/window_event_dispatcher.h" | |
| 19 #include "ui/compositor/compositor.h" | 14 #include "ui/compositor/compositor.h" |
| 20 #include "ui/compositor/dip_util.h" | 15 #include "ui/compositor/dip_util.h" |
| 21 #include "ui/compositor/layer.h" | 16 #include "ui/compositor/layer.h" |
| 22 #include "ui/gfx/codec/png_codec.h" | 17 #include "ui/snapshot/snapshot_async.h" |
| 23 #include "ui/gfx/display.h" | |
| 24 #include "ui/gfx/image/image.h" | |
| 25 #include "ui/gfx/image/image_skia.h" | |
| 26 #include "ui/gfx/rect.h" | |
| 27 #include "ui/gfx/rect_conversions.h" | |
| 28 #include "ui/gfx/rect_f.h" | |
| 29 #include "ui/gfx/screen.h" | |
| 30 #include "ui/gfx/skbitmap_operations.h" | |
| 31 #include "ui/gfx/transform.h" | |
| 32 | 18 |
| 33 namespace ui { | 19 namespace ui { |
| 34 | 20 |
| 35 namespace { | |
| 36 | |
| 37 void OnFrameScalingFinished( | |
| 38 const GrabWindowSnapshotAsyncCallback& callback, | |
| 39 const SkBitmap& scaled_bitmap) { | |
| 40 callback.Run(gfx::Image(gfx::ImageSkia::CreateFrom1xBitmap(scaled_bitmap))); | |
| 41 } | |
| 42 | |
| 43 SkBitmap ScaleBitmap(const SkBitmap& input_bitmap, | |
| 44 const gfx::Size& target_size) { | |
| 45 return skia::ImageOperations::Resize( | |
| 46 input_bitmap, | |
| 47 skia::ImageOperations::RESIZE_GOOD, | |
| 48 target_size.width(), | |
| 49 target_size.height(), | |
| 50 static_cast<SkBitmap::Allocator*>(NULL)); | |
| 51 } | |
| 52 | |
| 53 scoped_refptr<base::RefCountedBytes> EncodeBitmap(const SkBitmap& bitmap) { | |
| 54 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); | |
| 55 unsigned char* pixels = | |
| 56 reinterpret_cast<unsigned char*>(bitmap.pixelRef()->pixels()); | |
| 57 if (!gfx::PNGCodec::Encode(pixels, | |
| 58 gfx::PNGCodec::FORMAT_BGRA, | |
| 59 gfx::Size(bitmap.width(), bitmap.height()), | |
| 60 base::checked_cast<int>(bitmap.rowBytes()), | |
| 61 true, | |
| 62 std::vector<gfx::PNGCodec::Comment>(), | |
| 63 &png_data->data())) { | |
| 64 return scoped_refptr<base::RefCountedBytes>(); | |
| 65 } | |
| 66 return png_data; | |
| 67 } | |
| 68 | |
| 69 void ScaleCopyOutputResult( | |
| 70 const GrabWindowSnapshotAsyncCallback& callback, | |
| 71 const gfx::Size& target_size, | |
| 72 scoped_refptr<base::TaskRunner> background_task_runner, | |
| 73 scoped_ptr<cc::CopyOutputResult> result) { | |
| 74 if (result->IsEmpty()) { | |
| 75 callback.Run(gfx::Image()); | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 // TODO(sergeyu): Potentially images can be scaled on GPU before reading it | |
| 80 // from GPU. Image scaling is implemented in content::GlHelper, but it's can't | |
| 81 // be used here because it's not in content/public. Move the scaling code | |
| 82 // somewhere so that it can be reused here. | |
| 83 base::PostTaskAndReplyWithResult( | |
| 84 background_task_runner, | |
| 85 FROM_HERE, | |
| 86 base::Bind(ScaleBitmap, *result->TakeBitmap(), target_size), | |
| 87 base::Bind(&OnFrameScalingFinished, callback)); | |
| 88 } | |
| 89 | |
| 90 void EncodeCopyOutputResult( | |
| 91 const GrabWindowSnapshotAsyncPNGCallback& callback, | |
| 92 scoped_refptr<base::TaskRunner> background_task_runner, | |
| 93 scoped_ptr<cc::CopyOutputResult> result) { | |
| 94 if (result->IsEmpty()) { | |
| 95 callback.Run(scoped_refptr<base::RefCountedBytes>()); | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 // TODO(sergeyu): Potentially images can be scaled on GPU before reading it | |
| 100 // from GPU. Image scaling is implemented in content::GlHelper, but it's can't | |
| 101 // be used here because it's not in content/public. Move the scaling code | |
| 102 // somewhere so that it can be reused here. | |
| 103 base::PostTaskAndReplyWithResult(background_task_runner, | |
| 104 FROM_HERE, | |
| 105 base::Bind(EncodeBitmap, | |
| 106 *result->TakeBitmap()), | |
| 107 callback); | |
| 108 } | |
| 109 | |
| 110 } // namespace | |
| 111 | |
| 112 bool GrabViewSnapshot(gfx::NativeView view, | 21 bool GrabViewSnapshot(gfx::NativeView view, |
| 113 std::vector<unsigned char>* png_representation, | 22 std::vector<unsigned char>* png_representation, |
| 114 const gfx::Rect& snapshot_bounds) { | 23 const gfx::Rect& snapshot_bounds) { |
| 115 return GrabWindowSnapshot(view, png_representation, snapshot_bounds); | 24 return GrabWindowSnapshot(view, png_representation, snapshot_bounds); |
| 116 } | 25 } |
| 117 | 26 |
| 118 bool GrabWindowSnapshot(gfx::NativeWindow window, | 27 bool GrabWindowSnapshot(gfx::NativeWindow window, |
| 119 std::vector<unsigned char>* png_representation, | 28 std::vector<unsigned char>* png_representation, |
| 120 const gfx::Rect& snapshot_bounds) { | 29 const gfx::Rect& snapshot_bounds) { |
| 121 // Not supported in Aura. Callers should fall back to the async version. | 30 // Not supported in Aura. Callers should fall back to the async version. |
| 122 return false; | 31 return false; |
| 123 } | 32 } |
| 124 | 33 |
| 125 void MakeAsyncCopyRequest( | 34 static void MakeAsyncCopyRequest( |
| 126 gfx::NativeWindow window, | 35 gfx::NativeWindow window, |
| 127 const gfx::Rect& source_rect, | 36 const gfx::Rect& source_rect, |
| 128 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) { | 37 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) { |
| 129 scoped_ptr<cc::CopyOutputRequest> request = | 38 scoped_ptr<cc::CopyOutputRequest> request = |
| 130 cc::CopyOutputRequest::CreateBitmapRequest(callback); | 39 cc::CopyOutputRequest::CreateBitmapRequest(callback); |
| 131 request->set_area(ui::ConvertRectToPixel(window->layer(), source_rect)); | 40 request->set_area(ui::ConvertRectToPixel(window->layer(), source_rect)); |
| 132 window->layer()->RequestCopyOfOutput(request.Pass()); | 41 window->layer()->RequestCopyOfOutput(request.Pass()); |
| 133 } | 42 } |
| 134 | 43 |
| 135 void GrabWindowSnapshotAndScaleAsync( | 44 void GrabWindowSnapshotAndScaleAsync( |
| 136 gfx::NativeWindow window, | 45 gfx::NativeWindow window, |
| 137 const gfx::Rect& source_rect, | 46 const gfx::Rect& source_rect, |
| 138 const gfx::Size& target_size, | 47 const gfx::Size& target_size, |
| 139 scoped_refptr<base::TaskRunner> background_task_runner, | 48 scoped_refptr<base::TaskRunner> background_task_runner, |
| 140 const GrabWindowSnapshotAsyncCallback& callback) { | 49 const GrabWindowSnapshotAsyncCallback& callback) { |
| 141 MakeAsyncCopyRequest(window, | 50 MakeAsyncCopyRequest(window, |
| 142 source_rect, | 51 source_rect, |
| 143 base::Bind(&ScaleCopyOutputResult, | 52 base::Bind(&SnapshotAsync::ScaleCopyOutputResult, |
| 144 callback, | 53 callback, |
| 145 target_size, | 54 target_size, |
| 146 background_task_runner)); | 55 background_task_runner)); |
| 147 } | 56 } |
| 148 | 57 |
| 149 void GrabWindowSnapshotAsync( | 58 void GrabWindowSnapshotAsync( |
| 150 gfx::NativeWindow window, | 59 gfx::NativeWindow window, |
| 151 const gfx::Rect& source_rect, | 60 const gfx::Rect& source_rect, |
| 152 scoped_refptr<base::TaskRunner> background_task_runner, | 61 scoped_refptr<base::TaskRunner> background_task_runner, |
| 153 const GrabWindowSnapshotAsyncPNGCallback& callback) { | 62 const GrabWindowSnapshotAsyncPNGCallback& callback) { |
| 154 MakeAsyncCopyRequest(window, | 63 MakeAsyncCopyRequest(window, |
| 155 source_rect, | 64 source_rect, |
| 156 base::Bind(&EncodeCopyOutputResult, | 65 base::Bind(&SnapshotAsync::EncodeCopyOutputResult, |
| 157 callback, | 66 callback, |
| 158 background_task_runner)); | 67 background_task_runner)); |
| 159 } | 68 } |
| 160 | 69 |
| 161 void GrabViewSnapshotAsync( | 70 void GrabViewSnapshotAsync( |
| 162 gfx::NativeView view, | 71 gfx::NativeView view, |
| 163 const gfx::Rect& source_rect, | 72 const gfx::Rect& source_rect, |
| 164 scoped_refptr<base::TaskRunner> background_task_runner, | 73 scoped_refptr<base::TaskRunner> background_task_runner, |
| 165 const GrabWindowSnapshotAsyncPNGCallback& callback) { | 74 const GrabWindowSnapshotAsyncPNGCallback& callback) { |
| 166 GrabWindowSnapshotAsync(view, source_rect, background_task_runner, callback); | 75 GrabWindowSnapshotAsync(view, source_rect, background_task_runner, callback); |
| 167 } | 76 } |
| 168 | 77 |
| 169 | 78 |
| 170 } // namespace ui | 79 } // namespace ui |
| OLD | NEW |