Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
| 3 | |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of Code Aurora Forum, Inc. nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #ifndef INCLUDE_LIBQCOM_UI |
| 31 | #define INCLUDE_LIBQCOM_UI |
| 32 | |
| 33 | #include <cutils/native_handle.h> |
| 34 | #include <ui/GraphicBuffer.h> |
| 35 | #include <hardware/hwcomposer.h> |
| 36 | #include <hardware/hwcomposer_defs.h> |
| 37 | #include <ui/Region.h> |
| 38 | #include <EGL/egl.h> |
| 39 | #include <utils/Singleton.h> |
| 40 | #include <cutils/properties.h> |
| 41 | #include "../libgralloc/gralloc_priv.h" |
| 42 | |
| 43 | using namespace android; |
| 44 | using android::sp; |
| 45 | using android::GraphicBuffer; |
| 46 | |
| 47 | #define HWC_BYPASS_INDEX_MASK 0x00000030 |
| 48 | |
| 49 | /* |
| 50 | * Qcom specific Native Window perform operations |
| 51 | */ |
| 52 | enum { |
| 53 | NATIVE_WINDOW_SET_BUFFERS_SIZE = 0x10000000, |
| 54 | NATIVE_WINDOW_UPDATE_BUFFERS_GEOMETRY = 0x20000000, |
| 55 | NATIVE_WINDOW_SET_S3D_FORMAT = 0x40000000, |
| 56 | }; |
| 57 | |
| 58 | // Enum containing the supported composition types |
| 59 | enum { |
| 60 | COMPOSITION_TYPE_GPU = 0, |
| 61 | COMPOSITION_TYPE_MDP = 0x1, |
| 62 | COMPOSITION_TYPE_C2D = 0x2, |
| 63 | COMPOSITION_TYPE_CPU = 0x4, |
| 64 | COMPOSITION_TYPE_DYN = 0x8 |
| 65 | }; |
| 66 | |
| 67 | /* |
| 68 | * Layer Attributes |
| 69 | */ |
| 70 | enum eLayerAttrib { |
| 71 | LAYER_UPDATE_STATUS, |
| 72 | LAYER_ASYNCHRONOUS_STATUS, |
| 73 | }; |
| 74 | |
| 75 | /* |
| 76 | * Layer Flags |
| 77 | */ |
| 78 | enum { |
| 79 | LAYER_UPDATING = 1<<0, |
| 80 | LAYER_ASYNCHRONOUS = 1<<1, |
| 81 | }; |
| 82 | |
| 83 | /* |
| 84 | * Flags set by the layer and sent to HWC |
| 85 | */ |
| 86 | enum { |
| 87 | HWC_LAYER_NOT_UPDATING = 0x00000002, |
| 88 | HWC_LAYER_ASYNCHRONOUS = 0x00000004, |
| 89 | HWC_USE_ORIGINAL_RESOLUTION = 0x10000000, |
| 90 | HWC_DO_NOT_USE_OVERLAY = 0x20000000, |
| 91 | HWC_COMP_BYPASS = 0x40000000, |
| 92 | HWC_USE_EXT_ONLY = 0x80000000, //Layer displayed on external only |
| 93 | HWC_USE_EXT_BLOCK = 0x01000000, //Layer displayed on external only |
| 94 | HWC_BYPASS_RESERVE_0 = 0x00000010, |
| 95 | HWC_BYPASS_RESERVE_1 = 0x00000020, |
| 96 | }; |
| 97 | |
| 98 | enum HWCCompositionType { |
| 99 | HWC_USE_GPU = HWC_FRAMEBUFFER, // This layer is to be handled by Surfaceflinger |
| 100 | HWC_USE_OVERLAY = HWC_OVERLAY, // This layer is to be handled by the overlay |
| 101 | HWC_USE_COPYBIT // This layer is to be handled by copybit |
| 102 | }; |
| 103 | |
| 104 | enum external_display { |
| 105 | EXT_DISPLAY_OFF, |
| 106 | EXT_DISPLAY_HDMI, |
| 107 | EXT_DISPLAY_WIFI |
| 108 | }; |
| 109 | |
| 110 | /* |
| 111 | * Structure to hold the buffer geometry |
| 112 | */ |
| 113 | struct qBufGeometry { |
| 114 | int width; |
| 115 | int height; |
| 116 | int format; |
| 117 | void set(int w, int h, int f) { |
| 118 | width = w; |
| 119 | height = h; |
| 120 | format = f; |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | #ifndef DEBUG_CALC_FPS |
| 125 | #define CALC_FPS() ((void)0) |
| 126 | #define CALC_INIT() ((void)0) |
| 127 | #else |
| 128 | #define CALC_FPS() CalcFps::getInstance().Fps() |
| 129 | #define CALC_INIT() CalcFps::getInstance().Init() |
| 130 | |
| 131 | class CalcFps : public Singleton<CalcFps> { |
| 132 | public: |
| 133 | CalcFps(); |
| 134 | ~CalcFps(); |
| 135 | |
| 136 | void Init(); |
| 137 | void Fps(); |
| 138 | |
| 139 | private: |
| 140 | static const unsigned int MAX_FPS_CALC_PERIOD_IN_FRAMES = 128; |
| 141 | static const unsigned int MAX_FRAMEARRIVAL_STEPS = 50; |
| 142 | static const unsigned int MAX_DEBUG_FPS_LEVEL = 2; |
| 143 | |
| 144 | struct debug_fps_metadata_t { |
| 145 | /*fps calculation based on time or number of frames*/ |
| 146 | enum DfmType { |
| 147 | DFM_FRAMES = 0, |
| 148 | DFM_TIME = 1, |
| 149 | }; |
| 150 | |
| 151 | DfmType type; |
| 152 | |
| 153 | /* indicates how much time do we wait till we calculate FPS */ |
| 154 | unsigned long time_period; |
| 155 | |
| 156 | /*indicates how much time elapsed since we report fps*/ |
| 157 | float time_elapsed; |
| 158 | |
| 159 | /* indicates how many frames do we wait till we calculate FPS */ |
| 160 | unsigned int period; |
| 161 | /* current frame, will go upto period, and then reset */ |
| 162 | unsigned int curr_frame; |
| 163 | /* frame will arrive at a multiple of 16666 us at the display. |
| 164 | This indicates how many steps to consider for our calculations. |
| 165 | For example, if framearrival_steps = 10, then the frame that arrived |
| 166 | after 166660 us or more will be ignored. |
| 167 | */ |
| 168 | unsigned int framearrival_steps; |
| 169 | /* ignorethresh_us = framearrival_steps * 16666 */ |
| 170 | nsecs_t ignorethresh_us; |
| 171 | /* used to calculate the actual frame arrival step, the times might not be |
| 172 | accurate |
| 173 | */ |
| 174 | unsigned int margin_us; |
| 175 | |
| 176 | /* actual data storage */ |
| 177 | nsecs_t framearrivals[MAX_FPS_CALC_PERIOD_IN_FRAMES]; |
| 178 | nsecs_t accum_framearrivals[MAX_FRAMEARRIVAL_STEPS]; |
| 179 | }; |
| 180 | |
| 181 | private: |
| 182 | void populate_debug_fps_metadata(void); |
| 183 | void print_fps(float fps); |
| 184 | void calc_fps(nsecs_t currtime_us); |
| 185 | |
| 186 | private: |
| 187 | debug_fps_metadata_t debug_fps_metadata; |
| 188 | unsigned int debug_fps_level; |
| 189 | }; |
| 190 | #endif |
| 191 | |
| 192 | #if 0 |
| 193 | class QCBaseLayer |
| 194 | { |
| 195 | // int mS3DFormat; |
| 196 | int32_t mComposeS3DFormat; |
| 197 | public: |
| 198 | QCBaseLayer() |
| 199 | { |
| 200 | mComposeS3DFormat = 0; |
| 201 | } |
| 202 | enum { // S3D formats |
| 203 | eS3D_SIDE_BY_SIDE = 0x10000, |
| 204 | eS3D_TOP_BOTTOM = 0x20000 |
| 205 | }; |
| 206 | /* |
| 207 | virtual status_t setStereoscopic3DFormat(int format) { mS3DFormat = format; return 0; } |
| 208 | virtual int getStereoscopic3DFormat() const { return mS3DFormat; } |
| 209 | */ |
| 210 | void setS3DComposeFormat (int32_t hints) |
| 211 | { |
| 212 | if (hints & HWC_HINT_DRAW_S3D_SIDE_BY_SIDE) |
| 213 | mComposeS3DFormat = eS3D_SIDE_BY_SIDE; |
| 214 | else if (hints & HWC_HINT_DRAW_S3D_TOP_BOTTOM) |
| 215 | mComposeS3DFormat = eS3D_TOP_BOTTOM; |
| 216 | else |
| 217 | mComposeS3DFormat = 0; |
| 218 | } |
| 219 | int32_t needsS3DCompose () const { return mComposeS3DFormat; } |
| 220 | }; |
| 221 | #endif |
| 222 | |
| 223 | /* |
| 224 | * Function to check if the allocated buffer is of the correct size. |
| 225 | * Reallocate the buffer with the correct size, if the size doesn't |
| 226 | * match |
| 227 | * |
| 228 | * @param: handle of the allocated buffer |
| 229 | * @param: requested size for the buffer |
| 230 | * @param: usage flags |
| 231 | * |
| 232 | * return 0 on success |
| 233 | */ |
| 234 | int checkBuffer(native_handle_t *buffer_handle, int size, int usage); |
| 235 | |
| 236 | /* |
| 237 | * Checks if the format is supported by the GPU. |
| 238 | * |
| 239 | * @param: format to check |
| 240 | * |
| 241 | * @return true if the format is supported by the GPU. |
| 242 | */ |
| 243 | bool isGPUSupportedFormat(int format); |
| 244 | |
| 245 | /* |
| 246 | * Adreno is not optimized for GL_TEXTURE_EXTERNAL_OES |
| 247 | * texure target. DO NOT choose TEXTURE_EXTERNAL_OES |
| 248 | * target for RGB formats. |
| 249 | * |
| 250 | * Based on the pixel format, decide the texture target. |
| 251 | * |
| 252 | * @param : pixel format to check |
| 253 | * |
| 254 | * @return : GL_TEXTURE_2D for RGB formats, and |
| 255 | * GL_TEXTURE_EXTERNAL_OES for YUV formats. |
| 256 | * |
| 257 | */ |
| 258 | |
| 259 | int decideTextureTarget (const int pixel_format); |
| 260 | |
| 261 | /* |
| 262 | * Gets the number of arguments required for this operation. |
| 263 | * |
| 264 | * @param: operation whose argument count is required. |
| 265 | * |
| 266 | * @return -EINVAL if the operation is invalid. |
| 267 | */ |
| 268 | int getNumberOfArgsForOperation(int operation); |
| 269 | |
| 270 | /* |
| 271 | * Checks if memory needs to be reallocated for this buffer. |
| 272 | * |
| 273 | * @param: Geometry of the current buffer. |
| 274 | * @param: Required Geometry. |
| 275 | * @param: Geometry of the updated buffer. |
| 276 | * |
| 277 | * @return True if a memory reallocation is required. |
| 278 | */ |
| 279 | bool needNewBuffer(const qBufGeometry currentGeometry, |
| 280 | const qBufGeometry requiredGeometry, |
| 281 | const qBufGeometry updatedGeometry); |
| 282 | |
| 283 | /* |
| 284 | * Update the geometry of this buffer without reallocation. |
| 285 | * |
| 286 | * @param: buffer whose geometry needs to be updated. |
| 287 | * @param: Updated buffer geometry |
| 288 | */ |
| 289 | int updateBufferGeometry(sp<GraphicBuffer> buffer, const qBufGeometry bufGeometry); |
| 290 | |
| 291 | /* |
| 292 | * Update the S3D format of this buffer. |
| 293 | * |
| 294 | * @param: buffer whosei S3D format needs to be updated. |
| 295 | * @param: Updated buffer S3D format |
| 296 | */ |
| 297 | int updateBufferS3DFormat(sp<GraphicBuffer> buffer, const int s3dFormat); |
| 298 | |
| 299 | /* |
| 300 | * Updates the flags for the layer |
| 301 | * |
| 302 | * @param: Attribute |
| 303 | * @param: Identifies if the attribute was enabled or disabled. |
| 304 | * @param: current Layer flags. |
| 305 | * |
| 306 | * @return: Flags for the layer |
| 307 | */ |
| 308 | int updateLayerQcomFlags(eLayerAttrib attribute, bool enable, int& currentFlags); |
| 309 | |
| 310 | /* |
| 311 | * Gets the per frame HWC flags for this layer. |
| 312 | * |
| 313 | * @param: current hwcl flags |
| 314 | * @param: current layerFlags |
| 315 | * |
| 316 | * @return: the per frame flags. |
| 317 | */ |
| 318 | int getPerFrameFlags(int hwclFlags, int layerFlags); |
| 319 | |
| 320 | /* |
| 321 | * Checks if FB is updated by this composition type |
| 322 | * |
| 323 | * @param: composition type |
| 324 | * @return: true if FB is updated, false if not |
| 325 | */ |
| 326 | |
| 327 | bool isUpdatingFB(HWCCompositionType compositionType); |
| 328 | |
| 329 | /* |
| 330 | * Get the current composition Type |
| 331 | * |
| 332 | * @return the compositon Type |
| 333 | */ |
| 334 | int getCompositionType(); |
| 335 | |
| 336 | /* |
| 337 | * Clear region implementation for C2D/MDP versions. |
| 338 | * |
| 339 | * @param: region to be cleared |
| 340 | * @param: EGL Display |
| 341 | * @param: EGL Surface |
| 342 | * |
| 343 | * @return 0 on success |
| 344 | */ |
| 345 | int qcomuiClearRegion(Region region, EGLDisplay dpy, EGLSurface sur); |
| 346 | |
| 347 | /* |
| 348 | * Handles the externalDisplay event |
| 349 | * HDMI has highest priority compared to WifiDisplay |
| 350 | * Based on the current and the new display event, decides the |
| 351 | * external display to be enabled |
| 352 | * |
| 353 | * @param: newEvent - new external event |
| 354 | * @param: currEvent - currently enabled external event |
| 355 | * @return: external display to be enabled |
| 356 | * |
| 357 | */ |
| 358 | external_display handleEventHDMI(external_display newEvent, external_display |
| 359 | currEvent); |
| 360 | |
| 361 | /* |
| 362 | * Checks if layers need to be dumped based on system property "debug.sf.dump" |
| 363 | * for raw dumps and "debug.sf.dump.png" for png dumps. |
| 364 | * |
| 365 | * For example, to dump 25 frames in raw format, do, |
| 366 | * adb shell setprop debug.sf.dump 25 |
| 367 | * Layers are dumped in a time-stamped location: /data/sfdump*. |
| 368 | * |
| 369 | * To dump 10 frames in png format, do, |
| 370 | * adb shell setprop debug.sf.dump.png 10 |
| 371 | * To dump another 25 or so frames in raw format, do, |
| 372 | * adb shell setprop debug.sf.dump 26 |
| 373 | * |
| 374 | * To turn off logcat logging of layer-info, set both properties to 0, |
| 375 | * adb shell setprop debug.sf.dump.png 0 |
| 376 | * adb shell setprop debug.sf.dump 0 |
| 377 | * |
| 378 | * @return: true if layers need to be dumped (or logcat-ed). |
| 379 | */ |
| 380 | bool needToDumpLayers(); |
| 381 | |
| 382 | /* |
| 383 | * Dumps a layer's info into logcat and its buffer into raw/png files. |
| 384 | * |
| 385 | * @param: moduleCompositionType - Composition type set in hwcomposer module. |
| 386 | * @param: listFlags - Flags used in hwcomposer's list. |
| 387 | * @param: layerIndex - Index of layer being dumped. |
| 388 | * @param: hwLayers - Address of hwc_layer_t to log and dump. |
| 389 | * |
| 390 | */ |
| 391 | void dumpLayer(int moduleCompositionType, int listFlags, size_t layerIndex, |
| 392 | hwc_layer_t hwLayers[]); |
| 393 | |
| 394 | #endif // INCLUDE_LIBQCOM_UI |