Home | History | Annotate | Download | only in ui
      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_GRALLOC2_H
     18 #define ANDROID_UI_GRALLOC2_H
     19 
     20 #include <string>
     21 
     22 #include <android/hardware/graphics/allocator/2.0/IAllocator.h>
     23 #include <android/hardware/graphics/common/1.1/types.h>
     24 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
     25 #include <android/hardware/graphics/mapper/2.1/IMapper.h>
     26 #include <utils/StrongPointer.h>
     27 
     28 namespace android {
     29 
     30 namespace Gralloc2 {
     31 
     32 using hardware::graphics::allocator::V2_0::IAllocator;
     33 using hardware::graphics::common::V1_1::BufferUsage;
     34 using hardware::graphics::common::V1_1::PixelFormat;
     35 using hardware::graphics::mapper::V2_1::IMapper;
     36 using hardware::graphics::mapper::V2_0::BufferDescriptor;
     37 using hardware::graphics::mapper::V2_0::Error;
     38 using hardware::graphics::mapper::V2_0::YCbCrLayout;
     39 
     40 // A wrapper to IMapper
     41 class Mapper {
     42 public:
     43     static void preload();
     44 
     45     Mapper();
     46 
     47     Error createDescriptor(
     48             const IMapper::BufferDescriptorInfo& descriptorInfo,
     49             BufferDescriptor* outDescriptor) const;
     50 
     51     // Import a buffer that is from another HAL, another process, or is
     52     // cloned.
     53     //
     54     // The returned handle must be freed with freeBuffer.
     55     Error importBuffer(const hardware::hidl_handle& rawHandle,
     56             buffer_handle_t* outBufferHandle) const;
     57 
     58     void freeBuffer(buffer_handle_t bufferHandle) const;
     59 
     60     Error validateBufferSize(buffer_handle_t bufferHandle,
     61             const IMapper::BufferDescriptorInfo& descriptorInfo,
     62             uint32_t stride) const;
     63 
     64     void getTransportSize(buffer_handle_t bufferHandle,
     65             uint32_t* outNumFds, uint32_t* outNumInts) const;
     66 
     67     // The ownership of acquireFence is always transferred to the callee, even
     68     // on errors.
     69     Error lock(buffer_handle_t bufferHandle, uint64_t usage,
     70             const IMapper::Rect& accessRegion,
     71             int acquireFence, void** outData) const;
     72 
     73     // The ownership of acquireFence is always transferred to the callee, even
     74     // on errors.
     75     Error lock(buffer_handle_t bufferHandle, uint64_t usage,
     76             const IMapper::Rect& accessRegion,
     77             int acquireFence, YCbCrLayout* outLayout) const;
     78 
     79     // unlock returns a fence sync object (or -1) and the fence sync object is
     80     // owned by the caller
     81     int unlock(buffer_handle_t bufferHandle) const;
     82 
     83 private:
     84     // Determines whether the passed info is compatible with the mapper.
     85     Error validateBufferDescriptorInfo(
     86             const IMapper::BufferDescriptorInfo& descriptorInfo) const;
     87 
     88     sp<hardware::graphics::mapper::V2_0::IMapper> mMapper;
     89     sp<IMapper> mMapperV2_1;
     90 };
     91 
     92 // A wrapper to IAllocator
     93 class Allocator {
     94 public:
     95     // An allocator relies on a mapper, and that mapper must be alive at all
     96     // time.
     97     Allocator(const Mapper& mapper);
     98 
     99     std::string dumpDebugInfo() const;
    100 
    101     /*
    102      * The returned buffers are already imported and must not be imported
    103      * again.  outBufferHandles must point to a space that can contain at
    104      * least "count" buffer_handle_t.
    105      */
    106     Error allocate(BufferDescriptor descriptor, uint32_t count,
    107             uint32_t* outStride, buffer_handle_t* outBufferHandles) const;
    108 
    109     Error allocate(BufferDescriptor descriptor,
    110             uint32_t* outStride, buffer_handle_t* outBufferHandle) const
    111     {
    112         return allocate(descriptor, 1, outStride, outBufferHandle);
    113     }
    114 
    115     Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, uint32_t count,
    116             uint32_t* outStride, buffer_handle_t* outBufferHandles) const
    117     {
    118         BufferDescriptor descriptor;
    119         Error error = mMapper.createDescriptor(descriptorInfo, &descriptor);
    120         if (error == Error::NONE) {
    121             error = allocate(descriptor, count, outStride, outBufferHandles);
    122         }
    123         return error;
    124     }
    125 
    126     Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
    127             uint32_t* outStride, buffer_handle_t* outBufferHandle) const
    128     {
    129         return allocate(descriptorInfo, 1, outStride, outBufferHandle);
    130     }
    131 
    132 private:
    133     const Mapper& mMapper;
    134     sp<IAllocator> mAllocator;
    135 };
    136 
    137 } // namespace Gralloc2
    138 
    139 } // namespace android
    140 
    141 #endif // ANDROID_UI_GRALLOC2_H
    142