• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 namespace android {
29 
30 class DirectChannelBase {
31 public:
DirectChannelBase()32     DirectChannelBase() : mError(NO_INIT), mSize(0), mBase(nullptr) { }
~DirectChannelBase()33     virtual ~DirectChannelBase() {}
34     virtual bool memoryMatches(const struct sensors_direct_mem_t *mem) const = 0;
35 
36     bool isValid();
37     int getError();
38     void write(const sensors_event_t * ev);
39 
40 protected:
41     int mError;
42     std::unique_ptr<LockfreeBuffer> mBuffer;
43 
44     size_t mSize;
45     void* mBase;
46 };
47 
48 class AshmemDirectChannel : public DirectChannelBase {
49 public:
50     AshmemDirectChannel(const struct sensors_direct_mem_t *mem);
51     ~AshmemDirectChannel() override;
52     bool memoryMatches(const struct sensors_direct_mem_t *mem) const override;
53 private:
54     int mAshmemFd;
55 };
56 
57 class GrallocHalWrapper : public Singleton<GrallocHalWrapper> {
58 public:
59     int registerBuffer(const native_handle_t *handle);
60     int unregisterBuffer(const native_handle_t *handle);
61     int lock(const native_handle_t *handle, int usage, int l, int t, int w, int h, void **vaddr);
62     int unlock(const native_handle_t *handle);
63     bool isSameMemory(const native_handle_t *h1, const native_handle_t *h2);
unregisterImplyDelete()64     bool unregisterImplyDelete() { return mUnregisterImplyDelete; }
65 private:
66     friend class Singleton<GrallocHalWrapper>;
67     GrallocHalWrapper();
68     ~GrallocHalWrapper();
69     static int mapGralloc1Error(int grallocError);
70 
71     int mError;
72     int mVersion;
73     gralloc_module_t *mGrallocModule;
74     // gralloc
75     alloc_device_t *mAllocDevice;
76 
77     // gralloc1
78     gralloc1_device_t *mGralloc1Device;
79     GRALLOC1_PFN_RETAIN mPfnRetain;
80     GRALLOC1_PFN_RELEASE mPfnRelease;
81     GRALLOC1_PFN_LOCK mPfnLock;
82     GRALLOC1_PFN_UNLOCK mPfnUnlock;
83     GRALLOC1_PFN_GET_BACKING_STORE mPfnGetBackingStore;
84     bool mUnregisterImplyDelete;
85 };
86 
87 class GrallocDirectChannel : public DirectChannelBase {
88 public:
89     GrallocDirectChannel(const struct sensors_direct_mem_t *mem);
90     ~GrallocDirectChannel() override;
91     bool memoryMatches(const struct sensors_direct_mem_t *mem) const override;
92 private:
93     native_handle_t *mNativeHandle;
94 };
95 
96 } // namespace android
97 
98 #endif  // DIRECTCHANNEL_H_
99