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