1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * Copyright (c) 2011 - 2016, 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 int getRgbDataAddress(private_handle_t* pHnd, void** rgb_data);
75 
76 // To query if UBWC is enabled, based on format and usage flags
77 bool isUBwcEnabled(int format, int usage);
78 
79 // Function to check if the format is an RGB format
80 bool isUncompressedRgbFormat(int format);
81 
82 /*****************************************************************************/
83 
84 class Locker {
85     pthread_mutex_t mutex;
86     pthread_cond_t cond;
87     public:
88     class Autolock {
89         Locker& locker;
90         public:
Autolock(Locker & locker)91         inline Autolock(Locker& locker) : locker(locker) {  locker.lock(); }
~Autolock()92         inline ~Autolock() { locker.unlock(); }
93     };
Locker()94     inline Locker()        {
95         pthread_mutex_init(&mutex, 0);
96         pthread_cond_init(&cond, 0);
97     }
~Locker()98     inline ~Locker()       {
99         pthread_mutex_destroy(&mutex);
100         pthread_cond_destroy(&cond);
101     }
lock()102     inline void lock()     { pthread_mutex_lock(&mutex); }
wait()103     inline void wait()     { pthread_cond_wait(&cond, &mutex); }
unlock()104     inline void unlock()   { pthread_mutex_unlock(&mutex); }
signal()105     inline void signal()   { pthread_cond_signal(&cond); }
106 };
107 
108 
109 class AdrenoMemInfo : public android::Singleton <AdrenoMemInfo>
110 {
111     public:
112     AdrenoMemInfo();
113 
114     ~AdrenoMemInfo();
115 
116     /*
117      * Function to compute aligned width and aligned height based on
118      * width, height, format and usage flags.
119      *
120      * @return aligned width, aligned height
121      */
122     void getAlignedWidthAndHeight(int width, int height, int format,
123                             int usage, int& aligned_w, int& aligned_h);
124 
125     /*
126      * Function to compute aligned width and aligned height based on
127      * private handle
128      *
129      * @return aligned width, aligned height
130      */
131     void getAlignedWidthAndHeight(const private_handle_t *hnd, int& aligned_w, int& aligned_h);
132 
133     /*
134      * Function to compute the adreno aligned width and aligned height
135      * based on the width and format.
136      *
137      * @return aligned width, aligned height
138      */
139     void getGpuAlignedWidthHeight(int width, int height, int format,
140                             int tileEnabled, int& alignedw, int &alignedh);
141 
142     /*
143      * Function to return whether GPU support MacroTile feature
144      *
145      * @return >0 : supported
146      *          0 : not supported
147      */
148     int isMacroTilingSupportedByGPU();
149 
150     /*
151      * Function to query whether GPU supports UBWC for given HAL format
152      * @return > 0 : supported
153      *           0 : not supported
154      */
155     int isUBWCSupportedByGPU(int format);
156 
157     /*
158      * Function to get the corresponding Adreno format for given HAL format
159      */
160     ADRENOPIXELFORMAT getGpuPixelFormat(int hal_format);
161 
162     private:
163         // Overriding flag to disable UBWC alloc for graphics stack
164         int  gfx_ubwc_disable;
165         // Pointer to the padding library.
166         void *libadreno_utils;
167 
168         // link(s)to adreno surface padding library.
169         int (*LINK_adreno_compute_padding) (int width, int bpp,
170                                                 int surface_tile_height,
171                                                 int screen_tile_height,
172                                                 int padding_threshold);
173 
174         void (*LINK_adreno_compute_aligned_width_and_height) (int width,
175                                                 int height,
176                                                 int bpp,
177                                                 int tile_mode,
178                                                 int raster_mode,
179                                                 int padding_threshold,
180                                                 int *aligned_w,
181                                                 int *aligned_h);
182 
183         int (*LINK_adreno_isMacroTilingSupportedByGpu) (void);
184 
185         void(*LINK_adreno_compute_compressedfmt_aligned_width_and_height)(
186                                                 int width,
187                                                 int height,
188                                                 int format,
189                                                 int tile_mode,
190                                                 int raster_mode,
191                                                 int padding_threshold,
192                                                 int *aligned_w,
193                                                 int *aligned_h,
194                                                 int *bpp);
195 
196         int (*LINK_adreno_isUBWCSupportedByGpu) (ADRENOPIXELFORMAT format);
197 
198         unsigned int (*LINK_adreno_get_gpu_pixel_alignment) ();
199 };
200 #endif /* GR_H_ */
201