1/*
2 * Copyright (C) 2016 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#ifndef DIRECTCHANNEL_H_
18#define DIRECTCHANNEL_H_
19
20#include "ring.h"
21#include <cutils/native_handle.h>
22#include <hardware/gralloc.h>
23#include <hardware/gralloc1.h>
24#include <hardware/sensors.h>
25#include <utils/Singleton.h>
26#include <memory>
27
28namespace android {
29
30class DirectChannelBase {
31public:
32    DirectChannelBase() : mError(NO_INIT), mSize(0), mBase(nullptr) { }
33    virtual ~DirectChannelBase() {}
34
35    bool isValid();
36    int getError();
37    void write(const sensors_event_t * ev);
38
39protected:
40    int mError;
41    std::unique_ptr<LockfreeBuffer> mBuffer;
42
43    size_t mSize;
44    void* mBase;
45};
46
47class AshmemDirectChannel : public DirectChannelBase {
48public:
49    AshmemDirectChannel(const struct sensors_direct_mem_t *mem);
50    virtual ~AshmemDirectChannel();
51private:
52    int mAshmemFd;
53};
54
55class GrallocHalWrapper : public Singleton<GrallocHalWrapper> {
56public:
57    int registerBuffer(const native_handle_t *handle);
58    int unregisterBuffer(const native_handle_t *handle);
59    int lock(const native_handle_t *handle, int usage, int l, int t, int w, int h, void **vaddr);
60    int unlock(const native_handle_t *handle);
61private:
62    friend class Singleton<GrallocHalWrapper>;
63    GrallocHalWrapper();
64    ~GrallocHalWrapper();
65    static int mapGralloc1Error(int grallocError);
66
67    int mError;
68    int mVersion;
69    gralloc_module_t *mGrallocModule;
70    // gralloc
71    alloc_device_t *mAllocDevice;
72
73    // gralloc1
74    gralloc1_device_t *mGralloc1Device;
75    GRALLOC1_PFN_RETAIN mPfnRetain;
76    GRALLOC1_PFN_RELEASE mPfnRelease;
77    GRALLOC1_PFN_LOCK mPfnLock;
78    GRALLOC1_PFN_UNLOCK mPfnUnlock;
79};
80
81class GrallocDirectChannel : public DirectChannelBase {
82public:
83    GrallocDirectChannel(const struct sensors_direct_mem_t *mem);
84    virtual ~GrallocDirectChannel();
85private:
86    native_handle_t *mNativeHandle;
87};
88
89} // namespace android
90
91#endif  // DIRECTCHANNEL_H_
92