Home | History | Annotate | Download | only in libgralloc1
      1 /*
      2  * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved.
      3  * Not a Contribution
      4  *
      5  * Copyright (C) 2008 The Android Open Source Project
      6  *
      7  * Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  *      http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  */
     19 
     20 #ifndef __GR_BUF_MGR_H__
     21 #define __GR_BUF_MGR_H__
     22 
     23 #include <pthread.h>
     24 #include <unordered_map>
     25 #include <mutex>
     26 
     27 #include "gralloc_priv.h"
     28 #include "gr_allocator.h"
     29 
     30 namespace gralloc1 {
     31 
     32 class BufferManager {
     33  public:
     34   BufferManager();
     35   ~BufferManager();
     36   bool Init();
     37   gralloc1_error_t AllocateBuffers(uint32_t numDescriptors, const BufferDescriptor *descriptors,
     38                                    buffer_handle_t *outBuffers);
     39   gralloc1_error_t RetainBuffer(private_handle_t const *hnd);
     40   gralloc1_error_t ReleaseBuffer(private_handle_t const *hnd);
     41   gralloc1_error_t LockBuffer(const private_handle_t *hnd, gralloc1_producer_usage_t prod_usage,
     42                               gralloc1_consumer_usage_t cons_usage);
     43   gralloc1_error_t UnlockBuffer(const private_handle_t *hnd);
     44   gralloc1_error_t Perform(int operation, va_list args);
     45 
     46  private:
     47   gralloc1_error_t MapBuffer(private_handle_t const *hnd);
     48   gralloc1_error_t FreeBuffer(private_handle_t const *hnd);
     49   int GetBufferType(int format);
     50   int AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
     51                      unsigned int bufferSize = 0);
     52   int AllocateBuffer(unsigned int size, int aligned_w, int aligned_h, int unaligned_w,
     53                      int unaligned_h, int format, int bufferType,
     54                      gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage,
     55                      buffer_handle_t *handle);
     56   int GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage,
     57                        gralloc1_consumer_usage_t cons_usage);
     58   int GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage,
     59                      gralloc1_consumer_usage_t cons_usage);
     60   void CreateSharedHandle(buffer_handle_t inbuffer, const BufferDescriptor &descriptor,
     61                           buffer_handle_t *out_buffer);
     62 
     63   bool map_fb_mem_ = false;
     64   bool ubwc_for_fb_ = false;
     65   Allocator *allocator_ = NULL;
     66   std::mutex locker_;
     67   std::unordered_map<private_handle_t const *, int> handles_map_ = {};
     68 };
     69 
     70 }  // namespace gralloc1
     71 
     72 #endif  // __GR_BUF_MGR_H__
     73