OSDN Git Service

merge in klp-release history after reset to master
[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     // We're reference counted, never destroy SurfaceFlinger directly
136     virtual ~SurfaceFlinger();
137
138     /* ------------------------------------------------------------------------
139      * Internal data structures
140      */
141
142     class LayerVector : public SortedVector< sp<Layer> > {
143     public:
144         LayerVector();
145         LayerVector(const LayerVector& rhs);
146         virtual int do_compare(const void* lhs, const void* rhs) const;
147     };
148
149     struct DisplayDeviceState {
150         DisplayDeviceState();
151         DisplayDeviceState(DisplayDevice::DisplayType type);
152         bool isValid() const { return type >= 0; }
153         bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
154         bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
155         DisplayDevice::DisplayType type;
156         sp<IGraphicBufferProducer> surface;
157         uint32_t layerStack;
158         Rect viewport;
159         Rect frame;
160         uint8_t orientation;
161         String8 displayName;
162         bool isSecure;
163     };
164
165     struct State {
166         LayerVector layersSortedByZ;
167         DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
168     };
169
170     /* ------------------------------------------------------------------------
171      * IBinder interface
172      */
173     virtual status_t onTransact(uint32_t code, const Parcel& data,
174         Parcel* reply, uint32_t flags);
175     virtual status_t dump(int fd, const Vector<String16>& args);
176
177     /* ------------------------------------------------------------------------
178      * ISurfaceComposer interface
179      */
180     virtual sp<ISurfaceComposerClient> createConnection();
181     virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc();
182     virtual sp<IBinder> createDisplay(const String8& displayName, bool secure);
183     virtual sp<IBinder> getBuiltInDisplay(int32_t id);
184     virtual void setTransactionState(const Vector<ComposerState>& state,
185             const Vector<DisplayState>& displays, uint32_t flags);
186     virtual void bootFinished();
187     virtual bool authenticateSurfaceTexture(
188         const sp<IGraphicBufferProducer>& bufferProducer) const;
189     virtual sp<IDisplayEventConnection> createDisplayEventConnection();
190     virtual status_t captureScreen(const sp<IBinder>& display,
191             const sp<IGraphicBufferProducer>& producer,
192             uint32_t reqWidth, uint32_t reqHeight,
193             uint32_t minLayerZ, uint32_t maxLayerZ, bool isCpuConsumer);
194     // called when screen needs to turn off
195     virtual void blank(const sp<IBinder>& display);
196     // called when screen is turning back on
197     virtual void unblank(const sp<IBinder>& display);
198     virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info);
199
200     /* ------------------------------------------------------------------------
201      * DeathRecipient interface
202      */
203     virtual void binderDied(const wp<IBinder>& who);
204
205     /* ------------------------------------------------------------------------
206      * Thread interface
207      */
208     virtual bool threadLoop();
209     virtual status_t readyToRun();
210     virtual void onFirstRef();
211
212     /* ------------------------------------------------------------------------
213      * HWComposer::EventHandler interface
214      */
215     virtual void onVSyncReceived(int type, nsecs_t timestamp);
216     virtual void onHotplugReceived(int disp, bool connected);
217
218     /* ------------------------------------------------------------------------
219      * Message handling
220      */
221     void waitForEvent();
222     void signalTransaction();
223     void signalLayerUpdate();
224     void signalRefresh();
225
226     // called on the main thread in response to initializeDisplays()
227     void onInitializeDisplays();
228     // called on the main thread in response to blank()
229     void onScreenReleased(const sp<const DisplayDevice>& hw);
230     // called on the main thread in response to unblank()
231     void onScreenAcquired(const sp<const DisplayDevice>& hw);
232
233     void handleMessageTransaction();
234     void handleMessageInvalidate();
235     void handleMessageRefresh();
236
237     void handleTransaction(uint32_t transactionFlags);
238     void handleTransactionLocked(uint32_t transactionFlags);
239
240     /* handlePageFilp: this is were we latch a new buffer
241      * if available and compute the dirty region.
242      */
243     void handlePageFlip();
244
245     /* ------------------------------------------------------------------------
246      * Transactions
247      */
248     uint32_t getTransactionFlags(uint32_t flags);
249     uint32_t peekTransactionFlags(uint32_t flags);
250     uint32_t setTransactionFlags(uint32_t flags);
251     void commitTransaction();
252     uint32_t setClientStateLocked(const sp<Client>& client,
253         const layer_state_t& s);
254     uint32_t setDisplayStateLocked(const DisplayState& s);
255
256     /* ------------------------------------------------------------------------
257      * Layer management
258      */
259     status_t createLayer(const String8& name, const sp<Client>& client,
260             uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
261             sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp);
262
263     status_t createNormalLayer(const sp<Client>& client, const String8& name,
264             uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
265             sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp,
266             sp<Layer>* outLayer);
267
268     status_t createDimLayer(const sp<Client>& client, const String8& name,
269             uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
270             sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
271
272     // called in response to the window-manager calling
273     // ISurfaceComposerClient::destroySurface()
274     status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
275
276     // called when all clients have released all their references to
277     // this layer meaning it is entirely safe to destroy all
278     // resources associated to this layer.
279     status_t onLayerDestroyed(const wp<Layer>& layer);
280
281     // remove a layer from SurfaceFlinger immediately
282     status_t removeLayer(const sp<Layer>& layer);
283
284     // add a layer to SurfaceFlinger
285     void addClientLayer(const sp<Client>& client,
286             const sp<IBinder>& handle,
287             const sp<IGraphicBufferProducer>& gbc,
288             const sp<Layer>& lbc);
289
290     /* ------------------------------------------------------------------------
291      * Boot animation, on/off animations and screen capture
292      */
293
294     void startBootAnim();
295
296     void renderScreenImplLocked(
297             const sp<const DisplayDevice>& hw,
298             uint32_t reqWidth, uint32_t reqHeight,
299             uint32_t minLayerZ, uint32_t maxLayerZ,
300             bool yswap);
301
302     status_t captureScreenImplLocked(
303             const sp<const DisplayDevice>& hw,
304             const sp<IGraphicBufferProducer>& producer,
305             uint32_t reqWidth, uint32_t reqHeight,
306             uint32_t minLayerZ, uint32_t maxLayerZ,
307             bool useReadPixels);
308
309     /* ------------------------------------------------------------------------
310      * EGL
311      */
312     static status_t selectConfigForAttribute(EGLDisplay dpy,
313         EGLint const* attrs, EGLint attribute, EGLint value, EGLConfig* outConfig);
314     static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId);
315     size_t getMaxTextureSize() const;
316     size_t getMaxViewportDims() const;
317
318     /* ------------------------------------------------------------------------
319      * Display and layer stack management
320      */
321     // called when starting, or restarting after system_server death
322     void initializeDisplays();
323
324     // Create an IBinder for a builtin display and add it to current state
325     void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
326
327     // NOTE: can only be called from the main thread or with mStateLock held
328     sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
329         return mDisplays.valueFor(dpy);
330     }
331
332     // NOTE: can only be called from the main thread or with mStateLock held
333     sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
334         return mDisplays.valueFor(dpy);
335     }
336
337     // mark a region of a layer stack dirty. this updates the dirty
338     // region of all screens presenting this layer stack.
339     void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
340
341     // allocate a h/w composer display id
342     int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
343
344     /* ------------------------------------------------------------------------
345      * H/W composer
346      */
347
348     HWComposer& getHwComposer() const { return *mHwc; }
349
350     /* ------------------------------------------------------------------------
351      * Compositing
352      */
353     void invalidateHwcGeometry();
354     static void computeVisibleRegions(
355             const LayerVector& currentLayers, uint32_t layerStack,
356             Region& dirtyRegion, Region& opaqueRegion);
357
358     void preComposition();
359     void postComposition();
360     void rebuildLayerStacks();
361     void setUpHWComposer();
362     void doComposition();
363     void doDebugFlashRegions();
364     void doDisplayComposition(const sp<const DisplayDevice>& hw,
365             const Region& dirtyRegion);
366     void doComposeSurfaces(const sp<const DisplayDevice>& hw,
367             const Region& dirty);
368
369     void postFramebuffer();
370     void drawWormhole(const sp<const DisplayDevice>& hw,
371             const Region& region) const;
372
373     /* ------------------------------------------------------------------------
374      * Display management
375      */
376
377
378     /* ------------------------------------------------------------------------
379      * Debugging & dumpsys
380      */
381     void listLayersLocked(const Vector<String16>& args, size_t& index,
382         String8& result) const;
383     void dumpStatsLocked(const Vector<String16>& args, size_t& index,
384         String8& result) const;
385     void clearStatsLocked(const Vector<String16>& args, size_t& index,
386         String8& result);
387     void dumpAllLocked(const Vector<String16>& args, size_t& index,
388         String8& result) const;
389     bool startDdmConnection();
390     static void appendSfConfigString(String8& result);
391     void checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
392             const sp<const DisplayDevice>& hw,
393             uint32_t minLayerZ, uint32_t maxLayerZ);
394
395     /* ------------------------------------------------------------------------
396      * Attributes
397      */
398
399     // access must be protected by mStateLock
400     mutable Mutex mStateLock;
401     State mCurrentState;
402     volatile int32_t mTransactionFlags;
403     Condition mTransactionCV;
404     bool mTransactionPending;
405     bool mAnimTransactionPending;
406     Vector< sp<Layer> > mLayersPendingRemoval;
407     SortedVector< wp<IBinder> > mGraphicBufferProducerList;
408
409     // protected by mStateLock (but we could use another lock)
410     bool mLayersRemoved;
411
412     // access must be protected by mInvalidateLock
413     volatile int32_t mRepaintEverything;
414
415     // constant members (no synchronization needed for access)
416     HWComposer* mHwc;
417     RenderEngine* mRenderEngine;
418     nsecs_t mBootTime;
419     bool mGpuToCpuSupported;
420     sp<EventThread> mEventThread;
421     EGLContext mEGLContext;
422     EGLConfig mEGLConfig;
423     EGLDisplay mEGLDisplay;
424     EGLint mEGLNativeVisualId;
425     sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_DISPLAY_TYPES];
426
427     // Can only accessed from the main thread, these members
428     // don't need synchronization
429     State mDrawingState;
430     bool mVisibleRegionsDirty;
431     bool mHwWorkListDirty;
432     bool mAnimCompositionPending;
433
434     // this may only be written from the main thread with mStateLock held
435     // it may be read from other threads with mStateLock held
436     DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
437
438     // don't use a lock for these, we don't care
439     int mDebugRegion;
440     int mDebugDDMS;
441     int mDebugDisableHWC;
442     int mDebugDisableTransformHint;
443     volatile nsecs_t mDebugInSwapBuffers;
444     nsecs_t mLastSwapBufferTime;
445     volatile nsecs_t mDebugInTransaction;
446     nsecs_t mLastTransactionTime;
447     bool mBootFinished;
448
449     // these are thread safe
450     mutable MessageQueue mEventQueue;
451     mutable Barrier mReadyToRunBarrier;
452     FrameTracker mAnimFrameTracker;
453
454     // protected by mDestroyedLayerLock;
455     mutable Mutex mDestroyedLayerLock;
456     Vector<Layer const *> mDestroyedLayers;
457
458     /* ------------------------------------------------------------------------
459      * Feature prototyping
460      */
461
462     sp<IBinder> mExtDisplayToken;
463 };
464
465 // ---------------------------------------------------------------------------
466 }; // namespace android
467
468 #endif // ANDROID_SURFACE_FLINGER_H