1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * Copyright (c) 2011 - 2017, 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 #include <unistd.h>
28 
29 #include <cutils/native_handle.h>
30 #include <utils/Singleton.h>
31 #include "adreno_utils.h"
32 
33 /*****************************************************************************/
34 
35 struct private_module_t;
36 struct private_handle_t;
37 
roundUpToPageSize(unsigned int x)38 inline unsigned int roundUpToPageSize(unsigned int x) {
39     return (x + (getpagesize()-1)) & ~(getpagesize()-1);
40 }
41 
42 template <class Type>
ALIGN(Type x,Type align)43 inline Type ALIGN(Type x, Type align) {
44     return (x + align-1) & ~(align-1);
45 }
46 
47 #define FALSE 0
48 #define TRUE  1
49 
50 int mapFrameBufferLocked(struct private_module_t* module);
51 int terminateBuffer(gralloc_module_t const* module, private_handle_t* hnd);
52 unsigned int getBufferSizeAndDimensions(int width, int height, int format,
53         int usage, int& alignedw, int &alignedh);
54 unsigned int getBufferSizeAndDimensions(int width, int height, int format,
55         int& alignedw, int &alignedh);
56 
57 int decideBufferHandlingMechanism(int format, const char *compositionUsed,
58                                   int hasBlitEngine, int *needConversion,
59                                   int *useBufferDirectly);
60 
61 // Allocate buffer from width, height, format into a private_handle_t
62 // It is the responsibility of the caller to free the buffer
63 int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage);
64 void free_buffer(private_handle_t *hnd);
65 int getYUVPlaneInfo(private_handle_t* pHnd, struct android_ycbcr* ycbcr);
66 int getRgbDataAddress(private_handle_t* pHnd, void** rgb_data);
67 
68 // To query if UBWC is enabled, based on format and usage flags
69 bool isUBwcEnabled(int format, int usage);
70 
71 // Function to check if the format is an RGB format
72 bool isUncompressedRgbFormat(int format);
73 
74 // Returns number of planes, stride and offset of each plane for a given w,h,f
75 int getBufferLayout(private_handle_t *hnd, uint32_t stride[4],
76         uint32_t offset[4], uint32_t *num_planes);
77 /*****************************************************************************/
78 
79 class Locker {
80     pthread_mutex_t mutex;
81     pthread_cond_t cond;
82     public:
83     class Autolock {
84         Locker& locker;
85         public:
Autolock(Locker & locker)86         inline Autolock(Locker& locker) : locker(locker) {  locker.lock(); }
~Autolock()87         inline ~Autolock() { locker.unlock(); }
88     };
Locker()89     inline Locker()        {
90         pthread_mutex_init(&mutex, 0);
91         pthread_cond_init(&cond, 0);
92     }
~Locker()93     inline ~Locker()       {
94         pthread_mutex_destroy(&mutex);
95         pthread_cond_destroy(&cond);
96     }
lock()97     inline void lock()     { pthread_mutex_lock(&mutex); }
wait()98     inline void wait()     { pthread_cond_wait(&cond, &mutex); }
unlock()99     inline void unlock()   { pthread_mutex_unlock(&mutex); }
signal()100     inline void signal()   { pthread_cond_signal(&cond); }
101 };
102 
103 
104 class AdrenoMemInfo : public android::Singleton <AdrenoMemInfo>
105 {
106     public:
107     AdrenoMemInfo();
108 
109     ~AdrenoMemInfo();
110 
111     /*
112      * Function to compute aligned width and aligned height based on
113      * width, height, format and usage flags.
114      *
115      * @return aligned width, aligned height
116      */
117     void getAlignedWidthAndHeight(int width, int height, int format,
118                             int usage, int& aligned_w, int& aligned_h);
119 
120     /*
121      * Function to compute aligned width and aligned height based on
122      * private handle
123      *
124      * @return aligned width, aligned height
125      */
126     void getAlignedWidthAndHeight(const private_handle_t *hnd, int& aligned_w, int& aligned_h);
127 
128     /*
129      * Function to compute the adreno aligned width and aligned height
130      * based on the width and format.
131      *
132      * @return aligned width, aligned height
133      */
134     void getGpuAlignedWidthHeight(int width, int height, int format,
135                             int tileEnabled, int& alignedw, int &alignedh);
136 
137     /*
138      * Function to compute unaligned width and unaligned height based on
139      * private handle
140      *
141      * @return unaligned width, unaligned height
142      */
143     void getUnalignedWidthAndHeight(const private_handle_t *hnd, int& unaligned_w,
144                             int& unaligned_h);
145     /*
146      * Function to query whether GPU supports UBWC for given HAL format
147      * @return > 0 : supported
148      *           0 : not supported
149      */
150     int isUBWCSupportedByGPU(int format);
151 
152     /*
153      * Function to get the corresponding Adreno format for given HAL format
154      */
155     ADRENOPIXELFORMAT getGpuPixelFormat(int hal_format);
156 
157     private:
158         // Overriding flag to disable UBWC alloc for graphics stack
159         int  gfx_ubwc_disable;
160         // Pointer to the padding library.
161         void *libadreno_utils;
162 
163         // link(s)to adreno surface padding library.
164         int (*LINK_adreno_compute_padding) (int width, int bpp,
165                                                 int surface_tile_height,
166                                                 int screen_tile_height,
167                                                 int padding_threshold);
168 
169         void (*LINK_adreno_compute_aligned_width_and_height) (int width,
170                                                 int height,
171                                                 int bpp,
172                                                 int tile_mode,
173                                                 int raster_mode,
174                                                 int padding_threshold,
175                                                 int *aligned_w,
176                                                 int *aligned_h);
177 
178         void(*LINK_adreno_compute_compressedfmt_aligned_width_and_height)(
179                                                 int width,
180                                                 int height,
181                                                 int format,
182                                                 int tile_mode,
183                                                 int raster_mode,
184                                                 int padding_threshold,
185                                                 int *aligned_w,
186                                                 int *aligned_h,
187                                                 int *bpp);
188 
189         int (*LINK_adreno_isUBWCSupportedByGpu) (ADRENOPIXELFORMAT format);
190 
191         unsigned int (*LINK_adreno_get_gpu_pixel_alignment) ();
192 };
193 
194 
195 class MDPCapabilityInfo : public android::Singleton <MDPCapabilityInfo>
196 {
197     int isUBwcSupported = 0;
198     int isWBUBWCSupported = 0;
199 
200     public:
201         MDPCapabilityInfo();
202         /*
203         * Function to return whether MDP supports UBWC feature
204         *
205         * @return  1 : supported
206         *          0 : not supported
207         */
isUBwcSupportedByMDP()208         int isUBwcSupportedByMDP() { return isUBwcSupported; }
209         /*
210         * Function to return whether MDP WB block outputs UBWC format
211         *
212         * @return  1 : supported
213         *          0 : not supported
214         */
isWBUBWCSupportedByMDP()215         int isWBUBWCSupportedByMDP() { return isWBUBWCSupported; }
216 };
217 
218 #endif /* GR_H_ */
219