blob: 8a6761fcb16ca5b1ab3bb49f4eb94fdc401d50ee [file] [log] [blame]
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08003 * Copyright (C) 2012-2013, The Linux Foundation. All rights reserved.
Naseer Ahmed31da0b12012-07-31 18:55:33 -07004 *
radhakrishna2f00c8f2013-10-21 16:49:21 +05305 * Not a Contribution.
Naseer Ahmed31da0b12012-07-31 18:55:33 -07006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
Naseer Ahmed45a99602012-07-31 19:15:24 -070020#define DEBUG_COPYBIT 0
Naseer Ahmed72cf9762012-07-21 12:17:13 -070021#include <copybit.h>
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080022#include <utils/Timers.h>
Terence Hampson0124cc72013-04-18 23:45:56 -070023#include <mdp_version.h>
Naseer Ahmed45a99602012-07-31 19:15:24 -070024#include "hwc_copybit.h"
25#include "comptype.h"
Naseer Ahmed64b81212013-02-14 10:29:47 -050026#include "gr.h"
radhakrishna2f00c8f2013-10-21 16:49:21 +053027#include "cb_utils.h"
Naseer Ahmed74214722013-02-09 08:11:36 -050028
radhakrishna2f00c8f2013-10-21 16:49:21 +053029using namespace qdutils;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070030namespace qhwc {
31
Naseer Ahmed31da0b12012-07-31 18:55:33 -070032struct range {
33 int current;
34 int end;
35};
36struct region_iterator : public copybit_region_t {
37
38 region_iterator(hwc_region_t region) {
39 mRegion = region;
40 r.end = region.numRects;
41 r.current = 0;
42 this->next = iterate;
43 }
44
45private:
46 static int iterate(copybit_region_t const * self, copybit_rect_t* rect){
47 if (!self || !rect) {
48 ALOGE("iterate invalid parameters");
49 return 0;
50 }
51
52 region_iterator const* me =
53 static_cast<region_iterator const*>(self);
54 if (me->r.current != me->r.end) {
55 rect->l = me->mRegion.rects[me->r.current].left;
56 rect->t = me->mRegion.rects[me->r.current].top;
57 rect->r = me->mRegion.rects[me->r.current].right;
58 rect->b = me->mRegion.rects[me->r.current].bottom;
59 me->r.current++;
60 return 1;
61 }
62 return 0;
63 }
64
65 hwc_region_t mRegion;
66 mutable range r;
67};
68
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080069void CopyBit::reset() {
70 mIsModeOn = false;
71 mCopyBitDraw = false;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070072}
73
Naseer Ahmed45a99602012-07-31 19:15:24 -070074bool CopyBit::canUseCopybitForYUV(hwc_context_t *ctx) {
75 // return true for non-overlay targets
Terence Hampsond0fd5792013-05-29 11:56:52 -040076 if(ctx->mMDP.hasOverlay && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Naseer Ahmed45a99602012-07-31 19:15:24 -070077 return false;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070078 }
79 return true;
80}
Naseer Ahmed45a99602012-07-31 19:15:24 -070081
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080082bool CopyBit::canUseCopybitForRGB(hwc_context_t *ctx,
83 hwc_display_contents_1_t *list,
84 int dpy) {
85 int compositionType = qdutils::QCCompositionType::
86 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -070087
Naseer Ahmed45a99602012-07-31 19:15:24 -070088 if (compositionType & qdutils::COMPOSITION_TYPE_DYN) {
89 // DYN Composition:
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +053090 // use copybit, if (TotalRGBRenderArea < threashold * FB Area)
Naseer Ahmed45a99602012-07-31 19:15:24 -070091 // this is done based on perf inputs in ICS
92 // TODO: Above condition needs to be re-evaluated in JB
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080093 int fbWidth = ctx->dpyAttr[dpy].xres;
94 int fbHeight = ctx->dpyAttr[dpy].yres;
95 unsigned int fbArea = (fbWidth * fbHeight);
Naseer Ahmed45a99602012-07-31 19:15:24 -070096 unsigned int renderArea = getRGBRenderingArea(list);
97 ALOGD_IF (DEBUG_COPYBIT, "%s:renderArea %u, fbArea %u",
98 __FUNCTION__, renderArea, fbArea);
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +053099 if (renderArea < (mDynThreshold * fbArea)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700100 return true;
101 }
102 } else if ((compositionType & qdutils::COMPOSITION_TYPE_MDP)) {
103 // MDP composition, use COPYBIT always
104 return true;
105 } else if ((compositionType & qdutils::COMPOSITION_TYPE_C2D)) {
106 // C2D composition, use COPYBIT
107 return true;
108 }
109 return false;
110}
111
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800112unsigned int CopyBit::getRGBRenderingArea
113 (const hwc_display_contents_1_t *list) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700114 //Calculates total rendering area for RGB layers
115 unsigned int renderArea = 0;
116 unsigned int w=0, h=0;
Terence Hampsonef379d42013-05-21 10:38:01 -0400117 // Skipping last layer since FrameBuffer layer should not affect
118 // which composition to choose
119 for (unsigned int i=0; i<list->numHwLayers -1; i++) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700120 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
121 if (hnd) {
122 if (BUFFER_TYPE_UI == hnd->bufferType) {
123 getLayerResolution(&list->hwLayers[i], w, h);
124 renderArea += (w*h);
125 }
126 }
127 }
128 return renderArea;
129}
130
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800131bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
132 int dpy) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700133
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800134 if(mEngine == NULL) {
135 // No copybit device found - cannot use copybit
136 return false;
137 }
138 int compositionType = qdutils::QCCompositionType::
139 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700140
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800141 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
142 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700143 //GPU/CPU composition, don't change layer composition type
144 return true;
145 }
146
Naseer Ahmed45a99602012-07-31 19:15:24 -0700147 if(!(validateParams(ctx, list))) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800148 ALOGE("%s:Invalid Params", __FUNCTION__);
149 return false;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700150 }
151
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800152 if(ctx->listStats[dpy].skipCount) {
153 //GPU will be anyways used
154 return false;
155 }
156
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700157 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS) {
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700158 // Reached max layers supported by HWC.
159 return false;
160 }
161
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800162 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
163 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500164 LayerProp *layerProp = ctx->layerProp[dpy];
165 size_t fbLayerIndex = ctx->listStats[dpy].fbLayerIndex;
166 hwc_layer_1_t *fbLayer = &list->hwLayers[fbLayerIndex];
167 private_handle_t *fbHnd = (private_handle_t *)fbLayer->handle;
168
169
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530170 // Following are MDP3 limitations for which we
171 // need to fallback to GPU composition:
Terence Hampsonb1021932013-09-18 11:15:19 -0400172 // 1. Plane alpha is not supported by MDP3.
Terence Hampsonc235bda2013-07-12 12:47:38 -0400173 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
174 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
175 hwc_layer_1_t *layer = (hwc_layer_1_t *) &list->hwLayers[i];
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530176 if (layer->planeAlpha != 0xFF)
177 return true;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400178 }
179 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500180
181 //Allocate render buffers if they're not allocated
182 if (useCopybitForYUV || useCopybitForRGB) {
183 int ret = allocRenderBuffers(fbHnd->width,
184 fbHnd->height,
185 fbHnd->format);
186 if (ret < 0) {
187 return false;
188 } else {
189 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
190 NUM_RENDER_BUFFERS;
191 }
192 }
193
Terence Hampsond0fd5792013-05-29 11:56:52 -0400194 // We cannot mix copybit layer with layers marked to be drawn on FB
195 if (!useCopybitForYUV && ctx->listStats[dpy].yuvCount)
196 return true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800197
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530198 mCopyBitDraw = false;
199 if (useCopybitForRGB &&
200 (useCopybitForYUV || !ctx->listStats[dpy].yuvCount)) {
201 mCopyBitDraw = true;
202 // numAppLayers-1, as we iterate till 0th layer index
203 // Mark all layers to be drawn by copybit
204 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500205 layerProp[i].mFlags |= HWC_COPYBIT;
206 list->hwLayers[i].compositionType = HWC_OVERLAY;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700207 }
208 }
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530209
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700210 return true;
211}
212
Naseer Ahmed183939d2013-03-14 20:44:17 -0400213int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
214{
215 int ret = 0;
216 copybit_rect_t clear_rect = {rect.left, rect.top,
217 rect.right,
218 rect.bottom};
219
220 copybit_image_t buf;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700221 buf.w = ALIGN(getWidth(hnd),32);
222 buf.h = getHeight(hnd);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400223 buf.format = hnd->format;
224 buf.base = (void *)hnd->base;
225 buf.handle = (native_handle_t *)hnd;
226
227 copybit_device_t *copybit = mEngine;
228 ret = copybit->clear(copybit, &buf, &clear_rect);
229 return ret;
230}
231
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800232bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
233 int dpy, int32_t *fd) {
234 // draw layers marked for COPYBIT
235 int retVal = true;
236 int copybitLayerCount = 0;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500237 LayerProp *layerProp = ctx->layerProp[dpy];
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800238
239 if(mCopyBitDraw == false) // there is no layer marked for copybit
Naseer Ahmed64b81212013-02-14 10:29:47 -0500240 return false ;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800241
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800242 //render buffer
Naseer Ahmed64b81212013-02-14 10:29:47 -0500243 private_handle_t *renderBuffer = getCurrentRenderBuffer();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800244 if (!renderBuffer) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500245 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800246 return false;
247 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500248
Terence Hampson0124cc72013-04-18 23:45:56 -0700249 if (ctx->mMDP.version >= qdutils::MDP_V4_0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400250 //Wait for the previous frame to complete before rendering onto it
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530251 if(mRelFd[mCurRenderBufferIndex] >=0) {
252 sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
253 close(mRelFd[mCurRenderBufferIndex]);
254 mRelFd[mCurRenderBufferIndex] = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400255 }
Terence Hampson65fe4522013-10-17 17:40:03 -0400256 } else {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530257 if(mRelFd[mCurRenderBufferIndex] >=0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400258 copybit_device_t *copybit = getCopyBitDevice();
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530259 copybit->set_sync(copybit, mRelFd[mCurRenderBufferIndex]);
Terence Hampson65fe4522013-10-17 17:40:03 -0400260 }
Terence Hampson0124cc72013-04-18 23:45:56 -0700261 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530262
263 //Clear the transparent or left out region on the render buffer
264 hwc_rect_t clearRegion = {0,0,0,0};
265 if(CBUtils::getuiClearRegion(list, clearRegion, layerProp))
266 clear(renderBuffer, clearRegion);
267
Naseer Ahmed64b81212013-02-14 10:29:47 -0500268 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800269 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
270 hwc_layer_1_t *layer = &list->hwLayers[i];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500271 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
272 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800273 continue;
274 }
275 int ret = -1;
Terence Hampsonadf47302013-05-23 12:21:02 -0400276 if (list->hwLayers[i].acquireFenceFd != -1
277 && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800278 // Wait for acquire Fence on the App buffers.
279 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
280 if(ret < 0) {
281 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
282 __FUNCTION__, errno, strerror(errno));
283 }
284 close(list->hwLayers[i].acquireFenceFd);
285 list->hwLayers[i].acquireFenceFd = -1;
286 }
287 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530288 renderBuffer, dpy, !i);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800289 copybitLayerCount++;
290 if(retVal < 0) {
291 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
292 }
293 }
294
295 if (copybitLayerCount) {
296 copybit_device_t *copybit = getCopyBitDevice();
297 // Async mode
298 copybit->flush_get_fence(copybit, fd);
Radhika Ranjan Sonid8d8f332013-11-22 14:51:29 +0530299 if(mRelFd[mCurRenderBufferIndex] >=0 &&
300 ctx->mMDP.version == qdutils::MDP_V3_0_4) {
301 close(mRelFd[mCurRenderBufferIndex]);
302 mRelFd[mCurRenderBufferIndex] = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400303 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800304 }
305 return true;
306}
307
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700308int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530309 private_handle_t *renderBuffer, int dpy, bool isFG)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700310{
311 hwc_context_t* ctx = (hwc_context_t*)(dev);
Terence Hampsonadf47302013-05-23 12:21:02 -0400312 int err = 0, acquireFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700313 if(!ctx) {
314 ALOGE("%s: null context ", __FUNCTION__);
315 return -1;
316 }
317
318 private_handle_t *hnd = (private_handle_t *)layer->handle;
319 if(!hnd) {
Sushil Chauhan943797c2013-10-21 17:35:55 -0700320 if (layer->flags & HWC_COLOR_FILL) { // Color layer
321 return fillColorUsingCopybit(layer, renderBuffer);
322 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700323 ALOGE("%s: invalid handle", __FUNCTION__);
324 return -1;
325 }
326
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800327 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700328 if(!fbHandle) {
329 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700330 return -1;
331 }
332
333 // Set the copybit source:
334 copybit_image_t src;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700335 src.w = getWidth(hnd);
336 src.h = getHeight(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700337 src.format = hnd->format;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700338
339 // Handle R/B swap
340 if ((layer->flags & HWC_FORMAT_RB_SWAP)) {
341 if (src.format == HAL_PIXEL_FORMAT_RGBA_8888) {
342 src.format = HAL_PIXEL_FORMAT_BGRA_8888;
343 } else if (src.format == HAL_PIXEL_FORMAT_RGBX_8888) {
344 src.format = HAL_PIXEL_FORMAT_BGRX_8888;
345 }
346 }
347
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700348 src.base = (void *)hnd->base;
349 src.handle = (native_handle_t *)layer->handle;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700350 src.horiz_padding = src.w - getWidth(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700351 // Initialize vertical padding to zero for now,
352 // this needs to change to accomodate vertical stride
353 // if needed in the future
354 src.vert_padding = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700355
shuoy7c101f92013-08-26 14:48:57 +0800356 int layerTransform = layer->transform ;
357 // When flip and rotation(90) are present alter the flip,
358 // as GPU is doing the flip and rotation in opposite order
359 // to that of MDP3.0
360 // For 270 degrees, we get 90 + (H+V) which is same as doing
361 // flip first and then rotation (H+V) + 90
362 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
363 if (((layer->transform& HAL_TRANSFORM_FLIP_H) ||
364 (layer->transform & HAL_TRANSFORM_FLIP_V)) &&
365 (layer->transform & HAL_TRANSFORM_ROT_90) &&
366 !(layer->transform == HAL_TRANSFORM_ROT_270)){
367 if(layer->transform & HAL_TRANSFORM_FLIP_H){
368 layerTransform ^= HAL_TRANSFORM_FLIP_H;
369 layerTransform |= HAL_TRANSFORM_FLIP_V;
370 }
371 if(layer->transform & HAL_TRANSFORM_FLIP_V){
372 layerTransform ^= HAL_TRANSFORM_FLIP_V;
373 layerTransform |= HAL_TRANSFORM_FLIP_H;
374 }
375 }
376 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700377 // Copybit source rect
Saurabh Shah62e1d732013-09-17 10:44:05 -0700378 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700379 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
380 sourceCrop.right,
381 sourceCrop.bottom};
382
383 // Copybit destination rect
384 hwc_rect_t displayFrame = layer->displayFrame;
385 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
386 displayFrame.right,
387 displayFrame.bottom};
388
389 // Copybit dst
390 copybit_image_t dst;
391 dst.w = ALIGN(fbHandle->width,32);
392 dst.h = fbHandle->height;
393 dst.format = fbHandle->format;
394 dst.base = (void *)fbHandle->base;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800395 dst.handle = (native_handle_t *)fbHandle;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700396
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800397 copybit_device_t *copybit = mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700398
399 int32_t screen_w = displayFrame.right - displayFrame.left;
400 int32_t screen_h = displayFrame.bottom - displayFrame.top;
401 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
402 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
403
404 // Copybit dst
405 float copybitsMaxScale =
406 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
407 float copybitsMinScale =
408 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
409
410 if((layer->transform == HWC_TRANSFORM_ROT_90) ||
411 (layer->transform == HWC_TRANSFORM_ROT_270)) {
412 //swap screen width and height
413 int tmp = screen_w;
414 screen_w = screen_h;
415 screen_h = tmp;
416 }
417 private_handle_t *tmpHnd = NULL;
418
419 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
420 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
421 screen_w=%d src_crop_width=%d", __FUNCTION__, screen_w,
422 src_crop_width,screen_w,src_crop_width);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700423 return -1;
424 }
425
426 float dsdx = (float)screen_w/src_crop_width;
427 float dtdy = (float)screen_h/src_crop_height;
428
429 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
430 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
431 if(dsdx > scaleLimitMax ||
432 dtdy > scaleLimitMax ||
433 dsdx < 1/scaleLimitMin ||
434 dtdy < 1/scaleLimitMin) {
Terence Hampson3c560b92013-09-03 16:58:02 -0400435 ALOGW("%s: greater than max supported size dsdx=%f dtdy=%f \
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700436 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
437 scaleLimitMax,1/scaleLimitMin);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700438 return -1;
439 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400440 acquireFd = layer->acquireFenceFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700441 if(dsdx > copybitsMaxScale ||
442 dtdy > copybitsMaxScale ||
443 dsdx < 1/copybitsMinScale ||
444 dtdy < 1/copybitsMinScale){
445 // The requested scale is out of the range the hardware
446 // can support.
Terence Hampson663dfa72013-08-08 12:35:41 -0400447 ALOGD("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700448 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
449 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
450 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
451 src_crop_width,src_crop_height);
452
453 //Driver makes width and height as even
454 //that may cause wrong calculation of the ratio
455 //in display and crop.Hence we make
456 //crop width and height as even.
457 src_crop_width = (src_crop_width/2)*2;
458 src_crop_height = (src_crop_height/2)*2;
459
460 int tmp_w = src_crop_width;
461 int tmp_h = src_crop_height;
462
463 if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
464 tmp_w = src_crop_width*copybitsMaxScale;
465 tmp_h = src_crop_height*copybitsMaxScale;
466 }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
467 tmp_w = src_crop_width/copybitsMinScale;
468 tmp_h = src_crop_height/copybitsMinScale;
469 tmp_w = (tmp_w/2)*2;
470 tmp_h = (tmp_h/2)*2;
471 }
Terence Hampson663dfa72013-08-08 12:35:41 -0400472 ALOGD("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700473
Naseer Ahmed64b81212013-02-14 10:29:47 -0500474 int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400475 int format = fbHandle->format;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700476
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400477 // We do not want copybit to generate alpha values from nothing
478 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
479 src.format != HAL_PIXEL_FORMAT_RGBA_8888) {
480 format = HAL_PIXEL_FORMAT_RGBX_8888;
481 }
482 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, format, usage)){
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700483 copybit_image_t tmp_dst;
484 copybit_rect_t tmp_rect;
485 tmp_dst.w = tmp_w;
486 tmp_dst.h = tmp_h;
487 tmp_dst.format = tmpHnd->format;
488 tmp_dst.handle = tmpHnd;
489 tmp_dst.horiz_padding = src.horiz_padding;
490 tmp_dst.vert_padding = src.vert_padding;
491 tmp_rect.l = 0;
492 tmp_rect.t = 0;
493 tmp_rect.r = tmp_dst.w;
494 tmp_rect.b = tmp_dst.h;
495 //create one clip region
496 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
497 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
498 region_iterator tmp_it(tmp_hwc_reg);
499 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700500 //TODO: once, we are able to read layer alpha, update this
501 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Terence Hampsonadf47302013-05-23 12:21:02 -0400502 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700503 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
504 &srcRect, &tmp_it);
505 if(err < 0){
506 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
507 __LINE__);
508 if(tmpHnd)
509 free_buffer(tmpHnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700510 return err;
511 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400512 // use release fence as aquire fd for next stretch
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400513 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
Terence Hampsonadf47302013-05-23 12:21:02 -0400514 copybit->flush_get_fence(copybit, &acquireFd);
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400515 close(acquireFd);
516 acquireFd = -1;
517 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700518 // copy new src and src rect crop
519 src = tmp_dst;
520 srcRect = tmp_rect;
521 }
522 }
523 // Copybit region
524 hwc_region_t region = layer->visibleRegionScreen;
525 region_iterator copybitRegion(region);
526
527 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
528 renderBuffer->width);
529 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
530 renderBuffer->height);
531 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
shuoy7c101f92013-08-26 14:48:57 +0800532 layerTransform);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700533 //TODO: once, we are able to read layer alpha, update this
534 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800535 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
536 layer->blending);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700537 copybit->set_parameter(copybit, COPYBIT_DITHER,
538 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
539 COPYBIT_ENABLE : COPYBIT_DISABLE);
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530540 copybit->set_parameter(copybit, COPYBIT_FG_LAYER, isFG ?
541 COPYBIT_ENABLE : COPYBIT_DISABLE);
542
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700543 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
544 COPYBIT_ENABLE);
Terence Hampsonadf47302013-05-23 12:21:02 -0400545 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700546 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
547 &copybitRegion);
548 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
549 COPYBIT_DISABLE);
550
Terence Hampsonadf47302013-05-23 12:21:02 -0400551 if(tmpHnd) {
552 if (ctx->mMDP.version < qdutils::MDP_V4_0){
553 int ret = -1, releaseFd;
554 // we need to wait for the buffer before freeing
555 copybit->flush_get_fence(copybit, &releaseFd);
556 ret = sync_wait(releaseFd, 1000);
557 if(ret < 0) {
558 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
559 __FUNCTION__, errno, strerror(errno));
560 }
561 close(releaseFd);
562 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700563 free_buffer(tmpHnd);
Terence Hampsonadf47302013-05-23 12:21:02 -0400564 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700565
566 if(err < 0)
567 ALOGE("%s: copybit stretch failed",__FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700568 return err;
569}
570
Sushil Chauhan943797c2013-10-21 17:35:55 -0700571int CopyBit::fillColorUsingCopybit(hwc_layer_1_t *layer,
572 private_handle_t *renderBuffer)
573{
574 if (!renderBuffer) {
575 ALOGE("%s: Render Buffer is NULL", __FUNCTION__);
576 return -1;
577 }
578
579 // Copybit dst
580 copybit_image_t dst;
581 dst.w = ALIGN(renderBuffer->width, 32);
582 dst.h = renderBuffer->height;
583 dst.format = renderBuffer->format;
584 dst.base = (void *)renderBuffer->base;
585 dst.handle = (native_handle_t *)renderBuffer;
586
587 // Copybit dst rect
588 hwc_rect_t displayFrame = layer->displayFrame;
589 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
590 displayFrame.right, displayFrame.bottom};
591
592 uint32_t color = layer->transform;
593 copybit_device_t *copybit = mEngine;
594 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
595 renderBuffer->width);
596 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
597 renderBuffer->height);
598 copybit->set_parameter(copybit, COPYBIT_DITHER,
599 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ?
600 COPYBIT_ENABLE : COPYBIT_DISABLE);
601 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
602 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
603 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_ENABLE);
604 int res = copybit->fill_color(copybit, &dst, &dstRect, color);
605 copybit->set_parameter(copybit,COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_DISABLE);
606 return res;
607}
608
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700609void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmed45a99602012-07-31 19:15:24 -0700610 unsigned int& width, unsigned int& height)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700611{
612 hwc_rect_t displayFrame = layer->displayFrame;
613
614 width = displayFrame.right - displayFrame.left;
615 height = displayFrame.bottom - displayFrame.top;
616}
617
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800618bool CopyBit::validateParams(hwc_context_t *ctx,
619 const hwc_display_contents_1_t *list) {
620 //Validate parameters
621 if (!ctx) {
622 ALOGE("%s:Invalid HWC context", __FUNCTION__);
623 return false;
624 } else if (!list) {
625 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
626 return false;
627 }
628 return true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700629}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700630
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700631
Naseer Ahmed64b81212013-02-14 10:29:47 -0500632int CopyBit::allocRenderBuffers(int w, int h, int f)
633{
634 int ret = 0;
635 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
636 if (mRenderBuffer[i] == NULL) {
637 ret = alloc_buffer(&mRenderBuffer[i],
638 w, h, f,
639 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
640 }
641 if(ret < 0) {
642 freeRenderBuffers();
643 break;
644 }
645 }
646 return ret;
647}
648
649void CopyBit::freeRenderBuffers()
650{
651 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
652 if(mRenderBuffer[i]) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530653 //Since we are freeing buffer close the fence if it has a valid one.
654 if(mRelFd[i] >= 0) {
655 close(mRelFd[i]);
656 mRelFd[i] = -1;
657 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500658 free_buffer(mRenderBuffer[i]);
659 mRenderBuffer[i] = NULL;
660 }
661 }
662}
663
664private_handle_t * CopyBit::getCurrentRenderBuffer() {
665 return mRenderBuffer[mCurRenderBufferIndex];
666}
667
668void CopyBit::setReleaseFd(int fd) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530669 if(mRelFd[mCurRenderBufferIndex] >=0)
670 close(mRelFd[mCurRenderBufferIndex]);
671 mRelFd[mCurRenderBufferIndex] = dup(fd);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500672}
673
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800674struct copybit_device_t* CopyBit::getCopyBitDevice() {
675 return mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700676}
677
Naseer Ahmed64b81212013-02-14 10:29:47 -0500678CopyBit::CopyBit():mIsModeOn(false), mCopyBitDraw(false),
679 mCurRenderBufferIndex(0){
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700680 hw_module_t const *module;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530681 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500682 mRenderBuffer[i] = NULL;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530683 mRelFd[i] = -1;
684 }
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +0530685
686 char value[PROPERTY_VALUE_MAX];
687 property_get("debug.hwc.dynThreshold", value, "2");
688 mDynThreshold = atof(value);
689
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700690 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800691 if(copybit_open(module, &mEngine) < 0) {
692 ALOGE("FATAL ERROR: copybit open failed.");
693 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700694 } else {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800695 ALOGE("FATAL ERROR: copybit hw module not found");
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700696 }
697}
Saurabh Shah661a58f2012-08-30 15:30:49 -0700698
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800699CopyBit::~CopyBit()
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700700{
Naseer Ahmed64b81212013-02-14 10:29:47 -0500701 freeRenderBuffers();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800702 if(mEngine)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700703 {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800704 copybit_close(mEngine);
705 mEngine = NULL;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700706 }
707}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700708}; //namespace qhwc