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 #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 38 inline unsigned int roundUpToPageSize(unsigned int x) { 39 return (x + (getpagesize()-1)) & ~(getpagesize()-1); 40 } 41 42 template <class Type> 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 58 // Attributes include aligned width, aligned height, tileEnabled and size of the buffer 59 void getBufferAttributes(int width, int height, int format, int usage, 60 int& alignedw, int &alignedh, 61 int& tileEnabled, unsigned int &size); 62 63 64 bool isMacroTileEnabled(int format, int usage); 65 66 int decideBufferHandlingMechanism(int format, const char *compositionUsed, 67 int hasBlitEngine, int *needConversion, 68 int *useBufferDirectly); 69 70 // Allocate buffer from width, height, format into a private_handle_t 71 // It is the responsibility of the caller to free the buffer 72 int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage); 73 void free_buffer(private_handle_t *hnd); 74 int getYUVPlaneInfo(private_handle_t* pHnd, struct android_ycbcr* ycbcr); 75 int getRgbDataAddress(private_handle_t* pHnd, void** rgb_data); 76 77 // To query if UBWC is enabled, based on format and usage flags 78 bool isUBwcEnabled(int format, int usage); 79 80 // Function to check if the format is an RGB format 81 bool isUncompressedRgbFormat(int format); 82 83 /*****************************************************************************/ 84 85 class Locker { 86 pthread_mutex_t mutex; 87 pthread_cond_t cond; 88 public: 89 class Autolock { 90 Locker& locker; 91 public: 92 inline Autolock(Locker& locker) : locker(locker) { locker.lock(); } 93 inline ~Autolock() { locker.unlock(); } 94 }; 95 inline Locker() { 96 pthread_mutex_init(&mutex, 0); 97 pthread_cond_init(&cond, 0); 98 } 99 inline ~Locker() { 100 pthread_mutex_destroy(&mutex); 101 pthread_cond_destroy(&cond); 102 } 103 inline void lock() { pthread_mutex_lock(&mutex); } 104 inline void wait() { pthread_cond_wait(&cond, &mutex); } 105 inline void unlock() { pthread_mutex_unlock(&mutex); } 106 inline void signal() { pthread_cond_signal(&cond); } 107 }; 108 109 110 class AdrenoMemInfo : public android::Singleton <AdrenoMemInfo> 111 { 112 public: 113 AdrenoMemInfo(); 114 115 ~AdrenoMemInfo(); 116 117 /* 118 * Function to compute aligned width and aligned height based on 119 * width, height, format and usage flags. 120 * 121 * @return aligned width, aligned height 122 */ 123 void getAlignedWidthAndHeight(int width, int height, int format, 124 int usage, int& aligned_w, int& aligned_h); 125 126 /* 127 * Function to compute aligned width and aligned height based on 128 * private handle 129 * 130 * @return aligned width, aligned height 131 */ 132 void getAlignedWidthAndHeight(const private_handle_t *hnd, int& aligned_w, int& aligned_h); 133 134 /* 135 * Function to compute the adreno aligned width and aligned height 136 * based on the width and format. 137 * 138 * @return aligned width, aligned height 139 */ 140 void getGpuAlignedWidthHeight(int width, int height, int format, 141 int tileEnabled, int& alignedw, int &alignedh); 142 143 /* 144 * Function to compute unaligned width and unaligned height based on 145 * private handle 146 * 147 * @return unaligned width, unaligned height 148 */ 149 void getUnalignedWidthAndHeight(const private_handle_t *hnd, int& unaligned_w, 150 int& unaligned_h); 151 152 /* 153 * Function to return whether GPU support MacroTile feature 154 * 155 * @return >0 : supported 156 * 0 : not supported 157 */ 158 int isMacroTilingSupportedByGPU(); 159 160 /* 161 * Function to query whether GPU supports UBWC for given HAL format 162 * @return > 0 : supported 163 * 0 : not supported 164 */ 165 int isUBWCSupportedByGPU(int format); 166 167 /* 168 * Function to get the corresponding Adreno format for given HAL format 169 */ 170 ADRENOPIXELFORMAT getGpuPixelFormat(int hal_format); 171 172 private: 173 // Overriding flag to disable UBWC alloc for graphics stack 174 int gfx_ubwc_disable; 175 // Pointer to the padding library. 176 void *libadreno_utils; 177 178 // link(s)to adreno surface padding library. 179 int (*LINK_adreno_compute_padding) (int width, int bpp, 180 int surface_tile_height, 181 int screen_tile_height, 182 int padding_threshold); 183 184 void (*LINK_adreno_compute_aligned_width_and_height) (int width, 185 int height, 186 int bpp, 187 int tile_mode, 188 int raster_mode, 189 int padding_threshold, 190 int *aligned_w, 191 int *aligned_h); 192 193 int (*LINK_adreno_isMacroTilingSupportedByGpu) (void); 194 195 void(*LINK_adreno_compute_compressedfmt_aligned_width_and_height)( 196 int width, 197 int height, 198 int format, 199 int tile_mode, 200 int raster_mode, 201 int padding_threshold, 202 int *aligned_w, 203 int *aligned_h, 204 int *bpp); 205 206 int (*LINK_adreno_isUBWCSupportedByGpu) (ADRENOPIXELFORMAT format); 207 208 unsigned int (*LINK_adreno_get_gpu_pixel_alignment) (); 209 }; 210 211 212 class MDPCapabilityInfo : public android::Singleton <MDPCapabilityInfo> 213 { 214 int isMacroTileSupported = 0; 215 int isUBwcSupported = 0; 216 int isWBUBWCSupported = 0; 217 218 public: 219 MDPCapabilityInfo(); 220 /* 221 * Function to return whether MDP support MacroTile feature 222 * 223 * @return 1 : supported 224 * 0 : not supported 225 */ 226 int isMacroTilingSupportedByMDP() { return isMacroTileSupported; } 227 /* 228 * Function to return whether MDP supports UBWC feature 229 * 230 * @return 1 : supported 231 * 0 : not supported 232 */ 233 int isUBwcSupportedByMDP() { return isUBwcSupported; } 234 /* 235 * Function to return whether MDP WB block outputs UBWC format 236 * 237 * @return 1 : supported 238 * 0 : not supported 239 */ 240 int isWBUBWCSupportedByMDP() { return isWBUBWCSupported; } 241 }; 242 243 #endif /* GR_H_ */ 244