blob: 2b7820c04f010cb67235eb6abe0589fb540bd555 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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 <utils/SortedVector.h>
24#include <utils/KeyedVector.h>
25#include <utils/threads.h>
26#include <utils/Atomic.h>
27#include <utils/Errors.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
Mathias Agopian7303c6b2009-07-02 18:11:53 -070030#include <binder/IMemory.h>
Mathias Agopian375f5632009-06-15 18:24:59 -070031#include <binder/Permission.h>
32
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include <ui/PixelFormat.h>
34#include <ui/ISurfaceComposer.h>
35#include <ui/ISurfaceFlingerClient.h>
36
Mathias Agopiancbb288b2009-09-07 16:32:45 -070037#include <private/ui/SharedBufferStack.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include <private/ui/LayerState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
40#include "Barrier.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include "Layer.h"
42#include "Tokenizer.h"
43
Mathias Agopianf1d8e872009-04-20 19:39:12 -070044#include "MessageQueue.h"
45
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046struct copybit_device_t;
47struct overlay_device_t;
48
49namespace android {
50
51// ---------------------------------------------------------------------------
52
53class Client;
54class BClient;
55class DisplayHardware;
56class FreezeLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057class Layer;
58class LayerBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
60typedef int32_t ClientID;
61
62#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
63#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
64
65// ---------------------------------------------------------------------------
66
Mathias Agopianf9d93272009-06-19 17:00:27 -070067class Client : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068{
69public:
70 Client(ClientID cid, const sp<SurfaceFlinger>& flinger);
71 ~Client();
72
73 int32_t generateId(int pid);
74 void free(int32_t id);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070075 status_t bindLayer(const sp<LayerBaseClient>& layer, int32_t id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076
77 inline bool isValid(int32_t i) const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078 sp<LayerBaseClient> getLayerUser(int32_t i) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 void dump(const char* what);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080
Mathias Agopian7303c6b2009-07-02 18:11:53 -070081 const Vector< wp<LayerBaseClient> >& getLayers() const {
82 return mLayers;
83 }
84
85 const sp<IMemoryHeap>& getControlBlockMemory() const {
86 return mCblkHeap;
87 }
88
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 // pointer to this client's control block
Mathias Agopiancbb288b2009-09-07 16:32:45 -070090 SharedClient* ctrlblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 ClientID cid;
92
93
94private:
Mathias Agopian7303c6b2009-07-02 18:11:53 -070095 int getClientPid() const { return mPid; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096
Mathias Agopian076b1cc2009-04-10 14:24:30 -070097 int mPid;
98 uint32_t mBitmap;
99 SortedVector<uint8_t> mInUse;
100 Vector< wp<LayerBaseClient> > mLayers;
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700101 sp<IMemoryHeap> mCblkHeap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700102 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103};
104
105// ---------------------------------------------------------------------------
106
107class GraphicPlane
108{
109public:
110 static status_t orientationToTransfrom(int orientation, int w, int h,
111 Transform* tr);
112
113 GraphicPlane();
114 ~GraphicPlane();
115
116 bool initialized() const;
117
118 void setDisplayHardware(DisplayHardware *);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 status_t setOrientation(int orientation);
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700120 int getOrientation() const { return mOrientation; }
Mathias Agopian2b92d892010-02-08 15:49:35 -0800121 int getWidth() const;
122 int getHeight() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123
124 const DisplayHardware& displayHardware() const;
125 const Transform& transform() const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700126 EGLDisplay getEGLDisplay() const;
127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128private:
129 GraphicPlane(const GraphicPlane&);
130 GraphicPlane operator = (const GraphicPlane&);
131
132 DisplayHardware* mHw;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 Transform mGlobalTransform;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800134 Transform mDisplayTransform;
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700135 int mOrientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800136 float mDisplayWidth;
137 float mDisplayHeight;
138 int mWidth;
139 int mHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140};
141
142// ---------------------------------------------------------------------------
143
144enum {
145 eTransactionNeeded = 0x01,
146 eTraversalNeeded = 0x02
147};
148
149class SurfaceFlinger : public BnSurfaceComposer, protected Thread
150{
151public:
152 static void instantiate();
153 static void shutdown();
154
155 SurfaceFlinger();
156 virtual ~SurfaceFlinger();
157 void init();
158
159 virtual status_t onTransact(
160 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
161
162 virtual status_t dump(int fd, const Vector<String16>& args);
163
164 // ISurfaceComposer interface
165 virtual sp<ISurfaceFlingerClient> createConnection();
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700166 virtual sp<IMemoryHeap> getCblk() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 virtual void bootFinished();
168 virtual void openGlobalTransaction();
169 virtual void closeGlobalTransaction();
170 virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags);
171 virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags);
Mathias Agopianc08731e2009-03-27 18:11:38 -0700172 virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 virtual void signal() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174
175 void screenReleased(DisplayID dpy);
176 void screenAcquired(DisplayID dpy);
177
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 overlay_control_device_t* getOverlayEngine() const;
179
180
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700181 status_t removeLayer(const sp<LayerBase>& layer);
182 status_t addLayer(const sp<LayerBase>& layer);
183 status_t invalidateLayerVisibility(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184
185private:
186 friend class BClient;
187 friend class LayerBase;
188 friend class LayerBuffer;
189 friend class LayerBaseClient;
Mathias Agopian1df3bbb2009-07-06 19:04:03 -0700190 friend class LayerBaseClient::Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 friend class Layer;
192 friend class LayerBlur;
Mathias Agopian945ebbf2009-06-18 18:48:39 -0700193 friend class LayerDim;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194
195 sp<ISurface> createSurface(ClientID client, int pid,
196 ISurfaceFlingerClient::surface_data_t* params,
197 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
198 uint32_t flags);
199
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 sp<LayerBaseClient> createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700201 const sp<Client>& client, DisplayID display,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700202 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
203 PixelFormat& format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700205 sp<LayerBaseClient> createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700206 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
208
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700209 sp<LayerBaseClient> createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700210 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
212
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700213 sp<LayerBaseClient> createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700214 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
216
Mathias Agopian9a112062009-04-17 19:36:26 -0700217 status_t removeSurface(SurfaceID surface_id);
218 status_t destroySurface(const sp<LayerBaseClient>& layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700219 status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220
221
222 class LayerVector {
223 public:
224 inline LayerVector() { }
225 LayerVector(const LayerVector&);
226 inline size_t size() const { return layers.size(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700227 inline sp<LayerBase> const* array() const { return layers.array(); }
228 ssize_t add(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
229 ssize_t remove(const sp<LayerBase>&);
230 ssize_t reorder(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
231 ssize_t indexOf(const sp<LayerBase>& key, size_t guess=0) const;
232 inline sp<LayerBase> operator [] (size_t i) const { return layers[i]; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700234 KeyedVector< sp<LayerBase> , size_t> lookup;
235 Vector< sp<LayerBase> > layers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 };
237
238 struct State {
239 State() {
240 orientation = ISurfaceComposer::eOrientationDefault;
241 freezeDisplay = 0;
242 }
243 LayerVector layersSortedByZ;
244 uint8_t orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700245 uint8_t orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 uint8_t freezeDisplay;
247 };
248
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 virtual bool threadLoop();
250 virtual status_t readyToRun();
251 virtual void onFirstRef();
252
Andy McFadden550a1142009-10-29 10:19:34 -0700253public: // hack to work around gcc 4.0.3 bug
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 const GraphicPlane& graphicPlane(int dpy) const;
255 GraphicPlane& graphicPlane(int dpy);
Andy McFadden550a1142009-10-29 10:19:34 -0700256private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257
258 void waitForEvent();
Andy McFadden2944a2b2009-09-21 14:33:20 -0700259public: // hack to work around gcc 4.0.3 bug
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 void signalEvent();
Andy McFadden2944a2b2009-09-21 14:33:20 -0700261private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 void signalDelayedEvent(nsecs_t delay);
263
264 void handleConsoleEvents();
265 void handleTransaction(uint32_t transactionFlags);
Mathias Agopian3d579642009-06-04 18:46:21 -0700266 void handleTransactionLocked(
267 uint32_t transactionFlags,
268 Vector< sp<LayerBase> >& ditchedLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269
270 void computeVisibleRegions(
271 LayerVector& currentLayers,
272 Region& dirtyRegion,
273 Region& wormholeRegion);
274
275 void handlePageFlip();
276 bool lockPageFlip(const LayerVector& currentLayers);
277 void unlockPageFlip(const LayerVector& currentLayers);
278 void handleRepaint();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279 void postFramebuffer();
280 void composeSurfaces(const Region& dirty);
281 void unlockClients();
282
283
284 void destroyConnection(ClientID cid);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700285 sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const;
286 status_t addLayer_l(const sp<LayerBase>& layer);
287 status_t removeLayer_l(const sp<LayerBase>& layer);
Mathias Agopian9a112062009-04-17 19:36:26 -0700288 status_t purgatorizeLayer_l(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289 void free_resources_l();
290
291 uint32_t getTransactionFlags(uint32_t flags);
292 uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0);
293 void commitTransaction();
294
295
296 friend class FreezeLock;
297 sp<FreezeLock> getFreezeLock() const;
Mathias Agopian04087722009-12-01 17:23:28 -0800298 inline void incFreezeCount() {
299 if (mFreezeCount == 0)
300 mFreezeDisplayTime = 0;
301 mFreezeCount++;
302 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
304 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
305 inline bool isFrozen() const {
Mathias Agopian3330b202009-10-05 17:07:12 -0700306 return (mFreezeDisplay || mFreezeCount>0) && mBootFinished;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 }
308
309
310 void debugFlashRegions();
311 void debugShowFPS() const;
312 void drawWormhole() const;
313
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700314
315 mutable MessageQueue mEventQueue;
316
317
318
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 // access must be protected by mStateLock
320 mutable Mutex mStateLock;
321 State mCurrentState;
322 State mDrawingState;
323 volatile int32_t mTransactionFlags;
324 volatile int32_t mTransactionCount;
325 Condition mTransactionCV;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700326 bool mResizeTransationPending;
Mathias Agopian9a112062009-04-17 19:36:26 -0700327
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328 // protected by mStateLock (but we could use another lock)
329 Tokenizer mTokens;
Mathias Agopianf9d93272009-06-19 17:00:27 -0700330 DefaultKeyedVector<ClientID, sp<Client> > mClientsMap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700331 DefaultKeyedVector<SurfaceID, sp<LayerBaseClient> > mLayerMap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 GraphicPlane mGraphicPlanes[1];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700333 bool mLayersRemoved;
Mathias Agopianf9d93272009-06-19 17:00:27 -0700334 Vector< sp<Client> > mDisconnectedClients;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335
336 // constant members (no synchronization needed for access)
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700337 sp<IMemoryHeap> mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 surface_flinger_cblk_t* mServerCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339 GLuint mWormholeTexName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 nsecs_t mBootTime;
Mathias Agopian375f5632009-06-15 18:24:59 -0700341 Permission mHardwareTest;
342 Permission mAccessSurfaceFlinger;
343 Permission mDump;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344
345 // Can only accessed from the main thread, these members
346 // don't need synchronization
347 Region mDirtyRegion;
Mathias Agopian97011222009-07-28 10:57:27 -0700348 Region mDirtyRegionRemovedLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349 Region mInvalidRegion;
350 Region mWormholeRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 bool mVisibleRegionsDirty;
352 bool mDeferReleaseConsole;
353 bool mFreezeDisplay;
354 int32_t mFreezeCount;
355 nsecs_t mFreezeDisplayTime;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357 // don't use a lock for these, we don't care
358 int mDebugRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 int mDebugBackground;
Mathias Agopian9795c422009-08-26 16:36:26 -0700360 volatile nsecs_t mDebugInSwapBuffers;
361 nsecs_t mLastSwapBufferTime;
362 volatile nsecs_t mDebugInTransaction;
363 nsecs_t mLastTransactionTime;
Mathias Agopian3330b202009-10-05 17:07:12 -0700364 bool mBootFinished;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365
366 // these are thread safe
367 mutable Barrier mReadyToRunBarrier;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368
369 // atomic variables
370 enum {
371 eConsoleReleased = 1,
372 eConsoleAcquired = 2
373 };
374 volatile int32_t mConsoleSignals;
375
376 // only written in the main thread, only read in other threads
377 volatile int32_t mSecureFrameBuffer;
378};
379
380// ---------------------------------------------------------------------------
381
382class FreezeLock : public LightRefBase<FreezeLock> {
383 SurfaceFlinger* mFlinger;
384public:
385 FreezeLock(SurfaceFlinger* flinger)
386 : mFlinger(flinger) {
387 mFlinger->incFreezeCount();
388 }
389 ~FreezeLock() {
390 mFlinger->decFreezeCount();
391 }
392};
393
394// ---------------------------------------------------------------------------
395
396class BClient : public BnSurfaceFlingerClient
397{
398public:
399 BClient(SurfaceFlinger *flinger, ClientID cid,
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700400 const sp<IMemoryHeap>& cblk);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401 ~BClient();
402
403 // ISurfaceFlingerClient interface
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700404 virtual sp<IMemoryHeap> getControlBlock() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405
406 virtual sp<ISurface> createSurface(
407 surface_data_t* params, int pid,
408 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
409 uint32_t flags);
410
411 virtual status_t destroySurface(SurfaceID surfaceId);
412 virtual status_t setState(int32_t count, const layer_state_t* states);
413
414private:
415 ClientID mId;
416 SurfaceFlinger* mFlinger;
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700417 sp<IMemoryHeap> mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418};
419
420// ---------------------------------------------------------------------------
421}; // namespace android
422
423#endif // ANDROID_SURFACE_FLINGER_H