blob: a21e18644be1ab4a59704b50e63c7cea6c9d169d [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 Agopianc5b2c0b2009-05-19 19:08:10 -070028#include <binder/MemoryDealer.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <ui/PixelFormat.h>
32#include <ui/ISurfaceComposer.h>
33#include <ui/ISurfaceFlingerClient.h>
34
35#include <private/ui/SharedState.h>
36#include <private/ui/LayerState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
38#include "Barrier.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include "Layer.h"
40#include "Tokenizer.h"
41
Mathias Agopianf1d8e872009-04-20 19:39:12 -070042#include "MessageQueue.h"
43
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044struct copybit_device_t;
45struct overlay_device_t;
46
47namespace android {
48
49// ---------------------------------------------------------------------------
50
51class Client;
52class BClient;
53class DisplayHardware;
54class FreezeLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055class Layer;
56class LayerBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
58typedef int32_t ClientID;
59
60#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
61#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
62
63// ---------------------------------------------------------------------------
64
65class Client
66{
67public:
68 Client(ClientID cid, const sp<SurfaceFlinger>& flinger);
69 ~Client();
70
71 int32_t generateId(int pid);
72 void free(int32_t id);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070073 status_t bindLayer(const sp<LayerBaseClient>& layer, int32_t id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074
75 inline bool isValid(int32_t i) const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070076 sp<LayerBaseClient> getLayerUser(int32_t i) const;
77 const Vector< wp<LayerBaseClient> >& getLayers() const { return mLayers; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 const sp<IMemory>& controlBlockMemory() const { return mCblkMemory; }
79 void dump(const char* what);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080
81 // pointer to this client's control block
82 per_client_cblk_t* ctrlblk;
83 ClientID cid;
84
85
86private:
87 int getClientPid() const { return mPid; }
88
Mathias Agopian076b1cc2009-04-10 14:24:30 -070089 int mPid;
90 uint32_t mBitmap;
91 SortedVector<uint8_t> mInUse;
92 Vector< wp<LayerBaseClient> > mLayers;
93 sp<MemoryDealer> mCblkHeap;
94 sp<SurfaceFlinger> mFlinger;
95 sp<IMemory> mCblkMemory;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096};
97
98// ---------------------------------------------------------------------------
99
100class GraphicPlane
101{
102public:
103 static status_t orientationToTransfrom(int orientation, int w, int h,
104 Transform* tr);
105
106 GraphicPlane();
107 ~GraphicPlane();
108
109 bool initialized() const;
110
111 void setDisplayHardware(DisplayHardware *);
112 void setTransform(const Transform& tr);
113 status_t setOrientation(int orientation);
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700114 int getOrientation() const { return mOrientation; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115
116 const DisplayHardware& displayHardware() const;
117 const Transform& transform() const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700118 EGLDisplay getEGLDisplay() const;
119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120private:
121 GraphicPlane(const GraphicPlane&);
122 GraphicPlane operator = (const GraphicPlane&);
123
124 DisplayHardware* mHw;
125 Transform mTransform;
126 Transform mOrientationTransform;
127 Transform mGlobalTransform;
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700128 int mOrientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129};
130
131// ---------------------------------------------------------------------------
132
133enum {
134 eTransactionNeeded = 0x01,
135 eTraversalNeeded = 0x02
136};
137
138class SurfaceFlinger : public BnSurfaceComposer, protected Thread
139{
140public:
141 static void instantiate();
142 static void shutdown();
143
144 SurfaceFlinger();
145 virtual ~SurfaceFlinger();
146 void init();
147
148 virtual status_t onTransact(
149 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
150
151 virtual status_t dump(int fd, const Vector<String16>& args);
152
153 // ISurfaceComposer interface
154 virtual sp<ISurfaceFlingerClient> createConnection();
155 virtual sp<IMemory> getCblk() const;
156 virtual void bootFinished();
157 virtual void openGlobalTransaction();
158 virtual void closeGlobalTransaction();
159 virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags);
160 virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags);
Mathias Agopianc08731e2009-03-27 18:11:38 -0700161 virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 virtual void signal() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163
164 void screenReleased(DisplayID dpy);
165 void screenAcquired(DisplayID dpy);
166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 overlay_control_device_t* getOverlayEngine() const;
168
169
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700170 status_t removeLayer(const sp<LayerBase>& layer);
171 status_t addLayer(const sp<LayerBase>& layer);
172 status_t invalidateLayerVisibility(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173
174private:
175 friend class BClient;
176 friend class LayerBase;
177 friend class LayerBuffer;
178 friend class LayerBaseClient;
179 friend class Layer;
180 friend class LayerBlur;
181
182 sp<ISurface> createSurface(ClientID client, int pid,
183 ISurfaceFlingerClient::surface_data_t* params,
184 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
185 uint32_t flags);
186
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700187 sp<LayerBaseClient> createNormalSurfaceLocked(
188 Client* client, DisplayID display,
189 int32_t id, uint32_t w, uint32_t h,
190 PixelFormat format, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700192 sp<LayerBaseClient> createBlurSurfaceLocked(
193 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
195
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700196 sp<LayerBaseClient> createDimSurfaceLocked(
197 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
199
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 sp<LayerBaseClient> createPushBuffersSurfaceLocked(
201 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
203
Mathias Agopian9a112062009-04-17 19:36:26 -0700204 status_t removeSurface(SurfaceID surface_id);
205 status_t destroySurface(const sp<LayerBaseClient>& layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700206 status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207
208
209 class LayerVector {
210 public:
211 inline LayerVector() { }
212 LayerVector(const LayerVector&);
213 inline size_t size() const { return layers.size(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700214 inline sp<LayerBase> const* array() const { return layers.array(); }
215 ssize_t add(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
216 ssize_t remove(const sp<LayerBase>&);
217 ssize_t reorder(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
218 ssize_t indexOf(const sp<LayerBase>& key, size_t guess=0) const;
219 inline sp<LayerBase> operator [] (size_t i) const { return layers[i]; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700221 KeyedVector< sp<LayerBase> , size_t> lookup;
222 Vector< sp<LayerBase> > layers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223 };
224
225 struct State {
226 State() {
227 orientation = ISurfaceComposer::eOrientationDefault;
228 freezeDisplay = 0;
229 }
230 LayerVector layersSortedByZ;
231 uint8_t orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700232 uint8_t orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 uint8_t freezeDisplay;
234 };
235
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 virtual bool threadLoop();
237 virtual status_t readyToRun();
238 virtual void onFirstRef();
239
240 const GraphicPlane& graphicPlane(int dpy) const;
241 GraphicPlane& graphicPlane(int dpy);
242
243 void waitForEvent();
244 void signalEvent();
245 void signalDelayedEvent(nsecs_t delay);
246
247 void handleConsoleEvents();
248 void handleTransaction(uint32_t transactionFlags);
Mathias Agopian3d579642009-06-04 18:46:21 -0700249 void handleTransactionLocked(
250 uint32_t transactionFlags,
251 Vector< sp<LayerBase> >& ditchedLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252
253 void computeVisibleRegions(
254 LayerVector& currentLayers,
255 Region& dirtyRegion,
256 Region& wormholeRegion);
257
258 void handlePageFlip();
259 bool lockPageFlip(const LayerVector& currentLayers);
260 void unlockPageFlip(const LayerVector& currentLayers);
261 void handleRepaint();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 void scheduleBroadcast(Client* client);
263 void executeScheduledBroadcasts();
264 void postFramebuffer();
265 void composeSurfaces(const Region& dirty);
266 void unlockClients();
267
268
269 void destroyConnection(ClientID cid);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700270 sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const;
271 status_t addLayer_l(const sp<LayerBase>& layer);
272 status_t removeLayer_l(const sp<LayerBase>& layer);
Mathias Agopian9a112062009-04-17 19:36:26 -0700273 status_t purgatorizeLayer_l(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 void free_resources_l();
275
276 uint32_t getTransactionFlags(uint32_t flags);
277 uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0);
278 void commitTransaction();
279
280
281 friend class FreezeLock;
282 sp<FreezeLock> getFreezeLock() const;
283 inline void incFreezeCount() { mFreezeCount++; }
284 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
285 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
286 inline bool isFrozen() const {
287 return mFreezeDisplay || mFreezeCount>0;
288 }
289
290
291 void debugFlashRegions();
292 void debugShowFPS() const;
293 void drawWormhole() const;
294
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700295
296 mutable MessageQueue mEventQueue;
297
298
299
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 // access must be protected by mStateLock
301 mutable Mutex mStateLock;
302 State mCurrentState;
303 State mDrawingState;
304 volatile int32_t mTransactionFlags;
305 volatile int32_t mTransactionCount;
306 Condition mTransactionCV;
Mathias Agopian9a112062009-04-17 19:36:26 -0700307 SortedVector< sp<LayerBase> > mLayerPurgatory;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308
Mathias Agopian9a112062009-04-17 19:36:26 -0700309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 // protected by mStateLock (but we could use another lock)
311 Tokenizer mTokens;
312 DefaultKeyedVector<ClientID, Client*> mClientsMap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700313 DefaultKeyedVector<SurfaceID, sp<LayerBaseClient> > mLayerMap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 GraphicPlane mGraphicPlanes[1];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700315 bool mLayersRemoved;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 Vector<Client*> mDisconnectedClients;
317
318 // constant members (no synchronization needed for access)
319 sp<MemoryDealer> mServerHeap;
320 sp<IMemory> mServerCblkMemory;
321 surface_flinger_cblk_t* mServerCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 GLuint mWormholeTexName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 nsecs_t mBootTime;
324
325 // Can only accessed from the main thread, these members
326 // don't need synchronization
327 Region mDirtyRegion;
328 Region mInvalidRegion;
329 Region mWormholeRegion;
330 Client* mLastScheduledBroadcast;
331 SortedVector<Client*> mScheduledBroadcasts;
332 bool mVisibleRegionsDirty;
333 bool mDeferReleaseConsole;
334 bool mFreezeDisplay;
335 int32_t mFreezeCount;
336 nsecs_t mFreezeDisplayTime;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 // don't use a lock for these, we don't care
339 int mDebugRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 int mDebugBackground;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341
342 // these are thread safe
343 mutable Barrier mReadyToRunBarrier;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344
345 // atomic variables
346 enum {
347 eConsoleReleased = 1,
348 eConsoleAcquired = 2
349 };
350 volatile int32_t mConsoleSignals;
351
352 // only written in the main thread, only read in other threads
353 volatile int32_t mSecureFrameBuffer;
354};
355
356// ---------------------------------------------------------------------------
357
358class FreezeLock : public LightRefBase<FreezeLock> {
359 SurfaceFlinger* mFlinger;
360public:
361 FreezeLock(SurfaceFlinger* flinger)
362 : mFlinger(flinger) {
363 mFlinger->incFreezeCount();
364 }
365 ~FreezeLock() {
366 mFlinger->decFreezeCount();
367 }
368};
369
370// ---------------------------------------------------------------------------
371
372class BClient : public BnSurfaceFlingerClient
373{
374public:
375 BClient(SurfaceFlinger *flinger, ClientID cid,
376 const sp<IMemory>& cblk);
377 ~BClient();
378
379 // ISurfaceFlingerClient interface
380 virtual void getControlBlocks(sp<IMemory>* ctrl) const;
381
382 virtual sp<ISurface> createSurface(
383 surface_data_t* params, int pid,
384 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
385 uint32_t flags);
386
387 virtual status_t destroySurface(SurfaceID surfaceId);
388 virtual status_t setState(int32_t count, const layer_state_t* states);
389
390private:
391 ClientID mId;
392 SurfaceFlinger* mFlinger;
393 sp<IMemory> mCblk;
394};
395
396// ---------------------------------------------------------------------------
397}; // namespace android
398
399#endif // ANDROID_SURFACE_FLINGER_H