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" |
13 #include "ui/gfx/image/image.h" | 15 #include "ui/gfx/image/image.h" |
14 #include "ui/gfx/image/image_skia.h" | 16 #include "ui/gfx/image/image_skia.h" |
15 #include "ui/gfx/skbitmap_operations.h" | 17 #include "ui/gfx/skbitmap_operations.h" |
16 | 18 |
17 namespace ui { | 19 namespace ui { |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 void OnFrameScalingFinished(const GrabWindowSnapshotAsyncCallback& callback, | 23 void OnFrameScalingFinished(const GrabWindowSnapshotAsyncCallback& callback, |
22 const SkBitmap& scaled_bitmap) { | 24 const SkBitmap& scaled_bitmap) { |
23 callback.Run(gfx::Image(gfx::ImageSkia::CreateFrom1xBitmap(scaled_bitmap))); | 25 callback.Run(gfx::Image(gfx::ImageSkia::CreateFrom1xBitmap(scaled_bitmap))); |
24 } | 26 } |
25 | 27 |
26 SkBitmap ScaleBitmap(const SkBitmap& input_bitmap, | 28 SkBitmap ScaleBitmap(const SkBitmap& input_bitmap, |
27 const gfx::Size& target_size) { | 29 const gfx::Size& target_size) { |
28 return skia::ImageOperations::Resize(input_bitmap, | 30 return skia::ImageOperations::Resize(input_bitmap, |
29 skia::ImageOperations::RESIZE_GOOD, | 31 skia::ImageOperations::RESIZE_GOOD, |
30 target_size.width(), | 32 target_size.width(), |
31 target_size.height(), | 33 target_size.height(), |
32 static_cast<SkBitmap::Allocator*>(NULL)); | 34 static_cast<SkBitmap::Allocator*>(NULL)); |
33 } | 35 } |
34 | 36 |
| 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 |
35 } // namespace | 60 } // namespace |
36 | 61 |
37 void SnapshotAsync::ScaleCopyOutputResult( | 62 void SnapshotAsync::ScaleCopyOutputResult( |
38 const GrabWindowSnapshotAsyncCallback& callback, | 63 const GrabWindowSnapshotAsyncCallback& callback, |
39 const gfx::Size& target_size, | 64 const gfx::Size& target_size, |
40 scoped_refptr<base::TaskRunner> background_task_runner, | 65 scoped_refptr<base::TaskRunner> background_task_runner, |
41 std::unique_ptr<cc::CopyOutputResult> result) { | 66 std::unique_ptr<cc::CopyOutputResult> result) { |
42 if (result->IsEmpty()) { | 67 if (result->IsEmpty()) { |
43 callback.Run(gfx::Image()); | 68 callback.Run(gfx::Image()); |
44 return; | 69 return; |
45 } | 70 } |
46 | 71 |
47 // TODO(sergeyu): Potentially images can be scaled on GPU before reading it | 72 // TODO(sergeyu): Potentially images can be scaled on GPU before reading it |
48 // from GPU. Image scaling is implemented in content::GlHelper, but it's can't | 73 // from GPU. Image scaling is implemented in content::GlHelper, but it's can't |
49 // be used here because it's not in content/public. Move the scaling code | 74 // be used here because it's not in content/public. Move the scaling code |
50 // somewhere so that it can be reused here. | 75 // somewhere so that it can be reused here. |
51 base::PostTaskAndReplyWithResult( | 76 base::PostTaskAndReplyWithResult( |
52 background_task_runner.get(), | 77 background_task_runner.get(), |
53 FROM_HERE, | 78 FROM_HERE, |
54 base::Bind(ScaleBitmap, *result->TakeBitmap(), target_size), | 79 base::Bind(ScaleBitmap, *result->TakeBitmap(), target_size), |
55 base::Bind(&OnFrameScalingFinished, callback)); | 80 base::Bind(&OnFrameScalingFinished, callback)); |
56 } | 81 } |
57 | 82 |
58 void SnapshotAsync::RunCallbackWithCopyOutputResult( | 83 void SnapshotAsync::EncodeCopyOutputResult( |
59 const GrabWindowSnapshotAsyncCallback& callback, | 84 const GrabWindowSnapshotAsyncPNGCallback& callback, |
| 85 scoped_refptr<base::TaskRunner> background_task_runner, |
60 std::unique_ptr<cc::CopyOutputResult> result) { | 86 std::unique_ptr<cc::CopyOutputResult> result) { |
61 if (result->IsEmpty()) { | 87 if (result->IsEmpty()) { |
62 callback.Run(gfx::Image()); | 88 callback.Run(scoped_refptr<base::RefCountedBytes>()); |
63 return; | 89 return; |
64 } | 90 } |
65 | 91 |
66 callback.Run(gfx::Image::CreateFrom1xBitmap(*result->TakeBitmap())); | 92 // TODO(sergeyu): Potentially images can be scaled on GPU before reading it |
| 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); |
67 } | 101 } |
68 | 102 |
69 } // namespace ui | 103 } // namespace ui |
OLD | NEW |