1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 * Copyright (c) 2010-2014, 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 #include <math.h>
19 #include <mdp_version.h>
20 #include "overlayUtils.h"
21 #include "overlayMdp.h"
22 #include "mdp_version.h"
23 #include <overlay.h>
24 
25 #define HSIC_SETTINGS_DEBUG 0
26 
27 using namespace qdutils;
28 
29 namespace ovutils = overlay::utils;
30 namespace overlay {
31 
init(const int & dpy)32 bool MdpCtrl::init(const int& dpy) {
33     int fbnum = Overlay::getFbForDpy(dpy);
34     if( fbnum < 0 ) {
35         ALOGE("%s: Invalid FB for the display: %d",__FUNCTION__, dpy);
36         return false;
37     }
38 
39     // FD init
40     if(!utils::openDev(mFd, fbnum,
41                 Res::fbPath, O_RDWR)){
42         ALOGE("Ctrl failed to init fbnum=%d", fbnum);
43         return false;
44     }
45     mDpy = dpy;
46     return true;
47 }
48 
reset()49 void MdpCtrl::reset() {
50     utils::memset0(mOVInfo);
51     mOVInfo.id = MSMFB_NEW_REQUEST;
52     mOrientation = utils::OVERLAY_TRANSFORM_0;
53     mDpy = 0;
54 #ifdef USES_POST_PROCESSING
55     memset(&mParams, 0, sizeof(struct compute_params));
56     mParams.params.conv_params.order = hsic_order_hsc_i;
57     mParams.params.conv_params.interface = interface_rec601;
58     mParams.params.conv_params.cc_matrix[0][0] = 1;
59     mParams.params.conv_params.cc_matrix[1][1] = 1;
60     mParams.params.conv_params.cc_matrix[2][2] = 1;
61 #endif
62 }
63 
close()64 bool MdpCtrl::close() {
65     bool result = true;
66     if(MSMFB_NEW_REQUEST != static_cast<int>(mOVInfo.id)) {
67         if(!mdp_wrapper::unsetOverlay(mFd.getFD(), mOVInfo.id)) {
68             ALOGE("MdpCtrl close error in unset");
69             result = false;
70         }
71     }
72 #ifdef USES_POST_PROCESSING
73     /* free allocated memory in PP */
74     if (mOVInfo.overlay_pp_cfg.igc_cfg.c0_c1_data)
75             free(mOVInfo.overlay_pp_cfg.igc_cfg.c0_c1_data);
76 #endif
77     reset();
78 
79     if(!mFd.close()) {
80         result = false;
81     }
82 
83     return result;
84 }
85 
setSource(const utils::PipeArgs & args)86 void MdpCtrl::setSource(const utils::PipeArgs& args) {
87     setSrcWhf(args.whf);
88 
89     //TODO These are hardcoded. Can be moved out of setSource.
90     mOVInfo.transp_mask = 0xffffffff;
91 
92     //TODO These calls should ideally be a part of setPipeParams API
93     setFlags(args.mdpFlags);
94     setZ(args.zorder);
95     setPlaneAlpha(args.planeAlpha);
96     setBlending(args.blending);
97 }
98 
setCrop(const utils::Dim & d)99 void MdpCtrl::setCrop(const utils::Dim& d) {
100     setSrcRectDim(d);
101 }
102 
setColor(const uint32_t color)103 void MdpCtrl::setColor(const uint32_t color) {
104     mOVInfo.bg_color = color;
105 }
106 
setPosition(const overlay::utils::Dim & d)107 void MdpCtrl::setPosition(const overlay::utils::Dim& d) {
108     setDstRectDim(d);
109 }
110 
setTransform(const utils::eTransform & orient)111 void MdpCtrl::setTransform(const utils::eTransform& orient) {
112     int rot = utils::getMdpOrient(orient);
113     setUserData(rot);
114     mOrientation = static_cast<utils::eTransform>(rot);
115 }
116 
setPipeType(const utils::eMdpPipeType & pType)117 void MdpCtrl::setPipeType(const utils::eMdpPipeType& pType){
118     switch((int) pType){
119         case utils::OV_MDP_PIPE_RGB:
120             mOVInfo.pipe_type = PIPE_TYPE_RGB;
121             break;
122         case utils::OV_MDP_PIPE_VG:
123             mOVInfo.pipe_type = PIPE_TYPE_VIG;
124             break;
125         case utils::OV_MDP_PIPE_DMA:
126             mOVInfo.pipe_type = PIPE_TYPE_DMA;
127             break;
128         default:
129             mOVInfo.pipe_type = PIPE_TYPE_AUTO;
130             break;
131     }
132 }
133 
doTransform()134 void MdpCtrl::doTransform() {
135     setRotationFlags();
136     utils::Whf whf = getSrcWhf();
137     utils::Dim dim = getSrcRectDim();
138     utils::preRotateSource(mOrientation, whf, dim);
139     setSrcWhf(whf);
140     setSrcRectDim(dim);
141 }
142 
doDownscale()143 void MdpCtrl::doDownscale() {
144     if(MDPVersion::getInstance().supportsDecimation()) {
145         utils::getDecimationFactor(mOVInfo.src_rect.w, mOVInfo.src_rect.h,
146                 mOVInfo.dst_rect.w, mOVInfo.dst_rect.h, mOVInfo.horz_deci,
147                 mOVInfo.vert_deci);
148     }
149 }
150 
set()151 bool MdpCtrl::set() {
152     int mdpVersion = MDPVersion::getInstance().getMDPVersion();
153     //deferred calcs, so APIs could be called in any order.
154     doTransform();
155     utils::Whf whf = getSrcWhf();
156     if(utils::isYuv(whf.format)) {
157         utils::normalizeCrop(mOVInfo.src_rect.x, mOVInfo.src_rect.w);
158         utils::normalizeCrop(mOVInfo.src_rect.y, mOVInfo.src_rect.h);
159         if(mdpVersion < MDSS_V5) {
160             utils::even_floor(mOVInfo.dst_rect.w);
161             utils::even_floor(mOVInfo.dst_rect.h);
162         } else if (mOVInfo.flags & MDP_DEINTERLACE) {
163             // For interlaced, crop.h should be 4-aligned
164             if (!(mOVInfo.flags & MDP_SOURCE_ROTATED_90) &&
165                 (mOVInfo.src_rect.h % 4))
166                 mOVInfo.src_rect.h = utils::aligndown(mOVInfo.src_rect.h, 4);
167             // For interlaced, width must be multiple of 4 when rotated 90deg.
168             else if ((mOVInfo.flags & MDP_SOURCE_ROTATED_90) &&
169                 (mOVInfo.src_rect.w % 4))
170                 mOVInfo.src_rect.w = utils::aligndown(mOVInfo.src_rect.w, 4);
171         }
172     } else {
173         // On 8974 and 8x26, there is a limitation of 1-pixel down-scaling
174         if (mdpVersion >= MDSS_V5) {
175             if(qdutils::MDPVersion::getInstance().is8x74v2() ||
176                     qdutils::MDPVersion::getInstance().is8x26()) {
177                 if (mOVInfo.src_rect.w - mOVInfo.dst_rect.w == 1)
178                     mOVInfo.src_rect.w -= 1;
179                 if (mOVInfo.src_rect.h - mOVInfo.dst_rect.h == 1)
180                     mOVInfo.src_rect.h -= 1;
181             }
182         }
183     }
184 
185     doDownscale();
186     return true;
187 }
188 
189 //Update src format based on rotator's destination format.
updateSrcFormat(const uint32_t & rotDestFmt)190 void MdpCtrl::updateSrcFormat(const uint32_t& rotDestFmt) {
191     utils::Whf whf = getSrcWhf();
192     whf.format =  rotDestFmt;
193     setSrcWhf(whf);
194 }
195 
dump() const196 void MdpCtrl::dump() const {
197     ALOGE("== Dump MdpCtrl start ==");
198     mFd.dump();
199     mdp_wrapper::dump("mOVInfo", mOVInfo);
200     ALOGE("== Dump MdpCtrl end ==");
201 }
202 
getDump(char * buf,size_t len)203 void MdpCtrl::getDump(char *buf, size_t len) {
204     ovutils::getDump(buf, len, "Ctrl", mOVInfo);
205 }
206 
dump() const207 void MdpData::dump() const {
208     ALOGE("== Dump MdpData start ==");
209     mFd.dump();
210     mdp_wrapper::dump("mOvData", mOvData);
211     ALOGE("== Dump MdpData end ==");
212 }
213 
getDump(char * buf,size_t len)214 void MdpData::getDump(char *buf, size_t len) {
215     ovutils::getDump(buf, len, "Data", mOvData);
216 }
217 
setVisualParams(const MetaData_t & data)218 bool MdpCtrl::setVisualParams(const MetaData_t& data) {
219     ALOGD_IF(0, "In %s: data.operation = %d", __FUNCTION__, data.operation);
220 
221     // Set Color Space for MDP to configure CSC matrix
222     mOVInfo.color_space = ITU_R_601;
223     if (data.operation & UPDATE_COLOR_SPACE) {
224         mOVInfo.color_space = data.colorSpace;
225     }
226 
227 #ifdef USES_POST_PROCESSING
228     bool needUpdate = false;
229     /* calculate the data */
230     if (data.operation & PP_PARAM_HSIC) {
231         if (mParams.params.pa_params.hue != data.hsicData.hue) {
232             ALOGD_IF(HSIC_SETTINGS_DEBUG,
233                 "Hue has changed from %d to %d",
234                 mParams.params.pa_params.hue,data.hsicData.hue);
235             needUpdate = true;
236         }
237 
238         if (!isEqual(mParams.params.pa_params.sat,
239             data.hsicData.saturation)) {
240             ALOGD_IF(HSIC_SETTINGS_DEBUG,
241                 "Saturation has changed from %f to %f",
242                 mParams.params.pa_params.sat,
243                 data.hsicData.saturation);
244             needUpdate = true;
245         }
246 
247         if (mParams.params.pa_params.intensity != data.hsicData.intensity) {
248             ALOGD_IF(HSIC_SETTINGS_DEBUG,
249                 "Intensity has changed from %d to %d",
250                 mParams.params.pa_params.intensity,
251                 data.hsicData.intensity);
252             needUpdate = true;
253         }
254 
255         if (!isEqual(mParams.params.pa_params.contrast,
256             data.hsicData.contrast)) {
257             ALOGD_IF(HSIC_SETTINGS_DEBUG,
258                 "Contrast has changed from %f to %f",
259                 mParams.params.pa_params.contrast,
260                 data.hsicData.contrast);
261             needUpdate = true;
262         }
263 
264         if (needUpdate) {
265             mParams.params.pa_params.hue = data.hsicData.hue;
266             mParams.params.pa_params.sat = data.hsicData.saturation;
267             mParams.params.pa_params.intensity = data.hsicData.intensity;
268             mParams.params.pa_params.contrast = data.hsicData.contrast;
269             mParams.params.pa_params.ops = MDP_PP_OPS_WRITE | MDP_PP_OPS_ENABLE;
270             mParams.operation |= PP_OP_PA;
271         }
272     }
273 
274     if (data.operation & PP_PARAM_SHARP2) {
275         if (mParams.params.sharp_params.strength != data.Sharp2Data.strength) {
276             needUpdate = true;
277         }
278         if (mParams.params.sharp_params.edge_thr != data.Sharp2Data.edge_thr) {
279             needUpdate = true;
280         }
281         if (mParams.params.sharp_params.smooth_thr !=
282                 data.Sharp2Data.smooth_thr) {
283             needUpdate = true;
284         }
285         if (mParams.params.sharp_params.noise_thr !=
286                 data.Sharp2Data.noise_thr) {
287             needUpdate = true;
288         }
289 
290         if (needUpdate) {
291             mParams.params.sharp_params.strength = data.Sharp2Data.strength;
292             mParams.params.sharp_params.edge_thr = data.Sharp2Data.edge_thr;
293             mParams.params.sharp_params.smooth_thr =
294                 data.Sharp2Data.smooth_thr;
295             mParams.params.sharp_params.noise_thr = data.Sharp2Data.noise_thr;
296             mParams.params.sharp_params.ops =
297                 MDP_PP_OPS_WRITE | MDP_PP_OPS_ENABLE;
298             mParams.operation |= PP_OP_SHARP;
299         }
300     }
301 
302     if (data.operation & PP_PARAM_IGC) {
303         if (mOVInfo.overlay_pp_cfg.igc_cfg.c0_c1_data == NULL){
304             uint32_t *igcData
305                 = (uint32_t *)malloc(2 * MAX_IGC_LUT_ENTRIES * sizeof(uint32_t));
306             if (!igcData) {
307                 ALOGE("IGC storage allocated failed");
308                 return false;
309             }
310             mOVInfo.overlay_pp_cfg.igc_cfg.c0_c1_data = igcData;
311             mOVInfo.overlay_pp_cfg.igc_cfg.c2_data
312                 = igcData + MAX_IGC_LUT_ENTRIES;
313         }
314 
315         memcpy(mParams.params.igc_lut_params.c0,
316             data.igcData.c0, sizeof(uint16_t) * MAX_IGC_LUT_ENTRIES);
317         memcpy(mParams.params.igc_lut_params.c1,
318             data.igcData.c1, sizeof(uint16_t) * MAX_IGC_LUT_ENTRIES);
319         memcpy(mParams.params.igc_lut_params.c2,
320             data.igcData.c2, sizeof(uint16_t) * MAX_IGC_LUT_ENTRIES);
321 
322         mParams.params.igc_lut_params.ops
323             = MDP_PP_OPS_WRITE | MDP_PP_OPS_ENABLE;
324         mParams.operation |= PP_OP_IGC;
325         needUpdate = true;
326     }
327 
328     if (data.operation & PP_PARAM_VID_INTFC) {
329         mParams.params.conv_params.interface =
330             (interface_type) data.video_interface;
331         needUpdate = true;
332     }
333 
334     if (needUpdate) {
335         display_pp_compute_params(&mParams, &mOVInfo.overlay_pp_cfg);
336     }
337 #endif
338     return true;
339 }
340 
validateAndSet(MdpCtrl * mdpCtrlArray[],const int & count,const int & fbFd)341 bool MdpCtrl::validateAndSet(MdpCtrl* mdpCtrlArray[], const int& count,
342         const int& fbFd) {
343     mdp_overlay* ovArray[count];
344     memset(&ovArray, 0, sizeof(ovArray));
345 
346     for(int i = 0; i < count; i++) {
347         ovArray[i] = &mdpCtrlArray[i]->mOVInfo;
348     }
349 
350     struct mdp_overlay_list list;
351     memset(&list, 0, sizeof(struct mdp_overlay_list));
352     list.num_overlays = count;
353     list.overlay_list = ovArray;
354 
355    int (*fnProgramScale)(struct mdp_overlay_list *) =
356         Overlay::getFnProgramScale();
357     if(fnProgramScale) {
358         fnProgramScale(&list);
359     }
360 
361     // Error value is based on file errno-base.h
362     // 0 - indicates no error.
363     int errVal = mdp_wrapper::validateAndSet(fbFd, list);
364     if(errVal) {
365         /* No dump for failure due to insufficient resource */
366         if(errVal != E2BIG) {
367             mdp_wrapper::dump("Bad ov dump: ",
368                 *list.overlay_list[list.processed_overlays]);
369         }
370         return false;
371     }
372 
373     return true;
374 }
375 
376 
377 //// MdpData ////////////
init(const int & dpy)378 bool MdpData::init(const int& dpy) {
379     int fbnum = Overlay::getFbForDpy(dpy);
380     if( fbnum < 0 ) {
381         ALOGE("%s: Invalid FB for the display: %d",__FUNCTION__, dpy);
382         return false;
383     }
384 
385     // FD init
386     if(!utils::openDev(mFd, fbnum, Res::fbPath, O_RDWR)){
387         ALOGE("Ctrl failed to init fbnum=%d", fbnum);
388         return false;
389     }
390     return true;
391 }
392 
393 } // overlay
394