snapshot_async.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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
  3. // found in the LICENSE file.
  4. #include "ui/snapshot/snapshot_async.h"
  5. #include "base/bind.h"
  6. #include "base/location.h"
  7. #include "base/numerics/safe_conversions.h"
  8. #include "base/task/thread_pool.h"
  9. #include "skia/ext/image_operations.h"
  10. #include "third_party/skia/include/core/SkBitmap.h"
  11. #include "ui/gfx/image/image.h"
  12. #include "ui/gfx/image/image_skia.h"
  13. #include "ui/gfx/skbitmap_operations.h"
  14. namespace ui {
  15. namespace {
  16. void OnFrameScalingFinished(GrabWindowSnapshotAsyncCallback callback,
  17. const SkBitmap& scaled_bitmap) {
  18. std::move(callback).Run(gfx::Image::CreateFrom1xBitmap(scaled_bitmap));
  19. }
  20. SkBitmap ScaleBitmap(const SkBitmap& input_bitmap,
  21. const gfx::Size& target_size) {
  22. return skia::ImageOperations::Resize(input_bitmap,
  23. skia::ImageOperations::RESIZE_GOOD,
  24. target_size.width(),
  25. target_size.height(),
  26. static_cast<SkBitmap::Allocator*>(NULL));
  27. }
  28. } // namespace
  29. void SnapshotAsync::ScaleCopyOutputResult(
  30. GrabWindowSnapshotAsyncCallback callback,
  31. const gfx::Size& target_size,
  32. std::unique_ptr<viz::CopyOutputResult> result) {
  33. auto scoped_bitmap = result->ScopedAccessSkBitmap();
  34. auto bitmap = scoped_bitmap.GetOutScopedBitmap();
  35. if (!bitmap.readyToDraw()) {
  36. std::move(callback).Run(gfx::Image());
  37. return;
  38. }
  39. // TODO(sergeyu): Potentially images can be scaled on GPU before reading it
  40. // from GPU. Image scaling is implemented in content::GlHelper, but it's can't
  41. // be used here because it's not in content/public. Move the scaling code
  42. // somewhere so that it can be reused here.
  43. base::ThreadPool::PostTaskAndReplyWithResult(
  44. FROM_HERE, {base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
  45. base::BindOnce(ScaleBitmap, bitmap, target_size),
  46. base::BindOnce(&OnFrameScalingFinished, std::move(callback)));
  47. }
  48. void SnapshotAsync::RunCallbackWithCopyOutputResult(
  49. GrabWindowSnapshotAsyncCallback callback,
  50. std::unique_ptr<viz::CopyOutputResult> result) {
  51. auto scoped_bitmap = result->ScopedAccessSkBitmap();
  52. auto bitmap = scoped_bitmap.GetOutScopedBitmap();
  53. if (!bitmap.readyToDraw()) {
  54. std::move(callback).Run(gfx::Image());
  55. return;
  56. }
  57. std::move(callback).Run(gfx::Image::CreateFrom1xBitmap(bitmap));
  58. }
  59. } // namespace ui