• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2011-2012, The Linux Foundation. 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 The Linux Foundation 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 #include <stdlib.h>
31 #include <utils/Log.h>
32 #include <linux/msm_mdp.h>
33 #include <cutils/properties.h>
34 #include "gralloc_priv.h"
35 #include "fb_priv.h"
36 #include "overlayUtils.h"
37 #include "mdpWrapper.h"
38 #include "mdp_version.h"
39 
40 // just a helper static thingy
41 namespace {
42 struct IOFile {
IOFile__anonb3014a120111::IOFile43     IOFile(const char* s, const char* mode) : fp(0) {
44         fp = ::fopen(s, mode);
45         if(!fp) {
46             ALOGE("Failed open %s", s);
47         }
48     }
49     template <class T>
read__anonb3014a120111::IOFile50             size_t read(T& r, size_t elem) {
51                 if(fp) {
52                     return ::fread(&r, sizeof(T), elem, fp);
53                 }
54                 return 0;
55             }
write__anonb3014a120111::IOFile56     size_t write(const char* s, uint32_t val) {
57         if(fp) {
58             return ::fprintf(fp, s, val);
59         }
60         return 0;
61     }
valid__anonb3014a120111::IOFile62     bool valid() const { return fp != 0; }
~IOFile__anonb3014a120111::IOFile63     ~IOFile() {
64         if(fp) ::fclose(fp);
65         fp=0;
66     }
67     FILE* fp;
68 };
69 }
70 
71 namespace overlay {
72 
73 //----------From class Res ------------------------------
74 const char* const Res::fbPath = "/dev/graphics/fb%u";
75 const char* const Res::rotPath = "/dev/msm_rotator";
76 const char* const Res::format3DFile =
77         "/sys/class/graphics/fb1/format_3d";
78 const char* const Res::edid3dInfoFile =
79         "/sys/class/graphics/fb1/3d_present";
80 const char* const Res::barrierFile =
81         "/sys/devices/platform/mipi_novatek.0/enable_3d_barrier";
82 //--------------------------------------------------------
83 
84 
85 
86 namespace utils {
87 //------------------ defines -----------------------------
88 #define FB_DEVICE_TEMPLATE "/dev/graphics/fb%u"
89 #define NUM_FB_DEVICES 3
90 
91 //--------------------------------------------------------
FrameBufferInfo()92 FrameBufferInfo::FrameBufferInfo() {
93     mFBWidth = 0;
94     mFBHeight = 0;
95     mBorderFillSupported = false;
96 
97     OvFD mFd;
98 
99     // Use open defined in overlayFD file to open fd for fb0
100     if(!overlay::open(mFd, 0, Res::fbPath)) {
101         ALOGE("FrameBufferInfo: failed to open fd");
102         return;
103     }
104 
105     if (!mFd.valid()) {
106         ALOGE("FrameBufferInfo: FD not valid");
107         return;
108     }
109 
110     fb_var_screeninfo vinfo;
111     if (!mdp_wrapper::getVScreenInfo(mFd.getFD(), vinfo)) {
112         ALOGE("FrameBufferInfo: failed getVScreenInfo on fb0");
113         mFd.close();
114         return;
115     }
116 
117     int mdpVersion = qdutils::MDPVersion::getInstance().getMDPVersion();
118     if (mdpVersion < qdutils::MDSS_V5) {
119         mdp_overlay ov;
120         memset(&ov, 0, sizeof(ov));
121         ov.id = 1;
122         if (!mdp_wrapper::getOverlay(mFd.getFD(), ov)) {
123             ALOGE("FrameBufferInfo: failed getOverlay on fb0");
124             mFd.close();
125             return;
126         }
127         mBorderFillSupported = (ov.flags & MDP_BORDERFILL_SUPPORTED) ?
128                                true : false;
129     } else {
130         // badger always support border fill
131         mBorderFillSupported = true;
132     }
133 
134     mFd.close();
135     mFBWidth = vinfo.xres;
136     mFBHeight = vinfo.yres;
137 }
138 
getInstance()139 FrameBufferInfo* FrameBufferInfo::getInstance() {
140     if (!sFBInfoInstance) {
141         sFBInfoInstance = new FrameBufferInfo;
142     }
143     return sFBInfoInstance;
144 }
145 
getWidth() const146 int FrameBufferInfo::getWidth() const {
147     return mFBWidth;
148 }
149 
getHeight() const150 int FrameBufferInfo::getHeight() const {
151     return mFBHeight;
152 }
153 
154 /* clears any VG pipes allocated to the fb devices */
initOverlay()155 int initOverlay() {
156     msmfb_mixer_info_req  req;
157     mdp_mixer_info *minfo = NULL;
158     char name[64];
159     int fd = -1;
160     for(int i = 0; i < NUM_FB_DEVICES; i++) {
161         snprintf(name, 64, FB_DEVICE_TEMPLATE, i);
162         ALOGD("initoverlay:: opening the device:: %s", name);
163         fd = ::open(name, O_RDWR, 0);
164         if(fd < 0) {
165             ALOGE("cannot open framebuffer(%d)", i);
166             return -1;
167         }
168         //Get the mixer configuration */
169         req.mixer_num = i;
170         if (ioctl(fd, MSMFB_MIXER_INFO, &req) == -1) {
171             ALOGE("ERROR: MSMFB_MIXER_INFO ioctl failed");
172             close(fd);
173             return -1;
174         }
175         minfo = req.info;
176         for (int j = 0; j < req.cnt; j++) {
177             ALOGD("ndx=%d num=%d z_order=%d", minfo->pndx, minfo->pnum,
178                     minfo->z_order);
179             // except the RGB base layer with z_order of -1, clear any
180             // other pipes connected to mixer.
181             if((minfo->z_order) != -1) {
182                 int index = minfo->pndx;
183                 ALOGD("Unset overlay with index: %d at mixer %d", index, i);
184                 if(ioctl(fd, MSMFB_OVERLAY_UNSET, &index) == -1) {
185                     ALOGE("ERROR: MSMFB_OVERLAY_UNSET failed");
186                     close(fd);
187                     return -1;
188                 }
189             }
190             minfo++;
191         }
192         close(fd);
193         fd = -1;
194     }
195     return 0;
196 }
197 
198 //--------------------------------------------------------
199 //Refer to graphics.h, gralloc_priv.h, msm_mdp.h
getMdpFormat(int format)200 int getMdpFormat(int format) {
201     switch (format) {
202         //From graphics.h
203         case HAL_PIXEL_FORMAT_RGBA_8888 :
204             return MDP_RGBA_8888;
205         case HAL_PIXEL_FORMAT_RGBX_8888:
206             return MDP_RGBX_8888;
207         case HAL_PIXEL_FORMAT_RGB_888:
208             return MDP_RGB_888;
209         case HAL_PIXEL_FORMAT_RGB_565:
210             return MDP_RGB_565;
211         case HAL_PIXEL_FORMAT_BGRA_8888:
212             return MDP_BGRA_8888;
213         case HAL_PIXEL_FORMAT_YV12:
214             return MDP_Y_CR_CB_GH2V2;
215         case HAL_PIXEL_FORMAT_YCbCr_422_SP:
216             return MDP_Y_CBCR_H2V1;
217         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
218             return MDP_Y_CRCB_H2V2;
219 
220         //From gralloc_priv.h
221         case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
222             return MDP_Y_CBCR_H2V2_TILE;
223         case HAL_PIXEL_FORMAT_YCbCr_420_SP:
224             return MDP_Y_CBCR_H2V2;
225         case HAL_PIXEL_FORMAT_YCrCb_422_SP:
226             return MDP_Y_CRCB_H2V1;
227         case HAL_PIXEL_FORMAT_YCbCr_444_SP:
228             return MDP_Y_CBCR_H1V1;
229         case HAL_PIXEL_FORMAT_YCrCb_444_SP:
230             return MDP_Y_CRCB_H1V1;
231 
232         default:
233             //Unsupported by MDP
234             //---graphics.h--------
235             //HAL_PIXEL_FORMAT_RGBA_5551
236             //HAL_PIXEL_FORMAT_RGBA_4444
237             //HAL_PIXEL_FORMAT_YCbCr_422_I
238             //---gralloc_priv.h-----
239             //HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO    = 0x7FA30C01
240             //HAL_PIXEL_FORMAT_R_8                    = 0x10D
241             //HAL_PIXEL_FORMAT_RG_88                  = 0x10E
242             ALOGE("%s: Unsupported format = 0x%x", __func__, format);
243             return -1;
244     }
245     // not reached
246     return -1;
247 }
248 
is3DTV()249 bool is3DTV() {
250     char is3DTV = '0';
251     IOFile fp(Res::edid3dInfoFile, "r");
252     (void)fp.read(is3DTV, 1);
253     ALOGI("3DTV EDID flag: %d", is3DTV);
254     return (is3DTV == '0') ? false : true;
255 }
256 
isPanel3D()257 bool isPanel3D() {
258     OvFD fd;
259     if(!overlay::open(fd, 0 /*fb*/, Res::fbPath)){
260         ALOGE("isPanel3D Can't open framebuffer 0");
261         return false;
262     }
263     fb_fix_screeninfo finfo;
264     if(!mdp_wrapper::getFScreenInfo(fd.getFD(), finfo)) {
265         ALOGE("isPanel3D read fb0 failed");
266     }
267     fd.close();
268     return (FB_TYPE_3D_PANEL == finfo.type) ? true : false;
269 }
270 
usePanel3D()271 bool usePanel3D() {
272     if(!isPanel3D())
273         return false;
274     char value[PROPERTY_VALUE_MAX];
275     property_get("persist.user.panel3D", value, "0");
276     int usePanel3D = atoi(value);
277     return usePanel3D ? true : false;
278 }
279 
send3DInfoPacket(uint32_t format3D)280 bool send3DInfoPacket (uint32_t format3D) {
281     IOFile fp(Res::format3DFile, "wb");
282     (void)fp.write("%d", format3D);
283     if(!fp.valid()) {
284         ALOGE("send3DInfoPacket: no sysfs entry for setting 3d mode");
285         return false;
286     }
287     return true;
288 }
289 
enableBarrier(uint32_t orientation)290 bool enableBarrier (uint32_t orientation) {
291     IOFile fp(Res::barrierFile, "wb");
292     (void)fp.write("%d", orientation);
293     if(!fp.valid()) {
294         ALOGE("enableBarrier no sysfs entry for "
295                 "enabling barriers on 3D panel");
296         return false;
297     }
298     return true;
299 }
300 
getS3DFormat(uint32_t fmt)301 uint32_t getS3DFormat(uint32_t fmt) {
302     // The S3D is part of the HAL_PIXEL_FORMAT_YV12 value. Add
303     // an explicit check for the format
304     if (fmt == HAL_PIXEL_FORMAT_YV12) {
305         return 0;
306     }
307     uint32_t fmt3D = format3D(fmt);
308     uint32_t fIn3D = format3DInput(fmt3D); // MSB 2 bytes - inp
309     uint32_t fOut3D = format3DOutput(fmt3D); // LSB 2 bytes - out
310     fmt3D = fIn3D | fOut3D;
311     if (!fIn3D) {
312         fmt3D |= fOut3D << SHIFT_TOT_3D; //Set the input format
313     }
314     if (!fOut3D) {
315         switch (fIn3D) {
316             case HAL_3D_IN_SIDE_BY_SIDE_L_R:
317             case HAL_3D_IN_SIDE_BY_SIDE_R_L:
318                 // For all side by side formats, set the output
319                 // format as Side-by-Side i.e 0x1
320                 fmt3D |= HAL_3D_IN_SIDE_BY_SIDE_L_R >> SHIFT_TOT_3D;
321                 break;
322             default:
323                 fmt3D |= fIn3D >> SHIFT_TOT_3D; //Set the output format
324         }
325     }
326     return fmt3D;
327 }
328 
329 } // utils
330 
331 } // overlay
332