summaryrefslogtreecommitdiff
path: root/libs/gui/include/gui/bufferqueue/1.0/WGraphicBufferProducer.h
blob: 004d87574aeaf03881a3c3364f8634a516bb3cb7 (plain)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Copyright 2016, 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.
 */

#ifndef ANDROID_HARDWARE_GRAPHICS_BUFFERQUEUE_V1_0_WGRAPHICBUFFERPRODUCER_H_
#define ANDROID_HARDWARE_GRAPHICS_BUFFERQUEUE_V1_0_WGRAPHICBUFFERPRODUCER_H_

#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>

#include <binder/Binder.h>
#include <gui/IGraphicBufferProducer.h>
#include <gui/IProducerListener.h>
#include <gui/bufferqueue/1.0/Conversion.h>
#include <gui/bufferqueue/1.0/WProducerListener.h>
#include <system/window.h>

#include <android/hardware/graphics/bufferqueue/1.0/IGraphicBufferProducer.h>

namespace android {

using ::android::hardware::media::V1_0::AnwBuffer;
using ::android::hidl::base::V1_0::IBase;
using ::android::hardware::hidl_array;
using ::android::hardware::hidl_handle;
using ::android::hardware::hidl_memory;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;

typedef ::android::hardware::graphics::bufferqueue::V1_0::
        IGraphicBufferProducer HGraphicBufferProducer;
typedef ::android::hardware::graphics::bufferqueue::V1_0::
        IProducerListener HProducerListener;

typedef ::android::IGraphicBufferProducer BGraphicBufferProducer;
typedef ::android::IProducerListener BProducerListener;

#ifndef LOG
struct LOG_stub {
    template <typename T>
    LOG_stub& operator<<(const T&) {
        return *this;
    }
};

#define LOG(x) LOG_stub()
#endif

// Instantiate only if HGraphicBufferProducer is base of BASE.
template <typename BASE,
          typename = typename std::enable_if<std::is_base_of<HGraphicBufferProducer, BASE>::value>::type>
struct TWGraphicBufferProducer : public BASE {
    TWGraphicBufferProducer(sp<BGraphicBufferProducer> const& base) : mBase(base) {}
    Return<void> requestBuffer(int32_t slot, HGraphicBufferProducer::requestBuffer_cb _hidl_cb) override {
        sp<GraphicBuffer> buf;
        status_t status = mBase->requestBuffer(slot, &buf);
        AnwBuffer anwBuffer{};
        if (buf != nullptr) {
            ::android::conversion::wrapAs(&anwBuffer, *buf);
        }
        _hidl_cb(static_cast<int32_t>(status), anwBuffer);
        return Void();
    }

    Return<int32_t> setMaxDequeuedBufferCount(int32_t maxDequeuedBuffers) override {
        return static_cast<int32_t>(mBase->setMaxDequeuedBufferCount(
                static_cast<int>(maxDequeuedBuffers)));
    }

    Return<int32_t> setAsyncMode(bool async) override {
        return static_cast<int32_t>(mBase->setAsyncMode(async));
    }

    Return<void> dequeueBuffer(
            uint32_t width, uint32_t height,
            ::android::hardware::graphics::common::V1_0::PixelFormat format, uint32_t usage,
            bool getFrameTimestamps, HGraphicBufferProducer::dequeueBuffer_cb _hidl_cb) override {
        int slot{};
        sp<Fence> fence;
        ::android::FrameEventHistoryDelta outTimestamps;
        status_t status = mBase->dequeueBuffer(
            &slot, &fence, width, height,
            static_cast<::android::PixelFormat>(format), usage, nullptr,
            getFrameTimestamps ? &outTimestamps : nullptr);
        hidl_handle tFence{};
        HGraphicBufferProducer::FrameEventHistoryDelta tOutTimestamps{};

        native_handle_t* nh = nullptr;
        if ((fence == nullptr) || !::android::conversion::wrapAs(&tFence, &nh, *fence)) {
            LOG(ERROR) << "TWGraphicBufferProducer::dequeueBuffer - "
                    "Invalid output fence";
            _hidl_cb(static_cast<int32_t>(status),
                     static_cast<int32_t>(slot),
                     tFence,
                     tOutTimestamps);
            return Void();
        }
        std::vector<std::vector<native_handle_t*> > nhAA;
        if (getFrameTimestamps && !::android::conversion::wrapAs(&tOutTimestamps, &nhAA, outTimestamps)) {
            LOG(ERROR) << "TWGraphicBufferProducer::dequeueBuffer - "
                    "Invalid output timestamps";
            _hidl_cb(static_cast<int32_t>(status),
                     static_cast<int32_t>(slot),
                     tFence,
                     tOutTimestamps);
            native_handle_delete(nh);
            return Void();
        }

        _hidl_cb(static_cast<int32_t>(status),
                static_cast<int32_t>(slot),
                tFence,
                tOutTimestamps);
        native_handle_delete(nh);
        if (getFrameTimestamps) {
            for (auto& nhA : nhAA) {
                for (auto& handle : nhA) {
                    native_handle_delete(handle);
                }
            }
        }
        return Void();
    }

    Return<int32_t> detachBuffer(int32_t slot) override {
        return static_cast<int32_t>(mBase->detachBuffer(slot));
    }

    Return<void> detachNextBuffer(HGraphicBufferProducer::detachNextBuffer_cb _hidl_cb) override {
        sp<GraphicBuffer> outBuffer;
        sp<Fence> outFence;
        status_t status = mBase->detachNextBuffer(&outBuffer, &outFence);
        AnwBuffer tBuffer{};
        hidl_handle tFence{};

        if (outBuffer == nullptr) {
            LOG(ERROR) << "TWGraphicBufferProducer::detachNextBuffer - "
                    "Invalid output buffer";
            _hidl_cb(static_cast<int32_t>(status), tBuffer, tFence);
            return Void();
        }
        ::android::conversion::wrapAs(&tBuffer, *outBuffer);
        native_handle_t* nh = nullptr;
        if ((outFence != nullptr) && !::android::conversion::wrapAs(&tFence, &nh, *outFence)) {
            LOG(ERROR) << "TWGraphicBufferProducer::detachNextBuffer - "
                    "Invalid output fence";
            _hidl_cb(static_cast<int32_t>(status), tBuffer, tFence);
            return Void();
        }

        _hidl_cb(static_cast<int32_t>(status), tBuffer, tFence);
        native_handle_delete(nh);
        return Void();
    }

    Return<void> attachBuffer(const AnwBuffer& buffer, HGraphicBufferProducer::attachBuffer_cb _hidl_cb) override {
        int outSlot;
        sp<GraphicBuffer> lBuffer = new GraphicBuffer();
        if (!::android::conversion::convertTo(lBuffer.get(), buffer)) {
            LOG(ERROR) << "TWGraphicBufferProducer::attachBuffer - "
                    "Invalid input native window buffer";
            _hidl_cb(static_cast<int32_t>(BAD_VALUE), -1);
            return Void();
        }
        status_t status = mBase->attachBuffer(&outSlot, lBuffer);

        _hidl_cb(static_cast<int32_t>(status), static_cast<int32_t>(outSlot));
        return Void();
    }

    Return<void> queueBuffer(
            int32_t slot, const HGraphicBufferProducer::QueueBufferInput& input,
            HGraphicBufferProducer::queueBuffer_cb _hidl_cb) override {
        HGraphicBufferProducer::QueueBufferOutput tOutput{};
        BGraphicBufferProducer::QueueBufferInput lInput(
                0, false, HAL_DATASPACE_UNKNOWN,
                ::android::Rect(0, 0, 1, 1),
                NATIVE_WINDOW_SCALING_MODE_FREEZE,
                0, ::android::Fence::NO_FENCE);
        if (!::android::conversion::convertTo(&lInput, input)) {
            LOG(ERROR) << "TWGraphicBufferProducer::queueBuffer - "
                    "Invalid input";
            _hidl_cb(static_cast<int32_t>(BAD_VALUE), tOutput);
            return Void();
        }
        BGraphicBufferProducer::QueueBufferOutput lOutput;
        status_t status = mBase->queueBuffer(
                static_cast<int>(slot), lInput, &lOutput);

        std::vector<std::vector<native_handle_t*> > nhAA;
        if (!::android::conversion::wrapAs(&tOutput, &nhAA, lOutput)) {
            LOG(ERROR) << "TWGraphicBufferProducer::queueBuffer - "
                    "Invalid output";
            _hidl_cb(static_cast<int32_t>(BAD_VALUE), tOutput);
            return Void();
        }

        _hidl_cb(static_cast<int32_t>(status), tOutput);
        for (auto& nhA : nhAA) {
            for (auto& nh : nhA) {
                native_handle_delete(nh);
            }
        }
        return Void();
    }

    Return<int32_t> cancelBuffer(int32_t slot, const hidl_handle& fence) override {
        sp<Fence> lFence = new Fence();
        if (!::android::conversion::convertTo(lFence.get(), fence)) {
            LOG(ERROR) << "TWGraphicBufferProducer::cancelBuffer - "
                    "Invalid input fence";
            return static_cast<int32_t>(BAD_VALUE);
        }
        return static_cast<int32_t>(mBase->cancelBuffer(static_cast<int>(slot), lFence));
    }

    Return<void> query(int32_t what, HGraphicBufferProducer::query_cb _hidl_cb) override {
        int lValue;
        int lReturn = mBase->query(static_cast<int>(what), &lValue);
        _hidl_cb(static_cast<int32_t>(lReturn), static_cast<int32_t>(lValue));
        return Void();
    }

    Return<void> connect(const sp<HProducerListener>& listener,
            int32_t api, bool producerControlledByApp,
            HGraphicBufferProducer::connect_cb _hidl_cb) override {
        sp<BProducerListener> lListener = listener == nullptr ?
                nullptr : new LWProducerListener(listener);
        BGraphicBufferProducer::QueueBufferOutput lOutput;
        status_t status = mBase->connect(lListener,
                static_cast<int>(api),
                producerControlledByApp,
                &lOutput);

        HGraphicBufferProducer::QueueBufferOutput tOutput{};
        std::vector<std::vector<native_handle_t*> > nhAA;
        if (!::android::conversion::wrapAs(&tOutput, &nhAA, lOutput)) {
            LOG(ERROR) << "TWGraphicBufferProducer::connect - "
                    "Invalid output";
            _hidl_cb(static_cast<int32_t>(status), tOutput);
            return Void();
        }

        _hidl_cb(static_cast<int32_t>(status), tOutput);
        for (auto& nhA : nhAA) {
            for (auto& nh : nhA) {
                native_handle_delete(nh);
            }
        }
        return Void();
    }

    Return<int32_t> disconnect(
            int32_t api,
            HGraphicBufferProducer::DisconnectMode mode) override {
        return static_cast<int32_t>(mBase->disconnect(
                static_cast<int>(api),
                ::android::conversion::toGuiDisconnectMode(mode)));
    }

    Return<int32_t> setSidebandStream(const hidl_handle& stream) override {
        return static_cast<int32_t>(mBase->setSidebandStream(NativeHandle::create(
                stream ? native_handle_clone(stream) : NULL, true)));
    }

    Return<void> allocateBuffers(
            uint32_t width, uint32_t height,
            ::android::hardware::graphics::common::V1_0::PixelFormat format,
            uint32_t usage) override {
        mBase->allocateBuffers(
                width, height,
                static_cast<::android::PixelFormat>(format),
                usage);
        return Void();
    }

    Return<int32_t> allowAllocation(bool allow) override {
        return static_cast<int32_t>(mBase->allowAllocation(allow));
    }

    Return<int32_t> setGenerationNumber(uint32_t generationNumber) override {
        return static_cast<int32_t>(mBase->setGenerationNumber(generationNumber));
    }

    Return<void> getConsumerName(HGraphicBufferProducer::getConsumerName_cb _hidl_cb) override {
        _hidl_cb(mBase->getConsumerName().string());
        return Void();
    }

    Return<int32_t> setSharedBufferMode(bool sharedBufferMode) override {
        return static_cast<int32_t>(mBase->setSharedBufferMode(sharedBufferMode));
    }

    Return<int32_t> setAutoRefresh(bool autoRefresh) override {
        return static_cast<int32_t>(mBase->setAutoRefresh(autoRefresh));
    }

    Return<int32_t> setDequeueTimeout(int64_t timeoutNs) override {
        return static_cast<int32_t>(mBase->setDequeueTimeout(timeoutNs));
    }

    Return<void> getLastQueuedBuffer(HGraphicBufferProducer::getLastQueuedBuffer_cb _hidl_cb) override {
        sp<GraphicBuffer> lOutBuffer = new GraphicBuffer();
        sp<Fence> lOutFence = new Fence();
        float lOutTransformMatrix[16];
        status_t status = mBase->getLastQueuedBuffer(
                &lOutBuffer, &lOutFence, lOutTransformMatrix);

        AnwBuffer tOutBuffer{};
        if (lOutBuffer != nullptr) {
            ::android::conversion::wrapAs(&tOutBuffer, *lOutBuffer);
        }
        hidl_handle tOutFence{};
        native_handle_t* nh = nullptr;
        if ((lOutFence == nullptr) || !::android::conversion::wrapAs(&tOutFence, &nh, *lOutFence)) {
            LOG(ERROR) << "TWGraphicBufferProducer::getLastQueuedBuffer - "
                    "Invalid output fence";
            _hidl_cb(static_cast<int32_t>(status),
                    tOutBuffer,
                    tOutFence,
                    hidl_array<float, 16>());
            return Void();
        }
        hidl_array<float, 16> tOutTransformMatrix(lOutTransformMatrix);

        _hidl_cb(static_cast<int32_t>(status), tOutBuffer, tOutFence, tOutTransformMatrix);
        native_handle_delete(nh);
        return Void();
    }

    Return<void> getFrameTimestamps(HGraphicBufferProducer::getFrameTimestamps_cb _hidl_cb) override {
        ::android::FrameEventHistoryDelta lDelta;
        mBase->getFrameTimestamps(&lDelta);

        HGraphicBufferProducer::FrameEventHistoryDelta tDelta{};
        std::vector<std::vector<native_handle_t*> > nhAA;
        if (!::android::conversion::wrapAs(&tDelta, &nhAA, lDelta)) {
            LOG(ERROR) << "TWGraphicBufferProducer::getFrameTimestamps - "
                    "Invalid output frame timestamps";
            _hidl_cb(tDelta);
            return Void();
        }

        _hidl_cb(tDelta);
        for (auto& nhA : nhAA) {
            for (auto& nh : nhA) {
                native_handle_delete(nh);
            }
        }
        return Void();
    }

    Return<void> getUniqueId(HGraphicBufferProducer::getUniqueId_cb _hidl_cb) override {
        uint64_t outId{};
        status_t status = mBase->getUniqueId(&outId);
        _hidl_cb(static_cast<int32_t>(status), outId);
        return Void();
    }

private:
    sp<BGraphicBufferProducer> mBase;
};

}  // namespace android

#endif  // ANDROID_HARDWARE_GRAPHICS_BUFFERQUEUE_V1_0_WGRAPHICBUFFERPRODUCER_H_