forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandroid_image_reader_compat.cc
151 lines (122 loc) · 4.72 KB
/
android_image_reader_compat.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2018 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 "base/android/android_image_reader_compat.h"
#include <dlfcn.h>
#include "base/android/build_info.h"
#include "base/feature_list.h"
#include "base/logging.h"
#define LOAD_FUNCTION(lib, func) \
do { \
func##_ = reinterpret_cast<p##func>(dlsym(lib, #func)); \
if (!func##_) { \
DLOG(ERROR) << "Unable to load function " << #func; \
return false; \
} \
} while (0)
namespace base {
namespace android {
bool AndroidImageReader::disable_support_ = false;
AndroidImageReader& AndroidImageReader::GetInstance() {
// C++11 static local variable initialization is
// thread-safe.
static base::NoDestructor<AndroidImageReader> instance;
return *instance;
}
void AndroidImageReader::DisableSupport() {
disable_support_ = true;
}
bool AndroidImageReader::IsSupported() {
return !disable_support_ && is_supported_;
}
AndroidImageReader::AndroidImageReader() {
is_supported_ = LoadFunctions();
}
bool AndroidImageReader::LoadFunctions() {
// If the Chromium build requires __ANDROID_API__ >= 26 at some
// point in the future, we could directly use the global functions instead of
// dynamic loading. However, since this would be incompatible with pre-Oreo
// devices, this is unlikely to happen in the foreseeable future, so we use
// dynamic loading.
// Functions are not present for android version older than OREO
if (base::android::BuildInfo::GetInstance()->sdk_int() <
base::android::SDK_VERSION_OREO) {
return false;
}
void* libmediandk = dlopen("libmediandk.so", RTLD_NOW);
if (libmediandk == nullptr) {
LOG(ERROR) << "Couldnt open libmediandk.so";
return false;
}
LOAD_FUNCTION(libmediandk, AImage_delete);
LOAD_FUNCTION(libmediandk, AImage_deleteAsync);
LOAD_FUNCTION(libmediandk, AImage_getHardwareBuffer);
LOAD_FUNCTION(libmediandk, AImage_getWidth);
LOAD_FUNCTION(libmediandk, AImage_getHeight);
LOAD_FUNCTION(libmediandk, AImageReader_newWithUsage);
LOAD_FUNCTION(libmediandk, AImageReader_setImageListener);
LOAD_FUNCTION(libmediandk, AImageReader_delete);
LOAD_FUNCTION(libmediandk, AImageReader_getWindow);
LOAD_FUNCTION(libmediandk, AImageReader_acquireLatestImageAsync);
void* libandroid = dlopen("libandroid.so", RTLD_NOW);
if (libandroid == nullptr) {
LOG(ERROR) << "Couldnt open libandroid.so";
return false;
}
LOAD_FUNCTION(libandroid, ANativeWindow_toSurface);
return true;
}
void AndroidImageReader::AImage_delete(AImage* image) {
AImage_delete_(image);
}
void AndroidImageReader::AImage_deleteAsync(AImage* image, int releaseFenceFd) {
AImage_deleteAsync_(image, releaseFenceFd);
}
media_status_t AndroidImageReader::AImage_getHardwareBuffer(
const AImage* image,
AHardwareBuffer** buffer) {
return AImage_getHardwareBuffer_(image, buffer);
}
media_status_t AndroidImageReader::AImage_getWidth(const AImage* image,
int32_t* width) {
return AImage_getWidth_(image, width);
}
media_status_t AndroidImageReader::AImage_getHeight(const AImage* image,
int32_t* height) {
return AImage_getHeight_(image, height);
}
media_status_t AndroidImageReader::AImageReader_newWithUsage(
int32_t width,
int32_t height,
int32_t format,
uint64_t usage,
int32_t maxImages,
AImageReader** reader) {
return AImageReader_newWithUsage_(width, height, format, usage, maxImages,
reader);
}
media_status_t AndroidImageReader::AImageReader_setImageListener(
AImageReader* reader,
AImageReader_ImageListener* listener) {
return AImageReader_setImageListener_(reader, listener);
}
void AndroidImageReader::AImageReader_delete(AImageReader* reader) {
AImageReader_delete_(reader);
}
media_status_t AndroidImageReader::AImageReader_getWindow(
AImageReader* reader,
ANativeWindow** window) {
return AImageReader_getWindow_(reader, window);
}
media_status_t AndroidImageReader::AImageReader_acquireLatestImageAsync(
AImageReader* reader,
AImage** image,
int* acquireFenceFd) {
return AImageReader_acquireLatestImageAsync_(reader, image, acquireFenceFd);
}
jobject AndroidImageReader::ANativeWindow_toSurface(JNIEnv* env,
ANativeWindow* window) {
return ANativeWindow_toSurface_(env, window);
}
} // namespace android
} // namespace base