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
3 // found in the LICENSE file.
4
5 #include "ui/snapshot/snapshot.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/task_runner_util.h"
10 #include "cc/output/copy_output_request.h"
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "ui/aura/window.h"
13 #include "ui/compositor/compositor.h"
14 #include "ui/compositor/dip_util.h"
15 #include "ui/compositor/layer.h"
16 #include "ui/snapshot/snapshot_async.h"
17
18 namespace ui {
19
GrabViewSnapshot(gfx::NativeView view,std::vector<unsigned char> * png_representation,const gfx::Rect & snapshot_bounds)20 bool GrabViewSnapshot(gfx::NativeView view,
21 std::vector<unsigned char>* png_representation,
22 const gfx::Rect& snapshot_bounds) {
23 return GrabWindowSnapshot(view, png_representation, snapshot_bounds);
24 }
25
GrabWindowSnapshot(gfx::NativeWindow window,std::vector<unsigned char> * png_representation,const gfx::Rect & snapshot_bounds)26 bool GrabWindowSnapshot(gfx::NativeWindow window,
27 std::vector<unsigned char>* png_representation,
28 const gfx::Rect& snapshot_bounds) {
29 // Not supported in Aura. Callers should fall back to the async version.
30 return false;
31 }
32
MakeAsyncCopyRequest(gfx::NativeWindow window,const gfx::Rect & source_rect,const cc::CopyOutputRequest::CopyOutputRequestCallback & callback)33 static void MakeAsyncCopyRequest(
34 gfx::NativeWindow window,
35 const gfx::Rect& source_rect,
36 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) {
37 scoped_ptr<cc::CopyOutputRequest> request =
38 cc::CopyOutputRequest::CreateBitmapRequest(callback);
39 request->set_area(source_rect);
40 window->layer()->RequestCopyOfOutput(request.Pass());
41 }
42
GrabWindowSnapshotAndScaleAsync(gfx::NativeWindow window,const gfx::Rect & source_rect,const gfx::Size & target_size,scoped_refptr<base::TaskRunner> background_task_runner,const GrabWindowSnapshotAsyncCallback & callback)43 void GrabWindowSnapshotAndScaleAsync(
44 gfx::NativeWindow window,
45 const gfx::Rect& source_rect,
46 const gfx::Size& target_size,
47 scoped_refptr<base::TaskRunner> background_task_runner,
48 const GrabWindowSnapshotAsyncCallback& callback) {
49 MakeAsyncCopyRequest(window,
50 source_rect,
51 base::Bind(&SnapshotAsync::ScaleCopyOutputResult,
52 callback,
53 target_size,
54 background_task_runner));
55 }
56
GrabWindowSnapshotAsync(gfx::NativeWindow window,const gfx::Rect & source_rect,scoped_refptr<base::TaskRunner> background_task_runner,const GrabWindowSnapshotAsyncPNGCallback & callback)57 void GrabWindowSnapshotAsync(
58 gfx::NativeWindow window,
59 const gfx::Rect& source_rect,
60 scoped_refptr<base::TaskRunner> background_task_runner,
61 const GrabWindowSnapshotAsyncPNGCallback& callback) {
62 MakeAsyncCopyRequest(window,
63 source_rect,
64 base::Bind(&SnapshotAsync::EncodeCopyOutputResult,
65 callback,
66 background_task_runner));
67 }
68
GrabViewSnapshotAsync(gfx::NativeView view,const gfx::Rect & source_rect,scoped_refptr<base::TaskRunner> background_task_runner,const GrabWindowSnapshotAsyncPNGCallback & callback)69 void GrabViewSnapshotAsync(
70 gfx::NativeView view,
71 const gfx::Rect& source_rect,
72 scoped_refptr<base::TaskRunner> background_task_runner,
73 const GrabWindowSnapshotAsyncPNGCallback& callback) {
74 GrabWindowSnapshotAsync(view, source_rect, background_task_runner, callback);
75 }
76
77
78 } // namespace ui
79