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