OSDN Git Service

Merge "EGL: fix eglTerminate blob cache behavior"
[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, bool isCpuConsumer);
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             bool useReadPixels);
312
313     /* ------------------------------------------------------------------------
314      * EGL
315      */
316     static status_t selectConfigForAttribute(EGLDisplay dpy,
317         EGLint const* attrs, EGLint attribute, EGLint value, EGLConfig* outConfig);
318     static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId);
319     size_t getMaxTextureSize() const;
320     size_t getMaxViewportDims() const;
321
322     /* ------------------------------------------------------------------------
323      * Display and layer stack management
324      */
325     // called when starting, or restarting after system_server death
326     void initializeDisplays();
327
328     // Create an IBinder for a builtin display and add it to current state
329     void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
330
331     // NOTE: can only be called from the main thread or with mStateLock held
332     sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
333         return mDisplays.valueFor(dpy);
334     }
335
336     // NOTE: can only be called from the main thread or with mStateLock held
337     sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
338         return mDisplays.valueFor(dpy);
339     }
340
341     // mark a region of a layer stack dirty. this updates the dirty
342     // region of all screens presenting this layer stack.
343     void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
344
345     // allocate a h/w composer display id
346     int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
347
348     /* ------------------------------------------------------------------------
349      * H/W composer
350      */
351
352     HWComposer& getHwComposer() const { return *mHwc; }
353
354     /* ------------------------------------------------------------------------
355      * Compositing
356      */
357     void invalidateHwcGeometry();
358     static void computeVisibleRegions(
359             const LayerVector& currentLayers, uint32_t layerStack,
360             Region& dirtyRegion, Region& opaqueRegion);
361
362     void preComposition();
363     void postComposition();
364     void rebuildLayerStacks();
365     void setUpHWComposer();
366     void doComposition();
367     void doDebugFlashRegions();
368     void doDisplayComposition(const sp<const DisplayDevice>& hw,
369             const Region& dirtyRegion);
370     void doComposeSurfaces(const sp<const DisplayDevice>& hw,
371             const Region& dirty);
372
373     void postFramebuffer();
374     void drawWormhole(const sp<const DisplayDevice>& hw,
375             const Region& region) const;
376
377     /* ------------------------------------------------------------------------
378      * Display management
379      */
380
381
382     /* ------------------------------------------------------------------------
383      * Debugging & dumpsys
384      */
385     void listLayersLocked(const Vector<String16>& args, size_t& index,
386         String8& result) const;
387     void dumpStatsLocked(const Vector<String16>& args, size_t& index,
388         String8& result) const;
389     void clearStatsLocked(const Vector<String16>& args, size_t& index,
390         String8& result);
391     void dumpAllLocked(const Vector<String16>& args, size_t& index,
392         String8& result) const;
393     bool startDdmConnection();
394     static void appendSfConfigString(String8& result);
395     void checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
396             const sp<const DisplayDevice>& hw,
397             uint32_t minLayerZ, uint32_t maxLayerZ);
398
399     void logFrameStats();
400
401     /* ------------------------------------------------------------------------
402      * Attributes
403      */
404
405     // access must be protected by mStateLock
406     mutable Mutex mStateLock;
407     State mCurrentState;
408     volatile int32_t mTransactionFlags;
409     Condition mTransactionCV;
410     bool mTransactionPending;
411     bool mAnimTransactionPending;
412     Vector< sp<Layer> > mLayersPendingRemoval;
413     SortedVector< wp<IBinder> > mGraphicBufferProducerList;
414
415     // protected by mStateLock (but we could use another lock)
416     bool mLayersRemoved;
417
418     // access must be protected by mInvalidateLock
419     volatile int32_t mRepaintEverything;
420
421     // constant members (no synchronization needed for access)
422     HWComposer* mHwc;
423     RenderEngine* mRenderEngine;
424     nsecs_t mBootTime;
425     bool mGpuToCpuSupported;
426     sp<EventThread> mEventThread;
427     EGLContext mEGLContext;
428     EGLConfig mEGLConfig;
429     EGLDisplay mEGLDisplay;
430     EGLint mEGLNativeVisualId;
431     sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_DISPLAY_TYPES];
432
433     // Can only accessed from the main thread, these members
434     // don't need synchronization
435     State mDrawingState;
436     bool mVisibleRegionsDirty;
437     bool mHwWorkListDirty;
438     bool mAnimCompositionPending;
439
440     // this may only be written from the main thread with mStateLock held
441     // it may be read from other threads with mStateLock held
442     DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
443
444     // don't use a lock for these, we don't care
445     int mDebugRegion;
446     int mDebugDDMS;
447     int mDebugDisableHWC;
448     int mDebugDisableTransformHint;
449     volatile nsecs_t mDebugInSwapBuffers;
450     nsecs_t mLastSwapBufferTime;
451     volatile nsecs_t mDebugInTransaction;
452     nsecs_t mLastTransactionTime;
453     bool mBootFinished;
454
455     // these are thread safe
456     mutable MessageQueue mEventQueue;
457     mutable Barrier mReadyToRunBarrier;
458     FrameTracker mAnimFrameTracker;
459
460     // protected by mDestroyedLayerLock;
461     mutable Mutex mDestroyedLayerLock;
462     Vector<Layer const *> mDestroyedLayers;
463
464     /* ------------------------------------------------------------------------
465      * Feature prototyping
466      */
467
468     sp<IBinder> mExtDisplayToken;
469 };
470
471 // ---------------------------------------------------------------------------
472 }; // namespace android
473
474 #endif // ANDROID_SURFACE_FLINGER_H