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
71
72
73
74
75
76
77
78
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/data_decoder/public/cpp/decode_image.h"
#include <utility>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/data_decoder/public/mojom/constants.mojom.h"
#include "services/service_manager/public/cpp/connector.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace data_decoder {
namespace {
// Helper callback which owns a mojo::Remote<ImageDecoder> until invoked. This
// keeps the ImageDecoder pipe open just long enough to dispatch a reply, at
// which point the reply is forwarded to the wrapped |callback|.
void OnDecodeImage(mojo::Remote<mojom::ImageDecoder> decoder,
mojom::ImageDecoder::DecodeImageCallback callback,
const SkBitmap& bitmap) {
std::move(callback).Run(bitmap);
}
void OnDecodeImages(mojo::Remote<mojom::ImageDecoder> decoder,
mojom::ImageDecoder::DecodeAnimationCallback callback,
std::vector<mojom::AnimationFramePtr> bitmaps) {
std::move(callback).Run(std::move(bitmaps));
}
} // namespace
void DecodeImage(service_manager::Connector* connector,
const std::vector<uint8_t>& encoded_bytes,
mojom::ImageCodec codec,
bool shrink_to_fit,
uint64_t max_size_in_bytes,
const gfx::Size& desired_image_frame_size,
mojom::ImageDecoder::DecodeImageCallback callback) {
mojo::Remote<mojom::ImageDecoder> decoder;
connector->Connect(mojom::kServiceName, decoder.BindNewPipeAndPassReceiver());
// |call_once| runs |callback| on its first invocation.
auto call_once = base::AdaptCallbackForRepeating(std::move(callback));
decoder.set_disconnect_handler(base::Bind(call_once, SkBitmap()));
mojom::ImageDecoder* raw_decoder = decoder.get();
raw_decoder->DecodeImage(
encoded_bytes, codec, shrink_to_fit, max_size_in_bytes,
desired_image_frame_size,
base::BindOnce(&OnDecodeImage, std::move(decoder), std::move(call_once)));
}
void DecodeAnimation(service_manager::Connector* connector,
const std::vector<uint8_t>& encoded_bytes,
bool shrink_to_fit,
uint64_t max_size_in_bytes,
mojom::ImageDecoder::DecodeAnimationCallback callback) {
mojo::Remote<mojom::ImageDecoder> decoder;
connector->Connect(mojom::kServiceName, decoder.BindNewPipeAndPassReceiver());
// |call_once| runs |callback| on its first invocation.
auto call_once = base::AdaptCallbackForRepeating(std::move(callback));
decoder.set_disconnect_handler(base::Bind(
call_once, base::Passed(std::vector<mojom::AnimationFramePtr>())));
mojom::ImageDecoder* raw_decoder = decoder.get();
raw_decoder->DecodeAnimation(
encoded_bytes, shrink_to_fit, max_size_in_bytes,
base::BindOnce(&OnDecodeImages, std::move(decoder),
std::move(call_once)));
}
} // namespace data_decoder
|