blob: d7200b061fbee85dac12e30ccc8da31d82c9fd1e [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Saurabh Shah56f610d2012-08-07 15:27:06 -07003 * Copyright (C) 2012, The Linux Foundation. All rights reserved.
Naseer Ahmed29a26812012-06-14 00:56:20 -07004 *
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
Naseer Ahmed72cf9762012-07-21 12:17:13 -070020
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070021#define HWC_REMOVE_DEPRECATED_VERSIONS 1
Saurabh Shah3e858eb2012-09-17 16:53:21 -070022#include <fcntl.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070023#include <hardware/hwcomposer.h>
Naseer Ahmed32ff2252012-09-29 01:41:21 -040024#include <gr.h>
Naseer Ahmed72cf9762012-07-21 12:17:13 -070025#include <gralloc_priv.h>
Naseer Ahmed1d183f52012-11-26 12:35:16 -050026#include <utils/String8.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070027
Naseer Ahmed31da0b12012-07-31 18:55:33 -070028#define ALIGN_TO(x, align) (((x) + ((align)-1)) & ~((align)-1))
Naseer Ahmed29a26812012-06-14 00:56:20 -070029#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
30#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
Naseer Ahmed31da0b12012-07-31 18:55:33 -070031#define FINAL_TRANSFORM_MASK 0x000F
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070032#define MAX_NUM_DISPLAYS 4 //Yes, this is ambitious
Naseer Ahmedb1c76322012-10-17 00:32:50 -040033#define MAX_NUM_LAYERS 32
Naseer Ahmed29a26812012-06-14 00:56:20 -070034
Naseer Ahmed72cf9762012-07-21 12:17:13 -070035//Fwrd decls
Naseer Ahmed29a26812012-06-14 00:56:20 -070036struct hwc_context_t;
Naseer Ahmed72cf9762012-07-21 12:17:13 -070037struct framebuffer_device_t;
38
Saurabh Shah56f610d2012-08-07 15:27:06 -070039namespace qService {
40class QService;
41}
42
Naseer Ahmed72cf9762012-07-21 12:17:13 -070043namespace overlay {
44class Overlay;
45}
46
Naseer Ahmed29a26812012-06-14 00:56:20 -070047namespace qhwc {
Naseer Ahmed72cf9762012-07-21 12:17:13 -070048//fwrd decl
49class QueuedBufferStore;
Naseer Ahmed96c4c952012-07-25 18:27:14 -070050class ExternalDisplay;
Naseer Ahmed96c4c952012-07-25 18:27:14 -070051
52struct MDPInfo {
53 int version;
54 char panel;
55 bool hasOverlay;
56};
Naseer Ahmed29a26812012-06-14 00:56:20 -070057
Saurabh Shah3e858eb2012-09-17 16:53:21 -070058struct DisplayAttributes {
59 uint32_t vsync_period; //nanos
60 uint32_t xres;
61 uint32_t yres;
Naseer Ahmed54821fe2012-11-28 18:44:38 -050062 uint32_t stride;
Naseer Ahmed7b80d9c2012-09-26 20:14:38 -040063 float xdpi;
64 float ydpi;
Saurabh Shah3e858eb2012-09-17 16:53:21 -070065 int fd;
Saurabh Shahc4d034f2012-09-27 15:55:15 -070066 bool connected; //Applies only to pluggable disp.
67 //Connected does not mean it ready to use.
68 //It should be active also. (UNBLANKED)
Saurabh Shah3e858eb2012-09-17 16:53:21 -070069 bool isActive;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070070};
Saurabh Shah3e858eb2012-09-17 16:53:21 -070071
72struct ListStats {
73 int numAppLayers; //Total - 1, excluding FB layer.
74 int skipCount;
75 int fbLayerIndex; //Always last for now. = numAppLayers
76 //Video specific
77 int yuvCount;
78 int yuvIndex;
Naseer Ahmed018e5452012-12-03 14:46:15 -050079 bool needsAlphaScale;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070080};
81
Naseer Ahmed54821fe2012-11-28 18:44:38 -050082
83struct LayerProp {
84 uint32_t mFlags; //qcom specific layer flags
85 LayerProp():mFlags(0) {};
86};
87
88// LayerProp::flag values
Naseer Ahmed7c958d42012-07-31 18:57:03 -070089enum {
Naseer Ahmed54821fe2012-11-28 18:44:38 -050090 HWC_MDPCOMP = 0x00000001,
Naseer Ahmed7c958d42012-07-31 18:57:03 -070091};
92
Naseer Ahmedb1c76322012-10-17 00:32:50 -040093class LayerCache {
94 public:
95 LayerCache() {
96 canUseLayerCache = false;
97 numHwLayers = 0;
98 for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) {
99 hnd[i] = NULL;
100 }
101 }
102 //LayerCache optimization
103 void updateLayerCache(hwc_display_contents_1_t* list);
104 void resetLayerCache(int num);
105 void markCachedLayersAsOverlay(hwc_display_contents_1_t* list);
106 private:
107 uint32_t numHwLayers;
108 bool canUseLayerCache;
109 buffer_handle_t hnd[MAX_NUM_LAYERS];
110
111};
112
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700113
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500114
115
Naseer Ahmed29a26812012-06-14 00:56:20 -0700116// -----------------------------------------------------------------------------
117// Utility functions - implemented in hwc_utils.cpp
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700118void dumpLayer(hwc_layer_1_t const* l);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700119void setListStats(hwc_context_t *ctx, const hwc_display_contents_1_t *list,
120 int dpy);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700121void initContext(hwc_context_t *ctx);
122void closeContext(hwc_context_t *ctx);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700123//Crops source buffer against destination and FB boundaries
124void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
Saurabh Shah27c1d652012-08-14 19:30:28 -0700125 const int fbWidth, const int fbHeight, int orient);
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500126bool isSecuring(hwc_context_t* ctx);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700127bool isExternalActive(hwc_context_t* ctx);
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700128
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500129//Helper function to dump logs
130void dumpsys_log(android::String8& buf, const char* fmt, ...);
131
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700132//Sync point impl.
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700133int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy);
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700134
Naseer Ahmed29a26812012-06-14 00:56:20 -0700135// Inline utility functions
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700136static inline bool isSkipLayer(const hwc_layer_1_t* l) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700137 return (UNLIKELY(l && (l->flags & HWC_SKIP_LAYER)));
138}
139
140// Returns true if the buffer is yuv
141static inline bool isYuvBuffer(const private_handle_t* hnd) {
142 return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO));
143}
144
Saurabh Shahf395d092012-08-27 12:33:42 -0700145// Returns true if the buffer is secure
146static inline bool isSecureBuffer(const private_handle_t* hnd) {
147 return (hnd && (private_handle_t::PRIV_FLAGS_SECURE_BUFFER & hnd->flags));
148}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700149//Return true if buffer is marked locked
150static inline bool isBufferLocked(const private_handle_t* hnd) {
151 return (hnd && (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags));
152}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700153
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700154//Return true if buffer is for external display only
155static inline bool isExtOnly(const private_handle_t* hnd) {
156 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY));
157}
158
159//Return true if buffer is for external display only with a BLOCK flag.
160static inline bool isExtBlock(const private_handle_t* hnd) {
161 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK));
162}
163
164//Return true if buffer is for external display only with a Close Caption flag.
165static inline bool isExtCC(const private_handle_t* hnd) {
166 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_CC));
167}
168
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700169// Initialize uevent thread
170void init_uevent_thread(hwc_context_t* ctx);
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400171// Initialize vsync thread
172void init_vsync_thread(hwc_context_t* ctx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700173
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700174inline void getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400175 int& width, int& height)
Naseer Ahmed7c958d42012-07-31 18:57:03 -0700176{
177 hwc_rect_t displayFrame = layer->displayFrame;
178 width = displayFrame.right - displayFrame.left;
179 height = displayFrame.bottom - displayFrame.top;
180}
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700181
182static inline int openFb(int dpy) {
183 int fd = -1;
184 const char *devtmpl = "/dev/graphics/fb%u";
185 char name[64] = {0};
186 snprintf(name, 64, devtmpl, dpy);
187 fd = open(name, O_RDWR);
188 return fd;
189}
190
Saurabh Shah27c1d652012-08-14 19:30:28 -0700191template <class T>
192inline void swap(T& a, T& b) {
193 T tmp = a;
194 a = b;
195 b = tmp;
196}
197
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700198}; //qhwc namespace
Naseer Ahmed29a26812012-06-14 00:56:20 -0700199
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400200struct vsync_state {
201 pthread_mutex_t lock;
202 pthread_cond_t cond;
203 bool enable;
204};
205
Naseer Ahmed29a26812012-06-14 00:56:20 -0700206// -----------------------------------------------------------------------------
207// HWC context
208// This structure contains overall state
209struct hwc_context_t {
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700210 hwc_composer_device_1_t device;
Jesse Hall3be78d92012-08-21 15:12:23 -0700211 const hwc_procs_t* proc;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700212 //Framebuffer device
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700213 framebuffer_device_t *mFbDev;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700214 //Overlay object - NULL for non overlay devices
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500215 overlay::Overlay *mOverlay;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700216 //QService object
217 qService::QService *mQService;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700218 // External display related information
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700219 qhwc::ExternalDisplay *mExtDisplay;
Naseer Ahmed96c4c952012-07-25 18:27:14 -0700220 qhwc::MDPInfo mMDP;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700221 qhwc::DisplayAttributes dpyAttr[HWC_NUM_DISPLAY_TYPES];
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700222 qhwc::ListStats listStats[HWC_NUM_DISPLAY_TYPES];
Naseer Ahmede78f0522012-12-07 18:24:28 -0500223 qhwc::LayerCache *mLayerCache[HWC_NUM_DISPLAY_TYPES];
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500224 qhwc::LayerProp *layerProp[HWC_NUM_DISPLAY_TYPES];
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400225
Saurabh Shah56f610d2012-08-07 15:27:06 -0700226 //Securing in progress indicator
227 bool mSecuring;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700228 //Display in secure mode indicator
229 bool mSecureMode;
Naseer Ahmed32ff2252012-09-29 01:41:21 -0400230 //Lock to prevent set from being called while blanking
231 mutable Locker mBlankLock;
Kinjal Bhavsarf83d4482012-10-10 15:56:21 -0700232 //Lock to protect set when detaching external disp
233 mutable Locker mExtSetLock;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400234 //Vsync
235 struct vsync_state vstate;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700236};
237
Naseer Ahmed29a26812012-06-14 00:56:20 -0700238#endif //HWC_UTILS_H