Home | History | Annotate | Download | only in libhwcomposer
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  * Copyright (C)2012-2013, The Linux Foundation. All rights reserved.
      4  *
      5  * Not a Contribution, Apache license notifications and license are retained
      6  * for attribution purposes only.
      7  *
      8  * Licensed under the Apache License, Version 2.0 (the "License");
      9  * you may not use this file except in compliance with the License.
     10  * You may obtain a copy of the License at
     11  *
     12  *      http://www.apache.org/licenses/LICENSE-2.0
     13  *
     14  * Unless required by applicable law or agreed to in writing, software
     15  * distributed under the License is distributed on an "AS IS" BASIS,
     16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     17  * See the License for the specific language governing permissions and
     18  * limitations under the License.
     19  */
     20 
     21 #ifndef HWC_UTILS_H
     22 #define HWC_UTILS_H
     23 
     24 #define HWC_REMOVE_DEPRECATED_VERSIONS 1
     25 #include <fcntl.h>
     26 #include <hardware/hwcomposer.h>
     27 #include <gr.h>
     28 #include <gralloc_priv.h>
     29 #include <utils/String8.h>
     30 #include <linux/fb.h>
     31 #include "qdMetaData.h"
     32 #include <overlayUtils.h>
     33 #include <cutils/sockets.h>
     34 
     35 #define ALIGN_TO(x, align)     (((x) + ((align)-1)) & ~((align)-1))
     36 #define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
     37 #define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))
     38 #define MAX_NUM_LAYERS 32 //includes fb layer
     39 #define MAX_DISPLAY_DIM 2048
     40 
     41 // For support of virtual displays
     42 #define HWC_DISPLAY_VIRTUAL     (HWC_DISPLAY_EXTERNAL+1)
     43 #define MAX_DISPLAYS            (HWC_NUM_DISPLAY_TYPES+1)
     44 #define DAEMON_SOCKET "pps"
     45 //Fwrd decls
     46 struct hwc_context_t;
     47 
     48 namespace ovutils = overlay::utils;
     49 
     50 namespace overlay {
     51 class Overlay;
     52 class Rotator;
     53 class RotMgr;
     54 }
     55 
     56 namespace qhwc {
     57 //fwrd decl
     58 class QueuedBufferStore;
     59 class ExternalDisplay;
     60 class IFBUpdate;
     61 class IVideoOverlay;
     62 class MDPComp;
     63 class CopyBit;
     64 
     65 
     66 struct MDPInfo {
     67     int version;
     68     char panel;
     69     bool hasOverlay;
     70 };
     71 
     72 struct DisplayAttributes {
     73     uint32_t vsync_period; //nanos
     74     uint32_t xres;
     75     uint32_t yres;
     76     uint32_t stride;
     77     float xdpi;
     78     float ydpi;
     79     int fd;
     80     bool connected; //Applies only to pluggable disp.
     81     //Connected does not mean it ready to use.
     82     //It should be active also. (UNBLANKED)
     83     bool isActive;
     84     // In pause state, composition is bypassed
     85     // used for WFD displays only
     86     bool isPause;
     87 };
     88 
     89 struct ListStats {
     90     int numAppLayers; //Total - 1, excluding FB layer.
     91     int skipCount;
     92     int fbLayerIndex; //Always last for now. = numAppLayers
     93     //Video specific
     94     int yuvCount;
     95     int yuvIndices[MAX_NUM_LAYERS];
     96     bool needsAlphaScale;
     97     bool preMultipliedAlpha;
     98     bool planeAlpha;
     99 };
    100 
    101 struct LayerProp {
    102     uint32_t mFlags; //qcom specific layer flags
    103     LayerProp():mFlags(0) {};
    104 };
    105 
    106 struct VsyncState {
    107     bool enable;
    108     bool fakevsync;
    109 };
    110 
    111 struct CablProp {
    112     bool enabled;
    113     bool start;
    114     bool videoOnly;
    115     //daemon_socket for connection to pp-daemon
    116     int daemon_socket;
    117 };
    118 
    119 // LayerProp::flag values
    120 enum {
    121     HWC_MDPCOMP = 0x00000001,
    122     HWC_COPYBIT = 0x00000002,
    123 };
    124 
    125 class LayerRotMap {
    126 public:
    127     LayerRotMap() { reset(); }
    128     enum { MAX_SESS = 3 };
    129     void add(hwc_layer_1_t* layer, overlay::Rotator *rot);
    130     void reset();
    131     uint32_t getCount() const;
    132     hwc_layer_1_t* getLayer(uint32_t index) const;
    133     overlay::Rotator* getRot(uint32_t index) const;
    134     void setReleaseFd(const int& fence);
    135 private:
    136     hwc_layer_1_t* mLayer[MAX_SESS];
    137     overlay::Rotator* mRot[MAX_SESS];
    138     uint32_t mCount;
    139 };
    140 
    141 inline uint32_t LayerRotMap::getCount() const {
    142     return mCount;
    143 }
    144 
    145 inline hwc_layer_1_t* LayerRotMap::getLayer(uint32_t index) const {
    146     if(index >= mCount) return NULL;
    147     return mLayer[index];
    148 }
    149 
    150 inline overlay::Rotator* LayerRotMap::getRot(uint32_t index) const {
    151     if(index >= mCount) return NULL;
    152     return mRot[index];
    153 }
    154 
    155 // -----------------------------------------------------------------------------
    156 // Utility functions - implemented in hwc_utils.cpp
    157 void dumpLayer(hwc_layer_1_t const* l);
    158 void setListStats(hwc_context_t *ctx, const hwc_display_contents_1_t *list,
    159         int dpy);
    160 void initContext(hwc_context_t *ctx);
    161 void closeContext(hwc_context_t *ctx);
    162 //Crops source buffer against destination and FB boundaries
    163 void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
    164                          const hwc_rect_t& scissor, int orient);
    165 void getNonWormholeRegion(hwc_display_contents_1_t* list,
    166                               hwc_rect_t& nwr);
    167 bool isSecuring(hwc_context_t* ctx, hwc_layer_1_t const* layer);
    168 bool isSecureModePolicy(int mdpVersion);
    169 bool isExternalActive(hwc_context_t* ctx);
    170 bool needsScaling(hwc_layer_1_t const* layer);
    171 bool isAlphaPresent(hwc_layer_1_t const* layer);
    172 bool setupBasePipe(hwc_context_t *ctx);
    173 int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable);
    174 int getBlending(int blending);
    175 
    176 //Helper function to dump logs
    177 void dumpsys_log(android::String8& buf, const char* fmt, ...);
    178 
    179 /* Calculates the destination position based on the action safe rectangle */
    180 void getActionSafePosition(hwc_context_t *ctx, int dpy, uint32_t& x,
    181                                         uint32_t& y, uint32_t& w, uint32_t& h);
    182 
    183 //Close acquireFenceFds of all layers of incoming list
    184 void closeAcquireFds(hwc_display_contents_1_t* list);
    185 
    186 //Sync point impl.
    187 int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
    188         int fd);
    189 
    190 //Trims a layer's source crop which is outside of screen boundary.
    191 void trimLayer(hwc_context_t *ctx, const int& dpy, const int& transform,
    192         hwc_rect_t& crop, hwc_rect_t& dst);
    193 
    194 //Sets appropriate mdp flags for a layer.
    195 void setMdpFlags(hwc_layer_1_t *layer,
    196         ovutils::eMdpFlags &mdpFlags,
    197         int rotDownscale = 0);
    198 
    199 //Routine to configure low resolution panels (<= 2048 width)
    200 int configureLowRes(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
    201         ovutils::eMdpFlags& mdpFlags, const ovutils::eZorder& z,
    202         const ovutils::eIsFg& isFg, const ovutils::eDest& dest,
    203         overlay::Rotator **rot);
    204 
    205 //Routine to configure high resolution panels (> 2048 width)
    206 int configureHighRes(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
    207         ovutils::eMdpFlags& mdpFlags, const ovutils::eZorder& z,
    208         const ovutils::eIsFg& isFg, const ovutils::eDest& lDest,
    209         const ovutils::eDest& rDest, overlay::Rotator **rot);
    210 
    211 // Inline utility functions
    212 static inline bool isSkipLayer(const hwc_layer_1_t* l) {
    213     return (UNLIKELY(l && (l->flags & HWC_SKIP_LAYER)));
    214 }
    215 
    216 // Returns true if the buffer is yuv
    217 static inline bool isYuvBuffer(const private_handle_t* hnd) {
    218     return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO));
    219 }
    220 
    221 // Returns true if the buffer is secure
    222 static inline bool isSecureBuffer(const private_handle_t* hnd) {
    223     return (hnd && (private_handle_t::PRIV_FLAGS_SECURE_BUFFER & hnd->flags));
    224 }
    225 //Return true if buffer is marked locked
    226 static inline bool isBufferLocked(const private_handle_t* hnd) {
    227     return (hnd && (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags));
    228 }
    229 
    230 //Return true if buffer is for external display only
    231 static inline bool isExtOnly(const private_handle_t* hnd) {
    232     return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY));
    233 }
    234 
    235 //Return true if buffer is for external display only with a BLOCK flag.
    236 static inline bool isExtBlock(const private_handle_t* hnd) {
    237     return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK));
    238 }
    239 
    240 //Return true if buffer is for external display only with a Close Caption flag.
    241 static inline bool isExtCC(const private_handle_t* hnd) {
    242     return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_CC));
    243 }
    244 
    245 static inline int getWidth(const private_handle_t* hnd) {
    246     if(isYuvBuffer(hnd)) {
    247         MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
    248         if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
    249             return metadata->bufferDim.sliceWidth;
    250         }
    251     }
    252     return hnd->width;
    253 }
    254 
    255 static inline int getHeight(const private_handle_t* hnd) {
    256     if(isYuvBuffer(hnd)) {
    257         MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
    258         if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
    259             return metadata->bufferDim.sliceHeight;
    260         }
    261     }
    262     return hnd->height;
    263 }
    264 
    265 template<typename T> inline T max(T a, T b) { return (a > b) ? a : b; }
    266 template<typename T> inline T min(T a, T b) { return (a < b) ? a : b; }
    267 
    268 // Initialize uevent thread
    269 void init_uevent_thread(hwc_context_t* ctx);
    270 // Initialize vsync thread
    271 void init_vsync_thread(hwc_context_t* ctx);
    272 
    273 inline void getLayerResolution(const hwc_layer_1_t* layer,
    274                                int& width, int& height) {
    275     hwc_rect_t displayFrame  = layer->displayFrame;
    276     width = displayFrame.right - displayFrame.left;
    277     height = displayFrame.bottom - displayFrame.top;
    278 }
    279 
    280 static inline int openFb(int dpy) {
    281     int fd = -1;
    282     const char *devtmpl = "/dev/graphics/fb%u";
    283     char name[64] = {0};
    284     snprintf(name, 64, devtmpl, dpy);
    285     fd = open(name, O_RDWR);
    286     return fd;
    287 }
    288 
    289 template <class T>
    290 inline void swap(T& a, T& b) {
    291     T tmp = a;
    292     a = b;
    293     b = tmp;
    294 }
    295 
    296 }; //qhwc namespace
    297 
    298 // -----------------------------------------------------------------------------
    299 // HWC context
    300 // This structure contains overall state
    301 struct hwc_context_t {
    302     hwc_composer_device_1_t device;
    303     const hwc_procs_t* proc;
    304 
    305     //CopyBit objects
    306     qhwc::CopyBit *mCopyBit[MAX_DISPLAYS];
    307 
    308     //Overlay object - NULL for non overlay devices
    309     overlay::Overlay *mOverlay;
    310     //Holds a few rot objects
    311     overlay::RotMgr *mRotMgr;
    312 
    313     //Primary and external FB updater
    314     qhwc::IFBUpdate *mFBUpdate[MAX_DISPLAYS];
    315     // External display related information
    316     qhwc::ExternalDisplay *mExtDisplay;
    317     qhwc::MDPInfo mMDP;
    318     qhwc::VsyncState vstate;
    319     qhwc::DisplayAttributes dpyAttr[MAX_DISPLAYS];
    320     qhwc::ListStats listStats[MAX_DISPLAYS];
    321     qhwc::LayerProp *layerProp[MAX_DISPLAYS];
    322     qhwc::LayerRotMap *mLayerRotMap[MAX_DISPLAYS];
    323     qhwc::MDPComp *mMDPComp[MAX_DISPLAYS];
    324     qhwc::CablProp mCablProp;
    325 
    326     //Securing in progress indicator
    327     bool mSecuring;
    328     //External Display configuring progress indicator
    329     bool mExtDispConfiguring;
    330     //Display in secure mode indicator
    331     bool mSecureMode;
    332     //Lock to prevent set from being called while blanking
    333     mutable Locker mBlankLock;
    334     //Lock to protect set when detaching external disp
    335     mutable Locker mExtSetLock;
    336     //DMA used for rotator
    337     bool mDMAInUse;
    338     //MDP rotater needed
    339     bool mNeedsRotator;
    340     //Check if base pipe is set up
    341     bool mBasePipeSetup;
    342 };
    343 
    344 namespace qhwc {
    345 static inline bool isSkipPresent (hwc_context_t *ctx, int dpy) {
    346     return  ctx->listStats[dpy].skipCount;
    347 }
    348 
    349 static inline bool isYuvPresent (hwc_context_t *ctx, int dpy) {
    350     return  ctx->listStats[dpy].yuvCount;
    351 }
    352 };
    353 
    354 #endif //HWC_UTILS_H
    355