1 /*
2  * Copyright 2016 The Android Open Source Project
3  * * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <mapper-hal/2.1/MapperHal.h>
19 #include <mapper-passthrough/2.0/Gralloc0Hal.h>
20 
21 namespace android {
22 namespace hardware {
23 namespace graphics {
24 namespace mapper {
25 namespace V2_1 {
26 namespace passthrough {
27 
28 namespace detail {
29 
30 using V2_0::BufferDescriptor;
31 using V2_0::Error;
32 
33 // Gralloc0HalImpl implements V2_*::hal::MapperHal on top of gralloc0
34 template <typename Hal>
35 class Gralloc0HalImpl : public V2_0::passthrough::detail::Gralloc0HalImpl<Hal> {
36    public:
validateBufferSize(const native_handle_t * bufferHandle,const IMapper::BufferDescriptorInfo & descriptorInfo,uint32_t stride)37      Error validateBufferSize(const native_handle_t* bufferHandle,
38                               const IMapper::BufferDescriptorInfo& descriptorInfo,
39                               uint32_t stride) override {
40          if (descriptorInfo.layerCount != 1) {
41              return Error::BAD_VALUE;
42          }
43 
44          if (!mModule->validateBufferSize) {
45              return Error::NONE;
46          }
47 
48          int32_t ret = mModule->validateBufferSize(
49                  mModule, bufferHandle, descriptorInfo.width, descriptorInfo.height,
50                  static_cast<int32_t>(descriptorInfo.format),
51                  static_cast<uint64_t>(descriptorInfo.usage), stride);
52          if (ret == -EINVAL) {
53              return Error::BAD_BUFFER;
54          } else if (ret < 0) {
55              return Error::BAD_VALUE;
56          }
57          return Error::NONE;
58      }
getTransportSize(const native_handle_t * bufferHandle,uint32_t * outNumFds,uint32_t * outNumInts)59      Error getTransportSize(const native_handle_t* bufferHandle, uint32_t* outNumFds,
60                             uint32_t* outNumInts) override {
61          if (!mModule->getTransportSize) {
62              *outNumFds = bufferHandle->numFds;
63              *outNumInts = bufferHandle->numInts;
64              return Error::NONE;
65          }
66 
67          int32_t ret = mModule->getTransportSize(mModule, bufferHandle, outNumFds, outNumInts);
68          return static_cast<Error>(ret);
69     }
70 
createDescriptor_2_1(const IMapper::BufferDescriptorInfo & descriptorInfo,BufferDescriptor * outDescriptor)71     Error createDescriptor_2_1(const IMapper::BufferDescriptorInfo& descriptorInfo,
72                                BufferDescriptor* outDescriptor) override {
73         return createDescriptor(
74                 V2_0::IMapper::BufferDescriptorInfo{
75                         descriptorInfo.width,
76                         descriptorInfo.height,
77                         descriptorInfo.layerCount,
78                         static_cast<common::V1_0::PixelFormat>(descriptorInfo.format),
79                         descriptorInfo.usage,
80                 },
81                 outDescriptor);
82     }
83 
84    private:
85     using BaseType2_0 = V2_0::passthrough::detail::Gralloc0HalImpl<Hal>;
86     using BaseType2_0::createDescriptor;
87     using BaseType2_0::mModule;
88 };
89 
90 }  // namespace detail
91 
92 using Gralloc0Hal = detail::Gralloc0HalImpl<hal::MapperHal>;
93 
94 }  // namespace passthrough
95 }  // namespace V2_1
96 }  // namespace mapper
97 }  // namespace graphics
98 }  // namespace hardware
99 }  // namespace android
100