hwc_utils.h revision 27c1d65113aaac5c577523132b5b990552284ba9
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * Copyright (C) 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 HWC_UTILS_H 19#define HWC_UTILS_H 20 21#define HWC_REMOVE_DEPRECATED_VERSIONS 1 22#include <fcntl.h> 23#include <hardware/hwcomposer.h> 24#include <gr.h> 25#include <gralloc_priv.h> 26 27#define ALIGN_TO(x, align) (((x) + ((align)-1)) & ~((align)-1)) 28#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) 29#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) 30#define FINAL_TRANSFORM_MASK 0x000F 31#define MAX_NUM_DISPLAYS 4 //Yes, this is ambitious 32 33//Fwrd decls 34struct hwc_context_t; 35struct framebuffer_device_t; 36 37namespace qService { 38class QService; 39} 40 41namespace overlay { 42class Overlay; 43} 44 45namespace qhwc { 46//fwrd decl 47class QueuedBufferStore; 48class ExternalDisplay; 49 50struct MDPInfo { 51 int version; 52 char panel; 53 bool hasOverlay; 54}; 55 56struct DisplayAttributes { 57 uint32_t vsync_period; //nanos 58 uint32_t xres; 59 uint32_t yres; 60 float xdpi; 61 float ydpi; 62 int fd; 63 bool connected; //Applies only to pluggable disp. 64 //Connected does not mean it ready to use. 65 //It should be active also. (UNBLANKED) 66 bool isActive; 67}; 68 69struct ListStats { 70 int numAppLayers; //Total - 1, excluding FB layer. 71 int skipCount; 72 int fbLayerIndex; //Always last for now. = numAppLayers 73 //Video specific 74 int yuvCount; 75 int yuvIndex; 76}; 77 78enum { 79 HWC_MDPCOMP = 0x00000002, 80 HWC_LAYER_RESERVED_0 = 0x00000004, 81 HWC_LAYER_RESERVED_1 = 0x00000008 82}; 83 84 85// ----------------------------------------------------------------------------- 86// Utility functions - implemented in hwc_utils.cpp 87void dumpLayer(hwc_layer_1_t const* l); 88void setListStats(hwc_context_t *ctx, const hwc_display_contents_1_t *list, 89 int dpy); 90void initContext(hwc_context_t *ctx); 91void closeContext(hwc_context_t *ctx); 92//Crops source buffer against destination and FB boundaries 93void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst, 94 const int fbWidth, const int fbHeight, int orient); 95 96bool isExternalActive(hwc_context_t* ctx); 97 98//Sync point impl. 99int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy); 100 101// Inline utility functions 102static inline bool isSkipLayer(const hwc_layer_1_t* l) { 103 return (UNLIKELY(l && (l->flags & HWC_SKIP_LAYER))); 104} 105 106// Returns true if the buffer is yuv 107static inline bool isYuvBuffer(const private_handle_t* hnd) { 108 return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO)); 109} 110 111// Returns true if the buffer is secure 112static inline bool isSecureBuffer(const private_handle_t* hnd) { 113 return (hnd && (private_handle_t::PRIV_FLAGS_SECURE_BUFFER & hnd->flags)); 114} 115//Return true if buffer is marked locked 116static inline bool isBufferLocked(const private_handle_t* hnd) { 117 return (hnd && (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags)); 118} 119 120//Return true if buffer is for external display only 121static inline bool isExtOnly(const private_handle_t* hnd) { 122 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY)); 123} 124 125//Return true if buffer is for external display only with a BLOCK flag. 126static inline bool isExtBlock(const private_handle_t* hnd) { 127 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK)); 128} 129 130//Return true if buffer is for external display only with a Close Caption flag. 131static inline bool isExtCC(const private_handle_t* hnd) { 132 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_CC)); 133} 134 135// Initialize uevent thread 136void init_uevent_thread(hwc_context_t* ctx); 137// Initialize vsync thread 138void init_vsync_thread(hwc_context_t* ctx); 139 140inline void getLayerResolution(const hwc_layer_1_t* layer, 141 int& width, int& height) 142{ 143 hwc_rect_t displayFrame = layer->displayFrame; 144 width = displayFrame.right - displayFrame.left; 145 height = displayFrame.bottom - displayFrame.top; 146} 147 148static inline int openFb(int dpy) { 149 int fd = -1; 150 const char *devtmpl = "/dev/graphics/fb%u"; 151 char name[64] = {0}; 152 snprintf(name, 64, devtmpl, dpy); 153 fd = open(name, O_RDWR); 154 return fd; 155} 156 157template <class T> 158inline void swap(T& a, T& b) { 159 T tmp = a; 160 a = b; 161 b = tmp; 162} 163 164}; //qhwc namespace 165 166struct vsync_state { 167 pthread_mutex_t lock; 168 pthread_cond_t cond; 169 bool enable; 170}; 171 172// ----------------------------------------------------------------------------- 173// HWC context 174// This structure contains overall state 175struct hwc_context_t { 176 hwc_composer_device_1_t device; 177 const hwc_procs_t* proc; 178 179 //Private hwc handlers 180 struct Callbacks { 181 void (*onExtDisconnect)(const Callbacks& priv_proc); 182 hwc_context_t *ctx; 183 }; 184 185 Callbacks priv_proc; 186 187 int overlayInUse[HWC_NUM_DISPLAY_TYPES]; 188 189 //Framebuffer device 190 framebuffer_device_t *mFbDev; 191 192 //Overlay object - NULL for non overlay devices 193 overlay::Overlay *mOverlay[HWC_NUM_DISPLAY_TYPES]; 194 195 //QService object 196 qService::QService *mQService; 197 198 // External display related information 199 qhwc::ExternalDisplay *mExtDisplay; 200 201 qhwc::MDPInfo mMDP; 202 203 qhwc::DisplayAttributes dpyAttr[HWC_NUM_DISPLAY_TYPES]; 204 205 qhwc::ListStats listStats[HWC_NUM_DISPLAY_TYPES]; 206 207 //Securing in progress indicator 208 bool mSecuring; 209 210 //Display in secure mode indicator 211 bool mSecureMode; 212 213 //Lock to prevent set from being called while blanking 214 mutable Locker mBlankLock; 215 //Vsync 216 struct vsync_state vstate; 217}; 218 219#endif //HWC_UTILS_H 220