diff options
-rw-r--r-- | hwc/Android.mk | 2 | ||||
-rw-r--r-- | hwc/hwc.c | 310 | ||||
-rw-r--r-- | hwc/rgz_2d.c | 1528 | ||||
-rw-r--r-- | hwc/rgz_2d.h | 291 |
4 files changed, 2111 insertions, 20 deletions
diff --git a/hwc/Android.mk b/hwc/Android.mk index c6a2eae..140e11a 100644 --- a/hwc/Android.mk +++ b/hwc/Android.mk @@ -8,7 +8,7 @@ LOCAL_PRELINK_MODULE := false LOCAL_ARM_MODE := arm LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/../vendor/lib/hw LOCAL_SHARED_LIBRARIES := liblog libEGL libcutils libutils libhardware libhardware_legacy libz -LOCAL_SRC_FILES := hwc.c +LOCAL_SRC_FILES := hwc.c rgz_2d.c LOCAL_STATIC_LIBRARIES := libpng LOCAL_MODULE_TAGS := optional @@ -36,6 +36,9 @@ #include <png.h> #include <system/graphics.h> +#include <linux/bltsville.h> + +#define MAX_HWC_LAYERS 32 #define ASPECT_RATIO_TOLERANCE 0.02f @@ -49,8 +52,10 @@ #define DIV_ROUND_UP(a, b) (((a) + (b) - 1) / (b)) #include <video/dsscomp.h> +#include <video/omap_hwc.h> #include "hal_public.h" +#include "rgz_2d.h" #define MAX_HW_OVERLAYS 4 #define NUM_NONSCALING_OVERLAYS 1 @@ -102,6 +107,17 @@ enum { EXT_HFLIP = (1 << 2), /* flip l-r on output (after rotation) */ }; +enum bltpolicy { + BLTPOLICY_DISABLED = 0, + BLTPOLICY_DEFAULT = 1, /* Default blit policy */ + BLTPOLICY_ALL, /* Test mode to attempt to blit all */ +}; + +enum bltmode { + BLTMODE_PAINT = 0, /* Attempt to blit layer by layer */ + BLTMODE_REGION = 1, /* Attempt to blit layers via regions */ +}; + /* ARGB image */ struct omap4_hwc_img { int width; @@ -143,16 +159,23 @@ struct omap4_hwc_device { int idle; int ovls_blending; - /* composition data */ - struct dsscomp_setup_dispc_data dsscomp_data; buffer_handle_t *buffers; int use_sgx; int swap_rb; - unsigned int post2_layers; + unsigned int post2_layers; /* Buffers used with DSS pipes*/ + unsigned int post2_blit_buffers; /* Buffers used with blit */ int ext_ovls; /* # of overlays on external display for current composition */ int ext_ovls_wanted; /* # of overlays that should be on external display for current composition */ int last_ext_ovls; /* # of overlays on external/internal display for last composition */ int last_int_ovls; + + enum bltmode blt_mode; + enum bltpolicy blt_policy; + + int blit_flags; + int blit_num; + struct omap_hwc_data comp_data; /* This is a kernel data structure */ + struct rgz_blt_entry blit_ops[RGZ_MAX_BLITS]; }; typedef struct omap4_hwc_device omap4_hwc_device_t; @@ -170,6 +193,11 @@ typedef struct omap4_hwc_device omap4_hwc_device_t; (f) == OMAP_DSS_COLOR_RGB16 ? "RGB565" : "??") static int debug = 0; +static int debugpost2 = 0; +static int debugblt = 0; +static int gshowfps; +static rgz_t grgz; +static struct bvsurfgeom gscrngeom; static void dump_layer(hwc_layer_t const* l) { @@ -240,7 +268,7 @@ static void dump_printf(struct dump_buf *buf, const char *fmt, ...) static void dump_set_info(omap4_hwc_device_t *hwc_dev, hwc_layer_list_t* list) { - struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->dsscomp_data; + struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data; char logbuf[1024]; struct dump_buf log = { .buf = logbuf, @@ -254,7 +282,14 @@ static void dump_set_info(omap4_hwc_device_t *hwc_dev, hwc_layer_list_t* list) dump_printf(&log, " "); hwc_layer_t *layer = &list->hwLayers[i]; IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle; - dump_printf(&log, "%p:%s,", handle, layer->compositionType == HWC_OVERLAY ? "DSS" : "SGX"); + if (hwc_dev->post2_blit_buffers) { + if ((i + 1) < hwc_dev->post2_layers) + dump_printf(&log, "%p:%s,", handle, "DSS"); + else + dump_printf(&log, "%p:%s,", handle, "BV2D"); + } + else + dump_printf(&log, "%p:%s,", handle, layer->compositionType == HWC_OVERLAY ? "DSS" : "SGX"); if ((layer->flags & HWC_SKIP_LAYER) || !handle) { dump_printf(&log, "SKIP"); continue; @@ -285,6 +320,13 @@ static void dump_set_info(omap4_hwc_device_t *hwc_dev, hwc_layer_list_t* list) dump_printf(&log, " "); dump_printf(&log, "%p", hwc_dev->buffers[i]); } + if (hwc_dev->post2_blit_buffers) { + dump_printf(&log, "} B{"); + for (i = hwc_dev->post2_layers; + i < hwc_dev->post2_blit_buffers + hwc_dev->post2_layers; i++) { + dump_printf(&log, "%p ", hwc_dev->buffers[i]); + } + } dump_printf(&log, "}%s\n", hwc_dev->use_sgx ? " swap" : ""); ALOGD("%s", log.buf); @@ -1102,7 +1144,7 @@ static inline int display_area(struct dss2_ovl_info *o) } static int clone_layer(omap4_hwc_device_t *hwc_dev, int ix) { - struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->dsscomp_data; + struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data; omap4_hwc_ext_t *ext = &hwc_dev->ext; int ext_ovl_ix = dsscomp->num_ovls - hwc_dev->post2_layers; struct dss2_ovl_info *o = &dsscomp->ovls[dsscomp->num_ovls]; @@ -1129,7 +1171,7 @@ static int clone_layer(omap4_hwc_device_t *hwc_dev, int ix) { } static int clone_external_layer(omap4_hwc_device_t *hwc_dev, int ix) { - struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->dsscomp_data; + struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data; omap4_hwc_ext_t *ext = &hwc_dev->ext; /* mirror only 1 external layer */ @@ -1183,10 +1225,134 @@ static int setup_mirroring(omap4_hwc_device_t *hwc_dev) return 0; } +static void blit_reset(omap4_hwc_device_t *hwc_dev, int flags) +{ + hwc_dev->blit_flags = 0; + hwc_dev->blit_num = 0; + hwc_dev->post2_blit_buffers = 0; + hwc_dev->comp_data.blit_data.rgz_items = 0; + + /* We want to maintain the rgz dirty region data if there are no geometry changes */ + if (flags & HWC_GEOMETRY_CHANGED) + rgz_release(&grgz); +} + +static int blit_layers(omap4_hwc_device_t *hwc_dev, hwc_layer_list_t *list, int bufoff) +{ + /* Do not blit if this frame will be composed entirely by the GPU */ + if (!list || hwc_dev->force_sgx) + goto err_out; + + int rgz_in_op; + int rgz_out_op; + + switch (hwc_dev->blt_mode) { + case BLTMODE_PAINT: + rgz_in_op = RGZ_IN_HWCCHK; + rgz_out_op = RGZ_OUT_BVCMD_PAINT; + break; + case BLTMODE_REGION: + default: + rgz_in_op = RGZ_IN_HWC; + rgz_out_op = RGZ_OUT_BVCMD_REGION; + break; + } + + rgz_in_params_t in = { + .op = rgz_in_op, + .data = { + .hwc = { + .dstgeom = &gscrngeom, + .layers = list->hwLayers, + .layerno = list->numHwLayers + } + } + }; + + /* + * This means if all the layers marked for the FRAMEBUFFER cannot be + * blitted, do not blit, for e.g. SKIP layers + */ + if (rgz_in(&in, &grgz) != RGZ_ALL) + goto err_out; + + unsigned int i, count = 0; + for (i = 0; i < list->numHwLayers; i++) { + if (list->hwLayers[i].compositionType != HWC_OVERLAY) { + count++; + } + } + + rgz_out_params_t out = { + .op = rgz_out_op, + .data = { + .bvc = { + .dstgeom = &gscrngeom, + .noblend = 0, + } + } + }; + + if (rgz_out(&grgz, &out) != 0) { + ALOGE("Failed generating blits"); + goto err_out; + } + + /* This is a special situation where the regionizer decided no blits are + * needed for this frame but there are blit buffers to synchronize with. Can + * happen only if the regionizer is enabled otherwise it's likely a bug + */ + if (rgz_out_op != RGZ_OUT_BVCMD_REGION && out.data.bvc.out_blits == 0 && out.data.bvc.out_nhndls > 0) { + ALOGE("Regionizer invalid output blit_num %d, post2_blit_buffers %d", out.data.bvc.out_blits, out.data.bvc.out_nhndls); + goto err_out; + } + + hwc_dev->blit_flags |= HWC_BLT_FLAG_USE_FB; + hwc_dev->blit_num = out.data.bvc.out_blits; + hwc_dev->post2_blit_buffers = out.data.bvc.out_nhndls; + for (i = 0; i < hwc_dev->post2_blit_buffers; i++) { + //ALOGI("blit buffers[%d] = %p", bufoff, out.data.bvc.out_hndls[i]); + hwc_dev->buffers[bufoff++] = out.data.bvc.out_hndls[i]; + } + + struct rgz_blt_entry *res_blit_ops = (struct rgz_blt_entry *) out.data.bvc.cmdp; + memcpy(hwc_dev->comp_data.blit_data.rgz_blts, res_blit_ops, sizeof(*res_blit_ops) * out.data.bvc.cmdlen); + ALOGI_IF(debugblt, "blt struct sz %d", sizeof(*res_blit_ops) * out.data.bvc.cmdlen); + ALOGE_IF(hwc_dev->blit_num != out.data.bvc.cmdlen,"blit_num != out.data.bvc.cmdlen, %d != %d", hwc_dev->blit_num, out.data.bvc.cmdlen); + + /* all layers will be rendered without SGX help either via DSS or blitter */ + for (i = 0; i < list->numHwLayers; i++) { + if (list->hwLayers[i].compositionType != HWC_OVERLAY) { + list->hwLayers[i].compositionType = HWC_OVERLAY; + //ALOGI("blitting layer %d", i); + } + list->hwLayers[i].hints &= ~HWC_HINT_CLEAR_FB; + } + return 1; + +err_out: + rgz_release(&grgz); + return 0; +} + +void debug_post2(omap4_hwc_device_t *hwc_dev, int nbufs) +{ + if (!debugpost2) + return; + struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data; + int i; + for (i=0; i<nbufs; i++) { + ALOGI("buf[%d] hndl %p", i, hwc_dev->buffers[i]); + } + for (i=0; i < dsscomp->num_ovls; i++) { + ALOGI("ovl[%d] ba %d", i, dsscomp->ovls[i].ba); + } +} + static int omap4_hwc_prepare(struct hwc_composer_device *dev, hwc_layer_list_t* list) { omap4_hwc_device_t *hwc_dev = (omap4_hwc_device_t *)dev; - struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->dsscomp_data; + struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data; struct counts num = { .composited_layers = list ? list->numHwLayers : 0 }; unsigned int i, ix; @@ -1214,16 +1380,37 @@ static int omap4_hwc_prepare(struct hwc_composer_device *dev, hwc_layer_list_t* } /* setup pipes */ - dsscomp->num_ovls = hwc_dev->use_sgx; int z = 0; int fb_z = -1; int scaled_gfx = 0; int ix_docking = -1; + int blit_all = 0; + blit_reset(hwc_dev, list->flags); + + /* If the SGX is used or we are going to blit something we need a framebuffer + * and a DSS pipe + */ + int needs_fb = hwc_dev->use_sgx; + + if (hwc_dev->blt_policy == BLTPOLICY_ALL) { + /* Check if we can blit everything */ + blit_all = blit_layers(hwc_dev, list, 0); + if (blit_all) { + needs_fb = 1; + hwc_dev->use_sgx = 0; + } + } + + /* If a framebuffer is needed, begin using VID1 for DSS overlay layers, + * we need GFX for FB + */ + dsscomp->num_ovls = needs_fb ? 1 /*VID1*/ : 0 /*GFX*/; + /* set up if DSS layers */ unsigned int mem_used = 0; hwc_dev->ovls_blending = 0; - for (i = 0; list && i < list->numHwLayers; i++) { + for (i = 0; list && i < list->numHwLayers && !blit_all; i++) { hwc_layer_t *layer = &list->hwLayers[i]; IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle; @@ -1249,6 +1436,7 @@ static int omap4_hwc_prepare(struct hwc_composer_device *dev, hwc_layer_list_t* hwc_dev->ovls_blending = 1; hwc_dev->buffers[dsscomp->num_ovls] = layer->handle; + //ALOGI("dss buffers[%d] = %p", dsscomp->num_ovls, hwc_dev->buffers[dsscomp->num_ovls]); omap4_hwc_setup_layer(hwc_dev, &dsscomp->ovls[dsscomp->num_ovls], @@ -1297,15 +1485,31 @@ static int omap4_hwc_prepare(struct hwc_composer_device *dev, hwc_layer_list_t* if (scaled_gfx) dsscomp->ovls[0].cfg.ix = dsscomp->num_ovls; - if (hwc_dev->use_sgx) { + if (hwc_dev->blt_policy == BLTPOLICY_DEFAULT) { + if (hwc_dev->use_sgx) { + if (blit_layers(hwc_dev, list, dsscomp->num_ovls == 1 ? 0 : dsscomp->num_ovls)) { + hwc_dev->use_sgx = 0; + } + } + } + + /* If the SGX is not used and there is blit data we need a framebuffer and + * a DSS pipe well configured for it + */ + if (needs_fb) { /* assign a z-layer for fb */ if (fb_z < 0) { - if (num.composited_layers) + if (!hwc_dev->blt_policy != BLTPOLICY_DISABLED && num.composited_layers) ALOGE("**** should have assigned z-layer for fb"); fb_z = z++; } - - hwc_dev->buffers[0] = NULL; + /* + * This is needed because if we blit all we would lose the handle of + * the first layer + */ + if (hwc_dev->blit_num == 0) { + hwc_dev->buffers[0] = NULL; + } omap4_hwc_setup_layer_base(&dsscomp->ovls[0].cfg, fb_z, hwc_dev->fb_dev->base.format, 1, /* FB is always premultiplied */ @@ -1429,7 +1633,7 @@ static int omap4_hwc_set(struct hwc_composer_device *dev, hwc_display_t dpy, hwc_surface_t sur, hwc_layer_list_t* list) { omap4_hwc_device_t *hwc_dev = (omap4_hwc_device_t *)dev; - struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->dsscomp_data; + struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data; int err = 0; int invalidate; @@ -1462,10 +1666,50 @@ static int omap4_hwc_set(struct hwc_composer_device *dev, hwc_display_t dpy, if (hwc_dev->force_sgx > 0) hwc_dev->force_sgx--; + hwc_dev->comp_data.blit_data.rgz_flags = hwc_dev->blit_flags; + hwc_dev->comp_data.blit_data.rgz_items = hwc_dev->blit_num; + int omaplfb_comp_data_sz = sizeof(hwc_dev->comp_data) + + (hwc_dev->comp_data.blit_data.rgz_items * sizeof(struct rgz_blt_entry)); + + + unsigned int nbufs = hwc_dev->post2_layers; + if (hwc_dev->post2_blit_buffers) { + /* + * We don't want to pass a NULL entry in the Post2, but we need to + * fix up buffer handle array and overlay indexes to account for + * this + */ + nbufs += hwc_dev->post2_blit_buffers - 1; + + if (hwc_dev->post2_layers > 1) { + unsigned int i, j; + for (i = 0; i < nbufs; i++) { + hwc_dev->buffers[i] = hwc_dev->buffers[i+1]; + } + for (i = 1, j= 1; j < hwc_dev->post2_layers; i++, j++) { + dsscomp->ovls[j].ba = i; + } + } + } + ALOGI_IF(debugblt && hwc_dev->blt_policy != BLTPOLICY_DISABLED, + "Post2, blits %d, ovl_buffers %d, blit_buffers %d sgx %d", + hwc_dev->blit_num, hwc_dev->post2_layers, hwc_dev->post2_blit_buffers, + hwc_dev->use_sgx); + + debug_post2(hwc_dev, nbufs); err = hwc_dev->fb_dev->Post2((framebuffer_device_t *)hwc_dev->fb_dev, hwc_dev->buffers, - hwc_dev->post2_layers, - dsscomp, sizeof(*dsscomp)); + nbufs, + dsscomp, omaplfb_comp_data_sz); + + if (!hwc_dev->use_sgx) { + __u32 crt = 0; + int err2 = ioctl(hwc_dev->fb_fd, FBIO_WAITFORVSYNC, &crt); + if (err2) { + ALOGE("failed to wait for vsync (%d)", errno); + err = err ? : -errno; + } + } } hwc_dev->last_ext_ovls = hwc_dev->ext_ovls; hwc_dev->last_int_ovls = hwc_dev->post2_layers; @@ -1484,7 +1728,7 @@ err_out: static void omap4_hwc_dump(struct hwc_composer_device *dev, char *buff, int buff_len) { omap4_hwc_device_t *hwc_dev = (omap4_hwc_device_t *)dev; - struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->dsscomp_data; + struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data; struct dump_buf log = { .buf = buff, .buf_len = buff_len, @@ -1509,6 +1753,14 @@ static void omap4_hwc_dump(struct hwc_composer_device *dev, char *buff, int buff dump_printf(&log, " ix: %d\n", cfg->ix); dump_printf(&log, " zorder: %d\n\n", cfg->zorder); } + + if (hwc_dev->blt_policy != BLTPOLICY_DISABLED) { + dump_printf(&log, " bltpolicy: %s, bltmode: %s\n", + hwc_dev->blt_policy == BLTPOLICY_DEFAULT ? "default" : + hwc_dev->blt_policy == BLTPOLICY_ALL ? "all" : "unknown", + hwc_dev->blt_mode == BLTMODE_PAINT ? "paint" : "regionize"); + } + dump_printf(&log, "\n"); } static void free_png_image(omap4_hwc_device_t *hwc_dev, struct omap4_hwc_img *img) @@ -1986,7 +2238,8 @@ static int omap4_hwc_device_open(const hw_module_t* module, const char* name, goto done; } - hwc_dev->buffers = malloc(sizeof(buffer_handle_t) * MAX_HW_OVERLAYS); + /* Allocate the maximum buffers that we can receive from HWC */ + hwc_dev->buffers = malloc(sizeof(buffer_handle_t) * MAX_HWC_LAYERS); if (!hwc_dev->buffers) { err = -ENOMEM; goto done; @@ -2065,6 +2318,25 @@ static int omap4_hwc_device_open(const hw_module_t* module, const char* name, ALOGI("omap4_hwc_device_open(rgb_order=%d nv12_only=%d)", hwc_dev->flags_rgb_order, hwc_dev->flags_nv12_only); + int gc2d_fd = open("/dev/gcioctl", O_RDWR); + if (gc2d_fd < 0) { + ALOGI("Unable to open gc-core device (%d), blits disabled", errno); + hwc_dev->blt_policy = BLTPOLICY_DISABLED; + } else { + property_get("persist.hwc.bltmode", value, "1"); + hwc_dev->blt_mode = atoi(value); + property_get("persist.hwc.bltpolicy", value, "1"); + hwc_dev->blt_policy = atoi(value); + ALOGI("blitter present, blits mode %d, blits policy %d", hwc_dev->blt_mode, hwc_dev->blt_policy); + close(gc2d_fd); + + if (rgz_get_screengeometry(hwc_dev->fb_fd, &gscrngeom, + hwc_dev->fb_dev->base.format) != 0) { + err = -EINVAL; + goto done; + } + } + done: if (err && hwc_dev) { if (hwc_dev->dsscomp_fd >= 0) diff --git a/hwc/rgz_2d.c b/hwc/rgz_2d.c new file mode 100644 index 0000000..7f95a78 --- /dev/null +++ b/hwc/rgz_2d.c @@ -0,0 +1,1528 @@ +/* + * Copyright (C) Texas Instruments - http://www.ti.com/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <time.h> +#include <assert.h> +#include <strings.h> +#include <dlfcn.h> + +#include <fcntl.h> +#include <sys/mman.h> +#include <linux/fb.h> +#include <linux/bltsville.h> +#include <video/dsscomp.h> +#include <video/omap_hwc.h> + +#ifndef RGZ_TEST_INTEGRATION +#include <cutils/log.h> +#include <cutils/properties.h> +#include <hardware/hwcomposer.h> +#include "hal_public.h" +#else +#include "hwcomposer.h" +#include "buffer_handle.h" +#define ALIGN(x,a) (((x) + (a) - 1L) & ~((a) - 1L)) +#define HW_ALIGN 32 +#endif + +#include "rgz_2d.h" + +#ifdef RGZ_TEST_INTEGRATION +extern void BVDump(const char* prefix, const char* tab, const struct bvbltparams* parms); +#define BVDUMP(p,t,parms) BVDump(p, t, parms) +#define HANDLE_TO_BUFFER(h) handle_to_buffer(h) +#define HANDLE_TO_STRIDE(h) handle_to_stride(h) +#else +static int rgz_handle_to_stride(IMG_native_handle_t *h); +#define BVDUMP(p,t,parms) +#define HANDLE_TO_BUFFER(h) NULL +/* Needs to be meaningful for TILER & GFX buffers and NV12 */ +#define HANDLE_TO_STRIDE(h) rgz_handle_to_stride(h) +#endif +#define DSTSTRIDE(dstgeom) dstgeom->virtstride + +/* Borrowed macros from hwc.c vvv - consider sharing later */ +#define min(a, b) ( { typeof(a) __a = (a), __b = (b); __a < __b ? __a : __b; } ) +#define max(a, b) ( { typeof(a) __a = (a), __b = (b); __a > __b ? __a : __b; } ) +#define swap(a, b) do { typeof(a) __a = (a); (a) = (b); (b) = __a; } while (0) + +#define WIDTH(rect) ((rect).right - (rect).left) +#define HEIGHT(rect) ((rect).bottom - (rect).top) + +#define is_RGB(format) ((format) == HAL_PIXEL_FORMAT_BGRA_8888 || (format) == HAL_PIXEL_FORMAT_RGB_565 || (format) == HAL_PIXEL_FORMAT_BGRX_8888) +#define is_BGR(format) ((format) == HAL_PIXEL_FORMAT_RGBX_8888 || (format) == HAL_PIXEL_FORMAT_RGBA_8888) +#define is_NV12(format) ((format) == HAL_PIXEL_FORMAT_TI_NV12 || (format) == HAL_PIXEL_FORMAT_TI_NV12_PADDED) + +#define HAL_PIXEL_FORMAT_BGRX_8888 0x1FF +#define HAL_PIXEL_FORMAT_TI_NV12 0x100 +#define HAL_PIXEL_FORMAT_TI_NV12_PADDED 0x101 +/* Borrowed macros from hwc.c ^^^ */ +#define is_OPAQUE(format) ((format) == HAL_PIXEL_FORMAT_RGB_565 || (format) == HAL_PIXEL_FORMAT_RGBX_8888 || (format) == HAL_PIXEL_FORMAT_BGRX_8888) + +/* OUTP the means for grabbing diagnostic data */ +#ifndef RGZ_TEST_INTEGRATION +#define OUTP ALOGI +#define OUTE ALOGE +#else +#define OUTP(...) { printf(__VA_ARGS__); printf("\n"); fflush(stdout); } +#define OUTE OUTP +#define ALOGD_IF(debug, ...) { if (debug) OUTP(__VA_ARGS__); } +#endif + +#define IS_BVCMD(params) (params->op == RGZ_OUT_BVCMD_REGION || params->op == RGZ_OUT_BVCMD_PAINT) + +/* Number of framebuffers to track */ +#define RGZ_NUM_FB 2 + +struct rgz_blts { + struct rgz_blt_entry bvcmds[RGZ_MAX_BLITS]; + int idx; +}; + + +static int rgz_hwc_layer_blit(hwc_layer_t *l, rgz_out_params_t *params, int buff_idx); +static void rgz_blts_init(struct rgz_blts *blts); +static void rgz_blts_free(struct rgz_blts *blts); +static struct rgz_blt_entry* rgz_blts_get(struct rgz_blts *blts, rgz_out_params_t *params); +static int rgz_blts_bvdirect(rgz_t* rgz, struct rgz_blts *blts, rgz_out_params_t *params); + +int debug = 0; +struct rgz_blts blts; +/* Represents a screen sized background layer */ +static hwc_layer_t bg_layer; + +static void svgout_header(int htmlw, int htmlh, int coordw, int coordh) +{ + OUTP("<svg xmlns=\"http://www.w3.org/2000/svg\"" + "width=\"%d\" height=\"%d\"" + "viewBox=\"0 0 %d %d\">", + htmlw, htmlh, coordw, coordh); +} + +static void svgout_footer(void) +{ + OUTP("</svg>"); +} + +static void svgout_rect(blit_rect_t *r, char *color, char *text) +{ + OUTP("<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" fill=\"%s\" " + "fill-opacity=\"%f\" stroke=\"black\" stroke-width=\"1\" />", + r->left, r->top, r->right - r->left, r->bottom - r->top, color, 1.0f); + + if (!text) + return; + + OUTP("<text x=\"%d\" y=\"%d\" style=\"font-size:30\" fill=\"black\">%s" + "</text>", + r->left, r->top + 40, text); +} + +static int empty_rect(blit_rect_t *r) +{ + return !((((r->left == r->top) == r->right) == r->bottom) == 0); +} + +static int get_top_rect(blit_hregion_t *hregion, int subregion, blit_rect_t **routp) +{ + int l = hregion->nlayers - 1; + do { + *routp = &hregion->blitrects[l][subregion]; + if (!empty_rect(*routp)) + break; + } + while (--l >= 0); + return l; +} + +/* + * The idea here is that we walk the layers from front to back and count the + * number of layers in the hregion until the first layer which doesn't require + * blending. + */ +static int get_layer_ops(blit_hregion_t *hregion, int subregion, int *bottom) +{ + int l = hregion->nlayers - 1; + int ops = 0; + *bottom = -1; + do { + if (!empty_rect(&hregion->blitrects[l][subregion])) { + ops++; + *bottom = l; + hwc_layer_t *layer = hregion->rgz_layers[l]->hwc_layer; + IMG_native_handle_t *h = (IMG_native_handle_t *)layer->handle; + if ((layer->blending != HWC_BLENDING_PREMULT) || is_OPAQUE(h->iFormat)) + break; + } + } + while (--l >= 0); + return ops; +} + +static int get_layer_ops_next(blit_hregion_t *hregion, int subregion, int l) +{ + while (++l < hregion->nlayers) { + if (!empty_rect(&hregion->blitrects[l][subregion])) + return l; + } + return -1; +} + +static int svgout_intersects_display(blit_rect_t *a, int dispw, int disph) +{ + return ((a->bottom > 0) && (a->top < disph) && + (a->right > 0) && (a->left < dispw)); +} + +static void svgout_hregion(blit_hregion_t *hregion, int dispw, int disph) +{ + char *colors[] = {"red", "orange", "yellow", "green", "blue", "indigo", "violet", NULL}; + int b; + for (b = 0; b < hregion->nsubregions; b++) { + blit_rect_t *rect; + (void)get_top_rect(hregion, b, &rect); + /* Only generate SVG for subregions intersecting the displayed area */ + if (!svgout_intersects_display(rect, dispw, disph)) + continue; + svgout_rect(rect, colors[b % 7], NULL); + } +} + +static void rgz_out_svg(rgz_t *rgz, rgz_out_params_t *params) +{ + if (!rgz || !(rgz->state & RGZ_REGION_DATA)) { + OUTE("rgz_out_svg invoked with bad state"); + return; + } + blit_hregion_t *hregions = rgz->hregions; + svgout_header(params->data.svg.htmlw, params->data.svg.htmlh, + params->data.svg.dispw, params->data.svg.disph); + int i; + for (i = 0; i < rgz->nhregions; i++) { + + OUTP("<!-- hregion %d (subcount %d)-->", i, hregions[i].nsubregions); + svgout_hregion(&hregions[i], params->data.svg.dispw, + params->data.svg.disph); + } + svgout_footer(); +} + +/* XXX duplicate of hwc.c version */ +static void dump_layer(hwc_layer_t const* l, int iserr) +{ +#define FMT(f) ((f) == HAL_PIXEL_FORMAT_TI_NV12 ? "NV12" : \ + (f) == HAL_PIXEL_FORMAT_BGRX_8888 ? "xRGB32" : \ + (f) == HAL_PIXEL_FORMAT_RGBX_8888 ? "xBGR32" : \ + (f) == HAL_PIXEL_FORMAT_BGRA_8888 ? "ARGB32" : \ + (f) == HAL_PIXEL_FORMAT_RGBA_8888 ? "ABGR32" : \ + (f) == HAL_PIXEL_FORMAT_RGB_565 ? "RGB565" : "??") + + OUTE("%stype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}, {%d,%d,%d,%d}", + iserr ? ">> " : " ", + l->compositionType, l->flags, l->handle, l->transform, l->blending, + l->sourceCrop.left, + l->sourceCrop.top, + l->sourceCrop.right, + l->sourceCrop.bottom, + l->displayFrame.left, + l->displayFrame.top, + l->displayFrame.right, + l->displayFrame.bottom); + if (l->handle) { + IMG_native_handle_t *h = (IMG_native_handle_t *)l->handle; + OUTE("%s%d*%d(%s)", + iserr ? ">> " : " ", + h->iWidth, h->iHeight, FMT(h->iFormat)); + OUTE("hndl %p", l->handle); + } +} + +static void dump_all(rgz_layer_t *rgz_layers, unsigned int layerno, unsigned int errlayer) +{ + unsigned int i; + for (i = 0; i < layerno; i++) { + hwc_layer_t *l = rgz_layers[i].hwc_layer; + OUTE("Layer %d", i); + dump_layer(l, errlayer == i); + } +} + +static int rgz_out_bvdirect_paint(rgz_t *rgz, rgz_out_params_t *params) +{ + int rv = 0; + unsigned int i; + (void)rgz; + + rgz_blts_init(&blts); + + /* Begin from index 1 to remove the background layer from the output */ + for (i = 1; i < rgz->rgz_layerno; i++) { + hwc_layer_t *l = rgz->rgz_layers[i].hwc_layer; + + rv = rgz_hwc_layer_blit(l, params, -1); + if (rv) { + OUTE("bvdirect_paint: error in layer %d: %d", i, rv); + dump_all(rgz->rgz_layers, rgz->rgz_layerno, i); + rgz_blts_free(&blts); + return rv; + } + } + rgz_blts_bvdirect(rgz, &blts, params); + rgz_blts_free(&blts); + return rv; +} + +/* + * Clear the destination buffer, if rect is NULL means the whole screen, rect + * cannot be outside the boundaries of the screen + */ +static void rgz_out_clrdst(rgz_out_params_t *params, blit_rect_t *rect) +{ + struct bvsurfgeom *scrgeom = params->data.bvc.dstgeom; + + struct rgz_blt_entry* e; + e = rgz_blts_get(&blts, params); + + struct bvbuffdesc *src1desc = &e->src1desc; + src1desc->structsize = sizeof(struct bvbuffdesc); + src1desc->length = 4; + /* + * With the HWC we don't bother having a buffer for the fill we'll get the + * OMAPLFB to fixup the src1desc if this address is -1 + */ + src1desc->auxptr = (void*)-1; + struct bvsurfgeom *src1geom = &e->src1geom; + src1geom->structsize = sizeof(struct bvsurfgeom); + src1geom->format = OCDFMT_RGBA24; + src1geom->width = src1geom->height = 1; + src1geom->orientation = 0; + src1geom->virtstride = 1; + + struct bvsurfgeom *dstgeom = &e->dstgeom; + dstgeom->structsize = sizeof(struct bvsurfgeom); + dstgeom->format = scrgeom->format; + dstgeom->width = scrgeom->width; + dstgeom->height = scrgeom->height; + dstgeom->orientation = 0; /* TODO */ + dstgeom->virtstride = DSTSTRIDE(scrgeom); + + struct bvbltparams *bp = &e->bp; + bp->structsize = sizeof(struct bvbltparams); + bp->dstgeom = dstgeom; + bp->src1.desc = src1desc; + bp->src1geom = src1geom; + bp->src1rect.left = 0; + bp->src1rect.top = 0; + bp->src1rect.width = bp->src1rect.height = 1; + bp->cliprect.left = bp->dstrect.left = rect ? rect->left : 0; + bp->cliprect.top = bp->dstrect.top = rect ? rect->top : 0; + bp->cliprect.width = bp->dstrect.width = rect ? (unsigned int) WIDTH(*rect) : scrgeom->width; + bp->cliprect.height = bp->dstrect.height = rect ? (unsigned int) HEIGHT(*rect) : scrgeom->height; + + bp->flags = BVFLAG_CLIP | BVFLAG_ROP; + bp->op.rop = 0xCCCC; /* SRCCOPY */ +} + +static int rgz_out_bvcmd_paint(rgz_t *rgz, rgz_out_params_t *params) +{ + int rv = 0; + params->data.bvc.out_blits = 0; + params->data.bvc.out_nhndls = 0; + rgz_blts_init(&blts); + rgz_out_clrdst(params, NULL); + + unsigned int i; + + /* Begin from index 1 to remove the background layer from the output */ + for (i = 1; i < rgz->rgz_layerno; i++) { + hwc_layer_t *l = rgz->rgz_layers[i].hwc_layer; + + //OUTP("blitting meminfo %d", rgz->rgz_layers[i].buffidx); + + rv = rgz_hwc_layer_blit(l, params, rgz->rgz_layers[i].buffidx); + if (rv) { + OUTE("bvcmd_paint: error in layer %d: %d", i, rv); + dump_all(rgz->rgz_layers, rgz->rgz_layerno, i); + rgz_blts_free(&blts); + return rv; + } + params->data.bvc.out_hndls[i-1] = l->handle; + params->data.bvc.out_nhndls++; + } + + /* FIXME: we want to be able to call rgz_blts_free and populate the actual + * composition data structure ourselves */ + params->data.bvc.cmdp = blts.bvcmds; + params->data.bvc.cmdlen = blts.idx; + + if (params->data.bvc.out_blits >= RGZ_MAX_BLITS) { + rv = -1; + // rgz_blts_free(&blts); // FIXME + } + return rv; +} + +static float getscalew(hwc_layer_t *layer) +{ + int w = WIDTH(layer->sourceCrop); + int h = HEIGHT(layer->sourceCrop); + + if (layer->transform & HWC_TRANSFORM_ROT_90) + swap(w, h); + + return ((float)WIDTH(layer->displayFrame)) / (float)w; +} + +static float getscaleh(hwc_layer_t *layer) +{ + int w = WIDTH(layer->sourceCrop); + int h = HEIGHT(layer->sourceCrop); + + if (layer->transform & HWC_TRANSFORM_ROT_90) + swap(w, h); + + return ((float)HEIGHT(layer->displayFrame)) / (float)h; +} + +static int rgz_bswap(int *a, int *b) +{ + if (*a > *b) { + int tmp = *b; + *b = *a; + *a = tmp; + return 1; + } + return 0; +} + +/* + * Simple bubble sort on an array + */ +static void rgz_bsort(int *a, int len) +{ + int i, s; + + do { + s=0; + for (i=0; i+1<len; i++) { + if (rgz_bswap(&a[i], &a[i+1])) + s = 1; + } + } while (s); +} + +/* + * Leave only unique numbers in a sorted array + */ +static int rgz_bunique(int *a, int len) +{ + int unique = 1; + int base = 0; + while (base + 1 < len) { + if (a[base] == a[base + 1]) { + int skip = 1; + while (base + skip < len && a[base] == a[base + skip]) + skip++; + if (base + skip == len) + break; + int i; + for (i = 0; i < skip - 1; i++) + a[base + 1 + i] = a[base + skip]; + } + unique++; + base++; + } + return unique; +} + +static int rgz_hwc_layer_sortbyy(rgz_layer_t *ra, int rsz, int *out, int *width, int screen_height) +{ + int outsz = 0; + int i; + *width = 0; + for (i = 0; i < rsz; i++) { + hwc_layer_t *layer = ra[i].hwc_layer; + /* Maintain regions inside display boundaries */ + int top = layer->displayFrame.top; + int bottom = layer->displayFrame.bottom; + out[outsz++] = max(0, top); + out[outsz++] = min(bottom, screen_height); + int right = layer->displayFrame.right; + *width = *width > right ? *width : right; + } + rgz_bsort(out, outsz); + return outsz; +} + +static int rgz_hwc_intersects(blit_rect_t *a, hwc_rect_t *b) +{ + return ((a->bottom > b->top) && (a->top < b->bottom) && + (a->right > b->left) && (a->left < b->right)); +} + +static void rgz_gen_blitregions(blit_hregion_t *hregion, int screen_width) +{ +/* + * 1. Get the offsets (left/right positions) of each layer within the + * hregion. Assume that layers describe the bounds of the hregion. + * 2. We should then be able to generate an array of rects + * 3. Each layer will have a different z-order, for each z-order + * find the intersection. Some intersections will be empty. + */ + + int offsets[RGZ_SUBREGIONMAX]; + int noffsets=0; + int l, r; + for (l = 0; l < hregion->nlayers; l++) { + hwc_layer_t *layer = hregion->rgz_layers[l]->hwc_layer; + /* Make sure the subregion is not outside the boundaries of the screen */ + int left = layer->displayFrame.left; + int right = layer->displayFrame.right; + offsets[noffsets++] = max(0, left); + offsets[noffsets++] = min(right, screen_width); + } + rgz_bsort(offsets, noffsets); + noffsets = rgz_bunique(offsets, noffsets); + hregion->nsubregions = noffsets - 1; + bzero(hregion->blitrects, sizeof(hregion->blitrects)); + for (r = 0; r + 1 < noffsets; r++) { + blit_rect_t subregion; + subregion.top = hregion->rect.top; + subregion.bottom = hregion->rect.bottom; + subregion.left = offsets[r]; + subregion.right = offsets[r+1]; + + ALOGD_IF(debug, " sub l %d r %d", + subregion.left, subregion.right); + for (l = 0; l < hregion->nlayers; l++) { + hwc_layer_t *layer = hregion->rgz_layers[l]->hwc_layer; + if (rgz_hwc_intersects(&subregion, &layer->displayFrame)) { + + hregion->blitrects[l][r] = subregion; + + ALOGD_IF(debug, "hregion->blitrects[%d][%d] (%d %d %d %d)", l, r, + hregion->blitrects[l][r].left, + hregion->blitrects[l][r].top, + hregion->blitrects[l][r].right, + hregion->blitrects[l][r].bottom); + } + } + } +} + +static int rgz_hwc_scaled(hwc_layer_t *layer) +{ + int w = WIDTH(layer->sourceCrop); + int h = HEIGHT(layer->sourceCrop); + + if (layer->transform & HWC_TRANSFORM_ROT_90) + swap(w, h); + + return WIDTH(layer->displayFrame) != w || HEIGHT(layer->displayFrame) != h; +} + +static int rgz_in_valid_hwc_layer(hwc_layer_t *layer) +{ + IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle; + if ((layer->flags & HWC_SKIP_LAYER) || !handle) + return 0; + + if (is_NV12(handle->iFormat) || layer->transform || rgz_hwc_scaled(layer)) + return 0; + + switch(handle->iFormat) { + case HAL_PIXEL_FORMAT_BGRX_8888: + case HAL_PIXEL_FORMAT_RGBX_8888: + case HAL_PIXEL_FORMAT_RGB_565: + case HAL_PIXEL_FORMAT_RGBA_8888: + case HAL_PIXEL_FORMAT_BGRA_8888: + break; + default: + return 0; + } + return 1; +} + +static int rgz_in_hwccheck(rgz_in_params_t *p, rgz_t *rgz) +{ + hwc_layer_t *layers = p->data.hwc.layers; + int layerno = p->data.hwc.layerno; + + rgz->state &= ~RGZ_STATE_INIT; + + if (!layers) + return -1; + + /* For debugging */ + //dump_all(layers, layerno, 0); + + /* + * Store buffer index to be sent in the HWC Post2 list. Any overlay + * meminfos must come first + */ + int l, memidx = 0; + for (l = 0; l < layerno; l++) { + if (layers[l].compositionType == HWC_OVERLAY) + memidx++; + } + + int possible_blit = 0, candidates = 0; + rgz->screen_isdirty = 1; + + /* Insert the background layer at the beginning of the list */ + rgz->rgz_layers[0].hwc_layer = &bg_layer; + + for (l = 0; l < layerno; l++) { + if (layers[l].compositionType == HWC_FRAMEBUFFER) { + candidates++; + if (rgz_in_valid_hwc_layer(&layers[l]) && + possible_blit < RGZ_INPUT_MAXLAYERS) { + rgz_layer_t *rgz_layer = &rgz->rgz_layers[possible_blit+1]; + rgz_layer->hwc_layer = &layers[l]; + rgz_layer->buffidx = memidx++; + if (rgz_layer->hwc_layer->handle != rgz_layer->dirty_hndl) { + rgz_layer->dirty_count = RGZ_NUM_FB; + rgz_layer->dirty_hndl = (void*)rgz_layer->hwc_layer->handle; + } else { + rgz_layer->dirty_count -= rgz_layer->dirty_count ? 1 : 0; + if (rgz_layer->dirty_count == 0) + rgz->screen_isdirty = 0; /* Just update dirty regions from now on */ + } + possible_blit++; + } + } + } + + if (!possible_blit || possible_blit != candidates) { + return -1; + } + + rgz->state |= RGZ_STATE_INIT; + rgz->rgz_layerno = possible_blit + 1; /* Account for background layer */ + + return RGZ_ALL; +} + +static int rgz_in_hwc(rgz_in_params_t *p, rgz_t *rgz) +{ + int yentries[RGZ_SUBREGIONMAX]; + int dispw; /* widest layer */ + int screen_width = p->data.hwc.dstgeom->width; + int screen_height = p->data.hwc.dstgeom->height; + + if (!(rgz->state & RGZ_STATE_INIT)) { + OUTE("rgz_process started with bad state"); + return -1; + } + + /* If there is already region data avoid parsing it again */ + if (rgz->state & RGZ_REGION_DATA) { + return 0; + } + + int layerno = rgz->rgz_layerno; + + /* Find the horizontal regions */ + rgz_layer_t *rgz_layers = rgz->rgz_layers; + int ylen = rgz_hwc_layer_sortbyy(rgz_layers, layerno, yentries, &dispw, screen_height); + + ylen = rgz_bunique(yentries, ylen); + + /* at this point we have an array of horizontal regions */ + rgz->nhregions = ylen - 1; + + blit_hregion_t *hregions = calloc(rgz->nhregions, sizeof(blit_hregion_t)); + if (!hregions) { + OUTE("Unable to allocate memory for hregions"); + return -1; + } + rgz->hregions = hregions; + + ALOGD_IF(debug, "Allocated %d regions (sz = %d), layerno = %d", rgz->nhregions, rgz->nhregions * sizeof(blit_hregion_t), layerno); + int i, j; + for (i = 0; i < rgz->nhregions; i++) { + hregions[i].rect.top = yentries[i]; + hregions[i].rect.bottom = yentries[i+1]; + /* Avoid hregions outside the display boundaries */ + hregions[i].rect.left = 0; + hregions[i].rect.right = dispw > screen_width ? screen_width : dispw; + hregions[i].nlayers = 0; + for (j = 0; j < layerno; j++) { + hwc_layer_t *layer = rgz_layers[j].hwc_layer; + if (rgz_hwc_intersects(&hregions[i].rect, &layer->displayFrame)) { + int l = hregions[i].nlayers++; + hregions[i].rgz_layers[l] = &rgz_layers[j]; + } + } + } + + /* Calculate blit regions */ + for (i = 0; i < rgz->nhregions; i++) { + rgz_gen_blitregions(&hregions[i], screen_width); + ALOGD_IF(debug, "hregion %3d: nsubregions %d", i, hregions[i].nsubregions); + ALOGD_IF(debug, " : %d to %d: ", + hregions[i].rect.top, hregions[i].rect.bottom); + for (j = 0; j < hregions[i].nlayers; j++) + ALOGD_IF(debug, " %p ", hregions[i].rgz_layers[j]->hwc_layer); + } + rgz->state |= RGZ_REGION_DATA; + return 0; +} + +/* + * generate a human readable description of the layer + * + * idx, flags, fmt, type, sleft, stop, sright, sbot, dleft, dtop, \ + * dright, dbot, rot, flip, blending, scalew, scaleh, visrects + * + */ +static void rgz_print_layer(hwc_layer_t *l, int idx, int csv) +{ + char big_log[1024]; + int e = sizeof(big_log); + char *end = big_log + e; + e -= snprintf(end - e, e, "<!-- LAYER-DAT: %d", idx); + + + e -= snprintf(end - e, e, "%s %p", csv ? "," : " hndl:", + l->handle ? l->handle : NULL); + + e -= snprintf(end - e, e, "%s %s", csv ? "," : " flags:", + l->flags & HWC_SKIP_LAYER ? "skip" : "none"); + + IMG_native_handle_t *handle = (IMG_native_handle_t *)l->handle; + if (handle) { + e -= snprintf(end - e, e, "%s", csv ? ", " : " fmt: "); + switch(handle->iFormat) { + case HAL_PIXEL_FORMAT_BGRA_8888: + e -= snprintf(end - e, e, "bgra"); break; + case HAL_PIXEL_FORMAT_RGB_565: + e -= snprintf(end - e, e, "rgb565"); break; + case HAL_PIXEL_FORMAT_BGRX_8888: + e -= snprintf(end - e, e, "bgrx"); break; + case HAL_PIXEL_FORMAT_RGBX_8888: + e -= snprintf(end - e, e, "rgbx"); break; + case HAL_PIXEL_FORMAT_RGBA_8888: + e -= snprintf(end - e, e, "rgba"); break; + case HAL_PIXEL_FORMAT_TI_NV12: + case HAL_PIXEL_FORMAT_TI_NV12_PADDED: + e -= snprintf(end - e, e, "nv12"); break; + default: + e -= snprintf(end - e, e, "unknown"); + } + e -= snprintf(end - e, e, "%s", csv ? ", " : " type: "); + if (handle->usage & GRALLOC_USAGE_HW_RENDER) + e -= snprintf(end - e, e, "hw"); + else if (handle->usage & GRALLOC_USAGE_SW_READ_MASK || + handle->usage & GRALLOC_USAGE_SW_WRITE_MASK) + e -= snprintf(end - e, e, "sw"); + else + e -= snprintf(end - e, e, "unknown"); + } else { + e -= snprintf(end - e, e, csv ? ", unknown" : " fmt: unknown"); + e -= snprintf(end - e, e, csv ? ", na" : " type: na"); + } + e -= snprintf(end - e, e, csv ? ", %d, %d, %d, %d" : " src: %d %d %d %d", + l->sourceCrop.left, l->sourceCrop.top, l->sourceCrop.right, + l->sourceCrop.bottom); + e -= snprintf(end - e, e, csv ? ", %d, %d, %d, %d" : " disp: %d %d %d %d", + l->displayFrame.left, l->displayFrame.top, + l->displayFrame.right, l->displayFrame.bottom); + + e -= snprintf(end - e, e, "%s %s", csv ? "," : " rot:", + l->transform & HWC_TRANSFORM_ROT_90 ? "90" : + l->transform & HWC_TRANSFORM_ROT_180 ? "180" : + l->transform & HWC_TRANSFORM_ROT_270 ? "270" : "none"); + + char flip[5] = ""; + strcat(flip, l->transform & HWC_TRANSFORM_FLIP_H ? "H" : ""); + strcat(flip, l->transform & HWC_TRANSFORM_FLIP_V ? "V" : ""); + if (!(l->transform & (HWC_TRANSFORM_FLIP_V|HWC_TRANSFORM_FLIP_H))) + strcpy(flip, "none"); + e -= snprintf(end - e, e, "%s %s", csv ? "," : " flip:", flip); + + e -= snprintf(end - e, e, "%s %s", csv ? "," : " blending:", + l->blending == HWC_BLENDING_NONE ? "none" : + l->blending == HWC_BLENDING_PREMULT ? "premult" : + l->blending == HWC_BLENDING_COVERAGE ? "coverage" : "invalid"); + + e -= snprintf(end - e, e, "%s %1.3f", csv ? "," : " scalew:", getscalew(l)); + e -= snprintf(end - e, e, "%s %1.3f", csv ? "," : " scaleh:", getscaleh(l)); + + e -= snprintf(end - e, e, "%s %d", csv ? "," : " visrect:", + l->visibleRegionScreen.numRects); + + if (!csv) { + e -= snprintf(end - e, e, " -->"); + OUTP("%s", big_log); + + size_t i = 0; + for (; i < l->visibleRegionScreen.numRects; i++) { + hwc_rect_t const *r = &l->visibleRegionScreen.rects[i]; + OUTP("<!-- LAYER-VIS: %d: rect: %d %d %d %d -->", + i, r->left, r->top, r->right, r->bottom); + } + } else { + size_t i = 0; + for (; i < l->visibleRegionScreen.numRects; i++) { + hwc_rect_t const *r = &l->visibleRegionScreen.rects[i]; + e -= snprintf(end - e, e, ", %d, %d, %d, %d", + r->left, r->top, r->right, r->bottom); + } + e -= snprintf(end - e, e, " -->"); + OUTP("%s", big_log); + } +} + +static void rgz_print_layers(hwc_layer_list_t* list, int csv) +{ + size_t i; + for (i = 0; i < list->numHwLayers; i++) { + hwc_layer_t *l = &list->hwLayers[i]; + rgz_print_layer(l, i, csv); + } +} + +static int hal_to_ocd(int color) +{ + switch(color) { + case HAL_PIXEL_FORMAT_BGRA_8888: + return OCDFMT_BGRA24; + case HAL_PIXEL_FORMAT_BGRX_8888: + return OCDFMT_BGR124; + case HAL_PIXEL_FORMAT_RGB_565: + return OCDFMT_RGB16; + case HAL_PIXEL_FORMAT_RGBA_8888: + return OCDFMT_RGBA24; + case HAL_PIXEL_FORMAT_RGBX_8888: + return OCDFMT_RGB124; + case HAL_PIXEL_FORMAT_TI_NV12: + return OCDFMT_NV12; + case HAL_PIXEL_FORMAT_YV12: + return OCDFMT_YV12; + default: + return OCDFMT_UNKNOWN; + } +} + +/* + * The loadbltsville fn is only needed for testing, the bltsville shared + * libraries aren't planned to be used directly in production code here + */ +static BVFN_MAP bv_map; +static BVFN_BLT bv_blt; +static BVFN_UNMAP bv_unmap; +#ifndef RGZ_TEST_INTEGRATION +gralloc_module_t const *gralloc; +#endif +#define BLTSVILLELIB "libbltsville_cpu.so" + +#ifdef RGZ_TEST_INTEGRATION +static int loadbltsville(void) +{ + void *hndl = dlopen(BLTSVILLELIB, RTLD_LOCAL | RTLD_LAZY); + if (!hndl) { + OUTE("Loading bltsville failed"); + return -1; + } + bv_map = (BVFN_MAP)dlsym(hndl, "bv_map"); + bv_blt = (BVFN_BLT)dlsym(hndl, "bv_blt"); + bv_unmap = (BVFN_UNMAP)dlsym(hndl, "bv_unmap"); + if(!bv_blt || !bv_map || !bv_unmap) { + OUTE("Missing bltsville fn %p %p %p", bv_map, bv_blt, bv_unmap); + return -1; + } + OUTP("Loaded %s", BLTSVILLELIB); + +#ifndef RGZ_TEST_INTEGRATION + hw_module_t const* module; + int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); + if (err != 0) { + OUTE("Loading gralloc failed"); + return -1; + } + gralloc = (gralloc_module_t const *)module; +#endif + return 0; +} +#else +static int loadbltsville(void) { + return 0; +} +#endif + +#ifndef RGZ_TEST_INTEGRATION +static int rgz_handle_to_stride(IMG_native_handle_t *h) +{ + int bpp = is_NV12(h->iFormat) ? 0 : (h->iFormat == HAL_PIXEL_FORMAT_RGB_565 ? 2 : 4); + int stride = ALIGN(h->iWidth, HW_ALIGN) * bpp; + return stride; +} + +#endif + +extern void BVDump(const char* prefix, const char* tab, const struct bvbltparams* parms); + +static int rgz_hwc_layer_blit(hwc_layer_t *l, rgz_out_params_t *params, int buff_idx) +{ + IMG_native_handle_t *handle = (IMG_native_handle_t *)l->handle; + if (!handle || l->flags & HWC_SKIP_LAYER) { + /* + * This shouldn't happen regionizer should reject compositions w/ skip + * layers + */ + OUTP("Cannot handle skip layers\n"); + return -1; + } + static int loaded = 0; + if (!loaded) + loaded = loadbltsville() ? : 1; /* attempt load once */ + + struct bvbuffdesc *scrdesc; + struct bvsurfgeom *scrgeom; + int noblend; + + if (IS_BVCMD(params)) { + scrdesc = NULL; + scrgeom = params->data.bvc.dstgeom; + noblend = params->data.bvc.noblend; + } else { + scrdesc = params->data.bv.dstdesc; + scrgeom = params->data.bv.dstgeom; + noblend = params->data.bv.noblend; + } + + struct rgz_blt_entry* e; + e = rgz_blts_get(&blts, params); + + struct bvbuffdesc *src1desc = &e->src1desc; + src1desc->structsize = sizeof(struct bvbuffdesc); + src1desc->length = handle->iHeight * HANDLE_TO_STRIDE(handle); + /* + * The virtaddr isn't going to be used in the final 2D h/w integration + * because we will be handling buffers differently + */ + src1desc->auxptr = buff_idx == -1 ? HANDLE_TO_BUFFER(handle) : (void*)buff_idx; /* FIXME: revisit this later */ + + struct bvsurfgeom *src1geom = &e->src1geom; + src1geom->structsize = sizeof(struct bvsurfgeom); + src1geom->format = hal_to_ocd(handle->iFormat); + src1geom->width = handle->iWidth; + src1geom->height = handle->iHeight; + src1geom->orientation = l->transform & HWC_TRANSFORM_ROT_90 ? 90 : + l->transform & HWC_TRANSFORM_ROT_180 ? 180 : + l->transform & HWC_TRANSFORM_ROT_270 ? 270 : 0; + src1geom->virtstride = HANDLE_TO_STRIDE(handle); + + struct bvsurfgeom *dstgeom = &e->dstgeom; + dstgeom->structsize = sizeof(struct bvsurfgeom); + dstgeom->format = scrgeom->format; + dstgeom->width = scrgeom->width; + dstgeom->height = scrgeom->height; + dstgeom->orientation = 0; /* TODO */ + dstgeom->virtstride = DSTSTRIDE(scrgeom); + + struct bvbltparams *bp = &e->bp; + bp->structsize = sizeof(struct bvbltparams); + bp->dstdesc = scrdesc; + bp->dstgeom = dstgeom; + bp->dstrect.left = l->displayFrame.left; + bp->dstrect.top = l->displayFrame.top; + bp->dstrect.width = WIDTH(l->displayFrame); + bp->dstrect.height = HEIGHT(l->displayFrame); + bp->src1.desc = src1desc; + bp->src1geom = src1geom; + bp->src1rect.left = l->sourceCrop.left; + bp->src1rect.top = l->sourceCrop.top; + bp->src1rect.width = WIDTH(l->sourceCrop); + bp->src1rect.height = HEIGHT(l->sourceCrop); + bp->cliprect.left = bp->cliprect.top = 0; + bp->cliprect.width = scrgeom->width; + bp->cliprect.height = scrgeom->height; + + unsigned long bpflags = BVFLAG_CLIP; + if (!noblend && l->blending == HWC_BLENDING_PREMULT) { + struct bvsurfgeom *src2geom = &e->src2geom; + struct bvbuffdesc *src2desc = &e->src2desc; + *src2geom = *dstgeom; + src2desc->structsize = sizeof(struct bvbuffdesc); + src2desc->auxptr = (void*)HWC_BLT_DESC_FB_FN(0); + bpflags |= BVFLAG_BLEND; + bp->op.blend = BVBLEND_SRC1OVER; + bp->src2.desc = scrdesc; + bp->src2geom = dstgeom; + bp->src2rect.left = l->displayFrame.left; + bp->src2rect.top = l->displayFrame.top; + bp->src2rect.width = WIDTH(l->displayFrame); + bp->src2rect.height = HEIGHT(l->displayFrame); + } else { + bpflags |= BVFLAG_ROP; + bp->op.rop = 0xCCCC; /* SRCCOPY */ + if((src1geom->format == OCDFMT_BGR124) || + (src1geom->format == OCDFMT_RGB124) || + (src1geom->format == OCDFMT_RGB16)) + dstgeom->format = OCDFMT_BGR124; + } + + /* TODO regionizer won't permit transforms yet */ + if (l->transform & HWC_TRANSFORM_FLIP_H) + bpflags |= BVFLAG_HORZ_FLIP_SRC1; + if (l->transform & HWC_TRANSFORM_FLIP_V) + bpflags |= BVFLAG_VERT_FLIP_SRC1; + + bp->flags = bpflags; + + return 0; +} + +/* + * Calculate the left coord of the source on the basis of the location of the + * blit subregion relative to the HWC layer display frame + */ +static int effective_srcleft(hwc_layer_t* l, blit_rect_t *rect) +{ + // Assert rect->left >= l->sourceCrop.left + // Assert rect->left < l->sourceCrop.left + WIDTH(l->sourceCrop) + return l->sourceCrop.left + ((WIDTH(l->sourceCrop) * (rect->left - l->displayFrame.left)) / WIDTH(l->displayFrame)); +} + +static int effective_srctop(hwc_layer_t* l, blit_rect_t *rect) +{ + // Assert rect->top >= l->sourceCrop.top + // Assert rect->top < l->sourceCrop.top + HEIGHT(l->sourceCrop) + return l->sourceCrop.top + ((HEIGHT(l->sourceCrop) * (rect->top - l->displayFrame.top)) / HEIGHT(l->displayFrame)); +} + +/* + * Setup the src2 rectangle and the passed descriptor and + * geometry + */ +static void rgz_src2blend_prep2( + struct rgz_blt_entry* e, unsigned int hwc_transform, blit_rect_t *rect, + struct bvbuffdesc *dstdesc, struct bvsurfgeom *dstgeom, int is_fb_dest) +{ + unsigned long bpflags = BVFLAG_CLIP; + + struct bvbltparams *bp = &e->bp; + bpflags |= BVFLAG_BLEND; // FIXME batch? + bp->op.blend = BVBLEND_SRC1OVER; + bp->src2.desc = dstdesc; + bp->src2geom = dstgeom; + bp->src2rect.left = rect->left; + bp->src2rect.top = rect->top; + bp->src2rect.width = WIDTH(*rect); + bp->src2rect.height = HEIGHT(*rect); + + if (is_fb_dest) { + struct bvsurfgeom *src2geom = &e->src2geom; + struct bvbuffdesc *src2desc = &e->src2desc; + *src2geom = *dstgeom; + src2desc->structsize = sizeof(struct bvbuffdesc); + src2desc->auxptr = (void*)HWC_BLT_DESC_FB_FN(0); + } + + if (hwc_transform & HWC_TRANSFORM_FLIP_H) + bpflags |= BVFLAG_HORZ_FLIP_SRC1; + if (hwc_transform & HWC_TRANSFORM_FLIP_V) + bpflags |= BVFLAG_VERT_FLIP_SRC1; + + bp->flags = bpflags; +} + +static void rgz_src2blend_prep( + struct rgz_blt_entry* e, rgz_layer_t *rgz_layer, blit_rect_t *rect, rgz_out_params_t *params) +{ + hwc_layer_t *l = rgz_layer->hwc_layer; + IMG_native_handle_t *handle = (IMG_native_handle_t *)l->handle; + + struct bvbuffdesc *src2desc = &e->src2desc; + src2desc->structsize = sizeof(struct bvbuffdesc); + src2desc->length = handle->iHeight * HANDLE_TO_STRIDE(handle); + src2desc->auxptr = IS_BVCMD(params)? + (void*)rgz_layer->buffidx : HANDLE_TO_BUFFER(handle); + + struct bvsurfgeom *src2geom = &e->src2geom; + src2geom->structsize = sizeof(struct bvsurfgeom); + src2geom->format = hal_to_ocd(handle->iFormat); + src2geom->width = handle->iWidth; + src2geom->height = handle->iHeight; + src2geom->orientation = l->transform & HWC_TRANSFORM_ROT_90 ? 90 : + l->transform & HWC_TRANSFORM_ROT_180 ? 180 : + l->transform & HWC_TRANSFORM_ROT_270 ? 270 : 0; + src2geom->virtstride = HANDLE_TO_STRIDE(handle); + + /* + * This looks a little odd but what we need to do here is take the + * rectangle which has coordinates in terms of the display dimensions + * and find the offset of the source buffer for the layer + */ + blit_rect_t src2rect = *rect; + src2rect.top = effective_srctop(l, rect); + src2rect.left = effective_srcleft(l, rect); + src2rect.bottom = src2rect.top + HEIGHT(*rect); + src2rect.right = src2rect.left + WIDTH(*rect); + rgz_src2blend_prep2(e, l->transform, &src2rect, src2desc, src2geom, 0); +} + +static void rgz_src1_prep( + struct rgz_blt_entry* e, rgz_layer_t *rgz_layer, + blit_rect_t *rect, + struct bvbuffdesc *scrdesc, struct bvsurfgeom *scrgeom, rgz_out_params_t *params) +{ + hwc_layer_t *l = rgz_layer->hwc_layer; + if (!l) + return; + + IMG_native_handle_t *handle = (IMG_native_handle_t *)l->handle; + + struct bvbuffdesc *src1desc = &e->src1desc; + src1desc->structsize = sizeof(struct bvbuffdesc); + src1desc->length = handle->iHeight * HANDLE_TO_STRIDE(handle); + src1desc->auxptr = IS_BVCMD(params) ? + (void*)rgz_layer->buffidx : HANDLE_TO_BUFFER(handle); + + struct bvsurfgeom *src1geom = &e->src1geom; + src1geom->structsize = sizeof(struct bvsurfgeom); + src1geom->format = hal_to_ocd(handle->iFormat); + src1geom->width = handle->iWidth; + src1geom->height = handle->iHeight; + src1geom->orientation = l->transform & HWC_TRANSFORM_ROT_90 ? 90 : + l->transform & HWC_TRANSFORM_ROT_180 ? 180 : + l->transform & HWC_TRANSFORM_ROT_270 ? 270 : 0; + src1geom->virtstride = HANDLE_TO_STRIDE(handle); + + struct bvsurfgeom *dstgeom = &e->dstgeom; + dstgeom->structsize = sizeof(struct bvsurfgeom); + dstgeom->format = scrgeom->format; + dstgeom->width = scrgeom->width; + dstgeom->height = scrgeom->height; + dstgeom->orientation = 0; + dstgeom->virtstride = DSTSTRIDE(scrgeom); + + struct bvbltparams *bp = &e->bp; + bp->structsize = sizeof(struct bvbltparams); + bp->dstdesc = scrdesc; + bp->dstgeom = dstgeom; + bp->dstrect.left = rect->left; + bp->dstrect.top = rect->top; + bp->dstrect.width = WIDTH(*rect); + bp->dstrect.height = HEIGHT(*rect); + bp->src1.desc = src1desc; + bp->src1geom = src1geom; + bp->src1rect.left = effective_srcleft(l, rect); + bp->src1rect.top = effective_srctop(l, rect); + bp->src1rect.width = WIDTH(*rect); // XXX fixme - effective width/height? + bp->src1rect.height = HEIGHT(*rect); + bp->cliprect.left = bp->cliprect.top = 0; + bp->cliprect.width = scrgeom->width; + bp->cliprect.height = scrgeom->height; +} + +static void rgz_batch_entry(struct rgz_blt_entry* e, unsigned int flag, unsigned int set) +{ + e->bp.flags &= ~BVFLAG_BATCH_MASK; + e->bp.flags |= flag; + e->bp.batchflags |= set; +} + +static int rgz_hwc_subregion_blit(blit_hregion_t *hregion, int sidx, rgz_out_params_t *params, int screen_isdirty) +{ + static int loaded = 0; + if (!loaded) + loaded = loadbltsville() ? : 1; /* attempt load once */ + + struct bvbuffdesc *scrdesc; + struct bvsurfgeom *scrgeom; + int noblend; + + if (IS_BVCMD(params)) { + scrdesc = NULL; + scrgeom = params->data.bvc.dstgeom; + noblend = params->data.bvc.noblend; + } else { + scrdesc = params->data.bv.dstdesc; + scrgeom = params->data.bv.dstgeom; + noblend = params->data.bv.noblend; + } + + int lix; + int ldepth = get_layer_ops(hregion, sidx, &lix); + if (ldepth == 0) { + /* Impossible, there are no layers in this region even if the + * background is covering the whole screen + */ + OUTE("hregion %p subregion %d doesn't have any ops", hregion, sidx); + return -1; + } + + /* Check if the bottom layer is the background */ + if (hregion->rgz_layers[lix]->hwc_layer == &bg_layer) { + if (ldepth == 1) { + /* Background layer is the only operation, clear subregion */ + if (screen_isdirty) + rgz_out_clrdst(params, &hregion->blitrects[lix][sidx]); + return 0; + } else { + /* No need to generate blits with background layer if there is + * another layer on top of it, discard it + */ + ldepth--; + lix = get_layer_ops_next(hregion, sidx, lix); + } + } + + /* Determine if this region is dirty */ + int dirty = 0, dirtylix = lix; + while (dirtylix != -1) { + rgz_layer_t *rgz_layer = hregion->rgz_layers[dirtylix]; + if (rgz_layer->dirty_count){ + /* One of the layers is dirty, we need to generate blits for this subregion */ + dirty = 1; + break; + } + dirtylix = get_layer_ops_next(hregion, sidx, dirtylix); + } + + if (!dirty) + return 0; + + if (!noblend && ldepth > 1) { /* BLEND */ + blit_rect_t *rect = &hregion->blitrects[lix][sidx]; + struct rgz_blt_entry* e = rgz_blts_get(&blts, params); + + int s2lix = lix; + lix = get_layer_ops_next(hregion, sidx, lix); + + /* + * We save a read and a write from the FB if we blend the bottom + * two layers + */ + rgz_src1_prep(e, hregion->rgz_layers[lix], rect, scrdesc, scrgeom, params); + rgz_src2blend_prep(e, hregion->rgz_layers[s2lix], rect, params); + rgz_batch_entry(e, BVFLAG_BATCH_BEGIN, 0); + + /* Rest of layers blended with FB */ + int first = 1; + while((lix = get_layer_ops_next(hregion, sidx, lix)) != -1) { + int batchflags = 0; + e = rgz_blts_get(&blts, params); + + rgz_layer_t *rgz_layer = hregion->rgz_layers[lix]; + hwc_layer_t *layer = rgz_layer->hwc_layer; + rgz_src1_prep(e, rgz_layer, rect, scrdesc, scrgeom, params); + rgz_src2blend_prep2(e, layer->transform, rect, scrdesc, scrgeom, 1); + + if (first) { + first = 0; + batchflags |= BVBATCH_DST | BVBATCH_SRC2 | \ + BVBATCH_SRC2RECT_ORIGIN | BVBATCH_SRC2RECT_SIZE; + } + batchflags |= BVBATCH_SRC1; + if (rgz_hwc_scaled(layer)) + batchflags |= BVBATCH_SRC1RECT_ORIGIN | BVBATCH_SRC1RECT_SIZE; + rgz_batch_entry(e, BVFLAG_BATCH_CONTINUE, batchflags); + } + + if (e->bp.flags & BVFLAG_BATCH_BEGIN) + rgz_batch_entry(e, 0, 0); + else + rgz_batch_entry(e, BVFLAG_BATCH_END, 0); + + } else { /* COPY */ + blit_rect_t *rect = &hregion->blitrects[lix][sidx]; + if (noblend) /* get_layer_ops() doesn't understand this so get the top */ + lix = get_top_rect(hregion, sidx, &rect); + + struct rgz_blt_entry* e = rgz_blts_get(&blts, params); + + rgz_layer_t *rgz_layer = hregion->rgz_layers[lix]; + hwc_layer_t *l = rgz_layer->hwc_layer; + rgz_src1_prep(e, rgz_layer, rect, scrdesc, scrgeom, params); + + struct bvsurfgeom *src1geom = &e->src1geom; + unsigned long bpflags = BVFLAG_CLIP | BVFLAG_ROP; + e->bp.op.rop = 0xCCCC; /* SRCCOPY */ + if((src1geom->format == OCDFMT_BGR124) || + (src1geom->format == OCDFMT_RGB124) || + (src1geom->format == OCDFMT_RGB16)) + e->dstgeom.format = OCDFMT_BGR124; + + if (l->transform & HWC_TRANSFORM_FLIP_H) + bpflags |= BVFLAG_HORZ_FLIP_SRC1; + if (l->transform & HWC_TRANSFORM_FLIP_V) + bpflags |= BVFLAG_VERT_FLIP_SRC1; + e->bp.flags = bpflags; + } + return 0; +} + +struct bvbuffdesc gscrndesc = { + .structsize = sizeof(struct bvbuffdesc), .length = 0, + .auxptr = MAP_FAILED +}; +struct bvsurfgeom gscrngeom = { + .structsize = sizeof(struct bvsurfgeom), .format = OCDFMT_UNKNOWN +}; + +static void rgz_blts_init(struct rgz_blts *blts) +{ + bzero(blts, sizeof(*blts)); +} + +static void rgz_blts_free(struct rgz_blts *blts) +{ + /* TODO ??? maybe we should dynamically allocate this */ + rgz_blts_init(blts); +} + +static struct rgz_blt_entry* rgz_blts_get(struct rgz_blts *blts, rgz_out_params_t *params) +{ + struct rgz_blt_entry *ne; + if (blts->idx < RGZ_MAX_BLITS) { + ne = &blts->bvcmds[blts->idx++]; + if (IS_BVCMD(params)) + params->data.bvc.out_blits++; + } else { + OUTE("!!! BIG PROBLEM !!! run out of blit entries"); + ne = &blts->bvcmds[blts->idx - 1]; /* Return last slot */ + } + return ne; +} + +static int rgz_blts_bvdirect(rgz_t *rgz, struct rgz_blts *blts, rgz_out_params_t *params) +{ + struct bvbatch *batch = NULL; + int rv = -1; + int idx = 0; + + while (idx < blts->idx) { + struct rgz_blt_entry *e = &blts->bvcmds[idx]; + if (e->bp.flags & BVFLAG_BATCH_MASK) + e->bp.batch = batch; + rv = bv_blt(&e->bp); + if (rv) { + OUTE("BV_BLT failed: %d", rv); + BVDUMP("bv_blt:", " ", &e->bp); + return -1; + } + if (e->bp.flags & BVFLAG_BATCH_BEGIN) + batch = e->bp.batch; + idx++; + } + return rv; +} + +static int rgz_out_region(rgz_t *rgz, rgz_out_params_t *params) +{ + if (!(rgz->state & RGZ_REGION_DATA)) { + OUTE("rgz_out_region invoked with bad state"); + return -1; + } + + rgz_blts_init(&blts); + ALOGD_IF(debug, "rgz_out_region:"); + + if (IS_BVCMD(params)) + params->data.bvc.out_blits = 0; + + int i; + for (i = 0; i < rgz->nhregions; i++) { + blit_hregion_t *hregion = &rgz->hregions[i]; + int s; + ALOGD_IF(debug, "h[%d] nsubregions = %d", i, hregion->nsubregions); + if (hregion->nlayers == 0) { + /* Impossible, there are no layers in this region even if the + * background is covering the whole screen + */ + OUTE("hregion %p doesn't have any ops", hregion); + return -1; + } + for (s = 0; s < hregion->nsubregions; s++) { + ALOGD_IF(debug, "h[%d] -> [%d]", i, s); + rgz_hwc_subregion_blit(hregion, s, params, rgz->screen_isdirty); + } + } + + int rv = 0; + + if (IS_BVCMD(params)) { + unsigned int j; + params->data.bvc.out_nhndls = 0; + /* Begin from index 1 to remove the background layer from the output */ + for (j = 1; j < rgz->rgz_layerno; j++) { + hwc_layer_t *layer = rgz->rgz_layers[j].hwc_layer; + params->data.bvc.out_hndls[j-1] = layer->handle; + params->data.bvc.out_nhndls++; + } + + /* FIXME: we want to be able to call rgz_blts_free and populate the actual + * composition data structure ourselves */ + params->data.bvc.cmdp = blts.bvcmds; + params->data.bvc.cmdlen = blts.idx; + if (params->data.bvc.out_blits >= RGZ_MAX_BLITS) + rv = -1; + //rgz_blts_free(&blts); + } else { + rv = rgz_blts_bvdirect(rgz, &blts, params); + rgz_blts_free(&blts); + } + + return rv; +} + +void rgz_profile_hwc(hwc_layer_list_t* list, int dispw, int disph) +{ + if (!list) /* A NULL composition list can occur */ + return; + +#ifndef RGZ_TEST_INTEGRATION + static char regiondump2[PROPERTY_VALUE_MAX] = ""; + char regiondump[PROPERTY_VALUE_MAX]; + property_get("debug.2dhwc.region", regiondump, "0"); + int dumpregions = strncmp(regiondump, regiondump2, PROPERTY_VALUE_MAX); + if (dumpregions) + strncpy(regiondump2, regiondump, PROPERTY_VALUE_MAX); + else { + dumpregions = !strncmp(regiondump, "all", PROPERTY_VALUE_MAX) && + (list->flags & HWC_GEOMETRY_CHANGED); + static int iteration = 0; + if (dumpregions) + sprintf(regiondump, "iteration %d", iteration++); + } + + char dumplayerdata[PROPERTY_VALUE_MAX]; + /* 0 - off, 1 - human readable, 2 - CSV */ + property_get("debug.2dhwc.dumplayers", dumplayerdata, "0"); + int dumplayers = atoi(dumplayerdata); +#else + char regiondump[] = ""; + int dumplayers = 1; + int dumpregions = 0; +#endif + if (dumplayers && (list->flags & HWC_GEOMETRY_CHANGED)) { + OUTP("<!-- BEGUN-LAYER-DUMP: %d -->", list->numHwLayers); + rgz_print_layers(list, dumplayers == 1 ? 0 : 1); + OUTP("<!-- ENDED-LAYER-DUMP -->"); + } + + if(!dumpregions) + return; + + rgz_t rgz; + rgz_in_params_t ip = { .data = { .hwc = { + .layers = list->hwLayers, + .layerno = list->numHwLayers } } }; + ip.op = RGZ_IN_HWCCHK; + if (rgz_in(&ip, &rgz) == RGZ_ALL) { + ip.op = RGZ_IN_HWC; + if (rgz_in(&ip, &rgz) == RGZ_ALL) { + OUTP("<!-- BEGUN-SVG-DUMP: %s -->", regiondump); + OUTP("<b>%s</b>", regiondump); + rgz_out_params_t op = { + .op = RGZ_OUT_SVG, + .data = { + .svg = { + .dispw = dispw, .disph = disph, + .htmlw = 450, .htmlh = 800 + } + }, + }; + rgz_out(&rgz, &op); + OUTP("<!-- ENDED-SVG-DUMP -->"); + } + } + rgz_release(&rgz); +} + +int rgz_get_screengeometry(int fd, struct bvsurfgeom *geom, int fmt) +{ + /* Populate Bltsville destination buffer information with framebuffer data */ + struct fb_fix_screeninfo fb_fixinfo; + struct fb_var_screeninfo fb_varinfo; + + ALOGI("Attempting to get framebuffer device info."); + if(ioctl(fd, FBIOGET_FSCREENINFO, &fb_fixinfo)) { + OUTE("Error getting fb_fixinfo"); + return -EINVAL; + } + + if(ioctl(fd, FBIOGET_VSCREENINFO, &fb_varinfo)) { + ALOGE("Error gettting fb_varinfo"); + return -EINVAL; + } + + bg_layer.displayFrame.left = bg_layer.displayFrame.top = 0; + bg_layer.displayFrame.right = fb_varinfo.xres; + bg_layer.displayFrame.bottom = fb_varinfo.yres; + + bzero(geom, sizeof(*geom)); + geom->structsize = sizeof(*geom); + geom->width = fb_varinfo.xres; + geom->height = fb_varinfo.yres; + geom->virtstride = fb_fixinfo.line_length; + geom->format = hal_to_ocd(fmt); + geom->orientation = 0; + return 0; +} + +int rgz_in(rgz_in_params_t *p, rgz_t *rgz) +{ + int rv = -1; + switch (p->op) { + case RGZ_IN_HWC: + rv = rgz_in_hwccheck(p, rgz); + if (rv == RGZ_ALL) + rv = rgz_in_hwc(p, rgz) ? 0 : RGZ_ALL; + break; + case RGZ_IN_HWCCHK: + bzero(rgz, sizeof(rgz_t)); + rv = rgz_in_hwccheck(p, rgz); + break; + default: + return -1; + } + return rv; +} + +void rgz_release(rgz_t *rgz) +{ + if (!rgz) + return; + if (rgz->hregions) + free(rgz->hregions); + bzero(rgz, sizeof(*rgz)); +} + +int rgz_out(rgz_t *rgz, rgz_out_params_t *params) +{ + switch (params->op) { + case RGZ_OUT_SVG: + rgz_out_svg(rgz, params); + return 0; + case RGZ_OUT_BVDIRECT_PAINT: + return rgz_out_bvdirect_paint(rgz, params); + case RGZ_OUT_BVCMD_PAINT: + return rgz_out_bvcmd_paint(rgz, params); + case RGZ_OUT_BVDIRECT_REGION: + case RGZ_OUT_BVCMD_REGION: + return rgz_out_region(rgz, params); + default: + return -1; + } +} + diff --git a/hwc/rgz_2d.h b/hwc/rgz_2d.h new file mode 100644 index 0000000..4b4d3fb --- /dev/null +++ b/hwc/rgz_2d.h @@ -0,0 +1,291 @@ +/* + * Copyright (C) Texas Instruments - http://www.ti.com/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __RGZ_2D__ +#define __RGZ_2D__ + +#include <linux/bltsville.h> + +/* + * Maximum number of layers used to generate subregion rectangles in a + * horizontal region. + */ +#define RGZ_MAXLAYERS 13 + +/* + * Maximum number of layers the regionizer will accept as input. Account for an + * additional 'background layer' to generate empty subregion rectangles. + */ +#define RGZ_INPUT_MAXLAYERS (RGZ_MAXLAYERS - 1) + +/* + * Regionizer data + * + * This is an oqaque structure passed in by the client + */ +struct rgz; +typedef struct rgz rgz_t; + +/* + * With an open framebuffer file descriptor get the geometry of + * the device + */ +int rgz_get_screengeometry(int fd, struct bvsurfgeom *geom, int fmt); + +/* + * Regionizer input parameters + */ +struct rgz_in_hwc { + int flags; + int layerno; + hwc_layer_t *layers; + struct bvsurfgeom *dstgeom; +}; + +typedef struct rgz_in_params { + int op; /* See RGZ_IN_* */ + union { + struct rgz_in_hwc hwc; + } data; +} rgz_in_params_t; + +/* + * Validate whether the HWC layers can be rendered + * + * Arguments (rgz_in_params_t): + * op RGZ_IN_HWCCHK + * data.hwc.layers HWC layer array + * data.hwc.layerno HWC layer array size + * + * Returns: + * rv = RGZ_ALL, -1 failure + */ +#define RGZ_IN_HWCCHK 1 + +/* + * Regionize the HWC layers + * + * This generates region data which can be used with regionizer + * output function. This call will validate whether all or some of the + * layers can be rendered. + * + * The caller must use rgz_release when done with the region data + * + * Arguments (rgz_in_params_t): + * op RGZ_IN_HWC + * data.hwc.layers HWC layer array + * data.hwc.layerno HWC layer array size + * + * Returns: + * rv = RGZ_ALL, -1 failure + */ +#define RGZ_IN_HWC 2 + +int rgz_in(rgz_in_params_t *param, rgz_t *rgz); + +/* This means all layers can be blitted */ +#define RGZ_ALL 1 + +/* + * Free regionizer resources + */ +void rgz_release(rgz_t *rgz); + +/* + * Regionizer output operations + */ +struct rgz_out_bvcmd { + void *cmdp; + int cmdlen; + struct bvsurfgeom *dstgeom; + int noblend; + buffer_handle_t out_hndls[RGZ_INPUT_MAXLAYERS]; /* OUTPUT */ + int out_nhndls; /* OUTPUT */ + int out_blits; /* OUTPUT */ +}; + +struct rgz_out_svg { + int dispw; + int disph; + int htmlw; + int htmlh; +}; + +struct rgz_out_bvdirect { + struct bvbuffdesc *dstdesc; + struct bvsurfgeom *dstgeom; + int noblend; +}; + +typedef struct rgz_out_params { + int op; /* See RGZ_OUT_* */ + union { + struct rgz_out_bvcmd bvc; + struct rgz_out_bvdirect bv; + struct rgz_out_svg svg; + } data; +} rgz_out_params_t; + +/* + * Regionizer output commands + */ + +/* + * Output SVG from regionizer + * + * rgz_out_params_t: + * + * op RGZ_OUT_SVG + * data.svg.dispw + * data.svg.disph Display width and height these values will be the + * viewport dimensions i.e. the logical coordinate space + * rather than the physical size + * data.svg.htmlw + * data.svg.htmlh HTML output dimensions + */ +#define RGZ_OUT_SVG 0 + +/* + * This commands generates bltsville command data structures for HWC which will + * paint layer by layer + * + * rgz_out_params_t: + * + * op RGZ_OUT_BVCMD_PAINT + * data.bvc.cmdp Pointer to buffer with cmd data + * data.bvc.cmdlen length of cmdp + * data.bvc.dstgeom bltsville struct describing the destination geometry + * data.bvc.noblend Test option to disable blending + * data.bvc.out_hndls Array of buffer handles (OUTPUT) + * data.bvc.out_nhndls Number of buffer handles (OUTPUT) + * data.bvc.out_blits Number of blits (OUTPUT) + */ +#define RGZ_OUT_BVCMD_PAINT 1 + +/* + * This commands generates bltsville command data structures for HWC which will + * render via regions. This will involve a complete redraw of the screen. + * + * See RGZ_OUT_BVCMD_PAINT + */ +#define RGZ_OUT_BVCMD_REGION 2 + +/* + * Perform actual blits painting each layer from back to front - this is a test + * command + * + * rgz_out_params_t: + * + * op RGZ_OUT_BVDIRECT_PAINT + * data.bv.dstdesc bltsville struct describing the destination buffer + * data.bv.dstgeom bltsville struct describing the destination geometry + * data.bv.list List of HWC layers to blit, only HWC_OVERLAY layers + * will be rendered + * data.bv.noblend Test option to disable blending + */ +#define RGZ_OUT_BVDIRECT_PAINT 3 +/* + * Perform actual blits where each blit is a subregion - this is a test mode + */ +#define RGZ_OUT_BVDIRECT_REGION 5 + +int rgz_out(rgz_t *rgz, rgz_out_params_t* params); + +/* + * Produce instrumented logging of layer data + */ +void rgz_profile_hwc(hwc_layer_list_t* list, int dispw, int disph); + +/* + * ---------------------------------- + * IMPLEMENTATION DETAILS FOLLOW HERE + * ---------------------------------- + */ + +/* + * Regionizer blit data structures + */ +typedef struct blit_rect { + int left, top, right, bottom; +} blit_rect_t; + +/* + * A hregion is a horizontal area generated from the intersection of layers + * for a given composition. + * + * ---------------------------------------- + * | layer 0 | + * | xxxxxxxxxxxxxxxxxx | + * | x layer 1 x | + * | x x | + * | x xxxxxxxxxxxxxxxxxxx + * | x x layer 2 x + * | x x x + * | xxxxxxxxxx x + * | x x + * | x x + * ---------------------xxxxxxxxxxxxxxxxxxx + * + * This can be broken up into a number of horizontal regions: + * + * ---------------------------------------- + * | H1 l0 | + * |-----------xxxxxxxxxxxxxxxxxx---------| + * | H2 x x | + * | l0 x l01 x l0 | + * |-----------x--------xxxxxxxxxxxxxxxxxxx + * | H3 x x x x + * | l0 x l01 x l012 x l02 x + * |-----------xxxxxxxxxxxxxxxxxx---------x + * | H4 x x + * | l0 x l02 x + * ---------------------xxxxxxxxxxxxxxxxxxx + * + * Each hregion is just an array of rectangles. By accounting for the layers + * at different z-order, and hregion becomes a multi-dimensional array e.g. in + * the diagram above H4 has 2 sub-regions, layer 0 intersects with the first + * region and layers 0 and 2 intersect with the second region. + */ +#define RGZ_SUBREGIONMAX ((RGZ_MAXLAYERS << 1) - 1) +#define RGZ_MAX_BLITS (RGZ_SUBREGIONMAX * RGZ_SUBREGIONMAX) + +typedef struct rgz_layer { + hwc_layer_t *hwc_layer; + int buffidx; + int dirty_count; + void* dirty_hndl; +} rgz_layer_t; + +typedef struct blit_hregion { + blit_rect_t rect; + rgz_layer_t *rgz_layers[RGZ_MAXLAYERS]; + int nlayers; + int nsubregions; + blit_rect_t blitrects[RGZ_MAXLAYERS][RGZ_SUBREGIONMAX]; /* z-order | rectangle */ +} blit_hregion_t; + +enum { RGZ_STATE_INIT = 1, RGZ_REGION_DATA = 2} ; + +struct rgz { + /* All fields here are opaque to the caller */ + blit_hregion_t *hregions; + int nhregions; + int state; + unsigned int rgz_layerno; + rgz_layer_t rgz_layers[RGZ_MAXLAYERS]; + int screen_isdirty; +}; + +#endif /* __RGZ_2D__ */ |