1 /*
2  * Copyright 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 ANDROID_UI_GRALLOC_H
18 #define ANDROID_UI_GRALLOC_H
19 
20 #include <string>
21 
22 #include <hidl/HidlSupport.h>
23 #include <ui/PixelFormat.h>
24 #include <ui/Rect.h>
25 #include <utils/StrongPointer.h>
26 
27 namespace android {
28 
29 // A wrapper to IMapper
30 class GrallocMapper {
31 public:
32     virtual ~GrallocMapper();
33 
34     virtual bool isLoaded() const = 0;
35 
36     virtual status_t createDescriptor(void* bufferDescriptorInfo,
37                                       void* outBufferDescriptor) const = 0;
38 
39     // Import a buffer that is from another HAL, another process, or is
40     // cloned.
41     //
42     // The returned handle must be freed with freeBuffer.
43     virtual status_t importBuffer(const hardware::hidl_handle& rawHandle,
44                                   buffer_handle_t* outBufferHandle) const = 0;
45 
46     virtual void freeBuffer(buffer_handle_t bufferHandle) const = 0;
47 
48     virtual status_t validateBufferSize(buffer_handle_t bufferHandle, uint32_t width,
49                                         uint32_t height, android::PixelFormat format,
50                                         uint32_t layerCount, uint64_t usage,
51                                         uint32_t stride) const = 0;
52 
53     virtual void getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds,
54                                   uint32_t* outNumInts) const = 0;
55 
56     // The ownership of acquireFence is always transferred to the callee, even
57     // on errors.
58     virtual status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
59                           int acquireFence, void** outData, int32_t* outBytesPerPixel,
60                           int32_t* outBytesPerStride) const = 0;
61 
62     // The ownership of acquireFence is always transferred to the callee, even
63     // on errors.
64     virtual status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
65                           int acquireFence, android_ycbcr* ycbcr) const = 0;
66 
67     // unlock returns a fence sync object (or -1) and the fence sync object is
68     // owned by the caller
69     virtual int unlock(buffer_handle_t bufferHandle) const = 0;
70 
71     // isSupported queries whether or not a buffer with the given width, height,
72     // format, layer count, and usage can be allocated on the device.  If
73     // *outSupported is set to true, a buffer with the given specifications may be successfully
74     // allocated if resources are available.  If false, a buffer with the given specifications will
75     // never successfully allocate on this device. Note that this function is not guaranteed to be
76     // supported on all devices, in which case a status_t of INVALID_OPERATION will be returned.
77     virtual status_t isSupported(uint32_t width, uint32_t height, android::PixelFormat format,
78                                  uint32_t layerCount, uint64_t usage, bool* outSupported) const = 0;
79 };
80 
81 // A wrapper to IAllocator
82 class GrallocAllocator {
83 public:
84     virtual ~GrallocAllocator();
85 
86     virtual bool isLoaded() const = 0;
87 
88     virtual std::string dumpDebugInfo() const = 0;
89 
90     /*
91      * The returned buffers are already imported and must not be imported
92      * again.  outBufferHandles must point to a space that can contain at
93      * least "bufferCount" buffer_handle_t.
94      */
95     virtual status_t allocate(uint32_t width, uint32_t height, PixelFormat format,
96                               uint32_t layerCount, uint64_t usage, uint32_t bufferCount,
97                               uint32_t* outStride, buffer_handle_t* outBufferHandles) const = 0;
98 };
99 
100 } // namespace android
101 
102 #endif // ANDROID_UI_GRALLOC_H
103