blob: 84134e0f6792c268592376d527ef9289f1d8e401 [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>
Mathias Agopian7e27f052010-05-28 14:22:23 -070035#include <surfaceflinger/ISurfaceComposerClient.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"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
Mathias Agopianf1d8e872009-04-20 19:39:12 -070040#include "MessageQueue.h"
41
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042struct copybit_device_t;
43struct overlay_device_t;
44
45namespace android {
46
47// ---------------------------------------------------------------------------
48
49class Client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050class DisplayHardware;
51class FreezeLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052class Layer;
53class LayerBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
56#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
57
58// ---------------------------------------------------------------------------
59
Mathias Agopian96f08192010-06-02 23:28:45 -070060class Client : public BnSurfaceComposerClient
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061{
62public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063 // pointer to this client's control block
Mathias Agopian96f08192010-06-02 23:28:45 -070064 SharedClient* ctrlblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
Mathias Agopian96f08192010-06-02 23:28:45 -070066public:
67 Client(const sp<SurfaceFlinger>& flinger);
68 ~Client();
69
70 status_t initCheck() const;
71
72 // protected by SurfaceFlinger::mStateLock
73 ssize_t attachLayer(const sp<LayerBaseClient>& layer);
74 sp<LayerBaseClient> getLayerUser(int32_t i) const;
75 void free(LayerBaseClient const* layer);
76
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077private:
Mathias Agopian96f08192010-06-02 23:28:45 -070078
79 // ISurfaceComposerClient interface
80 virtual sp<IMemoryHeap> getControlBlock() const;
81 virtual sp<ISurface> createSurface(
82 surface_data_t* params, int pid, const String8& name,
83 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
84 uint32_t flags);
85 virtual status_t destroySurface(SurfaceID surfaceId);
86 virtual status_t setState(int32_t count, const layer_state_t* states);
87
88 uint32_t mBitmap;
89 DefaultKeyedVector< size_t, wp<LayerBaseClient> > mLayers;
90 sp<IMemoryHeap> mCblkHeap;
91 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092};
93
94// ---------------------------------------------------------------------------
95
96class GraphicPlane
97{
98public:
99 static status_t orientationToTransfrom(int orientation, int w, int h,
100 Transform* tr);
101
102 GraphicPlane();
103 ~GraphicPlane();
104
105 bool initialized() const;
106
107 void setDisplayHardware(DisplayHardware *);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 status_t setOrientation(int orientation);
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700109 int getOrientation() const { return mOrientation; }
Mathias Agopian2b92d892010-02-08 15:49:35 -0800110 int getWidth() const;
111 int getHeight() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112
113 const DisplayHardware& displayHardware() const;
114 const Transform& transform() const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700115 EGLDisplay getEGLDisplay() const;
116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117private:
118 GraphicPlane(const GraphicPlane&);
119 GraphicPlane operator = (const GraphicPlane&);
120
121 DisplayHardware* mHw;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 Transform mGlobalTransform;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800123 Transform mDisplayTransform;
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700124 int mOrientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800125 float mDisplayWidth;
126 float mDisplayHeight;
127 int mWidth;
128 int mHeight;
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
Mathias Agopian7e27f052010-05-28 14:22:23 -0700154 virtual sp<ISurfaceComposerClient> createConnection();
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700155 virtual sp<IMemoryHeap> getCblk() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 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:
Mathias Agopian96f08192010-06-02 23:28:45 -0700175 friend class Client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 friend class LayerBase;
177 friend class LayerBuffer;
178 friend class LayerBaseClient;
Mathias Agopian1df3bbb2009-07-06 19:04:03 -0700179 friend class LayerBaseClient::Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 friend class Layer;
181 friend class LayerBlur;
Mathias Agopian945ebbf2009-06-18 18:48:39 -0700182 friend class LayerDim;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183
Mathias Agopian96f08192010-06-02 23:28:45 -0700184 sp<ISurface> createSurface(const sp<Client>& client,
185 int pid, const String8& name,
Mathias Agopian7e27f052010-05-28 14:22:23 -0700186 ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
188 uint32_t flags);
189
Mathias Agopian96f08192010-06-02 23:28:45 -0700190 sp<LayerBaseClient> createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700191 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -0700192 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700193 PixelFormat& format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194
Mathias Agopian96f08192010-06-02 23:28:45 -0700195 sp<LayerBaseClient> createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700196 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -0700197 uint32_t w, uint32_t h, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198
Mathias Agopian96f08192010-06-02 23:28:45 -0700199 sp<LayerBaseClient> createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700200 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -0700201 uint32_t w, uint32_t h, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202
Mathias Agopian96f08192010-06-02 23:28:45 -0700203 sp<LayerBaseClient> createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700204 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -0700205 uint32_t w, uint32_t h, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206
Mathias Agopian96f08192010-06-02 23:28:45 -0700207 status_t removeSurface(const sp<Client>& client, SurfaceID sid);
Mathias Agopian9a112062009-04-17 19:36:26 -0700208 status_t destroySurface(const sp<LayerBaseClient>& layer);
Mathias Agopian96f08192010-06-02 23:28:45 -0700209 status_t setClientState(const sp<Client>& client,
210 int32_t count, const layer_state_t* states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211
212
213 class LayerVector {
214 public:
215 inline LayerVector() { }
216 LayerVector(const LayerVector&);
217 inline size_t size() const { return layers.size(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700218 inline sp<LayerBase> const* array() const { return layers.array(); }
219 ssize_t add(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
220 ssize_t remove(const sp<LayerBase>&);
221 ssize_t reorder(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
222 ssize_t indexOf(const sp<LayerBase>& key, size_t guess=0) const;
223 inline sp<LayerBase> operator [] (size_t i) const { return layers[i]; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700225 KeyedVector< sp<LayerBase> , size_t> lookup;
226 Vector< sp<LayerBase> > layers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 };
228
229 struct State {
230 State() {
231 orientation = ISurfaceComposer::eOrientationDefault;
232 freezeDisplay = 0;
233 }
234 LayerVector layersSortedByZ;
235 uint8_t orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700236 uint8_t orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 uint8_t freezeDisplay;
238 };
239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 virtual bool threadLoop();
241 virtual status_t readyToRun();
242 virtual void onFirstRef();
243
Andy McFadden550a1142009-10-29 10:19:34 -0700244public: // hack to work around gcc 4.0.3 bug
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 const GraphicPlane& graphicPlane(int dpy) const;
246 GraphicPlane& graphicPlane(int dpy);
Andy McFadden550a1142009-10-29 10:19:34 -0700247private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248
249 void waitForEvent();
Andy McFadden2944a2b2009-09-21 14:33:20 -0700250public: // hack to work around gcc 4.0.3 bug
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 void signalEvent();
Andy McFadden2944a2b2009-09-21 14:33:20 -0700252private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 void handleConsoleEvents();
254 void handleTransaction(uint32_t transactionFlags);
Mathias Agopian3d579642009-06-04 18:46:21 -0700255 void handleTransactionLocked(
256 uint32_t transactionFlags,
257 Vector< sp<LayerBase> >& ditchedLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258
259 void computeVisibleRegions(
260 LayerVector& currentLayers,
261 Region& dirtyRegion,
262 Region& wormholeRegion);
263
264 void handlePageFlip();
265 bool lockPageFlip(const LayerVector& currentLayers);
266 void unlockPageFlip(const LayerVector& currentLayers);
267 void handleRepaint();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 void postFramebuffer();
269 void composeSurfaces(const Region& dirty);
270 void unlockClients();
271
272
Mathias Agopian96f08192010-06-02 23:28:45 -0700273 ssize_t addClientLayer(const sp<Client>& client,
274 const sp<LayerBaseClient>& lbc);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700275 status_t addLayer_l(const sp<LayerBase>& layer);
276 status_t removeLayer_l(const sp<LayerBase>& layer);
Mathias Agopian9a112062009-04-17 19:36:26 -0700277 status_t purgatorizeLayer_l(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278
279 uint32_t getTransactionFlags(uint32_t flags);
Mathias Agopianbb641242010-05-18 17:06:55 -0700280 uint32_t setTransactionFlags(uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281 void commitTransaction();
282
283
284 friend class FreezeLock;
285 sp<FreezeLock> getFreezeLock() const;
Mathias Agopian04087722009-12-01 17:23:28 -0800286 inline void incFreezeCount() {
287 if (mFreezeCount == 0)
288 mFreezeDisplayTime = 0;
289 mFreezeCount++;
290 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
292 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
293 inline bool isFrozen() const {
Mathias Agopian3330b202009-10-05 17:07:12 -0700294 return (mFreezeDisplay || mFreezeCount>0) && mBootFinished;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 }
296
297
298 void debugFlashRegions();
299 void debugShowFPS() const;
300 void drawWormhole() const;
301
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700302
303 mutable MessageQueue mEventQueue;
Mathias Agopianbb641242010-05-18 17:06:55 -0700304
305 status_t postMessageAsync(const sp<MessageBase>& msg,
306 nsecs_t reltime=0, uint32_t flags = 0);
307
308 status_t postMessageSync(const sp<MessageBase>& msg,
309 nsecs_t reltime=0, uint32_t flags = 0);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700310
311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 // access must be protected by mStateLock
313 mutable Mutex mStateLock;
314 State mCurrentState;
315 State mDrawingState;
316 volatile int32_t mTransactionFlags;
317 volatile int32_t mTransactionCount;
318 Condition mTransactionCV;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700319 bool mResizeTransationPending;
Mathias Agopian96f08192010-06-02 23:28:45 -0700320
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321 // protected by mStateLock (but we could use another lock)
Mathias Agopian96f08192010-06-02 23:28:45 -0700322 GraphicPlane mGraphicPlanes[1];
323 bool mLayersRemoved;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324
325 // constant members (no synchronization needed for access)
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700326 sp<IMemoryHeap> mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 surface_flinger_cblk_t* mServerCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328 GLuint mWormholeTexName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329 nsecs_t mBootTime;
Mathias Agopian375f5632009-06-15 18:24:59 -0700330 Permission mHardwareTest;
331 Permission mAccessSurfaceFlinger;
332 Permission mDump;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333
334 // Can only accessed from the main thread, these members
335 // don't need synchronization
336 Region mDirtyRegion;
Mathias Agopian97011222009-07-28 10:57:27 -0700337 Region mDirtyRegionRemovedLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 Region mInvalidRegion;
339 Region mWormholeRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 bool mVisibleRegionsDirty;
341 bool mDeferReleaseConsole;
342 bool mFreezeDisplay;
343 int32_t mFreezeCount;
344 nsecs_t mFreezeDisplayTime;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 // don't use a lock for these, we don't care
347 int mDebugRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 int mDebugBackground;
Mathias Agopian9795c422009-08-26 16:36:26 -0700349 volatile nsecs_t mDebugInSwapBuffers;
350 nsecs_t mLastSwapBufferTime;
351 volatile nsecs_t mDebugInTransaction;
352 nsecs_t mLastTransactionTime;
Mathias Agopian3330b202009-10-05 17:07:12 -0700353 bool mBootFinished;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354
355 // these are thread safe
356 mutable Barrier mReadyToRunBarrier;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357
358 // atomic variables
359 enum {
360 eConsoleReleased = 1,
361 eConsoleAcquired = 2
362 };
363 volatile int32_t mConsoleSignals;
364
365 // only written in the main thread, only read in other threads
366 volatile int32_t mSecureFrameBuffer;
367};
368
369// ---------------------------------------------------------------------------
370
371class FreezeLock : public LightRefBase<FreezeLock> {
372 SurfaceFlinger* mFlinger;
373public:
374 FreezeLock(SurfaceFlinger* flinger)
375 : mFlinger(flinger) {
376 mFlinger->incFreezeCount();
377 }
378 ~FreezeLock() {
379 mFlinger->decFreezeCount();
380 }
381};
382
383// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384}; // namespace android
385
386#endif // ANDROID_SURFACE_FLINGER_H