1edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
2edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
3edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
4edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * you may not use this file except in compliance with the License.
6edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * You may obtain a copy of the License at
7edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
8edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
10edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * See the License for the specific language governing permissions and
14edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * limitations under the License.
15edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
16edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
17edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#ifndef ANDROID_SURFACE_FLINGER_H
18edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#define ANDROID_SURFACE_FLINGER_H
19edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
2087670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar#include <memory>
21edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <stdint.h>
22edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <sys/types.h>
23edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
24921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <EGL/egl.h>
253f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian
263f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian/*
273f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * NOTE: Make sure this file doesn't include  anything from <gl/ > or <gl2/ >
283f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian */
29921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
3099ed22412db547c59d3da08114d9d5a586442b30Glenn Kasten#include <cutils/compiler.h>
3199ed22412db547c59d3da08114d9d5a586442b30Glenn Kasten
32edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/Atomic.h>
33edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/Errors.h>
3499b49840d309727678b77403d6cc9f920111623fMathias Agopian#include <utils/KeyedVector.h>
35076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <utils/RefBase.h>
3699b49840d309727678b77403d6cc9f920111623fMathias Agopian#include <utils/SortedVector.h>
3799b49840d309727678b77403d6cc9f920111623fMathias Agopian#include <utils/threads.h>
38edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
393d4039d7a291cd9b6f2dd4b46fcdb576f2db3356Brian Anderson#include <ui/FenceTime.h>
40edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <ui/PixelFormat.h>
411d77b719d51a01cbd6954a048fb64e79d50a950eMathias Agopian#include <math/mat4.h>
42921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
430a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson#include <gui/FrameTimestamps.h>
4490ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include <gui/ISurfaceComposer.h>
4590ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include <gui/ISurfaceComposerClient.h>
46e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza#include <gui/OccupancyTracker.h>
47edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
488630320433bd15aca239522e54e711ef6372ab07Mathias Agopian#include <hardware/hwcomposer_defs.h>
498630320433bd15aca239522e54e711ef6372ab07Mathias Agopian
5028f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright#include <system/graphics.h>
5128f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright
52921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <private/gui/LayerState.h>
53edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
54921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include "Barrier.h"
5592a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian#include "DisplayDevice.h"
56faf77cce9d9ec0238d6999b3bd0d40c71ff403c5Jamie Gennis#include "DispSync.h"
574b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis#include "FrameTracker.h"
582047fae0cfed99c425dc7333f31d309e5b8ee1baRobert Carr#include "LayerVector.h"
594b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis#include "MessageQueue.h"
60468051e20be19130572231266db306396a56402bIrvel#include "SurfaceInterceptor.h"
61b254fa3a9eccd5ad7d853d687cf50a68dd8ee41cWei Wang#include "StartBootAnimThread.h"
62f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
638630320433bd15aca239522e54e711ef6372ab07Mathias Agopian#include "DisplayHardware/HWComposer.h"
64ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian#include "Effects/Daltonizer.h"
658630320433bd15aca239522e54e711ef6372ab07Mathias Agopian
66e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza#include <map>
670a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson#include <mutex>
680a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson#include <queue>
69e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza#include <string>
700a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson#include <utility>
71e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza
72edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectnamespace android {
73edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
74edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
75edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
76edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectclass Client;
77d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopianclass DisplayEventConnection;
78d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopianclass EventThread;
79edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectclass Layer;
80b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopianclass LayerDim;
81e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopianclass Surface;
82875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopianclass RenderEngine;
83d1700756ec9520c3fba22f9a14fd064a6e288810Jamie Gennisclass EventControlThread;
84c1ba5c4649554e744844b07cfe402b42fbe12ff3Sahil Dhanjuclass VSyncSource;
85c1ba5c4649554e744844b07cfe402b42fbe12ff3Sahil Dhanjuclass InjectVSyncSource;
8687670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar
8787670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaarnamespace dvr {
8887670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaarclass VrFlinger;
8987670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar} // namespace dvr
90edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
91edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
92edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
93edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectenum {
94e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    eTransactionNeeded        = 0x01,
95e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    eTraversalNeeded          = 0x02,
96e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    eDisplayTransactionNeeded = 0x04,
97e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    eTransactionMask          = 0x07
98edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project};
99edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1004f4f0943489d9113c66ac22b58cfba8c21dfa879Mathias Agopianclass SurfaceFlinger : public BnSurfaceComposer,
101921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian                       private IBinder::DeathRecipient,
1028630320433bd15aca239522e54e711ef6372ab07Mathias Agopian                       private HWComposer::EventHandler
103edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
104edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectpublic:
1050cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard
1060cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // This is the phase offset in nanoseconds of the software vsync event
1070cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // relative to the vsync event reported by HWComposer.  The software vsync
1080cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // event is when SurfaceFlinger and Choreographer-based applications run each
1090cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // frame.
1100cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    //
1110cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // This phase offset allows adjustment of the minimum latency from application
1120cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // wake-up time (by Choreographer) to the time at which the resulting window
1130cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // image is displayed.  This value may be either positive (after the HW vsync)
1140cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // or negative (before the HW vsync). Setting it to 0 will result in a lower
1150cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // latency bound of two vsync periods because the app and SurfaceFlinger
1160cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // will run just after the HW vsync.  Setting it to a positive number will
1170cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // result in the minimum latency being:
1180cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    //
1190cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    //     (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
1200cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    //
1210cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // Note that reducing this latency makes it more likely for the applications
1220cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // to not have their window content image ready in time.  When this happens
1230cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // the latency will end up being an additional vsync period, and animations
1240cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // will hiccup.  Therefore, this latency should be tuned somewhat
1250cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    // conservatively (or at least with awareness of the trade-off being made).
1260cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    static int64_t vsyncPhaseOffsetNs;
1270cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard    static int64_t sfVsyncPhaseOffsetNs;
1280cc1938871edd6659d6783404a3523abc6b98d92Fabien Sanglard
129cbf153bedf2eafc1443bbc97c4e74f97e7973eddFabien Sanglard    // If fences from sync Framework are supported.
130cbf153bedf2eafc1443bbc97c4e74f97e7973eddFabien Sanglard    static bool hasSyncFramework;
131cbf153bedf2eafc1443bbc97c4e74f97e7973eddFabien Sanglard
132c93afd54a05497c4ae42db99ea0310ee69cca492Fabien Sanglard    // Instruct the Render Engine to use EGL_IMG_context_priority is available.
133c93afd54a05497c4ae42db99ea0310ee69cca492Fabien Sanglard    static bool useContextPriority;
134c93afd54a05497c4ae42db99ea0310ee69cca492Fabien Sanglard
135c45a7d9dfdefa07512c5acc07bcbee5362b34e3dFabien Sanglard    // The offset in nanoseconds to use when DispSync timestamps present fence
136c45a7d9dfdefa07512c5acc07bcbee5362b34e3dFabien Sanglard    // signaling time.
137c45a7d9dfdefa07512c5acc07bcbee5362b34e3dFabien Sanglard    static int64_t dispSyncPresentTimeOffset;
138c45a7d9dfdefa07512c5acc07bcbee5362b34e3dFabien Sanglard
139a34ed639c3057b99da0fb703beb12827e30aa508Fabien Sanglard    // Some hardware can do RGB->YUV conversion more efficiently in hardware
140a34ed639c3057b99da0fb703beb12827e30aa508Fabien Sanglard    // controlled by HWC than in hardware controlled by the video encoder.
141a34ed639c3057b99da0fb703beb12827e30aa508Fabien Sanglard    // This instruct VirtualDisplaySurface to use HWC for such conversion on
142a34ed639c3057b99da0fb703beb12827e30aa508Fabien Sanglard    // GL composition.
143a34ed639c3057b99da0fb703beb12827e30aa508Fabien Sanglard    static bool useHwcForRgbToYuv;
144a34ed639c3057b99da0fb703beb12827e30aa508Fabien Sanglard
145c8e387edfcead55b6e6fb1d05db279c264b644faFabien Sanglard    // Maximum dimension supported by HWC for virtual display.
146c8e387edfcead55b6e6fb1d05db279c264b644faFabien Sanglard    // Equal to min(max_height, max_width).
147c8e387edfcead55b6e6fb1d05db279c264b644faFabien Sanglard    static uint64_t maxVirtualDisplaySize;
148c8e387edfcead55b6e6fb1d05db279c264b644faFabien Sanglard
1491971b63aa4d82db37794f19e0eb01feb1826e422Fabien Sanglard    // Controls the number of buffers SurfaceFlinger will allocate for use in
1501971b63aa4d82db37794f19e0eb01feb1826e422Fabien Sanglard    // FramebufferSurface
1511971b63aa4d82db37794f19e0eb01feb1826e422Fabien Sanglard    static int64_t maxFrameBufferAcquiredBuffers;
1521971b63aa4d82db37794f19e0eb01feb1826e422Fabien Sanglard
1535d94389241cc651e6bd327ab80eba3ad476f3724Courtney Goeltzenleuchter    // Indicate if platform supports color management on its
1545d94389241cc651e6bd327ab80eba3ad476f3724Courtney Goeltzenleuchter    // wide-color display. This is typically found on devices
1555d94389241cc651e6bd327ab80eba3ad476f3724Courtney Goeltzenleuchter    // with wide gamut (e.g. Display-P3) display.
1565d94389241cc651e6bd327ab80eba3ad476f3724Courtney Goeltzenleuchter    // This also allows devices with wide-color displays that don't
1575d94389241cc651e6bd327ab80eba3ad476f3724Courtney Goeltzenleuchter    // want to support color management to disable color management.
1585d94389241cc651e6bd327ab80eba3ad476f3724Courtney Goeltzenleuchter    static bool hasWideColorDisplay;
1595d94389241cc651e6bd327ab80eba3ad476f3724Courtney Goeltzenleuchter
160b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian    static char const* getServiceName() ANDROID_API {
161921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian        return "SurfaceFlinger";
162921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    }
163118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian
164b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian    SurfaceFlinger() ANDROID_API;
16599ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
1664f4f0943489d9113c66ac22b58cfba8c21dfa879Mathias Agopian    // must be called before clients can connect
1674f4f0943489d9113c66ac22b58cfba8c21dfa879Mathias Agopian    void init() ANDROID_API;
1684f4f0943489d9113c66ac22b58cfba8c21dfa879Mathias Agopian
1694f4f0943489d9113c66ac22b58cfba8c21dfa879Mathias Agopian    // starts SurfaceFlinger main loop in the current thread
1704f4f0943489d9113c66ac22b58cfba8c21dfa879Mathias Agopian    void run() ANDROID_API;
1714f4f0943489d9113c66ac22b58cfba8c21dfa879Mathias Agopian
1728630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    enum {
1738630320433bd15aca239522e54e711ef6372ab07Mathias Agopian        EVENT_VSYNC = HWC_EVENT_VSYNC
1748630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    };
1758630320433bd15aca239522e54e711ef6372ab07Mathias Agopian
176921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // post an asynchronous message to the main thread
1773f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
178118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian
179921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // post a synchronous message to the main thread
1803f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
181118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian
182921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // force full composition on all displays
183921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    void repaintEverything();
184b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian
185921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // returns the default Display
1868722a310c00557195a0703e95564f31d908ab2d5Tomasz Wasilczyk    sp<const DisplayDevice> getDefaultDisplayDevice() const {
1877d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk        Mutex::Autolock _l(mStateLock);
1887d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk        return getDefaultDisplayDeviceLocked();
189921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    }
190118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian
1918630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    // utility function to delete a texture on the main thread
1923f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    void deleteTextureAsync(uint32_t texture);
1938630320433bd15aca239522e54e711ef6372ab07Mathias Agopian
1948630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    // enable/disable h/w composer event
1958630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    // TODO: this should be made accessible only to EventThread
1969d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#ifdef USE_HWC2
1979e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    void setVsyncEnabled(int disp, int enabled);
1989d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#else
1999d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard    void eventControl(int disp, int event, int enabled);
2009d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#endif
2018630320433bd15aca239522e54e711ef6372ab07Mathias Agopian
202921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // called on the main thread by MessageQueue when an internal message
203921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // is received
204921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // TODO: this should be made accessible only to MessageQueue
205921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    void onMessageReceived(int32_t what);
206118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian
207cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian    // for debugging only
208cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian    // TODO: this should be made accessible only to HWComposer
20948bc05b56df9919fc39c5f2e3ea6535560eec98fJesse Hall    const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id);
210cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
211875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian    RenderEngine& getRenderEngine() const {
212875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian        return *mRenderEngine;
21329c3f352797d9d2ddf055d8f888e7694ef8b3947Jesse Hall    }
21429c3f352797d9d2ddf055d8f888e7694ef8b3947Jesse Hall
2150d48072f6047140119ff194c1194ce402fca2c0bRobert Carr    bool authenticateSurfaceTextureLocked(
2160d48072f6047140119ff194c1194ce402fca2c0bRobert Carr        const sp<IGraphicBufferProducer>& bufferProducer) const;
2170d48072f6047140119ff194c1194ce402fca2c0bRobert Carr
2181f339ff3875afad128a8e16ee6395c5fad295826Mathias Agopianprivate:
21996f0819f81293076e652792794a961543e6750d7Mathias Agopian    friend class Client;
220d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    friend class DisplayEventConnection;
221468051e20be19130572231266db306396a56402bIrvel    friend class EventThread;
222edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    friend class Layer;
223b9b088375d33a87b201cdbe18be71802e2607717Dan Stoza    friend class MonitoredProducer;
224edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
2256547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis    // This value is specified in number of frames.  Log frame stats at most
2266547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis    // every half hour.
2276547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis    enum { LOG_FRAME_STATS_PERIOD =  30*60*60 };
2286547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis
2297d89d06a6fe1bfadfe277f19dbb7e4aa021444e0Dan Stoza    static const size_t MAX_LAYERS = 4096;
2305c34de2c12ab2638fd6b16f1b77dedd0ffa3ab27Ian Elliott    static constexpr const char* kTimestampProperty = "service.sf.present_timestamp";
2317d89d06a6fe1bfadfe277f19dbb7e4aa021444e0Dan Stoza
232921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // We're reference counted, never destroy SurfaceFlinger directly
233921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    virtual ~SurfaceFlinger();
234edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
235921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
236921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     * Internal data structures
237921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
238118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian
2392047fae0cfed99c425dc7333f31d309e5b8ee1baRobert Carr    class State {
240edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    public:
241412903fce3a93f411c85c54375a1851bfb370400Dan Stoza        explicit State(LayerVector::StateSet set) : stateSet(set) {}
242412903fce3a93f411c85c54375a1851bfb370400Dan Stoza        State& operator=(const State& other) {
243412903fce3a93f411c85c54375a1851bfb370400Dan Stoza            // We explicitly don't copy stateSet so that, e.g., mDrawingState
244412903fce3a93f411c85c54375a1851bfb370400Dan Stoza            // always uses the Drawing StateSet.
245412903fce3a93f411c85c54375a1851bfb370400Dan Stoza            layersSortedByZ = other.layersSortedByZ;
246412903fce3a93f411c85c54375a1851bfb370400Dan Stoza            displays = other.displays;
247412903fce3a93f411c85c54375a1851bfb370400Dan Stoza            return *this;
248412903fce3a93f411c85c54375a1851bfb370400Dan Stoza        }
249412903fce3a93f411c85c54375a1851bfb370400Dan Stoza
250412903fce3a93f411c85c54375a1851bfb370400Dan Stoza        const LayerVector::StateSet stateSet = LayerVector::StateSet::Invalid;
251921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian        LayerVector layersSortedByZ;
252e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
2532047fae0cfed99c425dc7333f31d309e5b8ee1baRobert Carr
254412903fce3a93f411c85c54375a1851bfb370400Dan Stoza        void traverseInZOrder(const LayerVector::Visitor& visitor) const;
255412903fce3a93f411c85c54375a1851bfb370400Dan Stoza        void traverseInReverseZOrder(const LayerVector::Visitor& visitor) const;
256edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    };
257edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
258921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
259921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     * IBinder interface
260921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
261921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    virtual status_t onTransact(uint32_t code, const Parcel& data,
262921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian        Parcel* reply, uint32_t flags);
263921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    virtual status_t dump(int fd, const Vector<String16>& args);
264edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
265921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
266921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     * ISurfaceComposer interface
267921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
268921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    virtual sp<ISurfaceComposerClient> createConnection();
2691db73f66624e7d151710483dd58e03eed672f064Robert Carr    virtual sp<ISurfaceComposerClient> createScopedConnection(const sp<IGraphicBufferProducer>& gbp);
270dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis    virtual sp<IBinder> createDisplay(const String8& displayName, bool secure);
2716c913be9ca95fd6b556d056e165a4ba6dc69795bJesse Hall    virtual void destroyDisplay(const sp<IBinder>& display);
272e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    virtual sp<IBinder> getBuiltInDisplay(int32_t id);
273921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    virtual void setTransactionState(const Vector<ComposerState>& state,
2748b33f032327f8de0dcc0e6d0d43ed80f834b51f6Mathias Agopian            const Vector<DisplayState>& displays, uint32_t flags);
27592a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian    virtual void bootFinished();
276921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    virtual bool authenticateSurfaceTexture(
2772adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden        const sp<IGraphicBufferProducer>& bufferProducer) const;
2786b376713907086c9642e7b7e66e51ddfa531b003Brian Anderson    virtual status_t getSupportedFrameTimestamps(
2796b376713907086c9642e7b7e66e51ddfa531b003Brian Anderson            std::vector<FrameEvent>* outSupported) const;
280b1e2f8deb38353e4bcc9d3ef06bc15bd5e417425Jorim Jaggi    virtual sp<IDisplayEventConnection> createDisplayEventConnection(
281b1e2f8deb38353e4bcc9d3ef06bc15bd5e417425Jorim Jaggi            ISurfaceComposer::VsyncSource vsyncSource = eVsyncSourceApp);
2822a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    virtual status_t captureScreen(const sp<IBinder>& display,
2832a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            const sp<IGraphicBufferProducer>& producer,
284c18790018be5d7ea7061ccbc81f3044e74adc823Dan Stoza            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
285ae0608381b2b4699218febd6d45ad9d307544d55Robert Carr            int32_t minLayerZ, int32_t maxLayerZ,
286c3ebe66b49cfba035e1fd0e160a13db38eb81b0eRiley Andrews            bool useIdentityTransform, ISurfaceComposer::Rotation rotation);
28767d8bd66aaf04805cb8f2616ba964141b865e3b9Lajos Molnar    virtual status_t getDisplayStats(const sp<IBinder>& display,
28867d8bd66aaf04805cb8f2616ba964141b865e3b9Lajos Molnar            DisplayStatInfo* stats);
2897f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    virtual status_t getDisplayConfigs(const sp<IBinder>& display,
2907f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            Vector<DisplayInfo>* configs);
2917f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    virtual int getActiveConfig(const sp<IBinder>& display);
29228f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright    virtual status_t getDisplayColorModes(const sp<IBinder>& display,
29328f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright            Vector<android_color_mode_t>* configs);
29428f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright    virtual android_color_mode_t getActiveColorMode(const sp<IBinder>& display);
29528f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright    virtual status_t setActiveColorMode(const sp<IBinder>& display, android_color_mode_t colorMode);
2962c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani    virtual void setPowerMode(const sp<IBinder>& display, int mode);
2977f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    virtual status_t setActiveConfig(const sp<IBinder>& display, int id);
298d85084b2b65828442eafaff9b811e9b6c9ca9fadSvetoslav    virtual status_t clearAnimationFrameStats();
299d85084b2b65828442eafaff9b811e9b6c9ca9fadSvetoslav    virtual status_t getAnimationFrameStats(FrameStats* outStats) const;
300c4f471e75a8ec64ec34e3f2944a5a756215d0becDan Stoza    virtual status_t getHdrCapabilities(const sp<IBinder>& display,
301c4f471e75a8ec64ec34e3f2944a5a756215d0becDan Stoza            HdrCapabilities* outCapabilities) const;
302c1ba5c4649554e744844b07cfe402b42fbe12ff3Sahil Dhanju    virtual status_t enableVSyncInjections(bool enable);
303c1ba5c4649554e744844b07cfe402b42fbe12ff3Sahil Dhanju    virtual status_t injectVSync(nsecs_t when);
304c1ba5c4649554e744844b07cfe402b42fbe12ff3Sahil Dhanju
3051b03149f3533db04e72e088d3fdd09d0087ca594Mathias Agopian
306921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
307921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     * DeathRecipient interface
308921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
309921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    virtual void binderDied(const wp<IBinder>& who);
31099ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
311921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
3124f4f0943489d9113c66ac22b58cfba8c21dfa879Mathias Agopian     * RefBase interface
313921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
314921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    virtual void onFirstRef();
315921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
316921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
3178630320433bd15aca239522e54e711ef6372ab07Mathias Agopian     * HWComposer::EventHandler interface
3188630320433bd15aca239522e54e711ef6372ab07Mathias Agopian     */
3193cfac28462910d3f976aebac54ac7301aca7e434Steven Thomas    virtual void onVSyncReceived(HWComposer* composer, int type, nsecs_t timestamp);
3207d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk    virtual void onHotplugReceived(HWComposer* composer, int disp, bool connected);
3213cfac28462910d3f976aebac54ac7301aca7e434Steven Thomas    virtual void onInvalidateReceived(HWComposer* composer);
3228630320433bd15aca239522e54e711ef6372ab07Mathias Agopian
3238630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    /* ------------------------------------------------------------------------
324921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     * Message handling
325921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
326921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    void waitForEvent();
327921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    void signalTransaction();
328921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    void signalLayerUpdate();
329921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    void signalRefresh();
330921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
33113a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    // called on the main thread in response to initializeDisplays()
33213a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    void onInitializeDisplays();
3336c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    // called on the main thread in response to setActiveConfig()
3346c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    void setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode);
3352c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani    // called on the main thread in response to setPowerMode()
3362c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani    void setPowerModeInternal(const sp<DisplayDevice>& hw, int mode);
337921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
33828f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright    // Called on the main thread in response to setActiveColorMode()
33928f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright    void setActiveColorModeInternal(const sp<DisplayDevice>& hw, android_color_mode_t colorMode);
34028f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright
3416b9454d1fee0347711af1746642aa7820b1ea04dDan Stoza    // Returns whether the transaction actually modified any state
3426b9454d1fee0347711af1746642aa7820b1ea04dDan Stoza    bool handleMessageTransaction();
3436b9454d1fee0347711af1746642aa7820b1ea04dDan Stoza
3446b9454d1fee0347711af1746642aa7820b1ea04dDan Stoza    // Returns whether a new buffer has been latched (see handlePageFlip())
3456b9454d1fee0347711af1746642aa7820b1ea04dDan Stoza    bool handleMessageInvalidate();
3466b9454d1fee0347711af1746642aa7820b1ea04dDan Stoza
347921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    void handleMessageRefresh();
348921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
34987baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    void handleTransaction(uint32_t transactionFlags);
35087baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    void handleTransactionLocked(uint32_t transactionFlags);
351921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
35203414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    void updateCursorAsync();
35303414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews
3546b9454d1fee0347711af1746642aa7820b1ea04dDan Stoza    /* handlePageFlip - latch a new buffer if available and compute the dirty
3556b9454d1fee0347711af1746642aa7820b1ea04dDan Stoza     * region. Returns whether a new buffer has been latched, i.e., whether it
3566b9454d1fee0347711af1746642aa7820b1ea04dDan Stoza     * is necessary to perform a refresh during this vsync.
357921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
3586b9454d1fee0347711af1746642aa7820b1ea04dDan Stoza    bool handlePageFlip();
359921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
360921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
361921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     * Transactions
362921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
363921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    uint32_t getTransactionFlags(uint32_t flags);
364c8251eb7a6ecfdd16b3e4cfbfb442aa4c789c039Fabien Sanglard    uint32_t peekTransactionFlags();
365921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    uint32_t setTransactionFlags(uint32_t flags);
366921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    void commitTransaction();
3673f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s);
368e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    uint32_t setDisplayStateLocked(const DisplayState& s);
369921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
370921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
371921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     * Layer management
372921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
3734d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    status_t createLayer(const String8& name, const sp<Client>& client,
3744d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
375479c60c85c40fd3536b0c88036e838dc1a4c56a0Albert Chaulk            uint32_t windowType, uint32_t ownerUid, sp<IBinder>* handle,
376479c60c85c40fd3536b0c88036e838dc1a4c56a0Albert Chaulk            sp<IGraphicBufferProducer>* gbp, sp<Layer>* parent);
3774d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
3784d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    status_t createNormalLayer(const sp<Client>& client, const String8& name,
3794d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
3804d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp,
3814d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            sp<Layer>* outLayer);
3824d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
3834d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    status_t createDimLayer(const sp<Client>& client, const String8& name,
3844d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
3854d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
386921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
387bc7552874052ee33f1b35b4474e20c003d216391Cody Northrop    String8 getUniqueLayerName(const String8& name);
388bc7552874052ee33f1b35b4474e20c003d216391Cody Northrop
389921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // called in response to the window-manager calling
390921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // ISurfaceComposerClient::destroySurface()
391ac9fa427d4a86745e60a5f7fd8e3ea340c4db907Mathias Agopian    status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
392921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
393921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // called when all clients have released all their references to
394921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // this layer meaning it is entirely safe to destroy all
395921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // resources associated to this layer.
39613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    status_t onLayerDestroyed(const wp<Layer>& layer);
397921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
398921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // remove a layer from SurfaceFlinger immediately
399515dc9c538b8206b746eeb4906ac0b8aed1fb497Chia-I Wu    status_t removeLayer(const sp<Layer>& layer, bool topLevelOnly = false);
400edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
401921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // add a layer to SurfaceFlinger
4027d89d06a6fe1bfadfe277f19dbb7e4aa021444e0Dan Stoza    status_t addClientLayer(const sp<Client>& client,
4036710604286401d4205c27235a252dd0e5008cc08Mathias Agopian            const sp<IBinder>& handle,
4046710604286401d4205c27235a252dd0e5008cc08Mathias Agopian            const sp<IGraphicBufferProducer>& gbc,
4051f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr            const sp<Layer>& lbc,
4061f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr            const sp<Layer>& parent);
407921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
408921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
409921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     * Boot animation, on/off animations and screen capture
410921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
411921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
412921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    void startBootAnim();
413921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
414180f10de6f504d2ba56ff32ae8ed53c58bb458e9Mathias Agopian    void renderScreenImplLocked(
415180f10de6f504d2ba56ff32ae8ed53c58bb458e9Mathias Agopian            const sp<const DisplayDevice>& hw,
416c18790018be5d7ea7061ccbc81f3044e74adc823Dan Stoza            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
417ae0608381b2b4699218febd6d45ad9d307544d55Robert Carr            int32_t minLayerZ, int32_t maxLayerZ,
418c3ebe66b49cfba035e1fd0e160a13db38eb81b0eRiley Andrews            bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation);
419180f10de6f504d2ba56ff32ae8ed53c58bb458e9Mathias Agopian
4202a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    status_t captureScreenImplLocked(
4212a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            const sp<const DisplayDevice>& hw,
422abe815dd6978b718c04f6e22e1a893d2b51d11a1Mathias Agopian            const sp<IGraphicBufferProducer>& producer,
423c18790018be5d7ea7061ccbc81f3044e74adc823Dan Stoza            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
424ae0608381b2b4699218febd6d45ad9d307544d55Robert Carr            int32_t minLayerZ, int32_t maxLayerZ,
425b5b3563058c178811d434ab6e8c4ead4a519701bPablo Ceballos            bool useIdentityTransform, Transform::orientation_flags rotation,
426b5b3563058c178811d434ab6e8c4ead4a519701bPablo Ceballos            bool isLocalScreenshot);
427abe815dd6978b718c04f6e22e1a893d2b51d11a1Mathias Agopian
428b254fa3a9eccd5ad7d853d687cf50a68dd8ee41cWei Wang    sp<StartBootAnimThread> mStartBootAnimThread = nullptr;
429b254fa3a9eccd5ad7d853d687cf50a68dd8ee41cWei Wang
430921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
431921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     * EGL
432921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
433875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian    size_t getMaxTextureSize() const;
434875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian    size_t getMaxViewportDims() const;
435921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
436921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
43787baae104a3e4c2059990b01c393476065c558b0Mathias Agopian     * Display and layer stack management
438921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
43913a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    // called when starting, or restarting after system_server death
44013a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    void initializeDisplays();
44113a082e160c2d1d8006b93a555a57035213d568bAndy McFadden
442692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall    // Create an IBinder for a builtin display and add it to current state
443692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall    void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
444692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall
4457d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk
4463ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian    sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
4477d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk      Mutex::Autolock _l(mStateLock);
4487d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk      return getDisplayDeviceLocked(dpy);
4497d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk    }
4507d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk
4517d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk    sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
4527d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk      Mutex::Autolock _l(mStateLock);
4537d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk      return getDisplayDeviceLocked(dpy);
4547d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk    }
4557d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk
4567d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk    // NOTE: can only be called from the main thread or with mStateLock held
4577d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk    sp<const DisplayDevice> getDisplayDeviceLocked(const wp<IBinder>& dpy) const {
45892a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian        return mDisplays.valueFor(dpy);
45992a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian    }
460db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian
461db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    // NOTE: can only be called from the main thread or with mStateLock held
4627d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk    sp<DisplayDevice> getDisplayDeviceLocked(const wp<IBinder>& dpy) {
4634297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian        return mDisplays.valueFor(dpy);
464921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    }
465921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
4667d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk    sp<const DisplayDevice> getDefaultDisplayDeviceLocked() const {
4677d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk        return getDisplayDeviceLocked(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]);
4687d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk    }
4697d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk
4707d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk    void createDefaultDisplayDevice();
4717d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk
47228f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright    int32_t getDisplayType(const sp<IBinder>& display) {
47328f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright        if (!display.get()) return NAME_NOT_FOUND;
47428f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright        for (int i = 0; i < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES; ++i) {
47528f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright            if (display == mBuiltinDisplays[i]) {
47628f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright                return i;
47728f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright            }
47828f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright        }
47928f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright        return NAME_NOT_FOUND;
48028f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright    }
48128f24d0ab481bd9c6fd5618414fee694e837c5c6Michael Wright
48287baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    // mark a region of a layer stack dirty. this updates the dirty
48387baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    // region of all screens presenting this layer stack.
48487baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
48587baae104a3e4c2059990b01c393476065c558b0Mathias Agopian
4869d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#ifndef USE_HWC2
4879d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard    int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
4889d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#endif
4899d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard
490921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
4918630320433bd15aca239522e54e711ef6372ab07Mathias Agopian     * H/W composer
4928630320433bd15aca239522e54e711ef6372ab07Mathias Agopian     */
4938630320433bd15aca239522e54e711ef6372ab07Mathias Agopian
4948630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    HWComposer& getHwComposer() const { return *mHwc; }
4958630320433bd15aca239522e54e711ef6372ab07Mathias Agopian
496888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    /* ------------------------------------------------------------------------
497921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     * Compositing
498921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
499921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    void invalidateHwcGeometry();
5002047fae0cfed99c425dc7333f31d309e5b8ee1baRobert Carr    void computeVisibleRegions(uint32_t layerStack,
50187baae104a3e4c2059990b01c393476065c558b0Mathias Agopian            Region& dirtyRegion, Region& opaqueRegion);
502cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian
503d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson    void preComposition(nsecs_t refreshStartTime);
5040a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    void postComposition(nsecs_t refreshStartTime);
5050a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    void updateCompositorTiming(
5060a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson            nsecs_t vsyncPhase, nsecs_t vsyncInterval, nsecs_t compositeTime,
5070a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson            std::shared_ptr<FenceTime>& presentFenceTime);
508d001058145c2186f454a3fb043388d6d9b84c9d8Brian Anderson    void setCompositorTimingSnapped(
509d001058145c2186f454a3fb043388d6d9b84c9d8Brian Anderson            nsecs_t vsyncPhase, nsecs_t vsyncInterval,
510d001058145c2186f454a3fb043388d6d9b84c9d8Brian Anderson            nsecs_t compositeToPresentLatency);
511cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    void rebuildLayerStacks();
5125d94389241cc651e6bd327ab80eba3ad476f3724Courtney Goeltzenleuchter
5135d94389241cc651e6bd327ab80eba3ad476f3724Courtney Goeltzenleuchter    // Given a dataSpace, returns the appropriate color_mode to use
5145d94389241cc651e6bd327ab80eba3ad476f3724Courtney Goeltzenleuchter    // to display that dataSpace.
5155d94389241cc651e6bd327ab80eba3ad476f3724Courtney Goeltzenleuchter    android_color_mode pickColorMode(android_dataspace dataSpace);
5165d94389241cc651e6bd327ab80eba3ad476f3724Courtney Goeltzenleuchter    android_dataspace bestTargetDataSpace(android_dataspace a, android_dataspace b);
5175d94389241cc651e6bd327ab80eba3ad476f3724Courtney Goeltzenleuchter
518cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    void setUpHWComposer();
519cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    void doComposition();
520cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    void doDebugFlashRegions();
521830b84704b0f33030d0e391a0ea2e7faa5646e2eFabien Sanglard    void doDisplayComposition(const sp<const DisplayDevice>& displayDevice, const Region& dirtyRegion);
5223f121fc650d72d0103cef8e6a651093fb1589e0aMichael Lentine
5233f121fc650d72d0103cef8e6a651093fb1589e0aMichael Lentine    // compose surfaces for display hw. this fails if using GL and the surface
5243f121fc650d72d0103cef8e6a651093fb1589e0aMichael Lentine    // has been destroyed and is no longer valid.
525830b84704b0f33030d0e391a0ea2e7faa5646e2eFabien Sanglard    bool doComposeSurfaces(const sp<const DisplayDevice>& displayDevice, const Region& dirty);
526cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian
527921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    void postFramebuffer();
528830b84704b0f33030d0e391a0ea2e7faa5646e2eFabien Sanglard    void drawWormhole(const sp<const DisplayDevice>& displayDevice, const Region& region) const;
529921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
530921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
5310bceb84773882b796d9dacbaf96167cb15928d78Jamie Gennis     * Display management
5320bceb84773882b796d9dacbaf96167cb15928d78Jamie Gennis     */
5333ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian
534faf77cce9d9ec0238d6999b3bd0d40c71ff403c5Jamie Gennis    /* ------------------------------------------------------------------------
535faf77cce9d9ec0238d6999b3bd0d40c71ff403c5Jamie Gennis     * VSync
536faf77cce9d9ec0238d6999b3bd0d40c71ff403c5Jamie Gennis     */
5370a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    void enableHardwareVsync();
5380a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    void resyncToHardwareVsync(bool makeAvailable);
5390a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    void disableHardwareVsync(bool makeUnavailable);
5401f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
5414a4e4a239f034cb8af2df9a438b26c3bc088889cTim Murraypublic:
5420a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    void resyncWithRateLimit();
5430a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    void getCompositorTiming(CompositorTiming* compositorTiming);
5444a4e4a239f034cb8af2df9a438b26c3bc088889cTim Murrayprivate:
5450bceb84773882b796d9dacbaf96167cb15928d78Jamie Gennis
5460bceb84773882b796d9dacbaf96167cb15928d78Jamie Gennis    /* ------------------------------------------------------------------------
547921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     * Debugging & dumpsys
548921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
5493f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    void listLayersLocked(const Vector<String16>& args, size_t& index, String8& result) const;
5503f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    void dumpStatsLocked(const Vector<String16>& args, size_t& index, String8& result) const;
5513f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    void clearStatsLocked(const Vector<String16>& args, size_t& index, String8& result);
5523f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    void dumpAllLocked(const Vector<String16>& args, size_t& index, String8& result) const;
55363f165fd6b86d04be94d4023e845e98560504a96Keun young Park    bool startDdmConnection();
55463a5fcd2aa55c8eb0ccba621517a4215d8504df6Fabien Sanglard    void appendSfConfigString(String8& result) const;
555d555684cb36dfb959694db76962e570184f98838Mathias Agopian    void checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
556fee2b463c5fbe8fa0132d03634ccc02ea55c1505Mathias Agopian            const sp<const DisplayDevice>& hw,
557ae0608381b2b4699218febd6d45ad9d307544d55Robert Carr            int32_t minLayerZ, int32_t maxLayerZ);
558921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
5596547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis    void logFrameStats();
5606547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis
561b90cf07f8b3a8fe76e40b997924c2509d04575f4Dan Stoza    void dumpStaticScreenStats(String8& result) const;
562d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson    // Not const because each Layer needs to query Fences and cache timestamps.
563d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson    void dumpFrameEventsLocked(String8& result);
564b90cf07f8b3a8fe76e40b997924c2509d04575f4Dan Stoza
565e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza    void recordBufferingStats(const char* layerName,
566e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza            std::vector<OccupancyTracker::Segment>&& history);
567e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza    void dumpBufferingStats(String8& result) const;
568f3b2de10caa2a96e65f3e8ebecb2730ff28aeeb1Courtney Goeltzenleuchter    void dumpWideColorInfo(String8& result) const;
569e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza
57063a5fcd2aa55c8eb0ccba621517a4215d8504df6Fabien Sanglard    bool isLayerTripleBufferingDisabled() const {
57163a5fcd2aa55c8eb0ccba621517a4215d8504df6Fabien Sanglard        return this->mLayerTripleBufferingDisabled;
57263a5fcd2aa55c8eb0ccba621517a4215d8504df6Fabien Sanglard    }
57387670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar
57487670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar#ifdef USE_HWC2
57587670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar    /* ------------------------------------------------------------------------
57687670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar     * VrFlinger
57787670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar     */
57887670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar    void clearHwcLayers(const LayerVector& layers);
5797d3dcb9235ec149af2f5139a8a9c6eac859d92e2Stephen Kiazyk    void resetHwcLocked();
58087670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar
581050b2c83304bd16ec3a838da08b6ba6acf6a3af4Steven Thomas    // Check to see if we should handoff to vr flinger.
582050b2c83304bd16ec3a838da08b6ba6acf6a3af4Steven Thomas    void updateVrFlinger();
58387670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar#endif
58487670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar
585921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
586921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     * Attributes
587921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
588921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
589921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // access must be protected by mStateLock
590921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    mutable Mutex mStateLock;
591412903fce3a93f411c85c54375a1851bfb370400Dan Stoza    State mCurrentState{LayerVector::StateSet::Current};
592921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    volatile int32_t mTransactionFlags;
593921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    Condition mTransactionCV;
5942d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis    bool mTransactionPending;
5952d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis    bool mAnimTransactionPending;
5961f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    SortedVector< sp<Layer> > mLayersPendingRemoval;
5976710604286401d4205c27235a252dd0e5008cc08Mathias Agopian    SortedVector< wp<IBinder> > mGraphicBufferProducerList;
598921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
599921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // protected by mStateLock (but we could use another lock)
600921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    bool mLayersRemoved;
6011f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    bool mLayersAdded;
602921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
603921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // access must be protected by mInvalidateLock
60487baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    volatile int32_t mRepaintEverything;
605921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
60687670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar    // current, real and vr hardware composers.
6078630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    HWComposer* mHwc;
60887670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar#ifdef USE_HWC2
60987670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar    HWComposer* mRealHwc;
61087670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar    HWComposer* mVrHwc;
61187670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar#endif
61287670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar    // constant members (no synchronization needed for access)
613875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian    RenderEngine* mRenderEngine;
614921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    nsecs_t mBootTime;
615b4b1730abb7824dc084468c4942f010d94a7e039Mathias Agopian    bool mGpuToCpuSupported;
616921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    sp<EventThread> mEventThread;
6170a645cc5a935e67a8d1effc7679a838160b971d8Jamie Gennis    sp<EventThread> mSFEventThread;
618c1ba5c4649554e744844b07cfe402b42fbe12ff3Sahil Dhanju    sp<EventThread> mInjectorEventThread;
619c1ba5c4649554e744844b07cfe402b42fbe12ff3Sahil Dhanju    sp<InjectVSyncSource> mVSyncInjector;
620d1700756ec9520c3fba22f9a14fd064a6e288810Jamie Gennis    sp<EventControlThread> mEventControlThread;
621921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    EGLContext mEGLContext;
62234a09ba1efd706323a15633da5044b352988eb5fJesse Hall    EGLDisplay mEGLDisplay;
6239e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
624921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
62587670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar#ifdef USE_HWC2
62687670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar    std::unique_ptr<dvr::VrFlinger> mVrFlinger;
62787670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar#endif
62887670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar
629921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // Can only accessed from the main thread, these members
630921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // don't need synchronization
631412903fce3a93f411c85c54375a1851bfb370400Dan Stoza    State mDrawingState{LayerVector::StateSet::Drawing};
632921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    bool mVisibleRegionsDirty;
6339d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#ifndef USE_HWC2
6349d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard    bool mHwWorkListDirty;
6359d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#else
6369e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    bool mGeometryInvalid;
6379d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#endif
6384b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    bool mAnimCompositionPending;
6399d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#ifdef USE_HWC2
6409e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    std::vector<sp<Layer>> mLayersWithQueuedFrames;
64105dacfb68af895fce3cc8ebb0b4aa06c6c336e26Dan Stoza    sp<Fence> mPreviousPresentFence = Fence::NO_FENCE;
64205dacfb68af895fce3cc8ebb0b4aa06c6c336e26Dan Stoza    bool mHadClientComposition = false;
6439d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#endif
6443d4039d7a291cd9b6f2dd4b46fcdb576f2db3356Brian Anderson    FenceTimeline mGlCompositionDoneTimeline;
6453d4039d7a291cd9b6f2dd4b46fcdb576f2db3356Brian Anderson    FenceTimeline mDisplayTimeline;
646db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian
647db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    // this may only be written from the main thread with mStateLock held
648db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    // it may be read from other threads with mStateLock held
6493ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian    DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
650921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
651921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // don't use a lock for these, we don't care
652921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    int mDebugRegion;
653921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    int mDebugDDMS;
654921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    int mDebugDisableHWC;
655921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    int mDebugDisableTransformHint;
656921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    volatile nsecs_t mDebugInSwapBuffers;
657921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    nsecs_t mLastSwapBufferTime;
658921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    volatile nsecs_t mDebugInTransaction;
659921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    nsecs_t mLastTransactionTime;
660921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    bool mBootFinished;
661ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza    bool mForceFullDamage;
6629d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#ifdef USE_HWC2
663c5da271eec001da9e11a2786f2618a45257439c3Dan Stoza    bool mPropagateBackpressure = true;
6649d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#endif
665468051e20be19130572231266db306396a56402bIrvel    SurfaceInterceptor mInterceptor;
666642b23d70f7b513e88680c1d8400c1c1cfe6edd3Fabien Sanglard    bool mUseHwcVirtualDisplays = false;
667921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
66863a5fcd2aa55c8eb0ccba621517a4215d8504df6Fabien Sanglard    // Restrict layers to use two buffers in their bufferqueues.
66963a5fcd2aa55c8eb0ccba621517a4215d8504df6Fabien Sanglard    bool mLayerTripleBufferingDisabled = false;
67063a5fcd2aa55c8eb0ccba621517a4215d8504df6Fabien Sanglard
671921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // these are thread safe
672921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    mutable MessageQueue mEventQueue;
6734b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    FrameTracker mAnimFrameTracker;
674faf77cce9d9ec0238d6999b3bd0d40c71ff403c5Jamie Gennis    DispSync mPrimaryDispSync;
675921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
676921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    // protected by mDestroyedLayerLock;
677921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    mutable Mutex mDestroyedLayerLock;
67813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    Vector<Layer const *> mDestroyedLayers;
679921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
680faf77cce9d9ec0238d6999b3bd0d40c71ff403c5Jamie Gennis    // protected by mHWVsyncLock
681faf77cce9d9ec0238d6999b3bd0d40c71ff403c5Jamie Gennis    Mutex mHWVsyncLock;
682faf77cce9d9ec0238d6999b3bd0d40c71ff403c5Jamie Gennis    bool mPrimaryHWVsyncEnabled;
683948fe0ce74c13e1bbff233883c158519fa8fb293Jesse Hall    bool mHWVsyncAvailable;
684faf77cce9d9ec0238d6999b3bd0d40c71ff403c5Jamie Gennis
6850a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    // protected by mCompositorTimingLock;
686d001058145c2186f454a3fb043388d6d9b84c9d8Brian Anderson    mutable std::mutex mCompositorTimingLock;
6870a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    CompositorTiming mCompositorTiming;
6880a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson
6890a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    // Only accessed from the main thread.
6900a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    struct CompositePresentTime {
6910a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson        nsecs_t composite { -1 };
6920a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson        std::shared_ptr<FenceTime> display { FenceTime::NO_FENCE };
6930a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    };
6940a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    std::queue<CompositePresentTime> mCompositePresentTimes;
6950a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson
696921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    /* ------------------------------------------------------------------------
697921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     * Feature prototyping
698921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian     */
699921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
700c1ba5c4649554e744844b07cfe402b42fbe12ff3Sahil Dhanju    bool mInjectVSyncs;
701c1ba5c4649554e744844b07cfe402b42fbe12ff3Sahil Dhanju
702ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian    Daltonizer mDaltonizer;
7039d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#ifndef USE_HWC2
7049d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard    bool mDaltonize;
7059d96de49673cb6c916bfb797aad4f4e024ea73caFabien Sanglard#endif
7069c5a3335110769993d3fe997bdf1d594954d4304Alan Viverette
7079f26a9c8be6f00f55cbc30b93adf4895c6a093aaDan Stoza    mat4 mPreviousColorMatrix;
7089c5a3335110769993d3fe997bdf1d594954d4304Alan Viverette    mat4 mColorMatrix;
7099c5a3335110769993d3fe997bdf1d594954d4304Alan Viverette    bool mHasColorMatrix;
710b90cf07f8b3a8fe76e40b997924c2509d04575f4Dan Stoza
711b90cf07f8b3a8fe76e40b997924c2509d04575f4Dan Stoza    // Static screen stats
712b90cf07f8b3a8fe76e40b997924c2509d04575f4Dan Stoza    bool mHasPoweredOff;
713b90cf07f8b3a8fe76e40b997924c2509d04575f4Dan Stoza    static const size_t NUM_BUCKETS = 8; // < 1-7, 7+
714b90cf07f8b3a8fe76e40b997924c2509d04575f4Dan Stoza    nsecs_t mFrameBuckets[NUM_BUCKETS];
715b90cf07f8b3a8fe76e40b997924c2509d04575f4Dan Stoza    nsecs_t mTotalTime;
7164a4e4a239f034cb8af2df9a438b26c3bc088889cTim Murray    std::atomic<nsecs_t> mLastSwapTime;
717e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza
7181f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    size_t mNumLayers;
7191f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
720e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza    // Double- vs. triple-buffering stats
721e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza    struct BufferingStats {
722e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza        BufferingStats()
723e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza          : numSegments(0),
724e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza            totalTime(0),
725e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza            twoBufferTime(0),
726e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza            doubleBufferedTime(0),
727e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza            tripleBufferedTime(0) {}
728e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza
729e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza        size_t numSegments;
730e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza        nsecs_t totalTime;
731e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza
732e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza        // "Two buffer" means that a third buffer was never used, whereas
733e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza        // "double-buffered" means that on average the segment only used two
734e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza        // buffers (though it may have used a third for some part of the
735e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza        // segment)
736e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza        nsecs_t twoBufferTime;
737e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza        nsecs_t doubleBufferedTime;
738e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza        nsecs_t tripleBufferedTime;
739e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza    };
740e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza    mutable Mutex mBufferingStatsMutex;
741e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza    std::unordered_map<std::string, BufferingStats> mBufferingStats;
7426e8e98a23592c2522396b673145814a4bbee69dbFabien Sanglard
7436e8e98a23592c2522396b673145814a4bbee69dbFabien Sanglard    // Verify that transaction is being called by an approved process:
7446e8e98a23592c2522396b673145814a4bbee69dbFabien Sanglard    // either AID_GRAPHICS or AID_SYSTEM.
7456e8e98a23592c2522396b673145814a4bbee69dbFabien Sanglard    status_t CheckTransactCodeCredentials(uint32_t code);
74687670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar
74787670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar#ifdef USE_HWC2
748050b2c83304bd16ec3a838da08b6ba6acf6a3af4Steven Thomas    std::atomic<bool> mVrFlingerRequestsDisplay;
749050b2c83304bd16ec3a838da08b6ba6acf6a3af4Steven Thomas    static bool useVrFlinger;
75087670ffe26079eb50b62f698eb0f7bfa8ee51debHendrik Wagenaar#endif
7516e8e98a23592c2522396b673145814a4bbee69dbFabien Sanglard    };
752edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}; // namespace android
753edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
754edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif // ANDROID_SURFACE_FLINGER_H
755