-
Notifications
You must be signed in to change notification settings - Fork 54
/
GraphicBuffer.cpp
160 lines (139 loc) · 4.92 KB
/
GraphicBuffer.cpp
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
152
153
154
155
156
157
158
159
160
#include "GraphicBuffer.h"
#include <string>
#include <cstdlib>
#include <iostream>
using std::string;
const int GRAPHICBUFFER_SIZE = 1024;
template<typename Func>
void setFuncPtr (Func*& funcPtr, const DynamicLibrary& lib, const string& symname)
{
funcPtr = reinterpret_cast<Func*>(lib.getFunctionPtr(symname.c_str()));
}
#if defined(__aarch64__)
# define CPU_ARM_64
#elif defined(__arm__) || defined(__ARM__) || defined(__ARM_NEON__) || defined(ARM_BUILD)
# define CPU_ARM
#elif defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
# define CPU_X86_64
#elif defined(__i386__) || defined(_M_X86) || defined(_M_IX86) || defined(X86_BUILD)
# define CPU_X86
#else
# warning "target CPU does not support ABI"
#endif
template <typename RT, typename T1, typename T2, typename T3, typename T4>
RT* callConstructor4 (void (*fptr)(), void* memory, T1 param1, T2 param2, T3 param3, T4 param4)
{
#if defined(CPU_ARM)
// C1 constructors return pointer
typedef RT* (*ABIFptr)(void*, T1, T2, T3, T4);
(void)((ABIFptr)fptr)(memory, param1, param2, param3, param4);
return reinterpret_cast<RT*>(memory);
#elif defined(CPU_ARM_64)
// C1 constructors return void
typedef void (*ABIFptr)(void*, T1, T2, T3, T4);
((ABIFptr)fptr)(memory, param1, param2, param3, param4);
return reinterpret_cast<RT*>(memory);
#elif defined(CPU_X86) || defined(CPU_X86_64)
// ctor returns void
typedef void (*ABIFptr)(void*, T1, T2, T3, T4);
((ABIFptr)fptr)(memory, param1, param2, param3, param4);
return reinterpret_cast<RT*>(memory);
#else
return nullptr;
#endif
}
template <typename T>
void callDestructor (void (*fptr)(), T* obj)
{
#if defined(CPU_ARM)
// D1 destructor returns ptr
typedef void* (*ABIFptr)(T* obj);
(void)((ABIFptr)fptr)(obj);
#elif defined(CPU_ARM_64)
// D1 destructor returns void
typedef void (*ABIFptr)(T* obj);
((ABIFptr)fptr)(obj);
#elif defined(CPU_X86) || defined(CPU_X86_64)
// dtor returns void
typedef void (*ABIFptr)(T* obj);
((ABIFptr)fptr)(obj);
#endif
}
template<typename T1, typename T2>
T1* pointerToOffset (T2* ptr, size_t bytes)
{
return reinterpret_cast<T1*>((uint8_t *)ptr + bytes);
}
static android::android_native_base_t* getAndroidNativeBase (android::GraphicBuffer* gb)
{
return pointerToOffset<android::android_native_base_t>(gb, 2 * sizeof(void *));
}
GraphicBuffer::GraphicBuffer(uint32_t width, uint32_t height, PixelFormat format, uint32_t usage):
library("libui.so")
{
setFuncPtr(functions.constructor, library, "_ZN7android13GraphicBufferC1Ejjij");
setFuncPtr(functions.destructor, library, "_ZN7android13GraphicBufferD1Ev");
setFuncPtr(functions.getNativeBuffer, library, "_ZNK7android13GraphicBuffer15getNativeBufferEv");
setFuncPtr(functions.lock, library, "_ZN7android13GraphicBuffer4lockEjPPv");
setFuncPtr(functions.unlock, library, "_ZN7android13GraphicBuffer6unlockEv");
setFuncPtr(functions.initCheck, library, "_ZNK7android13GraphicBuffer9initCheckEv");
// allocate memory for GraphicBuffer object
void *const memory = malloc(GRAPHICBUFFER_SIZE);
if (memory == nullptr) {
std::cerr << "Could not alloc for GraphicBuffer" << std::endl;
return;
}
try {
android::GraphicBuffer* const gb = callConstructor4<android::GraphicBuffer, uint32_t, uint32_t, PixelFormat, uint32_t>(
functions.constructor,
memory,
width,
height,
format,
usage
);
android::android_native_base_t* const base = getAndroidNativeBase(gb);
status_t ctorStatus = functions.initCheck(gb);
if (ctorStatus) {
// ctor failed
callDestructor<android::GraphicBuffer>(functions.destructor, gb);
std::cerr << "GraphicBuffer ctor failed, initCheck returned " << ctorStatus << std::endl;
}
// check object layout
if (base->magic != 0x5f626672u) // "_bfr"
std::cerr << "GraphicBuffer layout unexpected" << std::endl;
// check object version
const uint32_t expectedVersion = sizeof(void *) == 4 ? 96 : 168;
if (base->version != expectedVersion)
std::cerr << "GraphicBuffer version unexpected" << std::endl;
base->incRef(base);
impl = gb;
} catch (...) {
free(memory);
throw;
}
}
GraphicBuffer::~GraphicBuffer()
{
if (impl) {
android::android_native_base_t* const base = getAndroidNativeBase(impl);
base->decRef(base);
free(impl);
}
}
status_t GraphicBuffer::lock(uint32_t usage, void** vaddr)
{
return functions.lock(impl, usage, vaddr);
}
status_t GraphicBuffer::unlock()
{
return functions.unlock(impl);
}
ANativeWindowBuffer *GraphicBuffer::getNativeBuffer() const
{
return functions.getNativeBuffer(impl);
}
uint32_t GraphicBuffer::getStride() const
{
return ((android::android_native_buffer_t*)getNativeBuffer())->stride;
}