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 #include "adreno_utils.h"
31 
32 /*****************************************************************************/
33 
34 struct private_module_t;
35 struct private_handle_t;
36 
roundUpToPageSize(unsigned int x)37 inline unsigned int roundUpToPageSize(unsigned int x) {
38     return (x + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
39 }
40 
41 template <class Type>
ALIGN(Type x,Type align)42 inline Type ALIGN(Type x, Type align) {
43     return (x + align-1) & ~(align-1);
44 }
45 
46 #define FALSE 0
47 #define TRUE  1
48 
49 int mapFrameBufferLocked(struct private_module_t* module);
50 int terminateBuffer(gralloc_module_t const* module, private_handle_t* hnd);
51 unsigned int getBufferSizeAndDimensions(int width, int height, int format,
52         int usage, int& alignedw, int &alignedh);
53 unsigned int getBufferSizeAndDimensions(int width, int height, int format,
54         int& alignedw, int &alignedh);
55 
56 
57 // Attributes include aligned width, aligned height, tileEnabled and size of the buffer
58 void getBufferAttributes(int width, int height, int format, int usage,
59                            int& alignedw, int &alignedh,
60                            int& tileEnabled, unsigned int &size);
61 
62 
63 bool isMacroTileEnabled(int format, int usage);
64 
65 int decideBufferHandlingMechanism(int format, const char *compositionUsed,
66                                   int hasBlitEngine, int *needConversion,
67                                   int *useBufferDirectly);
68 
69 // Allocate buffer from width, height, format into a private_handle_t
70 // It is the responsibility of the caller to free the buffer
71 int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage);
72 void free_buffer(private_handle_t *hnd);
73 int getYUVPlaneInfo(private_handle_t* pHnd, struct android_ycbcr* ycbcr);
74 
75 // To query if UBWC is enabled, based on format and usage flags
76 bool isUBwcEnabled(int format, int usage);
77 
78 // Function to check if the format is an uncompressed RGB format
79 bool isUncompressedRgbFormat(int format);
80 
81 /*****************************************************************************/
82 
83 class Locker {
84     pthread_mutex_t mutex;
85     pthread_cond_t cond;
86     public:
87     class Autolock {
88         Locker& locker;
89         public:
Autolock(Locker & locker)90         inline Autolock(Locker& locker) : locker(locker) {  locker.lock(); }
~Autolock()91         inline ~Autolock() { locker.unlock(); }
92     };
Locker()93     inline Locker()        {
94         pthread_mutex_init(&mutex, 0);
95         pthread_cond_init(&cond, 0);
96     }
~Locker()97     inline ~Locker()       {
98         pthread_mutex_destroy(&mutex);
99         pthread_cond_destroy(&cond);
100     }
lock()101     inline void lock()     { pthread_mutex_lock(&mutex); }
wait()102     inline void wait()     { pthread_cond_wait(&cond, &mutex); }
unlock()103     inline void unlock()   { pthread_mutex_unlock(&mutex); }
signal()104     inline void signal()   { pthread_cond_signal(&cond); }
105 };
106 
107 
108 class AdrenoMemInfo : public android::Singleton <AdrenoMemInfo>
109 {
110     public:
111     AdrenoMemInfo();
112 
113     ~AdrenoMemInfo();
114 
115     /*
116      * Function to compute aligned width and aligned height based on
117      * width, height, format and usage flags.
118      *
119      * @return aligned width, aligned height
120      */
121     void getAlignedWidthAndHeight(int width, int height, int format,
122                             int usage, int& aligned_w, int& aligned_h);
123 
124     /*
125      * Function to compute the adreno aligned width and aligned height
126      * based on the width and format.
127      *
128      * @return aligned width, aligned height
129      */
130     void getGpuAlignedWidthHeight(int width, int height, int format,
131                             int tileEnabled, int& alignedw, int &alignedh);
132 
133     /*
134      * Function to return whether GPU support MacroTile feature
135      *
136      * @return >0 : supported
137      *          0 : not supported
138      */
139     int isMacroTilingSupportedByGPU();
140 
141     /*
142      * Function to query whether GPU supports UBWC for given HAL format
143      * @return > 0 : supported
144      *           0 : not supported
145      */
146     int isUBWCSupportedByGPU(int format);
147 
148     /*
149      * Function to get the corresponding Adreno format for given HAL format
150      */
151     ADRENOPIXELFORMAT getGpuPixelFormat(int hal_format);
152 
153     private:
154         // Pointer to the padding library.
155         void *libadreno_utils;
156 
157         // link(s)to adreno surface padding library.
158         int (*LINK_adreno_compute_padding) (int width, int bpp,
159                                                 int surface_tile_height,
160                                                 int screen_tile_height,
161                                                 int padding_threshold);
162 
163         void (*LINK_adreno_compute_aligned_width_and_height) (int width,
164                                                 int height,
165                                                 int bpp,
166                                                 int tile_mode,
167                                                 int raster_mode,
168                                                 int padding_threshold,
169                                                 int *aligned_w,
170                                                 int *aligned_h);
171 
172         int (*LINK_adreno_isMacroTilingSupportedByGpu) (void);
173 
174         void(*LINK_adreno_compute_compressedfmt_aligned_width_and_height)(
175                                                 int width,
176                                                 int height,
177                                                 int format,
178                                                 int tile_mode,
179                                                 int raster_mode,
180                                                 int padding_threshold,
181                                                 int *aligned_w,
182                                                 int *aligned_h,
183                                                 int *bpp);
184 
185         int (*LINK_adreno_isUBWCSupportedByGpu) (ADRENOPIXELFORMAT format);
186 };
187 #endif /* GR_H_ */
188