summaryrefslogtreecommitdiffstats
path: root/hwc
diff options
context:
space:
mode:
authorcodeworkx <codeworkx@cyanogenmod.org>2013-02-11 17:29:55 +0000
committercodeworkx <codeworkx@cyanogenmod.org>2013-02-13 18:55:29 +0000
commit222b794ae146b4e0d61958c55a518396e293e6d7 (patch)
tree744f1fc5d63e8669de1018dc7c42c4e863ceae85 /hwc
downloaddevice_samsung_omap4-common-222b794ae146b4e0d61958c55a518396e293e6d7.zip
device_samsung_omap4-common-222b794ae146b4e0d61958c55a518396e293e6d7.tar.gz
device_samsung_omap4-common-222b794ae146b4e0d61958c55a518396e293e6d7.tar.bz2
initial commit
sources from http://omapzoom.org
Diffstat (limited to 'hwc')
-rw-r--r--hwc/Android.mk31
-rw-r--r--hwc/dock_image.c196
-rw-r--r--hwc/dock_image.h38
-rw-r--r--hwc/hal_public.h188
-rw-r--r--hwc/hwc.c2747
-rw-r--r--hwc/hwc_dev.h180
-rw-r--r--hwc/rgz_2d.c1997
-rw-r--r--hwc/rgz_2d.h306
-rw-r--r--hwc/sw_vsync.c145
-rw-r--r--hwc/sw_vsync.h25
10 files changed, 5853 insertions, 0 deletions
diff --git a/hwc/Android.mk b/hwc/Android.mk
new file mode 100644
index 0000000..dc0c713
--- /dev/null
+++ b/hwc/Android.mk
@@ -0,0 +1,31 @@
+LOCAL_PATH := $(call my-dir)
+
+# HAL module implementation, not prelinked and stored in
+# hw/<HWCOMPOSE_HARDWARE_MODULE_ID>.<ro.product.board>.so
+include $(CLEAR_VARS)
+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 \
+ libion_ti
+LOCAL_SRC_FILES := hwc.c rgz_2d.c dock_image.c sw_vsync.c
+LOCAL_STATIC_LIBRARIES := libpng
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_MODULE := hwcomposer.$(TARGET_BOOTLOADER_BOARD_NAME)
+LOCAL_CFLAGS := -DLOG_TAG=\"ti_hwc\"
+LOCAL_C_INCLUDES += external/libpng external/zlib
+
+LOCAL_C_INCLUDES += \
+ $(LOCAL_PATH)/../edid/inc \
+ $(LOCAL_PATH)/../include
+LOCAL_SHARED_LIBRARIES += libedid
+
+ifeq ($(BOARD_USE_SYSFS_VSYNC_NOTIFICATION),true)
+LOCAL_CFLAGS += -DSYSFS_VSYNC_NOTIFICATION
+endif
+
+# LOG_NDEBUG=0 means verbose logging enabled
+# LOCAL_CFLAGS += -DLOG_NDEBUG=0
+include $(BUILD_SHARED_LIBRARY)
diff --git a/hwc/dock_image.c b/hwc/dock_image.c
new file mode 100644
index 0000000..b74a17e
--- /dev/null
+++ b/hwc/dock_image.c
@@ -0,0 +1,196 @@
+/*
+ * 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 <errno.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+
+#include <cutils/log.h>
+#include <cutils/properties.h>
+#include <png.h>
+
+#include <linux/fb.h>
+
+#include "hwc_dev.h"
+#include "dock_image.h"
+
+static struct dock_image_state {
+ void *buffer; /* start of fb for hdmi */
+ uint32_t buffer_size; /* size of fb for hdmi */
+
+ uint32_t max_width;
+ uint32_t max_height;
+
+ image_info_t image;
+} dock_image;
+
+static void free_png_image(image_info_t *img)
+{
+ memset(img, 0, sizeof(*img));
+}
+
+static int load_png_image(char *path, image_info_t *img)
+{
+ void *ptr = NULL;
+ png_bytepp row_pointers = NULL;
+
+ FILE *fd = fopen(path, "rb");
+ if (!fd) {
+ ALOGE("failed to open PNG file %s: (%d)", path, errno);
+ return -EINVAL;
+ }
+
+ const int SIZE_PNG_HEADER = 8;
+ uint8_t header[SIZE_PNG_HEADER];
+ fread(header, 1, SIZE_PNG_HEADER, fd);
+ if (png_sig_cmp(header, 0, SIZE_PNG_HEADER)) {
+ ALOGE("%s is not a PNG file", path);
+ goto fail;
+ }
+
+ png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (!png_ptr)
+ goto fail_alloc;
+ png_infop info_ptr = png_create_info_struct(png_ptr);
+ if (!info_ptr)
+ goto fail_alloc;
+
+ if (setjmp(png_jmpbuf(png_ptr)))
+ goto fail_alloc;
+
+ png_init_io(png_ptr, fd);
+ png_set_sig_bytes(png_ptr, SIZE_PNG_HEADER);
+ png_set_user_limits(png_ptr, dock_image.max_width, dock_image.max_height);
+ png_read_info(png_ptr, info_ptr);
+
+ uint8_t bit_depth = png_get_bit_depth(png_ptr, info_ptr);
+ uint32_t width = png_get_image_width(png_ptr, info_ptr);
+ uint32_t height = png_get_image_height(png_ptr, info_ptr);
+ uint8_t color_type = png_get_color_type(png_ptr, info_ptr);
+
+ switch (color_type) {
+ case PNG_COLOR_TYPE_PALETTE:
+ png_set_palette_to_rgb(png_ptr);
+ png_set_filler(png_ptr, 128, PNG_FILLER_AFTER);
+ break;
+ case PNG_COLOR_TYPE_GRAY:
+ if (bit_depth < 8) {
+ png_set_expand_gray_1_2_4_to_8(png_ptr);
+ if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
+ png_set_tRNS_to_alpha(png_ptr);
+ } else {
+ png_set_filler(png_ptr, 128, PNG_FILLER_AFTER);
+ }
+ /* fall through */
+ case PNG_COLOR_TYPE_GRAY_ALPHA:
+ png_set_gray_to_rgb(png_ptr);
+ break;
+ case PNG_COLOR_TYPE_RGB:
+ png_set_filler(png_ptr, 128, PNG_FILLER_AFTER);
+ /* fall through */
+ case PNG_COLOR_TYPE_RGB_ALPHA:
+ png_set_bgr(png_ptr);
+ break;
+ default:
+ ALOGE("unsupported PNG color: %x", color_type);
+ goto fail_alloc;
+ }
+
+ if (bit_depth == 16)
+ png_set_strip_16(png_ptr);
+
+ const uint32_t bpp = 4;
+ img->size = ALIGN(width * height * bpp, 4096);
+ if ((uint32_t)img->size > dock_image.buffer_size) {
+ ALOGE("image does not fit into framebuffer area (%d > %d)", img->size, dock_image.buffer_size);
+ goto fail_alloc;
+ }
+ img->ptr = dock_image.buffer;
+
+ row_pointers = calloc(height, sizeof(*row_pointers));
+ if (!row_pointers) {
+ ALOGE("failed to allocate row pointers");
+ goto fail_alloc;
+ }
+ uint32_t i;
+ for (i = 0; i < height; i++)
+ row_pointers[i] = img->ptr + i * width * bpp;
+ png_set_rows(png_ptr, info_ptr, row_pointers);
+ png_read_update_info(png_ptr, info_ptr);
+ img->rowbytes = png_get_rowbytes(png_ptr, info_ptr);
+
+ png_read_image(png_ptr, row_pointers);
+ png_read_end(png_ptr, NULL);
+ free(row_pointers);
+ png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
+ fclose(fd);
+ img->width = width;
+ img->height = height;
+ return 0;
+
+fail_alloc:
+ free_png_image(img);
+ free(row_pointers);
+ if (!png_ptr || !info_ptr)
+ ALOGE("failed to allocate PNG structures");
+ png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
+fail:
+ fclose(fd);
+ return -EINVAL;
+}
+
+int init_dock_image(omap_hwc_device_t *hwc_dev, uint32_t max_width, uint32_t max_height)
+{
+ int err = 0;
+
+ struct fb_fix_screeninfo fix;
+ if (ioctl(hwc_dev->fb_fd, FBIOGET_FSCREENINFO, &fix)) {
+ ALOGE("failed to get fb info (%d)", errno);
+ err = -errno;
+ goto done;
+ }
+
+ dock_image.buffer_size = fix.smem_len;
+ dock_image.buffer = mmap(NULL, fix.smem_len, PROT_WRITE, MAP_SHARED, hwc_dev->fb_fd, 0);
+ if (dock_image.buffer == MAP_FAILED) {
+ ALOGE("failed to map fb memory");
+ err = -errno;
+ goto done;
+ }
+
+ dock_image.max_width = max_width;
+ dock_image.max_height = max_height;
+
+ done:
+ return err;
+}
+
+void load_dock_image()
+{
+ if (!dock_image.image.rowbytes) {
+ char value[PROPERTY_VALUE_MAX];
+ property_get("persist.hwc.dock_image", value, "/vendor/res/images/dock/dock.png");
+ load_png_image(value, &dock_image.image);
+ }
+}
+
+image_info_t *get_dock_image()
+{
+ return &dock_image.image;
+}
+
diff --git a/hwc/dock_image.h b/hwc/dock_image.h
new file mode 100644
index 0000000..44a3271
--- /dev/null
+++ b/hwc/dock_image.h
@@ -0,0 +1,38 @@
+/*
+ * 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 __DOCK_IMAGE__
+#define __DOCK_IMAGE__
+
+#include <stdint.h>
+
+/* ARGB image */
+struct image_info {
+ int width;
+ int height;
+ int rowbytes;
+ int size;
+ uint8_t *ptr;
+};
+typedef struct image_info image_info_t;
+
+typedef struct omap_hwc_device omap_hwc_device_t;
+
+int init_dock_image(omap_hwc_device_t *hwc_dev, uint32_t max_width, uint32_t max_height);
+void load_dock_image();
+image_info_t *get_dock_image();
+
+#endif
diff --git a/hwc/hal_public.h b/hwc/hal_public.h
new file mode 100644
index 0000000..a7dfb08
--- /dev/null
+++ b/hwc/hal_public.h
@@ -0,0 +1,188 @@
+/* Copyright (c) Imagination Technologies Ltd.
+ *
+ * The contents of this file are subject to the MIT license as set out below.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef HAL_PUBLIC_H
+#define HAL_PUBLIC_H
+
+/* Authors of third party hardware composer (HWC) modules will need to include
+ * this header to access functionality in the gralloc and framebuffer HALs.
+ */
+
+#include <hardware/gralloc.h>
+
+#define ALIGN(x,a) (((x) + (a) - 1L) & ~((a) - 1L))
+#define HW_ALIGN 32
+
+/* This can be tuned down as appropriate for the SOC.
+ *
+ * IMG formats are usually a single sub-alloc.
+ * Some OEM video formats are two sub-allocs (Y, UV planes).
+ * Future OEM video formats might be three sub-allocs (Y, U, V planes).
+ */
+#define MAX_SUB_ALLOCS 3
+
+typedef struct
+{
+ native_handle_t base;
+
+ /* These fields can be sent cross process. They are also valid
+ * to duplicate within the same process.
+ *
+ * A table is stored within psPrivateData on gralloc_module_t (this
+ * is obviously per-process) which maps stamps to a mapped
+ * PVRSRV_CLIENT_MEM_INFO in that process. Each map entry has a lock
+ * count associated with it, satisfying the requirements of the
+ * Android API. This also prevents us from leaking maps/allocations.
+ *
+ * This table has entries inserted either by alloc()
+ * (alloc_device_t) or map() (gralloc_module_t). Entries are removed
+ * by free() (alloc_device_t) and unmap() (gralloc_module_t).
+ *
+ * As a special case for framebuffer_device_t, framebuffer_open()
+ * will add and framebuffer_close() will remove from this table.
+ */
+
+#define IMG_NATIVE_HANDLE_NUMFDS MAX_SUB_ALLOCS
+ /* The `fd' field is used to "export" a meminfo to another process.
+ * Therefore, it is allocated by alloc_device_t, and consumed by
+ * gralloc_module_t. The framebuffer_device_t does not need a handle,
+ * and the special value IMG_FRAMEBUFFER_FD is used instead.
+ */
+ int fd[MAX_SUB_ALLOCS];
+
+#define IMG_NATIVE_HANDLE_NUMINTS ((sizeof(unsigned long long) / sizeof(int)) + 5)
+ /* A KERNEL unique identifier for any exported kernel meminfo. Each
+ * exported kernel meminfo will have a unique stamp, but note that in
+ * userspace, several meminfos across multiple processes could have
+ * the same stamp. As the native_handle can be dup(2)'d, there could be
+ * multiple handles with the same stamp but different file descriptors.
+ */
+ unsigned long long ui64Stamp;
+
+ /* This is used for buffer usage validation when locking a buffer,
+ * and also in WSEGL (for the composition bypass feature).
+ */
+ int usage;
+
+ /* In order to do efficient cache flushes we need the buffer dimensions
+ * and format. These are available on the ANativeWindowBuffer,
+ * but the platform doesn't pass them down to the graphics HAL.
+ *
+ * These fields are also used in the composition bypass. In this
+ * capacity, these are the "real" values for the backing allocation.
+ */
+ int iWidth;
+ int iHeight;
+ int iFormat;
+ unsigned int uiBpp;
+}
+__attribute__((aligned(sizeof(int)),packed)) IMG_native_handle_t;
+
+typedef struct
+{
+ framebuffer_device_t base;
+
+ /* The HWC was loaded. post() is no longer responsible for presents */
+ int bBypassPost;
+
+ /* HWC path for present posts */
+ int (*Post2)(framebuffer_device_t *fb, buffer_handle_t *buffers,
+ int num_buffers, void *data, int data_length);
+}
+IMG_framebuffer_device_public_t;
+
+typedef struct IMG_gralloc_module_public_t
+{
+ gralloc_module_t base;
+
+ /* If the framebuffer has been opened, this will point to the
+ * framebuffer device data required by the allocator, WSEGL
+ * modules and composerhal.
+ */
+ IMG_framebuffer_device_public_t *psFrameBufferDevice;
+
+ int (*GetPhyAddrs)(struct IMG_gralloc_module_public_t const* module,
+ buffer_handle_t handle,
+ unsigned int auiPhyAddr[MAX_SUB_ALLOCS]);
+
+ /* Custom-blit components in lieu of overlay hardware */
+ int (*Blit)(struct IMG_gralloc_module_public_t const *module,
+ buffer_handle_t src,
+ void *dest[MAX_SUB_ALLOCS], int format);
+
+ int (*Blit2)(struct IMG_gralloc_module_public_t const *module,
+ buffer_handle_t src, buffer_handle_t dest,
+ int w, int h, int x, int y);
+}
+IMG_gralloc_module_public_t;
+
+typedef struct
+{
+ int l, t, w, h;
+}
+IMG_write_lock_rect_t;
+
+typedef struct IMG_buffer_format_public_t
+{
+ /* Buffer formats are returned as a linked list */
+ struct IMG_buffer_format_public_t *psNext;
+
+ /* HAL_PIXEL_FORMAT_... enumerant */
+ int iHalPixelFormat;
+
+ /* WSEGL_PIXELFORMAT_... enumerant */
+ int iWSEGLPixelFormat;
+
+ /* Friendly name for format */
+ const char *const szName;
+
+ /* Bits (not bytes) per pixel */
+ unsigned int uiBpp;
+
+ /* GPU output format (creates EGLConfig for format) */
+ int bGPURenderable;
+}
+IMG_buffer_format_public_t;
+
+/*
+ * These are vendor specific pixel formats, by (informal) convention IMGTec
+ * formats start from the top of the range, TI formats start from the bottom
+ */
+#define HAL_PIXEL_FORMAT_BGRX_8888 0x1FF
+#define HAL_PIXEL_FORMAT_TI_NV12 0x100
+#define HAL_PIXEL_FORMAT_TI_UNUSED 0x101 /* Free for use */
+#define HAL_PIXEL_FORMAT_TI_NV12_1D 0x102
+
+#ifndef GRALLOC_USAGE_SYSTEM_HEAP
+#define GRALLOC_USAGE_SYSTEM_HEAP GRALLOC_USAGE_PRIVATE_0
+#else
+#error GRALLOC_USAGE_SYSTEM_HEAP should only be defined by hal_public.h
+#endif
+
+#ifndef GRALLOC_USAGE_PHYS_CONTIG
+#define GRALLOC_USAGE_PHYS_CONTIG GRALLOC_USAGE_PRIVATE_1
+#else
+#error GRALLOC_USAGE_PHYS_CONTIG should only be defined by hal_public.h
+#endif
+#endif /* HAL_PUBLIC_H */
+
diff --git a/hwc/hwc.c b/hwc/hwc.c
new file mode 100644
index 0000000..855942b
--- /dev/null
+++ b/hwc/hwc.c
@@ -0,0 +1,2747 @@
+/*
+ * 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 <errno.h>
+#include <malloc.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <sys/ioctl.h>
+#include <sys/resource.h>
+
+#ifdef SYSFS_VSYNC_NOTIFICATION
+#include <sys/prctl.h>
+#endif
+
+#include <cutils/properties.h>
+#include <cutils/log.h>
+#include <cutils/native_handle.h>
+#define HWC_REMOVE_DEPRECATED_VERSIONS 1
+#include <hardware/hardware.h>
+#include <hardware/hwcomposer.h>
+#include <hardware_legacy/uevent.h>
+#include <system/graphics.h>
+#include <utils/Timers.h>
+#include <EGL/egl.h>
+#include <edid_parser.h>
+#ifdef OMAP_ENHANCEMENT_S3D
+#include <ui/S3DFormat.h>
+#endif
+
+#include <linux/fb.h>
+#include <linux/omapfb.h>
+#include <ion_ti/ion.h>
+
+#include "hwc_dev.h"
+#include "dock_image.h"
+#include "sw_vsync.h"
+
+#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 DIV_ROUND_UP(a, b) (((a) + (b) - 1) / (b))
+
+#define MAX_HWC_LAYERS 32
+#define MAX_HW_OVERLAYS 4
+#define NUM_NONSCALING_OVERLAYS 1
+#define NUM_EXT_DISPLAY_BACK_BUFFERS 2
+#define ASPECT_RATIO_TOLERANCE 0.02f
+
+/* used by property settings */
+enum {
+ EXT_ROTATION = 3, /* rotation while mirroring */
+ EXT_HFLIP = (1 << 2), /* flip l-r on output (after rotation) */
+};
+
+#define HAL_FMT(f) ((f) == HAL_PIXEL_FORMAT_TI_NV12 ? "NV12" : \
+ (f) == HAL_PIXEL_FORMAT_TI_NV12_1D ? "NV12" : \
+ (f) == HAL_PIXEL_FORMAT_YV12 ? "YV12" : \
+ (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" : "??")
+
+#define DSS_FMT(f) ((f) == OMAP_DSS_COLOR_NV12 ? "NV12" : \
+ (f) == OMAP_DSS_COLOR_RGB24U ? "xRGB32" : \
+ (f) == OMAP_DSS_COLOR_ARGB32 ? "ARGB32" : \
+ (f) == OMAP_DSS_COLOR_RGB16 ? "RGB565" : "??")
+
+static bool debug = false;
+static bool debugpost2 = false;
+static bool debugblt = false;
+static rgz_t grgz;
+static rgz_ext_layer_list_t grgz_ext_layer_list;
+static struct bvsurfgeom gscrngeom;
+
+static void showfps(void)
+{
+ static int framecount = 0;
+ static int lastframecount = 0;
+ static nsecs_t lastfpstime = 0;
+ static float fps = 0;
+ char value[PROPERTY_VALUE_MAX];
+
+ property_get("debug.hwc.showfps", value, "0");
+ if (!atoi(value)) {
+ return;
+ }
+
+ framecount++;
+ if (!(framecount & 0x7)) {
+ nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
+ nsecs_t diff = now - lastfpstime;
+ fps = ((framecount - lastframecount) * (float)(s2ns(1))) / diff;
+ lastfpstime = now;
+ lastframecount = framecount;
+ ALOGI("%d Frames, %f FPS", framecount, fps);
+ }
+}
+
+static void dump_layer(hwc_layer_1_t const* l)
+{
+ ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}, {%d,%d,%d,%d}",
+ 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);
+}
+
+static void dump_dsscomp(struct dsscomp_setup_dispc_data *d)
+{
+ uint32_t i;
+
+ ALOGD("[%08x] set: %c%c%c %d ovls\n",
+ d->sync_id,
+ (d->mode & DSSCOMP_SETUP_MODE_APPLY) ? 'A' : '-',
+ (d->mode & DSSCOMP_SETUP_MODE_DISPLAY) ? 'D' : '-',
+ (d->mode & DSSCOMP_SETUP_MODE_CAPTURE) ? 'C' : '-',
+ d->num_ovls);
+
+ for (i = 0; i < d->num_mgrs; i++) {
+ struct dss2_mgr_info *mi = &d->mgrs[i];
+ ALOGD(" (dis%d alpha=%d col=%08x ilace=%d)\n",
+ mi->ix,
+ mi->alpha_blending, mi->default_color,
+ mi->interlaced);
+ }
+
+ for (i = 0; i < d->num_ovls; i++) {
+ struct dss2_ovl_info *oi = &d->ovls[i];
+ struct dss2_ovl_cfg *c = &oi->cfg;
+ if (c->zonly)
+ ALOGD("ovl%d(%s z%d)\n",
+ c->ix, c->enabled ? "ON" : "off", c->zorder);
+ else
+ ALOGD("ovl%d(%s z%d %s%s *%d%% %d*%d:%d,%d+%d,%d rot%d%s => %d,%d+%d,%d %p/%p|%d)\n",
+ c->ix, c->enabled ? "ON" : "off", c->zorder, DSS_FMT(c->color_mode),
+ c->pre_mult_alpha ? " premult" : "",
+ (c->global_alpha * 100 + 128) / 255,
+ c->width, c->height, c->crop.x, c->crop.y,
+ c->crop.w, c->crop.h,
+ c->rotation, c->mirror ? "+mir" : "",
+ c->win.x, c->win.y, c->win.w, c->win.h,
+ (void *) oi->ba, (void *) oi->uv, c->stride);
+ }
+}
+
+struct dump_buf {
+ char *buf;
+ int buf_len;
+ int len;
+};
+
+static void dump_printf(struct dump_buf *buf, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ buf->len += vsnprintf(buf->buf + buf->len, buf->buf_len - buf->len, fmt, ap);
+ va_end(ap);
+}
+
+static void dump_set_info(omap_hwc_device_t *hwc_dev, hwc_display_contents_1_t* list)
+{
+ struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
+ char logbuf[1024];
+ struct dump_buf log = {
+ .buf = logbuf,
+ .buf_len = sizeof(logbuf),
+ };
+ uint32_t i;
+
+ dump_printf(&log, "set H{");
+ for (i = 0; list && i < list->numHwLayers; i++) {
+ if (i)
+ dump_printf(&log, " ");
+ hwc_layer_1_t *layer = &list->hwLayers[i];
+ IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
+ 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;
+ }
+ if (layer->flags & HWC_HINT_CLEAR_FB)
+ dump_printf(&log, "CLR,");
+ dump_printf(&log, "%d*%d(%s)", handle->iWidth, handle->iHeight, HAL_FMT(handle->iFormat));
+ if (layer->transform)
+ dump_printf(&log, "~%d", layer->transform);
+ }
+ dump_printf(&log, "} D{");
+ for (i = 0; i < dsscomp->num_ovls; i++) {
+ if (i)
+ dump_printf(&log, " ");
+ dump_printf(&log, "%d=", dsscomp->ovls[i].cfg.ix);
+ if (dsscomp->ovls[i].cfg.enabled)
+ dump_printf(&log, "%08x:%d*%d,%s",
+ dsscomp->ovls[i].ba,
+ dsscomp->ovls[i].cfg.width,
+ dsscomp->ovls[i].cfg.height,
+ DSS_FMT(dsscomp->ovls[i].cfg.color_mode));
+ else
+ dump_printf(&log, "-");
+ }
+ dump_printf(&log, "} L{");
+ for (i = 0; i < hwc_dev->post2_layers; i++) {
+ if (i)
+ 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);
+}
+
+static int sync_id = 0;
+
+static bool is_valid_format(uint32_t format)
+{
+ switch(format) {
+ case HAL_PIXEL_FORMAT_RGB_565:
+ case HAL_PIXEL_FORMAT_RGBX_8888:
+ case HAL_PIXEL_FORMAT_RGBA_8888:
+ case HAL_PIXEL_FORMAT_BGRA_8888:
+ case HAL_PIXEL_FORMAT_BGRX_8888:
+ case HAL_PIXEL_FORMAT_TI_NV12:
+ case HAL_PIXEL_FORMAT_TI_NV12_1D:
+ return true;
+
+ default:
+ return false;
+ }
+}
+#ifdef OMAP_ENHANCEMENT_S3D
+static uint32_t get_s3d_layout_type(hwc_layer_1_t *layer)
+{
+ return (layer->flags & S3DLayoutTypeMask) >> S3DLayoutTypeShift;
+}
+
+static uint32_t get_s3d_layout_order(hwc_layer_1_t *layer)
+{
+ return (layer->flags & S3DLayoutOrderMask) >> S3DLayoutOrderShift;
+}
+#endif
+
+static bool scaled(hwc_layer_1_t *layer)
+{
+ int w = WIDTH(layer->sourceCrop);
+ int h = HEIGHT(layer->sourceCrop);
+
+ if (layer->transform & HWC_TRANSFORM_ROT_90)
+ swap(w, h);
+
+ bool res = WIDTH(layer->displayFrame) != w || HEIGHT(layer->displayFrame) != h;
+#ifdef OMAP_ENHANCEMENT_S3D
+ /* An S3D layer also needs scaling due to subsampling */
+ res = res || (get_s3d_layout_type(layer) != eMono);
+#endif
+
+ return res;
+}
+
+static bool is_protected(hwc_layer_1_t *layer)
+{
+ IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
+
+ return (handle->usage & GRALLOC_USAGE_PROTECTED) != 0;
+}
+
+#define is_BLENDED(layer) ((layer)->blending != HWC_BLENDING_NONE)
+
+static bool is_RGB(IMG_native_handle_t *handle)
+{
+ switch(handle->iFormat)
+ {
+ case HAL_PIXEL_FORMAT_BGRA_8888:
+ case HAL_PIXEL_FORMAT_BGRX_8888:
+ case HAL_PIXEL_FORMAT_RGB_565:
+ return true;
+ default:
+ return false;
+ }
+}
+static uint32_t get_format_bpp(uint32_t format)
+{
+ switch(format) {
+ case HAL_PIXEL_FORMAT_BGRA_8888:
+ case HAL_PIXEL_FORMAT_BGRX_8888:
+ case HAL_PIXEL_FORMAT_RGBX_8888:
+ case HAL_PIXEL_FORMAT_RGBA_8888:
+ return 32;
+ case HAL_PIXEL_FORMAT_RGB_565:
+ return 16;
+ case HAL_PIXEL_FORMAT_TI_NV12:
+ case HAL_PIXEL_FORMAT_TI_NV12_1D:
+ return 8;
+ default:
+ return 0;
+ }
+}
+
+static bool is_BGR_format(uint32_t format)
+{
+ switch (format) {
+ case HAL_PIXEL_FORMAT_RGBX_8888:
+ case HAL_PIXEL_FORMAT_RGBA_8888:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static bool is_BGR(IMG_native_handle_t *handle)
+{
+ return is_BGR_format(handle->iFormat);
+}
+
+static bool is_NV12(IMG_native_handle_t *handle)
+{
+ switch(handle->iFormat)
+ {
+ case HAL_PIXEL_FORMAT_TI_NV12:
+ case HAL_PIXEL_FORMAT_TI_NV12_1D:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static bool is_upscaled_NV12(omap_hwc_device_t *hwc_dev, hwc_layer_1_t *layer)
+{
+ if (!layer)
+ return false;
+
+ IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
+ if (!is_NV12(handle))
+ return false;
+
+ 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 * hwc_dev->upscaled_nv12_limit ||
+ HEIGHT(layer->displayFrame) >= h * hwc_dev->upscaled_nv12_limit);
+}
+
+static bool dockable(hwc_layer_1_t *layer)
+{
+ IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
+
+ return (handle->usage & GRALLOC_USAGE_EXTERNAL_DISP) != 0;
+}
+
+static uint32_t mem1d(IMG_native_handle_t *handle)
+{
+ if (handle == NULL || is_NV12(handle))
+ return 0;
+
+ int bpp = handle->iFormat == HAL_PIXEL_FORMAT_RGB_565 ? 2 : 4;
+ int stride = ALIGN(handle->iWidth, HW_ALIGN) * bpp;
+ return stride * handle->iHeight;
+}
+
+static void setup_layer_base(struct dss2_ovl_cfg *oc, int index, uint32_t format,
+ bool blended, int width, int height)
+{
+ /* YUV2RGB conversion */
+ const struct omap_dss_cconv_coefs ctbl_bt601_5 = {
+ 298, 409, 0, 298, -208, -100, 298, 0, 517, 0,
+ };
+
+ /* convert color format */
+ switch (format) {
+ case HAL_PIXEL_FORMAT_RGBA_8888:
+ case HAL_PIXEL_FORMAT_BGRA_8888:
+ oc->color_mode = OMAP_DSS_COLOR_ARGB32;
+ if (blended)
+ break;
+
+ case HAL_PIXEL_FORMAT_RGBX_8888:
+ case HAL_PIXEL_FORMAT_BGRX_8888:
+ oc->color_mode = OMAP_DSS_COLOR_RGB24U;
+ break;
+
+ case HAL_PIXEL_FORMAT_RGB_565:
+ oc->color_mode = OMAP_DSS_COLOR_RGB16;
+ break;
+
+ case HAL_PIXEL_FORMAT_TI_NV12:
+ case HAL_PIXEL_FORMAT_TI_NV12_1D:
+ oc->color_mode = OMAP_DSS_COLOR_NV12;
+ oc->cconv = ctbl_bt601_5;
+ break;
+
+ default:
+ /* Should have been filtered out */
+ ALOGV("Unsupported pixel format");
+ return;
+ }
+
+ oc->width = width;
+ oc->height = height;
+ oc->stride = ALIGN(width, HW_ALIGN) * get_format_bpp(format) / 8;
+
+ oc->enabled = 1;
+ oc->global_alpha = 255;
+ oc->zorder = index;
+ oc->ix = 0;
+
+ /* defaults for SGX framebuffer renders */
+ oc->crop.w = oc->win.w = width;
+ oc->crop.h = oc->win.h = height;
+
+ /* for now interlacing and vc1 info is not supplied */
+ oc->ilace = OMAP_DSS_ILACE_NONE;
+ oc->vc1.enable = 0;
+}
+
+static void setup_layer(omap_hwc_device_t *hwc_dev, struct dss2_ovl_info *ovl,
+ hwc_layer_1_t *layer, int index, uint32_t format, int width, int height)
+{
+ struct dss2_ovl_cfg *oc = &ovl->cfg;
+
+ //dump_layer(layer);
+
+ setup_layer_base(oc, index, format, is_BLENDED(layer), width, height);
+
+ /* convert transformation - assuming 0-set config */
+ if (layer->transform & HWC_TRANSFORM_FLIP_H)
+ oc->mirror = 1;
+ if (layer->transform & HWC_TRANSFORM_FLIP_V) {
+ oc->rotation = 2;
+ oc->mirror = !oc->mirror;
+ }
+ if (layer->transform & HWC_TRANSFORM_ROT_90) {
+ oc->rotation += oc->mirror ? -1 : 1;
+ oc->rotation &= 3;
+ }
+
+ oc->pre_mult_alpha = layer->blending == HWC_BLENDING_PREMULT;
+
+ /* display position */
+ oc->win.x = layer->displayFrame.left;
+ oc->win.y = layer->displayFrame.top;
+ oc->win.w = WIDTH(layer->displayFrame);
+ oc->win.h = HEIGHT(layer->displayFrame);
+
+ /* crop */
+ oc->crop.x = layer->sourceCrop.left;
+ oc->crop.y = layer->sourceCrop.top;
+ oc->crop.w = WIDTH(layer->sourceCrop);
+ oc->crop.h = HEIGHT(layer->sourceCrop);
+}
+
+const float m_unit[2][3] = { { 1., 0., 0. }, { 0., 1., 0. } };
+
+static inline void m_translate(float m[2][3], float dx, float dy)
+{
+ m[0][2] += dx;
+ m[1][2] += dy;
+}
+
+static inline void m_scale1(float m[3], int from, int to)
+{
+ m[0] = m[0] * to / from;
+ m[1] = m[1] * to / from;
+ m[2] = m[2] * to / from;
+}
+
+static inline void m_scale(float m[2][3], int x_from, int x_to, int y_from, int y_to)
+{
+ m_scale1(m[0], x_from, x_to);
+ m_scale1(m[1], y_from, y_to);
+}
+
+static void m_rotate(float m[2][3], int quarter_turns)
+{
+ if (quarter_turns & 2)
+ m_scale(m, 1, -1, 1, -1);
+ if (quarter_turns & 1) {
+ float q;
+ q = m[0][0]; m[0][0] = -m[1][0]; m[1][0] = q;
+ q = m[0][1]; m[0][1] = -m[1][1]; m[1][1] = q;
+ q = m[0][2]; m[0][2] = -m[1][2]; m[1][2] = q;
+ }
+}
+
+static inline int m_round(float x)
+{
+ /* int truncates towards 0 */
+ return (int) (x < 0 ? x - 0.5 : x + 0.5);
+}
+
+/*
+ * assuming xpy (xratio:yratio) original pixel ratio, calculate the adjusted width
+ * and height for a screen of xres/yres and physical size of width/height.
+ * The adjusted size is the largest that fits into the screen.
+ */
+static void get_max_dimensions(uint32_t orig_xres, uint32_t orig_yres,
+ float xpy,
+ uint32_t scr_xres, uint32_t scr_yres,
+ uint32_t scr_width, uint32_t scr_height,
+ uint32_t *adj_xres, uint32_t *adj_yres)
+{
+ /* assume full screen (largest size)*/
+ *adj_xres = scr_xres;
+ *adj_yres = scr_yres;
+
+ /* assume 1:1 pixel ratios if none supplied */
+ if (!scr_width || !scr_height) {
+ scr_width = scr_xres;
+ scr_height = scr_yres;
+ }
+
+ /* trim to keep aspect ratio */
+ float x_factor = orig_xres * xpy * scr_height;
+ float y_factor = orig_yres * scr_width;
+
+ /* allow for tolerance so we avoid scaling if framebuffer is standard size */
+ if (x_factor < y_factor * (1.f - ASPECT_RATIO_TOLERANCE))
+ *adj_xres = (uint32_t) (x_factor * *adj_xres / y_factor + 0.5);
+ else if (x_factor * (1.f - ASPECT_RATIO_TOLERANCE) > y_factor)
+ *adj_yres = (uint32_t) (y_factor * *adj_yres / x_factor + 0.5);
+}
+
+static void set_ext_matrix(omap_hwc_ext_t *ext, struct hwc_rect region)
+{
+ int orig_w = WIDTH(region);
+ int orig_h = HEIGHT(region);
+ float xpy = ext->lcd_xpy;
+
+ /* reorientation matrix is:
+ m = (center-from-target-center) * (scale-to-target) * (mirror) * (rotate) * (center-to-original-center) */
+
+ memcpy(ext->m, m_unit, sizeof(m_unit));
+ m_translate(ext->m, -(orig_w / 2.0f) - region.left, -(orig_h / 2.0f) - region.top);
+ m_rotate(ext->m, ext->current.rotation);
+ if (ext->current.hflip)
+ m_scale(ext->m, 1, -1, 1, 1);
+
+ if (ext->current.rotation & 1) {
+ swap(orig_w, orig_h);
+ xpy = 1. / xpy;
+ }
+
+ /* get target size */
+ uint32_t adj_xres, adj_yres;
+ get_max_dimensions(orig_w, orig_h, xpy,
+ ext->xres, ext->yres, ext->width, ext->height,
+ &adj_xres, &adj_yres);
+
+ m_scale(ext->m, orig_w, adj_xres, orig_h, adj_yres);
+ m_translate(ext->m, ext->xres >> 1, ext->yres >> 1);
+}
+
+static int
+crop_to_rect(struct dss2_ovl_cfg *cfg, struct hwc_rect vis_rect)
+{
+ struct {
+ int xy[2];
+ int wh[2];
+ } crop, win;
+ struct {
+ int lt[2];
+ int rb[2];
+ } vis;
+ win.xy[0] = cfg->win.x; win.xy[1] = cfg->win.y;
+ win.wh[0] = cfg->win.w; win.wh[1] = cfg->win.h;
+ crop.xy[0] = cfg->crop.x; crop.xy[1] = cfg->crop.y;
+ crop.wh[0] = cfg->crop.w; crop.wh[1] = cfg->crop.h;
+ vis.lt[0] = vis_rect.left; vis.lt[1] = vis_rect.top;
+ vis.rb[0] = vis_rect.right; vis.rb[1] = vis_rect.bottom;
+
+ int c;
+ bool swap = cfg->rotation & 1;
+
+ /* align crop window with display coordinates */
+ if (swap)
+ crop.xy[1] -= (crop.wh[1] = -crop.wh[1]);
+ if (cfg->rotation & 2)
+ crop.xy[!swap] -= (crop.wh[!swap] = -crop.wh[!swap]);
+ if ((!cfg->mirror) ^ !(cfg->rotation & 2))
+ crop.xy[swap] -= (crop.wh[swap] = -crop.wh[swap]);
+
+ for (c = 0; c < 2; c++) {
+ /* see if complete buffer is outside the vis or it is
+ fully cropped or scaled to 0 */
+ if (win.wh[c] <= 0 || vis.rb[c] <= vis.lt[c] ||
+ win.xy[c] + win.wh[c] <= vis.lt[c] ||
+ win.xy[c] >= vis.rb[c] ||
+ !crop.wh[c ^ swap])
+ return -ENOENT;
+
+ /* crop left/top */
+ if (win.xy[c] < vis.lt[c]) {
+ /* correction term */
+ int a = (vis.lt[c] - win.xy[c]) * crop.wh[c ^ swap] / win.wh[c];
+ crop.xy[c ^ swap] += a;
+ crop.wh[c ^ swap] -= a;
+ win.wh[c] -= vis.lt[c] - win.xy[c];
+ win.xy[c] = vis.lt[c];
+ }
+ /* crop right/bottom */
+ if (win.xy[c] + win.wh[c] > vis.rb[c]) {
+ crop.wh[c ^ swap] = crop.wh[c ^ swap] * (vis.rb[c] - win.xy[c]) / win.wh[c];
+ win.wh[c] = vis.rb[c] - win.xy[c];
+ }
+
+ if (!crop.wh[c ^ swap] || !win.wh[c])
+ return -ENOENT;
+ }
+
+ /* realign crop window to buffer coordinates */
+ if (cfg->rotation & 2)
+ crop.xy[!swap] -= (crop.wh[!swap] = -crop.wh[!swap]);
+ if ((!cfg->mirror) ^ !(cfg->rotation & 2))
+ crop.xy[swap] -= (crop.wh[swap] = -crop.wh[swap]);
+ if (swap)
+ crop.xy[1] -= (crop.wh[1] = -crop.wh[1]);
+
+ cfg->win.x = win.xy[0]; cfg->win.y = win.xy[1];
+ cfg->win.w = win.wh[0]; cfg->win.h = win.wh[1];
+ cfg->crop.x = crop.xy[0]; cfg->crop.y = crop.xy[1];
+ cfg->crop.w = crop.wh[0]; cfg->crop.h = crop.wh[1];
+
+ return 0;
+}
+
+static void apply_transform(float transform[2][3],struct dss2_ovl_cfg *oc)
+{
+ float x, y, w, h;
+
+ /* display position */
+ x = transform[0][0] * oc->win.x + transform[0][1] * oc->win.y + transform[0][2];
+ y = transform[1][0] * oc->win.x + transform[1][1] * oc->win.y + transform[1][2];
+ w = transform[0][0] * oc->win.w + transform[0][1] * oc->win.h;
+ h = transform[1][0] * oc->win.w + transform[1][1] * oc->win.h;
+ oc->win.x = m_round(w > 0 ? x : x + w);
+ oc->win.y = m_round(h > 0 ? y : y + h);
+ oc->win.w = m_round(w > 0 ? w : -w);
+ oc->win.h = m_round(h > 0 ? h : -h);
+}
+
+static void adjust_ext_layer(omap_hwc_ext_t *ext, struct dss2_ovl_info *ovl)
+{
+ struct dss2_ovl_cfg *oc = &ovl->cfg;
+
+ /* crop to clone region if mirroring */
+ if (!ext->current.docking &&
+ crop_to_rect(&ovl->cfg, ext->mirror_region) != 0) {
+ ovl->cfg.enabled = 0;
+ return;
+ }
+
+ apply_transform(ext->m, oc);
+
+ /* combining transformations: F^a*R^b*F^i*R^j = F^(a+b)*R^(j+b*(-1)^i), because F*R = R^(-1)*F */
+ oc->rotation += (oc->mirror ? -1 : 1) * ext->current.rotation;
+ oc->rotation &= 3;
+ if (ext->current.hflip)
+ oc->mirror = !oc->mirror;
+}
+
+static struct dsscomp_platform_info limits;
+
+static void adjust_primary_display_layer(omap_hwc_device_t *hwc_dev, struct dss2_ovl_info *ovl)
+{
+ struct dss2_ovl_cfg *oc = &ovl->cfg;
+
+ if (crop_to_rect(&ovl->cfg, hwc_dev->primary_region) != 0) {
+ ovl->cfg.enabled = 0;
+ return;
+ }
+
+ apply_transform(hwc_dev->primary_m, oc);
+
+ /* combining transformations: F^a*R^b*F^i*R^j = F^(a+b)*R^(j+b*(-1)^i), because F*R = R^(-1)*F */
+ oc->rotation += (oc->mirror ? -1 : 1) * hwc_dev->primary_rotation;
+ oc->rotation &= 3;
+}
+
+static bool can_scale(uint32_t src_w, uint32_t src_h, uint32_t dst_w, uint32_t dst_h, bool is_2d,
+ struct dsscomp_display_info *dis, struct dsscomp_platform_info *limits,
+ uint32_t pclk, IMG_native_handle_t *handle)
+{
+ uint32_t fclk = limits->fclk / 1000;
+ uint32_t min_src_w = DIV_ROUND_UP(src_w, is_2d ? limits->max_xdecim_2d : limits->max_xdecim_1d);
+ uint32_t min_src_h = DIV_ROUND_UP(src_h, is_2d ? limits->max_ydecim_2d : limits->max_ydecim_1d);
+
+ /* ERRATAs */
+ /* cannot render 1-width layers on DSI video mode panels - we just disallow all 1-width LCD layers */
+ if (dis->channel != OMAP_DSS_CHANNEL_DIGIT && dst_w < limits->min_width)
+ return false;
+
+ /* NOTE: no support for checking YUV422 layers that are tricky to scale */
+
+ /* FIXME: limit vertical downscale well below theoretical limit as we saw display artifacts */
+ if (dst_h < src_h / 4)
+ return false;
+
+ /* max downscale */
+ if (dst_h * limits->max_downscale < min_src_h)
+ return false;
+
+ /* for manual panels pclk is 0, and there are no pclk based scaling limits */
+ if (!pclk)
+ return !(dst_w < src_w / limits->max_downscale / (is_2d ? limits->max_xdecim_2d : limits->max_xdecim_1d));
+
+ /* :HACK: limit horizontal downscale well below theoretical limit as we saw display artifacts */
+ if (dst_w * 4 < src_w)
+ return false;
+
+ if (handle)
+ if (get_format_bpp(handle->iFormat) == 32 && src_w > 1280 && dst_w * 3 < src_w)
+ return false;
+
+ /* max horizontal downscale is 4, or the fclk/pixclk */
+ if (fclk > pclk * limits->max_downscale)
+ fclk = pclk * limits->max_downscale;
+ /* for small parts, we need to use integer fclk/pixclk */
+ if (src_w < limits->integer_scale_ratio_limit)
+ fclk = fclk / pclk * pclk;
+ if ((uint32_t) dst_w * fclk < min_src_w * pclk)
+ return false;
+
+ return true;
+}
+
+static bool can_scale_layer(omap_hwc_device_t *hwc_dev, hwc_layer_1_t *layer, IMG_native_handle_t *handle)
+{
+ int src_w = WIDTH(layer->sourceCrop);
+ int src_h = HEIGHT(layer->sourceCrop);
+ int dst_w = WIDTH(layer->displayFrame);
+ int dst_h = HEIGHT(layer->displayFrame);
+
+ /* account for 90-degree rotation */
+ if (layer->transform & HWC_TRANSFORM_ROT_90)
+ swap(src_w, src_h);
+
+ /* NOTE: layers should be able to be scaled externally since
+ framebuffer is able to be scaled on selected external resolution */
+ return can_scale(src_w, src_h, dst_w, dst_h, is_NV12(handle), &hwc_dev->fb_dis, &limits,
+ hwc_dev->fb_dis.timings.pixel_clock, handle);
+}
+
+static bool is_valid_layer(omap_hwc_device_t *hwc_dev, hwc_layer_1_t *layer, IMG_native_handle_t *handle)
+{
+ /* Skip layers are handled by SF */
+ if ((layer->flags & HWC_SKIP_LAYER) || !handle)
+ return false;
+
+ if (!is_valid_format(handle->iFormat))
+ return false;
+
+ /* 1D buffers: no transform, must fit in TILER slot */
+ if (!is_NV12(handle)) {
+ if (layer->transform)
+ return false;
+ if (mem1d(handle) > limits.tiler1d_slot_size)
+ return false;
+ }
+
+ return can_scale_layer(hwc_dev, layer, handle);
+}
+
+static uint32_t add_scaling_score(uint32_t score,
+ uint32_t xres, uint32_t yres, uint32_t refresh,
+ uint32_t ext_xres, uint32_t ext_yres,
+ uint32_t mode_xres, uint32_t mode_yres, uint32_t mode_refresh)
+{
+ uint32_t area = xres * yres;
+ uint32_t ext_area = ext_xres * ext_yres;
+ uint32_t mode_area = mode_xres * mode_yres;
+
+ /* prefer to upscale (1% tolerance) [0..1] (insert after 1st bit) */
+ int upscale = (ext_xres >= xres * 99 / 100 && ext_yres >= yres * 99 / 100);
+ score = (((score & ~1) | upscale) << 1) | (score & 1);
+
+ /* pick minimum scaling [0..16] */
+ if (ext_area > area)
+ score = (score << 5) | (16 * area / ext_area);
+ else
+ score = (score << 5) | (16 * ext_area / area);
+
+ /* pick smallest leftover area [0..16] */
+ score = (score << 5) | ((16 * ext_area + (mode_area >> 1)) / mode_area);
+
+ /* adjust mode refresh rate */
+ mode_refresh += mode_refresh % 6 == 5;
+
+ /* prefer same or higher frame rate */
+ upscale = (mode_refresh >= refresh);
+ score = (score << 1) | upscale;
+
+ /* pick closest frame rate */
+ if (mode_refresh > refresh)
+ score = (score << 8) | (240 * refresh / mode_refresh);
+ else
+ score = (score << 8) | (240 * mode_refresh / refresh);
+
+ return score;
+}
+
+static int set_best_hdmi_mode(omap_hwc_device_t *hwc_dev, uint32_t xres, uint32_t yres, float xpy)
+{
+ int dis_ix = hwc_dev->on_tv ? 0 : 1;
+ struct _qdis {
+ struct dsscomp_display_info dis;
+ struct dsscomp_videomode modedb[32];
+ } d = { .dis = { .ix = dis_ix } };
+ omap_hwc_ext_t *ext = &hwc_dev->ext;
+
+ d.dis.modedb_len = sizeof(d.modedb) / sizeof(*d.modedb);
+ int ret = ioctl(hwc_dev->dsscomp_fd, DSSCIOC_QUERY_DISPLAY, &d);
+ if (ret)
+ return ret;
+
+ if (d.dis.timings.x_res * d.dis.timings.y_res == 0 ||
+ xres * yres == 0)
+ return -EINVAL;
+
+ uint32_t i, best = ~0, best_score = 0;
+ ext->width = d.dis.width_in_mm;
+ ext->height = d.dis.height_in_mm;
+ ext->xres = d.dis.timings.x_res;
+ ext->yres = d.dis.timings.y_res;
+
+ /* use VGA external resolution as default */
+ if (!ext->xres || !ext->yres) {
+ ext->xres = 640;
+ ext->yres = 480;
+ }
+
+ uint32_t ext_fb_xres, ext_fb_yres;
+ for (i = 0; i < d.dis.modedb_len; i++) {
+ uint32_t score = 0;
+ uint32_t mode_xres = d.modedb[i].xres;
+ uint32_t mode_yres = d.modedb[i].yres;
+ uint32_t ext_width = d.dis.width_in_mm;
+ uint32_t ext_height = d.dis.height_in_mm;
+
+ if (d.modedb[i].vmode & FB_VMODE_INTERLACED)
+ mode_yres /= 2;
+
+ if (d.modedb[i].flag & FB_FLAG_RATIO_4_3) {
+ ext_width = 4;
+ ext_height = 3;
+ } else if (d.modedb[i].flag & FB_FLAG_RATIO_16_9) {
+ ext_width = 16;
+ ext_height = 9;
+ }
+
+ if (!mode_xres || !mode_yres)
+ continue;
+
+ get_max_dimensions(xres, yres, xpy, mode_xres, mode_yres,
+ ext_width, ext_height, &ext_fb_xres, &ext_fb_yres);
+
+ /* we need to ensure that even TILER2D buffers can be scaled */
+ if (!d.modedb[i].pixclock ||
+ (d.modedb[i].vmode & ~FB_VMODE_INTERLACED) ||
+ !can_scale(xres, yres, ext_fb_xres, ext_fb_yres,
+ 1, &d.dis, &limits,
+ 1000000000 / d.modedb[i].pixclock, NULL))
+ continue;
+
+ /* prefer CEA modes */
+ if (d.modedb[i].flag & (FB_FLAG_RATIO_4_3 | FB_FLAG_RATIO_16_9))
+ score = 1;
+
+ /* prefer the same mode as we use for mirroring to avoid mode change */
+ score = (score << 1) | (i == ~ext->mirror_mode && ext->avoid_mode_change);
+
+ score = add_scaling_score(score, xres, yres, 60, ext_fb_xres, ext_fb_yres,
+ mode_xres, mode_yres, d.modedb[i].refresh ? : 1);
+
+ ALOGD("#%d: %dx%d %dHz", i, mode_xres, mode_yres, d.modedb[i].refresh);
+ if (debug)
+ ALOGD(" score=0x%x adj.res=%dx%d", score, ext_fb_xres, ext_fb_yres);
+ if (best_score < score) {
+ ext->width = ext_width;
+ ext->height = ext_height;
+ ext->xres = mode_xres;
+ ext->yres = mode_yres;
+ best = i;
+ best_score = score;
+ }
+ }
+ if (~best) {
+ struct dsscomp_setup_display_data sdis = { .ix = dis_ix };
+ sdis.mode = d.dis.modedb[best];
+ ALOGD("picking #%d", best);
+ /* only reconfigure on change */
+ if (ext->last_mode != ~best)
+ ioctl(hwc_dev->dsscomp_fd, DSSCIOC_SETUP_DISPLAY, &sdis);
+ ext->last_mode = ~best;
+ } else {
+ uint32_t ext_width = d.dis.width_in_mm;
+ uint32_t ext_height = d.dis.height_in_mm;
+ uint32_t ext_fb_xres, ext_fb_yres;
+
+ get_max_dimensions(xres, yres, xpy, d.dis.timings.x_res, d.dis.timings.y_res,
+ ext_width, ext_height, &ext_fb_xres, &ext_fb_yres);
+ if (!d.dis.timings.pixel_clock ||
+ !can_scale(xres, yres, ext_fb_xres, ext_fb_yres,
+ 1, &d.dis, &limits,
+ d.dis.timings.pixel_clock, NULL)) {
+ ALOGW("DSS scaler cannot support HDMI cloning");
+ return -1;
+ }
+ }
+ ext->last_xres_used = xres;
+ ext->last_yres_used = yres;
+ ext->last_xpy = xpy;
+ if (d.dis.channel == OMAP_DSS_CHANNEL_DIGIT)
+ ext->on_tv = 1;
+ return 0;
+}
+
+static void gather_layer_statistics(omap_hwc_device_t *hwc_dev, hwc_display_contents_1_t *list)
+{
+ uint32_t i;
+ counts_t *num = &hwc_dev->counts;
+
+ memset(num, 0, sizeof(*num));
+
+ num->composited_layers = list ? list->numHwLayers : 0;
+
+ /* Figure out how many layers we can support via DSS */
+ for (i = 0; list && i < list->numHwLayers; i++) {
+ hwc_layer_1_t *layer = &list->hwLayers[i];
+ IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
+#ifdef OMAP_ENHANCEMENT_S3D
+ uint32_t s3d_layout_type = get_s3d_layout_type(layer);
+#endif
+
+ layer->compositionType = HWC_FRAMEBUFFER;
+
+ if (is_valid_layer(hwc_dev, layer, handle)) {
+#ifdef OMAP_ENHANCEMENT_S3D
+ if (s3d_layout_type != eMono) {
+ /* For now we can only handle 1 S3D layer, skip any additional ones */
+ if (num->s3d > 0 || !hwc_dev->ext.dock.enabled || !hwc_dev->ext.s3d_capable) {
+ layer->flags |= HWC_SKIP_LAYER;
+ continue;
+ } else if (num->s3d == 0) {
+ /* For now, S3D layer is made a dockable layer to trigger docking logic. */
+ if (!dockable(layer)) {
+ num->dockable++;
+ }
+ num->s3d++;
+ hwc_dev->s3d_input_type = s3d_layout_type;
+ hwc_dev->s3d_input_order = get_s3d_layout_order(layer);
+ }
+ }
+#endif
+ num->possible_overlay_layers++;
+
+ /* NV12 layers can only be rendered on scaling overlays */
+ if (scaled(layer) || is_NV12(handle) || hwc_dev->primary_transform)
+ num->scaled_layers++;
+
+ if (is_BGR(handle))
+ num->BGR++;
+ else if (is_RGB(handle))
+ num->RGB++;
+ else if (is_NV12(handle))
+ num->NV12++;
+
+ if (dockable(layer))
+ num->dockable++;
+
+ if (is_protected(layer))
+ num->protected++;
+
+ num->mem += mem1d(handle);
+ }
+ }
+}
+
+static void decide_supported_cloning(omap_hwc_device_t *hwc_dev)
+{
+ omap_hwc_ext_t *ext = &hwc_dev->ext;
+ counts_t *num = &hwc_dev->counts;
+ int nonscaling_ovls = NUM_NONSCALING_OVERLAYS;
+ num->max_hw_overlays = MAX_HW_OVERLAYS;
+
+ /*
+ * We cannot atomically switch overlays from one display to another. First, they
+ * have to be disabled, and the disabling has to take effect on the current display.
+ * We keep track of the available number of overlays here.
+ */
+ if (ext->dock.enabled && !(ext->mirror.enabled && !(num->dockable || ext->force_dock))) {
+ /* some overlays may already be used by the external display, so we account for this */
+
+ /* reserve just a video pipeline for HDMI if docking */
+ hwc_dev->ext_ovls = (num->dockable || ext->force_dock) ? 1 : 0;
+#ifdef OMAP_ENHANCEMENT_S3D
+ if (num->s3d && (hwc_dev->ext.s3d_type != hwc_dev->s3d_input_type)) {
+ /* S3D layers are dockable, and they need two overlays */
+ hwc_dev->ext_ovls += 1;
+ }
+#endif
+ num->max_hw_overlays -= max(hwc_dev->ext_ovls, hwc_dev->last_ext_ovls);
+
+ /* use mirroring transform if we are auto-switching to docking mode while mirroring*/
+ if (ext->mirror.enabled) {
+ ext->current = ext->mirror;
+ ext->current.docking = 1;
+ } else {
+ ext->current = ext->dock;
+ }
+ } else if (ext->mirror.enabled) {
+ /*
+ * otherwise, manage just from half the pipelines. NOTE: there is
+ * no danger of having used too many overlays for external display here.
+ */
+ num->max_hw_overlays >>= 1;
+ nonscaling_ovls >>= 1;
+ hwc_dev->ext_ovls = MAX_HW_OVERLAYS - num->max_hw_overlays;
+ ext->current = ext->mirror;
+ } else {
+ num->max_hw_overlays -= hwc_dev->last_ext_ovls;
+ hwc_dev->ext_ovls = 0;
+ ext->current.enabled = 0;
+ }
+
+ /*
+ * :TRICKY: We may not have enough overlays on the external display. We "reserve" them
+ * here to figure out if mirroring is supported, but may not do mirroring for the first
+ * frame while the overlays required for it are cleared.
+ */
+ hwc_dev->ext_ovls_wanted = hwc_dev->ext_ovls;
+ hwc_dev->ext_ovls = min(MAX_HW_OVERLAYS - hwc_dev->last_int_ovls, hwc_dev->ext_ovls);
+
+ /* if mirroring, we are limited by both internal and external overlays. However,
+ ext_ovls is always <= MAX_HW_OVERLAYS / 2 <= max_hw_overlays */
+ if (!num->protected && hwc_dev->ext_ovls && ext->current.enabled && !ext->current.docking)
+ num->max_hw_overlays = hwc_dev->ext_ovls;
+
+ /* If FB is not same resolution as LCD don't use GFX pipe line*/
+ if (hwc_dev->primary_transform) {
+ num->max_hw_overlays -= NUM_NONSCALING_OVERLAYS;
+ num->max_scaling_overlays = num->max_hw_overlays;
+ } else
+ num->max_scaling_overlays = num->max_hw_overlays - nonscaling_ovls;
+}
+
+static bool can_dss_render_all(omap_hwc_device_t *hwc_dev)
+{
+ omap_hwc_ext_t *ext = &hwc_dev->ext;
+ counts_t *num = &hwc_dev->counts;
+ bool on_tv = hwc_dev->on_tv || (ext->on_tv && ext->current.enabled);
+ bool tform = ext->current.enabled && (ext->current.rotation || ext->current.hflip);
+
+ return !hwc_dev->force_sgx &&
+ /* must have at least one layer if using composition bypass to get sync object */
+ num->possible_overlay_layers &&
+ num->possible_overlay_layers <= num->max_hw_overlays &&
+ num->possible_overlay_layers == num->composited_layers &&
+ num->scaled_layers <= num->max_scaling_overlays &&
+ num->NV12 <= num->max_scaling_overlays &&
+ /* fits into TILER slot */
+ num->mem <= limits.tiler1d_slot_size &&
+ /* we cannot clone non-NV12 transformed layers */
+ (!tform || (num->NV12 == num->possible_overlay_layers) ||
+ (num->NV12 && ext->current.docking)) &&
+ /* HDMI cannot display BGR */
+ (num->BGR == 0 || (num->RGB == 0 && !on_tv) || !hwc_dev->flags_rgb_order) &&
+ /* If nv12_only flag is set DSS should only render NV12 */
+ (!hwc_dev->flags_nv12_only || (num->BGR == 0 && num->RGB == 0));
+}
+
+static inline bool can_dss_render_layer(omap_hwc_device_t *hwc_dev, hwc_layer_1_t *layer)
+{
+ IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
+
+ omap_hwc_ext_t *ext = &hwc_dev->ext;
+ bool cloning = ext->current.enabled && (!ext->current.docking || (handle!=NULL ? dockable(layer) : 0));
+ bool on_tv = hwc_dev->on_tv || (ext->on_tv && cloning);
+ bool tform = cloning && (ext->current.rotation || ext->current.hflip);
+
+ return is_valid_layer(hwc_dev, layer, handle) &&
+ /* cannot rotate non-NV12 layers on external display */
+ (!tform || is_NV12(handle)) &&
+ /* skip non-NV12 layers if also using SGX (if nv12_only flag is set) */
+ (!hwc_dev->flags_nv12_only || (!hwc_dev->use_sgx || is_NV12(handle))) &&
+ /* make sure RGB ordering is consistent (if rgb_order flag is set) */
+ (!(hwc_dev->swap_rb ? is_RGB(handle) : is_BGR(handle)) ||
+ !hwc_dev->flags_rgb_order) &&
+ /* TV can only render RGB */
+ !(on_tv && is_BGR(handle));
+}
+
+static inline int display_area(struct dss2_ovl_info *o)
+{
+ return o->cfg.win.w * o->cfg.win.h;
+}
+
+static int clone_layer(omap_hwc_device_t *hwc_dev, int ix) {
+ struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
+ omap_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];
+
+ if (dsscomp->num_ovls >= MAX_HW_OVERLAYS) {
+ ALOGE("**** cannot clone layer #%d. using all %d overlays.", ix, dsscomp->num_ovls);
+ return -EBUSY;
+ }
+
+ memcpy(o, dsscomp->ovls + ix, sizeof(*o));
+
+ /* reserve overlays at end for other display */
+ o->cfg.ix = MAX_HW_OVERLAYS - 1 - ext_ovl_ix;
+ o->cfg.mgr_ix = 1;
+ /*
+ * Here the assumption is that overlay0 is the one attached to FB.
+ * Hence this clone_layer call is for FB cloning (provided use_sgx is true).
+ */
+ /* For the external displays whose transform is the same as
+ * that of primary display, ion_handles would be NULL hence
+ * the below logic doesn't execute.
+ */
+ if (ix == 0 && hwc_dev->ion_handles[sync_id%2] && hwc_dev->use_sgx) {
+ o->addressing = OMAP_DSS_BUFADDR_ION;
+ o->ba = (int)hwc_dev->ion_handles[sync_id%2];
+ } else {
+ o->addressing = OMAP_DSS_BUFADDR_OVL_IX;
+ o->ba = ix;
+ }
+
+ /* use distinct z values (to simplify z-order checking) */
+ o->cfg.zorder += hwc_dev->post2_layers;
+
+ adjust_ext_layer(&hwc_dev->ext, o);
+ dsscomp->num_ovls++;
+ return 0;
+}
+
+static int clone_external_layer(omap_hwc_device_t *hwc_dev, int ix) {
+ struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
+ omap_hwc_ext_t *ext = &hwc_dev->ext;
+
+ /* mirror only 1 external layer */
+ struct dss2_ovl_info *o = &dsscomp->ovls[ix];
+
+ /* full screen video after transformation */
+ uint32_t xres = o->cfg.crop.w, yres = o->cfg.crop.h;
+ if ((ext->current.rotation + o->cfg.rotation) & 1)
+ swap(xres, yres);
+ float xpy = ext->lcd_xpy * o->cfg.win.w / o->cfg.win.h;
+ if (o->cfg.rotation & 1)
+ xpy = o->cfg.crop.h / xpy / o->cfg.crop.w;
+ else
+ xpy = o->cfg.crop.h * xpy / o->cfg.crop.w;
+ if (ext->current.rotation & 1)
+ xpy = 1. / xpy;
+
+ /* adjust hdmi mode based on resolution */
+ if (xres != ext->last_xres_used ||
+ yres != ext->last_yres_used ||
+ xpy < ext->last_xpy * (1.f - ASPECT_RATIO_TOLERANCE) ||
+ xpy * (1.f - ASPECT_RATIO_TOLERANCE) > ext->last_xpy) {
+ ALOGD("set up HDMI for %d*%d\n", xres, yres);
+ if (set_best_hdmi_mode(hwc_dev, xres, yres, xpy)) {
+ ext->current.enabled = 0;
+ return -ENODEV;
+ }
+ }
+
+ struct hwc_rect region = {
+ .left = o->cfg.win.x, .top = o->cfg.win.y,
+ .right = o->cfg.win.x + o->cfg.win.w,
+ .bottom = o->cfg.win.y + o->cfg.win.h
+ };
+ set_ext_matrix(&hwc_dev->ext, region);
+
+ return clone_layer(hwc_dev, ix);
+}
+
+#ifdef OMAP_ENHANCEMENT_S3D
+const char hdmiS3DTypePath[] = "/sys/devices/platform/omapdss/display1/s3d_type";
+const char hdmiS3DEnablePath[] = "/sys/devices/platform/omapdss/display1/s3d_enable";
+
+static void enable_s3d_hdmi(omap_hwc_device_t *hwc_dev, bool enable)
+{
+ size_t bytesWritten;
+ char data;
+ int fd;
+
+ if (hwc_dev->ext.s3d_enabled == enable) {
+ return;
+ }
+
+ if (enable) {
+ char type[2];
+
+ switch(hwc_dev->ext.s3d_type) {
+ case eSideBySide:
+ snprintf(type, sizeof(type), "%d", HDMI_SIDE_BY_SIDE_HALF);
+ break;
+ case eTopBottom:
+ snprintf(type, sizeof(type), "%d", HDMI_TOPBOTTOM);
+ break;
+ default:
+ return;
+ }
+
+ fd = open(hdmiS3DTypePath, O_WRONLY);
+ if (fd < 0) {
+ ALOGE("Failed to open sysfs %s", hdmiS3DTypePath);
+ return;
+ }
+ bytesWritten = write(fd, type, sizeof(type));
+ close(fd);
+
+ if (bytesWritten != sizeof(type)) {
+ ALOGE("Failed to write (%s) to sysfs %s", type, hdmiS3DTypePath);
+ return;
+ }
+ }
+ data = enable ? '1' : '0';
+
+ fd = open(hdmiS3DEnablePath, O_WRONLY);
+ if (fd < 0) {
+ ALOGE("Failed to open sysfs %s", hdmiS3DEnablePath);
+ return;
+ }
+ bytesWritten = write(fd, &data, 1);
+ close(fd);
+
+ if (bytesWritten != 1) {
+ ALOGE("Failed to write(%d) to sysfs %s", enable, hdmiS3DEnablePath);
+ return;
+ }
+
+ hwc_dev->ext.s3d_enabled = enable;
+}
+
+static void adjust_ext_s3d_layer(omap_hwc_device_t *hwc_dev,
+ struct dss2_ovl_info *ovl, bool left_view)
+{
+ struct dss2_ovl_cfg *oc = &ovl->cfg;
+ float x, y, w, h;
+
+ switch (hwc_dev->s3d_input_type) {
+ case eSideBySide:
+ oc->crop.w = oc->crop.w/2;
+ if ((left_view && hwc_dev->s3d_input_order == eRightViewFirst) ||
+ (!left_view && hwc_dev->s3d_input_order == eLeftViewFirst)) {
+ oc->crop.x = oc->crop.x + oc->crop.w;
+ }
+ break;
+ case eTopBottom:
+ oc->crop.h = oc->crop.h/2;
+ if ((left_view && hwc_dev->s3d_input_order == eRightViewFirst) ||
+ (!left_view && hwc_dev->s3d_input_order == eLeftViewFirst)) {
+ oc->crop.y = oc->crop.y + oc->crop.h;
+ }
+ break;
+ default:
+ /* Should never fall here! */
+ ALOGE("Unsupported S3D layer type!");
+ break;
+ }
+
+ switch (hwc_dev->ext.s3d_type) {
+ case eSideBySide:
+ oc->win.w = oc->win.w/2;
+ if ((left_view && hwc_dev->ext.s3d_order == eRightViewFirst) ||
+ (!left_view && hwc_dev->ext.s3d_order == eLeftViewFirst)) {
+ oc->win.x = oc->win.x/2 + hwc_dev->ext.xres/2;
+ } else {
+ oc->win.x = oc->win.x/2;
+ }
+ break;
+ case eTopBottom:
+ oc->win.h = oc->win.h/2;
+ if ((left_view && hwc_dev->ext.s3d_order == eRightViewFirst) ||
+ (!left_view && hwc_dev->ext.s3d_order == eLeftViewFirst)) {
+ oc->win.y = oc->win.y/2 + hwc_dev->ext.yres/2;
+ } else {
+ oc->win.y = oc->win.y/2;
+ }
+ break;
+ default:
+ /* Currently unhandled!!! */
+ ALOGE("Unsupported S3D display type!");
+ break;
+ }
+}
+
+static int clone_s3d_external_layer(omap_hwc_device_t *hwc_dev, int ix_s3d)
+{
+ struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
+ int r;
+
+ /* S3D layers are forced into docking layers. If the display layout and
+ * the layer layout don't match, we have to use 2 overlay pipelines */
+ r = clone_external_layer(hwc_dev, ix_s3d);
+ if (r) {
+ ALOGE("Failed to clone s3d layer (%d)", r);
+ return r;
+ }
+
+ r = clone_layer(hwc_dev, ix_s3d);
+ if (r) {
+ ALOGE("Failed to clone s3d layer (%d)", r);
+ return r;
+ }
+
+ if (dsscomp->num_ovls < 2) {
+ ALOGE("Number of overlays is inconsistent (%d)", dsscomp->num_ovls);
+ return -EINVAL;
+ }
+
+ adjust_ext_s3d_layer(hwc_dev, &dsscomp->ovls[dsscomp->num_ovls - 1], true);
+ adjust_ext_s3d_layer(hwc_dev, &dsscomp->ovls[dsscomp->num_ovls - 2], false);
+
+ return 0;
+}
+#endif
+static int setup_mirroring(omap_hwc_device_t *hwc_dev)
+{
+ omap_hwc_ext_t *ext = &hwc_dev->ext;
+
+ uint32_t xres = WIDTH(ext->mirror_region);
+ uint32_t yres = HEIGHT(ext->mirror_region);
+ if (ext->current.rotation & 1)
+ swap(xres, yres);
+ if (set_best_hdmi_mode(hwc_dev, xres, yres, ext->lcd_xpy))
+ return -ENODEV;
+ set_ext_matrix(ext, ext->mirror_region);
+ return 0;
+}
+
+/*
+ * We're using "implicit" synchronization, so make sure we aren't passing any
+ * sync object descriptors around.
+ */
+static void check_sync_fds(size_t numDisplays, hwc_display_contents_1_t** displays)
+{
+ //ALOGD("checking sync FDs");
+ unsigned int i, j;
+ for (i = 0; i < numDisplays; i++) {
+ hwc_display_contents_1_t* list = displays[i];
+ if (list->retireFenceFd >= 0) {
+ ALOGW("retireFenceFd[%u] was %d", i, list->retireFenceFd);
+ list->retireFenceFd = -1;
+ }
+
+ for (j = 0; j < list->numHwLayers; j++) {
+ hwc_layer_1_t* layer = &list->hwLayers[j];
+ if (layer->acquireFenceFd >= 0) {
+ ALOGW("acquireFenceFd[%u][%u] was %d, closing", i, j, layer->acquireFenceFd);
+ close(layer->acquireFenceFd);
+ layer->acquireFenceFd = -1;
+ }
+ if (layer->releaseFenceFd >= 0) {
+ ALOGW("releaseFenceFd[%u][%u] was %d", i, j, layer->releaseFenceFd);
+ layer->releaseFenceFd = -1;
+ }
+ }
+ }
+}
+
+static void blit_reset(omap_hwc_device_t *hwc_dev)
+{
+ 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;
+}
+
+static bool blit_layers(omap_hwc_device_t *hwc_dev, hwc_display_contents_1_t *list, int bufoff)
+{
+ if (!list || hwc_dev->ext.mirror.enabled)
+ 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;
+ }
+
+ /*
+ * Request the layer identities to SurfaceFlinger, first figure out if the
+ * operation is supported
+ */
+ if (!(list->flags & HWC_EXTENDED_API) || !hwc_dev->procs ||
+ hwc_dev->procs->extension_cb(hwc_dev->procs, HWC_EXTENDED_OP_LAYERDATA, NULL, -1) != 0)
+ goto err_out;
+
+ /* Check if we have enough space in the extended layer list */
+ if ((sizeof(hwc_layer_extended_t) * list->numHwLayers) > sizeof(grgz_ext_layer_list))
+ goto err_out;
+
+ uint32_t i;
+ for (i = 0; i < list->numHwLayers; i++) {
+ hwc_layer_extended_t *ext_layer = &grgz_ext_layer_list.layers[i];
+ ext_layer->idx = i;
+ if (hwc_dev->procs->extension_cb(hwc_dev->procs, HWC_EXTENDED_OP_LAYERDATA,
+ (void **) &ext_layer, sizeof(hwc_layer_extended_t)) != 0)
+ goto err_out;
+ }
+
+ rgz_in_params_t in = {
+ .op = rgz_in_op,
+ .data = {
+ .hwc = {
+ .dstgeom = &gscrngeom,
+ .layers = list->hwLayers,
+ .extlayers = grgz_ext_layer_list.layers,
+ .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;
+
+ uint32_t 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_TRIPLE_BUFFER;
+ }
+ list->hwLayers[i].hints &= ~HWC_HINT_CLEAR_FB;
+ }
+ return true;
+
+err_out:
+ rgz_release(&grgz);
+ return false;
+}
+
+void debug_post2(omap_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 free_tiler2d_buffers(omap_hwc_device_t *hwc_dev)
+{
+ int i;
+
+ for (i = 0 ; i < NUM_EXT_DISPLAY_BACK_BUFFERS; i++) {
+ ion_free(hwc_dev->ion_fd, hwc_dev->ion_handles[i]);
+ hwc_dev->ion_handles[i] = NULL;
+ }
+ return 0;
+}
+
+static int allocate_tiler2d_buffers(omap_hwc_device_t *hwc_dev)
+{
+ int ret, i;
+ size_t stride;
+
+ if (hwc_dev->ion_fd < 0) {
+ ALOGE("No ion fd, hence can't allocate tiler2d buffers");
+ return -1;
+ }
+
+ for (i = 0; i < NUM_EXT_DISPLAY_BACK_BUFFERS; i++) {
+ if (hwc_dev->ion_handles[i])
+ return 0;
+ }
+
+ for (i = 0 ; i < NUM_EXT_DISPLAY_BACK_BUFFERS; i++) {
+ ret = ion_alloc_tiler(hwc_dev->ion_fd, hwc_dev->fb_dev->base.width, hwc_dev->fb_dev->base.height,
+ TILER_PIXEL_FMT_32BIT, 0, &hwc_dev->ion_handles[i], &stride);
+ if (ret)
+ goto handle_error;
+
+ ALOGI("ion handle[%d][%p]", i, hwc_dev->ion_handles[i]);
+ }
+ return 0;
+
+handle_error:
+ free_tiler2d_buffers(hwc_dev);
+ return -1;
+}
+
+static int hwc_prepare(struct hwc_composer_device_1 *dev, size_t numDisplays,
+ hwc_display_contents_1_t** displays)
+{
+ if (!numDisplays || displays == NULL) {
+ return 0;
+ }
+
+ hwc_display_contents_1_t* list = displays[0]; // ignore displays beyond the first
+ omap_hwc_device_t *hwc_dev = (omap_hwc_device_t *)dev;
+ struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
+ counts_t *num = &hwc_dev->counts;
+ uint32_t i, ix;
+
+ pthread_mutex_lock(&hwc_dev->lock);
+ memset(dsscomp, 0x0, sizeof(*dsscomp));
+ dsscomp->sync_id = sync_id++;
+
+ gather_layer_statistics(hwc_dev, list);
+
+ decide_supported_cloning(hwc_dev);
+
+ /* phase 3 logic */
+ if (can_dss_render_all(hwc_dev)) {
+ /* All layers can be handled by the DSS -- don't use SGX for composition */
+ hwc_dev->use_sgx = 0;
+ hwc_dev->swap_rb = num->BGR != 0;
+ } else {
+ /* Use SGX for composition plus first 3 layers that are DSS renderable */
+ hwc_dev->use_sgx = 1;
+ hwc_dev->swap_rb = is_BGR_format(hwc_dev->fb_dev->base.format);
+ }
+
+ /* setup pipes */
+ int z = 0;
+ int fb_z = -1;
+ int ix_docking = -1;
+#ifdef OMAP_ENHANCEMENT_S3D
+ int ix_s3d = -1;
+#endif
+ bool scaled_gfx = false;
+ bool blit_all = false;
+ blit_reset(hwc_dev);
+
+ /* If the SGX is used or we are going to blit something we need a framebuffer
+ * and a DSS pipe
+ */
+ bool 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;
+ /* No need to swap red and blue channels */
+ hwc_dev->swap_rb = 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 */
+ uint32_t mem_used = 0;
+ for (i = 0; list && i < list->numHwLayers && !blit_all; i++) {
+ hwc_layer_1_t *layer = &list->hwLayers[i];
+ IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
+
+ if (dsscomp->num_ovls < num->max_hw_overlays &&
+ can_dss_render_layer(hwc_dev, layer) &&
+ (!hwc_dev->force_sgx ||
+ /* render protected and dockable layers via DSS */
+ is_protected(layer) ||
+ is_upscaled_NV12(hwc_dev, layer) ||
+ (hwc_dev->ext.current.docking && hwc_dev->ext.current.enabled && dockable(layer))) &&
+ mem_used + mem1d(handle) <= limits.tiler1d_slot_size &&
+ /* can't have a transparent overlay in the middle of the framebuffer stack */
+ !(is_BLENDED(layer) && fb_z >= 0)) {
+
+ /* render via DSS overlay */
+ mem_used += mem1d(handle);
+ layer->compositionType = HWC_OVERLAY;
+ /*
+ * This hint will not be used in vanilla ICS, but maybe in
+ * JellyBean, it is useful to distinguish between blts and true
+ * overlays
+ */
+ layer->hints |= HWC_HINT_TRIPLE_BUFFER;
+
+ /* clear FB above all opaque layers if rendering via SGX */
+ if (hwc_dev->use_sgx && !is_BLENDED(layer))
+ layer->hints |= HWC_HINT_CLEAR_FB;
+
+ hwc_dev->buffers[dsscomp->num_ovls] = layer->handle;
+ //ALOGI("dss buffers[%d] = %p", dsscomp->num_ovls, hwc_dev->buffers[dsscomp->num_ovls]);
+
+ setup_layer(hwc_dev,
+ &dsscomp->ovls[dsscomp->num_ovls],
+ layer,
+ z,
+ handle->iFormat,
+ handle->iWidth,
+ handle->iHeight);
+
+ dsscomp->ovls[dsscomp->num_ovls].cfg.ix = dsscomp->num_ovls + hwc_dev->primary_transform;
+ dsscomp->ovls[dsscomp->num_ovls].addressing = OMAP_DSS_BUFADDR_LAYER_IX;
+ dsscomp->ovls[dsscomp->num_ovls].ba = dsscomp->num_ovls;
+
+ /* ensure GFX layer is never scaled */
+ if ((dsscomp->num_ovls == 0) && (!hwc_dev->primary_transform)) {
+ scaled_gfx = scaled(layer) || is_NV12(handle);
+ } else if (scaled_gfx && !scaled(layer) && !is_NV12(handle)) {
+ /* swap GFX layer with this one */
+ dsscomp->ovls[dsscomp->num_ovls].cfg.ix = 0;
+ dsscomp->ovls[0].cfg.ix = dsscomp->num_ovls;
+ scaled_gfx = 0;
+ }
+
+ /* remember largest dockable layer */
+ if (dockable(layer) &&
+ (ix_docking < 0 ||
+ display_area(&dsscomp->ovls[dsscomp->num_ovls]) > display_area(&dsscomp->ovls[ix_docking])))
+ ix_docking = dsscomp->num_ovls;
+#ifdef OMAP_ENHANCEMENT_S3D
+ /* remember the ix for s3d layer */
+ if (get_s3d_layout_type(layer) != eMono) {
+ ix_s3d = dsscomp->num_ovls;
+ }
+#endif
+ dsscomp->num_ovls++;
+ z++;
+ } else if (hwc_dev->use_sgx) {
+ if (fb_z < 0) {
+ /* NOTE: we are not handling transparent cutout for now */
+ fb_z = z;
+ z++;
+ } else {
+ /* move fb z-order up (by lowering dss layers) */
+ while (fb_z < z - 1)
+ dsscomp->ovls[1 + fb_z++].cfg.zorder--;
+ }
+ }
+ }
+
+ /* if scaling GFX (e.g. only 1 scaled surface) use a VID pipe */
+ if (scaled_gfx)
+ dsscomp->ovls[0].cfg.ix = dsscomp->num_ovls;
+
+ if (hwc_dev->blt_policy == BLTPOLICY_DEFAULT) {
+ /*
+ * As long as we keep blitting on consecutive frames keep the regionizer
+ * state, if this is not possible the regionizer state is unreliable and
+ * we need to reset its state.
+ */
+ if (hwc_dev->use_sgx) {
+ if (blit_layers(hwc_dev, list, dsscomp->num_ovls == 1 ? 0 : dsscomp->num_ovls)) {
+ hwc_dev->use_sgx = 0;
+ }
+ } else
+ rgz_release(&grgz);
+ }
+
+ /* 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 (!hwc_dev->blt_policy != BLTPOLICY_DISABLED && num->composited_layers)
+ ALOGE("**** should have assigned z-layer for fb");
+ fb_z = z++;
+ }
+ /*
+ * This is needed because if we blit all we would lose the handle of
+ * the first layer
+ */
+ if (hwc_dev->use_sgx) {
+ hwc_dev->buffers[0] = NULL;
+ }
+ setup_layer_base(&dsscomp->ovls[0].cfg, fb_z,
+ hwc_dev->fb_dev->base.format,
+ 1, /* FB is always premultiplied */
+ hwc_dev->fb_dev->base.width,
+ hwc_dev->fb_dev->base.height);
+ dsscomp->ovls[0].cfg.pre_mult_alpha = 1;
+ dsscomp->ovls[0].addressing = OMAP_DSS_BUFADDR_LAYER_IX;
+ dsscomp->ovls[0].ba = 0;
+ dsscomp->ovls[0].cfg.ix = hwc_dev->primary_transform;
+ }
+
+ /* mirror layers */
+ hwc_dev->post2_layers = dsscomp->num_ovls;
+
+ omap_hwc_ext_t *ext = &hwc_dev->ext;
+ if (ext->current.enabled && ((!num->protected && hwc_dev->ext_ovls) ||
+ (hwc_dev->ext_ovls_wanted && hwc_dev->ext_ovls >= hwc_dev->ext_ovls_wanted))) {
+#ifdef OMAP_ENHANCEMENT_S3D
+ if (ext->current.docking && ix_s3d >= 0) {
+ if (clone_s3d_external_layer(hwc_dev, ix_s3d) == 0) {
+ dsscomp->ovls[dsscomp->num_ovls - 2].cfg.zorder = z++;
+ dsscomp->ovls[dsscomp->num_ovls - 1].cfg.zorder = z++;
+ /* For now, show only the left view of an S3D layer
+ * in the local display while we have hdmi attached */
+ switch (hwc_dev->s3d_input_type) {
+ case eSideBySide:
+ dsscomp->ovls[ix_s3d].cfg.crop.w = dsscomp->ovls[ix_s3d].cfg.crop.w/2;
+ break;
+ case eTopBottom:
+ dsscomp->ovls[ix_s3d].cfg.crop.h = dsscomp->ovls[ix_s3d].cfg.crop.h/2;
+ break;
+ default:
+ ALOGE("Unsupported S3D input type");
+ break;
+ }
+ }
+ } else if (ext->current.docking && ix_docking >= 0) {
+#else
+ if (ext->current.docking && ix_docking >= 0) {
+#endif
+ if (clone_external_layer(hwc_dev, ix_docking) == 0)
+ dsscomp->ovls[dsscomp->num_ovls - 1].cfg.zorder = z++;
+ } else if (ext->current.docking && ix_docking < 0 && ext->force_dock) {
+ ix_docking = dsscomp->num_ovls;
+ struct dss2_ovl_info *oi = &dsscomp->ovls[ix_docking];
+ image_info_t *dock_image = get_dock_image();
+ setup_layer_base(&oi->cfg, 0, HAL_PIXEL_FORMAT_BGRA_8888, 1,
+ dock_image->width, dock_image->height);
+ oi->cfg.stride = dock_image->rowbytes;
+ if (clone_external_layer(hwc_dev, ix_docking) == 0) {
+ oi->addressing = OMAP_DSS_BUFADDR_FB;
+ oi->ba = 0;
+ z++;
+ }
+ } else if (!ext->current.docking) {
+ int res = 0;
+
+ /* reset mode if we are coming from docking */
+ if (ext->last.docking)
+ res = setup_mirroring(hwc_dev);
+
+ /* mirror all layers */
+ for (ix = 0; res == 0 && ix < hwc_dev->post2_layers; ix++) {
+ if (clone_layer(hwc_dev, ix))
+ break;
+ z++;
+ }
+ }
+ }
+
+ /* Apply transform for primary display */
+ if (hwc_dev->primary_transform)
+ for (i = 0; i < dsscomp->num_ovls; i++) {
+ if(dsscomp->ovls[i].cfg.mgr_ix == 0)
+ adjust_primary_display_layer(hwc_dev, &dsscomp->ovls[i]);
+ }
+
+#ifdef OMAP_ENHANCEMENT_S3D
+ enable_s3d_hdmi(hwc_dev, ix_s3d >= 0);
+#endif
+ ext->last = ext->current;
+
+ if (z != dsscomp->num_ovls || dsscomp->num_ovls > MAX_HW_OVERLAYS)
+ ALOGE("**** used %d z-layers for %d overlays\n", z, dsscomp->num_ovls);
+
+ /* verify all z-orders and overlay indices are distinct */
+ for (i = z = ix = 0; i < dsscomp->num_ovls; i++) {
+ struct dss2_ovl_cfg *c = &dsscomp->ovls[i].cfg;
+
+ if (z & (1 << c->zorder))
+ ALOGE("**** used z-order #%d multiple times", c->zorder);
+ if (ix & (1 << c->ix))
+ ALOGE("**** used ovl index #%d multiple times", c->ix);
+ z |= 1 << c->zorder;
+ ix |= 1 << c->ix;
+ }
+ dsscomp->mode = DSSCOMP_SETUP_DISPLAY;
+ dsscomp->mgrs[0].ix = 0;
+ dsscomp->mgrs[0].alpha_blending = 1;
+ dsscomp->mgrs[0].swap_rb = hwc_dev->swap_rb;
+ dsscomp->num_mgrs = 1;
+
+ if (ext->current.enabled || hwc_dev->last_ext_ovls) {
+ dsscomp->mgrs[1] = dsscomp->mgrs[0];
+ dsscomp->mgrs[1].ix = 1;
+ dsscomp->num_mgrs++;
+ hwc_dev->ext_ovls = dsscomp->num_ovls - hwc_dev->post2_layers;
+ }
+
+ /*
+ * Whilst the mode of the display is being changed drop compositions to the
+ * display
+ */
+ if (ext->last_mode == 0 && hwc_dev->on_tv) {
+ dsscomp->num_ovls = 0;
+ }
+
+ if (debug) {
+ ALOGD("prepare (%d) - %s (comp=%d, poss=%d/%d scaled, RGB=%d,BGR=%d,NV12=%d) (ext=%s%s%ddeg%s %dex/%dmx (last %dex,%din)\n",
+ dsscomp->sync_id,
+ hwc_dev->use_sgx ? "SGX+OVL" : "all-OVL",
+ num->composited_layers,
+ num->possible_overlay_layers, num->scaled_layers,
+ num->RGB, num->BGR, num->NV12,
+ ext->on_tv ? "tv+" : "",
+ ext->current.enabled ? ext->current.docking ? "dock+" : "mirror+" : "OFF+",
+ ext->current.rotation * 90,
+ ext->current.hflip ? "+hflip" : "",
+ hwc_dev->ext_ovls, num->max_hw_overlays, hwc_dev->last_ext_ovls, hwc_dev->last_int_ovls);
+ }
+
+ pthread_mutex_unlock(&hwc_dev->lock);
+ return 0;
+}
+
+static void reset_screen(omap_hwc_device_t *hwc_dev)
+{
+ static int first_set = 1;
+ int ret;
+
+ if (first_set) {
+ first_set = 0;
+ struct dsscomp_setup_dispc_data d = {
+ .num_mgrs = 1,
+ };
+ /* remove bootloader image from the screen as blank/unblank does not change the composition */
+ ret = ioctl(hwc_dev->dsscomp_fd, DSSCIOC_SETUP_DISPC, &d);
+ if (ret)
+ ALOGW("failed to remove bootloader image");
+
+ /* blank and unblank fd to make sure display is properly programmed on boot.
+ * This is needed because the bootloader can not be trusted.
+ */
+ ret = ioctl(hwc_dev->fb_fd, FBIOBLANK, FB_BLANK_POWERDOWN);
+ if (ret)
+ ALOGW("failed to blank display");
+
+ ret = ioctl(hwc_dev->fb_fd, FBIOBLANK, FB_BLANK_UNBLANK);
+ if (ret)
+ ALOGW("failed to blank display");
+ }
+}
+
+static int hwc_set(struct hwc_composer_device_1 *dev,
+ size_t numDisplays, hwc_display_contents_1_t** displays)
+{
+ if (!numDisplays || displays == NULL) {
+ ALOGD("set: empty display list");
+ return 0;
+ }
+ hwc_display_t dpy = NULL;
+ hwc_surface_t sur = NULL;
+ hwc_display_contents_1_t* list = displays[0]; // ignore displays beyond the first
+ if (list != NULL) {
+ dpy = list->dpy;
+ sur = list->sur;
+ }
+ omap_hwc_device_t *hwc_dev = (omap_hwc_device_t *)dev;
+ struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
+ int err = 0;
+ bool invalidate;
+
+ pthread_mutex_lock(&hwc_dev->lock);
+
+ reset_screen(hwc_dev);
+
+ invalidate = hwc_dev->ext_ovls_wanted && (hwc_dev->ext_ovls < hwc_dev->ext_ovls_wanted) &&
+ (hwc_dev->counts.protected || !hwc_dev->ext_ovls);
+
+ if (debug)
+ dump_set_info(hwc_dev, list);
+
+ if (dpy && sur) {
+ // list can be NULL which means hwc is temporarily disabled.
+ // however, if dpy and sur are null it means we're turning the
+ // screen off. no shall not call eglSwapBuffers() in that case.
+
+ if (hwc_dev->use_sgx) {
+ if (!eglSwapBuffers((EGLDisplay)dpy, (EGLSurface)sur)) {
+ ALOGE("eglSwapBuffers error");
+ err = HWC_EGL_ERROR;
+ goto err_out;
+ }
+ }
+
+ //dump_dsscomp(dsscomp);
+
+ // signal the event thread that a post has happened
+ write(hwc_dev->pipe_fds[1], "s", 1);
+ 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));
+
+
+ uint32_t 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) {
+ uint32_t 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,
+ nbufs,
+ dsscomp, omaplfb_comp_data_sz);
+ showfps();
+ }
+ hwc_dev->last_ext_ovls = hwc_dev->ext_ovls;
+ hwc_dev->last_int_ovls = hwc_dev->post2_layers;
+ if (err)
+ ALOGE("Post2 error");
+
+ check_sync_fds(numDisplays, displays);
+
+err_out:
+ pthread_mutex_unlock(&hwc_dev->lock);
+
+ if (invalidate)
+ hwc_dev->procs->invalidate(hwc_dev->procs);
+
+ return err;
+}
+
+static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff, int buff_len)
+{
+ omap_hwc_device_t *hwc_dev = (omap_hwc_device_t *)dev;
+ struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
+ struct dump_buf log = {
+ .buf = buff,
+ .buf_len = buff_len,
+ };
+ int i;
+
+ dump_printf(&log, "omap_hwc %d:\n", dsscomp->num_ovls);
+ dump_printf(&log, " idle timeout: %dms\n", hwc_dev->idle);
+
+ for (i = 0; i < dsscomp->num_ovls; i++) {
+ struct dss2_ovl_cfg *cfg = &dsscomp->ovls[i].cfg;
+
+ dump_printf(&log, " layer %d:\n", i);
+ dump_printf(&log, " enabled:%s buff:%p %dx%d stride:%d\n",
+ cfg->enabled ? "true" : "false", hwc_dev->buffers[i],
+ cfg->width, cfg->height, cfg->stride);
+ dump_printf(&log, " src:(%d,%d) %dx%d dst:(%d,%d) %dx%d ix:%d zorder:%d\n",
+ cfg->crop.x, cfg->crop.y, cfg->crop.w, cfg->crop.h,
+ cfg->win.x, cfg->win.y, cfg->win.w, cfg->win.h,
+ cfg->ix, 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 int hwc_device_close(hw_device_t* device)
+{
+ omap_hwc_device_t *hwc_dev = (omap_hwc_device_t *) device;;
+
+ if (hwc_dev) {
+ if (hwc_dev->dsscomp_fd >= 0)
+ close(hwc_dev->dsscomp_fd);
+ if (hwc_dev->hdmi_fb_fd >= 0)
+ close(hwc_dev->hdmi_fb_fd);
+ if (hwc_dev->fb_fd >= 0)
+ close(hwc_dev->fb_fd);
+ if (hwc_dev->ion_fd >= 0)
+ ion_close(hwc_dev->ion_fd);
+
+ /* pthread will get killed when parent process exits */
+ pthread_mutex_destroy(&hwc_dev->lock);
+ free(hwc_dev);
+ }
+
+ return 0;
+}
+
+static int open_fb_hal(IMG_framebuffer_device_public_t **fb_dev)
+{
+ const struct hw_module_t *psModule;
+ IMG_gralloc_module_public_t *psGrallocModule;
+ int err;
+
+ err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &psModule);
+ psGrallocModule = (IMG_gralloc_module_public_t *) psModule;
+
+ if(err)
+ goto err_out;
+
+ if (strcmp(psGrallocModule->base.common.author, "Imagination Technologies")) {
+ err = -EINVAL;
+ goto err_out;
+ }
+
+ *fb_dev = psGrallocModule->psFrameBufferDevice;
+
+ return 0;
+
+err_out:
+ ALOGE("Composer HAL failed to load compatible Graphics HAL");
+ return err;
+}
+
+static void set_primary_display_transform_matrix(omap_hwc_device_t *hwc_dev)
+{
+ /* create primary display translation matrix */
+ hwc_dev->fb_dis.ix = 0;/*Default display*/
+
+ int ret = ioctl(hwc_dev->dsscomp_fd, DSSCIOC_QUERY_DISPLAY, &hwc_dev->fb_dis);
+ if (ret)
+ ALOGE("failed to get display info (%d): %m", errno);
+
+ int lcd_w = hwc_dev->fb_dis.timings.x_res;
+ int lcd_h = hwc_dev->fb_dis.timings.y_res;
+ int orig_w = hwc_dev->fb_dev->base.width;
+ int orig_h = hwc_dev->fb_dev->base.height;
+ hwc_rect_t region = {.left = 0, .top = 0, .right = orig_w, .bottom = orig_h};
+ hwc_dev->primary_region = region;
+ hwc_dev->primary_rotation = ((lcd_w > lcd_h) ^ (orig_w > orig_h)) ? 1 : 0;
+ hwc_dev->primary_transform = ((lcd_w != orig_w)||(lcd_h != orig_h)) ? 1 : 0;
+
+ ALOGI("transforming FB (%dx%d) => (%dx%d) rot%d", orig_w, orig_h, lcd_w, lcd_h, hwc_dev->primary_rotation);
+
+ /* reorientation matrix is:
+ m = (center-from-target-center) * (scale-to-target) * (mirror) * (rotate) * (center-to-original-center) */
+
+ memcpy(hwc_dev->primary_m, m_unit, sizeof(m_unit));
+ m_translate(hwc_dev->primary_m, -(orig_w >> 1), -(orig_h >> 1));
+ m_rotate(hwc_dev->primary_m, hwc_dev->primary_rotation);
+ if (hwc_dev->primary_rotation & 1)
+ swap(orig_w, orig_h);
+ m_scale(hwc_dev->primary_m, orig_w, lcd_w, orig_h, lcd_h);
+ m_translate(hwc_dev->primary_m, lcd_w >> 1, lcd_h >> 1);
+}
+
+#ifdef OMAP_ENHANCEMENT_S3D
+static void handle_s3d_hotplug(omap_hwc_ext_t *ext, bool state)
+{
+ struct edid_t *edid = NULL;
+ if (state) {
+ int fd = open("/sys/devices/platform/omapdss/display1/edid", O_RDONLY);
+ if (!fd)
+ return;
+ uint8_t edid_data[EDID_SIZE];
+ size_t bytes_read = read(fd, edid_data, EDID_SIZE);
+ close(fd);
+ if (bytes_read < EDID_SIZE)
+ return;
+ if (edid_parser_init(&edid, edid_data))
+ return;
+ }
+
+ ext->s3d_enabled = false;
+ ext->s3d_capable = false;
+ ext->s3d_type = eMono;
+ ext->s3d_order = eLeftViewFirst;
+
+ if (edid) {
+ ext->s3d_capable = edid_s3d_capable(edid);
+ /* For now assume Side-by-Side half support applies to all modes */
+ ext->s3d_type = eSideBySide;
+ ext->s3d_order = eLeftViewFirst;
+ edid_parser_deinit(edid);
+ }
+}
+#endif
+static void handle_hotplug(omap_hwc_device_t *hwc_dev)
+{
+ omap_hwc_ext_t *ext = &hwc_dev->ext;
+ bool state = ext->hdmi_state;
+
+ /* Ignore external HDMI logic if the primary display is HDMI */
+ if (hwc_dev->on_tv) {
+ ALOGI("Primary display is HDMI - skip clone/dock logic");
+
+ if (state) {
+ uint32_t xres = hwc_dev->fb_dev->base.width;
+ uint32_t yres = hwc_dev->fb_dev->base.height;
+ if (set_best_hdmi_mode(hwc_dev, xres, yres, ext->lcd_xpy)) {
+ ALOGE("Failed to set HDMI mode");
+ }
+ set_primary_display_transform_matrix(hwc_dev);
+
+ ioctl(hwc_dev->fb_fd, FBIOBLANK, FB_BLANK_UNBLANK);
+
+ if (hwc_dev->procs && hwc_dev->procs->invalidate) {
+ hwc_dev->procs->invalidate(hwc_dev->procs);
+ }
+ } else
+ ext->last_mode = 0;
+
+ return;
+ }
+
+ pthread_mutex_lock(&hwc_dev->lock);
+#ifdef OMAP_ENHANCEMENT_S3D
+ handle_s3d_hotplug(ext, state);
+#endif
+ ext->dock.enabled = ext->mirror.enabled = 0;
+ if (state) {
+ /* check whether we can clone and/or dock */
+ char value[PROPERTY_VALUE_MAX];
+ property_get("persist.hwc.docking.enabled", value, "1");
+ ext->dock.enabled = atoi(value) > 0;
+ property_get("persist.hwc.mirroring.enabled", value, "1");
+ ext->mirror.enabled = atoi(value) > 0;
+ property_get("persist.hwc.avoid_mode_change", value, "1");
+ ext->avoid_mode_change = atoi(value) > 0;
+
+ /* get cloning transformation */
+ property_get("persist.hwc.docking.transform", value, "0");
+ ext->dock.rotation = atoi(value) & EXT_ROTATION;
+ ext->dock.hflip = (atoi(value) & EXT_HFLIP) > 0;
+ ext->dock.docking = 1;
+ property_get("persist.hwc.mirroring.transform", value, hwc_dev->fb_dis.timings.y_res > hwc_dev->fb_dis.timings.x_res ? "3" : "0");
+ ext->mirror.rotation = atoi(value) & EXT_ROTATION;
+ ext->mirror.hflip = (atoi(value) & EXT_HFLIP) > 0;
+ ext->mirror.docking = 0;
+
+ if (ext->force_dock) {
+ /* restrict to docking with no transform */
+ ext->mirror.enabled = 0;
+ ext->dock.rotation = 0;
+ ext->dock.hflip = 0;
+
+ load_dock_image();
+ }
+
+ /* select best mode for mirroring */
+ if (ext->mirror.enabled) {
+ ext->current = ext->mirror;
+ ext->mirror_mode = 0;
+ if (setup_mirroring(hwc_dev) == 0) {
+ ext->mirror_mode = ext->last_mode;
+ ioctl(hwc_dev->hdmi_fb_fd, FBIOBLANK, FB_BLANK_UNBLANK);
+ } else
+ ext->mirror.enabled = 0;
+ }
+ /* Allocate backup buffers for FB rotation
+ * This is required only if the FB tranform is different from that
+ * of the external display and the FB is not in TILER2D space
+ */
+ if (ext->mirror.rotation && (limits.fbmem_type != DSSCOMP_FBMEM_TILER2D))
+ allocate_tiler2d_buffers(hwc_dev);
+
+ } else {
+ ext->last_mode = 0;
+ if (ext->mirror.rotation && (limits.fbmem_type != DSSCOMP_FBMEM_TILER2D)) {
+ /* free tiler 2D buffer on detach */
+ free_tiler2d_buffers(hwc_dev);
+ }
+ }
+ ALOGI("external display changed (state=%d, mirror={%s tform=%ddeg%s}, dock={%s tform=%ddeg%s%s}, tv=%d", state,
+ ext->mirror.enabled ? "enabled" : "disabled",
+ ext->mirror.rotation * 90,
+ ext->mirror.hflip ? "+hflip" : "",
+ ext->dock.enabled ? "enabled" : "disabled",
+ ext->dock.rotation * 90,
+ ext->dock.hflip ? "+hflip" : "",
+ ext->force_dock ? " forced" : "",
+ ext->on_tv);
+
+ pthread_mutex_unlock(&hwc_dev->lock);
+
+ /* hwc_dev->procs is set right after the device is opened, but there is
+ * still a race condition where a hotplug event might occur after the open
+ * but before the procs are registered. */
+ if (hwc_dev->procs)
+ hwc_dev->procs->invalidate(hwc_dev->procs);
+}
+
+static void handle_uevents(omap_hwc_device_t *hwc_dev, const char *buff, int len)
+{
+ int dock;
+ int hdmi;
+ int vsync;
+ int state = 0;
+ uint64_t timestamp = 0;
+ const char *s = buff;
+
+ dock = !strcmp(s, "change@/devices/virtual/switch/dock");
+ hdmi = !strcmp(s, "change@/devices/virtual/switch/hdmi");
+ vsync = !strcmp(s, "change@/devices/platform/omapfb") ||
+ !strcmp(s, "change@/devices/virtual/switch/omapfb-vsync");
+
+ if (!dock && !vsync && !hdmi)
+ return;
+
+ s += strlen(s) + 1;
+
+ while(*s) {
+ if (!strncmp(s, "SWITCH_STATE=", strlen("SWITCH_STATE=")))
+ state = atoi(s + strlen("SWITCH_STATE="));
+ else if (!strncmp(s, "SWITCH_TIME=", strlen("SWITCH_TIME=")))
+ timestamp = strtoull(s + strlen("SWITCH_TIME="), NULL, 0);
+ else if (!strncmp(s, "VSYNC=", strlen("VSYNC=")))
+ timestamp = strtoull(s + strlen("VSYNC="), NULL, 0);
+
+ s += strlen(s) + 1;
+ if (s - buff >= len)
+ break;
+ }
+
+ if (vsync) {
+ if (hwc_dev->procs)
+ hwc_dev->procs->vsync(hwc_dev->procs, 0, timestamp);
+ } else {
+ if (dock)
+ hwc_dev->ext.force_dock = state == 1;
+ else
+ hwc_dev->ext.hdmi_state = state == 1;
+ handle_hotplug(hwc_dev);
+ }
+}
+
+#ifdef SYSFS_VSYNC_NOTIFICATION
+static void *omap4_hwc_vsync_sysfs_loop(void *data)
+{
+ omap_hwc_device_t *hwc_dev = data;
+ static char buf[4096];
+ int vsync_timestamp_fd;
+ fd_set exceptfds;
+ int res;
+ int64_t timestamp = 0;
+
+ vsync_timestamp_fd = open("/sys/devices/platform/omapfb/vsync_time", O_RDONLY);
+ char thread_name[64] = "hwcVsyncThread";
+ prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
+ setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
+ memset(buf, 0, sizeof(buf));
+
+ ALOGD("Using sysfs mechanism for VSYNC notification");
+
+ FD_ZERO(&exceptfds);
+ FD_SET(vsync_timestamp_fd, &exceptfds);
+ do {
+ ssize_t len = read(vsync_timestamp_fd, buf, sizeof(buf));
+ timestamp = strtoull(buf, NULL, 0);
+ hwc_dev->procs->vsync(hwc_dev->procs, 0, timestamp);
+ select(vsync_timestamp_fd + 1, NULL, NULL, &exceptfds, NULL);
+ lseek(vsync_timestamp_fd, 0, SEEK_SET);
+ } while (1);
+
+ return NULL;
+}
+#endif
+
+static void *hdmi_thread(void *data)
+{
+ omap_hwc_device_t *hwc_dev = data;
+ static char uevent_desc[4096];
+ struct pollfd fds[2];
+ bool invalidate = false;
+ int timeout;
+ int err;
+
+ setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
+
+ uevent_init();
+
+ fds[0].fd = uevent_get_fd();
+ fds[0].events = POLLIN;
+ fds[1].fd = hwc_dev->pipe_fds[0];
+ fds[1].events = POLLIN;
+
+ timeout = hwc_dev->idle ? hwc_dev->idle : -1;
+
+ memset(uevent_desc, 0, sizeof(uevent_desc));
+
+ do {
+ err = poll(fds, hwc_dev->idle ? 2 : 1, timeout);
+
+ if (err == 0) {
+ if (hwc_dev->idle) {
+ if (hwc_dev->procs) {
+ pthread_mutex_lock(&hwc_dev->lock);
+ invalidate = hwc_dev->last_int_ovls > 1 && !hwc_dev->force_sgx;
+ if (invalidate) {
+ hwc_dev->force_sgx = 2;
+ }
+ pthread_mutex_unlock(&hwc_dev->lock);
+
+ if (invalidate) {
+ hwc_dev->procs->invalidate(hwc_dev->procs);
+ timeout = -1;
+ }
+ }
+
+ continue;
+ }
+ }
+
+ if (err == -1) {
+ if (errno != EINTR)
+ ALOGE("event error: %m");
+ continue;
+ }
+
+ if (hwc_dev->idle && fds[1].revents & POLLIN) {
+ char c;
+ read(hwc_dev->pipe_fds[0], &c, 1);
+ if (!hwc_dev->force_sgx)
+ timeout = hwc_dev->idle ? hwc_dev->idle : -1;
+ }
+
+ if (fds[0].revents & POLLIN) {
+ /* keep last 2 zeroes to ensure double 0 termination */
+ int len = uevent_next_event(uevent_desc, sizeof(uevent_desc) - 2);
+ handle_uevents(hwc_dev, uevent_desc, len);
+ }
+ } while (1);
+
+ return NULL;
+}
+
+static void hwc_registerProcs(struct hwc_composer_device_1* dev,
+ hwc_procs_t const* procs)
+{
+ omap_hwc_device_t *hwc_dev = (omap_hwc_device_t *) dev;
+
+ hwc_dev->procs = (typeof(hwc_dev->procs)) procs;
+}
+
+static int hwc_query(struct hwc_composer_device_1* dev, int what, int* value)
+{
+ omap_hwc_device_t *hwc_dev = (omap_hwc_device_t *) dev;
+
+ switch (what) {
+ case HWC_BACKGROUND_LAYER_SUPPORTED:
+ // we don't support the background layer yet
+ value[0] = 0;
+ break;
+ case HWC_VSYNC_PERIOD:
+ // vsync period in nanosecond
+ value[0] = 1000000000.0 / hwc_dev->fb_dev->base.fps;
+ break;
+ default:
+ // unsupported query
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static int hwc_eventControl(struct hwc_composer_device_1* dev,
+ int dpy, int event, int enabled)
+{
+ omap_hwc_device_t *hwc_dev = (omap_hwc_device_t *) dev;
+
+ switch (event) {
+ case HWC_EVENT_VSYNC:
+ {
+ int val = !!enabled;
+ int err;
+
+ if (hwc_dev->use_sw_vsync) {
+ if (enabled)
+ start_sw_vsync(hwc_dev);
+ else
+ stop_sw_vsync();
+ return 0;
+ }
+
+ err = ioctl(hwc_dev->fb_fd, OMAPFB_ENABLEVSYNC, &val);
+ if (err < 0)
+ return -errno;
+
+ return 0;
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+static int hwc_blank(struct hwc_composer_device_1 *dev, int dpy, int blank)
+{
+ // We're using an older method of screen blanking based on
+ // early_suspend in the kernel. No need to do anything here.
+ return 0;
+}
+
+static int hwc_device_open(const hw_module_t* module, const char* name, hw_device_t** device)
+{
+ omap_hwc_module_t *hwc_mod = (omap_hwc_module_t *)module;
+ omap_hwc_device_t *hwc_dev;
+ int err = 0;
+
+ if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
+ return -EINVAL;
+ }
+
+ if (!hwc_mod->fb_dev) {
+ err = open_fb_hal(&hwc_mod->fb_dev);
+ if (err)
+ return err;
+
+ if (!hwc_mod->fb_dev) {
+ ALOGE("Framebuffer HAL not opened before HWC");
+ return -EFAULT;
+ }
+ hwc_mod->fb_dev->bBypassPost = 1;
+ }
+
+ hwc_dev = (omap_hwc_device_t *)malloc(sizeof(*hwc_dev));
+ if (hwc_dev == NULL)
+ return -ENOMEM;
+
+ memset(hwc_dev, 0, sizeof(*hwc_dev));
+
+ hwc_dev->base.common.tag = HARDWARE_DEVICE_TAG;
+ hwc_dev->base.common.version = HWC_DEVICE_API_VERSION_1_0;
+
+ if (use_sw_vsync()) {
+ hwc_dev->use_sw_vsync = true;
+ init_sw_vsync(hwc_dev);
+ }
+
+ hwc_dev->base.common.module = (hw_module_t *)module;
+ hwc_dev->base.common.close = hwc_device_close;
+ hwc_dev->base.prepare = hwc_prepare;
+ hwc_dev->base.set = hwc_set;
+ hwc_dev->base.eventControl = hwc_eventControl;
+ hwc_dev->base.blank = hwc_blank;
+ hwc_dev->base.dump = hwc_dump;
+ hwc_dev->base.registerProcs = hwc_registerProcs;
+ hwc_dev->base.query = hwc_query;
+
+ hwc_dev->fb_dev = hwc_mod->fb_dev;
+ *device = &hwc_dev->base.common;
+
+ hwc_dev->dsscomp_fd = open("/dev/dsscomp", O_RDWR);
+ if (hwc_dev->dsscomp_fd < 0) {
+ ALOGE("failed to open dsscomp (%d)", errno);
+ err = -errno;
+ goto done;
+ }
+
+ int ret = ioctl(hwc_dev->dsscomp_fd, DSSCIOC_QUERY_PLATFORM, &limits);
+ if (ret) {
+ ALOGE("failed to get platform limits (%d): %m", errno);
+ err = -errno;
+ goto done;
+ }
+
+ hwc_dev->fb_fd = open("/dev/graphics/fb0", O_RDWR);
+ if (hwc_dev->fb_fd < 0) {
+ ALOGE("failed to open fb (%d)", errno);
+ err = -errno;
+ goto done;
+ }
+
+ err = init_dock_image(hwc_dev, limits.max_width, limits.max_height);
+ if (err)
+ goto done;
+
+ /* 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;
+ }
+
+ ret = ioctl(hwc_dev->dsscomp_fd, DSSCIOC_QUERY_DISPLAY, &hwc_dev->fb_dis);
+ if (ret) {
+ ALOGE("failed to get display info (%d): %m", errno);
+ err = -errno;
+ goto done;
+ }
+
+ hwc_dev->ion_fd = ion_open();
+ if (hwc_dev->ion_fd < 0) {
+ ALOGE("failed to open ion driver (%d)", errno);
+ }
+
+ int i;
+ for (i = 0; i < NUM_EXT_DISPLAY_BACK_BUFFERS; i++) {
+ hwc_dev->ion_handles[i] = NULL;
+ }
+
+ /* use default value in case some of requested display parameters missing */
+ hwc_dev->ext.lcd_xpy = 1.0;
+ if (hwc_dev->fb_dis.timings.x_res && hwc_dev->fb_dis.height_in_mm) {
+ hwc_dev->ext.lcd_xpy = (float)
+ hwc_dev->fb_dis.width_in_mm / hwc_dev->fb_dis.timings.x_res /
+ hwc_dev->fb_dis.height_in_mm * hwc_dev->fb_dis.timings.y_res;
+ }
+
+ if (hwc_dev->fb_dis.channel == OMAP_DSS_CHANNEL_DIGIT) {
+ ALOGI("Primary display is HDMI");
+ hwc_dev->on_tv = 1;
+ } else {
+ hwc_dev->hdmi_fb_fd = open("/dev/graphics/fb1", O_RDWR);
+ if (hwc_dev->hdmi_fb_fd < 0) {
+ ALOGE("failed to open hdmi fb (%d)", errno);
+ err = -errno;
+ goto done;
+ }
+ }
+
+ set_primary_display_transform_matrix(hwc_dev);
+
+ if (pipe(hwc_dev->pipe_fds) == -1) {
+ ALOGE("failed to event pipe (%d): %m", errno);
+ err = -errno;
+ goto done;
+ }
+
+ if (pthread_mutex_init(&hwc_dev->lock, NULL)) {
+ ALOGE("failed to create mutex (%d): %m", errno);
+ err = -errno;
+ goto done;
+ }
+
+ if (pthread_create(&hwc_dev->hdmi_thread, NULL, hdmi_thread, hwc_dev))
+ {
+ ALOGE("failed to create HDMI listening thread (%d): %m", errno);
+ err = -errno;
+ goto done;
+ }
+
+ /* get debug properties */
+
+ /* see if hwc is enabled at all */
+ char value[PROPERTY_VALUE_MAX];
+ property_get("debug.hwc.rgb_order", value, "1");
+ hwc_dev->flags_rgb_order = atoi(value);
+ property_get("debug.hwc.nv12_only", value, "0");
+ hwc_dev->flags_nv12_only = atoi(value);
+ property_get("debug.hwc.idle", value, "250");
+ hwc_dev->idle = atoi(value);
+
+ /* get the board specific clone properties */
+ /* 0:0:1280:720 */
+ if (property_get("persist.hwc.mirroring.region", value, "") <= 0 ||
+ sscanf(value, "%d:%d:%d:%d",
+ &hwc_dev->ext.mirror_region.left, &hwc_dev->ext.mirror_region.top,
+ &hwc_dev->ext.mirror_region.right, &hwc_dev->ext.mirror_region.bottom) != 4 ||
+ hwc_dev->ext.mirror_region.left >= hwc_dev->ext.mirror_region.right ||
+ hwc_dev->ext.mirror_region.top >= hwc_dev->ext.mirror_region.bottom) {
+ struct hwc_rect fb_region = { .right = hwc_dev->fb_dev->base.width, .bottom = hwc_dev->fb_dev->base.height };
+ hwc_dev->ext.mirror_region = fb_region;
+ }
+ ALOGI("clone region is set to (%d,%d) to (%d,%d)",
+ hwc_dev->ext.mirror_region.left, hwc_dev->ext.mirror_region.top,
+ hwc_dev->ext.mirror_region.right, hwc_dev->ext.mirror_region.bottom);
+
+ /* read switch state */
+ int sw_fd = open("/sys/class/switch/hdmi/state", O_RDONLY);
+ if (sw_fd >= 0) {
+ char value;
+ if (read(sw_fd, &value, 1) == 1)
+ hwc_dev->ext.hdmi_state = value == '1';
+ close(sw_fd);
+ }
+ sw_fd = open("/sys/class/switch/dock/state", O_RDONLY);
+ if (sw_fd >= 0) {
+ char value;
+ if (read(sw_fd, &value, 1) == 1)
+ hwc_dev->ext.force_dock = value == '1';
+ close(sw_fd);
+ }
+ handle_hotplug(hwc_dev);
+
+#ifdef SYSFS_VSYNC_NOTIFICATION
+ if (pthread_create(&hwc_dev->vsync_thread, NULL, omap4_hwc_vsync_sysfs_loop, hwc_dev))
+ {
+ ALOGE("pthread_create() failed (%d): %m", errno);
+ err = -errno;
+ goto done;
+ }
+#endif
+
+ ALOGI("open_device(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;
+ }
+ }
+
+ property_get("persist.hwc.upscaled_nv12_limit", value, "2.");
+ sscanf(value, "%f", &hwc_dev->upscaled_nv12_limit);
+ if (hwc_dev->upscaled_nv12_limit < 0. || hwc_dev->upscaled_nv12_limit > 2048.) {
+ ALOGW("Invalid upscaled_nv12_limit (%s), setting to 2.", value);
+ hwc_dev->upscaled_nv12_limit = 2.;
+ }
+
+done:
+ if (err && hwc_dev) {
+ if (hwc_dev->dsscomp_fd >= 0)
+ close(hwc_dev->dsscomp_fd);
+ if (hwc_dev->hdmi_fb_fd >= 0)
+ close(hwc_dev->hdmi_fb_fd);
+ if (hwc_dev->fb_fd >= 0)
+ close(hwc_dev->fb_fd);
+ pthread_mutex_destroy(&hwc_dev->lock);
+ free(hwc_dev->buffers);
+ free(hwc_dev);
+ }
+
+ return err;
+}
+
+static struct hw_module_methods_t module_methods = {
+ .open = hwc_device_open,
+};
+
+omap_hwc_module_t HAL_MODULE_INFO_SYM = {
+ .base = {
+ .common = {
+ .tag = HARDWARE_MODULE_TAG,
+ .module_api_version = HWC_MODULE_API_VERSION_0_1,
+ .hal_api_version = HARDWARE_HAL_API_VERSION,
+ .id = HWC_HARDWARE_MODULE_ID,
+ .name = "OMAP 44xx Hardware Composer HAL",
+ .author = "Texas Instruments",
+ .methods = &module_methods,
+ },
+ },
+};
diff --git a/hwc/hwc_dev.h b/hwc/hwc_dev.h
new file mode 100644
index 0000000..32cc273
--- /dev/null
+++ b/hwc/hwc_dev.h
@@ -0,0 +1,180 @@
+/*
+ * 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 __HWC_DEV__
+#define __HWC_DEV__
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include <hardware/hwcomposer.h>
+#ifdef OMAP_ENHANCEMENT_S3D
+#include <ui/S3DFormat.h>
+#endif
+
+#include <linux/bltsville.h>
+#include <video/dsscomp.h>
+#include <video/omap_hwc.h>
+
+#include "hal_public.h"
+#include "rgz_2d.h"
+
+struct ext_transform {
+ uint8_t rotation : 3; /* 90-degree clockwise rotations */
+ uint8_t hflip : 1; /* flip l-r (after rotation) */
+ uint8_t enabled : 1; /* cloning enabled */
+ uint8_t docking : 1; /* docking vs. mirroring - used for state */
+};
+typedef struct ext_transform ext_transform_t;
+
+/* cloning support and state */
+struct omap_hwc_ext {
+ /* support */
+ ext_transform_t mirror; /* mirroring settings */
+ ext_transform_t dock; /* docking settings */
+ float lcd_xpy; /* pixel ratio for UI */
+ bool avoid_mode_change; /* use HDMI mode used for mirroring if possible */
+ bool force_dock; /* must dock */
+
+ /* state */
+ bool hdmi_state; /* whether HDMI is connected */
+ bool on_tv; /* using a tv */
+ ext_transform_t current; /* current settings */
+ ext_transform_t last; /* last-used settings */
+
+ /* configuration */
+ uint32_t last_xres_used; /* resolution and pixel ratio used for mode selection */
+ uint32_t last_yres_used;
+ uint32_t last_mode; /* 2-s complement of last HDMI mode set, 0 if none */
+ uint32_t mirror_mode; /* 2-s complement of mode used when mirroring */
+ float last_xpy;
+ uint16_t width; /* external screen dimensions */
+ uint16_t height;
+ uint32_t xres; /* external screen resolution */
+ uint32_t yres;
+ float m[2][3]; /* external transformation matrix */
+ hwc_rect_t mirror_region; /* region of screen to mirror */
+#ifdef OMAP_ENHANCEMENT_S3D
+ bool s3d_enabled;
+ bool s3d_capable;
+ enum S3DLayoutType s3d_type;
+ enum S3DLayoutOrder s3d_order;
+#endif
+};
+typedef struct omap_hwc_ext omap_hwc_ext_t;
+
+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 */
+};
+
+struct omap_hwc_module {
+ hwc_module_t base;
+
+ IMG_framebuffer_device_public_t *fb_dev;
+};
+typedef struct omap_hwc_module omap_hwc_module_t;
+
+struct counts {
+ uint32_t possible_overlay_layers;
+ uint32_t composited_layers;
+ uint32_t scaled_layers;
+ uint32_t RGB;
+ uint32_t BGR;
+ uint32_t NV12;
+ uint32_t dockable;
+ uint32_t protected;
+#ifdef OMAP_ENHANCEMENT_S3D
+ uint32_t s3d;
+#endif
+
+ uint32_t max_hw_overlays;
+ uint32_t max_scaling_overlays;
+ uint32_t mem;
+};
+typedef struct counts counts_t;
+
+struct omap_hwc_device {
+ /* static data */
+ hwc_composer_device_1_t base;
+ hwc_procs_t *procs;
+ pthread_t hdmi_thread;
+#ifdef SYSFS_VSYNC_NOTIFICATION
+ pthread_t vsync_thread;
+#endif
+ pthread_mutex_t lock;
+
+ IMG_framebuffer_device_public_t *fb_dev;
+ struct dsscomp_display_info fb_dis;
+ int fb_fd; /* file descriptor for /dev/fb0 */
+ int dsscomp_fd; /* file descriptor for /dev/dsscomp */
+ int hdmi_fb_fd; /* file descriptor for /dev/fb1 */
+ int pipe_fds[2]; /* pipe to event thread */
+
+ int img_mem_size; /* size of fb for hdmi */
+ void *img_mem_ptr; /* start of fb for hdmi */
+
+ int flags_rgb_order;
+ int flags_nv12_only;
+ float upscaled_nv12_limit;
+
+ bool on_tv; /* using a tv */
+ int force_sgx;
+ omap_hwc_ext_t ext; /* external mirroring data */
+ int idle;
+
+ float primary_m[2][3]; /* internal transformation matrix */
+ int primary_transform;
+ int primary_rotation;
+ hwc_rect_t primary_region;
+
+ buffer_handle_t *buffers;
+ bool use_sgx;
+ bool swap_rb;
+ uint32_t post2_layers; /* buffers used with DSS pipes*/
+ uint32_t 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;
+#ifdef OMAP_ENHANCEMENT_S3D
+ enum S3DLayoutType s3d_input_type;
+ enum S3DLayoutOrder s3d_input_order;
+#endif
+ enum bltmode blt_mode;
+ enum bltpolicy blt_policy;
+
+ uint32_t 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];
+
+ counts_t counts;
+
+ int ion_fd;
+ struct ion_handle *ion_handles[2];
+ bool use_sw_vsync;
+
+};
+typedef struct omap_hwc_device omap_hwc_device_t;
+
+#endif
diff --git a/hwc/rgz_2d.c b/hwc/rgz_2d.c
new file mode 100644
index 0000000..ac4be96
--- /dev/null
+++ b/hwc/rgz_2d.c
@@ -0,0 +1,1997 @@
+/*
+ * 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>
+
+#include <cutils/log.h>
+#include <cutils/properties.h>
+#include <hardware/hwcomposer.h>
+
+#include "hwc_dev.h"
+
+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)
+#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 */
+#define OUTP ALOGI
+#define OUTE ALOGE
+
+#define IS_BVCMD(params) (params->op == RGZ_OUT_BVCMD_REGION || params->op == RGZ_OUT_BVCMD_PAINT)
+
+#define RECT_INTERSECTS(a, b) (((a).bottom > (b).top) && ((a).top < (b).bottom) && ((a).right > (b).left) && ((a).left < (b).right))
+
+/* Buffer indexes used to distinguish background and layers with the clear fb hint */
+#define RGZ_BACKGROUND_BUFFIDX -2
+#define RGZ_CLEARHINT_BUFFIDX -1
+
+struct rgz_blts {
+ struct rgz_blt_entry bvcmds[RGZ_MAX_BLITS];
+ int idx;
+};
+
+
+static int rgz_hwc_layer_blit(rgz_out_params_t *params, rgz_layer_t *rgz_layer);
+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);
+static void rgz_get_src_rect(hwc_layer_1_t* layer, blit_rect_t *subregion_rect, blit_rect_t *res_rect);
+static int hal_to_ocd(int color);
+static int rgz_get_orientation(unsigned int transform);
+static int rgz_get_flip_flags(unsigned int transform, int use_src2_flags);
+static int rgz_hwc_scaled(hwc_layer_1_t *layer);
+
+int debug = 0;
+struct rgz_blts blts;
+/* Represents a screen sized background layer */
+static hwc_layer_1_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;
+}
+
+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_1_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_1_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_1_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;
+ int i;
+ (void)rgz;
+
+ rgz_blts_init(&blts);
+
+ /* Begin from index 1 to remove the background layer from the output */
+ rgz_fb_state_t *cur_fb_state = &rgz->cur_fb_state;
+ for (i = 1; i < cur_fb_state->rgz_layerno; i++) {
+ rv = rgz_hwc_layer_blit(params, &cur_fb_state->rgz_layers[i]);
+ if (rv) {
+ OUTE("bvdirect_paint: error in layer %d: %d", i, rv);
+ dump_all(cur_fb_state->rgz_layers, cur_fb_state->rgz_layerno, i);
+ rgz_blts_free(&blts);
+ return rv;
+ }
+ }
+ rgz_blts_bvdirect(rgz, &blts, params);
+ rgz_blts_free(&blts);
+ return rv;
+}
+
+static void rgz_set_async(struct rgz_blt_entry *e, int async)
+{
+ e->bp.flags = async ? e->bp.flags | BVFLAG_ASYNC : e->bp.flags & ~BVFLAG_ASYNC;
+}
+
+static void rgz_get_screen_info(rgz_out_params_t *params, struct bvsurfgeom **screen_geom)
+{
+ *screen_geom = params->data.bvc.dstgeom;
+}
+
+static int rgz_is_blending_disabled(rgz_out_params_t *params)
+{
+ return params->data.bvc.noblend;
+}
+
+static void rgz_get_displayframe_rect(hwc_layer_1_t *layer, blit_rect_t *res_rect)
+{
+ res_rect->left = layer->displayFrame.left;
+ res_rect->top = layer->displayFrame.top;
+ res_rect->bottom = layer->displayFrame.bottom;
+ res_rect->right = layer->displayFrame.right;
+}
+
+/*
+ * Returns a clock-wise rotated view of the inner rectangle relative to
+ * the outer rectangle. The inner rectangle must be contained in the outer
+ * rectangle and coordinates must be relative to the top,left corner of the outer
+ * rectangle.
+ */
+static void rgz_get_rotated_view(blit_rect_t *outer_rect, blit_rect_t *inner_rect,
+ blit_rect_t *res_rect, int orientation)
+{
+ int outer_width = WIDTH(*outer_rect);
+ int outer_height = HEIGHT(*outer_rect);
+ int inner_width = WIDTH(*inner_rect);
+ int inner_height = HEIGHT(*inner_rect);
+ int delta_top = inner_rect->top - outer_rect->top;
+ int delta_left = inner_rect->left - outer_rect->left;
+
+ /* Normalize the angle */
+ orientation = (orientation % 360) + 360;
+
+ /*
+ * Calculate the top,left offset of the inner rectangle inside the outer
+ * rectangle depending on the tranformation value.
+ */
+ switch(orientation % 360) {
+ case 0:
+ res_rect->left = delta_left;
+ res_rect->top = delta_top;
+ break;
+ case 180:
+ res_rect->left = outer_width - inner_width - delta_left;
+ res_rect->top = outer_height - inner_height - delta_top;
+ break;
+ case 90:
+ res_rect->left = outer_height - inner_height - delta_top;
+ res_rect->top = delta_left;
+ break;
+ case 270:
+ res_rect->left = delta_top;
+ res_rect->top = outer_width - inner_width - delta_left;
+ break;
+ default:
+ OUTE("Invalid transform value %d", orientation);
+ }
+
+ if (orientation % 180)
+ swap(inner_width, inner_height);
+
+ res_rect->right = res_rect->left + inner_width;
+ res_rect->bottom = res_rect->top + inner_height;
+}
+
+static void rgz_get_src_rect(hwc_layer_1_t* layer, blit_rect_t *subregion_rect, blit_rect_t *res_rect)
+{
+ if (rgz_hwc_scaled(layer)) {
+ /*
+ * If the layer is scaled we use the whole cropping rectangle from the
+ * source and just move the clipping rectangle for the region we want to
+ * blit, this is done to prevent any artifacts when blitting subregions of
+ * a scaled layer.
+ */
+ res_rect->top = layer->sourceCrop.top;
+ res_rect->left = layer->sourceCrop.left;
+ res_rect->bottom = layer->sourceCrop.bottom;
+ res_rect->right = layer->sourceCrop.right;
+ return;
+ }
+
+ blit_rect_t display_frame;
+ rgz_get_displayframe_rect(layer, &display_frame);
+
+ /*
+ * Get the rotated subregion rectangle with respect to the display frame.
+ * In order to get this correctly we need to take in account the HWC
+ * orientation is clock-wise so to return to the 0 degree view we need to
+ * rotate counter-clock wise the orientation. For example, if the
+ * orientation is 90 we need to rotate -90 to return to a 0 degree view.
+ */
+ int src_orientation = 0 - rgz_get_orientation(layer->transform);
+ rgz_get_rotated_view(&display_frame, subregion_rect, res_rect, src_orientation);
+
+ /*
+ * In order to translate the resulting rectangle relative to the cropping
+ * rectangle the only thing left is account for the offset (result is already
+ * rotated).
+ */
+ res_rect->left += layer->sourceCrop.left;
+ res_rect->right += layer->sourceCrop.left;
+ res_rect->top += layer->sourceCrop.top;
+ res_rect->bottom += layer->sourceCrop.top;
+}
+
+/*
+ * Convert a destination geometry and rectangle to a specified rotated view.
+ * Since clipping rectangle is relative to the destination geometry it will be
+ * rotated as well.
+ */
+static void rgz_rotate_dst(struct rgz_blt_entry* e, int dst_orientation)
+{
+ struct bvsurfgeom *dstgeom = &e->dstgeom;
+ struct bvrect *dstrect = &e->bp.dstrect;
+ struct bvrect *cliprect = &e->bp.cliprect;
+
+ /*
+ * Create a rectangle that represents the destination geometry (outter
+ * rectangle), destination and clipping rectangles (inner rectangles).
+ */
+ blit_rect_t dstgeom_r;
+ dstgeom_r.top = dstgeom_r.left = 0;
+ dstgeom_r.bottom = dstgeom->height;
+ dstgeom_r.right = dstgeom->width;
+
+ blit_rect_t dstrect_r;
+ dstrect_r.top = dstrect->top;
+ dstrect_r.left = dstrect->left;
+ dstrect_r.bottom = dstrect->top + dstrect->height;
+ dstrect_r.right = dstrect->left + dstrect->width;
+
+ blit_rect_t cliprect_r;
+ cliprect_r.top = cliprect->top;
+ cliprect_r.left = cliprect->left;
+ cliprect_r.bottom = cliprect->top + cliprect->height;
+ cliprect_r.right = cliprect->left + cliprect->width;
+
+ /* Get the CW rotated view of the destination rectangle */
+ blit_rect_t res_rect;
+ rgz_get_rotated_view(&dstgeom_r, &dstrect_r, &res_rect, dst_orientation);
+ dstrect->left = res_rect.left;
+ dstrect->top = res_rect.top;
+ dstrect->width = WIDTH(res_rect);
+ dstrect->height = HEIGHT(res_rect);
+
+ rgz_get_rotated_view(&dstgeom_r, &cliprect_r, &res_rect, dst_orientation);
+ cliprect->left = res_rect.left;
+ cliprect->top = res_rect.top;
+ cliprect->width = WIDTH(res_rect);
+ cliprect->height = HEIGHT(res_rect);
+
+ if (dst_orientation % 180)
+ swap(e->dstgeom.width, e->dstgeom.height);
+}
+
+static void rgz_set_dst_data(rgz_out_params_t *params, blit_rect_t *subregion_rect,
+ struct rgz_blt_entry* e, int dst_orientation)
+{
+ struct bvsurfgeom *screen_geom;
+ rgz_get_screen_info(params, &screen_geom);
+
+ /* omaplfb is in charge of assigning the correct dstdesc in the kernel */
+ e->dstgeom.structsize = sizeof(struct bvsurfgeom);
+ e->dstgeom.format = screen_geom->format;
+ e->dstgeom.width = screen_geom->width;
+ e->dstgeom.height = screen_geom->height;
+ e->dstgeom.orientation = dst_orientation;
+ e->dstgeom.virtstride = DSTSTRIDE(screen_geom);
+
+ e->bp.dstrect.left = subregion_rect->left;
+ e->bp.dstrect.top = subregion_rect->top;
+ e->bp.dstrect.width = WIDTH(*subregion_rect);
+ e->bp.dstrect.height = HEIGHT(*subregion_rect);
+
+ /* Give a rotated buffer representation of the destination if requested */
+ if (e->dstgeom.orientation)
+ rgz_rotate_dst(e, dst_orientation);
+}
+
+/* Convert a source geometry and rectangle to a specified rotated view */
+static void rgz_rotate_src(struct rgz_blt_entry* e, int src_orientation, int is_src2)
+{
+ struct bvsurfgeom *srcgeom = is_src2 ? &e->src2geom : &e->src1geom;
+ struct bvrect *srcrect = is_src2 ? &e->bp.src2rect : &e->bp.src1rect;
+
+ /*
+ * Create a rectangle that represents the source geometry (outter rectangle),
+ * source rectangle (inner rectangle).
+ */
+ blit_rect_t srcgeom_r;
+ srcgeom_r.top = srcgeom_r.left = 0;
+ srcgeom_r.bottom = srcgeom->height;
+ srcgeom_r.right = srcgeom->width;
+
+ blit_rect_t srcrect_r;
+ srcrect_r.top = srcrect->top;
+ srcrect_r.left = srcrect->left;
+ srcrect_r.bottom = srcrect->top + srcrect->height;
+ srcrect_r.right = srcrect->left + srcrect->width;
+
+ /* Get the CW rotated view of the source rectangle */
+ blit_rect_t res_rect;
+ rgz_get_rotated_view(&srcgeom_r, &srcrect_r, &res_rect, src_orientation);
+
+ srcrect->left = res_rect.left;
+ srcrect->top = res_rect.top;
+ srcrect->width = WIDTH(res_rect);
+ srcrect->height = HEIGHT(res_rect);
+
+ if (src_orientation % 180)
+ swap(srcgeom->width, srcgeom->height);
+}
+
+static void rgz_set_src_data(rgz_out_params_t *params, rgz_layer_t *rgz_layer,
+ blit_rect_t *subregion_rect, struct rgz_blt_entry* e, int src_orientation,
+ int is_src2)
+{
+ hwc_layer_1_t *hwc_layer = &rgz_layer->hwc_layer;
+ struct bvbuffdesc *srcdesc = is_src2 ? &e->src2desc : &e->src1desc;
+ struct bvsurfgeom *srcgeom = is_src2 ? &e->src2geom : &e->src1geom;
+ struct bvrect *srcrect = is_src2 ? &e->bp.src2rect : &e->bp.src1rect;
+ IMG_native_handle_t *handle = (IMG_native_handle_t *)hwc_layer->handle;
+
+ srcdesc->structsize = sizeof(struct bvbuffdesc);
+ srcdesc->length = handle->iHeight * HANDLE_TO_STRIDE(handle);
+ srcdesc->auxptr = (void*)rgz_layer->buffidx;
+ srcgeom->structsize = sizeof(struct bvsurfgeom);
+ srcgeom->format = hal_to_ocd(handle->iFormat);
+ srcgeom->width = handle->iWidth;
+ srcgeom->height = handle->iHeight;
+ srcgeom->virtstride = HANDLE_TO_STRIDE(handle);
+
+ /* Find out what portion of the src we want to use for the blit */
+ blit_rect_t res_rect;
+ rgz_get_src_rect(hwc_layer, subregion_rect, &res_rect);
+ srcrect->left = res_rect.left;
+ srcrect->top = res_rect.top;
+ srcrect->width = WIDTH(res_rect);
+ srcrect->height = HEIGHT(res_rect);
+
+ /* Give a rotated buffer representation of this source if requested */
+ if (src_orientation) {
+ srcgeom->orientation = src_orientation;
+ rgz_rotate_src(e, src_orientation, is_src2);
+ } else
+ srcgeom->orientation = 0;
+}
+
+/*
+ * Set the clipping rectangle, if part of the subregion rectangle is outside
+ * the boundaries of the destination, remove only the out-of-bounds area
+ */
+static void rgz_set_clip_rect(rgz_out_params_t *params, blit_rect_t *subregion_rect,
+ struct rgz_blt_entry* e)
+{
+ struct bvsurfgeom *screen_geom;
+ rgz_get_screen_info(params, &screen_geom);
+
+ blit_rect_t clip_rect;
+ clip_rect.left = max(0, subregion_rect->left);
+ clip_rect.top = max(0, subregion_rect->top);
+ clip_rect.bottom = min(screen_geom->height, subregion_rect->bottom);
+ clip_rect.right = min(screen_geom->width, subregion_rect->right);
+
+ e->bp.cliprect.left = clip_rect.left;
+ e->bp.cliprect.top = clip_rect.top;
+ e->bp.cliprect.width = WIDTH(clip_rect);
+ e->bp.cliprect.height = HEIGHT(clip_rect);
+}
+
+/*
+ * Configures blit entry to set src2 is the same as the destination
+ */
+static void rgz_set_src2_is_dst(rgz_out_params_t *params, struct rgz_blt_entry* e)
+{
+ /* omaplfb is in charge of assigning the correct src2desc in the kernel */
+ e->src2geom = e->dstgeom;
+ e->src2desc.structsize = sizeof(struct bvbuffdesc);
+ e->src2desc.auxptr = (void*)HWC_BLT_DESC_FB_FN(0);
+ e->bp.src2rect = e->bp.dstrect;
+}
+
+static int rgz_is_layer_nv12(hwc_layer_1_t *layer)
+{
+ IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
+ return is_NV12(handle->iFormat);
+}
+
+/*
+ * Configure the scaling mode according to the layer format
+ */
+static void rgz_cfg_scale_mode(struct rgz_blt_entry* e, hwc_layer_1_t *layer)
+{
+ /*
+ * TODO: Revisit scaling mode assignment later, output between GPU and GC320
+ * seem different
+ */
+ e->bp.scalemode = rgz_is_layer_nv12(layer) ? BVSCALE_9x9_TAP : BVSCALE_BILINEAR;
+}
+
+/*
+ * Copies src1 into the framebuffer
+ */
+static struct rgz_blt_entry* rgz_hwc_subregion_copy(rgz_out_params_t *params,
+ blit_rect_t *subregion_rect, rgz_layer_t *rgz_src1)
+{
+ struct rgz_blt_entry* e = rgz_blts_get(&blts, params);
+ hwc_layer_1_t *hwc_src1 = &rgz_src1->hwc_layer;
+ e->bp.structsize = sizeof(struct bvbltparams);
+ e->bp.op.rop = 0xCCCC; /* SRCCOPY */
+ e->bp.flags = BVFLAG_CLIP | BVFLAG_ROP;
+ e->bp.flags |= rgz_get_flip_flags(hwc_src1->transform, 0);
+ rgz_set_async(e, 1);
+
+ blit_rect_t tmp_rect;
+ if (rgz_hwc_scaled(hwc_src1)) {
+ rgz_get_displayframe_rect(hwc_src1, &tmp_rect);
+ rgz_cfg_scale_mode(e, hwc_src1);
+ } else
+ tmp_rect = *subregion_rect;
+
+ int src1_orientation = rgz_get_orientation(hwc_src1->transform);
+ int dst_orientation = 0;
+
+ if (rgz_is_layer_nv12(hwc_src1)) {
+ /*
+ * Leave NV12 as 0 degree and rotate destination instead, this is done
+ * because of a GC limitation. Rotate destination CW.
+ */
+ dst_orientation = 360 - src1_orientation;
+ src1_orientation = 0;
+ }
+
+ rgz_set_src_data(params, rgz_src1, &tmp_rect, e, src1_orientation, 0);
+ rgz_set_clip_rect(params, subregion_rect, e);
+ rgz_set_dst_data(params, &tmp_rect, e, dst_orientation);
+
+ if((e->src1geom.format == OCDFMT_BGR124) ||
+ (e->src1geom.format == OCDFMT_RGB124) ||
+ (e->src1geom.format == OCDFMT_RGB16))
+ e->dstgeom.format = OCDFMT_BGR124;
+
+ return e;
+}
+
+/*
+ * Blends two layers and write the result in the framebuffer, src1 must be the
+ * top most layer while src2 is the one behind. If src2 is NULL means src1 will
+ * be blended with the current content of the framebuffer.
+ */
+static struct rgz_blt_entry* rgz_hwc_subregion_blend(rgz_out_params_t *params,
+ blit_rect_t *subregion_rect, rgz_layer_t *rgz_src1, rgz_layer_t *rgz_src2)
+{
+ struct rgz_blt_entry* e = rgz_blts_get(&blts, params);
+ hwc_layer_1_t *hwc_src1 = &rgz_src1->hwc_layer;
+ e->bp.structsize = sizeof(struct bvbltparams);
+ e->bp.op.blend = BVBLEND_SRC1OVER;
+ e->bp.flags = BVFLAG_CLIP | BVFLAG_BLEND;
+ e->bp.flags |= rgz_get_flip_flags(hwc_src1->transform, 0);
+ rgz_set_async(e, 1);
+
+ blit_rect_t tmp_rect;
+ if (rgz_hwc_scaled(hwc_src1)) {
+ rgz_get_displayframe_rect(hwc_src1, &tmp_rect);
+ rgz_cfg_scale_mode(e, hwc_src1);
+ } else
+ tmp_rect = *subregion_rect;
+
+ int src1_orientation = rgz_get_orientation(hwc_src1->transform);
+ int dst_orientation = 0;
+
+ if (rgz_is_layer_nv12(hwc_src1)) {
+ /*
+ * Leave NV12 as 0 degree and rotate destination instead, this is done
+ * because of a GC limitation. Rotate destination CW.
+ */
+ dst_orientation = 360 - src1_orientation;
+ src1_orientation = 0;
+ }
+
+ rgz_set_src_data(params, rgz_src1, &tmp_rect, e, src1_orientation, 0);
+ rgz_set_clip_rect(params, subregion_rect, e);
+ rgz_set_dst_data(params, &tmp_rect, e, dst_orientation);
+
+ if (rgz_src2) {
+ /*
+ * NOTE: Due to an API limitation it's not possible to blend src1 and
+ * src2 if both have scaling, hence only src1 is used for now
+ */
+ hwc_layer_1_t *hwc_src2 = &rgz_src2->hwc_layer;
+ if (rgz_hwc_scaled(hwc_src2))
+ OUTE("src2 layer %p has scaling, this is not supported", hwc_src2);
+ /*
+ * We shouldn't receive a NV12 buffer as src2 at this point, this is an
+ * invalid parameter for the blend request
+ */
+ if (rgz_is_layer_nv12(hwc_src2))
+ OUTE("invalid input layer, src2 layer %p is NV12", hwc_src2);
+ e->bp.flags |= rgz_get_flip_flags(hwc_src2->transform, 1);
+ int src2_orientation = rgz_get_orientation(hwc_src2->transform);
+ rgz_set_src_data(params, rgz_src2, subregion_rect, e, src2_orientation, 1);
+ } else
+ rgz_set_src2_is_dst(params, e);
+
+ return e;
+}
+
+/*
+ * 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 rgz_blt_entry* e = rgz_blts_get(&blts, params);
+ e->bp.structsize = sizeof(struct bvbltparams);
+ e->bp.op.rop = 0xCCCC; /* SRCCOPY */
+ e->bp.flags = BVFLAG_CLIP | BVFLAG_ROP;
+ rgz_set_async(e, 1);
+
+ struct bvsurfgeom *screen_geom;
+ rgz_get_screen_info(params, &screen_geom);
+
+ e->src1desc.structsize = sizeof(struct bvbuffdesc);
+ e->src1desc.length = 4; /* 1 pixel, 32bpp */
+ /*
+ * With the HWC we don't bother having a buffer for the fill we'll get the
+ * OMAPLFB to fixup the src1desc and stride if the auxiliary pointer is -1
+ */
+ e->src1desc.auxptr = (void*)-1;
+ e->src1geom.structsize = sizeof(struct bvsurfgeom);
+ e->src1geom.format = OCDFMT_RGBA24;
+ e->bp.src1rect.left = e->bp.src1rect.top = e->src1geom.orientation = 0;
+ e->src1geom.height = e->src1geom.width = e->bp.src1rect.height = e->bp.src1rect.width = 1;
+
+ blit_rect_t clear_rect;
+ if (rect) {
+ clear_rect.left = rect->left;
+ clear_rect.top = rect->top;
+ clear_rect.right = rect->right;
+ clear_rect.bottom = rect->bottom;
+ } else {
+ clear_rect.left = clear_rect.top = 0;
+ clear_rect.right = screen_geom->width;
+ clear_rect.bottom = screen_geom->height;
+ }
+
+ rgz_set_clip_rect(params, &clear_rect, e);
+ rgz_set_dst_data(params, &clear_rect, e, 0);
+}
+
+static int rgz_out_bvcmd_paint(rgz_t *rgz, rgz_out_params_t *params)
+{
+ int rv = 0;
+ int i, j;
+ params->data.bvc.out_blits = 0;
+ params->data.bvc.out_nhndls = 0;
+ rgz_blts_init(&blts);
+ rgz_out_clrdst(params, NULL);
+
+ /* Begin from index 1 to remove the background layer from the output */
+ rgz_fb_state_t *cur_fb_state = &rgz->cur_fb_state;
+ for (i = 1, j = 0; i < cur_fb_state->rgz_layerno; i++) {
+ rgz_layer_t *rgz_layer = &cur_fb_state->rgz_layers[i];
+ hwc_layer_1_t *l = &rgz_layer->hwc_layer;
+
+ //OUTP("blitting meminfo %d", rgz->rgz_layers[i].buffidx);
+
+ /*
+ * See if it is needed to put transparent pixels where this layer
+ * is located in the screen
+ */
+ if (rgz_layer->buffidx == -1) {
+ struct bvsurfgeom *scrgeom = params->data.bvc.dstgeom;
+ blit_rect_t srcregion;
+ srcregion.left = max(0, l->displayFrame.left);
+ srcregion.top = max(0, l->displayFrame.top);
+ srcregion.bottom = min(scrgeom->height, l->displayFrame.bottom);
+ srcregion.right = min(scrgeom->width, l->displayFrame.right);
+ rgz_out_clrdst(params, &srcregion);
+ continue;
+ }
+
+ rv = rgz_hwc_layer_blit(params, rgz_layer);
+ if (rv) {
+ OUTE("bvcmd_paint: error in layer %d: %d", i, rv);
+ dump_all(cur_fb_state->rgz_layers, cur_fb_state->rgz_layerno, i);
+ rgz_blts_free(&blts);
+ return rv;
+ }
+ params->data.bvc.out_hndls[j++] = l->handle;
+ params->data.bvc.out_nhndls++;
+ }
+
+ /* Last blit is made sync to act like a fence for the previous async blits */
+ struct rgz_blt_entry* e = &blts.bvcmds[blts.idx-1];
+ rgz_set_async(e, 0);
+
+ /* 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_1_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_1_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;
+}
+
+/*
+ * Simple bubble sort on an array, ascending order
+ */
+static void rgz_bsort(int *a, int len)
+{
+ int i, j;
+ for (i = 0; i < len; i++) {
+ for (j = 0; j < i; j++) {
+ if (a[i] < a[j]) {
+ int temp = a[i];
+ a[i] = a[j];
+ a[j] = temp;
+ }
+ }
+ }
+}
+
+/*
+ * 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 void rgz_gen_blitregions(rgz_t *rgz, 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;
+
+ /*
+ * Add damaged region, then all layers. We are guaranteed to not go outside
+ * of offsets array boundaries at this point.
+ */
+ offsets[noffsets++] = rgz->damaged_area.left;
+ offsets[noffsets++] = rgz->damaged_area.right;
+
+ for (l = 0; l < hregion->nlayers; l++) {
+ hwc_layer_1_t *layer = &hregion->rgz_layers[l]->hwc_layer;
+ /* Make sure the subregion is not outside the boundaries of the screen */
+ offsets[noffsets++] = max(0, layer->displayFrame.left);
+ offsets[noffsets++] = min(layer->displayFrame.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_1_t *layer = &hregion->rgz_layers[l]->hwc_layer;
+ if (RECT_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_1_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_1_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))
+ return handle->iFormat == HAL_PIXEL_FORMAT_TI_NV12;
+
+ /* FIXME: The following must be removed when GC supports vertical/horizontal
+ * buffer flips, please note having a FLIP_H and FLIP_V means 180 rotation
+ * which is supported indeed
+ */
+ if (layer->transform) {
+ int is_flipped = !!(layer->transform & HWC_TRANSFORM_FLIP_H) ^ !!(layer->transform & HWC_TRANSFORM_FLIP_V);
+ if (is_flipped) {
+ ALOGE("Layer %p is flipped %d", layer, layer->transform);
+ 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;
+}
+
+/* Reset dirty region data and state */
+static void rgz_delete_region_data(rgz_t *rgz){
+ if (!rgz)
+ return;
+ if (rgz->hregions)
+ free(rgz->hregions);
+ rgz->hregions = NULL;
+ rgz->nhregions = 0;
+ rgz->state &= ~RGZ_REGION_DATA;
+}
+
+static rgz_fb_state_t* get_prev_fb_state(rgz_t *rgz)
+{
+ return &rgz->fb_states[rgz->fb_state_idx];
+}
+
+static rgz_fb_state_t* get_next_fb_state(rgz_t *rgz)
+{
+ rgz->fb_state_idx = (rgz->fb_state_idx + 1) % RGZ_NUM_FB;
+ return &rgz->fb_states[rgz->fb_state_idx];
+}
+
+static void rgz_add_to_damaged_area(rgz_in_params_t *params, rgz_layer_t *rgz_layer,
+ blit_rect_t *damaged_area)
+{
+ struct bvsurfgeom *screen_geom = params->data.hwc.dstgeom;
+ hwc_layer_1_t *layer = &rgz_layer->hwc_layer;
+
+ blit_rect_t screen_rect;
+ screen_rect.left = screen_rect.top = 0;
+ screen_rect.right = screen_geom->width;
+ screen_rect.bottom = screen_geom->height;
+
+ /* Ignore the layer rectangle if it doesn't intersect the screen */
+ if (!RECT_INTERSECTS(screen_rect, layer->displayFrame))
+ return;
+
+ /* Clip the layer rectangle to the screen geometry */
+ blit_rect_t layer_rect;
+ rgz_get_displayframe_rect(layer, &layer_rect);
+ layer_rect.left = max(0, layer_rect.left);
+ layer_rect.top = max(0, layer_rect.top);
+ layer_rect.right = min(screen_rect.right, layer_rect.right);
+ layer_rect.bottom = min(screen_rect.bottom, layer_rect.bottom);
+
+ /* Then add the rectangle to the damage area */
+ if (empty_rect(damaged_area)) {
+ /* Adding for the first time */
+ damaged_area->left = layer_rect.left;
+ damaged_area->top = layer_rect.top;
+ damaged_area->right = layer_rect.right;
+ damaged_area->bottom = layer_rect.bottom;
+ } else {
+ /* Grow current damaged area */
+ damaged_area->left = min(damaged_area->left, layer_rect.left);
+ damaged_area->top = min(damaged_area->top, layer_rect.top);
+ damaged_area->right = max(damaged_area->right, layer_rect.right);
+ damaged_area->bottom = max(damaged_area->bottom, layer_rect.bottom);
+ }
+}
+
+/* Search a layer with the specified identity in the passed array */
+static rgz_layer_t* rgz_find_layer(rgz_layer_t *rgz_layers, int rgz_layerno,
+ uint32_t layer_identity)
+{
+ int i;
+ for (i = 0; i < rgz_layerno; i++) {
+ rgz_layer_t *rgz_layer = &rgz_layers[i];
+ /* Ignore background layer, it has no identity */
+ if (rgz_layer->buffidx == RGZ_BACKGROUND_BUFFIDX)
+ continue;
+ if (rgz_layer->identity == layer_identity)
+ return rgz_layer;
+ }
+ return NULL;
+}
+
+/* Determines if two layers with the same identity have changed its own window content */
+static int rgz_has_layer_content_changed(rgz_layer_t *cur_rgz_layer, rgz_layer_t *prev_rgz_layer)
+{
+ hwc_layer_1_t *cur_hwc_layer = &cur_rgz_layer->hwc_layer;
+ hwc_layer_1_t *prev_hwc_layer = &prev_rgz_layer->hwc_layer;
+
+ /* The background has no identity and never changes */
+ if (cur_rgz_layer->buffidx == RGZ_BACKGROUND_BUFFIDX &&
+ prev_rgz_layer->buffidx == RGZ_BACKGROUND_BUFFIDX)
+ return 0;
+
+ if (cur_rgz_layer->identity != prev_rgz_layer->identity) {
+ OUTE("%s: Invalid input, layer identities differ (current=%d, prev=%d)",
+ __func__, cur_rgz_layer->identity, prev_rgz_layer->identity);
+ return 1;
+ }
+
+ /* If the layer has the clear fb hint we don't care about the content */
+ if (cur_rgz_layer->buffidx == RGZ_CLEARHINT_BUFFIDX &&
+ prev_rgz_layer->buffidx == RGZ_CLEARHINT_BUFFIDX)
+ return 0;
+
+ /* Check if the layer content has changed */
+ if (cur_hwc_layer->handle != prev_hwc_layer->handle ||
+ cur_hwc_layer->transform != prev_hwc_layer->transform ||
+ cur_hwc_layer->sourceCrop.top != prev_hwc_layer->sourceCrop.top ||
+ cur_hwc_layer->sourceCrop.left != prev_hwc_layer->sourceCrop.left ||
+ cur_hwc_layer->sourceCrop.bottom != prev_hwc_layer->sourceCrop.bottom ||
+ cur_hwc_layer->sourceCrop.right != prev_hwc_layer->sourceCrop.right)
+ return 1;
+
+ return 0;
+}
+
+/* Determines if two layers with the same identity have changed their screen position */
+static int rgz_has_layer_frame_moved(rgz_layer_t *cur_rgz_layer, rgz_layer_t *target_rgz_layer)
+{
+ hwc_layer_1_t *cur_hwc_layer = &cur_rgz_layer->hwc_layer;
+ hwc_layer_1_t *target_hwc_layer = &target_rgz_layer->hwc_layer;
+
+ if (cur_rgz_layer->identity != target_rgz_layer->identity) {
+ OUTE("%s: Invalid input, layer identities differ (current=%d, target=%d)",
+ __func__, cur_rgz_layer->identity, target_rgz_layer->identity);
+ return 1;
+ }
+
+ if (cur_hwc_layer->displayFrame.top != target_hwc_layer->displayFrame.top ||
+ cur_hwc_layer->displayFrame.left != target_hwc_layer->displayFrame.left ||
+ cur_hwc_layer->displayFrame.bottom != target_hwc_layer->displayFrame.bottom ||
+ cur_hwc_layer->displayFrame.right != target_hwc_layer->displayFrame.right)
+ return 1;
+
+ return 0;
+}
+
+static void rgz_handle_dirty_region(rgz_t *rgz, rgz_in_params_t *params,
+ rgz_fb_state_t* prev_fb_state, rgz_fb_state_t* target_fb_state)
+{
+ /* Reset damaged area */
+ bzero(&rgz->damaged_area, sizeof(rgz->damaged_area));
+
+ int i;
+ rgz_fb_state_t *cur_fb_state = &rgz->cur_fb_state;
+
+ for (i = 0; i < cur_fb_state->rgz_layerno; i++) {
+ rgz_layer_t *cur_rgz_layer = &cur_fb_state->rgz_layers[i];
+ rgz_layer_t *prev_rgz_layer = NULL;
+ int layer_changed = 0;
+
+ if (i == 0) {
+ /*
+ * Background is always zero, no need to search for it. If the previous state
+ * is empty reset the dirty count for the background layer.
+ */
+ if (prev_fb_state->rgz_layerno)
+ prev_rgz_layer = &prev_fb_state->rgz_layers[0];
+ } else {
+ /* Find out if this layer was present in the previous frame */
+ prev_rgz_layer = rgz_find_layer(prev_fb_state->rgz_layers,
+ prev_fb_state->rgz_layerno, cur_rgz_layer->identity);
+ }
+
+ /* Check if the layer is new or if the content changed from the previous frame */
+ if (prev_rgz_layer && !rgz_has_layer_content_changed(cur_rgz_layer, prev_rgz_layer)) {
+ /* Copy previous dirty count */
+ cur_rgz_layer->dirty_count = prev_rgz_layer->dirty_count;
+ cur_rgz_layer->dirty_count -= cur_rgz_layer->dirty_count ? 1 : 0;
+ } else
+ cur_rgz_layer->dirty_count = RGZ_NUM_FB;
+
+ /* If the layer is new, redraw the layer area */
+ if (!prev_rgz_layer) {
+ rgz_add_to_damaged_area(params, cur_rgz_layer, &rgz->damaged_area);
+ continue;
+ }
+
+ /* Nothing more to do with the background layer */
+ if (i == 0)
+ continue;
+
+ /* Find out if the layer is present in the target frame */
+ rgz_layer_t *target_rgz_layer = rgz_find_layer(target_fb_state->rgz_layers,
+ target_fb_state->rgz_layerno, cur_rgz_layer->identity);
+
+ if (target_rgz_layer) {
+ /* Find out if the window size and position are different from the target frame */
+ if (rgz_has_layer_frame_moved(cur_rgz_layer, target_rgz_layer)) {
+ /*
+ * Redraw both layer areas. This will effectively clear the area where
+ * this layer was in the target frame and force to draw the new layer
+ * location.
+ */
+ rgz_add_to_damaged_area(params, cur_rgz_layer, &rgz->damaged_area);
+ rgz_add_to_damaged_area(params, target_rgz_layer, &rgz->damaged_area);
+ cur_rgz_layer->dirty_count = RGZ_NUM_FB;
+ }
+ } else {
+ /* If the layer is not in the target just draw it's new location */
+ rgz_add_to_damaged_area(params, cur_rgz_layer, &rgz->damaged_area);
+ }
+ }
+
+ /*
+ * Add to damage area layers missing from the target frame to the current frame
+ * ignoring the background
+ */
+ for (i = 1; i < target_fb_state->rgz_layerno; i++) {
+ rgz_layer_t *target_rgz_layer = &target_fb_state->rgz_layers[i];
+
+ rgz_layer_t *cur_rgz_layer = rgz_find_layer(cur_fb_state->rgz_layers,
+ cur_fb_state->rgz_layerno, target_rgz_layer->identity);
+
+ /* Layers present in the target have been handled already in the loop above */
+ if (cur_rgz_layer)
+ continue;
+
+ /* The target layer is not present in the current frame, redraw its area */
+ rgz_add_to_damaged_area(params, target_rgz_layer, &rgz->damaged_area);
+ }
+}
+
+/* Adds the background layer in first the position of the passed fb state */
+static void rgz_add_background_layer(rgz_fb_state_t *fb_state)
+{
+ rgz_layer_t *rgz_layer = &fb_state->rgz_layers[0];
+ rgz_layer->hwc_layer = bg_layer;
+ rgz_layer->buffidx = RGZ_BACKGROUND_BUFFIDX;
+ /* Set dummy handle to maintain dirty region state */
+ rgz_layer->hwc_layer.handle = (void*) 0x1;
+}
+
+static int rgz_in_hwccheck(rgz_in_params_t *p, rgz_t *rgz)
+{
+ hwc_layer_1_t *layers = p->data.hwc.layers;
+ hwc_layer_extended_t *extlayers = p->data.hwc.extlayers;
+ 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;
+
+ /*
+ * Insert the background layer at the beginning of the list, maintain a
+ * state for dirty region handling
+ */
+ rgz_fb_state_t *cur_fb_state = &rgz->cur_fb_state;
+ rgz_add_background_layer(cur_fb_state);
+
+ 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 = &cur_fb_state->rgz_layers[possible_blit+1];
+ rgz_layer->hwc_layer = layers[l];
+ rgz_layer->identity = extlayers[l].identity;
+ rgz_layer->buffidx = memidx++;
+ possible_blit++;
+ }
+ continue;
+ }
+
+ if (layers[l].hints & HWC_HINT_CLEAR_FB) {
+ candidates++;
+ if (possible_blit < RGZ_INPUT_MAXLAYERS) {
+ /*
+ * Use only the layer rectangle as an input to regionize when the clear
+ * fb hint is present, mark this layer to identify it.
+ */
+ rgz_layer_t *rgz_layer = &cur_fb_state->rgz_layers[possible_blit+1];
+ rgz_layer->hwc_layer = layers[l];
+ rgz_layer->identity = extlayers[l].identity;
+ rgz_layer->buffidx = RGZ_CLEARHINT_BUFFIDX;
+ /* Set dummy handle to maintain dirty region state */
+ rgz_layer->hwc_layer.handle = (void*) 0x1;
+ possible_blit++;
+ }
+ }
+ }
+
+ if (!possible_blit || possible_blit != candidates) {
+ return -1;
+ }
+
+ rgz->state |= RGZ_STATE_INIT;
+ cur_fb_state->rgz_layerno = possible_blit + 1; /* Account for background layer */
+
+ /* Get the target and previous frame geometries */
+ rgz_fb_state_t* prev_fb_state = get_prev_fb_state(rgz);
+ rgz_fb_state_t* target_fb_state = get_next_fb_state(rgz);
+
+ /* Modifiy dirty counters and create the damaged region */
+ rgz_handle_dirty_region(rgz, p, prev_fb_state, target_fb_state);
+
+ /* Copy the current geometry to use it in the next frame */
+ memcpy(target_fb_state->rgz_layers, cur_fb_state->rgz_layers, sizeof(rgz_layer_t) * cur_fb_state->rgz_layerno);
+ target_fb_state->rgz_layerno = cur_fb_state->rgz_layerno;
+
+ return RGZ_ALL;
+}
+
+static int rgz_in_hwc(rgz_in_params_t *p, rgz_t *rgz)
+{
+ int i, j;
+ int yentries[RGZ_SUBREGIONMAX];
+ int dispw; /* widest layer */
+ int screen_width = p->data.hwc.dstgeom->width;
+ int screen_height = p->data.hwc.dstgeom->height;
+ rgz_fb_state_t *cur_fb_state = &rgz->cur_fb_state;
+
+ if (!(rgz->state & RGZ_STATE_INIT)) {
+ OUTE("rgz_process started with bad state");
+ return -1;
+ }
+
+ /*
+ * Figure out if there is enough space to store the top-bottom coordinates
+ * of each layer including the damaged area
+ */
+ if (((cur_fb_state->rgz_layerno + 1) * 2) > RGZ_SUBREGIONMAX) {
+ OUTE("%s: Not enough space to store top-bottom coordinates of each layer (max %d, needed %d*2)",
+ __func__, RGZ_SUBREGIONMAX, cur_fb_state->rgz_layerno + 1);
+ return -1;
+ }
+
+ /* Delete the previous region data */
+ rgz_delete_region_data(rgz);
+
+ /*
+ * Find the horizontal regions, add damaged area first which is already
+ * inside display boundaries
+ */
+ int ylen = 0;
+ yentries[ylen++] = rgz->damaged_area.top;
+ yentries[ylen++] = rgz->damaged_area.bottom;
+ dispw = rgz->damaged_area.right;
+
+ /* Add the top and bottom coordinates of each layer */
+ for (i = 0; i < cur_fb_state->rgz_layerno; i++) {
+ hwc_layer_1_t *layer = &cur_fb_state->rgz_layers[i].hwc_layer;
+ /* Maintain regions inside display boundaries */
+ yentries[ylen++] = max(0, layer->displayFrame.top);
+ yentries[ylen++] = min(layer->displayFrame.bottom, screen_height);
+ dispw = dispw > layer->displayFrame.right ? dispw : layer->displayFrame.right;
+ }
+ rgz_bsort(yentries, ylen);
+ 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), cur_fb_state->rgz_layerno);
+
+ 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 < cur_fb_state->rgz_layerno; j++) {
+ hwc_layer_1_t *layer = &cur_fb_state->rgz_layers[j].hwc_layer;
+ if (RECT_INTERSECTS(hregions[i].rect, layer->displayFrame)) {
+ int l = hregions[i].nlayers++;
+ hregions[i].rgz_layers[l] = &cur_fb_state->rgz_layers[j];
+ }
+ }
+ }
+
+ /* Calculate blit regions */
+ for (i = 0; i < rgz->nhregions; i++) {
+ rgz_gen_blitregions(rgz, &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_1_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_display_contents_1_t* list, int csv)
+{
+ size_t i;
+ for (i = 0; i < list->numHwLayers; i++) {
+ hwc_layer_1_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;
+ }
+}
+
+static BVFN_MAP bv_map;
+static BVFN_BLT bv_blt;
+static BVFN_UNMAP bv_unmap;
+
+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;
+}
+
+static int rgz_get_orientation(unsigned int transform)
+{
+ int orientation = 0;
+ if ((transform & HWC_TRANSFORM_FLIP_H) && (transform & HWC_TRANSFORM_FLIP_V))
+ orientation += 180;
+ if (transform & HWC_TRANSFORM_ROT_90)
+ orientation += 90;
+
+ return orientation;
+}
+
+static int rgz_get_flip_flags(unsigned int transform, int use_src2_flags)
+{
+ /*
+ * If vertical and horizontal flip flags are set it means a 180 rotation
+ * (with no flip) is intended for the layer, so we return 0 in that case.
+ */
+ int flip_flags = 0;
+ if (transform & HWC_TRANSFORM_FLIP_H)
+ flip_flags |= (use_src2_flags ? BVFLAG_HORZ_FLIP_SRC2 : BVFLAG_HORZ_FLIP_SRC1);
+ if (transform & HWC_TRANSFORM_FLIP_V)
+ flip_flags = flip_flags ? 0 : flip_flags | (use_src2_flags ? BVFLAG_VERT_FLIP_SRC2 : BVFLAG_VERT_FLIP_SRC1);
+ return flip_flags;
+}
+
+static int rgz_hwc_layer_blit(rgz_out_params_t *params, rgz_layer_t *rgz_layer)
+{
+ hwc_layer_1_t* layer = &rgz_layer->hwc_layer;
+ blit_rect_t srcregion;
+ rgz_get_displayframe_rect(layer, &srcregion);
+
+ int noblend = rgz_is_blending_disabled(params);
+ if (!noblend && layer->blending == HWC_BLENDING_PREMULT)
+ rgz_hwc_subregion_blend(params, &srcregion, rgz_layer, NULL);
+ else
+ rgz_hwc_subregion_copy(params, &srcregion, rgz_layer);
+
+ return 0;
+}
+
+static int rgz_can_blend_together(hwc_layer_1_t* src1_layer, hwc_layer_1_t* src2_layer)
+{
+ /* If any layer is scaled we cannot blend both layers in one blit */
+ if (rgz_hwc_scaled(src1_layer) || rgz_hwc_scaled(src2_layer))
+ return 0;
+
+ /* NV12 buffers don't have alpha information on it */
+ IMG_native_handle_t *src1_hndl = (IMG_native_handle_t *)src1_layer->handle;
+ IMG_native_handle_t *src2_hndl = (IMG_native_handle_t *)src2_layer->handle;
+ if (is_NV12(src1_hndl->iFormat) || is_NV12(src2_hndl->iFormat))
+ return 0;
+
+ return 1;
+}
+
+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,
+ blit_rect_t *damaged_area)
+{
+ 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;
+ }
+
+ /* Determine if this region is dirty */
+ int dirty = 0;
+ blit_rect_t *subregion_rect = &hregion->blitrects[lix][sidx];
+ if (RECT_INTERSECTS(*damaged_area, *subregion_rect)) {
+ /* The subregion intersects the damaged area, draw unconditionally */
+ dirty = 1;
+ } else {
+ int 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;
+
+ /* Check if the bottom layer is the background */
+ if (hregion->rgz_layers[lix]->buffidx == RGZ_BACKGROUND_BUFFIDX) {
+ if (ldepth == 1) {
+ /* Background layer is the only operation, clear subregion */
+ 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);
+ }
+ }
+
+ /*
+ * See if the depth most layer needs to be ignored. If this layer is the
+ * only operation, we need to clear this subregion.
+ */
+ if (hregion->rgz_layers[lix]->buffidx == RGZ_CLEARHINT_BUFFIDX) {
+ ldepth--;
+ if (!ldepth) {
+ rgz_out_clrdst(params, &hregion->blitrects[lix][sidx]);
+ return 0;
+ }
+ lix = get_layer_ops_next(hregion, sidx, lix);
+ }
+
+ int noblend = rgz_is_blending_disabled(params);
+
+ if (!noblend && ldepth > 1) { /* BLEND */
+ blit_rect_t *rect = &hregion->blitrects[lix][sidx];
+ struct rgz_blt_entry* e;
+
+ 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, we can do this only if both layers are not scaled
+ */
+ int prev_layer_scaled = 0;
+ int prev_layer_nv12 = 0;
+ int first_batchflags = 0;
+ rgz_layer_t *rgz_src1 = hregion->rgz_layers[lix];
+ rgz_layer_t *rgz_src2 = hregion->rgz_layers[s2lix];
+ if (rgz_can_blend_together(&rgz_src1->hwc_layer, &rgz_src2->hwc_layer))
+ e = rgz_hwc_subregion_blend(params, rect, rgz_src1, rgz_src2);
+ else {
+ /* Return index to the first operation and make a copy of the first layer */
+ lix = s2lix;
+ rgz_src1 = hregion->rgz_layers[lix];
+ e = rgz_hwc_subregion_copy(params, rect, rgz_src1);
+ /*
+ * First blit is a copy, the rest will be blends, hence the operation
+ * changed on the second blit.
+ */
+ first_batchflags |= BVBATCH_OP;
+ prev_layer_nv12 = rgz_is_layer_nv12(&rgz_src1->hwc_layer);
+ prev_layer_scaled = rgz_hwc_scaled(&rgz_src1->hwc_layer);
+ }
+
+ /*
+ * Regardless if the first blit is a copy or blend, src2 may have changed
+ * on the second blit
+ */
+ first_batchflags |= BVBATCH_SRC2 | BVBATCH_SRC2RECT_ORIGIN | BVBATCH_SRC2RECT_SIZE;
+
+ rgz_batch_entry(e, BVFLAG_BATCH_BEGIN, 0);
+
+ /* Rest of layers blended with FB */
+ while((lix = get_layer_ops_next(hregion, sidx, lix)) != -1) {
+ int batchflags = first_batchflags;
+ first_batchflags = 0;
+ rgz_src1 = hregion->rgz_layers[lix];
+
+ /* Blend src1 into dst */
+ e = rgz_hwc_subregion_blend(params, rect, rgz_src1, NULL);
+
+ /*
+ * NOTE: After the first blit is configured, consequent blits are
+ * blend operations done with src1 and the destination, that is,
+ * src2 is the same as dst, any batchflag changed for the destination
+ * applies to src2 as well.
+ */
+
+ /* src1 parameters always change on every blit */
+ batchflags |= BVBATCH_SRC1 | BVBATCH_SRC1RECT_ORIGIN| BVBATCH_SRC1RECT_SIZE;
+
+ /*
+ * If the current/previous layer has scaling, destination rectangles
+ * likely changed as well as the scaling mode. Clipping rectangle
+ * remains the same as well as destination geometry.
+ */
+ int cur_layer_scaled = rgz_hwc_scaled(&rgz_src1->hwc_layer);
+ if (cur_layer_scaled || prev_layer_scaled) {
+ batchflags |= BVBATCH_DSTRECT_ORIGIN | BVBATCH_DSTRECT_SIZE |
+ BVBATCH_SRC2RECT_ORIGIN | BVBATCH_SRC2RECT_SIZE |
+ BVBATCH_SCALE;
+ }
+ prev_layer_scaled = cur_layer_scaled;
+
+ /*
+ * If the current/previous layer is NV12, the destination geometry
+ * could have been rotated, hence the destination and clipping
+ * rectangles might have been trasformed to match the rotated
+ * destination geometry.
+ */
+ int cur_layer_nv12 = rgz_is_layer_nv12(&rgz_src1->hwc_layer);
+ if (cur_layer_nv12 || prev_layer_nv12) {
+ batchflags |= BVBATCH_DST | BVBATCH_DSTRECT_ORIGIN | BVBATCH_DSTRECT_SIZE |
+ BVBATCH_SRC2 | BVBATCH_SRC2RECT_ORIGIN | BVBATCH_SRC2RECT_SIZE |
+ BVBATCH_CLIPRECT;
+ }
+ prev_layer_nv12 = cur_layer_nv12;
+
+ 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);
+ rgz_hwc_subregion_copy(params, rect, hregion->rgz_layers[lix]);
+ }
+ 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);
+ if (rgz_hwc_subregion_blit(hregion, s, params, &rgz->damaged_area))
+ return -1;
+ }
+ }
+
+ int rv = 0;
+
+ if (IS_BVCMD(params)) {
+ int j;
+ params->data.bvc.out_nhndls = 0;
+ rgz_fb_state_t *cur_fb_state = &rgz->cur_fb_state;
+ /* Begin from index 1 to remove the background layer from the output */
+ for (j = 1, i = 0; j < cur_fb_state->rgz_layerno; j++) {
+ rgz_layer_t *rgz_layer = &cur_fb_state->rgz_layers[j];
+ /* We don't need the handles for layers marked as -1 */
+ if (rgz_layer->buffidx == -1)
+ continue;
+ params->data.bvc.out_hndls[i++] = rgz_layer->hwc_layer.handle;
+ params->data.bvc.out_nhndls++;
+ }
+
+ if (blts.idx > 0) {
+ /* Last blit is made sync to act like a fence for the previous async blits */
+ struct rgz_blt_entry* e = &blts.bvcmds[blts.idx-1];
+ rgz_set_async(e, 0);
+ }
+
+ /* 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_display_contents_1_t* list, int dispw, int disph)
+{
+ if (!list) /* A NULL composition list can occur */
+ return;
+
+ 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);
+ 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;
+ }
+
+ bzero(&bg_layer, sizeof(bg_layer));
+ 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..de41b82
--- /dev/null
+++ b/hwc/rgz_2d.h
@@ -0,0 +1,306 @@
+/*
+ * 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 and
+ * a damage region as well.
+ */
+#define RGZ_INPUT_MAXLAYERS (RGZ_MAXLAYERS - 2)
+
+/* Number of framebuffers to track */
+#define RGZ_NUM_FB 2
+
+/*
+ * 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_1_t *layers;
+ hwc_layer_extended_t *extlayers;
+ struct bvsurfgeom *dstgeom;
+};
+
+typedef struct rgz_in_params {
+ int op; /* See RGZ_IN_* */
+ union {
+ struct rgz_in_hwc hwc;
+ } data;
+} rgz_in_params_t;
+
+typedef struct rgz_ext_layer_list {
+ hwc_layer_extended_t layers[RGZ_INPUT_MAXLAYERS];
+} rgz_ext_layer_list_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_display_contents_1_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_1_t hwc_layer;
+ uint32_t identity;
+ int buffidx;
+ int dirty_count;
+} rgz_layer_t;
+
+typedef struct rgz_fb_state {
+ int rgz_layerno;
+ rgz_layer_t rgz_layers[RGZ_MAXLAYERS];
+} rgz_fb_state_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;
+ rgz_fb_state_t cur_fb_state;
+ int fb_state_idx; /* Target framebuffer index. Points to the fb where the blits will be applied to */
+ rgz_fb_state_t fb_states[RGZ_NUM_FB]; /* Storage for previous framebuffer geometry states */
+ blit_rect_t damaged_area; /* Area of the screen which will be redrawn unconditionally */
+};
+
+#endif /* __RGZ_2D__ */
diff --git a/hwc/sw_vsync.c b/hwc/sw_vsync.c
new file mode 100644
index 0000000..cf24b31
--- /dev/null
+++ b/hwc/sw_vsync.c
@@ -0,0 +1,145 @@
+/*
+ * 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 <errno.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <sys/resource.h>
+#include <pthread.h>
+#include <time.h>
+
+#include <cutils/properties.h>
+#include <cutils/log.h>
+#include <utils/Timers.h>
+
+#include "hwc_dev.h"
+
+static pthread_t vsync_thread;
+static pthread_mutex_t vsync_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t vsync_cond;
+static bool vsync_loop_active = false;
+
+nsecs_t vsync_rate;
+
+static struct timespec diff(struct timespec start, struct timespec end)
+{
+ struct timespec temp;
+ if ((end.tv_nsec - start.tv_nsec) < 0) {
+ temp.tv_sec = end.tv_sec-start.tv_sec - 1;
+ temp.tv_nsec = 1000000000 + end.tv_nsec-start.tv_nsec;
+ } else {
+ temp.tv_sec = end.tv_sec - start.tv_sec;
+ temp.tv_nsec = end.tv_nsec - start.tv_nsec;
+ }
+ return temp;
+}
+
+static void *vsync_loop(void *data)
+{
+ struct timespec tp, tp_next, tp_sleep;
+ nsecs_t now = 0, period = vsync_rate, next_vsync = 0, next_fake_vsync = 0, sleep = 0;
+ omap_hwc_device_t *hwc_dev = (omap_hwc_device_t *)data;
+ tp_sleep.tv_sec = tp_sleep.tv_nsec = 0;
+ bool reset_timers = true;
+
+ setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
+
+ for (;;) {
+ pthread_mutex_lock(&vsync_mutex);
+ period = vsync_rate; /* re-read rate */
+ while (!vsync_loop_active) {
+ pthread_cond_wait(&vsync_cond, &vsync_mutex);
+ }
+ pthread_mutex_unlock(&vsync_mutex);
+
+ clock_gettime(CLOCK_MONOTONIC, &tp);
+ now = (tp.tv_sec * 1000000000) + tp.tv_nsec;
+ next_vsync = next_fake_vsync;
+ sleep = next_vsync - now;
+ if (sleep < 0) {
+ /* we missed, find where the next vsync should be */
+ sleep = (period - ((now - next_vsync) % period));
+ next_vsync = now + sleep;
+ }
+ next_fake_vsync = next_vsync + period;
+ tp_next.tv_sec = (next_vsync / 1000000000);
+ tp_next.tv_nsec = (next_vsync % 1000000000);
+ tp_sleep = diff(tp, tp_next);
+
+ nanosleep(&tp_sleep, NULL);
+ if (hwc_dev->procs && hwc_dev->procs->vsync) {
+ hwc_dev->procs->vsync(hwc_dev->procs, 0, next_vsync);
+ }
+ }
+ return NULL;
+}
+
+bool use_sw_vsync()
+{
+ char board[PROPERTY_VALUE_MAX];
+ bool rv = false;
+ property_get("ro.product.board", board, "");
+ if ((strncmp("blaze", board, PROPERTY_VALUE_MAX) == 0) ||
+ (strncmp("panda5", board, PROPERTY_VALUE_MAX) == 0)) {
+ /* TODO: panda5 really should support h/w vsync */
+ rv = true;
+ } else {
+ char value[PROPERTY_VALUE_MAX];
+ property_get("persist.hwc.sw_vsync", value, "0");
+ int use_sw_vsync = atoi(value);
+ rv = use_sw_vsync > 0;
+ }
+ ALOGI("Expecting %s vsync for %s", rv ? "s/w" : "h/w", board);
+ return rv;
+}
+
+void init_sw_vsync(omap_hwc_device_t *hwc_dev)
+{
+ pthread_cond_init(&vsync_cond, NULL);
+ pthread_create(&vsync_thread, NULL, vsync_loop, (void *)hwc_dev);
+}
+
+void start_sw_vsync()
+{
+ char refresh_rate[PROPERTY_VALUE_MAX];
+ property_get("persist.hwc.sw_vsync_rate", refresh_rate, "60");
+
+ pthread_mutex_lock(&vsync_mutex);
+ int rate = atoi(refresh_rate);
+ if (rate <= 0)
+ rate = 60;
+ vsync_rate = 1000000000 / rate;
+ if (vsync_loop_active) {
+ pthread_mutex_unlock(&vsync_mutex);
+ return;
+ }
+ vsync_loop_active = true;
+ pthread_mutex_unlock(&vsync_mutex);
+ pthread_cond_signal(&vsync_cond);
+}
+
+void stop_sw_vsync()
+{
+ pthread_mutex_lock(&vsync_mutex);
+ if (!vsync_loop_active) {
+ pthread_mutex_unlock(&vsync_mutex);
+ return;
+ }
+ vsync_loop_active = false;
+ pthread_mutex_unlock(&vsync_mutex);
+ pthread_cond_signal(&vsync_cond);
+}
diff --git a/hwc/sw_vsync.h b/hwc/sw_vsync.h
new file mode 100644
index 0000000..bb06165
--- /dev/null
+++ b/hwc/sw_vsync.h
@@ -0,0 +1,25 @@
+/*
+ * 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 __SWVSYNC_H__
+#define __SWVSYNC_H__
+
+bool use_sw_vsync();
+void init_sw_vsync(omap_hwc_device_t *hwc_dev);
+void start_sw_vsync();
+void stop_sw_vsync();
+
+#endif