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/logging.h" | 8 #include "base/logging.h" |
8 #include "base/safe_numerics.h" | 9 #include "base/safe_numerics.h" |
| 10 #include "base/task_runner_util.h" |
| 11 #include "cc/output/copy_output_request.h" |
| 12 #include "cc/output/copy_output_result.h" |
| 13 #include "skia/ext/image_operations.h" |
9 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
10 #include "third_party/skia/include/core/SkPixelRef.h" | 15 #include "third_party/skia/include/core/SkPixelRef.h" |
11 #include "ui/aura/root_window.h" | 16 #include "ui/aura/root_window.h" |
12 #include "ui/aura/window.h" | 17 #include "ui/aura/window.h" |
13 #include "ui/compositor/compositor.h" | 18 #include "ui/compositor/compositor.h" |
14 #include "ui/compositor/dip_util.h" | 19 #include "ui/compositor/dip_util.h" |
15 #include "ui/compositor/layer.h" | 20 #include "ui/compositor/layer.h" |
16 #include "ui/gfx/codec/png_codec.h" | 21 #include "ui/gfx/codec/png_codec.h" |
17 #include "ui/gfx/display.h" | 22 #include "ui/gfx/display.h" |
| 23 #include "ui/gfx/image/image.h" |
| 24 #include "ui/gfx/image/image_skia.h" |
18 #include "ui/gfx/rect.h" | 25 #include "ui/gfx/rect.h" |
19 #include "ui/gfx/rect_conversions.h" | 26 #include "ui/gfx/rect_conversions.h" |
20 #include "ui/gfx/rect_f.h" | 27 #include "ui/gfx/rect_f.h" |
21 #include "ui/gfx/screen.h" | 28 #include "ui/gfx/screen.h" |
22 #include "ui/gfx/skbitmap_operations.h" | 29 #include "ui/gfx/skbitmap_operations.h" |
23 #include "ui/gfx/transform.h" | 30 #include "ui/gfx/transform.h" |
24 | 31 |
25 namespace ui { | 32 namespace ui { |
26 | 33 |
27 bool GrabViewSnapshot(gfx::NativeView view, | 34 bool GrabViewSnapshot(gfx::NativeView view, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 unsigned char* pixels = reinterpret_cast<unsigned char*>( | 87 unsigned char* pixels = reinterpret_cast<unsigned char*>( |
81 bitmap.pixelRef()->pixels()); | 88 bitmap.pixelRef()->pixels()); |
82 return gfx::PNGCodec::Encode( | 89 return gfx::PNGCodec::Encode( |
83 pixels, gfx::PNGCodec::FORMAT_BGRA, | 90 pixels, gfx::PNGCodec::FORMAT_BGRA, |
84 gfx::Size(bitmap.width(), bitmap.height()), | 91 gfx::Size(bitmap.width(), bitmap.height()), |
85 base::checked_numeric_cast<int>(bitmap.rowBytes()), | 92 base::checked_numeric_cast<int>(bitmap.rowBytes()), |
86 true, std::vector<gfx::PNGCodec::Comment>(), | 93 true, std::vector<gfx::PNGCodec::Comment>(), |
87 png_representation); | 94 png_representation); |
88 } | 95 } |
89 | 96 |
| 97 void OnFrameScalingFinished( |
| 98 const GrapWindowSnapshotAsyncCallback& callback, |
| 99 const SkBitmap& scaled_bitmap) { |
| 100 callback.Run(gfx::Image(gfx::ImageSkia::CreateFrom1xBitmap(scaled_bitmap))); |
| 101 } |
| 102 |
| 103 void ScaleCopyOutputResult( |
| 104 const GrapWindowSnapshotAsyncCallback& callback, |
| 105 const gfx::Size& target_size, |
| 106 scoped_refptr<base::TaskRunner> background_task_runner, |
| 107 scoped_ptr<cc::CopyOutputResult> result) { |
| 108 if (result->IsEmpty()) { |
| 109 callback.Run(gfx::Image()); |
| 110 return; |
| 111 } |
| 112 |
| 113 // There are two overrides for skia::ImageOperations::Resize(), so we need get |
| 114 // pointer to the right override explicitly (otherwise the base::Bind() call |
| 115 // below won't compile). |
| 116 SkBitmap (*resize_function)(const SkBitmap&, |
| 117 skia::ImageOperations::ResizeMethod, int, int, |
| 118 SkBitmap::Allocator* allocator) = |
| 119 &skia::ImageOperations::Resize; |
| 120 |
| 121 // TODO(sergeyu): Potentially images can be scaled on GPU before reading it |
| 122 // from GPU. Image scaling is implemented in content::GlHelper, but it's can't |
| 123 // be used here because it's not in content/public. Move the scaling code |
| 124 // somewhere so that it can be reused here. |
| 125 base::PostTaskAndReplyWithResult( |
| 126 background_task_runner, FROM_HERE, |
| 127 base::Bind(resize_function, *result->TakeBitmap(), |
| 128 skia::ImageOperations::RESIZE_GOOD, |
| 129 target_size.width(), target_size.height(), |
| 130 static_cast<SkBitmap::Allocator*>(NULL)), |
| 131 base::Bind(&OnFrameScalingFinished, callback)); |
| 132 } |
| 133 |
| 134 SNAPSHOT_EXPORT void GrapWindowSnapshotAsync( |
| 135 gfx::NativeWindow window, |
| 136 const gfx::Rect& source_rect, |
| 137 const gfx::Size& target_size, |
| 138 scoped_refptr<base::TaskRunner> background_task_runner, |
| 139 const GrapWindowSnapshotAsyncCallback& callback) { |
| 140 scoped_ptr<cc::CopyOutputRequest> request = |
| 141 cc::CopyOutputRequest::CreateBitmapRequest( |
| 142 base::Bind(&ScaleCopyOutputResult, callback, target_size, |
| 143 background_task_runner)); |
| 144 request->set_area(ui::ConvertRectToPixel(window->layer(), source_rect)); |
| 145 window->layer()->RequestCopyOfOutput(request.Pass()); |
| 146 } |
| 147 |
90 } // namespace ui | 148 } // namespace ui |
OLD | NEW |