-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsGrallocConsumer.cpp
291 lines (247 loc) · 9.32 KB
/
rsGrallocConsumer.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
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright (C) 2013 The Android Open Source Project
*
* 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.
*/
#include "rsAllocation.h"
#include "rsContext.h"
#include "rsGrallocConsumer.h"
#include "rs_hal.h"
namespace android {
namespace renderscript {
GrallocConsumer::GrallocConsumer (const Context *rsc, Allocation *a, uint32_t numAlloc)
{
mCtx = rsc;
mAlloc = new Allocation *[numAlloc];
mAcquiredBuffer = new AcquiredBuffer[numAlloc];
isIdxUsed = new bool[numAlloc];
mAlloc[0] = a;
isIdxUsed[0] = true;
mNumAlloc = numAlloc;
uint32_t width = a->mHal.drvState.lod[0].dimX;
uint32_t height = a->mHal.drvState.lod[0].dimY;
if (height < 1) height = 1;
int32_t format = AIMAGE_FORMAT_RGBA_8888;
if (a->mHal.state.yuv) {
format = AIMAGE_FORMAT_YUV_420_888;
}
// GRALLOC_USAGE_RENDERSCRIPT
const uint64_t USAGE_RENDERSCRIPT = 0x00100000U;
uint64_t usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | USAGE_RENDERSCRIPT;
media_status_t ret = AImageReader_newWithUsage(
width, height, format, usage,
mNumAlloc, &mImgReader);
if (ret != AMEDIA_OK || mImgReader == nullptr) {
ALOGE("Error creating image reader. ret %d", ret);
}
ret = AImageReader_getWindow(mImgReader, &mNativeWindow);
if (ret != AMEDIA_OK || mNativeWindow == nullptr) {
ALOGE("Error creating native window. ret %d", ret);
}
mReaderCb = {this, GrallocConsumer::onFrameAvailable};
ret = AImageReader_setImageListener(mImgReader, &mReaderCb);
for (uint32_t i = 1; i < numAlloc; i++) {
isIdxUsed[i] = false;
}
}
GrallocConsumer::~GrallocConsumer() {
AImageReader_delete(mImgReader);
delete[] mAlloc;
delete[] mAcquiredBuffer;
delete[] isIdxUsed;
}
void GrallocConsumer::onFrameAvailable(void* obj, AImageReader* reader) {
GrallocConsumer* consumer = (GrallocConsumer *) obj;
for (uint32_t i = 0; i < consumer->mNumAlloc; i++) {
if (consumer->mAlloc[i] != nullptr) {
intptr_t ip = (intptr_t)(consumer->mAlloc[i]);
consumer->mCtx->sendMessageToClient(&ip,
RS_MESSAGE_TO_CLIENT_NEW_BUFFER, 0, sizeof(ip), true);
}
}
}
ANativeWindow* GrallocConsumer::getNativeWindow() {
return mNativeWindow;
}
media_status_t GrallocConsumer::lockNextBuffer(uint32_t idx) {
media_status_t ret;
if (idx >= mNumAlloc) {
ALOGE("Invalid buffer index: %d", idx);
return AMEDIA_ERROR_INVALID_PARAMETER;
}
if (mAcquiredBuffer[idx].mImg != nullptr) {
ret = unlockBuffer(idx);
if (ret != AMEDIA_OK) {
return ret;
}
}
ret = AImageReader_acquireNextImage(mImgReader, &(mAcquiredBuffer[idx].mImg));
if (ret != AMEDIA_OK || mAcquiredBuffer[idx].mImg == nullptr) {
ALOGE("%s: acquire image from reader %p failed! ret: %d, img %p",
__FUNCTION__, mImgReader, ret, mAcquiredBuffer[idx].mImg);
return ret;
}
AImage *img = mAcquiredBuffer[idx].mImg;
int32_t format = -1;
ret = AImage_getFormat(img, &format);
if (ret != AMEDIA_OK || format == -1) {
ALOGE("%s: get format for image %p failed! ret: %d, format %d",
__FUNCTION__, img, ret, format);
return ret;
}
if (format != AIMAGE_FORMAT_YUV_420_888 && format != AIMAGE_FORMAT_RGBA_8888) {
ALOGE("Format %d not supported", format);
return AMEDIA_ERROR_INVALID_OBJECT;
}
uint8_t *data = nullptr;
int dataLength = 0;
ret = AImage_getPlaneData(img, 0, &data, &dataLength);
if (ret != AMEDIA_OK || data == nullptr || dataLength <= 0) {
ALOGE("%s: get data for image %p failed! ret: %d, data %p, len %d",
__FUNCTION__, img, ret, data, dataLength);
return ret;
}
int64_t timestamp = -1;
ret = AImage_getTimestamp(img, ×tamp);
if (ret != AMEDIA_OK || timestamp == -1) {
ALOGE("%s: get timestamp for image %p failed! ret: %d",
__FUNCTION__, img, ret);
return ret;
}
int32_t rowstride = -1;
ret = AImage_getPlaneRowStride(img, 0, &rowstride);
if (ret != AMEDIA_OK || rowstride == -1) {
ALOGE("%s: get row stride for image %p failed! ret: %d, rowstride %d",
__FUNCTION__, img, ret, rowstride);
return ret;
}
AHardwareBuffer *hardwareBuffer = nullptr;
ret = AImage_getHardwareBuffer(img, &hardwareBuffer);
if (ret != AMEDIA_OK || hardwareBuffer == nullptr) {
ALOGE("%s: get hardware buffer for image %p failed! ret: %d",
__FUNCTION__, img, ret);
return ret;
}
mAcquiredBuffer[idx].mBufferPointer = data;
mAlloc[idx]->mHal.drvState.lod[0].mallocPtr = data;
mAlloc[idx]->mHal.drvState.lod[0].stride = rowstride;
mAlloc[idx]->mHal.state.nativeBuffer = hardwareBuffer;
mAlloc[idx]->mHal.state.timestamp = timestamp;
if (format == AIMAGE_FORMAT_YUV_420_888) {
const int yWidth = mAlloc[idx]->mHal.drvState.lod[0].dimX;
const int yHeight = mAlloc[idx]->mHal.drvState.lod[0].dimY;
const int cWidth = yWidth / 2;
const int cHeight = yHeight / 2;
uint8_t *uData = nullptr;
int uDataLength = 0;
ret = AImage_getPlaneData(img, 1, &uData, &uDataLength);
if (ret != AMEDIA_OK || uData == nullptr || uDataLength <= 0) {
ALOGE("%s: get U data for image %p failed! ret: %d, data %p, len %d",
__FUNCTION__, img, ret, uData, uDataLength);
return ret;
}
uint8_t *vData = nullptr;
int vDataLength = 0;
ret = AImage_getPlaneData(img, 2, &vData, &vDataLength);
if (ret != AMEDIA_OK || vData == nullptr || vDataLength <= 0) {
ALOGE("%s: get V data for image %p failed! ret: %d, data %p, len %d",
__FUNCTION__, img, ret, vData, vDataLength);
return ret;
}
int32_t uRowStride = -1;
ret = AImage_getPlaneRowStride(img, 1, &uRowStride);
if (ret != AMEDIA_OK || uRowStride == -1) {
ALOGE("%s: get U row stride for image %p failed! ret: %d, uRowStride %d",
__FUNCTION__, img, ret, uRowStride);
return ret;
}
int32_t vRowStride = -1;
ret = AImage_getPlaneRowStride(img, 2, &vRowStride);
if (ret != AMEDIA_OK || vRowStride == -1) {
ALOGE("%s: get V row stride for image %p failed! ret: %d, vRowStride %d",
__FUNCTION__, img, ret, vRowStride);
return ret;
}
int32_t uPixStride = -1;
ret = AImage_getPlanePixelStride(img, 1, &uPixStride);
if (ret != AMEDIA_OK || uPixStride == -1) {
ALOGE("%s: get U pixel stride for image %p failed! ret: %d, uPixStride %d",
__FUNCTION__, img, ret, uPixStride);
return ret;
}
mAlloc[idx]->mHal.drvState.lod[1].dimX = cWidth;
mAlloc[idx]->mHal.drvState.lod[1].dimY = cHeight;
mAlloc[idx]->mHal.drvState.lod[2].dimX = cWidth;
mAlloc[idx]->mHal.drvState.lod[2].dimY = cHeight;
mAlloc[idx]->mHal.drvState.lod[1].mallocPtr = uData;
mAlloc[idx]->mHal.drvState.lod[2].mallocPtr = vData;
mAlloc[idx]->mHal.drvState.lod[1].stride = uRowStride;
mAlloc[idx]->mHal.drvState.lod[2].stride = vRowStride;
mAlloc[idx]->mHal.drvState.yuv.shift = 1;
mAlloc[idx]->mHal.drvState.yuv.step = uPixStride;
mAlloc[idx]->mHal.drvState.lodCount = 3;
}
return AMEDIA_OK;
}
media_status_t GrallocConsumer::unlockBuffer(uint32_t idx) {
media_status_t ret;
if (idx >= mNumAlloc) {
ALOGE("Invalid buffer index: %d", idx);
return AMEDIA_ERROR_INVALID_PARAMETER;
}
if (mAcquiredBuffer[idx].mImg == nullptr) {
return AMEDIA_OK;
}
AImage_delete(mAcquiredBuffer[idx].mImg);
mAcquiredBuffer[idx].mImg = nullptr;
return AMEDIA_OK;
}
uint32_t GrallocConsumer::getNextAvailableIdx(Allocation *a) {
for (uint32_t i = 0; i < mNumAlloc; i++) {
if (isIdxUsed[i] == false) {
mAlloc[i] = a;
isIdxUsed[i] = true;
return i;
}
}
return mNumAlloc;
}
bool GrallocConsumer::releaseIdx(uint32_t idx) {
if (idx >= mNumAlloc) {
ALOGE("Invalid buffer index: %d", idx);
return false;
}
if (isIdxUsed[idx] == false) {
ALOGV("Buffer index already released: %d", idx);
return true;
}
media_status_t ret;
ret = unlockBuffer(idx);
if (ret != OK) {
ALOGE("Unable to unlock graphic buffer");
return false;
}
mAlloc[idx] = nullptr;
isIdxUsed[idx] = false;
return true;
}
bool GrallocConsumer::isActive() {
for (uint32_t i = 0; i < mNumAlloc; i++) {
if (isIdxUsed[i]) {
return true;
}
}
return false;
}
} // namespace renderscript
} // namespace android