forked from sailfishos/droidmedia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
droidmediabuffer.cpp
280 lines (235 loc) · 7.05 KB
/
droidmediabuffer.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright (C) 2014-2015 Jolla Ltd.
* Copyright (C) 2021 Open Mobile Platform LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Authored by: Mohammed Hassan <[email protected]>
*/
#include "droidmediabuffer.h"
#include "private.h"
#include <cutils/atomic.h>
#undef LOG_TAG
#define LOG_TAG "DroidMediaBuffer"
_DroidMediaBuffer::_DroidMediaBuffer(DroidMediaBufferItem& buffer,
android::sp<DroidMediaBufferQueue> queue) :
m_buffer(buffer.mGraphicBuffer),
m_queue(queue),
m_refCount(0),
m_transform(buffer.mTransform),
m_scalingMode(buffer.mScalingMode),
m_timestamp(buffer.mTimestamp),
m_frameNumber(buffer.mFrameNumber),
m_crop(buffer.mCrop),
#if ANDROID_MAJOR >= 6
m_slot(buffer.mSlot),
#else
m_slot(buffer.mBuf),
#endif
m_userData(0)
{
width = buffer.mGraphicBuffer->width;
height = buffer.mGraphicBuffer->height;
stride = buffer.mGraphicBuffer->stride;
format = buffer.mGraphicBuffer->format;
usage = buffer.mGraphicBuffer->usage;
handle = buffer.mGraphicBuffer->handle;
common.incRef = incRef;
common.decRef = decRef;
}
_DroidMediaBuffer::_DroidMediaBuffer(android::sp<android::GraphicBuffer>& buffer) :
m_buffer(buffer),
m_refCount(0),
m_transform(-1),
m_scalingMode(-1),
m_timestamp(-1),
m_frameNumber(-1),
m_slot(-1),
m_userData(0)
{
width = m_buffer->width;
height = m_buffer->height;
stride = m_buffer->stride;
format = m_buffer->format;
usage = m_buffer->usage;
handle = m_buffer->handle;
common.incRef = incRef;
common.decRef = decRef;
}
_DroidMediaBuffer::~_DroidMediaBuffer()
{
}
void _DroidMediaBuffer::incRef(struct android_native_base_t* base)
{
DroidMediaBuffer * const buffer = reinterpret_cast<DroidMediaBuffer *>(base);
android_atomic_inc(&buffer->m_refCount);
}
void _DroidMediaBuffer::decRef(struct android_native_base_t* base)
{
DroidMediaBuffer * const buffer = reinterpret_cast<DroidMediaBuffer *>(base);
if (android_atomic_dec(&buffer->m_refCount) == 1) {
delete buffer;
}
}
void _DroidMediaBuffer::update(const DroidMediaBufferItem& buffer)
{
m_transform = buffer.mTransform;
m_scalingMode = buffer.mScalingMode;
m_timestamp = buffer.mTimestamp;
m_frameNumber = buffer.mFrameNumber;
m_crop = buffer.mCrop;
}
extern "C" {
DroidMediaBuffer *droid_media_buffer_create(uint32_t w, uint32_t h,
uint32_t format)
{
android::sp<android::GraphicBuffer>
buffer(new android::GraphicBuffer(w, h, format,
android::GraphicBuffer::USAGE_HW_TEXTURE));
android::status_t err = buffer->initCheck();
if (err != android::NO_ERROR) {
ALOGE("Error 0x%x allocating buffer", -err);
buffer.clear();
return NULL;
}
DroidMediaBuffer *droidBuffer = new DroidMediaBuffer(buffer);
droidBuffer->incStrong(0);
return droidBuffer;
}
void droid_media_buffer_destroy(DroidMediaBuffer *buffer)
{
buffer->decStrong(0);
}
void droid_media_buffer_set_user_data(DroidMediaBuffer *buffer, void *data)
{
buffer->m_userData = data;
}
void *droid_media_buffer_get_user_data(DroidMediaBuffer *buffer)
{
return buffer->m_userData;
}
void droid_media_buffer_release(DroidMediaBuffer *buffer,
EGLDisplay display, EGLSyncKHR fence)
{
if (buffer->m_queue == NULL) {
return;
}
buffer->m_queue->releaseMediaBuffer(buffer, display, fence);
}
void *droid_media_buffer_lock(DroidMediaBuffer *buffer, uint32_t flags)
{
int usage = 0;
void *addr = NULL;
android::status_t err;
if (flags & DROID_MEDIA_BUFFER_LOCK_READ) {
usage |= android::GraphicBuffer::USAGE_SW_READ_RARELY;
}
if (flags & DROID_MEDIA_BUFFER_LOCK_WRITE) {
usage |= android::GraphicBuffer::USAGE_SW_WRITE_RARELY;
}
err = buffer->m_buffer->lock(usage, &addr);
if (err != android::NO_ERROR) {
ALOGE("Error 0x%x locking buffer", -err);
return NULL;
} else {
return addr;
}
}
bool droid_media_buffer_lock_ycbcr(DroidMediaBuffer *buffer,
uint32_t flags,
DroidMediaBufferYCbCr *ycbcr)
{
int usage = 0;
android_ycbcr droid_ycbcr;
android::status_t err;
if (!ycbcr) {
ALOGE("No buffer for ycbcr data provided");
return false;
}
if (flags & DROID_MEDIA_BUFFER_LOCK_READ) {
usage |= android::GraphicBuffer::USAGE_SW_READ_RARELY;
}
if (flags & DROID_MEDIA_BUFFER_LOCK_WRITE) {
usage |= android::GraphicBuffer::USAGE_SW_WRITE_RARELY;
}
err = buffer->m_buffer->lockYCbCr(usage, &droid_ycbcr);
if (err != android::NO_ERROR) {
ALOGE("Error 0x%x locking buffer", -err);
return false;
} else {
ycbcr->y = droid_ycbcr.y;
ycbcr->cb = droid_ycbcr.cb;
ycbcr->cr = droid_ycbcr.cr;
ycbcr->ystride = droid_ycbcr.ystride;
ycbcr->cstride = droid_ycbcr.cstride;
ycbcr->chroma_step = droid_ycbcr.chroma_step;
return true;
}
}
void droid_media_buffer_unlock(DroidMediaBuffer *buffer)
{
android::status_t err = buffer->m_buffer->unlock();
if (err != android::NO_ERROR) {
ALOGE("Error 0x%x unlocking buffer", -err);
}
}
uint32_t droid_media_buffer_get_transform(DroidMediaBuffer * buffer)
{
return buffer->m_transform;
}
uint32_t droid_media_buffer_get_scaling_mode(DroidMediaBuffer * buffer)
{
return buffer->m_scalingMode;
}
int64_t droid_media_buffer_get_timestamp(DroidMediaBuffer * buffer)
{
return buffer->m_timestamp;
}
uint64_t droid_media_buffer_get_frame_number(DroidMediaBuffer * buffer)
{
return buffer->m_frameNumber;
}
DroidMediaRect droid_media_buffer_get_crop_rect(DroidMediaBuffer * buffer)
{
DroidMediaRect rect;
rect.left = buffer->m_crop.left;
rect.right = buffer->m_crop.right;
rect.top = buffer->m_crop.top;
rect.bottom = buffer->m_crop.bottom;
return rect;
}
uint32_t droid_media_buffer_get_width(DroidMediaBuffer * buffer)
{
return buffer->width;
}
uint32_t droid_media_buffer_get_height(DroidMediaBuffer * buffer)
{
return buffer->height;
}
const void *droid_media_buffer_get_handle(DroidMediaBuffer *buffer)
{
return buffer->handle;
}
void droid_media_buffer_get_info(DroidMediaBuffer *buffer, DroidMediaBufferInfo *info)
{
info->width = buffer->width;
info->height = buffer->height;
info->transform = buffer->m_transform;
info->scaling_mode = buffer->m_scalingMode;
info->timestamp = buffer->m_timestamp;
info->frame_number = buffer->m_frameNumber;
info->crop_rect = droid_media_buffer_get_crop_rect(buffer);
info->format = buffer->format;
info->stride = buffer->stride;
}
};