1a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian/*
2a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * Copyright (C) 2010 The Android Open Source Project
3a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *
4a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * you may not use this file except in compliance with the License.
6a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * You may obtain a copy of the License at
7a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *
8a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *
10a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * Unless required by applicable law or agreed to in writing, software
11a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * See the License for the specific language governing permissions and
14a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * limitations under the License.
15a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian */
16a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
17a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#ifndef ANDROID_SF_HWCOMPOSER_H
18a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#define ANDROID_SF_HWCOMPOSER_H
19a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
20a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <stdint.h>
21a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <sys/types.h>
22a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
233e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian#include <hardware/hwcomposer_defs.h>
24a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
252ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis#include <ui/Fence.h>
262ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis
272ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis#include <utils/BitSet.h>
28921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <utils/Condition.h>
29921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <utils/Mutex.h>
30c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian#include <utils/StrongPointer.h>
31921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <utils/Thread.h>
32921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <utils/Timers.h>
3322da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian#include <utils/Vector.h>
34c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
353eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopianextern "C" int clock_nanosleep(clockid_t clock_id, int flags,
363eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian                           const struct timespec *request,
373eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian                           struct timespec *remain);
383eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
395880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallstruct hwc_composer_device_1;
40b685c542836b93c99cd85053e07696406ea37adbJesse Hallstruct hwc_display_contents_1;
41da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianstruct hwc_layer_1;
423e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianstruct hwc_procs;
438b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopianstruct framebuffer_device_t;
443e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
45a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopiannamespace android {
46a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
47a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
48da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianclass Fence;
496b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopianclass FloatRect;
50399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hallclass GraphicBuffer;
51399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hallclass NativeHandle;
521a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennisclass Region;
538372785879d329f592f6883620b5a32d80d74691Mathias Agopianclass String8;
54c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianclass SurfaceFlinger;
558372785879d329f592f6883620b5a32d80d74691Mathias Agopian
56a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianclass HWComposer
57a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
58a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianpublic:
593eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    class EventHandler {
603eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        friend class HWComposer;
611bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        virtual void onVSyncReceived(int disp, nsecs_t timestamp) = 0;
62148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian        virtual void onHotplugReceived(int disp, bool connected) = 0;
633eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    protected:
643eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        virtual ~EventHandler() {}
653eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    };
66a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
671e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    enum {
689e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall        NUM_BUILTIN_DISPLAYS = HWC_NUM_PHYSICAL_DISPLAY_TYPES,
699e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall        MAX_HWC_DISPLAYS = HWC_NUM_DISPLAY_TYPES,
709e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall        VIRTUAL_DISPLAY_ID_BASE = HWC_DISPLAY_VIRTUAL,
711e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    };
721e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
738b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian    HWComposer(
748b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian            const sp<SurfaceFlinger>& flinger,
75b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            EventHandler& handler);
768b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
77a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    ~HWComposer();
78a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
79a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    status_t initCheck() const;
80a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
819e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    // Returns a display ID starting at VIRTUAL_DISPLAY_ID_BASE, this ID is to
829e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    // be used with createWorkList (and all other methods requiring an ID
839e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    // below).
849e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    // IDs below NUM_BUILTIN_DISPLAYS are pre-defined and therefore are
859e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    // always valid.
869e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    // Returns -1 if an ID cannot be allocated
87e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    int32_t allocateDisplayId();
88e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
899e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    // Recycles the given virtual display ID and frees the associated worklist.
909e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    // IDs below NUM_BUILTIN_DISPLAYS are not recycled.
91e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    status_t freeDisplayId(int32_t id);
92e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
93e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
94a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    // Asks the HAL what it can do
95e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    status_t prepare();
96a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
97a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    // commits the list
9830bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    status_t commit();
99a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
1002c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani    // set power mode
1012c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani    status_t setPowerMode(int disp, int mode);
10210fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross
1036c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    // set active config
1046c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    status_t setActiveConfig(int disp, int mode);
1056c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine
10627ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    // reset state when an external, non-virtual display is disconnected
10727ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    void disconnectDisplay(int disp);
10827ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden
1093e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
1101e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    status_t createWorkList(int32_t id, size_t numLayers);
111a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
112da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    bool supportsFramebufferTarget() const;
113da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
114e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    // does this display have layers handled by HWC
115e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    bool hasHwcComposition(int32_t id) const;
116e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
117e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    // does this display have layers handled by GLES
118e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    bool hasGlesComposition(int32_t id) const;
1199c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
12080e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall    // get the releaseFence file descriptor for a display's framebuffer layer.
121da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    // the release fence is only valid after commit()
12213f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall    sp<Fence> getAndResetReleaseFence(int32_t id);
123da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
1243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // needed forward declarations
1253e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    class LayerListIterator;
1263e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
127cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    // return the visual id to be used to find a suitable EGLConfig for
128cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    // *ALL* displays.
129cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    int getVisualID() const;
130cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
131b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // Forwarding to FB HAL for pre-HWC-1.1 code (see FramebufferSurface).
132da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int fbPost(int32_t id, const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
133b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    int fbCompositionComplete();
134b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    void fbDump(String8& result);
135b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
136851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    // Set the output buffer and acquire fence for a virtual display.
137851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    // Returns INVALID_OPERATION if id is not a virtual display.
138851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    status_t setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
139851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall            const sp<GraphicBuffer>& buf);
140851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall
141851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    // Get the retire fence for the last committed frame. This fence will
142851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    // signal when the h/w composer is completely finished with the frame.
143851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    // For physical displays, it is no longer being displayed. For virtual
144851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    // displays, writes to the output buffer are complete.
14541d67d7ab4da1c393497a620a116a854b3c618e7Andy McFadden    sp<Fence> getLastRetireFence(int32_t id) const;
146851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall
14703414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    status_t setCursorPositionAsync(int32_t id, const Rect &pos);
14803414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews
1493e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    /*
1503e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian     * Interface to hardware composer's layers functionality.
1513e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian     * This abstracts the HAL interface to layers which can evolve in
1523e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian     * incompatible ways from one release to another.
1533e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian     * The idea is that we could extend this interface as we add
1543e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian     * features to h/w composer.
1553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian     */
1563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    class HWCLayerInterface {
1573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    protected:
1583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        virtual ~HWCLayerInterface() { }
1593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    public:
1603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        virtual int32_t getCompositionType() const = 0;
1613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        virtual uint32_t getHints() const = 0;
16213f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall        virtual sp<Fence> getAndResetReleaseFence() = 0;
1633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        virtual void setDefaultState() = 0;
1643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        virtual void setSkip(bool skip) = 0;
16503414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        virtual void setIsCursorLayerHint(bool isCursor = true) = 0;
1663e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        virtual void setBlending(uint32_t blending) = 0;
1673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        virtual void setTransform(uint32_t transform) = 0;
1683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        virtual void setFrame(const Rect& frame) = 0;
1696b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        virtual void setCrop(const FloatRect& crop) = 0;
1703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        virtual void setVisibleRegionScreen(const Region& reg) = 0;
171399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall        virtual void setSidebandStream(const sp<NativeHandle>& stream) = 0;
1723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
173dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        virtual void setAcquireFenceFd(int fenceFd) = 0;
1749f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        virtual void setPlaneAlpha(uint8_t alpha) = 0;
175c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        virtual void onDisplayed() = 0;
1763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    };
1773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
1783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    /*
1793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian     * Interface used to implement an iterator to a list
1803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian     * of HWCLayer.
1813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian     */
1823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    class HWCLayer : public HWCLayerInterface {
1833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        friend class LayerListIterator;
1843e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // select the layer at the given index
1853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        virtual status_t setLayer(size_t index) = 0;
1863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        virtual HWCLayer* dup() = 0;
1873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        static HWCLayer* copy(HWCLayer *rhs) {
1883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            return rhs ? rhs->dup() : NULL;
1893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
1903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    protected:
1913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        virtual ~HWCLayer() { }
1923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    };
1933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
1943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    /*
1953e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian     * Iterator through a HWCLayer list.
1963e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian     * This behaves more or less like a forward iterator.
1973e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian     */
1983e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    class LayerListIterator {
1991495ceb88594bf3c5963ef65ee12953dc0354546Jesse Hall        friend class HWComposer;
2003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        HWCLayer* const mLayerList;
2013e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        size_t mIndex;
2023e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
2033e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        LayerListIterator() : mLayerList(NULL), mIndex(0) { }
2043e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
2053e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        LayerListIterator(HWCLayer* layer, size_t index)
2063e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            : mLayerList(layer), mIndex(index) { }
2073e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
2083e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // we don't allow assignment, because we don't need it for now
2093e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        LayerListIterator& operator = (const LayerListIterator& rhs);
2103e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
2113e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    public:
2123e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // copy operators
2133e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        LayerListIterator(const LayerListIterator& rhs)
2143e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
2153e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
2163e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
2173e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        ~LayerListIterator() { delete mLayerList; }
2183e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
2193e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // pre-increment
2203e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        LayerListIterator& operator++() {
2213e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            mLayerList->setLayer(++mIndex);
2223e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            return *this;
2233e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
2243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
2253e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // dereference
2263e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        HWCLayerInterface& operator * () { return *mLayerList; }
2273e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        HWCLayerInterface* operator -> () { return mLayerList; }
2283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
2293e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // comparison
2303e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        bool operator == (const LayerListIterator& rhs) const {
2313e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            return mIndex == rhs.mIndex;
2323e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
2333e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        bool operator != (const LayerListIterator& rhs) const {
2343e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            return !operator==(rhs);
2353e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
2363e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    };
2373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
2383e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // Returns an iterator to the beginning of the layer list
2391e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    LayerListIterator begin(int32_t id);
2403e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
2413e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // Returns an iterator to the end of the layer list
2421e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    LayerListIterator end(int32_t id);
2433e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
2443e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
2453eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    // Events handling ---------------------------------------------------------
2463eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
2473eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    enum {
2483eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        EVENT_VSYNC = HWC_EVENT_VSYNC
2493eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    };
2503eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
25181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    void eventControl(int disp, int event, int enabled);
2523eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
2537f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    struct DisplayConfig {
2547f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        uint32_t width;
2557f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        uint32_t height;
2567f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        float xdpi;
2577f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        float ydpi;
2587f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        nsecs_t refresh;
2597f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    };
2607f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
261b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // Query display parameters.  Pass in a display index (e.g.
262b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // HWC_DISPLAY_PRIMARY).
263b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    nsecs_t getRefreshTimestamp(int disp) const;
2642ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis    sp<Fence> getDisplayFence(int disp) const;
2657f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    uint32_t getFormat(int disp) const;
2667f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    bool isConnected(int disp) const;
2677f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
2687f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    // These return the values for the current config of a given display index.
2697f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    // To get the values for all configs, use getConfigs below.
270db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    uint32_t getWidth(int disp) const;
271db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    uint32_t getHeight(int disp) const;
272b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    float getDpiX(int disp) const;
273b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    float getDpiY(int disp) const;
2747f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    nsecs_t getRefreshPeriod(int disp) const;
2757f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
2767f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    const Vector<DisplayConfig>& getConfigs(int disp) const;
2777f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t getCurrentConfig(int disp) const;
278d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
2791c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    status_t setVirtualDisplayProperties(int32_t id, uint32_t w, uint32_t h,
2801c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall            uint32_t format);
2811c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall
2823eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    // this class is only used to fake the VSync event on systems that don't
2833eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    // have it.
2843eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    class VSyncThread : public Thread {
2853eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        HWComposer& mHwc;
2863eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mutable Mutex mLock;
2873eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        Condition mCondition;
2883eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        bool mEnabled;
2893eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mutable nsecs_t mNextFakeVSync;
2903eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        nsecs_t mRefreshPeriod;
2912965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        virtual void onFirstRef();
2922965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        virtual bool threadLoop();
2933eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    public:
2942965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        VSyncThread(HWComposer& hwc);
2952965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        void setEnabled(bool enabled);
2963eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    };
2973eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
2983eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    friend class VSyncThread;
2993eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
3003eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    // for debugging ----------------------------------------------------------
30174d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian    void dump(String8& out) const;
3028372785879d329f592f6883620b5a32d80d74691Mathias Agopian
303a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianprivate:
304b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    void loadHwcModule();
305ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall    int loadFbHalModule();
306b685c542836b93c99cd85053e07696406ea37adbJesse Hall
3071e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    LayerListIterator getLayerIterator(int32_t id, size_t index);
30831d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
3093e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    struct cb_context;
31031d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
311bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    static void hook_invalidate(const struct hwc_procs* procs);
3121bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    static void hook_vsync(const struct hwc_procs* procs, int disp,
313bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            int64_t timestamp);
3141bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    static void hook_hotplug(const struct hwc_procs* procs, int disp,
3151bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            int connected);
31631d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
3173eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    inline void invalidate();
3181bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    inline void vsync(int disp, int64_t timestamp);
3191bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    inline void hotplug(int disp, int connected);
320c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
3211604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    status_t queryDisplayProperties(int disp);
3221e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
323da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    status_t setFramebufferTarget(int32_t id,
324da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
325da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
326e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    struct DisplayData {
327a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall        DisplayData();
328a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall        ~DisplayData();
3297f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        Vector<DisplayConfig> configs;
3307f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        size_t currentConfig;
331b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        uint32_t format;    // pixel format from FB hal, for pre-hwc-1.1
332f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian        bool connected;
333e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        bool hasFbComp;
334e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        bool hasOvComp;
335f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        size_t capacity;
336f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        hwc_display_contents_1* list;
337da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        hwc_layer_1* framebufferTarget;
338da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        buffer_handle_t fbTargetHandle;
3392ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis        sp<Fence> lastRetireFence;  // signals when the last set op retires
3402ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis        sp<Fence> lastDisplayFence; // signals when the last set op takes
3412ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis                                    // effect on screen
342851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        buffer_handle_t outbufHandle;
343851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        sp<Fence> outbufAcquireFence;
3442ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis
34581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // protected by mEventControlLock
34681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        int32_t events;
347e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    };
348e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
3495880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    sp<SurfaceFlinger>              mFlinger;
350b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    framebuffer_device_t*           mFbDev;
3515880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    struct hwc_composer_device_1*   mHwc;
352b685c542836b93c99cd85053e07696406ea37adbJesse Hall    // invariant: mLists[0] != NULL iff mHwc != NULL
353e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    // mLists[i>0] can be NULL. that display is to be ignored
3549e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    struct hwc_display_contents_1*  mLists[MAX_HWC_DISPLAYS];
3559e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    DisplayData                     mDisplayData[MAX_HWC_DISPLAYS];
3568f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall    size_t                          mNumDisplays;
3571e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
3585880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    cb_context*                     mCBContext;
3595880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    EventHandler&                   mEventHandler;
360bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    size_t                          mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
3615880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    sp<VSyncThread>                 mVSyncThread;
3625880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    bool                            mDebugForceFakeVSync;
363f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    BitSet32                        mAllocatedDisplayIDs;
364d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
365d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // protected by mLock
366d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    mutable Mutex mLock;
367bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    mutable nsecs_t mLastHwVSync[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
36881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian
36981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    // thread-safe
37081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    mutable Mutex mEventControlLock;
371a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian};
372a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
373a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
374a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
375a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
376a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#endif // ANDROID_SF_HWCOMPOSER_H
377