• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 /*
3  * Copyright (C) 2017 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <errno.h>
19 #include <cutils/native_handle.h>
20 #include <hardware/gralloc.h>
21 #include <log/log.h>
22 
23 struct vsoc_alloc_device_t {
24   alloc_device_t device;
25 };
26 
27 struct vsoc_gralloc_module_t {
28   gralloc_module_t base;
29 };
30 
31 static_assert(sizeof(int) >= 4, "At least 4 bytes are needed for offsets");
32 
33 struct vsoc_buffer_handle_t : public native_handle {
34   // File descriptors
35   int fd;
36   // ints
37   int magic;
38   int format;
39   int x_res;
40   int y_res;
41   int stride_in_pixels;
42   int size;
43   // buffer offset in bytes divided by PAGE_SIZE
44   int offset;
45 
46   static inline int sNumInts() {
47     return ((sizeof(vsoc_buffer_handle_t) - sizeof(native_handle_t)) /
48                 sizeof(int) -
49             sNumFds);
50   }
51   static const int sNumFds = 1;
52   static const int sMagic = 0xc63752f4;
53 
54   vsoc_buffer_handle_t(int fd,
55                        int offset,
56                        int size,
57                        int format,
58                        int x_res,
59                        int y_res,
60                        int stride_in_pixels)
61       : fd(fd),
62         magic(sMagic),
63         format(format),
64         x_res(x_res),
65         y_res(y_res),
66         stride_in_pixels(stride_in_pixels),
67         size(size),
68         offset(offset) {
69     version = sizeof(native_handle);
70     numInts = sNumInts();
71     numFds = sNumFds;
72   }
73 
74   ~vsoc_buffer_handle_t() {
75     magic = 0;
76   }
77 
78   static int validate(const native_handle* handle) {
79     const vsoc_buffer_handle_t* hnd =
80         reinterpret_cast<const vsoc_buffer_handle_t*>(handle);
81     if (!hnd || hnd->version != sizeof(native_handle) ||
82         hnd->numInts != sNumInts() || hnd->numFds != sNumFds ||
83         hnd->magic != sMagic) {
84       ALOGE("Invalid gralloc handle (at %p)", handle);
85       return -EINVAL;
86     }
87     return 0;
88   }
89 };
90 
91 // These functions are to be used to map/unmap gralloc buffers. They are thread
92 // safe and ensure that the same buffer never gets mapped twice.
93 void* reference_buffer(const vsoc_buffer_handle_t* hnd);
94 int unreference_buffer(const vsoc_buffer_handle_t* hnd);
95 
96 // TODO(jemoreira): Move this to a place where it can be used by the gralloc
97 // region as well.
98 inline int align(int input, int alignment) {
99   return (input + alignment - 1) & -alignment;
100 }
101