OSDN Git Service

Merge "Set the swap interval of virtual display surfaces to 0"
[android-x86/frameworks-native.git] / services / surfaceflinger / SurfaceFlinger.h
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef ANDROID_SURFACE_FLINGER_H
18 #define ANDROID_SURFACE_FLINGER_H
19
20 #include <stdint.h>
21 #include <sys/types.h>
22
23 #include <EGL/egl.h>
24 #include <GLES/gl.h>        // needed for GLuint
25
26 #include <cutils/compiler.h>
27
28 #include <utils/Atomic.h>
29 #include <utils/Errors.h>
30 #include <utils/KeyedVector.h>
31 #include <utils/RefBase.h>
32 #include <utils/SortedVector.h>
33 #include <utils/threads.h>
34
35 #include <binder/BinderService.h>
36 #include <binder/IMemory.h>
37
38 #include <ui/PixelFormat.h>
39
40 #include <gui/ISurfaceComposer.h>
41 #include <gui/ISurfaceComposerClient.h>
42
43 #include <hardware/hwcomposer_defs.h>
44
45 #include <private/gui/LayerState.h>
46
47 #include "Barrier.h"
48 #include "DisplayDevice.h"
49 #include "FrameTracker.h"
50 #include "MessageQueue.h"
51
52 #include "DisplayHardware/HWComposer.h"
53
54 namespace android {
55
56 // ---------------------------------------------------------------------------
57
58 class Client;
59 class DisplayEventConnection;
60 class EventThread;
61 class IGraphicBufferAlloc;
62 class Layer;
63 class LayerDim;
64 class Surface;
65 class RenderEngine;
66
67 // ---------------------------------------------------------------------------
68
69 enum {
70     eTransactionNeeded        = 0x01,
71     eTraversalNeeded          = 0x02,
72     eDisplayTransactionNeeded = 0x04,
73     eTransactionMask          = 0x07
74 };
75
76 class SurfaceFlinger : public BinderService<SurfaceFlinger>,
77                        public BnSurfaceComposer,
78                        private IBinder::DeathRecipient,
79                        private Thread,
80                        private HWComposer::EventHandler
81 {
82 public:
83     static char const* getServiceName() ANDROID_API {
84         return "SurfaceFlinger";
85     }
86
87     SurfaceFlinger() ANDROID_API;
88
89     enum {
90         EVENT_VSYNC = HWC_EVENT_VSYNC
91     };
92
93     // post an asynchronous message to the main thread
94     status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0,
95         uint32_t flags = 0);
96
97     // post a synchronous message to the main thread
98     status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0,
99         uint32_t flags = 0);
100
101     // force full composition on all displays
102     void repaintEverything();
103
104     // returns the default Display
105     sp<const DisplayDevice> getDefaultDisplayDevice() const {
106         return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]);
107     }
108
109     // utility function to delete a texture on the main thread
110     void deleteTextureAsync(GLuint texture);
111
112     // enable/disable h/w composer event
113     // TODO: this should be made accessible only to EventThread
114     void eventControl(int disp, int event, int enabled);
115
116     // called on the main thread by MessageQueue when an internal message
117     // is received
118     // TODO: this should be made accessible only to MessageQueue
119     void onMessageReceived(int32_t what);
120
121     // for debugging only
122     // TODO: this should be made accessible only to HWComposer
123     const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id);
124
125     RenderEngine& getRenderEngine() const {
126         return *mRenderEngine;
127     }
128
129 private:
130     friend class Client;
131     friend class DisplayEventConnection;
132     friend class Layer;
133     friend class SurfaceTextureLayer;
134
135     // This value is specified in number of frames.  Log frame stats at most
136     // every half hour.
137     enum { LOG_FRAME_STATS_PERIOD =  30*60*60 };
138
139     // We're reference counted, never destroy SurfaceFlinger directly
140     virtual ~SurfaceFlinger();
141
142     /* ------------------------------------------------------------------------
143      * Internal data structures
144      */
145
146     class LayerVector : public SortedVector< sp<Layer> > {
147     public:
148         LayerVector();
149         LayerVector(const LayerVector& rhs);
150         virtual int do_compare(const void* lhs, const void* rhs) const;
151     };
152
153     struct DisplayDeviceState {
154         DisplayDeviceState();
155         DisplayDeviceState(DisplayDevice::DisplayType type);
156         bool isValid() const { return type >= 0; }
157         bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
158         bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
159         DisplayDevice::DisplayType type;
160         sp<IGraphicBufferProducer> surface;
161         uint32_t layerStack;
162         Rect viewport;
163         Rect frame;
164         uint8_t orientation;
165         String8 displayName;
166         bool isSecure;
167     };
168
169     struct State {
170         LayerVector layersSortedByZ;
171         DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
172     };
173
174     /* ------------------------------------------------------------------------
175      * IBinder interface
176      */
177     virtual status_t onTransact(uint32_t code, const Parcel& data,
178         Parcel* reply, uint32_t flags);
179     virtual status_t dump(int fd, const Vector<String16>& args);
180
181     /* ------------------------------------------------------------------------
182      * ISurfaceComposer interface
183      */
184     virtual sp<ISurfaceComposerClient> createConnection();
185     virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc();
186     virtual sp<IBinder> createDisplay(const String8& displayName, bool secure);
187     virtual sp<IBinder> getBuiltInDisplay(int32_t id);
188     virtual void setTransactionState(const Vector<ComposerState>& state,
189             const Vector<DisplayState>& displays, uint32_t flags);
190     virtual void bootFinished();
191     virtual bool authenticateSurfaceTexture(
192         const sp<IGraphicBufferProducer>& bufferProducer) const;
193     virtual sp<IDisplayEventConnection> createDisplayEventConnection();
194     virtual status_t captureScreen(const sp<IBinder>& display,
195             const sp<IGraphicBufferProducer>& producer,
196             uint32_t reqWidth, uint32_t reqHeight,
197             uint32_t minLayerZ, uint32_t maxLayerZ);
198     // called when screen needs to turn off
199     virtual void blank(const sp<IBinder>& display);
200     // called when screen is turning back on
201     virtual void unblank(const sp<IBinder>& display);
202     virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info);
203
204     /* ------------------------------------------------------------------------
205      * DeathRecipient interface
206      */
207     virtual void binderDied(const wp<IBinder>& who);
208
209     /* ------------------------------------------------------------------------
210      * Thread interface
211      */
212     virtual bool threadLoop();
213     virtual status_t readyToRun();
214     virtual void onFirstRef();
215
216     /* ------------------------------------------------------------------------
217      * HWComposer::EventHandler interface
218      */
219     virtual void onVSyncReceived(int type, nsecs_t timestamp);
220     virtual void onHotplugReceived(int disp, bool connected);
221
222     /* ------------------------------------------------------------------------
223      * Message handling
224      */
225     void waitForEvent();
226     void signalTransaction();
227     void signalLayerUpdate();
228     void signalRefresh();
229
230     // called on the main thread in response to initializeDisplays()
231     void onInitializeDisplays();
232     // called on the main thread in response to blank()
233     void onScreenReleased(const sp<const DisplayDevice>& hw);
234     // called on the main thread in response to unblank()
235     void onScreenAcquired(const sp<const DisplayDevice>& hw);
236
237     void handleMessageTransaction();
238     void handleMessageInvalidate();
239     void handleMessageRefresh();
240
241     void handleTransaction(uint32_t transactionFlags);
242     void handleTransactionLocked(uint32_t transactionFlags);
243
244     /* handlePageFilp: this is were we latch a new buffer
245      * if available and compute the dirty region.
246      */
247     void handlePageFlip();
248
249     /* ------------------------------------------------------------------------
250      * Transactions
251      */
252     uint32_t getTransactionFlags(uint32_t flags);
253     uint32_t peekTransactionFlags(uint32_t flags);
254     uint32_t setTransactionFlags(uint32_t flags);
255     void commitTransaction();
256     uint32_t setClientStateLocked(const sp<Client>& client,
257         const layer_state_t& s);
258     uint32_t setDisplayStateLocked(const DisplayState& s);
259
260     /* ------------------------------------------------------------------------
261      * Layer management
262      */
263     status_t createLayer(const String8& name, const sp<Client>& client,
264             uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
265             sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp);
266
267     status_t createNormalLayer(const sp<Client>& client, const String8& name,
268             uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
269             sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp,
270             sp<Layer>* outLayer);
271
272     status_t createDimLayer(const sp<Client>& client, const String8& name,
273             uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
274             sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
275
276     // called in response to the window-manager calling
277     // ISurfaceComposerClient::destroySurface()
278     status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
279
280     // called when all clients have released all their references to
281     // this layer meaning it is entirely safe to destroy all
282     // resources associated to this layer.
283     status_t onLayerDestroyed(const wp<Layer>& layer);
284
285     // remove a layer from SurfaceFlinger immediately
286     status_t removeLayer(const sp<Layer>& layer);
287
288     // add a layer to SurfaceFlinger
289     void addClientLayer(const sp<Client>& client,
290             const sp<IBinder>& handle,
291             const sp<IGraphicBufferProducer>& gbc,
292             const sp<Layer>& lbc);
293
294     /* ------------------------------------------------------------------------
295      * Boot animation, on/off animations and screen capture
296      */
297
298     void startBootAnim();
299
300     void renderScreenImplLocked(
301             const sp<const DisplayDevice>& hw,
302             uint32_t reqWidth, uint32_t reqHeight,
303             uint32_t minLayerZ, uint32_t maxLayerZ,
304             bool yswap);
305
306     status_t captureScreenImplLocked(
307             const sp<const DisplayDevice>& hw,
308             const sp<IGraphicBufferProducer>& producer,
309             uint32_t reqWidth, uint32_t reqHeight,
310             uint32_t minLayerZ, uint32_t maxLayerZ);
311
312     /* ------------------------------------------------------------------------
313      * EGL
314      */
315     static status_t selectConfigForAttribute(EGLDisplay dpy,
316         EGLint const* attrs, EGLint attribute, EGLint value, EGLConfig* outConfig);
317     static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId);
318     size_t getMaxTextureSize() const;
319     size_t getMaxViewportDims() const;
320
321     /* ------------------------------------------------------------------------
322      * Display and layer stack management
323      */
324     // called when starting, or restarting after system_server death
325     void initializeDisplays();
326
327     // Create an IBinder for a builtin display and add it to current state
328     void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
329
330     // NOTE: can only be called from the main thread or with mStateLock held
331     sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
332         return mDisplays.valueFor(dpy);
333     }
334
335     // NOTE: can only be called from the main thread or with mStateLock held
336     sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
337         return mDisplays.valueFor(dpy);
338     }
339
340     // mark a region of a layer stack dirty. this updates the dirty
341     // region of all screens presenting this layer stack.
342     void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
343
344     // allocate a h/w composer display id
345     int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
346
347     /* ------------------------------------------------------------------------
348      * H/W composer
349      */
350
351     HWComposer& getHwComposer() const { return *mHwc; }
352
353     /* ------------------------------------------------------------------------
354      * Compositing
355      */
356     void invalidateHwcGeometry();
357     static void computeVisibleRegions(
358             const LayerVector& currentLayers, uint32_t layerStack,
359             Region& dirtyRegion, Region& opaqueRegion);
360
361     void preComposition();
362     void postComposition();
363     void rebuildLayerStacks();
364     void setUpHWComposer();
365     void doComposition();
366     void doDebugFlashRegions();
367     void doDisplayComposition(const sp<const DisplayDevice>& hw,
368             const Region& dirtyRegion);
369     void doComposeSurfaces(const sp<const DisplayDevice>& hw,
370             const Region& dirty);
371
372     void postFramebuffer();
373     void drawWormhole(const sp<const DisplayDevice>& hw,
374             const Region& region) const;
375
376     /* ------------------------------------------------------------------------
377      * Display management
378      */
379
380
381     /* ------------------------------------------------------------------------
382      * Debugging & dumpsys
383      */
384     void listLayersLocked(const Vector<String16>& args, size_t& index,
385         String8& result) const;
386     void dumpStatsLocked(const Vector<String16>& args, size_t& index,
387         String8& result) const;
388     void clearStatsLocked(const Vector<String16>& args, size_t& index,
389         String8& result);
390     void dumpAllLocked(const Vector<String16>& args, size_t& index,
391         String8& result) const;
392     bool startDdmConnection();
393     static void appendSfConfigString(String8& result);
394     void checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
395             const sp<const DisplayDevice>& hw,
396             uint32_t minLayerZ, uint32_t maxLayerZ);
397
398     void logFrameStats();
399
400     /* ------------------------------------------------------------------------
401      * Attributes
402      */
403
404     // access must be protected by mStateLock
405     mutable Mutex mStateLock;
406     State mCurrentState;
407     volatile int32_t mTransactionFlags;
408     Condition mTransactionCV;
409     bool mTransactionPending;
410     bool mAnimTransactionPending;
411     Vector< sp<Layer> > mLayersPendingRemoval;
412     SortedVector< wp<IBinder> > mGraphicBufferProducerList;
413
414     // protected by mStateLock (but we could use another lock)
415     bool mLayersRemoved;
416
417     // access must be protected by mInvalidateLock
418     volatile int32_t mRepaintEverything;
419
420     // constant members (no synchronization needed for access)
421     HWComposer* mHwc;
422     RenderEngine* mRenderEngine;
423     nsecs_t mBootTime;
424     bool mGpuToCpuSupported;
425     sp<EventThread> mEventThread;
426     EGLContext mEGLContext;
427     EGLConfig mEGLConfig;
428     EGLDisplay mEGLDisplay;
429     EGLint mEGLNativeVisualId;
430     sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_DISPLAY_TYPES];
431
432     // Can only accessed from the main thread, these members
433     // don't need synchronization
434     State mDrawingState;
435     bool mVisibleRegionsDirty;
436     bool mHwWorkListDirty;
437     bool mAnimCompositionPending;
438
439     // this may only be written from the main thread with mStateLock held
440     // it may be read from other threads with mStateLock held
441     DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
442
443     // don't use a lock for these, we don't care
444     int mDebugRegion;
445     int mDebugDDMS;
446     int mDebugDisableHWC;
447     int mDebugDisableTransformHint;
448     volatile nsecs_t mDebugInSwapBuffers;
449     nsecs_t mLastSwapBufferTime;
450     volatile nsecs_t mDebugInTransaction;
451     nsecs_t mLastTransactionTime;
452     bool mBootFinished;
453
454     // these are thread safe
455     mutable MessageQueue mEventQueue;
456     mutable Barrier mReadyToRunBarrier;
457     FrameTracker mAnimFrameTracker;
458
459     // protected by mDestroyedLayerLock;
460     mutable Mutex mDestroyedLayerLock;
461     Vector<Layer const *> mDestroyedLayers;
462
463     /* ------------------------------------------------------------------------
464      * Feature prototyping
465      */
466
467     sp<IBinder> mExtDisplayToken;
468 };
469
470 // ---------------------------------------------------------------------------
471 }; // namespace android
472
473 #endif // ANDROID_SURFACE_FLINGER_H