hwc.c revision b29f69b6b65a33ec0052036e8b6bf0c219cc157e
1c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*
2c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * Copyright (C) Texas Instruments - http://www.ti.com/
3c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev *
4c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * Licensed under the Apache License, Version 2.0 (the "License");
5c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * you may not use this file except in compliance with the License.
6c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * You may obtain a copy of the License at
7c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev *
8c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev *      http://www.apache.org/licenses/LICENSE-2.0
9c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev *
10c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * Unless required by applicable law or agreed to in writing, software
11c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * distributed under the License is distributed on an "AS IS" BASIS,
12c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * See the License for the specific language governing permissions and
14c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * limitations under the License.
15c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev */
16c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
17c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <errno.h>
18c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <malloc.h>
19c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <stdlib.h>
20c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <stdarg.h>
212125fa148686edfa389121f946377aedaa3d9483Lajos Molnar#include <fcntl.h>
2202150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian#include <poll.h>
23751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen#include <sys/eventfd.h>
242125fa148686edfa389121f946377aedaa3d9483Lajos Molnar#include <sys/ioctl.h>
252125fa148686edfa389121f946377aedaa3d9483Lajos Molnar#include <linux/fb.h>
26e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian#include <linux/omapfb.h>
270aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar#include <sys/mman.h>
28e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian#include <sys/resource.h>
29c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
30c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <cutils/properties.h>
31c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <cutils/log.h>
32c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <cutils/native_handle.h>
33876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen#define HWC_REMOVE_DEPRECATED_VERSIONS 0
34c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <hardware/hardware.h>
35c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <hardware/hwcomposer.h>
36c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <EGL/egl.h>
372125fa148686edfa389121f946377aedaa3d9483Lajos Molnar#include <hardware_legacy/uevent.h>
380aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar#include <png.h>
391fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse#include <utils/Timers.h>
40c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
41e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian#include <system/graphics.h>
42876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen#include <linux/bltsville.h>
43876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
44876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen#define MAX_HWC_LAYERS 32
45ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar
46e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian#define ASPECT_RATIO_TOLERANCE 0.02f
47a02dce1ac438e0538fbcef1c663092035c5f4348Lajos Molnar
4878fce72baff38f488a0117591d4241d278a48b1bLajos Molnar#define min(a, b) ( { typeof(a) __a = (a), __b = (b); __a < __b ? __a : __b; } )
4978fce72baff38f488a0117591d4241d278a48b1bLajos Molnar#define max(a, b) ( { typeof(a) __a = (a), __b = (b); __a > __b ? __a : __b; } )
50d4599029522592f46eccf9d57add7ebd7d23fdd1Lajos Molnar#define swap(a, b) do { typeof(a) __a = (a); (a) = (b); (b) = __a; } while (0)
5178fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
522704d381ccfba0fe96e61cd368507506c3adfacaLajos Molnar#define WIDTH(rect) ((rect).right - (rect).left)
532704d381ccfba0fe96e61cd368507506c3adfacaLajos Molnar#define HEIGHT(rect) ((rect).bottom - (rect).top)
542704d381ccfba0fe96e61cd368507506c3adfacaLajos Molnar
5516b7f6c4b06ac7350c30309d0559a466097d8ae7Lajos Molnar#define DIV_ROUND_UP(a, b) (((a) + (b) - 1) / (b))
5616b7f6c4b06ac7350c30309d0559a466097d8ae7Lajos Molnar
57c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <video/dsscomp.h>
58876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen#include <video/omap_hwc.h>
59c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
60c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include "hal_public.h"
61876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen#include "rgz_2d.h"
62c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
6383f80658d865b741a024515c523e373df50c091eSunita Nadampalli#include <linux/ion.h>
6483f80658d865b741a024515c523e373df50c091eSunita Nadampalli#include <linux/omap_ion.h>
6583f80658d865b741a024515c523e373df50c091eSunita Nadampalli#include <ion/ion.h>
6683f80658d865b741a024515c523e373df50c091eSunita Nadampalli
67c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#define MAX_HW_OVERLAYS 4
6878fce72baff38f488a0117591d4241d278a48b1bLajos Molnar#define NUM_NONSCALING_OVERLAYS 1
6983f80658d865b741a024515c523e373df50c091eSunita Nadampalli#define NUM_EXT_DISPLAY_BACK_BUFFERS 2
70c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
71ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnarstruct ext_transform_t {
72ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    __u8 rotation : 3;          /* 90-degree clockwise rotations */
73ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    __u8 hflip    : 1;          /* flip l-r (after rotation) */
74ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    __u8 enabled  : 1;          /* cloning enabled */
75ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    __u8 docking  : 1;          /* docking vs. mirroring - used for state */
76ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar};
77ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar
78ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar/* cloning support and state */
79ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnarstruct omap4_hwc_ext {
80ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    /* support */
81ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    struct ext_transform_t mirror;      /* mirroring settings */
82ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    struct ext_transform_t dock;        /* docking settings */
833797fa989f5fee97caea2bbadf433a5a9ac03d8dLajos Molnar    float lcd_xpy;                      /* pixel ratio for UI */
844e635e537afaa9f1416fa5bdbfd1b03afd8b7e6aLajos Molnar    __u8 avoid_mode_change;             /* use HDMI mode used for mirroring if possible */
852b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar    __u8 force_dock;                     /* must dock */
862b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar    __u8 hdmi_state;                     /* whether HDMI is connected */
87ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar
88ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    /* state */
89ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    __u8 on_tv;                         /* using a tv */
90ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    struct ext_transform_t current;     /* current settings */
91ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    struct ext_transform_t last;        /* last-used settings */
92ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar
93ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    /* configuration */
941b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar    __u32 last_xres_used;               /* resolution and pixel ratio used for mode selection */
95ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    __u32 last_yres_used;
964cb51e55fa79a592d50d94c0700660e42c988a0bLajos Molnar    __u32 last_mode;                    /* 2-s complement of last HDMI mode set, 0 if none */
974e635e537afaa9f1416fa5bdbfd1b03afd8b7e6aLajos Molnar    __u32 mirror_mode;                  /* 2-s complement of mode used when mirroring */
981b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar    float last_xpy;
99ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    __u16 width;                        /* external screen dimensions */
100ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    __u16 height;
101ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    __u32 xres;                         /* external screen resolution */
102ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    __u32 yres;
103ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    float m[2][3];                      /* external transformation matrix */
1048d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    hwc_rect_t mirror_region;           /* region of screen to mirror */
105ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar};
106ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnartypedef struct omap4_hwc_ext omap4_hwc_ext_t;
107ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar
108ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar/* used by property settings */
10978fce72baff38f488a0117591d4241d278a48b1bLajos Molnarenum {
11078fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    EXT_ROTATION    = 3,        /* rotation while mirroring */
11178fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    EXT_HFLIP       = (1 << 2), /* flip l-r on output (after rotation) */
11278fce72baff38f488a0117591d4241d278a48b1bLajos Molnar};
11378fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
114876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenenum bltpolicy {
115876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    BLTPOLICY_DISABLED = 0,
116876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    BLTPOLICY_DEFAULT = 1,    /* Default blit policy */
117876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    BLTPOLICY_ALL,            /* Test mode to attempt to blit all */
118876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen};
119876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
120876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenenum bltmode {
121876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    BLTMODE_PAINT = 0,    /* Attempt to blit layer by layer */
122876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    BLTMODE_REGION = 1,   /* Attempt to blit layers via regions */
123876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen};
124876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1250aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar/* ARGB image */
1260aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnarstruct omap4_hwc_img {
1270aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    int width;
1280aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    int height;
1290aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    int rowbytes;
1300aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    int size;
1310aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    unsigned char *ptr;
1320aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar} dock_image = { .rowbytes = 0 };
1330aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
134c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstruct omap4_hwc_module {
135c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    hwc_module_t base;
136c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
137c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    IMG_framebuffer_device_public_t *fb_dev;
138c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev};
139c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevtypedef struct omap4_hwc_module omap4_hwc_module_t;
140c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
14158aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthousestruct counts {
14258aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    unsigned int possible_overlay_layers;
14358aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    unsigned int composited_layers;
14458aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    unsigned int scaled_layers;
14558aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    unsigned int RGB;
14658aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    unsigned int BGR;
14758aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    unsigned int NV12;
14858aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    unsigned int dockable;
14958aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    unsigned int protected;
15058aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse
15158aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    unsigned int max_hw_overlays;
15258aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    unsigned int max_scaling_overlays;
15358aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    unsigned int mem;
15458aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    unsigned int s3d;
15558aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse};
15658aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse
157c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstruct omap4_hwc_device {
15800d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar    /* static data */
1596ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    hwc_composer_device_1_t base;
1602125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    hwc_procs_t *procs;
1612125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    pthread_t hdmi_thread;
1622125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    pthread_mutex_t lock;
163c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
164c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    IMG_framebuffer_device_public_t *fb_dev;
165d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    struct dsscomp_display_info fb_dis;
16600d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar    int fb_fd;                  /* file descriptor for /dev/fb0 */
16700d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar    int dsscomp_fd;             /* file descriptor for /dev/dsscomp */
16800d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar    int hdmi_fb_fd;             /* file descriptor for /dev/fb1 */
169751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    int wakeup_evt;             /* eventfd used to wakeup event thread */
17000d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar
1710aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    int img_mem_size;           /* size of fb for hdmi */
1720aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    void *img_mem_ptr;          /* start of fb for hdmi */
1730aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
17400d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar    int flags_rgb_order;
17500d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar    int flags_nv12_only;
176f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar    float upscaled_nv12_limit;
177c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
178876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int on_tv;                  /* using a tv */
17900d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar    int force_sgx;
18000d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar    omap4_hwc_ext_t ext;        /* external mirroring data */
18100d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar    int idle;
182ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar
183876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    float primary_m[2][3];          /* internal transformation matrix */
184876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int primary_transform;
185876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int primary_rotation;
186876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    hwc_rect_t primary_region;
1875db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
188c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    buffer_handle_t *buffers;
189c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int use_sgx;
19078fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    int swap_rb;
191876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    unsigned int post2_layers; /* Buffers used with DSS pipes*/
192876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    unsigned int post2_blit_buffers; /* Buffers used with blit */
19300d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar    int ext_ovls;               /* # of overlays on external display for current composition */
19400d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar    int ext_ovls_wanted;        /* # of overlays that should be on external display for current composition */
19500d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar    int last_ext_ovls;          /* # of overlays on external/internal display for last composition */
19678fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    int last_int_ovls;
197876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
198876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    enum bltmode blt_mode;
199876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    enum bltpolicy blt_policy;
200876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
201876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int blit_flags;
202876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int blit_num;
203876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    struct omap_hwc_data comp_data; /* This is a kernel data structure */
204876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    struct rgz_blt_entry blit_ops[RGZ_MAX_BLITS];
20558aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    struct counts stats;
20683f80658d865b741a024515c523e373df50c091eSunita Nadampalli    int    ion_fd;
20783f80658d865b741a024515c523e373df50c091eSunita Nadampalli    struct ion_handle *ion_handles[2];
208751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
209751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    /* fake vsync event state */
210751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    pthread_mutex_t vsync_lock;
211751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    int vsync_enabled;
212751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    uint64_t last_vsync_time;
213751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    int last_vsync_time_valid;
214751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    uint64_t fake_vsync_period;
215c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev};
216c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevtypedef struct omap4_hwc_device omap4_hwc_device_t;
217c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
218734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar#define HAL_FMT(f) ((f) == HAL_PIXEL_FORMAT_TI_NV12 ? "NV12" : \
219dfc644c88eeac97a2dd0feb5967e746fa64dd1c6Tony Lofthouse                    (f) == HAL_PIXEL_FORMAT_TI_NV12_1D ? "NV12" : \
220734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar                    (f) == HAL_PIXEL_FORMAT_YV12 ? "YV12" : \
221734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar                    (f) == HAL_PIXEL_FORMAT_BGRX_8888 ? "xRGB32" : \
222734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar                    (f) == HAL_PIXEL_FORMAT_RGBX_8888 ? "xBGR32" : \
223734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar                    (f) == HAL_PIXEL_FORMAT_BGRA_8888 ? "ARGB32" : \
224734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar                    (f) == HAL_PIXEL_FORMAT_RGBA_8888 ? "ABGR32" : \
225734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar                    (f) == HAL_PIXEL_FORMAT_RGB_565 ? "RGB565" : "??")
226734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar
227734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar#define DSS_FMT(f) ((f) == OMAP_DSS_COLOR_NV12 ? "NV12" : \
228734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar                    (f) == OMAP_DSS_COLOR_RGB24U ? "xRGB32" : \
229734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar                    (f) == OMAP_DSS_COLOR_ARGB32 ? "ARGB32" : \
230734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar                    (f) == OMAP_DSS_COLOR_RGB16 ? "RGB565" : "??")
231734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar
232c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatic int debug = 0;
233876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenstatic int debugpost2 = 0;
234876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenstatic int debugblt = 0;
235876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenstatic rgz_t grgz;
236876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenstatic struct bvsurfgeom gscrngeom;
237c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2381fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthousestatic void showfps(void)
2391fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse{
2401fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse    static int framecount = 0;
2411fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse    static int lastframecount = 0;
2421fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse    static nsecs_t lastfpstime = 0;
2431fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse    static float fps = 0;
2441fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse    char value[PROPERTY_VALUE_MAX];
2451fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse
2461fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse    property_get("debug.hwc.showfps", value, "0");
2471fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse    if (!atoi(value)) {
2481fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse        return;
2491fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse    }
2501fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse
2511fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse    framecount++;
2521fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse    if (!(framecount & 0x7)) {
2531fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse        nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
2541fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse        nsecs_t diff = now - lastfpstime;
2551fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse        fps = ((framecount - lastframecount) * (float)(s2ns(1))) / diff;
2561fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse        lastfpstime = now;
2571fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse        lastframecount = framecount;
2581fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse        ALOGI("%d Frames, %f FPS", framecount, fps);
2591fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse    }
2601fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse}
2611fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse
2626ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFaddenstatic void dump_layer(hwc_layer_1_t const* l)
263c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2643a7df2c042eb8c7289e24e77dd316f73bd0c456fSteve Block    ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}, {%d,%d,%d,%d}",
265b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar         l->compositionType, l->flags, l->handle, l->transform, l->blending,
266b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar         l->sourceCrop.left,
267b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar         l->sourceCrop.top,
268b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar         l->sourceCrop.right,
269b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar         l->sourceCrop.bottom,
270b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar         l->displayFrame.left,
271b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar         l->displayFrame.top,
272b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar         l->displayFrame.right,
273b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar         l->displayFrame.bottom);
274c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
275c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
276c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatic void dump_dsscomp(struct dsscomp_setup_dispc_data *d)
277c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
27878fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    unsigned i;
279c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2803a7df2c042eb8c7289e24e77dd316f73bd0c456fSteve Block    ALOGD("[%08x] set: %c%c%c %d ovls\n",
281c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         d->sync_id,
282c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         (d->mode & DSSCOMP_SETUP_MODE_APPLY) ? 'A' : '-',
283c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         (d->mode & DSSCOMP_SETUP_MODE_DISPLAY) ? 'D' : '-',
284c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         (d->mode & DSSCOMP_SETUP_MODE_CAPTURE) ? 'C' : '-',
285c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         d->num_ovls);
286c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
28778fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    for (i = 0; i < d->num_mgrs; i++) {
28800d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar        struct dss2_mgr_info *mi = &d->mgrs[i];
2893a7df2c042eb8c7289e24e77dd316f73bd0c456fSteve Block        ALOGD(" (dis%d alpha=%d col=%08x ilace=%d)\n",
290b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar             mi->ix,
291b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar             mi->alpha_blending, mi->default_color,
292b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar             mi->interlaced);
29378fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    }
29478fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
295c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    for (i = 0; i < d->num_ovls; i++) {
29600d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar        struct dss2_ovl_info *oi = &d->ovls[i];
297b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar        struct dss2_ovl_cfg *c = &oi->cfg;
298b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar        if (c->zonly)
2993a7df2c042eb8c7289e24e77dd316f73bd0c456fSteve Block            ALOGD("ovl%d(%s z%d)\n",
300b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar                 c->ix, c->enabled ? "ON" : "off", c->zorder);
301b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar        else
3023a7df2c042eb8c7289e24e77dd316f73bd0c456fSteve Block            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",
303b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar                 c->ix, c->enabled ? "ON" : "off", c->zorder, DSS_FMT(c->color_mode),
304b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar                 c->pre_mult_alpha ? " premult" : "",
305b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar                 (c->global_alpha * 100 + 128) / 255,
306b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar                 c->width, c->height, c->crop.x, c->crop.y,
307b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar                 c->crop.w, c->crop.h,
308b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar                 c->rotation, c->mirror ? "+mir" : "",
309b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar                 c->win.x, c->win.y, c->win.w, c->win.h,
310b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar                 (void *) oi->ba, (void *) oi->uv, c->stride);
311c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
312c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
313c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
314734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnarstruct dump_buf {
315734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    char *buf;
316734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    int buf_len;
317734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    int len;
318734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar};
319734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar
320734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnarstatic void dump_printf(struct dump_buf *buf, const char *fmt, ...)
321734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar{
322734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    va_list ap;
323734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar
324734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    va_start(ap, fmt);
325734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    buf->len += vsnprintf(buf->buf + buf->len, buf->buf_len - buf->len, fmt, ap);
326734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    va_end(ap);
327734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar}
328734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar
3296ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFaddenstatic void dump_set_info(omap4_hwc_device_t *hwc_dev, hwc_display_contents_1_t* list)
330734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar{
331876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
332734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    char logbuf[1024];
333734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    struct dump_buf log = {
334734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        .buf = logbuf,
335734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        .buf_len = sizeof(logbuf),
336734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    };
337734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    unsigned int i;
338734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar
339734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    dump_printf(&log, "set H{");
340734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    for (i = 0; list && i < list->numHwLayers; i++) {
341734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        if (i)
342734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar            dump_printf(&log, " ");
3436ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        hwc_layer_1_t *layer = &list->hwLayers[i];
344734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
345876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        if (hwc_dev->post2_blit_buffers) {
346876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            if ((i + 1) < hwc_dev->post2_layers)
347876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                dump_printf(&log, "%p:%s,", handle, "DSS");
348876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            else
349876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                dump_printf(&log, "%p:%s,", handle, "BV2D");
350876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        }
351876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        else
352876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            dump_printf(&log, "%p:%s,", handle, layer->compositionType == HWC_OVERLAY ? "DSS" : "SGX");
353734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        if ((layer->flags & HWC_SKIP_LAYER) || !handle) {
354734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar            dump_printf(&log, "SKIP");
355734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar            continue;
356734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        }
357734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        if (layer->flags & HWC_HINT_CLEAR_FB)
358734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar            dump_printf(&log, "CLR,");
359734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        dump_printf(&log, "%d*%d(%s)", handle->iWidth, handle->iHeight, HAL_FMT(handle->iFormat));
360734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        if (layer->transform)
361734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar            dump_printf(&log, "~%d", layer->transform);
362734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    }
363734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    dump_printf(&log, "} D{");
364734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    for (i = 0; i < dsscomp->num_ovls; i++) {
365734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        if (i)
366734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar            dump_printf(&log, " ");
367734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        dump_printf(&log, "%d=", dsscomp->ovls[i].cfg.ix);
368734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        if (dsscomp->ovls[i].cfg.enabled)
369734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar            dump_printf(&log, "%08x:%d*%d,%s",
370734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar                        dsscomp->ovls[i].ba,
371734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar                        dsscomp->ovls[i].cfg.width,
372734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar                        dsscomp->ovls[i].cfg.height,
373734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar                        DSS_FMT(dsscomp->ovls[i].cfg.color_mode));
374734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        else
375734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar            dump_printf(&log, "-");
376734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    }
377734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    dump_printf(&log, "} L{");
378734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    for (i = 0; i < hwc_dev->post2_layers; i++) {
379734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        if (i)
380734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar            dump_printf(&log, " ");
381734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        dump_printf(&log, "%p", hwc_dev->buffers[i]);
382734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    }
383876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (hwc_dev->post2_blit_buffers) {
384876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        dump_printf(&log, "} B{");
385876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        for (i = hwc_dev->post2_layers;
386876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen             i < hwc_dev->post2_blit_buffers + hwc_dev->post2_layers; i++) {
387876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            dump_printf(&log, "%p ", hwc_dev->buffers[i]);
388876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        }
389876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
390734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    dump_printf(&log, "}%s\n", hwc_dev->use_sgx ? " swap" : "");
391734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar
3923a7df2c042eb8c7289e24e77dd316f73bd0c456fSteve Block    ALOGD("%s", log.buf);
393734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar}
394734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar
3955706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnarstatic int sync_id = 0;
3965706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar
397c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatic int omap4_hwc_is_valid_format(int format)
398c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
399c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    switch(format) {
400c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case HAL_PIXEL_FORMAT_RGB_565:
401c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case HAL_PIXEL_FORMAT_RGBX_8888:
402c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case HAL_PIXEL_FORMAT_RGBA_8888:
403c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case HAL_PIXEL_FORMAT_BGRA_8888:
404c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case HAL_PIXEL_FORMAT_BGRX_8888:
405c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case HAL_PIXEL_FORMAT_TI_NV12:
406dfc644c88eeac97a2dd0feb5967e746fa64dd1c6Tony Lofthouse    case HAL_PIXEL_FORMAT_TI_NV12_1D:
407c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return 1;
408c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
409c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    default:
410c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return 0;
411c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
412c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
413c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
414876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenstatic int scaled(hwc_layer_1_t *layer)
415c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
4162704d381ccfba0fe96e61cd368507506c3adfacaLajos Molnar    int w = WIDTH(layer->sourceCrop);
4172704d381ccfba0fe96e61cd368507506c3adfacaLajos Molnar    int h = HEIGHT(layer->sourceCrop);
418d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
419d4599029522592f46eccf9d57add7ebd7d23fdd1Lajos Molnar    if (layer->transform & HWC_TRANSFORM_ROT_90)
420d4599029522592f46eccf9d57add7ebd7d23fdd1Lajos Molnar        swap(w, h);
421d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
422876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    return WIDTH(layer->displayFrame) != w || HEIGHT(layer->displayFrame) != h;
423c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
424c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
4256ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFaddenstatic int is_protected(hwc_layer_1_t *layer)
4264ce532200f33ca2c77cd66de924c83decaf7fa30Sunita Nadampalli{
4274ce532200f33ca2c77cd66de924c83decaf7fa30Sunita Nadampalli    IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
4284ce532200f33ca2c77cd66de924c83decaf7fa30Sunita Nadampalli
4294ce532200f33ca2c77cd66de924c83decaf7fa30Sunita Nadampalli    return (handle->usage & GRALLOC_USAGE_PROTECTED);
4304ce532200f33ca2c77cd66de924c83decaf7fa30Sunita Nadampalli}
4314ce532200f33ca2c77cd66de924c83decaf7fa30Sunita Nadampalli
4325706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar#define is_BLENDED(layer) ((layer)->blending != HWC_BLENDING_NONE)
4335706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar
434fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrechtstatic int is_RGB32(IMG_native_handle_t *handle)
435fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht{
436fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht    switch(handle->iFormat)
437fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht    {
438fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht    case HAL_PIXEL_FORMAT_BGRA_8888:
439fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht    case HAL_PIXEL_FORMAT_BGRX_8888:
440fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht        return 1;
441fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht    default:
442fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht        return 0;
443fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht    }
444fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht}
445fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht
4465706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnarstatic int is_RGB(IMG_native_handle_t *handle)
4475706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar{
4485706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    switch(handle->iFormat)
4495706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    {
4505706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    case HAL_PIXEL_FORMAT_BGRA_8888:
4515706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    case HAL_PIXEL_FORMAT_BGRX_8888:
4525706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    case HAL_PIXEL_FORMAT_RGB_565:
4535706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar        return 1;
4545706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    default:
4555706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar        return 0;
4565706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    }
4575706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar}
458a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthousestatic int get_rgb_bpp(IMG_native_handle_t *handle)
459a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse{
460a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse    switch(handle->iFormat)
461a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse    {
462a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse    case HAL_PIXEL_FORMAT_BGRA_8888:
463a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse    case HAL_PIXEL_FORMAT_BGRX_8888:
464a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse    case HAL_PIXEL_FORMAT_RGBX_8888:
465a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse    case HAL_PIXEL_FORMAT_RGBA_8888:
466a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse        return 32;
467a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse    case HAL_PIXEL_FORMAT_RGB_565:
468a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse        return 16;
469a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse    default:
470a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse        return 0;
471a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse    }
472a41d64f2cc51096c1110f518b948c316bf15943bTony Lofthouse}
473c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
4745706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnarstatic int is_BGR_format(int format)
4755706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar{
4765706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    switch (format) {
4775706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    case HAL_PIXEL_FORMAT_RGBX_8888:
4785706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    case HAL_PIXEL_FORMAT_RGBA_8888:
4795706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar        return 1;
4805706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    default:
4815706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar        return 0;
4825706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    }
4835706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar}
484a3a76c576a0a1706c102570697b4fc0017eb46fbMathias Agopian
4855706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnarstatic int is_BGR(IMG_native_handle_t *handle)
4865706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar{
4875706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    return is_BGR_format(handle->iFormat);
4885706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar}
4895706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar
4905706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnarstatic int is_NV12(IMG_native_handle_t *handle)
4915706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar{
4925706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    switch(handle->iFormat)
4935706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    {
4945706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    case HAL_PIXEL_FORMAT_TI_NV12:
495dfc644c88eeac97a2dd0feb5967e746fa64dd1c6Tony Lofthouse    case HAL_PIXEL_FORMAT_TI_NV12_1D:
4965706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar        return 1;
4975706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    default:
4985706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar        return 0;
4995706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    }
5005706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar}
501c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
502f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnarstatic int is_upscaled_NV12(omap4_hwc_device_t *hwc_dev, hwc_layer_t *layer)
503f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar{
504f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar    if (!layer)
505f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar        return 0;
506f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar
507f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar    IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
508f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar    if (!is_NV12(handle))
509f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar        return 0;
510f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar
511f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar    int w = WIDTH(layer->sourceCrop);
512f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar    int h = HEIGHT(layer->sourceCrop);
513f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar
514f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar    if (layer->transform & HWC_TRANSFORM_ROT_90)
515f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar        swap(w, h);
516f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar
517f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar    return (WIDTH(layer->displayFrame) >= w * hwc_dev->upscaled_nv12_limit ||
518f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar            HEIGHT(layer->displayFrame) >= h * hwc_dev->upscaled_nv12_limit);
519f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar}
520f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar
521f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnarstatic int dockable(hwc_layer_t *layer)
522834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar{
523834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar    IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
524834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar
525834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar    return (handle->usage & GRALLOC_USAGE_EXTERNAL_DISP);
526834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar}
527834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar
528751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chenstatic uint64_t vsync_clock_now()
529751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen{
530751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    uint64_t now = 0;
531751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    struct timespec ts;
532751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
533751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    if (!clock_gettime(CLOCK_MONOTONIC, &ts))
534751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        now = ((uint64_t)ts.tv_sec) * 1000000000ull + ts.tv_nsec;
535751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
536751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    return now;
537751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen}
538751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
539751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chenstatic void wakeup_hdmi_thread(omap4_hwc_device_t *hwc_dev)
540751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen{
541751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    uint64_t tmp = 1;
542751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    write(hwc_dev->wakeup_evt, &tmp, sizeof(tmp));
543751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen}
544751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
545751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chenstatic void fire_vsync_event(omap4_hwc_device_t *hwc_dev, uint64_t timestamp)
546751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen{
547751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    pthread_mutex_lock(&hwc_dev->vsync_lock);
548751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
549751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    hwc_dev->last_vsync_time_valid = 1;
550751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    hwc_dev->last_vsync_time = timestamp;
551751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
552751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    pthread_mutex_unlock(&hwc_dev->vsync_lock);
553751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
554876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (hwc_dev->procs && hwc_dev->procs->vsync)
555751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        hwc_dev->procs->vsync(hwc_dev->procs, 0, timestamp);
556751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen}
557751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
55838934f125a600817dded7aeba9639c6a75afe358Lajos Molnarstatic unsigned int mem1d(IMG_native_handle_t *handle)
55938934f125a600817dded7aeba9639c6a75afe358Lajos Molnar{
5605706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    if (handle == NULL || is_NV12(handle))
561b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar        return 0;
56238934f125a600817dded7aeba9639c6a75afe358Lajos Molnar
5635706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    int bpp = handle->iFormat == HAL_PIXEL_FORMAT_RGB_565 ? 2 : 4;
56438934f125a600817dded7aeba9639c6a75afe358Lajos Molnar    int stride = ALIGN(handle->iWidth, HW_ALIGN) * bpp;
56538934f125a600817dded7aeba9639c6a75afe358Lajos Molnar    return stride * handle->iHeight;
56638934f125a600817dded7aeba9639c6a75afe358Lajos Molnar}
56738934f125a600817dded7aeba9639c6a75afe358Lajos Molnar
568c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatic void
5695706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnaromap4_hwc_setup_layer_base(struct dss2_ovl_cfg *oc, int index, int format, int blended, int width, int height)
570c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
571c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    unsigned int bits_per_pixel;
572c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
573c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* YUV2RGB conversion */
574c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    const struct omap_dss_cconv_coefs ctbl_bt601_5 = {
575c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        298,  409,    0,  298, -208, -100,  298,    0,  517, 0,
576c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    };
577c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
578c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* convert color format */
579c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    switch (format) {
580c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case HAL_PIXEL_FORMAT_RGBA_8888:
581c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case HAL_PIXEL_FORMAT_BGRA_8888:
582c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        oc->color_mode = OMAP_DSS_COLOR_ARGB32;
583c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        bits_per_pixel = 32;
5845706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar        if (blended)
5855706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar                break;
586c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
5875706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    case HAL_PIXEL_FORMAT_RGBX_8888:
588c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case HAL_PIXEL_FORMAT_BGRX_8888:
589c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        oc->color_mode = OMAP_DSS_COLOR_RGB24U;
590c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        bits_per_pixel = 32;
591c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
592c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
593c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case HAL_PIXEL_FORMAT_RGB_565:
594c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        oc->color_mode = OMAP_DSS_COLOR_RGB16;
595c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        bits_per_pixel = 16;
596c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
597c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
598c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case HAL_PIXEL_FORMAT_TI_NV12:
599dfc644c88eeac97a2dd0feb5967e746fa64dd1c6Tony Lofthouse    case HAL_PIXEL_FORMAT_TI_NV12_1D:
600c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        oc->color_mode = OMAP_DSS_COLOR_NV12;
601c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        bits_per_pixel = 8;
602c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        oc->cconv = ctbl_bt601_5;
603c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
604c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
605c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    default:
606c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        /* Should have been filtered out */
607ac837f2654f5a7a7fbecf05e4b085b87a7701714Steve Block        ALOGV("Unsupported pixel format");
608c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return;
609c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
610c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
611c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->width = width;
612c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->height = height;
613c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->stride = ALIGN(width, HW_ALIGN) * bits_per_pixel / 8;
614c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
615c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->enabled = 1;
616c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->global_alpha = 255;
617c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->zorder = index;
618c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->ix = 0;
619c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
620c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* defaults for SGX framebuffer renders */
621c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->crop.w = oc->win.w = width;
622c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->crop.h = oc->win.h = height;
623c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
624c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* for now interlacing and vc1 info is not supplied */
625c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->ilace = OMAP_DSS_ILACE_NONE;
626c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->vc1.enable = 0;
627c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
628c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
629c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatic void
630c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevomap4_hwc_setup_layer(omap4_hwc_device_t *hwc_dev, struct dss2_ovl_info *ovl,
6316ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden                      hwc_layer_1_t *layer, int index,
632c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                      int format, int width, int height)
633c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
634c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    struct dss2_ovl_cfg *oc = &ovl->cfg;
635c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
636c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    //dump_layer(layer);
637c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
6385706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    omap4_hwc_setup_layer_base(oc, index, format, is_BLENDED(layer), width, height);
639c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
640c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* convert transformation - assuming 0-set config */
641c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (layer->transform & HWC_TRANSFORM_FLIP_H)
642c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        oc->mirror = 1;
643c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (layer->transform & HWC_TRANSFORM_FLIP_V) {
644c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        oc->rotation = 2;
64578fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        oc->mirror = !oc->mirror;
646c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
647c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (layer->transform & HWC_TRANSFORM_ROT_90) {
648c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        oc->rotation += oc->mirror ? -1 : 1;
649c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        oc->rotation &= 3;
650c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
651c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
652c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->pre_mult_alpha = layer->blending == HWC_BLENDING_PREMULT;
653c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
654c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* display position */
655c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->win.x = layer->displayFrame.left;
656c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->win.y = layer->displayFrame.top;
6572704d381ccfba0fe96e61cd368507506c3adfacaLajos Molnar    oc->win.w = WIDTH(layer->displayFrame);
6582704d381ccfba0fe96e61cd368507506c3adfacaLajos Molnar    oc->win.h = HEIGHT(layer->displayFrame);
659c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
660c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* crop */
661c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->crop.x = layer->sourceCrop.left;
662c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    oc->crop.y = layer->sourceCrop.top;
6632704d381ccfba0fe96e61cd368507506c3adfacaLajos Molnar    oc->crop.w = WIDTH(layer->sourceCrop);
6642704d381ccfba0fe96e61cd368507506c3adfacaLajos Molnar    oc->crop.h = HEIGHT(layer->sourceCrop);
665c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
666c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
6672952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnarconst float m_unit[2][3] = { { 1., 0., 0. }, { 0., 1., 0. } };
6682952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
6692952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnarstatic inline void m_translate(float m[2][3], int dx, int dy)
6702952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar{
6712952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    m[0][2] += dx;
6722952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    m[1][2] += dy;
6732952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar}
6742952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
6752952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnarstatic inline void m_scale1(float m[3], int from, int to)
6762952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar{
6772952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    m[0] = m[0] * to / from;
6782952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    m[1] = m[1] * to / from;
6792952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    m[2] = m[2] * to / from;
6802952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar}
6812952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
6822952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnarstatic inline void m_scale(float m[2][3], int x_from, int x_to, int y_from, int y_to)
6832952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar{
6842952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    m_scale1(m[0], x_from, x_to);
6852952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    m_scale1(m[1], y_from, y_to);
6862952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar}
6872952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
6882952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnarstatic void m_rotate(float m[2][3], int quarter_turns)
6892952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar{
6902952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    if (quarter_turns & 2)
6912952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar        m_scale(m, 1, -1, 1, -1);
6922952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    if (quarter_turns & 1) {
6932952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar        int q;
6942952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar        q = m[0][0]; m[0][0] = -m[1][0]; m[1][0] = q;
6952952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar        q = m[0][1]; m[0][1] = -m[1][1]; m[1][1] = q;
6962952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar        q = m[0][2]; m[0][2] = -m[1][2]; m[1][2] = q;
6972952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    }
6982952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar}
6992952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
7002952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnarstatic inline int m_round(float x)
7012952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar{
7022952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    /* int truncates towards 0 */
7032952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    return (int) (x < 0 ? x - 0.5 : x + 0.5);
7042952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar}
7052952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
706ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar/*
7071b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar * assuming xpy (xratio:yratio) original pixel ratio, calculate the adjusted width
708ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar * and height for a screen of xres/yres and physical size of width/height.
709ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar * The adjusted size is the largest that fits into the screen.
710ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar */
711ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnarstatic void get_max_dimensions(__u32 orig_xres, __u32 orig_yres,
7121b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar                               float xpy,
713ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar                               __u32 scr_xres, __u32 scr_yres,
714ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar                               __u32 scr_width, __u32 scr_height,
715ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar                               __u32 *adj_xres, __u32 *adj_yres)
716ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar{
717ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar    /* assume full screen (largest size)*/
718ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar    *adj_xres = scr_xres;
719ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar    *adj_yres = scr_yres;
720ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar
721ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar    /* assume 1:1 pixel ratios if none supplied */
722ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar    if (!scr_width || !scr_height) {
723ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar        scr_width = scr_xres;
724ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar        scr_height = scr_yres;
725ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar    }
726ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar
727ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar    /* trim to keep aspect ratio */
7281b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar    float x_factor = orig_xres * xpy * scr_height;
7291b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar    float y_factor = orig_yres *       scr_width;
730ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar
731ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar    /* allow for tolerance so we avoid scaling if framebuffer is standard size */
732ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar    if (x_factor < y_factor * (1.f - ASPECT_RATIO_TOLERANCE))
733ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar        *adj_xres = (__u32) (x_factor * *adj_xres / y_factor + 0.5);
734ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar    else if (x_factor * (1.f - ASPECT_RATIO_TOLERANCE) > y_factor)
735ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar        *adj_yres = (__u32) (y_factor * *adj_yres / x_factor + 0.5);
736ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar}
737ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar
7388d546610cba3698c62af537d97e38a7368362f1aLajos Molnarstatic void set_ext_matrix(omap4_hwc_ext_t *ext, struct hwc_rect region)
7392952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar{
7408d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    int orig_w = WIDTH(region);
7418d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    int orig_h = HEIGHT(region);
7423797fa989f5fee97caea2bbadf433a5a9ac03d8dLajos Molnar    float xpy = ext->lcd_xpy;
7432952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
7442952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    /* reorientation matrix is:
7452952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar       m = (center-from-target-center) * (scale-to-target) * (mirror) * (rotate) * (center-to-original-center) */
7462952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
747ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    memcpy(ext->m, m_unit, sizeof(m_unit));
7488d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    m_translate(ext->m, -(orig_w >> 1) - region.left, -(orig_h >> 1) - region.top);
749ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    m_rotate(ext->m, ext->current.rotation);
750ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    if (ext->current.hflip)
751ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        m_scale(ext->m, 1, -1, 1, 1);
7522952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
753ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    if (ext->current.rotation & 1) {
754d4599029522592f46eccf9d57add7ebd7d23fdd1Lajos Molnar        swap(orig_w, orig_h);
7551b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar        xpy = 1. / xpy;
7562952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    }
7572952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
758ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar    /* get target size */
759ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar    __u32 adj_xres, adj_yres;
7601b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar    get_max_dimensions(orig_w, orig_h, xpy,
761ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar                       ext->xres, ext->yres, ext->width, ext->height,
762ae5ccc208800debb41acfc45096d318847e8db50Lajos Molnar                       &adj_xres, &adj_yres);
7632952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
764ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    m_scale(ext->m, orig_w, adj_xres, orig_h, adj_yres);
765ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    m_translate(ext->m, ext->xres >> 1, ext->yres >> 1);
7662952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar}
7672952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
7688d546610cba3698c62af537d97e38a7368362f1aLajos Molnarstatic int
7698d546610cba3698c62af537d97e38a7368362f1aLajos Molnarcrop_to_rect(struct dss2_ovl_cfg *cfg, struct hwc_rect vis_rect)
7708d546610cba3698c62af537d97e38a7368362f1aLajos Molnar{
7718d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    struct {
7728d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        int xy[2];
7738d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        int wh[2];
7748d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    } crop, win;
7758d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    struct {
7768d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        int lt[2];
7778d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        int rb[2];
7788d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    } vis;
7798d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    win.xy[0] = cfg->win.x; win.xy[1] = cfg->win.y;
7808d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    win.wh[0] = cfg->win.w; win.wh[1] = cfg->win.h;
7818d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    crop.xy[0] = cfg->crop.x; crop.xy[1] = cfg->crop.y;
7828d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    crop.wh[0] = cfg->crop.w; crop.wh[1] = cfg->crop.h;
7838d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    vis.lt[0] = vis_rect.left; vis.lt[1] = vis_rect.top;
7848d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    vis.rb[0] = vis_rect.right; vis.rb[1] = vis_rect.bottom;
7858d546610cba3698c62af537d97e38a7368362f1aLajos Molnar
7868d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    int c, swap = cfg->rotation & 1;
7878d546610cba3698c62af537d97e38a7368362f1aLajos Molnar
7888d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    /* align crop window with display coordinates */
7898d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    if (swap)
7908d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        crop.xy[1] -= (crop.wh[1] = -crop.wh[1]);
7918d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    if (cfg->rotation & 2)
7928d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        crop.xy[!swap] -= (crop.wh[!swap] = -crop.wh[!swap]);
7938d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    if ((!cfg->mirror) ^ !(cfg->rotation & 2))
7948d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        crop.xy[swap] -= (crop.wh[swap] = -crop.wh[swap]);
7958d546610cba3698c62af537d97e38a7368362f1aLajos Molnar
7968d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    for (c = 0; c < 2; c++) {
7978d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        /* see if complete buffer is outside the vis or it is
7988d546610cba3698c62af537d97e38a7368362f1aLajos Molnar          fully cropped or scaled to 0 */
7998d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        if (win.wh[c] <= 0 || vis.rb[c] <= vis.lt[c] ||
8008d546610cba3698c62af537d97e38a7368362f1aLajos Molnar            win.xy[c] + win.wh[c] <= vis.lt[c] ||
8018d546610cba3698c62af537d97e38a7368362f1aLajos Molnar            win.xy[c] >= vis.rb[c] ||
8028d546610cba3698c62af537d97e38a7368362f1aLajos Molnar            !crop.wh[c ^ swap])
8038d546610cba3698c62af537d97e38a7368362f1aLajos Molnar            return -ENOENT;
8048d546610cba3698c62af537d97e38a7368362f1aLajos Molnar
8058d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        /* crop left/top */
8068d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        if (win.xy[c] < vis.lt[c]) {
8078d546610cba3698c62af537d97e38a7368362f1aLajos Molnar            /* correction term */
8088d546610cba3698c62af537d97e38a7368362f1aLajos Molnar            int a = (vis.lt[c] - win.xy[c]) * crop.wh[c ^ swap] / win.wh[c];
8098d546610cba3698c62af537d97e38a7368362f1aLajos Molnar            crop.xy[c ^ swap] += a;
8108d546610cba3698c62af537d97e38a7368362f1aLajos Molnar            crop.wh[c ^ swap] -= a;
8118d546610cba3698c62af537d97e38a7368362f1aLajos Molnar            win.wh[c] -= vis.lt[c] - win.xy[c];
8128d546610cba3698c62af537d97e38a7368362f1aLajos Molnar            win.xy[c] = vis.lt[c];
8138d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        }
8148d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        /* crop right/bottom */
8158d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        if (win.xy[c] + win.wh[c] > vis.rb[c]) {
8168d546610cba3698c62af537d97e38a7368362f1aLajos Molnar            crop.wh[c ^ swap] = crop.wh[c ^ swap] * (vis.rb[c] - win.xy[c]) / win.wh[c];
8178d546610cba3698c62af537d97e38a7368362f1aLajos Molnar            win.wh[c] = vis.rb[c] - win.xy[c];
8188d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        }
8198d546610cba3698c62af537d97e38a7368362f1aLajos Molnar
8208d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        if (!crop.wh[c ^ swap] || !win.wh[c])
8218d546610cba3698c62af537d97e38a7368362f1aLajos Molnar            return -ENOENT;
8228d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    }
8238d546610cba3698c62af537d97e38a7368362f1aLajos Molnar
8248d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    /* realign crop window to buffer coordinates */
8258d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    if (cfg->rotation & 2)
8268d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        crop.xy[!swap] -= (crop.wh[!swap] = -crop.wh[!swap]);
8278d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    if ((!cfg->mirror) ^ !(cfg->rotation & 2))
8288d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        crop.xy[swap] -= (crop.wh[swap] = -crop.wh[swap]);
8298d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    if (swap)
8308d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        crop.xy[1] -= (crop.wh[1] = -crop.wh[1]);
8318d546610cba3698c62af537d97e38a7368362f1aLajos Molnar
8328d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    cfg->win.x = win.xy[0]; cfg->win.y = win.xy[1];
8338d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    cfg->win.w = win.wh[0]; cfg->win.h = win.wh[1];
8348d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    cfg->crop.x = crop.xy[0]; cfg->crop.y = crop.xy[1];
8358d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    cfg->crop.w = crop.wh[0]; cfg->crop.h = crop.wh[1];
8368d546610cba3698c62af537d97e38a7368362f1aLajos Molnar
8378d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    return 0;
8388d546610cba3698c62af537d97e38a7368362f1aLajos Molnar}
8398d546610cba3698c62af537d97e38a7368362f1aLajos Molnar
8402952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnarstatic void
841876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenomap4_hwc_apply_transform(float transform[2][3],struct dss2_ovl_cfg *oc)
8422952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar{
8432952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    float x, y, w, h;
8442952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
8452952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    /* display position */
846876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    x = transform[0][0] * oc->win.x + transform[0][1] * oc->win.y + transform[0][2];
847876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    y = transform[1][0] * oc->win.x + transform[1][1] * oc->win.y + transform[1][2];
848876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    w = transform[0][0] * oc->win.w + transform[0][1] * oc->win.h;
849876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    h = transform[1][0] * oc->win.w + transform[1][1] * oc->win.h;
8502952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    oc->win.x = m_round(w > 0 ? x : x + w);
8512952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    oc->win.y = m_round(h > 0 ? y : y + h);
8522952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    oc->win.w = m_round(w > 0 ? w : -w);
8532952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar    oc->win.h = m_round(h > 0 ? h : -h);
8542952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar}
8552952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar
8565db21d521ed41e2e2cc110b698981e841e027287Dandawate Saketstatic void
857876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenomap4_hwc_adjust_ext_layer(omap4_hwc_ext_t *ext, struct dss2_ovl_info *ovl)
8585db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket{
8595db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    struct dss2_ovl_cfg *oc = &ovl->cfg;
8605db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
861876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /* crop to clone region if mirroring */
862876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (!ext->current.docking &&
863876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        crop_to_rect(&ovl->cfg, ext->mirror_region) != 0) {
8645db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        ovl->cfg.enabled = 0;
8655db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        return;
8665db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    }
8675db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
868876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    omap4_hwc_apply_transform(ext->m, oc);
8695db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
8705db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    /* combining transformations: F^a*R^b*F^i*R^j = F^(a+b)*R^(j+b*(-1)^i), because F*R = R^(-1)*F */
871876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    oc->rotation += (oc->mirror ? -1 : 1) * ext->current.rotation;
8725db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    oc->rotation &= 3;
873876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (ext->current.hflip)
874876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        oc->mirror = !oc->mirror;
8755db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket}
876876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
877876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenstatic struct dsscomp_platform_info limits;
878876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
8795db21d521ed41e2e2cc110b698981e841e027287Dandawate Saketstatic void
880876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenomap4_hwc_adjust_primary_display_layer(omap4_hwc_device_t *hwc_dev, struct dss2_ovl_info *ovl)
8815db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket{
8825db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    struct dss2_ovl_cfg *oc = &ovl->cfg;
8835db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
884876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (crop_to_rect(&ovl->cfg, hwc_dev->primary_region) != 0) {
8855db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        ovl->cfg.enabled = 0;
8865db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        return;
8875db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    }
8885db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
889876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    omap4_hwc_apply_transform(hwc_dev->primary_m, oc);
8905db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
891876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /* combining transformations: F^a*R^b*F^i*R^j = F^(a+b)*R^(j+b*(-1)^i), because F*R = R^(-1)*F */
892876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    oc->rotation += (oc->mirror ? -1 : 1) * hwc_dev->primary_rotation;
893876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    oc->rotation &= 3;
8945db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket}
8955db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
896e055e4bf77e6c429d7a159e8e853974d9dfc57adLajos Molnarstatic int omap4_hwc_can_scale(__u32 src_w, __u32 src_h, __u32 dst_w, __u32 dst_h, int is_2d,
897876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                               struct dsscomp_display_info *dis, struct dsscomp_platform_info *limits,
8984a3fef97a616e47d9b26a8cffa2833f230aba19bTony Lofthouse                               __u32 pclk, void *handle)
899d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar{
900d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    __u32 fclk = limits->fclk / 1000;
90116b7f6c4b06ac7350c30309d0559a466097d8ae7Lajos Molnar    __u32 min_src_w = DIV_ROUND_UP(src_w, is_2d ? limits->max_xdecim_2d : limits->max_xdecim_1d);
90216b7f6c4b06ac7350c30309d0559a466097d8ae7Lajos Molnar    __u32 min_src_h = DIV_ROUND_UP(src_h, is_2d ? limits->max_ydecim_2d : limits->max_ydecim_1d);
903d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
904d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    /* ERRATAs */
905d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    /* cannot render 1-width layers on DSI video mode panels - we just disallow all 1-width LCD layers */
906d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    if (dis->channel != OMAP_DSS_CHANNEL_DIGIT && dst_w < limits->min_width)
907d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar        return 0;
908d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
909d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    /* NOTE: no support for checking YUV422 layers that are tricky to scale */
910d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
9111a45d7e2f85f1e3f3eeef590aa52316376055440Mathias Agopian    /* FIXME: limit vertical downscale well below theoretical limit as we saw display artifacts */
9121a45d7e2f85f1e3f3eeef590aa52316376055440Mathias Agopian    if (dst_h < src_h / 4)
9131a45d7e2f85f1e3f3eeef590aa52316376055440Mathias Agopian        return 0;
9141a45d7e2f85f1e3f3eeef590aa52316376055440Mathias Agopian
915d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    /* max downscale */
91616b7f6c4b06ac7350c30309d0559a466097d8ae7Lajos Molnar    if (dst_h * limits->max_downscale < min_src_h)
917d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar        return 0;
918d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
9194ceb627d195b2efb8cfa3612f38cee211436a7a2Lajos Molnar    /* for manual panels pclk is 0, and there are no pclk based scaling limits */
9204ceb627d195b2efb8cfa3612f38cee211436a7a2Lajos Molnar    if (!pclk)
921b319783cc9e00318eb67b554029bef5932d5436dSunita Nadampalli        return !(dst_w < src_w / limits->max_downscale / (is_2d ? limits->max_xdecim_2d : limits->max_xdecim_1d));
9224ceb627d195b2efb8cfa3612f38cee211436a7a2Lajos Molnar
923d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    /* :HACK: limit horizontal downscale well below theoretical limit as we saw display artifacts */
92416b7f6c4b06ac7350c30309d0559a466097d8ae7Lajos Molnar    if (dst_w * 4 < src_w)
925d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar        return 0;
926d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
927b29f69b6b65a33ec0052036e8b6bf0c219cc157eTony Lofthouse    if (handle)
928b29f69b6b65a33ec0052036e8b6bf0c219cc157eTony Lofthouse        if (get_rgb_bpp(handle) == 32 && src_w > 1280 && dst_w * 3 < src_w)
929b29f69b6b65a33ec0052036e8b6bf0c219cc157eTony Lofthouse            return 0;
930b29f69b6b65a33ec0052036e8b6bf0c219cc157eTony Lofthouse
931d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    /* max horizontal downscale is 4, or the fclk/pixclk */
932d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    if (fclk > pclk * limits->max_downscale)
933d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar        fclk = pclk * limits->max_downscale;
934d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    /* for small parts, we need to use integer fclk/pixclk */
935d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    if (src_w < limits->integer_scale_ratio_limit)
936d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar        fclk = fclk / pclk * pclk;
93716b7f6c4b06ac7350c30309d0559a466097d8ae7Lajos Molnar    if ((__u32) dst_w * fclk < min_src_w * pclk)
938d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar        return 0;
939d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
940d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    return 1;
941d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar}
942d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
9436ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFaddenstatic int omap4_hwc_can_scale_layer(omap4_hwc_device_t *hwc_dev, hwc_layer_1_t *layer, IMG_native_handle_t *handle)
944d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar{
9452704d381ccfba0fe96e61cd368507506c3adfacaLajos Molnar    int src_w = WIDTH(layer->sourceCrop);
9462704d381ccfba0fe96e61cd368507506c3adfacaLajos Molnar    int src_h = HEIGHT(layer->sourceCrop);
9472704d381ccfba0fe96e61cd368507506c3adfacaLajos Molnar    int dst_w = WIDTH(layer->displayFrame);
9482704d381ccfba0fe96e61cd368507506c3adfacaLajos Molnar    int dst_h = HEIGHT(layer->displayFrame);
949d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
950d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    /* account for 90-degree rotation */
951d4599029522592f46eccf9d57add7ebd7d23fdd1Lajos Molnar    if (layer->transform & HWC_TRANSFORM_ROT_90)
952d4599029522592f46eccf9d57add7ebd7d23fdd1Lajos Molnar        swap(src_w, src_h);
953d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
9544ceb627d195b2efb8cfa3612f38cee211436a7a2Lajos Molnar    /* NOTE: layers should be able to be scaled externally since
9554ceb627d195b2efb8cfa3612f38cee211436a7a2Lajos Molnar       framebuffer is able to be scaled on selected external resolution */
9565706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    return omap4_hwc_can_scale(src_w, src_h, dst_w, dst_h, is_NV12(handle), &hwc_dev->fb_dis, &limits,
9574a3fef97a616e47d9b26a8cffa2833f230aba19bTony Lofthouse                               hwc_dev->fb_dis.timings.pixel_clock, handle);
958d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar}
959d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
960d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnarstatic int omap4_hwc_is_valid_layer(omap4_hwc_device_t *hwc_dev,
9616ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden                                    hwc_layer_1_t *layer,
962c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                    IMG_native_handle_t *handle)
963c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
964c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* Skip layers are handled by SF */
965c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ((layer->flags & HWC_SKIP_LAYER) || !handle)
966c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return 0;
967c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
968c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (!omap4_hwc_is_valid_format(handle->iFormat))
969c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return 0;
970c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
971c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* 1D buffers: no transform, must fit in TILER slot */
9725706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar    if (!is_NV12(handle)) {
973c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if (layer->transform)
974c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            return 0;
975876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        if (mem1d(handle) > limits.tiler1d_slot_size)
976c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            return 0;
977c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
978d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
979d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    return omap4_hwc_can_scale_layer(hwc_dev, layer, handle);
980c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
981c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
982e14d52936107e390e2464df1e6653efde5409096Lajos Molnarstatic __u32 add_scaling_score(__u32 score,
983e14d52936107e390e2464df1e6653efde5409096Lajos Molnar                               __u32 xres, __u32 yres, __u32 refresh,
984e14d52936107e390e2464df1e6653efde5409096Lajos Molnar                               __u32 ext_xres, __u32 ext_yres,
985e14d52936107e390e2464df1e6653efde5409096Lajos Molnar                               __u32 mode_xres, __u32 mode_yres, __u32 mode_refresh)
986e14d52936107e390e2464df1e6653efde5409096Lajos Molnar{
987e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    __u32 area = xres * yres;
988e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    __u32 ext_area = ext_xres * ext_yres;
989e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    __u32 mode_area = mode_xres * mode_yres;
990e14d52936107e390e2464df1e6653efde5409096Lajos Molnar
991e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    /* prefer to upscale (1% tolerance) [0..1] (insert after 1st bit) */
992e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    int upscale = (ext_xres >= xres * 99 / 100 && ext_yres >= yres * 99 / 100);
993e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    score = (((score & ~1) | upscale) << 1) | (score & 1);
994e14d52936107e390e2464df1e6653efde5409096Lajos Molnar
995e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    /* pick minimum scaling [0..16] */
996e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    if (ext_area > area)
997e14d52936107e390e2464df1e6653efde5409096Lajos Molnar        score = (score << 5) | (16 * area / ext_area);
998e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    else
999e14d52936107e390e2464df1e6653efde5409096Lajos Molnar        score = (score << 5) | (16 * ext_area / area);
1000e14d52936107e390e2464df1e6653efde5409096Lajos Molnar
1001e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    /* pick smallest leftover area [0..16] */
1002e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    score = (score << 5) | ((16 * ext_area + (mode_area >> 1)) / mode_area);
1003e14d52936107e390e2464df1e6653efde5409096Lajos Molnar
1004e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    /* adjust mode refresh rate */
1005e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    mode_refresh += mode_refresh % 6 == 5;
1006e14d52936107e390e2464df1e6653efde5409096Lajos Molnar
1007e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    /* prefer same or higher frame rate */
1008e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    upscale = (mode_refresh >= refresh);
1009e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    score = (score << 1) | upscale;
1010e14d52936107e390e2464df1e6653efde5409096Lajos Molnar
1011e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    /* pick closest frame rate */
1012e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    if (mode_refresh > refresh)
1013e14d52936107e390e2464df1e6653efde5409096Lajos Molnar        score = (score << 8) | (240 * refresh / mode_refresh);
1014e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    else
1015e14d52936107e390e2464df1e6653efde5409096Lajos Molnar        score = (score << 8) | (240 * mode_refresh / refresh);
1016e14d52936107e390e2464df1e6653efde5409096Lajos Molnar
1017e14d52936107e390e2464df1e6653efde5409096Lajos Molnar    return score;
1018e14d52936107e390e2464df1e6653efde5409096Lajos Molnar}
1019e14d52936107e390e2464df1e6653efde5409096Lajos Molnar
102004c7041e0dc1e9c7d2c4e1be1e78a092de993c27Lajos Molnarstatic int omap4_hwc_set_best_hdmi_mode(omap4_hwc_device_t *hwc_dev, __u32 xres, __u32 yres,
10211b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar                                        float xpy)
1022bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar{
10235db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    int dis_ix = hwc_dev->on_tv ? 0 : 1;
1024ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen    int forced_preferred_mode = 0;
10255db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
1026bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar    struct _qdis {
1027bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        struct dsscomp_display_info dis;
102804512dd8a3fd2830139e3f352a295841c7eed190Travis Geiselbrecht        struct dsscomp_videomode modedb[32];
10295db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    } d = { .dis = { .ix = dis_ix } };
1030ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    omap4_hwc_ext_t *ext = &hwc_dev->ext;
1031bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar
1032bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar    d.dis.modedb_len = sizeof(d.modedb) / sizeof(*d.modedb);
1033cb64c2be21ad9550b1f5d2b4a8361e997b9be757Lajos Molnar    int ret = ioctl(hwc_dev->dsscomp_fd, DSSCIOC_QUERY_DISPLAY, &d);
1034bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar    if (ret)
1035bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        return ret;
1036bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar
10376c12df17d9a51d20ca799ca22ffee6818d438c94Lajos Molnar    if (d.dis.timings.x_res * d.dis.timings.y_res == 0 ||
10386c12df17d9a51d20ca799ca22ffee6818d438c94Lajos Molnar        xres * yres == 0)
10396c12df17d9a51d20ca799ca22ffee6818d438c94Lajos Molnar        return -EINVAL;
10406c12df17d9a51d20ca799ca22ffee6818d438c94Lajos Molnar
1041bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar    __u32 i, best = ~0, best_score = 0;
1042ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    ext->width = d.dis.width_in_mm;
1043ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    ext->height = d.dis.height_in_mm;
1044ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    ext->xres = d.dis.timings.x_res;
1045ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    ext->yres = d.dis.timings.y_res;
10463837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
10473837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    /* use VGA external resolution as default */
10483837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    if (!ext->xres || !ext->yres) {
10493837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        ext->xres = 640;
10503837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        ext->yres = 480;
10513837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    }
10523837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
1053ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht    /*
1054ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht     * copy the xres/yres from the preferred mode
1055ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht     */
1056ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht    __u32 preferred_mode_xres = 0;
1057ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht    __u32 preferred_mode_yres = 0;
1058ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen
1059ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen    char value[PROPERTY_VALUE_MAX];
1060ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen    if (property_get("persist.hwc.preferred_mode", value, "") <= 0 ||
1061ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen        sscanf(value, "%dx%d", &preferred_mode_xres,
1062ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen               &preferred_mode_yres) != 2) {
1063ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen        for (i = 0; i < d.dis.modedb_len; i++) {
1064ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen            if (d.modedb[i].flag & FB_FLAG_PREFERRED) {
1065ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen                preferred_mode_xres = d.modedb[i].xres;
1066ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen                preferred_mode_yres = d.modedb[i].yres;
1067ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen                ALOGD("preferred mode %d: xres %u yres %u\n",
1068ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen                    i, d.modedb[i].xres, d.modedb[i].yres);
1069ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen                break;
1070ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen            }
1071ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht        }
1072ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen    } else {
1073ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen        ALOGD("forced preferred mode xres %u yres %u\n",
1074ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen             preferred_mode_xres, preferred_mode_yres);
1075ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen        forced_preferred_mode = 1;
1076ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht    }
1077ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht
1078bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar    __u32 ext_fb_xres, ext_fb_yres;
1079bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar    for (i = 0; i < d.dis.modedb_len; i++) {
1080bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        __u32 score = 0;
1081e14d52936107e390e2464df1e6653efde5409096Lajos Molnar        __u32 mode_xres = d.modedb[i].xres;
1082e14d52936107e390e2464df1e6653efde5409096Lajos Molnar        __u32 mode_yres = d.modedb[i].yres;
1083bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        __u32 ext_width = d.dis.width_in_mm;
1084bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        __u32 ext_height = d.dis.height_in_mm;
1085bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar
1086ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht        /* reject it because the hw says it can't actually use this mode */
1087ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht        if ((d.modedb[i].flag & FB_FLAG_HW_CAPABLE) == 0)
1088ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht            continue;
1089ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht
1090620a972bf780aff56d3a3c76e8f53ca8b0a3c17dSunita Nadampalli        if (d.modedb[i].vmode & FB_VMODE_INTERLACED)
1091620a972bf780aff56d3a3c76e8f53ca8b0a3c17dSunita Nadampalli            mode_yres /= 2;
1092620a972bf780aff56d3a3c76e8f53ca8b0a3c17dSunita Nadampalli
1093bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        if (d.modedb[i].flag & FB_FLAG_RATIO_4_3) {
1094bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar            ext_width = 4;
1095bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar            ext_height = 3;
1096bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        } else if (d.modedb[i].flag & FB_FLAG_RATIO_16_9) {
1097bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar            ext_width = 16;
1098bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar            ext_height = 9;
1099bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        }
1100bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar
1101e14d52936107e390e2464df1e6653efde5409096Lajos Molnar        if (!mode_xres || !mode_yres)
11026c12df17d9a51d20ca799ca22ffee6818d438c94Lajos Molnar            continue;
11036c12df17d9a51d20ca799ca22ffee6818d438c94Lajos Molnar
1104e14d52936107e390e2464df1e6653efde5409096Lajos Molnar        get_max_dimensions(xres, yres, xpy, mode_xres, mode_yres,
1105bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar                           ext_width, ext_height, &ext_fb_xres, &ext_fb_yres);
1106bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar
1107f4524c063d4cc4afeb19b08966f51b42257d3acdLajos Molnar        /* we need to ensure that even TILER2D buffers can be scaled */
11084ceb627d195b2efb8cfa3612f38cee211436a7a2Lajos Molnar        if (!d.modedb[i].pixclock ||
1109620a972bf780aff56d3a3c76e8f53ca8b0a3c17dSunita Nadampalli            (d.modedb[i].vmode & ~FB_VMODE_INTERLACED) ||
11104ceb627d195b2efb8cfa3612f38cee211436a7a2Lajos Molnar            !omap4_hwc_can_scale(xres, yres, ext_fb_xres, ext_fb_yres,
1111ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar                                 1, &d.dis, &limits,
11124a3fef97a616e47d9b26a8cffa2833f230aba19bTony Lofthouse                                 1000000000 / d.modedb[i].pixclock, NULL))
1113bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar            continue;
1114bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar
1115bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        /* prefer CEA modes */
1116bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        if (d.modedb[i].flag & (FB_FLAG_RATIO_4_3 | FB_FLAG_RATIO_16_9))
1117ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht            score += 1;
1118ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht
1119ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht        /* prefer modes that match the preferred mode's resolution */
1120ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht        if (d.modedb[i].xres == preferred_mode_xres &&
1121ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht            d.modedb[i].yres == preferred_mode_yres) {
1122ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen            if (forced_preferred_mode)
1123ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen                score += 10;
1124ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen            else
1125ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen                score += 1;
1126ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht        }
1127ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht
1128ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht        /* prefer modes the kernel has hinted is the correct mode */
1129ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen        if (!forced_preferred_mode && (d.modedb[i].flag & FB_FLAG_PREFERRED))
1130ab9d83c73ae44ed9a8479d17ab4ab6b11354e8c4Travis Geiselbrecht            score += 1;
1131bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar
11324e635e537afaa9f1416fa5bdbfd1b03afd8b7e6aLajos Molnar        /* prefer the same mode as we use for mirroring to avoid mode change */
1133ef2e2467d7c5487cf3be888956adcdc686b03412Mike J. Chen        score = (score << 1) | (i == ~ext->mirror_mode && ext->avoid_mode_change);
1134bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar
1135e14d52936107e390e2464df1e6653efde5409096Lajos Molnar        score = add_scaling_score(score, xres, yres, 60, ext_fb_xres, ext_fb_yres,
1136e14d52936107e390e2464df1e6653efde5409096Lajos Molnar                                  mode_xres, mode_yres, d.modedb[i].refresh ? : 1);
1137bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar
1138876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        ALOGD("#%d: %dx%d %dHz flag 0x%x vmode 0x%x, score 0x%x",
1139876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen             i, mode_xres, mode_yres,
1140876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen             d.modedb[i].refresh, d.modedb[i].flag, d.modedb[i].vmode,
1141876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen             score);
1142876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1143bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        if (debug)
11443a7df2c042eb8c7289e24e77dd316f73bd0c456fSteve Block            ALOGD("  score=0x%x adj.res=%dx%d", score, ext_fb_xres, ext_fb_yres);
1145bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        if (best_score < score) {
1146ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar            ext->width = ext_width;
1147ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar            ext->height = ext_height;
1148e14d52936107e390e2464df1e6653efde5409096Lajos Molnar            ext->xres = mode_xres;
1149e14d52936107e390e2464df1e6653efde5409096Lajos Molnar            ext->yres = mode_yres;
1150bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar            best = i;
1151bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar            best_score = score;
1152bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        }
1153bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar    }
1154bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar    if (~best) {
11555db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        struct dsscomp_setup_display_data sdis = { .ix = dis_ix, };
1156bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        sdis.mode = d.dis.modedb[best];
11573a7df2c042eb8c7289e24e77dd316f73bd0c456fSteve Block        ALOGD("picking #%d", best);
11584cb51e55fa79a592d50d94c0700660e42c988a0bLajos Molnar        /* only reconfigure on change */
11595142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen        if (ext->last_mode != ~best) {
11605142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen            /* set a property that apps that care (e.g. YouTube) can use
11615142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen             * to determine whether or not to stream lower resolution
11625142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen             * videos when the hdmi mode is < 1080p.
11635142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen             * otherwise, they'd give us 1080p and we'd just scale it
11645142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen             * down to the hdmi mode res.  UI apps are always going
11655142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen             * to draw at 1080p and we'll scale down because the
11665142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen             * system can't support dynamic dpi changes.
11675142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen             */
11685142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen            char display[PROPERTY_VALUE_MAX];
11695142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen            snprintf(display, sizeof(display), "%dx%d",
11705142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen                     d.modedb[best].xres, d.modedb[best].yres);
11715142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen            ALOGD("setting property sys.display-size to %s", display);
11725142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen            property_set("sys.display-size", display);
11735142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen
1174cb64c2be21ad9550b1f5d2b4a8361e997b9be757Lajos Molnar            ioctl(hwc_dev->dsscomp_fd, DSSCIOC_SETUP_DISPLAY, &sdis);
11755142031872205241cfc707a7ac9bd77d095ff0f8Mike J. Chen        }
11764cb51e55fa79a592d50d94c0700660e42c988a0bLajos Molnar        ext->last_mode = ~best;
1177bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar    } else {
1178bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        __u32 ext_width = d.dis.width_in_mm;
1179bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        __u32 ext_height = d.dis.height_in_mm;
1180bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        __u32 ext_fb_xres, ext_fb_yres;
1181bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar
11821b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar        get_max_dimensions(xres, yres, xpy, d.dis.timings.x_res, d.dis.timings.y_res,
1183bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar                           ext_width, ext_height, &ext_fb_xres, &ext_fb_yres);
11844ceb627d195b2efb8cfa3612f38cee211436a7a2Lajos Molnar        if (!d.dis.timings.pixel_clock ||
11854ceb627d195b2efb8cfa3612f38cee211436a7a2Lajos Molnar            !omap4_hwc_can_scale(xres, yres, ext_fb_xres, ext_fb_yres,
1186ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar                                 1, &d.dis, &limits,
11874a3fef97a616e47d9b26a8cffa2833f230aba19bTony Lofthouse                                 d.dis.timings.pixel_clock, NULL)) {
1188a4e4aeab47b17a82524ac56f2d69daa3e47c1ce7Steve Block            ALOGW("DSS scaler cannot support HDMI cloning");
1189bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar            return -1;
1190bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar        }
1191bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar    }
1192ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    ext->last_xres_used = xres;
1193ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    ext->last_yres_used = yres;
11941b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar    ext->last_xpy = xpy;
1195bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar    if (d.dis.channel == OMAP_DSS_CHANNEL_DIGIT)
1196ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        ext->on_tv = 1;
1197bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar    return 0;
1198bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar}
1199bd43f27d21c976df59645759598e530ba1a8e01eLajos Molnar
12006ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFaddenstatic void gather_layer_statistics(omap4_hwc_device_t *hwc_dev, struct counts *num, hwc_display_contents_1_t *list)
120144a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar{
120244a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    unsigned int i;
120344a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar
120444a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    /* Figure out how many layers we can support via DSS */
120544a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    for (i = 0; list && i < list->numHwLayers; i++) {
12066ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        hwc_layer_1_t *layer = &list->hwLayers[i];
120744a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar        IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
120844a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar
120944a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar        layer->compositionType = HWC_FRAMEBUFFER;
121044a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar
121144a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar        if (omap4_hwc_is_valid_layer(hwc_dev, layer, handle)) {
121244a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar            num->possible_overlay_layers++;
121344a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar
121444a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar            /* NV12 layers can only be rendered on scaling overlays */
1215876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            if (scaled(layer) || is_NV12(handle) || hwc_dev->primary_transform)
121644a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar                num->scaled_layers++;
121744a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar
121844a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar            if (is_BGR(handle))
121944a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar                num->BGR++;
122044a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar            else if (is_RGB(handle))
122144a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar                num->RGB++;
122244a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar            else if (is_NV12(handle))
122344a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar                num->NV12++;
122444a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar
122544a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar            if (dockable(layer))
122644a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar                num->dockable++;
122744a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar
122844a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar            if (is_protected(layer))
122944a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar                num->protected++;
123044a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar
123144a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar            num->mem += mem1d(handle);
123244a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar        }
123344a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    }
123458aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    hwc_dev->stats = *num;
123544a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar}
123644a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar
123744a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnarstatic void decide_supported_cloning(omap4_hwc_device_t *hwc_dev, struct counts *num)
123878fce72baff38f488a0117591d4241d278a48b1bLajos Molnar{
1239ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    omap4_hwc_ext_t *ext = &hwc_dev->ext;
124078fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    int nonscaling_ovls = NUM_NONSCALING_OVERLAYS;
124178fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    num->max_hw_overlays = MAX_HW_OVERLAYS;
124278fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
124378fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    /*
124478fce72baff38f488a0117591d4241d278a48b1bLajos Molnar     * We cannot atomically switch overlays from one display to another.  First, they
124578fce72baff38f488a0117591d4241d278a48b1bLajos Molnar     * have to be disabled, and the disabling has to take effect on the current display.
124678fce72baff38f488a0117591d4241d278a48b1bLajos Molnar     * We keep track of the available number of overlays here.
124778fce72baff38f488a0117591d4241d278a48b1bLajos Molnar     */
12480aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    if (ext->dock.enabled && !(ext->mirror.enabled && !(num->dockable || ext->force_dock))) {
124978fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        /* some overlays may already be used by the external display, so we account for this */
125078fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
125178fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        /* reserve just a video pipeline for HDMI if docking */
12520aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        hwc_dev->ext_ovls = (num->dockable || ext->force_dock) ? 1 : 0;
125378fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        num->max_hw_overlays -= max(hwc_dev->ext_ovls, hwc_dev->last_ext_ovls);
12541b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar
12551b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar        /* use mirroring transform if we are auto-switching to docking mode while mirroring*/
12561b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar        if (ext->mirror.enabled) {
12571b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar            ext->current = ext->mirror;
12581b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar            ext->current.docking = 1;
12591b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar        } else {
12601b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar            ext->current = ext->dock;
12611b24d08333d1f847e6211aaa1b81f5792aca2704Lajos Molnar        }
1262ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    } else if (ext->mirror.enabled) {
126378fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        /*
126478fce72baff38f488a0117591d4241d278a48b1bLajos Molnar         * otherwise, manage just from half the pipelines.  NOTE: there is
126578fce72baff38f488a0117591d4241d278a48b1bLajos Molnar         * no danger of having used too many overlays for external display here.
126678fce72baff38f488a0117591d4241d278a48b1bLajos Molnar         */
12676d94b7785c5318b6de36193a7f5488a88b6178ebDandawate Saket
126878fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        num->max_hw_overlays >>= 1;
126978fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        nonscaling_ovls >>= 1;
127078fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        hwc_dev->ext_ovls = MAX_HW_OVERLAYS - num->max_hw_overlays;
1271ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        ext->current = ext->mirror;
127278fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    } else {
127378fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        num->max_hw_overlays -= hwc_dev->last_ext_ovls;
127478fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        hwc_dev->ext_ovls = 0;
1275ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        ext->current.enabled = 0;
127678fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    }
127778fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
127878fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    /*
127978fce72baff38f488a0117591d4241d278a48b1bLajos Molnar     * :TRICKY: We may not have enough overlays on the external display.  We "reserve" them
128078fce72baff38f488a0117591d4241d278a48b1bLajos Molnar     * here to figure out if mirroring is supported, but may not do mirroring for the first
128178fce72baff38f488a0117591d4241d278a48b1bLajos Molnar     * frame while the overlays required for it are cleared.
128278fce72baff38f488a0117591d4241d278a48b1bLajos Molnar     */
1283bd0fe036293115c9f9b182ed2a911bdd557ea491Lajos Molnar    hwc_dev->ext_ovls_wanted = hwc_dev->ext_ovls;
128478fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    hwc_dev->ext_ovls = min(MAX_HW_OVERLAYS - hwc_dev->last_int_ovls, hwc_dev->ext_ovls);
128578fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
1286ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    /* if mirroring, we are limited by both internal and external overlays.  However,
1287ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar       ext_ovls is always <= MAX_HW_OVERLAYS / 2 <= max_hw_overlays */
128858aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    if (!num->protected && hwc_dev->ext_ovls && ext->current.enabled && !ext->current.docking)
1289b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar        num->max_hw_overlays = hwc_dev->ext_ovls;
129078fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
12916d94b7785c5318b6de36193a7f5488a88b6178ebDandawate Saket    /* If FB is not same resolution as LCD don't use GFX pipe line*/
1292876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (hwc_dev->primary_transform) {
1293876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        num->max_hw_overlays -= NUM_NONSCALING_OVERLAYS;
12946d94b7785c5318b6de36193a7f5488a88b6178ebDandawate Saket        num->max_scaling_overlays = num->max_hw_overlays;
1295876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    } else
12965db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        num->max_scaling_overlays = num->max_hw_overlays - nonscaling_ovls;
129744a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar}
129878fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
129944a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnarstatic int can_dss_render_all(omap4_hwc_device_t *hwc_dev, struct counts *num)
130044a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar{
130144a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    omap4_hwc_ext_t *ext = &hwc_dev->ext;
13025db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    int on_tv = hwc_dev->on_tv || (ext->on_tv && ext->current.enabled);
130344a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    int tform = ext->current.enabled && (ext->current.rotation || ext->current.hflip);
1304ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar
13054b267e18b9e98b788d2b1bf2f1268ed25e5b726dLajos Molnar    return  !hwc_dev->force_sgx &&
13064b267e18b9e98b788d2b1bf2f1268ed25e5b726dLajos Molnar            /* must have at least one layer if using composition bypass to get sync object */
130778fce72baff38f488a0117591d4241d278a48b1bLajos Molnar            num->possible_overlay_layers &&
130878fce72baff38f488a0117591d4241d278a48b1bLajos Molnar            num->possible_overlay_layers <= num->max_hw_overlays &&
130978fce72baff38f488a0117591d4241d278a48b1bLajos Molnar            num->possible_overlay_layers == num->composited_layers &&
131078fce72baff38f488a0117591d4241d278a48b1bLajos Molnar            num->scaled_layers <= num->max_scaling_overlays &&
13114af5978e0677b13118611721f265e2dbaba849ffLajos Molnar            num->NV12 <= num->max_scaling_overlays &&
131238934f125a600817dded7aeba9639c6a75afe358Lajos Molnar            /* fits into TILER slot */
1313876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            num->mem <= limits.tiler1d_slot_size &&
131478fce72baff38f488a0117591d4241d278a48b1bLajos Molnar            /* we cannot clone non-NV12 transformed layers */
13155a87355f6a7ed393d5d8f71bf7aac5f65b87c640Muralidhar Dixit            (!tform || (num->NV12 == num->possible_overlay_layers) ||
13165a87355f6a7ed393d5d8f71bf7aac5f65b87c640Muralidhar Dixit            (num->NV12 && ext->current.docking)) &&
131778fce72baff38f488a0117591d4241d278a48b1bLajos Molnar            /* HDMI cannot display BGR */
1318ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar            (num->BGR == 0 || (num->RGB == 0 && !on_tv) || !hwc_dev->flags_rgb_order);
131978fce72baff38f488a0117591d4241d278a48b1bLajos Molnar}
132078fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
132178fce72baff38f488a0117591d4241d278a48b1bLajos Molnarstatic inline int can_dss_render_layer(omap4_hwc_device_t *hwc_dev,
13226ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden            hwc_layer_1_t *layer)
132378fce72baff38f488a0117591d4241d278a48b1bLajos Molnar{
132478fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
132578fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
132644a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    omap4_hwc_ext_t *ext = &hwc_dev->ext;
1327876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int cloning = ext->current.enabled && (!ext->current.docking || (handle!=NULL ? dockable(layer) : 0));
1328876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int on_tv = hwc_dev->on_tv || (ext->on_tv && cloning);
1329876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int tform = cloning && (ext->current.rotation || ext->current.hflip);
133078fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
1331d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    return omap4_hwc_is_valid_layer(hwc_dev, layer, handle) &&
13322952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar           /* cannot rotate non-NV12 layers on external display */
13335706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar           (!tform || is_NV12(handle)) &&
133478fce72baff38f488a0117591d4241d278a48b1bLajos Molnar           /* skip non-NV12 layers if also using SGX (if nv12_only flag is set) */
13355706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar           (!hwc_dev->flags_nv12_only || (!hwc_dev->use_sgx || is_NV12(handle))) &&
133678fce72baff38f488a0117591d4241d278a48b1bLajos Molnar           /* make sure RGB ordering is consistent (if rgb_order flag is set) */
13375706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar           (!(hwc_dev->swap_rb ? is_RGB(handle) : is_BGR(handle)) ||
133878fce72baff38f488a0117591d4241d278a48b1bLajos Molnar            !hwc_dev->flags_rgb_order) &&
133978fce72baff38f488a0117591d4241d278a48b1bLajos Molnar           /* TV can only render RGB */
13405706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar           !(on_tv && is_BGR(handle));
134178fce72baff38f488a0117591d4241d278a48b1bLajos Molnar}
134278fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
1343834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnarstatic inline int display_area(struct dss2_ovl_info *o)
1344834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar{
1345834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar    return o->cfg.win.w * o->cfg.win.h;
1346834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar}
1347834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar
13483837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnarstatic int clone_layer(omap4_hwc_device_t *hwc_dev, int ix) {
1349876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
13503837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    omap4_hwc_ext_t *ext = &hwc_dev->ext;
13513837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    int ext_ovl_ix = dsscomp->num_ovls - hwc_dev->post2_layers;
13523837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    struct dss2_ovl_info *o = &dsscomp->ovls[dsscomp->num_ovls];
13533837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
13543837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    if (dsscomp->num_ovls >= MAX_HW_OVERLAYS) {
135546de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("**** cannot clone layer #%d. using all %d overlays.", ix, dsscomp->num_ovls);
13563837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        return -EBUSY;
13573837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    }
13583837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
13593837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    memcpy(o, dsscomp->ovls + ix, sizeof(*o));
13603837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
13613837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    /* reserve overlays at end for other display */
13623837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    o->cfg.ix = MAX_HW_OVERLAYS - 1 - ext_ovl_ix;
13633837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    o->cfg.mgr_ix = 1;
136483f80658d865b741a024515c523e373df50c091eSunita Nadampalli    /*
136583f80658d865b741a024515c523e373df50c091eSunita Nadampalli    * Here the assumption is that overlay0 is the one attached to FB.
136683f80658d865b741a024515c523e373df50c091eSunita Nadampalli    * Hence this clone_layer call is for FB cloning (provided use_sgx is true).
136783f80658d865b741a024515c523e373df50c091eSunita Nadampalli    */
136883f80658d865b741a024515c523e373df50c091eSunita Nadampalli    /* For the external displays whose transform is the same as
136983f80658d865b741a024515c523e373df50c091eSunita Nadampalli    * that of primary display, ion_handles would be NULL hence
137083f80658d865b741a024515c523e373df50c091eSunita Nadampalli    * the below logic doesn't execute.
137183f80658d865b741a024515c523e373df50c091eSunita Nadampalli    */
137283f80658d865b741a024515c523e373df50c091eSunita Nadampalli    if (ix == 0 && hwc_dev->ion_handles[sync_id%2] && hwc_dev->use_sgx) {
137383f80658d865b741a024515c523e373df50c091eSunita Nadampalli        o->addressing = OMAP_DSS_BUFADDR_ION;
137483f80658d865b741a024515c523e373df50c091eSunita Nadampalli        o->ba = (int)hwc_dev->ion_handles[sync_id%2];
137583f80658d865b741a024515c523e373df50c091eSunita Nadampalli    } else {
137683f80658d865b741a024515c523e373df50c091eSunita Nadampalli        o->addressing = OMAP_DSS_BUFADDR_OVL_IX;
137783f80658d865b741a024515c523e373df50c091eSunita Nadampalli        o->ba = ix;
137883f80658d865b741a024515c523e373df50c091eSunita Nadampalli    }
13793837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
13803837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    /* use distinct z values (to simplify z-order checking) */
13813837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    o->cfg.zorder += hwc_dev->post2_layers;
13823837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
13833837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    omap4_hwc_adjust_ext_layer(&hwc_dev->ext, o);
13843837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    dsscomp->num_ovls++;
13853837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    return 0;
13863837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar}
13873837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
13883837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnarstatic int clone_external_layer(omap4_hwc_device_t *hwc_dev, int ix) {
1389876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
13903837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    omap4_hwc_ext_t *ext = &hwc_dev->ext;
13913837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
13923837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    /* mirror only 1 external layer */
13933837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    struct dss2_ovl_info *o = &dsscomp->ovls[ix];
13943837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
13953837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    /* full screen video after transformation */
13963837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    __u32 xres = o->cfg.crop.w, yres = o->cfg.crop.h;
13973837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    if ((ext->current.rotation + o->cfg.rotation) & 1)
13983837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        swap(xres, yres);
13993837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    float xpy = ext->lcd_xpy * o->cfg.win.w / o->cfg.win.h;
14003837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    if (o->cfg.rotation & 1)
14013837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        xpy = o->cfg.crop.h / xpy / o->cfg.crop.w;
14023837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    else
14033837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        xpy = o->cfg.crop.h * xpy / o->cfg.crop.w;
14043837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    if (ext->current.rotation & 1)
14053837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        xpy = 1. / xpy;
14063837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
14073837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    /* adjust hdmi mode based on resolution */
14083837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    if (xres != ext->last_xres_used ||
14093837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        yres != ext->last_yres_used ||
14103837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        xpy < ext->last_xpy * (1.f - ASPECT_RATIO_TOLERANCE) ||
14113837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        xpy * (1.f - ASPECT_RATIO_TOLERANCE) > ext->last_xpy) {
14123a7df2c042eb8c7289e24e77dd316f73bd0c456fSteve Block        ALOGD("set up HDMI for %d*%d\n", xres, yres);
14133837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        if (omap4_hwc_set_best_hdmi_mode(hwc_dev, xres, yres, xpy)) {
14143837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar            ext->current.enabled = 0;
14153837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar            return -ENODEV;
14163837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        }
14173837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    }
14183837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
14193837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    struct hwc_rect region = {
14203837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        .left = o->cfg.win.x, .top = o->cfg.win.y,
14213837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        .right = o->cfg.win.x + o->cfg.win.w,
14223837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        .bottom = o->cfg.win.y + o->cfg.win.h
14233837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    };
14243837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    set_ext_matrix(&hwc_dev->ext, region);
14253837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
14263837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    return clone_layer(hwc_dev, ix);
14273837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar}
14283837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
14293837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnarstatic int setup_mirroring(omap4_hwc_device_t *hwc_dev)
14303837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar{
14313837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    omap4_hwc_ext_t *ext = &hwc_dev->ext;
14323837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
14333837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    __u32 xres = WIDTH(ext->mirror_region);
14343837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    __u32 yres = HEIGHT(ext->mirror_region);
14353837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    if (ext->current.rotation & 1)
14363837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar       swap(xres, yres);
14373837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    if (omap4_hwc_set_best_hdmi_mode(hwc_dev, xres, yres, ext->lcd_xpy))
14383837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        return -ENODEV;
14393837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    set_ext_matrix(ext, ext->mirror_region);
14403837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    return 0;
14413837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar}
14423837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
14436ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden/*
14446ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden * We're using "implicit" synchronization, so make sure we aren't passing any
14456ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden * sync object descriptors around.
14466ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden */
14476ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFaddenstatic void check_sync_fds(size_t numDisplays, hwc_display_contents_1_t** displays)
14486ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden{
14496ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    //ALOGD("checking sync FDs");
14506ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    unsigned int i, j;
14516ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    for (i = 0; i < numDisplays; i++) {
14526ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        hwc_display_contents_1_t* list = displays[i];
14533d6b69f0a0fb325443ef8c2987c16af1f8618d3eJesse Hall        if (list->retireFenceFd >= 0) {
14543d6b69f0a0fb325443ef8c2987c16af1f8618d3eJesse Hall            ALOGW("retireFenceFd[%u] was %d", i, list->retireFenceFd);
14553d6b69f0a0fb325443ef8c2987c16af1f8618d3eJesse Hall            list->retireFenceFd = -1;
14566ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        }
14576ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden
14586ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        for (j = 0; j < list->numHwLayers; j++) {
14596ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden            hwc_layer_1_t* layer = &list->hwLayers[j];
14606ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden            if (layer->acquireFenceFd >= 0) {
14616ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden                ALOGW("acquireFenceFd[%u][%u] was %d, closing", i, j, layer->acquireFenceFd);
14626ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden                close(layer->acquireFenceFd);
14636ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden                layer->acquireFenceFd = -1;
14646ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden            }
14656ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden            if (layer->releaseFenceFd >= 0) {
14666ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden                ALOGW("releaseFenceFd[%u][%u] was %d", i, j, layer->releaseFenceFd);
14676ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden                layer->releaseFenceFd = -1;
14686ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden            }
14696ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        }
14706ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    }
14716ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden}
14726ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden
1473fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht/* test if layer appears to be RGB32 (4 Bpp) and > 1280x720 */
1474fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrechtstatic int is_large_rgb32_layer(const hwc_layer_1_t *layer)
1475fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht{
1476fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht    IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
1477fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht
1478fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht    return is_RGB32(handle) &&
1479fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht        (((layer->sourceCrop.right - layer->sourceCrop.left) > 1280) ||
1480fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht        ((layer->sourceCrop.bottom - layer->sourceCrop.top) > 720));
1481fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht}
1482fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht
1483876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenstatic void blit_reset(omap4_hwc_device_t *hwc_dev, int flags)
1484876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen{
1485876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    hwc_dev->blit_flags = 0;
1486876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    hwc_dev->blit_num = 0;
1487876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    hwc_dev->post2_blit_buffers = 0;
1488876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    hwc_dev->comp_data.blit_data.rgz_items = 0;
1489876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1490876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /* We want to maintain the rgz dirty region data if there are no geometry changes */
1491876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (flags & HWC_GEOMETRY_CHANGED)
1492876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        rgz_release(&grgz);
1493876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen}
1494876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1495876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenstatic int blit_layers(omap4_hwc_device_t *hwc_dev, hwc_layer_list_t *list, int bufoff)
1496876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen{
1497876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /* Do not blit if this frame will be composed entirely by the GPU */
1498876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (!list || hwc_dev->force_sgx)
1499876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        goto err_out;
1500876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1501876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int rgz_in_op;
1502876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int rgz_out_op;
1503876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1504876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    switch (hwc_dev->blt_mode) {
1505876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        case BLTMODE_PAINT:
1506876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            rgz_in_op = RGZ_IN_HWCCHK;
1507876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            rgz_out_op = RGZ_OUT_BVCMD_PAINT;
1508876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            break;
1509876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        case BLTMODE_REGION:
1510876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        default:
1511876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            rgz_in_op = RGZ_IN_HWC;
1512876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            rgz_out_op = RGZ_OUT_BVCMD_REGION;
1513876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            break;
1514876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
1515876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1516876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    rgz_in_params_t in = {
1517876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        .op = rgz_in_op,
1518876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        .data = {
1519876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            .hwc = {
1520876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                .dstgeom = &gscrngeom,
1521876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                .layers = list->hwLayers,
1522876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                .layerno = list->numHwLayers
1523876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            }
1524876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        }
1525876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    };
1526876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1527876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /*
1528876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen     * This means if all the layers marked for the FRAMEBUFFER cannot be
1529876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen     * blitted, do not blit, for e.g. SKIP layers
1530876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen     */
1531876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (rgz_in(&in, &grgz) != RGZ_ALL)
1532876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        goto err_out;
1533876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1534876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    unsigned int i, count = 0;
1535876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    for (i = 0; i < list->numHwLayers; i++) {
1536876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        if (list->hwLayers[i].compositionType != HWC_OVERLAY) {
1537876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            count++;
1538876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        }
1539876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
1540876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1541876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    rgz_out_params_t out = {
1542876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        .op = rgz_out_op,
1543876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        .data = {
1544876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            .bvc = {
1545876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                .dstgeom = &gscrngeom,
1546876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                .noblend = 0,
1547876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            }
1548876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        }
1549876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    };
1550876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1551876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (rgz_out(&grgz, &out) != 0) {
1552876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        ALOGE("Failed generating blits");
1553876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        goto err_out;
1554876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
1555876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1556876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /* This is a special situation where the regionizer decided no blits are
1557876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen     * needed for this frame but there are blit buffers to synchronize with. Can
1558876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen     * happen only if the regionizer is enabled otherwise it's likely a bug
1559876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen     */
1560876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (rgz_out_op != RGZ_OUT_BVCMD_REGION && out.data.bvc.out_blits == 0 && out.data.bvc.out_nhndls > 0) {
1561876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        ALOGE("Regionizer invalid output blit_num %d, post2_blit_buffers %d", out.data.bvc.out_blits, out.data.bvc.out_nhndls);
1562876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        goto err_out;
1563876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
1564876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1565876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    hwc_dev->blit_flags |= HWC_BLT_FLAG_USE_FB;
1566876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    hwc_dev->blit_num = out.data.bvc.out_blits;
1567876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    hwc_dev->post2_blit_buffers = out.data.bvc.out_nhndls;
1568876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    for (i = 0; i < hwc_dev->post2_blit_buffers; i++) {
1569876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        //ALOGI("blit buffers[%d] = %p", bufoff, out.data.bvc.out_hndls[i]);
1570876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        hwc_dev->buffers[bufoff++] = out.data.bvc.out_hndls[i];
1571876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
1572876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1573876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    struct rgz_blt_entry *res_blit_ops = (struct rgz_blt_entry *) out.data.bvc.cmdp;
1574876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    memcpy(hwc_dev->comp_data.blit_data.rgz_blts, res_blit_ops, sizeof(*res_blit_ops) * out.data.bvc.cmdlen);
1575876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    ALOGI_IF(debugblt, "blt struct sz %d", sizeof(*res_blit_ops) * out.data.bvc.cmdlen);
1576876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    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);
1577876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1578876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /* all layers will be rendered without SGX help either via DSS or blitter */
1579876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    for (i = 0; i < list->numHwLayers; i++) {
1580876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        if (list->hwLayers[i].compositionType != HWC_OVERLAY) {
1581876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            list->hwLayers[i].compositionType = HWC_OVERLAY;
1582876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            //ALOGI("blitting layer %d", i);
1583a718b61ddb61e613cef6f0053624b20d151da6eaTony Lofthouse            list->hwLayers[i].hints &= ~HWC_HINT_TRIPLE_BUFFER;
1584876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        }
1585876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        list->hwLayers[i].hints &= ~HWC_HINT_CLEAR_FB;
1586876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
1587876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    return 1;
1588876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1589876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenerr_out:
1590876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    rgz_release(&grgz);
1591876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    return 0;
1592876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen}
1593876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1594876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenvoid debug_post2(omap4_hwc_device_t *hwc_dev, int nbufs)
1595876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen{
1596876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (!debugpost2)
1597876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        return;
1598876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
1599876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int i;
1600876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    for (i=0; i<nbufs; i++) {
1601876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        ALOGI("buf[%d] hndl %p", i, hwc_dev->buffers[i]);
1602876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
1603876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    for (i=0; i < dsscomp->num_ovls; i++) {
1604876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        ALOGI("ovl[%d] ba %d", i, dsscomp->ovls[i].ba);
1605876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
1606876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen}
1607876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
160883f80658d865b741a024515c523e373df50c091eSunita Nadampallistatic int free_tiler2d_buffers(omap4_hwc_device_t *hwc_dev)
160983f80658d865b741a024515c523e373df50c091eSunita Nadampalli{
161083f80658d865b741a024515c523e373df50c091eSunita Nadampalli    int i;
161183f80658d865b741a024515c523e373df50c091eSunita Nadampalli
161283f80658d865b741a024515c523e373df50c091eSunita Nadampalli    for (i = 0 ; i < NUM_EXT_DISPLAY_BACK_BUFFERS; i++) {
161383f80658d865b741a024515c523e373df50c091eSunita Nadampalli        ion_free(hwc_dev->ion_fd, hwc_dev->ion_handles[i]);
161483f80658d865b741a024515c523e373df50c091eSunita Nadampalli        hwc_dev->ion_handles[i] = NULL;
161583f80658d865b741a024515c523e373df50c091eSunita Nadampalli    }
161683f80658d865b741a024515c523e373df50c091eSunita Nadampalli    return 0;
161783f80658d865b741a024515c523e373df50c091eSunita Nadampalli}
161883f80658d865b741a024515c523e373df50c091eSunita Nadampalli
161983f80658d865b741a024515c523e373df50c091eSunita Nadampallistatic int allocate_tiler2d_buffers(omap4_hwc_device_t *hwc_dev)
162083f80658d865b741a024515c523e373df50c091eSunita Nadampalli{
162183f80658d865b741a024515c523e373df50c091eSunita Nadampalli    int ret, i;
162283f80658d865b741a024515c523e373df50c091eSunita Nadampalli    size_t stride;
162383f80658d865b741a024515c523e373df50c091eSunita Nadampalli
162483f80658d865b741a024515c523e373df50c091eSunita Nadampalli    if (hwc_dev->ion_fd < 0) {
162583f80658d865b741a024515c523e373df50c091eSunita Nadampalli        ALOGE("No ion fd, hence can't allocate tiler2d buffers");
162683f80658d865b741a024515c523e373df50c091eSunita Nadampalli        return -1;
162783f80658d865b741a024515c523e373df50c091eSunita Nadampalli    }
162883f80658d865b741a024515c523e373df50c091eSunita Nadampalli
162983f80658d865b741a024515c523e373df50c091eSunita Nadampalli    for (i = 0; i < NUM_EXT_DISPLAY_BACK_BUFFERS; i++) {
163083f80658d865b741a024515c523e373df50c091eSunita Nadampalli        if (hwc_dev->ion_handles[i])
163183f80658d865b741a024515c523e373df50c091eSunita Nadampalli            return 0;
163283f80658d865b741a024515c523e373df50c091eSunita Nadampalli    }
163383f80658d865b741a024515c523e373df50c091eSunita Nadampalli
163483f80658d865b741a024515c523e373df50c091eSunita Nadampalli    for (i = 0 ; i < NUM_EXT_DISPLAY_BACK_BUFFERS; i++) {
163583f80658d865b741a024515c523e373df50c091eSunita Nadampalli        ret = ion_alloc_tiler(hwc_dev->ion_fd, hwc_dev->fb_dev->base.width, hwc_dev->fb_dev->base.height,
163683f80658d865b741a024515c523e373df50c091eSunita Nadampalli                                            TILER_PIXEL_FMT_32BIT, 0, &hwc_dev->ion_handles[i], &stride);
163783f80658d865b741a024515c523e373df50c091eSunita Nadampalli        if (ret)
163883f80658d865b741a024515c523e373df50c091eSunita Nadampalli            goto handle_error;
163983f80658d865b741a024515c523e373df50c091eSunita Nadampalli
164083f80658d865b741a024515c523e373df50c091eSunita Nadampalli        ALOGI("ion handle[%d][%p]", i, hwc_dev->ion_handles[i]);
164183f80658d865b741a024515c523e373df50c091eSunita Nadampalli    }
164283f80658d865b741a024515c523e373df50c091eSunita Nadampalli    return 0;
164383f80658d865b741a024515c523e373df50c091eSunita Nadampalli
164483f80658d865b741a024515c523e373df50c091eSunita Nadampallihandle_error:
164583f80658d865b741a024515c523e373df50c091eSunita Nadampalli    free_tiler2d_buffers(hwc_dev);
164683f80658d865b741a024515c523e373df50c091eSunita Nadampalli    return -1;
164783f80658d865b741a024515c523e373df50c091eSunita Nadampalli}
164883f80658d865b741a024515c523e373df50c091eSunita Nadampalli
16496ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFaddenstatic int omap4_hwc_prepare(struct hwc_composer_device_1 *dev, size_t numDisplays,
16506ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        hwc_display_contents_1_t** displays)
1651c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
16526ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    if (!numDisplays || displays == NULL) {
16536ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        return 0;
16546ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    }
16556ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden
16566ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    hwc_display_contents_1_t* list = displays[0];  // ignore displays beyond the first
1657c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    omap4_hwc_device_t *hwc_dev = (omap4_hwc_device_t *)dev;
1658876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
16590b62b7ca3c6430caaebb2d3815642b2ff80f527aLajos Molnar    struct counts num = { .composited_layers = list ? list->numHwLayers : 0 };
16603837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    unsigned int i, ix;
1661c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
16622125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    pthread_mutex_lock(&hwc_dev->lock);
1663c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    memset(dsscomp, 0x0, sizeof(*dsscomp));
1664c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    dsscomp->sync_id = sync_id++;
1665c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
166644a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    gather_layer_statistics(hwc_dev, &num, list);
1667834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar
166844a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    decide_supported_cloning(hwc_dev, &num);
1669c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
16704b267e18b9e98b788d2b1bf2f1268ed25e5b726dLajos Molnar    /* Disable the forced SGX rendering if there is only one layer */
16714b267e18b9e98b788d2b1bf2f1268ed25e5b726dLajos Molnar    if (hwc_dev->force_sgx && num.composited_layers <= 1)
16724b267e18b9e98b788d2b1bf2f1268ed25e5b726dLajos Molnar        hwc_dev->force_sgx = 0;
16734b267e18b9e98b788d2b1bf2f1268ed25e5b726dLajos Molnar
167478fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    /* phase 3 logic */
16754b267e18b9e98b788d2b1bf2f1268ed25e5b726dLajos Molnar    if (can_dss_render_all(hwc_dev, &num)) {
1676c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        /* All layers can be handled by the DSS -- don't use SGX for composition */
1677c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        hwc_dev->use_sgx = 0;
167878fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        hwc_dev->swap_rb = num.BGR != 0;
1679c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    } else {
1680c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        /* Use SGX for composition plus first 3 layers that are DSS renderable */
1681c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        hwc_dev->use_sgx = 1;
16825706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar        hwc_dev->swap_rb = is_BGR_format(hwc_dev->fb_dev->base.format);
1683c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
1684c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1685c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* setup pipes */
1686c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int z = 0;
1687a51ba32b3ed323a74235d28a6cb0f31e51d8e514Lajos Molnar    int fb_z = -1;
1688c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int scaled_gfx = 0;
1689834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar    int ix_docking = -1;
1690fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht    int big_layers = 0;
1691c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1692876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int blit_all = 0;
16932acde915b4868129963ef7dc7afe1f3b35ffeeacJon Pry    blit_reset(hwc_dev, list ? list->flags : 0);
1694876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1695876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /* If the SGX is used or we are going to blit something we need a framebuffer
1696876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen     * and a DSS pipe
1697876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen     */
1698876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int needs_fb = hwc_dev->use_sgx;
1699876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1700876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (hwc_dev->blt_policy == BLTPOLICY_ALL) {
1701876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        /* Check if we can blit everything */
1702876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        blit_all = blit_layers(hwc_dev, list, 0);
1703876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        if (blit_all) {
1704876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            needs_fb = 1;
1705876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            hwc_dev->use_sgx = 0;
1706876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        }
1707876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
1708876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1709876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /* If a framebuffer is needed, begin using VID1 for DSS overlay layers,
1710876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen     * we need GFX for FB
1711876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen     */
1712876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    dsscomp->num_ovls = needs_fb ? 1 /*VID1*/ : 0 /*GFX*/;
1713876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1714c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* set up if DSS layers */
171538934f125a600817dded7aeba9639c6a75afe358Lajos Molnar    unsigned int mem_used = 0;
1716876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    for (i = 0; list && i < list->numHwLayers && !blit_all; i++) {
17176ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        hwc_layer_1_t *layer = &list->hwLayers[i];
1718c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        IMG_native_handle_t *handle = (IMG_native_handle_t *)layer->handle;
1719c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
17204b267e18b9e98b788d2b1bf2f1268ed25e5b726dLajos Molnar        if (dsscomp->num_ovls < num.max_hw_overlays &&
1721a1be32cc5c3978f9bac62c3b40a257bba2315861Lajos Molnar            can_dss_render_layer(hwc_dev, layer) &&
17224b267e18b9e98b788d2b1bf2f1268ed25e5b726dLajos Molnar            (!hwc_dev->force_sgx ||
17234b267e18b9e98b788d2b1bf2f1268ed25e5b726dLajos Molnar             /* render protected and dockable layers via DSS */
17245706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar             is_protected(layer) ||
1725f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar             is_upscaled_NV12(hwc_dev, layer) ||
17264b267e18b9e98b788d2b1bf2f1268ed25e5b726dLajos Molnar             (hwc_dev->ext.current.docking && hwc_dev->ext.current.enabled && dockable(layer))) &&
1727876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            mem_used + mem1d(handle) < limits.tiler1d_slot_size &&
1728c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            /* can't have a transparent overlay in the middle of the framebuffer stack */
1729fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht            !(is_BLENDED(layer) && fb_z >= 0) &&
1730fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht            /* current hardware is unable to keep up with more than 1 'large' RGB32 layer */
1731fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht            !(is_large_rgb32_layer(layer) && big_layers > 0)) {
17325706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar
1733c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            /* render via DSS overlay */
173438934f125a600817dded7aeba9639c6a75afe358Lajos Molnar            mem_used += mem1d(handle);
1735c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            layer->compositionType = HWC_OVERLAY;
1736a718b61ddb61e613cef6f0053624b20d151da6eaTony Lofthouse            /*
1737a718b61ddb61e613cef6f0053624b20d151da6eaTony Lofthouse             * This hint will not be used in vanilla ICS, but maybe in
1738a718b61ddb61e613cef6f0053624b20d151da6eaTony Lofthouse             * JellyBean, it is useful to distinguish between blts and true
1739a718b61ddb61e613cef6f0053624b20d151da6eaTony Lofthouse             * overlays
1740a718b61ddb61e613cef6f0053624b20d151da6eaTony Lofthouse             */
1741a718b61ddb61e613cef6f0053624b20d151da6eaTony Lofthouse            layer->hints |= HWC_HINT_TRIPLE_BUFFER;
1742b81fb68ce4c70170ebc9e41f9fe4540cc05685d5Lajos Molnar
1743b81fb68ce4c70170ebc9e41f9fe4540cc05685d5Lajos Molnar            /* clear FB above all opaque layers if rendering via SGX */
1744b81fb68ce4c70170ebc9e41f9fe4540cc05685d5Lajos Molnar            if (hwc_dev->use_sgx && !is_BLENDED(layer))
1745b81fb68ce4c70170ebc9e41f9fe4540cc05685d5Lajos Molnar                layer->hints |= HWC_HINT_CLEAR_FB;
1746b81fb68ce4c70170ebc9e41f9fe4540cc05685d5Lajos Molnar
1747e055e4bf77e6c429d7a159e8e853974d9dfc57adLajos Molnar            hwc_dev->buffers[dsscomp->num_ovls] = layer->handle;
1748c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1749c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            omap4_hwc_setup_layer(hwc_dev,
1750c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                  &dsscomp->ovls[dsscomp->num_ovls],
1751f1e357b2cf26fa5a50789061e068e4f0bd4ea772Mathias Agopian                                  layer,
1752c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                  z,
1753c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                  handle->iFormat,
1754c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                  handle->iWidth,
1755c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                  handle->iHeight);
1756d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
1757876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            dsscomp->ovls[dsscomp->num_ovls].cfg.ix = dsscomp->num_ovls + hwc_dev->primary_transform;
175812c821dbf70ee94bd9702e7cb60c66c8156c83d6Erik Gilling            dsscomp->ovls[dsscomp->num_ovls].addressing = OMAP_DSS_BUFADDR_LAYER_IX;
175912c821dbf70ee94bd9702e7cb60c66c8156c83d6Erik Gilling            dsscomp->ovls[dsscomp->num_ovls].ba = dsscomp->num_ovls;
1760c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1761c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            /* ensure GFX layer is never scaled */
1762876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            if ((dsscomp->num_ovls == 0) && (!hwc_dev->primary_transform)) {
1763876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                scaled_gfx = scaled(layer) || is_NV12(handle);
1764876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            } else if (scaled_gfx && !scaled(layer) && !is_NV12(handle)) {
1765c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                /* swap GFX layer with this one */
1766c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                dsscomp->ovls[dsscomp->num_ovls].cfg.ix = 0;
1767876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                dsscomp->ovls[0].cfg.ix = dsscomp->num_ovls;
1768c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                scaled_gfx = 0;
1769c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1770c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1771834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar            /* remember largest dockable layer */
1772834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar            if (dockable(layer) &&
1773834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar                (ix_docking < 0 ||
177400d8485d3d319633f410e52cab1562fe165af1e3Lajos Molnar                 display_area(&dsscomp->ovls[dsscomp->num_ovls]) > display_area(&dsscomp->ovls[ix_docking])))
1775834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar                ix_docking = dsscomp->num_ovls;
177678fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
1777c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            dsscomp->num_ovls++;
1778c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            z++;
1779fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht
1780fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht            /* record whether or not this was a 'big' RGB32 layer */
1781fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht            if (is_large_rgb32_layer(layer)) {
1782fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht                big_layers++;
1783fc13c9292160e3eb83fe5b4f191d1ccee2fd67d9Travis Geiselbrecht            }
1784c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        } else if (hwc_dev->use_sgx) {
1785c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if (fb_z < 0) {
1786c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                /* NOTE: we are not handling transparent cutout for now */
1787a51ba32b3ed323a74235d28a6cb0f31e51d8e514Lajos Molnar                fb_z = z;
1788c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                z++;
1789c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            } else {
1790c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                /* move fb z-order up (by lowering dss layers) */
1791a51ba32b3ed323a74235d28a6cb0f31e51d8e514Lajos Molnar                while (fb_z < z - 1)
1792a51ba32b3ed323a74235d28a6cb0f31e51d8e514Lajos Molnar                    dsscomp->ovls[1 + fb_z++].cfg.zorder--;
1793c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1794c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1795c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
1796c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1797c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* if scaling GFX (e.g. only 1 scaled surface) use a VID pipe */
1798c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (scaled_gfx)
1799876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        dsscomp->ovls[0].cfg.ix = dsscomp->num_ovls;
1800876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1801876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (hwc_dev->blt_policy == BLTPOLICY_DEFAULT) {
1802876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        if (hwc_dev->use_sgx) {
1803876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            if (blit_layers(hwc_dev, list, dsscomp->num_ovls == 1 ? 0 : dsscomp->num_ovls)) {
1804876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                hwc_dev->use_sgx = 0;
1805876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            }
1806876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        }
1807876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
1808c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1809876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /* If the SGX is not used and there is blit data we need a framebuffer and
1810876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen     * a DSS pipe well configured for it
1811876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen     */
1812876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (needs_fb) {
1813b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar        /* assign a z-layer for fb */
1814449e98a958753e6ae8f16b923596af0c6783ff8aLajos Molnar        if (fb_z < 0) {
1815876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            if (!hwc_dev->blt_policy != BLTPOLICY_DISABLED && num.composited_layers)
18166d94b7785c5318b6de36193a7f5488a88b6178ebDandawate Saket                ALOGE("**** should have assigned z-layer for fb");
1817449e98a958753e6ae8f16b923596af0c6783ff8aLajos Molnar            fb_z = z++;
1818449e98a958753e6ae8f16b923596af0c6783ff8aLajos Molnar        }
1819876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        /*
1820876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen         * This is needed because if we blit all we would lose the handle of
1821876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen         * the first layer
1822876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen         */
1823876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        if (hwc_dev->blit_num == 0) {
1824876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            hwc_dev->buffers[0] = NULL;
1825876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        }
1826c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        omap4_hwc_setup_layer_base(&dsscomp->ovls[0].cfg, fb_z,
1827c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                   hwc_dev->fb_dev->base.format,
18285706ba0da054830e63b82b195ec32a97c390bd2cLajos Molnar                                   1,   /* FB is always premultiplied */
1829c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                   hwc_dev->fb_dev->base.width,
1830c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                   hwc_dev->fb_dev->base.height);
1831c35f8ef2dc4bf1606f2b27f750057b12e51cb99aErik Gilling        dsscomp->ovls[0].cfg.pre_mult_alpha = 1;
183212c821dbf70ee94bd9702e7cb60c66c8156c83d6Erik Gilling        dsscomp->ovls[0].addressing = OMAP_DSS_BUFADDR_LAYER_IX;
183312c821dbf70ee94bd9702e7cb60c66c8156c83d6Erik Gilling        dsscomp->ovls[0].ba = 0;
1834876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        dsscomp->ovls[0].cfg.ix = hwc_dev->primary_transform;
1835c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
1836c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
183778fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    /* mirror layers */
183878fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    hwc_dev->post2_layers = dsscomp->num_ovls;
18393837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar
18403837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    omap4_hwc_ext_t *ext = &hwc_dev->ext;
18413837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    if (ext->current.enabled && hwc_dev->ext_ovls) {
18423837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        if (ext->current.docking && ix_docking >= 0) {
1843876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            if (clone_external_layer(hwc_dev, ix_docking) == 0)
184412c821dbf70ee94bd9702e7cb60c66c8156c83d6Erik Gilling                dsscomp->ovls[dsscomp->num_ovls - 1].cfg.zorder = z++;
18450aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        } else if (ext->current.docking && ix_docking < 0 && ext->force_dock) {
18460aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar            ix_docking = dsscomp->num_ovls;
18470aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar            struct dss2_ovl_info *oi = &dsscomp->ovls[ix_docking];
18480aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar            omap4_hwc_setup_layer_base(&oi->cfg, 0, HAL_PIXEL_FORMAT_BGRA_8888, 1,
18490aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar                                       dock_image.width, dock_image.height);
18500aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar            oi->cfg.stride = dock_image.rowbytes;
18510aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar            if (clone_external_layer(hwc_dev, ix_docking) == 0) {
18520aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar                oi->addressing = OMAP_DSS_BUFADDR_FB;
18530aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar                oi->ba = 0;
18540aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar                z++;
18550aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar            }
18563837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar        } else if (!ext->current.docking) {
18573837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar            int res = 0;
1858834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar
1859834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar            /* reset mode if we are coming from docking */
18603837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar            if (ext->last.docking)
18613837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar                res = setup_mirroring(hwc_dev);
1862c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
18633837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar            /* mirror all layers */
18643837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar            for (ix = 0; res == 0 && ix < hwc_dev->post2_layers; ix++) {
18653837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar                if (clone_layer(hwc_dev, ix))
18663837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar                    break;
18673837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar                z++;
18682952fd7510718abf21a5fc6d683fd1b4a6025171Lajos Molnar            }
186978fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        }
187078fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    }
1871876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
1872876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /* Apply transform for primary display */
1873876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (hwc_dev->primary_transform)
1874876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        for (i = 0; i < dsscomp->num_ovls; i++) {
1875876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            if(dsscomp->ovls[i].cfg.mgr_ix == 0)
1876876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                omap4_hwc_adjust_primary_display_layer(hwc_dev, &dsscomp->ovls[i]);
1877876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        }
1878876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
18793837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    ext->last = ext->current;
188078fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
188178fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    if (z != dsscomp->num_ovls || dsscomp->num_ovls > MAX_HW_OVERLAYS)
188246de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("**** used %d z-layers for %d overlays\n", z, dsscomp->num_ovls);
188378fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
188444a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    /* verify all z-orders and overlay indices are distinct */
188544a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    for (i = z = ix = 0; i < dsscomp->num_ovls; i++) {
188644a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar        struct dss2_ovl_cfg *c = &dsscomp->ovls[i].cfg;
188744a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar
188844a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar        if (z & (1 << c->zorder))
188946de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block            ALOGE("**** used z-order #%d multiple times", c->zorder);
189044a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar        if (ix & (1 << c->ix))
189146de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block            ALOGE("**** used ovl index #%d multiple times", c->ix);
189244a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar        z |= 1 << c->zorder;
189344a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar        ix |= 1 << c->ix;
189444a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    }
189578fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    dsscomp->mode = DSSCOMP_SETUP_DISPLAY;
189678fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    dsscomp->mgrs[0].ix = 0;
189778fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    dsscomp->mgrs[0].alpha_blending = 1;
189878fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    dsscomp->mgrs[0].swap_rb = hwc_dev->swap_rb;
189978fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    dsscomp->num_mgrs = 1;
190078fce72baff38f488a0117591d4241d278a48b1bLajos Molnar
19013837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar    if (ext->current.enabled || hwc_dev->last_ext_ovls) {
190278fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        dsscomp->mgrs[1] = dsscomp->mgrs[0];
190378fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        dsscomp->mgrs[1].ix = 1;
190478fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        dsscomp->num_mgrs++;
190578fce72baff38f488a0117591d4241d278a48b1bLajos Molnar        hwc_dev->ext_ovls = dsscomp->num_ovls - hwc_dev->post2_layers;
190678fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    }
190744a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar
190844a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    if (debug) {
19093a7df2c042eb8c7289e24e77dd316f73bd0c456fSteve Block        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",
191044a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar             dsscomp->sync_id,
191144a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar             hwc_dev->use_sgx ? "SGX+OVL" : "all-OVL",
191244a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar             num.composited_layers,
191344a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar             num.possible_overlay_layers, num.scaled_layers,
191444a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar             num.RGB, num.BGR, num.NV12,
191544a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar             ext->on_tv ? "tv+" : "",
191644a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar             ext->current.enabled ? ext->current.docking ? "dock+" : "mirror+" : "OFF+",
191744a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar             ext->current.rotation * 90,
191844a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar             ext->current.hflip ? "+hflip" : "",
191944a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar             hwc_dev->ext_ovls, num.max_hw_overlays, hwc_dev->last_ext_ovls, hwc_dev->last_int_ovls);
192044a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar    }
192144a0023db5b752a81fb0bd3badd4cca1a1c4edc0Lajos Molnar
19222125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    pthread_mutex_unlock(&hwc_dev->lock);
1923c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return 0;
1924c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
1925c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
192624a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnarstatic void omap4_hwc_reset_screen(omap4_hwc_device_t *hwc_dev)
192724a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar{
192824a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar    static int first_set = 1;
192924a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar    int ret;
193024a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar
193124a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar    if (first_set) {
193224a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar        first_set = 0;
193324a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar        struct dsscomp_setup_dispc_data d = {
1934b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar            .num_mgrs = 1,
193524a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar        };
193624a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar        /* remove bootloader image from the screen as blank/unblank does not change the composition */
1937cb64c2be21ad9550b1f5d2b4a8361e997b9be757Lajos Molnar        ret = ioctl(hwc_dev->dsscomp_fd, DSSCIOC_SETUP_DISPC, &d);
193824a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar        if (ret)
1939a4e4aeab47b17a82524ac56f2d69daa3e47c1ce7Steve Block            ALOGW("failed to remove bootloader image");
194024a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar
194124a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar        /* blank and unblank fd to make sure display is properly programmed on boot.
194224a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar         * This is needed because the bootloader can not be trusted.
194324a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar         */
194424a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar        ret = ioctl(hwc_dev->fb_fd, FBIOBLANK, FB_BLANK_POWERDOWN);
194524a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar        if (ret)
1946a4e4aeab47b17a82524ac56f2d69daa3e47c1ce7Steve Block            ALOGW("failed to blank display");
194724a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar
194824a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar        ret = ioctl(hwc_dev->fb_fd, FBIOBLANK, FB_BLANK_UNBLANK);
194924a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar        if (ret)
1950a4e4aeab47b17a82524ac56f2d69daa3e47c1ce7Steve Block            ALOGW("failed to blank display");
195124a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar    }
195224a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar}
195324a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar
19546ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFaddenstatic int omap4_hwc_set(struct hwc_composer_device_1 *dev,
19556ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        size_t numDisplays, hwc_display_contents_1_t** displays)
1956c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
19576ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    if (!numDisplays || displays == NULL) {
19586ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        ALOGD("set: empty display list");
19596ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        return 0;
19606ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    }
19616ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    hwc_display_t dpy = NULL;
19626ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    hwc_surface_t sur = NULL;
19636ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    hwc_display_contents_1_t* list = displays[0];  // ignore displays beyond the first
19646ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    if (list != NULL) {
19656ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        dpy = list->dpy;
19666ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        sur = list->sur;
19676ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    }
1968c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    omap4_hwc_device_t *hwc_dev = (omap4_hwc_device_t *)dev;
1969876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
1970ee0725d529a6adced20dd0182f9fd470607ce495Lajos Molnar    int err = 0;
1971bd0fe036293115c9f9b182ed2a911bdd557ea491Lajos Molnar    int invalidate;
1972c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
19732125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    pthread_mutex_lock(&hwc_dev->lock);
197424a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar
197527843d0acbb3d17ec4458900b6cd5e281a464611Travis Geiselbrecht    /* disable resetting the screen on the first boot for devices
197627843d0acbb3d17ec4458900b6cd5e281a464611Travis Geiselbrecht     * with hdmi as primary input.
197727843d0acbb3d17ec4458900b6cd5e281a464611Travis Geiselbrecht     */
197827843d0acbb3d17ec4458900b6cd5e281a464611Travis Geiselbrecht    if (!hwc_dev->on_tv)
197927843d0acbb3d17ec4458900b6cd5e281a464611Travis Geiselbrecht        omap4_hwc_reset_screen(hwc_dev);
198024a18d2f746877f2abd63e95b4be9c54dac4472eLajos Molnar
198158aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse    invalidate = hwc_dev->ext_ovls_wanted && (hwc_dev->ext_ovls < hwc_dev->ext_ovls_wanted) &&
198258aa32dc9d23875627e39f16831ffb471baf7890Tony Lofthouse                                              (hwc_dev->stats.protected || !hwc_dev->ext_ovls);
1983bd0fe036293115c9f9b182ed2a911bdd557ea491Lajos Molnar
1984734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    if (debug)
1985734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        dump_set_info(hwc_dev, list);
1986c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1987ee0725d529a6adced20dd0182f9fd470607ce495Lajos Molnar    if (dpy && sur) {
1988e37a11c21bcac1fbcac995cd34884eac37492b95Mathias Agopian        // list can be NULL which means hwc is temporarily disabled.
1989e37a11c21bcac1fbcac995cd34884eac37492b95Mathias Agopian        // however, if dpy and sur are null it means we're turning the
1990e37a11c21bcac1fbcac995cd34884eac37492b95Mathias Agopian        // screen off. no shall not call eglSwapBuffers() in that case.
1991ee0725d529a6adced20dd0182f9fd470607ce495Lajos Molnar
1992ee0725d529a6adced20dd0182f9fd470607ce495Lajos Molnar        if (hwc_dev->use_sgx) {
1993ee0725d529a6adced20dd0182f9fd470607ce495Lajos Molnar            if (!eglSwapBuffers((EGLDisplay)dpy, (EGLSurface)sur)) {
199446de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block                ALOGE("eglSwapBuffers error");
1995ee0725d529a6adced20dd0182f9fd470607ce495Lajos Molnar                err = HWC_EGL_ERROR;
1996ee0725d529a6adced20dd0182f9fd470607ce495Lajos Molnar                goto err_out;
1997ee0725d529a6adced20dd0182f9fd470607ce495Lajos Molnar            }
1998c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1999c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2000ee0725d529a6adced20dd0182f9fd470607ce495Lajos Molnar        //dump_dsscomp(dsscomp);
2001c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
200202150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian        // signal the event thread that a post has happened
2003751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        wakeup_hdmi_thread(hwc_dev);
200402150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian        if (hwc_dev->force_sgx > 0)
200502150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian            hwc_dev->force_sgx--;
200602150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian
2007876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        hwc_dev->comp_data.blit_data.rgz_flags = hwc_dev->blit_flags;
2008876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        hwc_dev->comp_data.blit_data.rgz_items = hwc_dev->blit_num;
2009876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        int omaplfb_comp_data_sz = sizeof(hwc_dev->comp_data) +
2010876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            (hwc_dev->comp_data.blit_data.rgz_items * sizeof(struct rgz_blt_entry));
2011876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
2012876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
2013876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        unsigned int nbufs = hwc_dev->post2_layers;
2014876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        if (hwc_dev->post2_blit_buffers) {
2015876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            /*
2016876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen             * We don't want to pass a NULL entry in the Post2, but we need to
2017876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen             * fix up buffer handle array and overlay indexes to account for
2018876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen             * this
2019876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen             */
2020876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            nbufs += hwc_dev->post2_blit_buffers - 1;
2021876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
2022876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            if (hwc_dev->post2_layers > 1) {
2023876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                unsigned int i, j;
2024876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                for (i = 0; i < nbufs; i++) {
2025876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                    hwc_dev->buffers[i] = hwc_dev->buffers[i+1];
2026876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                }
2027876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                for (i = 1, j= 1; j < hwc_dev->post2_layers; i++, j++) {
2028876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                    dsscomp->ovls[j].ba = i;
2029876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                }
2030876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            }
2031876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        }
2032876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        ALOGI_IF(debugblt && hwc_dev->blt_policy != BLTPOLICY_DISABLED,
2033876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            "Post2, blits %d, ovl_buffers %d, blit_buffers %d sgx %d",
2034876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            hwc_dev->blit_num, hwc_dev->post2_layers, hwc_dev->post2_blit_buffers,
2035876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            hwc_dev->use_sgx);
2036876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
2037876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        debug_post2(hwc_dev, nbufs);
2038ee0725d529a6adced20dd0182f9fd470607ce495Lajos Molnar        err = hwc_dev->fb_dev->Post2((framebuffer_device_t *)hwc_dev->fb_dev,
2039c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                 hwc_dev->buffers,
2040876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                                 nbufs,
2041876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                                 dsscomp, omaplfb_comp_data_sz);
20421fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse        showfps();
20431fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse
2044876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen#if 0
2045876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        if (!hwc_dev->use_sgx) {
2046876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            __u32 crt = 0;
2047876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            int err2 = ioctl(hwc_dev->fb_fd, FBIO_WAITFORVSYNC, &crt);
2048876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            if (err2) {
2049876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                ALOGE("failed to wait for vsync (%d)", errno);
2050876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                err = err ? : -errno;
2051876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            }
2052876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        }
2053876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen#endif
20547f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar    }
205578fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    hwc_dev->last_ext_ovls = hwc_dev->ext_ovls;
205678fce72baff38f488a0117591d4241d278a48b1bLajos Molnar    hwc_dev->last_int_ovls = hwc_dev->post2_layers;
2057c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (err)
205846de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("Post2 error");
2059c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
20606ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    check_sync_fds(numDisplays, displays);
20616ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden
2062c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malcheverr_out:
20632125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    pthread_mutex_unlock(&hwc_dev->lock);
2064bd0fe036293115c9f9b182ed2a911bdd557ea491Lajos Molnar
2065876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (invalidate && hwc_dev->procs && hwc_dev->procs->invalidate)
2066bd0fe036293115c9f9b182ed2a911bdd557ea491Lajos Molnar        hwc_dev->procs->invalidate(hwc_dev->procs);
2067bd0fe036293115c9f9b182ed2a911bdd557ea491Lajos Molnar
2068c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return err;
2069c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2070c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
20716ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFaddenstatic void omap4_hwc_dump(struct hwc_composer_device_1 *dev, char *buff, int buff_len)
2072c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2073c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    omap4_hwc_device_t *hwc_dev = (omap4_hwc_device_t *)dev;
2074876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    struct dsscomp_setup_dispc_data *dsscomp = &hwc_dev->comp_data.dsscomp_data;
2075734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    struct dump_buf log = {
2076734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        .buf = buff,
2077734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        .buf_len = buff_len,
2078734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    };
2079c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int i;
2080c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2081734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    dump_printf(&log, "omap4_hwc %d:\n", dsscomp->num_ovls);
2082734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar    dump_printf(&log, "  idle timeout: %dms\n", hwc_dev->idle);
2083c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2084c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    for (i = 0; i < dsscomp->num_ovls; i++) {
2085c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        struct dss2_ovl_cfg *cfg = &dsscomp->ovls[i].cfg;
2086c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2087734de7a7631e5f115ee89c5b2f35c7c484098779Lajos Molnar        dump_printf(&log, "  layer %d:\n", i);
20883d32f43362ff52bdac5a83855c63e8a8274da55aTony Lofthouse        dump_printf(&log, "     enabled:%s buff:%p %dx%d stride:%d\n",
20893d32f43362ff52bdac5a83855c63e8a8274da55aTony Lofthouse                          cfg->enabled ? "true" : "false", hwc_dev->buffers[i],
20903d32f43362ff52bdac5a83855c63e8a8274da55aTony Lofthouse                          cfg->width, cfg->height, cfg->stride);
20913d32f43362ff52bdac5a83855c63e8a8274da55aTony Lofthouse        dump_printf(&log, "     src:(%d,%d) %dx%d dst:(%d,%d) %dx%d ix:%d zorder:%d\n",
20923d32f43362ff52bdac5a83855c63e8a8274da55aTony Lofthouse                          cfg->crop.x, cfg->crop.y, cfg->crop.w, cfg->crop.h,
20933d32f43362ff52bdac5a83855c63e8a8274da55aTony Lofthouse                          cfg->win.x, cfg->win.y, cfg->win.w, cfg->win.h,
20943d32f43362ff52bdac5a83855c63e8a8274da55aTony Lofthouse                          cfg->ix, cfg->zorder);
2095c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2096876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
2097876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (hwc_dev->blt_policy != BLTPOLICY_DISABLED) {
2098876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        dump_printf(&log, "  bltpolicy: %s, bltmode: %s\n",
2099876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            hwc_dev->blt_policy == BLTPOLICY_DEFAULT ? "default" :
2100876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                hwc_dev->blt_policy == BLTPOLICY_ALL ? "all" : "unknown",
2101876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                    hwc_dev->blt_mode == BLTMODE_PAINT ? "paint" : "regionize");
2102876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
2103876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    dump_printf(&log, "\n");
2104c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2105c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
21060aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnarstatic void free_png_image(omap4_hwc_device_t *hwc_dev, struct omap4_hwc_img *img)
21070aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar{
21080aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    memset(img, 0, sizeof(*img));
21090aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar}
21100aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
21110aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnarstatic int load_png_image(omap4_hwc_device_t *hwc_dev, char *path, struct omap4_hwc_img *img)
21120aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar{
21130aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    void *ptr = NULL;
21140aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    png_bytepp row_pointers = NULL;
21150aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
21160aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    FILE *fd = fopen(path, "rb");
21170aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    if (!fd) {
211846de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("failed to open PNG file %s: (%d)", path, errno);
21190aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        return -EINVAL;
21200aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    }
21210aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
21220aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    const int SIZE_PNG_HEADER = 8;
21230aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    __u8 header[SIZE_PNG_HEADER];
21240aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    fread(header, 1, SIZE_PNG_HEADER, fd);
21250aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    if (png_sig_cmp(header, 0, SIZE_PNG_HEADER)) {
212646de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("%s is not a PNG file", path);
21270aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        goto fail;
21280aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    }
21290aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
21300aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
21310aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    if (!png_ptr)
21320aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar         goto fail_alloc;
21330aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    png_infop info_ptr = png_create_info_struct(png_ptr);
21340aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    if (!info_ptr)
21350aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar         goto fail_alloc;
21360aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
21370aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    if (setjmp(png_jmpbuf(png_ptr)))
21380aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        goto fail_alloc;
21390aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
21400aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    png_init_io(png_ptr, fd);
21410aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    png_set_sig_bytes(png_ptr, SIZE_PNG_HEADER);
21420aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    png_set_user_limits(png_ptr, limits.max_width, limits.max_height);
21430aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    png_read_info(png_ptr, info_ptr);
21440aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
21450aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    __u8 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
21460aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    __u32 width = png_get_image_width(png_ptr, info_ptr);
21470aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    __u32 height = png_get_image_height(png_ptr, info_ptr);
21480aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    __u8 color_type = png_get_color_type(png_ptr, info_ptr);
21490aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
21500aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    switch (color_type) {
21510aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    case PNG_COLOR_TYPE_PALETTE:
21520aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        png_set_palette_to_rgb(png_ptr);
21530aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        png_set_filler(png_ptr, 128, PNG_FILLER_AFTER);
21540aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        break;
21550aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    case PNG_COLOR_TYPE_GRAY:
21560aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        if (bit_depth < 8) {
21570aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar            png_set_expand_gray_1_2_4_to_8(png_ptr);
21580aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar            if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
21590aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar                png_set_tRNS_to_alpha(png_ptr);
21600aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        } else {
21610aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar            png_set_filler(png_ptr, 128, PNG_FILLER_AFTER);
21620aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        }
21630aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        /* fall through */
21640aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    case PNG_COLOR_TYPE_GRAY_ALPHA:
21650aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        png_set_gray_to_rgb(png_ptr);
21660aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        break;
21670aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    case PNG_COLOR_TYPE_RGB:
21680aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        png_set_filler(png_ptr, 128, PNG_FILLER_AFTER);
21690aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        /* fall through */
21700aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    case PNG_COLOR_TYPE_RGB_ALPHA:
21710aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        png_set_bgr(png_ptr);
21720aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        break;
21730aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    default:
217446de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("unsupported PNG color: %x", color_type);
21750aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        goto fail_alloc;
21760aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    }
21770aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
21780aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    if (bit_depth == 16)
21790aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        png_set_strip_16(png_ptr);
21800aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
21810aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    const int bpp = 4;
21820aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    img->size = ALIGN(width * height * bpp, 4096);
21830aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    if (img->size > hwc_dev->img_mem_size) {
218446de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("image does not fit into framebuffer area (%d > %d)", img->size, hwc_dev->img_mem_size);
21850aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        goto fail_alloc;
21860aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    }
21870aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    img->ptr = hwc_dev->img_mem_ptr;
21880aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
21890aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    row_pointers = calloc(height, sizeof(*row_pointers));
21900aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    if (!row_pointers) {
219146de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("failed to allocate row pointers");
21920aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        goto fail_alloc;
21930aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    }
21940aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    __u32 i;
21950aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    for (i = 0; i < height; i++)
21960aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        row_pointers[i] = img->ptr + i * width * bpp;
21970aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    png_set_rows(png_ptr, info_ptr, row_pointers);
21980aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    png_read_update_info(png_ptr, info_ptr);
21990aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    img->rowbytes = png_get_rowbytes(png_ptr, info_ptr);
22000aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
22010aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    png_read_image(png_ptr, row_pointers);
22020aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    png_read_end(png_ptr, NULL);
22030aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    free(row_pointers);
22040aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
22050aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    fclose(fd);
22060aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    img->width = width;
22070aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    img->height = height;
22080aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    return 0;
22090aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
22100aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnarfail_alloc:
22110aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    free_png_image(hwc_dev, img);
22120aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    free(row_pointers);
22130aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    if (!png_ptr || !info_ptr)
221446de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("failed to allocate PNG structures");
22150aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
22160aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnarfail:
22170aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    fclose(fd);
22180aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    return -EINVAL;
22190aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar}
22200aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
2221c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2222c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatic int omap4_hwc_device_close(hw_device_t* device)
2223c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2224c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    omap4_hwc_device_t *hwc_dev = (omap4_hwc_device_t *) device;;
2225c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
22262125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    if (hwc_dev) {
22272125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        if (hwc_dev->dsscomp_fd >= 0)
22282125fa148686edfa389121f946377aedaa3d9483Lajos Molnar            close(hwc_dev->dsscomp_fd);
22292125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        if (hwc_dev->hdmi_fb_fd >= 0)
22302125fa148686edfa389121f946377aedaa3d9483Lajos Molnar            close(hwc_dev->hdmi_fb_fd);
22317f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar        if (hwc_dev->fb_fd >= 0)
22327f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar            close(hwc_dev->fb_fd);
223383f80658d865b741a024515c523e373df50c091eSunita Nadampalli        if (hwc_dev->ion_fd >= 0)
223483f80658d865b741a024515c523e373df50c091eSunita Nadampalli            ion_close(hwc_dev->ion_fd);
223583f80658d865b741a024515c523e373df50c091eSunita Nadampalli
22362125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        /* pthread will get killed when parent process exits */
22372125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        pthread_mutex_destroy(&hwc_dev->lock);
2238751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        pthread_mutex_destroy(&hwc_dev->vsync_lock);
2239c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        free(hwc_dev);
22402125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    }
2241c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2242c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return 0;
2243c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2244c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2245c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatic int omap4_hwc_open_fb_hal(IMG_framebuffer_device_public_t **fb_dev)
2246c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2247e055e4bf77e6c429d7a159e8e853974d9dfc57adLajos Molnar    const struct hw_module_t *psModule;
2248c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    IMG_gralloc_module_public_t *psGrallocModule;
2249c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int err;
2250c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2251e055e4bf77e6c429d7a159e8e853974d9dfc57adLajos Molnar    err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &psModule);
2252e055e4bf77e6c429d7a159e8e853974d9dfc57adLajos Molnar    psGrallocModule = (IMG_gralloc_module_public_t *) psModule;
2253e055e4bf77e6c429d7a159e8e853974d9dfc57adLajos Molnar
2254c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(err)
2255c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto err_out;
2256c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2257b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar    if (strcmp(psGrallocModule->base.common.author, "Imagination Technologies")) {
2258c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        err = -EINVAL;
2259c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto err_out;
2260c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2261c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2262c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    *fb_dev = psGrallocModule->psFrameBufferDevice;
2263c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2264c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return 0;
2265c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2266c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malcheverr_out:
226746de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block    ALOGE("Composer HAL failed to load compatible Graphics HAL");
2268c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return err;
2269c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
22705db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
2271876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chenstatic void set_primary_display_transform_matrix(omap4_hwc_device_t *hwc_dev)
2272876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen{
2273876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /* create primary display translation matrix */
22745db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    hwc_dev->fb_dis.ix = 0;/*Default display*/
2275c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2276876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int ret = ioctl(hwc_dev->dsscomp_fd, DSSCIOC_QUERY_DISPLAY, &hwc_dev->fb_dis);
2277876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (ret)
2278876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        ALOGE("failed to get display info (%d): %m", errno);
2279876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
2280876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int lcd_w = hwc_dev->fb_dis.timings.x_res;
2281876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int lcd_h = hwc_dev->fb_dis.timings.y_res;
2282876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int orig_w = hwc_dev->fb_dev->base.width;
2283876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int orig_h = hwc_dev->fb_dev->base.height;
2284876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    hwc_rect_t region = {.left = 0, .top = 0, .right = orig_w, .bottom = orig_h};
2285876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    hwc_dev->primary_region = region;
2286876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    hwc_dev->primary_rotation = ((lcd_w > lcd_h) ^ (orig_w > orig_h)) ? 1 : 0;
2287876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    hwc_dev->primary_transform = ((lcd_w != orig_w)||(lcd_h != orig_h)) ? 1 : 0;
2288876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
2289876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    ALOGI("transforming FB (%dx%d) => (%dx%d) rot%d", orig_w, orig_h, lcd_w, lcd_h, hwc_dev->primary_rotation);
2290876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
2291876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /* reorientation matrix is:
2292876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen       m = (center-from-target-center) * (scale-to-target) * (mirror) * (rotate) * (center-to-original-center) */
2293876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
2294876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    memcpy(hwc_dev->primary_m, m_unit, sizeof(m_unit));
2295876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    m_translate(hwc_dev->primary_m, -(orig_w >> 1), -(orig_h >> 1));
2296876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    m_rotate(hwc_dev->primary_m, hwc_dev->primary_rotation);
2297876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (hwc_dev->primary_rotation & 1)
2298876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen         swap(orig_w, orig_h);
2299876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    m_scale(hwc_dev->primary_m, orig_w, lcd_w, orig_h, lcd_h);
2300876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    m_translate(hwc_dev->primary_m, lcd_w >> 1, lcd_h >> 1);
23015db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket}
2302751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2303876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
23042b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnarstatic void handle_hotplug(omap4_hwc_device_t *hwc_dev)
23052125fa148686edfa389121f946377aedaa3d9483Lajos Molnar{
2306ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    omap4_hwc_ext_t *ext = &hwc_dev->ext;
23072b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar    __u8 state = ext->hdmi_state;
2308876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
23095db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    /* Ignore external HDMI logic if the primary display is HDMI */
23105db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    if (hwc_dev->on_tv) {
23115db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        ALOGI("Primary display is HDMI - skip clone/dock logic");
23125db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
23135db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        if (state) {
2314876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            __u32 xres = hwc_dev->fb_dev->base.width;
2315876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            __u32 yres = hwc_dev->fb_dev->base.height;
23165db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket            if (omap4_hwc_set_best_hdmi_mode(hwc_dev, xres, yres, ext->lcd_xpy)) {
2317876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                ALOGE("Failed to set HDMI mode");
23185db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket            }
2319876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            set_primary_display_transform_matrix(hwc_dev);
2320876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
23215db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket            ioctl(hwc_dev->fb_fd, FBIOBLANK, FB_BLANK_UNBLANK);
23225db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
23235db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket            if (hwc_dev->procs && hwc_dev->procs->invalidate) {
23245db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket                hwc_dev->procs->invalidate(hwc_dev->procs);
23255db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket            }
2326876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        } else
23275db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket            ext->last_mode = 0;
23285db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
23295db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        return;
23305db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    }
2331ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar
23322125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    pthread_mutex_lock(&hwc_dev->lock);
2333ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar    ext->dock.enabled = ext->mirror.enabled = 0;
23342125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    if (state) {
233541bb71e35280943ebe07ecfe1cf9e015918164e8Lajos Molnar        /* check whether we can clone and/or dock */
233641bb71e35280943ebe07ecfe1cf9e015918164e8Lajos Molnar        char value[PROPERTY_VALUE_MAX];
2337aa9be8a14744cae50a8f99e6cbb44be0d0c2c938Erik Gilling        property_get("persist.hwc.docking.enabled", value, "1");
2338ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        ext->dock.enabled = atoi(value) > 0;
2339aa9be8a14744cae50a8f99e6cbb44be0d0c2c938Erik Gilling        property_get("persist.hwc.mirroring.enabled", value, "1");
2340ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        ext->mirror.enabled = atoi(value) > 0;
23414e635e537afaa9f1416fa5bdbfd1b03afd8b7e6aLajos Molnar        property_get("persist.hwc.avoid_mode_change", value, "1");
23424e635e537afaa9f1416fa5bdbfd1b03afd8b7e6aLajos Molnar        ext->avoid_mode_change = atoi(value) > 0;
234341bb71e35280943ebe07ecfe1cf9e015918164e8Lajos Molnar
234441bb71e35280943ebe07ecfe1cf9e015918164e8Lajos Molnar        /* get cloning transformation */
2345aa9be8a14744cae50a8f99e6cbb44be0d0c2c938Erik Gilling        property_get("persist.hwc.docking.transform", value, "0");
2346ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        ext->dock.rotation = atoi(value) & EXT_ROTATION;
2347ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        ext->dock.hflip = (atoi(value) & EXT_HFLIP) > 0;
2348ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        ext->dock.docking = 1;
23495db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        property_get("persist.hwc.mirroring.transform", value, hwc_dev->fb_dis.timings.y_res > hwc_dev->fb_dis.timings.x_res ? "3" : "0");
2350ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        ext->mirror.rotation = atoi(value) & EXT_ROTATION;
2351ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        ext->mirror.hflip = (atoi(value) & EXT_HFLIP) > 0;
2352ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        ext->mirror.docking = 0;
2353ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar
23542b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar        if (ext->force_dock) {
23552b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar            /* restrict to docking with no transform */
23562b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar            ext->mirror.enabled = 0;
23572b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar            ext->dock.rotation = 0;
23582b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar            ext->dock.hflip = 0;
23590aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
23600aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar            if (!dock_image.rowbytes) {
23610aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar                property_get("persist.hwc.dock_image", value, "/vendor/res/images/dock/dock.png");
23620aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar                load_png_image(hwc_dev, value, &dock_image);
23630aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar            }
23642b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar        }
23652b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar
2366ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        /* select best mode for mirroring */
2367ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        if (ext->mirror.enabled) {
23683837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar            ext->current = ext->mirror;
23693797fa989f5fee97caea2bbadf433a5a9ac03d8dLajos Molnar            ext->mirror_mode = 0;
23703837de4ff6b7562965758dd2a7af5550af1bd869Lajos Molnar            if (setup_mirroring(hwc_dev) == 0) {
23714e635e537afaa9f1416fa5bdbfd1b03afd8b7e6aLajos Molnar                ext->mirror_mode = ext->last_mode;
2372ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar                ioctl(hwc_dev->hdmi_fb_fd, FBIOBLANK, FB_BLANK_UNBLANK);
23734e635e537afaa9f1416fa5bdbfd1b03afd8b7e6aLajos Molnar            } else
2374ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar                ext->mirror.enabled = 0;
2375ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar        }
237683f80658d865b741a024515c523e373df50c091eSunita Nadampalli        /* Allocate backup buffers for FB rotation
237783f80658d865b741a024515c523e373df50c091eSunita Nadampalli        * This is required only if the FB tranform is different from that
237883f80658d865b741a024515c523e373df50c091eSunita Nadampalli        * of the external display and the FB is not in TILER2D space
237983f80658d865b741a024515c523e373df50c091eSunita Nadampalli        */
238083f80658d865b741a024515c523e373df50c091eSunita Nadampalli        if (ext->mirror.rotation && (limits.fbmem_type != DSSCOMP_FBMEM_TILER2D))
238183f80658d865b741a024515c523e373df50c091eSunita Nadampalli            allocate_tiler2d_buffers(hwc_dev);
238283f80658d865b741a024515c523e373df50c091eSunita Nadampalli
23834cb51e55fa79a592d50d94c0700660e42c988a0bLajos Molnar    } else {
23844cb51e55fa79a592d50d94c0700660e42c988a0bLajos Molnar        ext->last_mode = 0;
238583f80658d865b741a024515c523e373df50c091eSunita Nadampalli        if (ext->mirror.rotation && (limits.fbmem_type != DSSCOMP_FBMEM_TILER2D)) {
238683f80658d865b741a024515c523e373df50c091eSunita Nadampalli            /* free tiler 2D buffer on detach */
238783f80658d865b741a024515c523e373df50c091eSunita Nadampalli            free_tiler2d_buffers(hwc_dev);
238883f80658d865b741a024515c523e373df50c091eSunita Nadampalli        }
23892125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    }
2390bb0a9edbe9d4072ed227550d898f0c2d0149e0baSteve Block    ALOGI("external display changed (state=%d, mirror={%s tform=%ddeg%s}, dock={%s tform=%ddeg%s%s}, tv=%d", state,
2391ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar         ext->mirror.enabled ? "enabled" : "disabled",
2392ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar         ext->mirror.rotation * 90,
2393ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar         ext->mirror.hflip ? "+hflip" : "",
2394ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar         ext->dock.enabled ? "enabled" : "disabled",
2395ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar         ext->dock.rotation * 90,
2396ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar         ext->dock.hflip ? "+hflip" : "",
23972b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar         ext->force_dock ? " forced" : "",
2398ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar         ext->on_tv);
2399ffa0d75e7fa7142eb289ccc9c7dc04e4cedb7c46Lajos Molnar
2400bd0fe036293115c9f9b182ed2a911bdd557ea491Lajos Molnar    pthread_mutex_unlock(&hwc_dev->lock);
2401834b8ba01376239a2f286a3766c14bdeeb166498Lajos Molnar
24023a96e09f206327fb5fd64d3fdc7ba3a315421082Jesse Hall    /* hwc_dev->procs is set right after the device is opened, but there is
24033a96e09f206327fb5fd64d3fdc7ba3a315421082Jesse Hall     * still a race condition where a hotplug event might occur after the open
24043a96e09f206327fb5fd64d3fdc7ba3a315421082Jesse Hall     * but before the procs are registered. */
2405876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (hwc_dev->procs && hwc_dev->procs->invalidate)
2406876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            hwc_dev->procs->invalidate(hwc_dev->procs);
24072125fa148686edfa389121f946377aedaa3d9483Lajos Molnar}
24082125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
2409e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopianstatic void handle_uevents(omap4_hwc_device_t *hwc_dev, const char *buff, int len)
24102125fa148686edfa389121f946377aedaa3d9483Lajos Molnar{
2411e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    int dock;
2412e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    int hdmi;
2413e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    int vsync;
2414e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    int state = 0;
2415e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    uint64_t timestamp = 0;
2416e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    const char *s = buff;
2417e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian
2418e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    dock = !strcmp(s, "change@/devices/virtual/switch/dock");
2419e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    hdmi = !strcmp(s, "change@/devices/virtual/switch/hdmi");
2420bc85c3e357438aca809270bc8e85f5479bbe6a40Erik Gilling    vsync = !strcmp(s, "change@/devices/platform/omapfb") ||
2421bc85c3e357438aca809270bc8e85f5479bbe6a40Erik Gilling        !strcmp(s, "change@/devices/virtual/switch/omapfb-vsync");
2422e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian
2423e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    if (!dock && !vsync && !hdmi)
2424e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian       return;
24252125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
24262125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    s += strlen(s) + 1;
24272125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
24282125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    while(*s) {
2429e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        if (!strncmp(s, "SWITCH_STATE=", strlen("SWITCH_STATE=")))
2430e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian            state = atoi(s + strlen("SWITCH_STATE="));
2431e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        else if (!strncmp(s, "SWITCH_TIME=", strlen("SWITCH_TIME=")))
2432e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian            timestamp = strtoull(s + strlen("SWITCH_TIME="), NULL, 0);
2433bc85c3e357438aca809270bc8e85f5479bbe6a40Erik Gilling        else if (!strncmp(s, "VSYNC=", strlen("VSYNC=")))
2434bc85c3e357438aca809270bc8e85f5479bbe6a40Erik Gilling            timestamp = strtoull(s + strlen("VSYNC="), NULL, 0);
24352125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
24362125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        s += strlen(s) + 1;
2437e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        if (s - buff >= len)
2438e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian            break;
2439e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    }
2440e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian
2441e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    if (vsync) {
2442751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        fire_vsync_event(hwc_dev, timestamp);
2443e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    } else {
2444751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        if (dock) {
2445e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian            hwc_dev->ext.force_dock = state == 1;
2446751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        } else {
2447751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            /* If the primary display is HDMI, VSYNC is enabled, and HDMI's plug
2448751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen             * state has just gone from 1->0, then we need to be sure to start
2449751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen             * faking the VSYNC events.
2450751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen             */
2451751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            if (hwc_dev->on_tv) {
2452751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                int new_state, state_change;
2453751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2454751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                pthread_mutex_lock(&hwc_dev->vsync_lock);
2455751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2456751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                new_state = state == 1;
2457751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                state_change = (new_state != hwc_dev->ext.hdmi_state);
2458751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                hwc_dev->ext.hdmi_state = new_state;
2459751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                if (state_change && !new_state)
2460751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                    wakeup_hdmi_thread(hwc_dev);
2461751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2462751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                pthread_mutex_unlock(&hwc_dev->vsync_lock);
2463751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            } else {
2464751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                hwc_dev->ext.hdmi_state = state == 1;
2465751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            }
2466751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        }
2467e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        handle_hotplug(hwc_dev);
24682125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    }
24692125fa148686edfa389121f946377aedaa3d9483Lajos Molnar}
24702125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
24712125fa148686edfa389121f946377aedaa3d9483Lajos Molnarstatic void *omap4_hwc_hdmi_thread(void *data)
24722125fa148686edfa389121f946377aedaa3d9483Lajos Molnar{
24732125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    omap4_hwc_device_t *hwc_dev = data;
24742125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    static char uevent_desc[4096];
247502150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian    struct pollfd fds[2];
2476b1c5d8ef7fc96ef49a1eaef0d81ff35dc4d2e4e7Jamie Gennis    int invalidate = 0;
247702150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian    int timeout;
247802150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian    int err;
247902150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian
2480e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
2481e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian
24822125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    uevent_init();
24832125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
248402150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian    fds[0].fd = uevent_get_fd();
248502150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian    fds[0].events = POLLIN;
2486751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    fds[1].fd = hwc_dev->wakeup_evt;
248702150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian    fds[1].events = POLLIN;
248802150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian
248902150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian    timeout = hwc_dev->idle ? hwc_dev->idle : -1;
249002150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian
24912125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    memset(uevent_desc, 0, sizeof(uevent_desc));
24922125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
24932125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    do {
2494751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        uint64_t idle_wakeup  = (uint64_t)(-1);
2495751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        uint64_t vsync_wakeup = (uint64_t)(-1);
2496751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        uint64_t now = vsync_clock_now();
2497751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        uint64_t effective_wakeup;
2498751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        int effective_timeout;
2499751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2500751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        if (timeout >= 0)
2501751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            idle_wakeup = now + (((uint64_t)timeout) * 1000000);
2502751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2503751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        if (hwc_dev->on_tv) {
2504751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            pthread_mutex_lock(&hwc_dev->vsync_lock);
2505751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2506751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            if (!hwc_dev->ext.hdmi_state && hwc_dev->vsync_enabled) {
2507751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                vsync_wakeup = hwc_dev->last_vsync_time_valid
2508751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                             ? hwc_dev->last_vsync_time
2509751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                             : now;
2510751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2511751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                vsync_wakeup += hwc_dev->fake_vsync_period;
2512751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2513751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                if (vsync_wakeup < now)
2514751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                    vsync_wakeup = now;
2515751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            }
2516751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2517751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            pthread_mutex_unlock(&hwc_dev->vsync_lock);
2518751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        }
2519751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2520751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        effective_wakeup = idle_wakeup < vsync_wakeup
2521751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                         ? idle_wakeup
2522751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                         : vsync_wakeup;
2523751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        if (effective_wakeup == (uint64_t)(-1))
2524751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            effective_timeout = -1;
2525751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        else if (effective_wakeup <= now)
2526751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            effective_timeout = 0;
2527751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        else
2528751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            effective_timeout = (int)((effective_wakeup - now + 999999) / 1000000);
2529751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2530751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        if (effective_timeout)
2531751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            err = poll(fds, 2, effective_timeout);
2532751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        else
2533751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            err = 0;
2534751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2535751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        now = vsync_clock_now();
253602150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian
253702150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian        if (err == 0) {
2538751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            int fired = 0;
2539751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2540751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            if (now >= vsync_wakeup) {
2541751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                fire_vsync_event(hwc_dev, vsync_wakeup);
2542751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                fired = 1;
2543751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            }
2544751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2545751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            if (hwc_dev->idle && (now >= idle_wakeup)) {
25463a96e09f206327fb5fd64d3fdc7ba3a315421082Jesse Hall                if (hwc_dev->procs) {
2547b1c5d8ef7fc96ef49a1eaef0d81ff35dc4d2e4e7Jamie Gennis                    pthread_mutex_lock(&hwc_dev->lock);
2548a9bec459a62ebc71f9e74832047c2d9da8a84ff0Dima Svetlov                    invalidate = !hwc_dev->force_sgx;
2549b1c5d8ef7fc96ef49a1eaef0d81ff35dc4d2e4e7Jamie Gennis                    if (invalidate) {
2550b1c5d8ef7fc96ef49a1eaef0d81ff35dc4d2e4e7Jamie Gennis                        hwc_dev->force_sgx = 2;
2551b1c5d8ef7fc96ef49a1eaef0d81ff35dc4d2e4e7Jamie Gennis                    }
2552b1c5d8ef7fc96ef49a1eaef0d81ff35dc4d2e4e7Jamie Gennis                    pthread_mutex_unlock(&hwc_dev->lock);
2553b1c5d8ef7fc96ef49a1eaef0d81ff35dc4d2e4e7Jamie Gennis
2554b1c5d8ef7fc96ef49a1eaef0d81ff35dc4d2e4e7Jamie Gennis                    if (invalidate) {
2555b1c5d8ef7fc96ef49a1eaef0d81ff35dc4d2e4e7Jamie Gennis                        hwc_dev->procs->invalidate(hwc_dev->procs);
2556b1c5d8ef7fc96ef49a1eaef0d81ff35dc4d2e4e7Jamie Gennis                        timeout = -1;
2557b1c5d8ef7fc96ef49a1eaef0d81ff35dc4d2e4e7Jamie Gennis                    }
255802150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian                }
255902150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian
2560751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                fired = 1;
256102150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian            }
2562751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2563751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            if (fired)
2564751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                continue;
256502150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian        }
256602150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian
256702150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian        if (err == -1) {
256802150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian            if (errno != EINTR)
256946de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block                ALOGE("event error: %m");
257002150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian            continue;
257102150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian        }
257202150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian
2573751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        if (fds[1].revents & POLLIN) {
2574751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            uint64_t tmp;
2575751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2576751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            read(hwc_dev->wakeup_evt, &tmp, sizeof(tmp));
2577751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
257802150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian            if (!hwc_dev->force_sgx)
257902150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian                timeout = hwc_dev->idle ? hwc_dev->idle : -1;
258002150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian        }
258102150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian
258202150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian        if (fds[0].revents & POLLIN) {
258302150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian            /* keep last 2 zeroes to ensure double 0 termination */
2584e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian            int len = uevent_next_event(uevent_desc, sizeof(uevent_desc) - 2);
2585e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian            handle_uevents(hwc_dev, uevent_desc, len);
258602150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian        }
25872125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    } while (1);
25882125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
25892125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    return NULL;
25902125fa148686edfa389121f946377aedaa3d9483Lajos Molnar}
25912125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
25926ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFaddenstatic void omap4_hwc_registerProcs(struct hwc_composer_device_1* dev,
25932125fa148686edfa389121f946377aedaa3d9483Lajos Molnar                                    hwc_procs_t const* procs)
25942125fa148686edfa389121f946377aedaa3d9483Lajos Molnar{
2595b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar    omap4_hwc_device_t *hwc_dev = (omap4_hwc_device_t *) dev;
25962125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
2597b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar    hwc_dev->procs = (typeof(hwc_dev->procs)) procs;
25982125fa148686edfa389121f946377aedaa3d9483Lajos Molnar}
25992125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
26006ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFaddenstatic int omap4_hwc_query(struct hwc_composer_device_1* dev,
2601e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        int what, int* value)
2602e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian{
2603e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    omap4_hwc_device_t *hwc_dev = (omap4_hwc_device_t *) dev;
2604e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian
2605e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    switch (what) {
2606e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    case HWC_BACKGROUND_LAYER_SUPPORTED:
2607e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        // we don't support the background layer yet
2608e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        value[0] = 0;
2609e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        break;
2610e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    case HWC_VSYNC_PERIOD:
2611e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        // vsync period in nanosecond
2612e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        value[0] = 1000000000.0 / hwc_dev->fb_dev->base.fps;
2613e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        break;
2614e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    default:
2615e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        // unsupported query
2616e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        return -EINVAL;
2617e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    }
2618e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    return 0;
2619e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian}
2620e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian
26216ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFaddenstatic int omap4_hwc_event_control(struct hwc_composer_device_1* dev,
26226ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden        int dpy, int event, int enabled)
2623e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian{
2624e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    omap4_hwc_device_t *hwc_dev = (omap4_hwc_device_t *) dev;
2625e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian
2626e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    switch (event) {
2627e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    case HWC_EVENT_VSYNC:
2628e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    {
2629e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        int val = !!enabled;
2630e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        int err;
2631e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian
2632751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        /* If the primary display is HDMI, then we need to be sure to fake a
2633751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen         * stream vsync events if vsync is enabled, but HDMI happens to be
2634751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen         * disconnected.
2635751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen         */
2636751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        if (hwc_dev->on_tv) {
2637751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            pthread_mutex_lock(&hwc_dev->vsync_lock);
2638751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2639751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            if (!val)
2640751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                hwc_dev->last_vsync_time_valid = 0;
2641751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2642751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            /* If VSYNC is enabled, but HDMI is not actually plugged in, we need
2643751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen             * to fake it.  Poke the work thread to make sure it is taking care
2644751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen             * of things.
2645751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen             */
2646751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            if (!hwc_dev->ext.hdmi_state && !hwc_dev->vsync_enabled && val)
2647751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen                wakeup_hdmi_thread(hwc_dev);
2648751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2649751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            hwc_dev->vsync_enabled = val;
2650751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2651751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            err = ioctl(hwc_dev->fb_fd, OMAPFB_ENABLEVSYNC, &val);
2652751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            pthread_mutex_unlock(&hwc_dev->vsync_lock);
2653751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        } else {
2654751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            err = ioctl(hwc_dev->fb_fd, OMAPFB_ENABLEVSYNC, &val);
2655751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        }
2656751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2657e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        if (err < 0)
2658e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian            return -errno;
2659e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian
2660e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        return 0;
2661e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    }
2662e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    default:
2663e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian        return -EINVAL;
2664e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    }
2665e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian}
2666e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian
26673a96e09f206327fb5fd64d3fdc7ba3a315421082Jesse Hallstatic int omap4_hwc_blank(struct hwc_composer_device_1 *dev, int dpy, int blank)
26686ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden{
26696ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    // We're using an older method of screen blanking based on
26706ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    // early_suspend in the kernel.  No need to do anything here.
26716ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    return 0;
26726ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden}
26736ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden
2674c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatic int omap4_hwc_device_open(const hw_module_t* module, const char* name,
2675c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                hw_device_t** device)
2676c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2677c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    omap4_hwc_module_t *hwc_mod = (omap4_hwc_module_t *)module;
2678c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    omap4_hwc_device_t *hwc_dev;
26792125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    int err = 0;
2680c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2681c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
2682c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return -EINVAL;
2683c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2684c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2685c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (!hwc_mod->fb_dev) {
2686c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        err = omap4_hwc_open_fb_hal(&hwc_mod->fb_dev);
2687c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if (err)
2688c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            return err;
2689c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2690c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if (!hwc_mod->fb_dev) {
269146de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block            ALOGE("Framebuffer HAL not opened before HWC");
2692c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            return -EFAULT;
2693c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2694c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        hwc_mod->fb_dev->bBypassPost = 1;
2695c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2696c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2697c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    hwc_dev = (omap4_hwc_device_t *)malloc(sizeof(*hwc_dev));
2698c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (hwc_dev == NULL)
2699c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return -ENOMEM;
2700c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2701c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    memset(hwc_dev, 0, sizeof(*hwc_dev));
2702c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2703c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    hwc_dev->base.common.tag = HARDWARE_DEVICE_TAG;
27046ce89d3c3ec5d07a21bd1fc33f6ff61d67acbe95Andy McFadden    hwc_dev->base.common.version = HWC_DEVICE_API_VERSION_1_0;
2705c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    hwc_dev->base.common.module = (hw_module_t *)module;
2706c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    hwc_dev->base.common.close = omap4_hwc_device_close;
2707c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    hwc_dev->base.prepare = omap4_hwc_prepare;
2708c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    hwc_dev->base.set = omap4_hwc_set;
27093a96e09f206327fb5fd64d3fdc7ba3a315421082Jesse Hall    hwc_dev->base.eventControl = omap4_hwc_event_control;
27103a96e09f206327fb5fd64d3fdc7ba3a315421082Jesse Hall    hwc_dev->base.blank = omap4_hwc_blank;
2711e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian    hwc_dev->base.query = omap4_hwc_query;
27123a96e09f206327fb5fd64d3fdc7ba3a315421082Jesse Hall    hwc_dev->base.registerProcs = omap4_hwc_registerProcs;
27133a96e09f206327fb5fd64d3fdc7ba3a315421082Jesse Hall    hwc_dev->base.dump = omap4_hwc_dump;
2714c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    hwc_dev->fb_dev = hwc_mod->fb_dev;
2715751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    hwc_dev->wakeup_evt = -1;
2716c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    *device = &hwc_dev->base.common;
2717c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2718751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    hwc_dev->vsync_enabled = 0;
2719751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    hwc_dev->last_vsync_time_valid = 0;
2720751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    hwc_dev->fake_vsync_period = 1000000000ull/60;
2721751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
27222125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    hwc_dev->dsscomp_fd = open("/dev/dsscomp", O_RDWR);
27237f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar    if (hwc_dev->dsscomp_fd < 0) {
272446de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("failed to open dsscomp (%d)", errno);
27257f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar        err = -errno;
27267f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar        goto done;
27277f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar    }
27282125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
2729876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int ret = ioctl(hwc_dev->dsscomp_fd, DSSCIOC_QUERY_PLATFORM, &limits);
2730876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (ret) {
2731876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        ALOGE("failed to get platform limits (%d): %m", errno);
2732876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        err = -errno;
2733876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        goto done;
2734876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
2735876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
27367f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar    hwc_dev->fb_fd = open("/dev/graphics/fb0", O_RDWR);
27377f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar    if (hwc_dev->fb_fd < 0) {
273846de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("failed to open fb (%d)", errno);
27397f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar        err = -errno;
27407f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar        goto done;
27417f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar    }
27422125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
27430aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    struct fb_fix_screeninfo fix;
27440aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    if (ioctl(hwc_dev->fb_fd, FBIOGET_FSCREENINFO, &fix)) {
274546de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("failed to get fb info (%d)", errno);
27460aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        err = -errno;
27470aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        goto done;
27480aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    }
27490aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
27500aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    hwc_dev->img_mem_size = fix.smem_len;
27510aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    hwc_dev->img_mem_ptr = mmap(NULL, fix.smem_len, PROT_WRITE, MAP_SHARED, hwc_dev->fb_fd, 0);
27520aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    if (hwc_dev->img_mem_ptr == MAP_FAILED) {
275346de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("failed to map fb memory");
27540aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        err = -errno;
27550aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar        goto done;
27560aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar    }
27570aead355133c2a087cb369c5eb62a07cbd4a13beLajos Molnar
2758876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    /* Allocate the maximum buffers that we can receive from HWC */
2759876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    hwc_dev->buffers = malloc(sizeof(buffer_handle_t) * MAX_HWC_LAYERS);
2760c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (!hwc_dev->buffers) {
27612125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        err = -ENOMEM;
27622125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        goto done;
27632125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    }
27642125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
2765876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    ret = ioctl(hwc_dev->dsscomp_fd, DSSCIOC_QUERY_DISPLAY, &hwc_dev->fb_dis);
2766d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    if (ret) {
276746de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("failed to get display info (%d): %m", errno);
2768d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar        err = -errno;
2769d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar        goto done;
2770d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar    }
2771f1eab7b129ea2cf4bbbbfbec717a07b4419313c6Oleksandr Lugovyk
277283f80658d865b741a024515c523e373df50c091eSunita Nadampalli    hwc_dev->ion_fd = ion_open();
277383f80658d865b741a024515c523e373df50c091eSunita Nadampalli    if (hwc_dev->ion_fd < 0) {
277483f80658d865b741a024515c523e373df50c091eSunita Nadampalli        ALOGE("failed to open ion driver (%d)", errno);
277583f80658d865b741a024515c523e373df50c091eSunita Nadampalli    }
277683f80658d865b741a024515c523e373df50c091eSunita Nadampalli
277783f80658d865b741a024515c523e373df50c091eSunita Nadampalli    int i;
277883f80658d865b741a024515c523e373df50c091eSunita Nadampalli    for (i = 0; i < NUM_EXT_DISPLAY_BACK_BUFFERS; i++) {
277983f80658d865b741a024515c523e373df50c091eSunita Nadampalli        hwc_dev->ion_handles[i] = NULL;
278083f80658d865b741a024515c523e373df50c091eSunita Nadampalli    }
278183f80658d865b741a024515c523e373df50c091eSunita Nadampalli
2782f1eab7b129ea2cf4bbbbfbec717a07b4419313c6Oleksandr Lugovyk    /* use default value in case some of requested display parameters missing */
2783f1eab7b129ea2cf4bbbbfbec717a07b4419313c6Oleksandr Lugovyk    hwc_dev->ext.lcd_xpy = 1.0;
2784f1eab7b129ea2cf4bbbbfbec717a07b4419313c6Oleksandr Lugovyk    if (hwc_dev->fb_dis.timings.x_res && hwc_dev->fb_dis.height_in_mm) {
2785f1eab7b129ea2cf4bbbbfbec717a07b4419313c6Oleksandr Lugovyk        hwc_dev->ext.lcd_xpy = (float)
2786f1eab7b129ea2cf4bbbbfbec717a07b4419313c6Oleksandr Lugovyk            hwc_dev->fb_dis.width_in_mm / hwc_dev->fb_dis.timings.x_res /
2787f1eab7b129ea2cf4bbbbfbec717a07b4419313c6Oleksandr Lugovyk            hwc_dev->fb_dis.height_in_mm * hwc_dev->fb_dis.timings.y_res;
2788f1eab7b129ea2cf4bbbbfbec717a07b4419313c6Oleksandr Lugovyk    }
2789d98920b36b52ca1784742cbd053c631ae62ca1c4Lajos Molnar
27906d94b7785c5318b6de36193a7f5488a88b6178ebDandawate Saket    if (hwc_dev->fb_dis.channel == OMAP_DSS_CHANNEL_DIGIT) {
27915db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        ALOGI("Primary display is HDMI");
27925db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        hwc_dev->on_tv = 1;
27935db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    }
27946d94b7785c5318b6de36193a7f5488a88b6178ebDandawate Saket    else {
27955db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        hwc_dev->hdmi_fb_fd = open("/dev/graphics/fb1", O_RDWR);
27965db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        if (hwc_dev->hdmi_fb_fd < 0) {
27975db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket            ALOGE("failed to open hdmi fb (%d)", errno);
27985db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket            err = -errno;
27995db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket            goto done;
28005db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket        }
28015db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket    }
2802876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    set_primary_display_transform_matrix(hwc_dev);
28035db21d521ed41e2e2cc110b698981e841e027287Dandawate Saket
2804751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    if ((hwc_dev->wakeup_evt = eventfd(0, EFD_NONBLOCK)) < 0) {
2805751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            ALOGE("failed to eventfd (%d): %m", errno);
280602150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian            err = -errno;
280702150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian            goto done;
280802150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian    }
280902150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian
28102125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    if (pthread_mutex_init(&hwc_dev->lock, NULL)) {
281146de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("failed to create mutex (%d): %m", errno);
2812b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar        err = -errno;
2813b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar        goto done;
28142125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    }
2815751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2816751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    if (pthread_mutex_init(&hwc_dev->vsync_lock, NULL)) {
2817751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        ALOGE("failed to create vsync mutex (%d): %m", errno);
2818751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        err = -errno;
2819751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        goto done;
2820751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen    }
2821751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen
2822876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (pthread_create(&hwc_dev->hdmi_thread, NULL, omap4_hwc_hdmi_thread, hwc_dev))
2823876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    {
282446de639b23db99d7b99ff1c676ac98b84b6336c6Steve Block        ALOGE("failed to create HDMI listening thread (%d): %m", errno);
2825b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar        err = -errno;
2826b79b42d125e488c0bcf101edfd3b1f30bafae53eLajos Molnar        goto done;
2827c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2828c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2829c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* get debug properties */
2830c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2831c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* see if hwc is enabled at all */
2832c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    char value[PROPERTY_VALUE_MAX];
2833c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    property_get("debug.hwc.rgb_order", value, "1");
2834c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    hwc_dev->flags_rgb_order = atoi(value);
2835c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    property_get("debug.hwc.nv12_only", value, "0");
2836c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    hwc_dev->flags_nv12_only = atoi(value);
283702150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian    property_get("debug.hwc.idle", value, "250");
283802150d77cba303ea6c532fa740910fbab5e3e1ccMathias Agopian    hwc_dev->idle = atoi(value);
28392125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
28408d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    /* get the board specific clone properties */
28418d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    /* 0:0:1280:720 */
2842dd922b9387f70e24b3462afe1bdc86ceb7541d4bLajos Molnar    if (property_get("persist.hwc.mirroring.region", value, "") <= 0 ||
28438d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        sscanf(value, "%d:%d:%d:%d",
28448d546610cba3698c62af537d97e38a7368362f1aLajos Molnar               &hwc_dev->ext.mirror_region.left, &hwc_dev->ext.mirror_region.top,
28458d546610cba3698c62af537d97e38a7368362f1aLajos Molnar               &hwc_dev->ext.mirror_region.right, &hwc_dev->ext.mirror_region.bottom) != 4 ||
28468d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        hwc_dev->ext.mirror_region.left >= hwc_dev->ext.mirror_region.right ||
28478d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        hwc_dev->ext.mirror_region.top >= hwc_dev->ext.mirror_region.bottom) {
2848876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        struct hwc_rect fb_region = { .right = hwc_dev->fb_dev->base.width, .bottom = hwc_dev->fb_dev->base.height };
28498d546610cba3698c62af537d97e38a7368362f1aLajos Molnar        hwc_dev->ext.mirror_region = fb_region;
28508d546610cba3698c62af537d97e38a7368362f1aLajos Molnar    }
2851bb0a9edbe9d4072ed227550d898f0c2d0149e0baSteve Block    ALOGI("clone region is set to (%d,%d) to (%d,%d)",
28528d546610cba3698c62af537d97e38a7368362f1aLajos Molnar         hwc_dev->ext.mirror_region.left, hwc_dev->ext.mirror_region.top,
28538d546610cba3698c62af537d97e38a7368362f1aLajos Molnar         hwc_dev->ext.mirror_region.right, hwc_dev->ext.mirror_region.bottom);
28548d546610cba3698c62af537d97e38a7368362f1aLajos Molnar
28552125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    /* read switch state */
28562125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    int sw_fd = open("/sys/class/switch/hdmi/state", O_RDONLY);
28572125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    if (sw_fd >= 0) {
28582125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        char value;
28592125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        if (read(sw_fd, &value, 1) == 1)
28602b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar            hwc_dev->ext.hdmi_state = value == '1';
28612b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar        close(sw_fd);
28622b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar    }
28632b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar    sw_fd = open("/sys/class/switch/dock/state", O_RDONLY);
28642b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar    if (sw_fd >= 0) {
28652b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar        char value;
28662b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar        if (read(sw_fd, &value, 1) == 1)
28672b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar            hwc_dev->ext.force_dock = value == '1';
28682125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        close(sw_fd);
28692125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    }
28702b86fd0cbeb42123fc20b1f646ee81299e831d16Lajos Molnar    handle_hotplug(hwc_dev);
28712125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
2872bb0a9edbe9d4072ed227550d898f0c2d0149e0baSteve Block    ALOGI("omap4_hwc_device_open(rgb_order=%d nv12_only=%d)",
2873c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        hwc_dev->flags_rgb_order, hwc_dev->flags_nv12_only);
2874c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2875876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    int gc2d_fd = open("/dev/gcioctl", O_RDWR);
2876876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    if (gc2d_fd < 0) {
2877876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        ALOGI("Unable to open gc-core device (%d), blits disabled", errno);
2878876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        hwc_dev->blt_policy = BLTPOLICY_DISABLED;
2879876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    } else {
2880876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        property_get("persist.hwc.bltmode", value, "1");
2881876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        hwc_dev->blt_mode = atoi(value);
2882876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        property_get("persist.hwc.bltpolicy", value, "1");
2883876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        hwc_dev->blt_policy = atoi(value);
2884876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        ALOGI("blitter present, blits mode %d, blits policy %d", hwc_dev->blt_mode, hwc_dev->blt_policy);
2885876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        close(gc2d_fd);
2886876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
2887876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        if (rgz_get_screengeometry(hwc_dev->fb_fd, &gscrngeom,
2888876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen                hwc_dev->fb_dev->base.format) != 0) {
2889876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            err = -EINVAL;
2890876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen            goto done;
2891876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen        }
2892876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen    }
2893876331fc77f6763c8c390d1289ead00f89f8d9feMike J. Chen
2894f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar    property_get("persist.hwc.upscaled_nv12_limit", value, "2.");
2895f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar    sscanf(value, "%f", &hwc_dev->upscaled_nv12_limit);
2896f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar    if (hwc_dev->upscaled_nv12_limit < 0. || hwc_dev->upscaled_nv12_limit > 2048.) {
2897f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar        ALOGW("Invalid upscaled_nv12_limit (%s), setting to 2.", value);
2898f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar        hwc_dev->upscaled_nv12_limit = 2.;
2899f11c7bb8c6e7d0254eeaa4aadb8eae412580ac0eLajos Molnar    }
29001fd204fb13367eb8279a1e2d1ee245ff0f441571Tony Lofthouse
29012125fa148686edfa389121f946377aedaa3d9483Lajos Molnardone:
29022125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    if (err && hwc_dev) {
29032125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        if (hwc_dev->dsscomp_fd >= 0)
29042125fa148686edfa389121f946377aedaa3d9483Lajos Molnar            close(hwc_dev->dsscomp_fd);
29052125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        if (hwc_dev->hdmi_fb_fd >= 0)
29062125fa148686edfa389121f946377aedaa3d9483Lajos Molnar            close(hwc_dev->hdmi_fb_fd);
29077f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar        if (hwc_dev->fb_fd >= 0)
29087f2cbf97908a26e0b463d4fa20040b129216f86eLajos Molnar            close(hwc_dev->fb_fd);
2909751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        if (hwc_dev->wakeup_evt >= 0)
2910751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen            close(hwc_dev->wakeup_evt);
29112125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        pthread_mutex_destroy(&hwc_dev->lock);
2912751880491cd2d46a6bb0ff1070a0dc05557aa041Mike J. Chen        pthread_mutex_destroy(&hwc_dev->vsync_lock);
29132125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        free(hwc_dev->buffers);
29142125fa148686edfa389121f946377aedaa3d9483Lajos Molnar        free(hwc_dev);
29152125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    }
29162125fa148686edfa389121f946377aedaa3d9483Lajos Molnar
29172125fa148686edfa389121f946377aedaa3d9483Lajos Molnar    return err;
2918c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2919c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2920c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatic struct hw_module_methods_t omap4_hwc_module_methods = {
2921c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    .open = omap4_hwc_device_open,
2922c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev};
2923c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2924c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevomap4_hwc_module_t HAL_MODULE_INFO_SYM = {
2925c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    .base = {
2926c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        .common = {
2927c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            .tag =                  HARDWARE_MODULE_TAG,
2928e914b7e0a972af85845268ecf962773b8e4da977Mathias Agopian            .module_api_version =   HWC_MODULE_API_VERSION_0_1,
2929a35209b2e26289458670e04d6e62dee3d085ea63Mathias Agopian            .hal_api_version =      HARDWARE_HAL_API_VERSION,
2930c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            .id =                   HWC_HARDWARE_MODULE_ID,
2931c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            .name =                 "OMAP 44xx Hardware Composer HAL",
2932c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            .author =               "Texas Instruments",
2933c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            .methods =              &omap4_hwc_module_methods,
2934c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        },
2935c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    },
2936c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev};
2937