blob: 2558324d90d3e5f5f9e1b45d3c62c0de205aa490 [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>
Mathias Agopian9cce3252010-02-09 17:46:37 -080034#include <surfaceflinger/ISurfaceComposer.h>
35#include <surfaceflinger/ISurfaceFlingerClient.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
37#include "Barrier.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include "Layer.h"
39#include "Tokenizer.h"
40
Mathias Agopianf1d8e872009-04-20 19:39:12 -070041#include "MessageQueue.h"
42
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043struct copybit_device_t;
44struct overlay_device_t;
45
46namespace android {
47
48// ---------------------------------------------------------------------------
49
50class Client;
51class BClient;
52class DisplayHardware;
53class FreezeLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054class Layer;
55class LayerBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
57typedef int32_t ClientID;
58
59#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
60#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
61
62// ---------------------------------------------------------------------------
63
Mathias Agopianf9d93272009-06-19 17:00:27 -070064class Client : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065{
66public:
67 Client(ClientID cid, const sp<SurfaceFlinger>& flinger);
68 ~Client();
69
70 int32_t generateId(int pid);
71 void free(int32_t id);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070072 status_t bindLayer(const sp<LayerBaseClient>& layer, int32_t id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073
74 inline bool isValid(int32_t i) const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070075 sp<LayerBaseClient> getLayerUser(int32_t i) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076
Mathias Agopian7303c6b2009-07-02 18:11:53 -070077 const Vector< wp<LayerBaseClient> >& getLayers() const {
78 return mLayers;
79 }
80
81 const sp<IMemoryHeap>& getControlBlockMemory() const {
82 return mCblkHeap;
83 }
84
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 // pointer to this client's control block
Mathias Agopiancbb288b2009-09-07 16:32:45 -070086 SharedClient* ctrlblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 ClientID cid;
88
89
90private:
Mathias Agopian7303c6b2009-07-02 18:11:53 -070091 int getClientPid() const { return mPid; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092
Mathias Agopian076b1cc2009-04-10 14:24:30 -070093 int mPid;
94 uint32_t mBitmap;
95 SortedVector<uint8_t> mInUse;
96 Vector< wp<LayerBaseClient> > mLayers;
Mathias Agopian7303c6b2009-07-02 18:11:53 -070097 sp<IMemoryHeap> mCblkHeap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099};
100
101// ---------------------------------------------------------------------------
102
103class GraphicPlane
104{
105public:
106 static status_t orientationToTransfrom(int orientation, int w, int h,
107 Transform* tr);
108
109 GraphicPlane();
110 ~GraphicPlane();
111
112 bool initialized() const;
113
114 void setDisplayHardware(DisplayHardware *);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 status_t setOrientation(int orientation);
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700116 int getOrientation() const { return mOrientation; }
Mathias Agopian2b92d892010-02-08 15:49:35 -0800117 int getWidth() const;
118 int getHeight() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119
120 const DisplayHardware& displayHardware() const;
121 const Transform& transform() const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700122 EGLDisplay getEGLDisplay() const;
123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124private:
125 GraphicPlane(const GraphicPlane&);
126 GraphicPlane operator = (const GraphicPlane&);
127
128 DisplayHardware* mHw;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 Transform mGlobalTransform;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800130 Transform mDisplayTransform;
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700131 int mOrientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800132 float mDisplayWidth;
133 float mDisplayHeight;
134 int mWidth;
135 int mHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136};
137
138// ---------------------------------------------------------------------------
139
140enum {
141 eTransactionNeeded = 0x01,
142 eTraversalNeeded = 0x02
143};
144
145class SurfaceFlinger : public BnSurfaceComposer, protected Thread
146{
147public:
148 static void instantiate();
149 static void shutdown();
150
151 SurfaceFlinger();
152 virtual ~SurfaceFlinger();
153 void init();
154
155 virtual status_t onTransact(
156 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
157
158 virtual status_t dump(int fd, const Vector<String16>& args);
159
160 // ISurfaceComposer interface
161 virtual sp<ISurfaceFlingerClient> createConnection();
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700162 virtual sp<IMemoryHeap> getCblk() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 virtual void bootFinished();
164 virtual void openGlobalTransaction();
165 virtual void closeGlobalTransaction();
166 virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags);
167 virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags);
Mathias Agopianc08731e2009-03-27 18:11:38 -0700168 virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 virtual void signal() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170
171 void screenReleased(DisplayID dpy);
172 void screenAcquired(DisplayID dpy);
173
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 overlay_control_device_t* getOverlayEngine() const;
175
176
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177 status_t removeLayer(const sp<LayerBase>& layer);
178 status_t addLayer(const sp<LayerBase>& layer);
179 status_t invalidateLayerVisibility(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180
181private:
182 friend class BClient;
183 friend class LayerBase;
184 friend class LayerBuffer;
185 friend class LayerBaseClient;
Mathias Agopian1df3bbb2009-07-06 19:04:03 -0700186 friend class LayerBaseClient::Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 friend class Layer;
188 friend class LayerBlur;
Mathias Agopian945ebbf2009-06-18 18:48:39 -0700189 friend class LayerDim;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190
Mathias Agopian285dbde2010-03-01 16:09:43 -0800191 sp<ISurface> createSurface(ClientID client, int pid, const String8& name,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 ISurfaceFlingerClient::surface_data_t* params,
193 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
194 uint32_t flags);
195
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700196 sp<LayerBaseClient> createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700197 const sp<Client>& client, DisplayID display,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700198 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
199 PixelFormat& format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700201 sp<LayerBaseClient> createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700202 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
204
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700205 sp<LayerBaseClient> createDimSurfaceLocked(
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> createPushBuffersSurfaceLocked(
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 Agopian9a112062009-04-17 19:36:26 -0700213 status_t removeSurface(SurfaceID surface_id);
214 status_t destroySurface(const sp<LayerBaseClient>& layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700215 status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216
217
218 class LayerVector {
219 public:
220 inline LayerVector() { }
221 LayerVector(const LayerVector&);
222 inline size_t size() const { return layers.size(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700223 inline sp<LayerBase> const* array() const { return layers.array(); }
224 ssize_t add(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
225 ssize_t remove(const sp<LayerBase>&);
226 ssize_t reorder(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
227 ssize_t indexOf(const sp<LayerBase>& key, size_t guess=0) const;
228 inline sp<LayerBase> operator [] (size_t i) const { return layers[i]; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700230 KeyedVector< sp<LayerBase> , size_t> lookup;
231 Vector< sp<LayerBase> > layers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 };
233
234 struct State {
235 State() {
236 orientation = ISurfaceComposer::eOrientationDefault;
237 freezeDisplay = 0;
238 }
239 LayerVector layersSortedByZ;
240 uint8_t orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700241 uint8_t orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 uint8_t freezeDisplay;
243 };
244
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 virtual bool threadLoop();
246 virtual status_t readyToRun();
247 virtual void onFirstRef();
248
Andy McFadden550a1142009-10-29 10:19:34 -0700249public: // hack to work around gcc 4.0.3 bug
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 const GraphicPlane& graphicPlane(int dpy) const;
251 GraphicPlane& graphicPlane(int dpy);
Andy McFadden550a1142009-10-29 10:19:34 -0700252private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253
254 void waitForEvent();
Andy McFadden2944a2b2009-09-21 14:33:20 -0700255public: // hack to work around gcc 4.0.3 bug
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 void signalEvent();
Andy McFadden2944a2b2009-09-21 14:33:20 -0700257private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 void handleConsoleEvents();
259 void handleTransaction(uint32_t transactionFlags);
Mathias Agopian3d579642009-06-04 18:46:21 -0700260 void handleTransactionLocked(
261 uint32_t transactionFlags,
262 Vector< sp<LayerBase> >& ditchedLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263
264 void computeVisibleRegions(
265 LayerVector& currentLayers,
266 Region& dirtyRegion,
267 Region& wormholeRegion);
268
269 void handlePageFlip();
270 bool lockPageFlip(const LayerVector& currentLayers);
271 void unlockPageFlip(const LayerVector& currentLayers);
272 void handleRepaint();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 void postFramebuffer();
274 void composeSurfaces(const Region& dirty);
275 void unlockClients();
276
277
278 void destroyConnection(ClientID cid);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700279 sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const;
280 status_t addLayer_l(const sp<LayerBase>& layer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700281 status_t addClientLayer_l(const sp<LayerBaseClient>& lbc);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700282 status_t removeLayer_l(const sp<LayerBase>& layer);
Mathias Agopian9a112062009-04-17 19:36:26 -0700283 status_t purgatorizeLayer_l(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284 void free_resources_l();
285
286 uint32_t getTransactionFlags(uint32_t flags);
Mathias Agopianbb641242010-05-18 17:06:55 -0700287 uint32_t setTransactionFlags(uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288 void commitTransaction();
289
290
291 friend class FreezeLock;
292 sp<FreezeLock> getFreezeLock() const;
Mathias Agopian04087722009-12-01 17:23:28 -0800293 inline void incFreezeCount() {
294 if (mFreezeCount == 0)
295 mFreezeDisplayTime = 0;
296 mFreezeCount++;
297 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
299 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
300 inline bool isFrozen() const {
Mathias Agopian3330b202009-10-05 17:07:12 -0700301 return (mFreezeDisplay || mFreezeCount>0) && mBootFinished;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 }
303
304
305 void debugFlashRegions();
306 void debugShowFPS() const;
307 void drawWormhole() const;
308
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700309
310 mutable MessageQueue mEventQueue;
Mathias Agopianbb641242010-05-18 17:06:55 -0700311
312 status_t postMessageAsync(const sp<MessageBase>& msg,
313 nsecs_t reltime=0, uint32_t flags = 0);
314
315 status_t postMessageSync(const sp<MessageBase>& msg,
316 nsecs_t reltime=0, uint32_t flags = 0);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700317
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(
Mathias Agopian285dbde2010-03-01 16:09:43 -0800407 surface_data_t* params, int pid, const String8& name,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408 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