Jorge E. Moreira | 7a8dadd | 2017-08-22 17:01:39 -0700 | [diff] [blame] | 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 <cutils/native_handle.h> |
Jorge E. Moreira | 7a8dadd | 2017-08-22 17:01:39 -0700 | [diff] [blame] | 19 | #include <hardware/gralloc.h> |
| 20 | #include <log/log.h> |
| 21 | |
Greg Hartman | d072a34 | 2017-12-05 20:27:40 -0800 | [diff] [blame^] | 22 | #include "guest/vsoc/lib/gralloc_region_view.h" |
| 23 | |
Jorge E. Moreira | 7a8dadd | 2017-08-22 17:01:39 -0700 | [diff] [blame] | 24 | struct vsoc_alloc_device_t { |
| 25 | alloc_device_t device; |
Greg Hartman | d072a34 | 2017-12-05 20:27:40 -0800 | [diff] [blame^] | 26 | vsoc::gralloc::GrallocRegionView* gralloc_region; |
Jorge E. Moreira | 7a8dadd | 2017-08-22 17:01:39 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | struct vsoc_gralloc_module_t { |
| 30 | gralloc_module_t base; |
| 31 | }; |
| 32 | |
| 33 | static_assert(sizeof(int) >= 4, "At least 4 bytes are needed for offsets"); |
| 34 | |
| 35 | struct vsoc_buffer_handle_t : public native_handle { |
| 36 | // File descriptors |
| 37 | int fd; |
| 38 | // ints |
| 39 | int magic; |
| 40 | int format; |
| 41 | int x_res; |
| 42 | int y_res; |
| 43 | int stride_in_pixels; |
| 44 | int size; |
| 45 | // buffer offset in bytes divided by PAGE_SIZE |
| 46 | int offset; |
| 47 | |
| 48 | static inline int sNumInts() { |
| 49 | return ((sizeof(vsoc_buffer_handle_t) - sizeof(native_handle_t)) / |
| 50 | sizeof(int) - |
| 51 | sNumFds); |
| 52 | } |
| 53 | static const int sNumFds = 1; |
| 54 | static const int sMagic = 0xc63752f4; |
| 55 | |
| 56 | vsoc_buffer_handle_t(int fd, |
| 57 | int offset, |
| 58 | int size, |
| 59 | int format, |
| 60 | int x_res, |
| 61 | int y_res, |
| 62 | int stride_in_pixels) |
| 63 | : fd(fd), |
| 64 | magic(sMagic), |
| 65 | format(format), |
| 66 | x_res(x_res), |
| 67 | y_res(y_res), |
| 68 | stride_in_pixels(stride_in_pixels), |
| 69 | size(size), |
| 70 | offset(offset) { |
| 71 | version = sizeof(native_handle); |
| 72 | numInts = sNumInts(); |
| 73 | numFds = sNumFds; |
| 74 | } |
| 75 | |
| 76 | ~vsoc_buffer_handle_t() { |
| 77 | magic = 0; |
| 78 | } |
| 79 | |
| 80 | static int validate(const native_handle* handle) { |
| 81 | const vsoc_buffer_handle_t* hnd = |
| 82 | reinterpret_cast<const vsoc_buffer_handle_t*>(handle); |
| 83 | if (!hnd || hnd->version != sizeof(native_handle) || |
| 84 | hnd->numInts != sNumInts() || hnd->numFds != sNumFds || |
| 85 | hnd->magic != sMagic) { |
| 86 | ALOGE("Invalid gralloc handle (at %p)", handle); |
| 87 | return -EINVAL; |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | // These functions are to be used to map/unmap gralloc buffers. They are thread |
| 94 | // safe and ensure that the same buffer never gets mapped twice. |
| 95 | void* reference_buffer(const vsoc_buffer_handle_t* hnd); |
| 96 | int unreference_buffer(const vsoc_buffer_handle_t* hnd); |
| 97 | |
| 98 | // TODO(jemoreira): Move this to a place where it can be used by the gralloc |
| 99 | // region as well. |
| 100 | inline int align(int input, int alignment) { |
| 101 | return (input + alignment - 1) & -alignment; |
| 102 | } |