• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cutils/compiler.h>
24 
25 #include <utils/Atomic.h>
26 #include <utils/Errors.h>
27 #include <utils/KeyedVector.h>
28 #include <utils/RefBase.h>
29 #include <utils/SortedVector.h>
30 #include <utils/threads.h>
31 
32 #include <binder/BinderService.h>
33 #include <binder/IMemory.h>
34 
35 #include <ui/PixelFormat.h>
36 #include <gui/IGraphicBufferAlloc.h>
37 #include <gui/ISurfaceComposer.h>
38 #include <gui/ISurfaceComposerClient.h>
39 
40 #include "Barrier.h"
41 #include "Layer.h"
42 
43 #include "MessageQueue.h"
44 
45 namespace android {
46 
47 // ---------------------------------------------------------------------------
48 
49 class Client;
50 class DisplayHardware;
51 class DisplayEventConnection;
52 class EventThread;
53 class Layer;
54 class LayerDim;
55 class LayerScreenshot;
56 struct surface_flinger_cblk_t;
57 
58 // ---------------------------------------------------------------------------
59 
60 class Client : public BnSurfaceComposerClient
61 {
62 public:
63         Client(const sp<SurfaceFlinger>& flinger);
64         ~Client();
65 
66     status_t initCheck() const;
67 
68     // protected by SurfaceFlinger::mStateLock
69     size_t attachLayer(const sp<LayerBaseClient>& layer);
70     void detachLayer(const LayerBaseClient* layer);
71     sp<LayerBaseClient> getLayerUser(int32_t i) const;
72 
73 private:
74     // ISurfaceComposerClient interface
75     virtual sp<ISurface> createSurface(
76             surface_data_t* params, const String8& name,
77             DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
78             uint32_t flags);
79     virtual status_t destroySurface(SurfaceID surfaceId);
80     virtual status_t onTransact(
81         uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
82 
83     // constant
84     sp<SurfaceFlinger> mFlinger;
85 
86     // protected by mLock
87     DefaultKeyedVector< size_t, wp<LayerBaseClient> > mLayers;
88     size_t mNameGenerator;
89 
90     // thread-safe
91     mutable Mutex mLock;
92 };
93 
94 class GraphicBufferAlloc : public BnGraphicBufferAlloc
95 {
96 public:
97     GraphicBufferAlloc();
98     virtual ~GraphicBufferAlloc();
99     virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h,
100         PixelFormat format, uint32_t usage, status_t* error);
101 };
102 
103 // ---------------------------------------------------------------------------
104 
105 class GraphicPlane
106 {
107 public:
108     static status_t orientationToTransfrom(int orientation, int w, int h,
109             Transform* tr);
110 
111                                 GraphicPlane();
112                                 ~GraphicPlane();
113 
114         bool                    initialized() const;
115 
116         void                    setDisplayHardware(DisplayHardware *);
117         status_t                setOrientation(int orientation);
getOrientation()118         int                     getOrientation() const { return mOrientation; }
119         int                     getWidth() const;
120         int                     getHeight() const;
121 
122         const DisplayHardware&  displayHardware() const;
123         DisplayHardware&        editDisplayHardware();
124         const Transform&        transform() const;
125         EGLDisplay              getEGLDisplay() const;
126 
127 private:
128                                 GraphicPlane(const GraphicPlane&);
129         GraphicPlane            operator = (const GraphicPlane&);
130 
131         DisplayHardware*        mHw;
132         Transform               mGlobalTransform;
133         Transform               mDisplayTransform;
134         int                     mOrientation;
135         float                   mDisplayWidth;
136         float                   mDisplayHeight;
137         int                     mWidth;
138         int                     mHeight;
139 };
140 
141 // ---------------------------------------------------------------------------
142 
143 enum {
144     eTransactionNeeded      = 0x01,
145     eTraversalNeeded        = 0x02
146 };
147 
148 class SurfaceFlinger :
149         public BinderService<SurfaceFlinger>,
150         public BnSurfaceComposer,
151         public IBinder::DeathRecipient,
152         protected Thread
153 {
154 public:
getServiceName()155     static char const* getServiceName() { return "SurfaceFlinger"; }
156 
157                     SurfaceFlinger();
158     virtual         ~SurfaceFlinger();
159             void    init();
160 
161     virtual status_t onTransact(
162         uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
163 
164     virtual status_t dump(int fd, const Vector<String16>& args);
165 
166     // ISurfaceComposer interface
167     virtual sp<ISurfaceComposerClient>  createConnection();
168     virtual sp<IGraphicBufferAlloc>     createGraphicBufferAlloc();
169     virtual sp<IMemoryHeap>             getCblk() const;
170     virtual void                        bootFinished();
171     virtual void                        setTransactionState(const Vector<ComposerState>& state,
172                                                             int orientation, uint32_t flags);
173     virtual bool                        authenticateSurfaceTexture(const sp<ISurfaceTexture>& surface) const;
174     virtual sp<IDisplayEventConnection> createDisplayEventConnection();
175 
176     virtual status_t captureScreen(DisplayID dpy,
177             sp<IMemoryHeap>* heap,
178             uint32_t* width, uint32_t* height,
179             PixelFormat* format, uint32_t reqWidth, uint32_t reqHeight,
180             uint32_t minLayerZ, uint32_t maxLayerZ);
181 
182     virtual status_t                    turnElectronBeamOff(int32_t mode);
183     virtual status_t                    turnElectronBeamOn(int32_t mode);
184 
185 
186             // called when screen needs to turn off
187             void screenReleased();
188             // called when screen is turning back on
189             void screenAcquired();
190 
191             // called on the main thread in response to screenReleased()
192             void onScreenReleased();
193             // called on the main thread in response to screenAcquired()
194             void onScreenAcquired();
195 
196 
197             status_t renderScreenToTexture(DisplayID dpy,
198                     GLuint* textureName, GLfloat* uOut, GLfloat* vOut);
199             status_t renderScreenToTextureLocked(DisplayID dpy,
200                     GLuint* textureName, GLfloat* uOut, GLfloat* vOut);
201 
202             void onMessageReceived(int32_t what);
203 
204             status_t postMessageAsync(const sp<MessageBase>& msg,
205                     nsecs_t reltime=0, uint32_t flags = 0);
206 
207             status_t postMessageSync(const sp<MessageBase>& msg,
208                     nsecs_t reltime=0, uint32_t flags = 0);
209 
210     status_t removeLayer(const sp<LayerBase>& layer);
211     status_t addLayer(const sp<LayerBase>& layer);
212     status_t invalidateLayerVisibility(const sp<LayerBase>& layer);
213     void invalidateHwcGeometry();
214 
215     sp<Layer> getLayer(const sp<ISurface>& sur) const;
216 
getProtectedTexName()217     GLuint getProtectedTexName() const { return mProtectedTexName; }
218 
219 
220     class MessageDestroyGLTexture : public MessageBase {
221         GLuint texture;
222     public:
MessageDestroyGLTexture(GLuint texture)223         MessageDestroyGLTexture(GLuint texture) : texture(texture) { }
handler()224         virtual bool handler() {
225             glDeleteTextures(1, &texture);
226             return true;
227         }
228     };
229 
230 
231 private:
232     // DeathRecipient interface
233     virtual void binderDied(const wp<IBinder>& who);
234 
235 private:
236     friend class Client;
237     friend class DisplayEventConnection;
238     friend class LayerBase;
239     friend class LayerBaseClient;
240     friend class Layer;
241 
242     sp<ISurface> createSurface(
243             ISurfaceComposerClient::surface_data_t* params,
244             const String8& name,
245             const sp<Client>& client,
246             DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
247             uint32_t flags);
248 
249     sp<Layer> createNormalSurface(
250             const sp<Client>& client, DisplayID display,
251             uint32_t w, uint32_t h, uint32_t flags,
252             PixelFormat& format);
253 
254     sp<LayerDim> createDimSurface(
255             const sp<Client>& client, DisplayID display,
256             uint32_t w, uint32_t h, uint32_t flags);
257 
258     sp<LayerScreenshot> createScreenshotSurface(
259             const sp<Client>& client, DisplayID display,
260             uint32_t w, uint32_t h, uint32_t flags);
261 
262     status_t removeSurface(const sp<Client>& client, SurfaceID sid);
263     status_t destroySurface(const wp<LayerBaseClient>& layer);
264     uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s);
265 
266     class LayerVector : public SortedVector< sp<LayerBase> > {
267     public:
LayerVector()268         LayerVector() { }
LayerVector(const LayerVector & rhs)269         LayerVector(const LayerVector& rhs) : SortedVector< sp<LayerBase> >(rhs) { }
do_compare(const void * lhs,const void * rhs)270         virtual int do_compare(const void* lhs, const void* rhs) const {
271             const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
272             const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
273             // sort layers by Z order
274             uint32_t lz = l->currentState().z;
275             uint32_t rz = r->currentState().z;
276             // then by sequence, so we get a stable ordering
277             return (lz != rz) ? (lz - rz) : (l->sequence - r->sequence);
278         }
279     };
280 
281     struct State {
StateState282         State()
283             : orientation(ISurfaceComposer::eOrientationDefault),
284               orientationFlags(0) {
285         }
286         LayerVector     layersSortedByZ;
287         uint8_t         orientation;
288         uint8_t         orientationFlags;
289     };
290 
291     virtual bool        threadLoop();
292     virtual status_t    readyToRun();
293     virtual void        onFirstRef();
294 
295 public:     // hack to work around gcc 4.0.3 bug
296     const GraphicPlane&     graphicPlane(int dpy) const;
297           GraphicPlane&     graphicPlane(int dpy);
298 
299           void              signalTransaction();
300           void              signalLayerUpdate();
301           void              signalRefresh();
302           void              repaintEverything();
303 
304 private:
305             void        waitForEvent();
306             void        handleTransaction(uint32_t transactionFlags);
307             void        handleTransactionLocked(uint32_t transactionFlags);
308 
309             void        computeVisibleRegions(
310                             const LayerVector& currentLayers,
311                             Region& dirtyRegion,
312                             Region& wormholeRegion);
313 
314             void        handlePageFlip();
315             bool        lockPageFlip(const LayerVector& currentLayers);
316             void        unlockPageFlip(const LayerVector& currentLayers);
317             void        handleRefresh();
318             void        handleWorkList();
319             void        handleRepaint();
320             void        postFramebuffer();
321             void        setupHardwareComposer();
322             void        composeSurfaces(const Region& dirty);
323 
324 
325             void        setInvalidateRegion(const Region& reg);
326             Region      getAndClearInvalidateRegion();
327 
328             ssize_t     addClientLayer(const sp<Client>& client,
329                     const sp<LayerBaseClient>& lbc);
330             status_t    addLayer_l(const sp<LayerBase>& layer);
331             status_t    removeLayer_l(const sp<LayerBase>& layer);
332             status_t    purgatorizeLayer_l(const sp<LayerBase>& layer);
333 
334             uint32_t    getTransactionFlags(uint32_t flags);
335             uint32_t    peekTransactionFlags(uint32_t flags);
336             uint32_t    setTransactionFlags(uint32_t flags);
337             void        commitTransaction();
338 
339 
340             status_t captureScreenImplLocked(DisplayID dpy,
341                     sp<IMemoryHeap>* heap,
342                     uint32_t* width, uint32_t* height, PixelFormat* format,
343                     uint32_t reqWidth, uint32_t reqHeight,
344                     uint32_t minLayerZ, uint32_t maxLayerZ);
345 
346             status_t turnElectronBeamOffImplLocked(int32_t mode);
347             status_t turnElectronBeamOnImplLocked(int32_t mode);
348             status_t electronBeamOffAnimationImplLocked();
349             status_t electronBeamOnAnimationImplLocked();
350 
351             void        debugFlashRegions();
352             void        drawWormhole() const;
353 
354             void        startBootAnim();
355 
356             void listLayersLocked(const Vector<String16>& args, size_t& index,
357                     String8& result, char* buffer, size_t SIZE) const;
358             void dumpStatsLocked(const Vector<String16>& args, size_t& index,
359                     String8& result, char* buffer, size_t SIZE) const;
360             void clearStatsLocked(const Vector<String16>& args, size_t& index,
361                     String8& result, char* buffer, size_t SIZE) const;
362             void dumpAllLocked(String8& result, char* buffer, size_t SIZE) const;
363 
364     mutable     MessageQueue    mEventQueue;
365 
366                 // access must be protected by mStateLock
367     mutable     Mutex                   mStateLock;
368                 State                   mCurrentState;
369     volatile    int32_t                 mTransactionFlags;
370                 Condition               mTransactionCV;
371                 SortedVector< sp<LayerBase> > mLayerPurgatory;
372                 bool                    mTransationPending;
373                 Vector< sp<LayerBase> > mLayersPendingRemoval;
374 
375                 // protected by mStateLock (but we could use another lock)
376                 GraphicPlane                mGraphicPlanes[1];
377                 bool                        mLayersRemoved;
378                 DefaultKeyedVector< wp<IBinder>, wp<Layer> > mLayerMap;
379 
380                 // access must be protected by mInvalidateLock
381     mutable     Mutex                       mInvalidateLock;
382                 Region                      mInvalidateRegion;
383 
384                 // constant members (no synchronization needed for access)
385                 sp<IMemoryHeap>             mServerHeap;
386                 surface_flinger_cblk_t*     mServerCblk;
387                 GLuint                      mWormholeTexName;
388                 GLuint                      mProtectedTexName;
389                 nsecs_t                     mBootTime;
390                 sp<EventThread>             mEventThread;
391 
392                 // Can only accessed from the main thread, these members
393                 // don't need synchronization
394                 State                       mDrawingState;
395                 Region                      mDirtyRegion;
396                 Region                      mDirtyRegionRemovedLayer;
397                 Region                      mSwapRegion;
398                 Region                      mWormholeRegion;
399                 bool                        mVisibleRegionsDirty;
400                 bool                        mHwWorkListDirty;
401                 int32_t                     mElectronBeamAnimationMode;
402                 Vector< sp<LayerBase> >     mVisibleLayersSortedByZ;
403 
404 
405                 // don't use a lock for these, we don't care
406                 int                         mDebugRegion;
407                 int                         mDebugDDMS;
408                 int                         mDebugDisableHWC;
409                 int                         mDebugDisableTransformHint;
410                 volatile nsecs_t            mDebugInSwapBuffers;
411                 nsecs_t                     mLastSwapBufferTime;
412                 volatile nsecs_t            mDebugInTransaction;
413                 nsecs_t                     mLastTransactionTime;
414                 bool                        mBootFinished;
415 
416                 // these are thread safe
417     mutable     Barrier                     mReadyToRunBarrier;
418 
419 
420                 // protected by mDestroyedLayerLock;
421     mutable     Mutex                       mDestroyedLayerLock;
422                 Vector<LayerBase const *>   mDestroyedLayers;
423 
424    // only written in the main thread, only read in other threads
425    volatile     int32_t                     mSecureFrameBuffer;
426 };
427 
428 // ---------------------------------------------------------------------------
429 }; // namespace android
430 
431 #endif // ANDROID_SURFACE_FLINGER_H
432