forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot_async.cc
70 lines (59 loc) · 2.5 KB
/
snapshot_async.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/snapshot/snapshot_async.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/numerics/safe_conversions.h"
#include "base/task/thread_pool.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/skbitmap_operations.h"
namespace ui {
namespace {
void OnFrameScalingFinished(GrabWindowSnapshotAsyncCallback callback,
const SkBitmap& scaled_bitmap) {
std::move(callback).Run(gfx::Image::CreateFrom1xBitmap(scaled_bitmap));
}
SkBitmap ScaleBitmap(const SkBitmap& input_bitmap,
const gfx::Size& target_size) {
return skia::ImageOperations::Resize(input_bitmap,
skia::ImageOperations::RESIZE_GOOD,
target_size.width(),
target_size.height(),
static_cast<SkBitmap::Allocator*>(NULL));
}
} // namespace
void SnapshotAsync::ScaleCopyOutputResult(
GrabWindowSnapshotAsyncCallback callback,
const gfx::Size& target_size,
std::unique_ptr<viz::CopyOutputResult> result) {
auto scoped_bitmap = result->ScopedAccessSkBitmap();
auto bitmap = scoped_bitmap.GetOutScopedBitmap();
if (!bitmap.readyToDraw()) {
std::move(callback).Run(gfx::Image());
return;
}
// TODO(sergeyu): Potentially images can be scaled on GPU before reading it
// from GPU. Image scaling is implemented in content::GlHelper, but it's can't
// be used here because it's not in content/public. Move the scaling code
// somewhere so that it can be reused here.
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(ScaleBitmap, bitmap, target_size),
base::BindOnce(&OnFrameScalingFinished, std::move(callback)));
}
void SnapshotAsync::RunCallbackWithCopyOutputResult(
GrabWindowSnapshotAsyncCallback callback,
std::unique_ptr<viz::CopyOutputResult> result) {
auto scoped_bitmap = result->ScopedAccessSkBitmap();
auto bitmap = scoped_bitmap.GetOutScopedBitmap();
if (!bitmap.readyToDraw()) {
std::move(callback).Run(gfx::Image());
return;
}
std::move(callback).Run(gfx::Image::CreateFrom1xBitmap(bitmap));
}
} // namespace ui