OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_async.h" | 5 #include "ui/snapshot/snapshot_async.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
11 #include "skia/ext/image_operations.h" | 11 #include "skia/ext/image_operations.h" |
12 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
13 #include "third_party/skia/include/core/SkPixelRef.h" | |
14 #include "ui/gfx/codec/png_codec.h" | |
15 #include "ui/gfx/image/image.h" | 13 #include "ui/gfx/image/image.h" |
16 #include "ui/gfx/image/image_skia.h" | 14 #include "ui/gfx/image/image_skia.h" |
17 #include "ui/gfx/skbitmap_operations.h" | 15 #include "ui/gfx/skbitmap_operations.h" |
18 | 16 |
19 namespace ui { | 17 namespace ui { |
20 | 18 |
21 namespace { | 19 namespace { |
22 | 20 |
23 void OnFrameScalingFinished(const GrabWindowSnapshotAsyncCallback& callback, | 21 void OnFrameScalingFinished(const GrabWindowSnapshotAsyncCallback& callback, |
24 const SkBitmap& scaled_bitmap) { | 22 const SkBitmap& scaled_bitmap) { |
25 callback.Run(gfx::Image(gfx::ImageSkia::CreateFrom1xBitmap(scaled_bitmap))); | 23 callback.Run(gfx::Image(gfx::ImageSkia::CreateFrom1xBitmap(scaled_bitmap))); |
26 } | 24 } |
27 | 25 |
28 SkBitmap ScaleBitmap(const SkBitmap& input_bitmap, | 26 SkBitmap ScaleBitmap(const SkBitmap& input_bitmap, |
29 const gfx::Size& target_size) { | 27 const gfx::Size& target_size) { |
30 return skia::ImageOperations::Resize(input_bitmap, | 28 return skia::ImageOperations::Resize(input_bitmap, |
31 skia::ImageOperations::RESIZE_GOOD, | 29 skia::ImageOperations::RESIZE_GOOD, |
32 target_size.width(), | 30 target_size.width(), |
33 target_size.height(), | 31 target_size.height(), |
34 static_cast<SkBitmap::Allocator*>(NULL)); | 32 static_cast<SkBitmap::Allocator*>(NULL)); |
35 } | 33 } |
36 | 34 |
37 scoped_refptr<base::RefCountedBytes> EncodeBitmap(const SkBitmap& bitmap) { | |
38 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); | |
39 SkAutoLockPixels lock(bitmap); | |
40 unsigned char* pixels = reinterpret_cast<unsigned char*>(bitmap.getPixels()); | |
41 #if SK_A32_SHIFT == 24 && SK_R32_SHIFT == 16 && SK_G32_SHIFT == 8 | |
42 gfx::PNGCodec::ColorFormat kColorFormat = gfx::PNGCodec::FORMAT_BGRA; | |
43 #elif SK_A32_SHIFT == 24 && SK_B32_SHIFT == 16 && SK_G32_SHIFT == 8 | |
44 gfx::PNGCodec::ColorFormat kColorFormat = gfx::PNGCodec::FORMAT_RGBA; | |
45 #else | |
46 #error Unknown color format | |
47 #endif | |
48 if (!gfx::PNGCodec::Encode(pixels, | |
49 kColorFormat, | |
50 gfx::Size(bitmap.width(), bitmap.height()), | |
51 base::checked_cast<int>(bitmap.rowBytes()), | |
52 true, | |
53 std::vector<gfx::PNGCodec::Comment>(), | |
54 &png_data->data())) { | |
55 return scoped_refptr<base::RefCountedBytes>(); | |
56 } | |
57 return png_data; | |
58 } | |
59 | |
60 } // namespace | 35 } // namespace |
61 | 36 |
62 void SnapshotAsync::ScaleCopyOutputResult( | 37 void SnapshotAsync::ScaleCopyOutputResult( |
63 const GrabWindowSnapshotAsyncCallback& callback, | 38 const GrabWindowSnapshotAsyncCallback& callback, |
64 const gfx::Size& target_size, | 39 const gfx::Size& target_size, |
65 scoped_refptr<base::TaskRunner> background_task_runner, | 40 scoped_refptr<base::TaskRunner> background_task_runner, |
66 std::unique_ptr<cc::CopyOutputResult> result) { | 41 std::unique_ptr<cc::CopyOutputResult> result) { |
67 if (result->IsEmpty()) { | 42 if (result->IsEmpty()) { |
68 callback.Run(gfx::Image()); | 43 callback.Run(gfx::Image()); |
69 return; | 44 return; |
70 } | 45 } |
71 | 46 |
72 // TODO(sergeyu): Potentially images can be scaled on GPU before reading it | 47 // TODO(sergeyu): Potentially images can be scaled on GPU before reading it |
73 // from GPU. Image scaling is implemented in content::GlHelper, but it's can't | 48 // from GPU. Image scaling is implemented in content::GlHelper, but it's can't |
74 // be used here because it's not in content/public. Move the scaling code | 49 // be used here because it's not in content/public. Move the scaling code |
75 // somewhere so that it can be reused here. | 50 // somewhere so that it can be reused here. |
76 base::PostTaskAndReplyWithResult( | 51 base::PostTaskAndReplyWithResult( |
77 background_task_runner.get(), | 52 background_task_runner.get(), |
78 FROM_HERE, | 53 FROM_HERE, |
79 base::Bind(ScaleBitmap, *result->TakeBitmap(), target_size), | 54 base::Bind(ScaleBitmap, *result->TakeBitmap(), target_size), |
80 base::Bind(&OnFrameScalingFinished, callback)); | 55 base::Bind(&OnFrameScalingFinished, callback)); |
81 } | 56 } |
82 | 57 |
83 void SnapshotAsync::EncodeCopyOutputResult( | 58 void SnapshotAsync::RunCallbackWithCopyOutputResult( |
84 const GrabWindowSnapshotAsyncPNGCallback& callback, | 59 const GrabWindowSnapshotAsyncCallback& callback, |
85 scoped_refptr<base::TaskRunner> background_task_runner, | |
86 std::unique_ptr<cc::CopyOutputResult> result) { | 60 std::unique_ptr<cc::CopyOutputResult> result) { |
87 if (result->IsEmpty()) { | 61 if (result->IsEmpty()) { |
88 callback.Run(scoped_refptr<base::RefCountedBytes>()); | 62 callback.Run(gfx::Image()); |
89 return; | 63 return; |
90 } | 64 } |
91 | 65 |
92 // TODO(sergeyu): Potentially images can be scaled on GPU before reading it | 66 callback.Run(gfx::Image::CreateFrom1xBitmap(*result->TakeBitmap())); |
93 // from GPU. Image scaling is implemented in content::GlHelper, but it's can't | |
94 // be used here because it's not in content/public. Move the scaling code | |
95 // somewhere so that it can be reused here. | |
96 base::PostTaskAndReplyWithResult( | |
97 background_task_runner.get(), | |
98 FROM_HERE, | |
99 base::Bind(EncodeBitmap, *result->TakeBitmap()), | |
100 callback); | |
101 } | 67 } |
102 | 68 |
103 } // namespace ui | 69 } // namespace ui |
OLD | NEW |