Home | History | Annotate | Download | only in libgralloc
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
      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 #ifndef GR_H_
     19 #define GR_H_
     20 
     21 #include <stdint.h>
     22 #include <limits.h>
     23 #include <sys/cdefs.h>
     24 #include <hardware/gralloc.h>
     25 #include <pthread.h>
     26 #include <errno.h>
     27 
     28 #include <cutils/native_handle.h>
     29 #include <utils/Singleton.h>
     30 
     31 /*****************************************************************************/
     32 
     33 struct private_module_t;
     34 struct private_handle_t;
     35 
     36 inline size_t roundUpToPageSize(size_t x) {
     37     return (x + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
     38 }
     39 
     40 inline size_t ALIGN(size_t x, size_t align) {
     41     return (x + align-1) & ~(align-1);
     42 }
     43 
     44 #define FALSE 0
     45 #define TRUE  1
     46 
     47 int mapFrameBufferLocked(struct private_module_t* module);
     48 int terminateBuffer(gralloc_module_t const* module, private_handle_t* hnd);
     49 size_t getBufferSizeAndDimensions(int width, int height, int format, int usage,
     50                                   int& alignedw, int &alignedh);
     51 size_t getBufferSizeAndDimensions(int width, int height, int format,
     52                                   int& alignedw, int &alignedh);
     53 
     54 
     55 // Attributes include aligned width, aligned height, tileEnabled and size of the buffer
     56 void getBufferAttributes(int width, int height, int format, int usage,
     57                            int& alignedw, int &alignedh,
     58                            int& tileEnabled, size_t &size);
     59 
     60 
     61 bool isMacroTileEnabled(int format, int usage);
     62 
     63 int decideBufferHandlingMechanism(int format, const char *compositionUsed,
     64                                   int hasBlitEngine, int *needConversion,
     65                                   int *useBufferDirectly);
     66 
     67 // Allocate buffer from width, height, format into a private_handle_t
     68 // It is the responsibility of the caller to free the buffer
     69 int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage);
     70 void free_buffer(private_handle_t *hnd);
     71 
     72 /*****************************************************************************/
     73 
     74 class Locker {
     75     pthread_mutex_t mutex;
     76     public:
     77     class Autolock {
     78         Locker& locker;
     79         public:
     80         inline Autolock(Locker& locker) : locker(locker) {  locker.lock(); }
     81         inline ~Autolock() { locker.unlock(); }
     82     };
     83     inline Locker()        { pthread_mutex_init(&mutex, 0); }
     84     inline ~Locker()       { pthread_mutex_destroy(&mutex); }
     85     inline void lock()     { pthread_mutex_lock(&mutex); }
     86     inline void unlock()   { pthread_mutex_unlock(&mutex); }
     87 };
     88 
     89 
     90 class AdrenoMemInfo : public android::Singleton <AdrenoMemInfo>
     91 {
     92     public:
     93     AdrenoMemInfo();
     94 
     95     ~AdrenoMemInfo();
     96 
     97     /*
     98      * Function to compute the adreno aligned width and aligned height
     99      * based on the width and format.
    100      *
    101      * @return aligned width, aligned height
    102      */
    103     void getAlignedWidthAndHeight(int width, int height, int format,
    104                             int tileEnabled, int& alignedw, int &alignedh);
    105 
    106     /*
    107      * Function to return whether GPU support MacroTile feature
    108      *
    109      * @return >0 : supported
    110      *          0 : not supported
    111      */
    112     int isMacroTilingSupportedByGPU();
    113 
    114     private:
    115         // Pointer to the padding library.
    116         void *libadreno_utils;
    117 
    118         // link(s)to adreno surface padding library.
    119         int (*LINK_adreno_compute_padding) (int width, int bpp,
    120                                                 int surface_tile_height,
    121                                                 int screen_tile_height,
    122                                                 int padding_threshold);
    123 
    124         void (*LINK_adreno_compute_aligned_width_and_height) (int width,
    125                                                 int height,
    126                                                 int bpp,
    127                                                 int tile_mode,
    128                                                 int raster_mode,
    129                                                 int padding_threshold,
    130                                                 int *aligned_w,
    131                                                 int *aligned_h);
    132 
    133         int (*LINK_adreno_isMacroTilingSupportedByGpu) (void);
    134 
    135         void(*LINK_adreno_compute_compressedfmt_aligned_width_and_height)(
    136                                                 int width,
    137                                                 int height,
    138                                                 int format,
    139                                                 int tile_mode,
    140                                                 int raster_mode,
    141                                                 int padding_threshold,
    142                                                 int *aligned_w,
    143                                                 int *aligned_h,
    144                                                 int *bpp);
    145 };
    146 #endif /* GR_H_ */
    147