blob: edfa78a35fa2fb86d508fdaae13b6368fa62c8e7 [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#define LOG_TAG "SurfaceComposerClient"
18
19#include <stdint.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <utils/Errors.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/Log.h>
Mathias Agopiand4784a32010-05-27 19:41:15 -070024#include <utils/Singleton.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070025#include <utils/SortedVector.h>
26#include <utils/String8.h>
27#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
Mathias Agopian9cce3252010-02-09 17:46:37 -080029#include <binder/IMemory.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <binder/IServiceManager.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080031
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <ui/DisplayInfo.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
Mathias Agopiane3c697f2013-02-14 17:11:02 -080034#include <gui/IGraphicBufferProducer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080035#include <gui/ISurface.h>
36#include <gui/ISurfaceComposer.h>
37#include <gui/ISurfaceComposerClient.h>
38#include <gui/SurfaceComposerClient.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
Mathias Agopian41f673c2011-11-17 17:48:35 -080040#include <private/gui/ComposerService.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080041#include <private/gui/LayerState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044// ---------------------------------------------------------------------------
45
Mathias Agopian7e27f052010-05-28 14:22:23 -070046ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
47
Mathias Agopianb7e930d2010-06-01 15:12:58 -070048ComposerService::ComposerService()
49: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-06 18:45:56 -070050 Mutex::Autolock _l(mLock);
51 connectLocked();
52}
53
54void ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -070055 const String16 name("SurfaceFlinger");
56 while (getService(name, &mComposerService) != NO_ERROR) {
57 usleep(250000);
58 }
Andy McFadden6652b3e2012-09-06 18:45:56 -070059 assert(mComposerService != NULL);
60
61 // Create the death listener.
62 class DeathObserver : public IBinder::DeathRecipient {
63 ComposerService& mComposerService;
64 virtual void binderDied(const wp<IBinder>& who) {
65 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
66 who.unsafe_get());
67 mComposerService.composerServiceDied();
68 }
69 public:
70 DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
71 };
72
73 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
74 mComposerService->asBinder()->linkToDeath(mDeathObserver);
Mathias Agopianb7e930d2010-06-01 15:12:58 -070075}
76
Andy McFadden6652b3e2012-09-06 18:45:56 -070077/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
78 ComposerService& instance = ComposerService::getInstance();
79 Mutex::Autolock _l(instance.mLock);
80 if (instance.mComposerService == NULL) {
81 ComposerService::getInstance().connectLocked();
82 assert(instance.mComposerService != NULL);
83 ALOGD("ComposerService reconnected");
84 }
85 return instance.mComposerService;
86}
87
88void ComposerService::composerServiceDied()
89{
90 Mutex::Autolock _l(mLock);
91 mComposerService = NULL;
92 mDeathObserver = NULL;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070093}
94
Mathias Agopian7e27f052010-05-28 14:22:23 -070095// ---------------------------------------------------------------------------
96
Mathias Agopian698c0872011-06-28 19:09:31 -070097static inline
Mathias Agopiane57f2922012-08-09 16:29:12 -070098int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
Mathias Agopian698c0872011-06-28 19:09:31 -070099 if (lhs.client < rhs.client) return -1;
100 if (lhs.client > rhs.client) return 1;
101 if (lhs.state.surface < rhs.state.surface) return -1;
102 if (lhs.state.surface > rhs.state.surface) return 1;
103 return 0;
104}
105
Mathias Agopiane57f2922012-08-09 16:29:12 -0700106static inline
107int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
108 return compare_type(lhs.token, rhs.token);
109}
110
Mathias Agopian7e27f052010-05-28 14:22:23 -0700111class Composer : public Singleton<Composer>
112{
Mathias Agopiand4784a32010-05-27 19:41:15 -0700113 friend class Singleton<Composer>;
114
Mathias Agopian698c0872011-06-28 19:09:31 -0700115 mutable Mutex mLock;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700116 SortedVector<ComposerState> mComposerStates;
117 SortedVector<DisplayState > mDisplayStates;
Jamie Gennis28378392011-10-12 17:39:00 -0700118 uint32_t mForceSynchronous;
Jeff Brownf3f7db62012-08-31 02:18:38 -0700119 uint32_t mTransactionNestCount;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700120 bool mAnimation;
Mathias Agopian698c0872011-06-28 19:09:31 -0700121
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700122 Composer() : Singleton<Composer>(),
Jeff Brownf3f7db62012-08-31 02:18:38 -0700123 mForceSynchronous(0), mTransactionNestCount(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700124 mAnimation(false)
Jamie Gennis28378392011-10-12 17:39:00 -0700125 { }
Mathias Agopian698c0872011-06-28 19:09:31 -0700126
Jeff Brownf3f7db62012-08-31 02:18:38 -0700127 void openGlobalTransactionImpl();
Jamie Gennis28378392011-10-12 17:39:00 -0700128 void closeGlobalTransactionImpl(bool synchronous);
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700129 void setAnimationTransactionImpl();
Mathias Agopian698c0872011-06-28 19:09:31 -0700130
131 layer_state_t* getLayerStateLocked(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800132 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700133
Mathias Agopiane57f2922012-08-09 16:29:12 -0700134 DisplayState& getDisplayStateLocked(const sp<IBinder>& token);
135
Mathias Agopiand4784a32010-05-27 19:41:15 -0700136public:
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700137 sp<IBinder> createDisplay(const String8& displayName, bool secure);
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700138 sp<IBinder> getBuiltInDisplay(int32_t id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700139
Mathias Agopianac9fa422013-02-11 16:40:36 -0800140 status_t setPosition(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700141 float x, float y);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800142 status_t setSize(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700143 uint32_t w, uint32_t h);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800144 status_t setLayer(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700145 int32_t z);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800146 status_t setFlags(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700147 uint32_t flags, uint32_t mask);
148 status_t setTransparentRegionHint(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800149 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700150 const Region& transparentRegion);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800151 status_t setAlpha(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700152 float alpha);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800153 status_t setMatrix(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700154 float dsdx, float dtdx, float dsdy, float dtdy);
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700155 status_t setOrientation(int orientation);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800156 status_t setCrop(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700157 const Rect& crop);
Mathias Agopian87855782012-07-24 21:41:09 -0700158 status_t setLayerStack(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800159 const sp<IBinder>& id, uint32_t layerStack);
Mathias Agopian698c0872011-06-28 19:09:31 -0700160
Andy McFadden2adaf042012-12-18 09:49:45 -0800161 void setDisplaySurface(const sp<IBinder>& token,
162 const sp<IGraphicBufferProducer>& bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700163 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700164 void setDisplayProjection(const sp<IBinder>& token,
165 uint32_t orientation,
166 const Rect& layerStackRect,
167 const Rect& displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700168
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700169 static void setAnimationTransaction() {
170 Composer::getInstance().setAnimationTransactionImpl();
171 }
172
Jeff Brownf3f7db62012-08-31 02:18:38 -0700173 static void openGlobalTransaction() {
174 Composer::getInstance().openGlobalTransactionImpl();
175 }
176
Jamie Gennis28378392011-10-12 17:39:00 -0700177 static void closeGlobalTransaction(bool synchronous) {
178 Composer::getInstance().closeGlobalTransactionImpl(synchronous);
Mathias Agopiand4784a32010-05-27 19:41:15 -0700179 }
180};
181
182ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184// ---------------------------------------------------------------------------
185
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700186sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
187 return ComposerService::getComposerService()->createDisplay(displayName,
188 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700189}
190
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700191sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
192 return ComposerService::getComposerService()->getBuiltInDisplay(id);
193}
194
Jeff Brownf3f7db62012-08-31 02:18:38 -0700195void Composer::openGlobalTransactionImpl() {
196 { // scope for the lock
197 Mutex::Autolock _l(mLock);
198 mTransactionNestCount += 1;
199 }
200}
201
Jamie Gennis28378392011-10-12 17:39:00 -0700202void Composer::closeGlobalTransactionImpl(bool synchronous) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700203 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopian698c0872011-06-28 19:09:31 -0700204
205 Vector<ComposerState> transaction;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700206 Vector<DisplayState> displayTransaction;
Jamie Gennis28378392011-10-12 17:39:00 -0700207 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -0700208
209 { // scope for the lock
210 Mutex::Autolock _l(mLock);
Jeff Brownf3f7db62012-08-31 02:18:38 -0700211 mForceSynchronous |= synchronous;
212 if (!mTransactionNestCount) {
213 ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
214 "call to openGlobalTransaction().");
215 } else if (--mTransactionNestCount) {
216 return;
217 }
218
Mathias Agopiane57f2922012-08-09 16:29:12 -0700219 transaction = mComposerStates;
220 mComposerStates.clear();
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700221
Mathias Agopiane57f2922012-08-09 16:29:12 -0700222 displayTransaction = mDisplayStates;
223 mDisplayStates.clear();
Jamie Gennis28378392011-10-12 17:39:00 -0700224
Jeff Brownf3f7db62012-08-31 02:18:38 -0700225 if (mForceSynchronous) {
Jamie Gennis28378392011-10-12 17:39:00 -0700226 flags |= ISurfaceComposer::eSynchronous;
227 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700228 if (mAnimation) {
229 flags |= ISurfaceComposer::eAnimation;
230 }
231
Jamie Gennis28378392011-10-12 17:39:00 -0700232 mForceSynchronous = false;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700233 mAnimation = false;
Mathias Agopian698c0872011-06-28 19:09:31 -0700234 }
235
Mathias Agopian8b33f032012-07-24 20:43:54 -0700236 sm->setTransactionState(transaction, displayTransaction, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237}
238
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700239void Composer::setAnimationTransactionImpl() {
240 Mutex::Autolock _l(mLock);
241 mAnimation = true;
242}
243
Mathias Agopian698c0872011-06-28 19:09:31 -0700244layer_state_t* Composer::getLayerStateLocked(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800245 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700246
247 ComposerState s;
248 s.client = client->mClient;
249 s.state.surface = id;
250
Mathias Agopiane57f2922012-08-09 16:29:12 -0700251 ssize_t index = mComposerStates.indexOf(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700252 if (index < 0) {
253 // we don't have it, add an initialized layer_state to our list
Mathias Agopiane57f2922012-08-09 16:29:12 -0700254 index = mComposerStates.add(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700255 }
256
Mathias Agopiane57f2922012-08-09 16:29:12 -0700257 ComposerState* const out = mComposerStates.editArray();
Mathias Agopian698c0872011-06-28 19:09:31 -0700258 return &(out[index].state);
259}
260
261status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800262 const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700263 Mutex::Autolock _l(mLock);
264 layer_state_t* s = getLayerStateLocked(client, id);
265 if (!s)
266 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700267 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700268 s->x = x;
269 s->y = y;
270 return NO_ERROR;
271}
272
273status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800274 const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700275 Mutex::Autolock _l(mLock);
276 layer_state_t* s = getLayerStateLocked(client, id);
277 if (!s)
278 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700279 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700280 s->w = w;
281 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700282
283 // Resizing a surface makes the transaction synchronous.
284 mForceSynchronous = true;
285
Mathias Agopian698c0872011-06-28 19:09:31 -0700286 return NO_ERROR;
287}
288
289status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800290 const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700291 Mutex::Autolock _l(mLock);
292 layer_state_t* s = getLayerStateLocked(client, id);
293 if (!s)
294 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700295 s->what |= layer_state_t::eLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700296 s->z = z;
297 return NO_ERROR;
298}
299
300status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800301 const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700302 uint32_t mask) {
303 Mutex::Autolock _l(mLock);
304 layer_state_t* s = getLayerStateLocked(client, id);
305 if (!s)
306 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700307 s->what |= layer_state_t::eVisibilityChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700308 s->flags &= ~mask;
309 s->flags |= (flags & mask);
310 s->mask |= mask;
311 return NO_ERROR;
312}
313
314status_t Composer::setTransparentRegionHint(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800315 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700316 const Region& transparentRegion) {
317 Mutex::Autolock _l(mLock);
318 layer_state_t* s = getLayerStateLocked(client, id);
319 if (!s)
320 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700321 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700322 s->transparentRegion = transparentRegion;
323 return NO_ERROR;
324}
325
326status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800327 const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700328 Mutex::Autolock _l(mLock);
329 layer_state_t* s = getLayerStateLocked(client, id);
330 if (!s)
331 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700332 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700333 s->alpha = alpha;
334 return NO_ERROR;
335}
336
Mathias Agopian87855782012-07-24 21:41:09 -0700337status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800338 const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700339 Mutex::Autolock _l(mLock);
340 layer_state_t* s = getLayerStateLocked(client, id);
341 if (!s)
342 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700343 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700344 s->layerStack = layerStack;
345 return NO_ERROR;
346}
347
Mathias Agopian698c0872011-06-28 19:09:31 -0700348status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800349 const sp<IBinder>& id, float dsdx, float dtdx,
Mathias Agopian698c0872011-06-28 19:09:31 -0700350 float dsdy, float dtdy) {
351 Mutex::Autolock _l(mLock);
352 layer_state_t* s = getLayerStateLocked(client, id);
353 if (!s)
354 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700355 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700356 layer_state_t::matrix22_t matrix;
357 matrix.dsdx = dsdx;
358 matrix.dtdx = dtdx;
359 matrix.dsdy = dsdy;
360 matrix.dtdy = dtdy;
361 s->matrix = matrix;
362 return NO_ERROR;
363}
364
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700365status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800366 const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700367 Mutex::Autolock _l(mLock);
368 layer_state_t* s = getLayerStateLocked(client, id);
369 if (!s)
370 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700371 s->what |= layer_state_t::eCropChanged;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700372 s->crop = crop;
373 return NO_ERROR;
374}
375
Mathias Agopian698c0872011-06-28 19:09:31 -0700376// ---------------------------------------------------------------------------
377
Mathias Agopiane57f2922012-08-09 16:29:12 -0700378DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
379 DisplayState s;
380 s.token = token;
381 ssize_t index = mDisplayStates.indexOf(s);
382 if (index < 0) {
383 // we don't have it, add an initialized layer_state to our list
384 s.what = 0;
385 index = mDisplayStates.add(s);
386 }
387 return mDisplayStates.editItemAt(index);
388}
389
390void Composer::setDisplaySurface(const sp<IBinder>& token,
Andy McFadden2adaf042012-12-18 09:49:45 -0800391 const sp<IGraphicBufferProducer>& bufferProducer) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700392 Mutex::Autolock _l(mLock);
393 DisplayState& s(getDisplayStateLocked(token));
Andy McFadden2adaf042012-12-18 09:49:45 -0800394 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700395 s.what |= DisplayState::eSurfaceChanged;
396}
397
398void Composer::setDisplayLayerStack(const sp<IBinder>& token,
399 uint32_t layerStack) {
400 Mutex::Autolock _l(mLock);
401 DisplayState& s(getDisplayStateLocked(token));
402 s.layerStack = layerStack;
403 s.what |= DisplayState::eLayerStackChanged;
404}
405
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700406void Composer::setDisplayProjection(const sp<IBinder>& token,
407 uint32_t orientation,
408 const Rect& layerStackRect,
409 const Rect& displayRect) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700410 Mutex::Autolock _l(mLock);
411 DisplayState& s(getDisplayStateLocked(token));
412 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700413 s.viewport = layerStackRect;
414 s.frame = displayRect;
415 s.what |= DisplayState::eDisplayProjectionChanged;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700416 mForceSynchronous = true; // TODO: do we actually still need this?
417}
418
Mathias Agopiane57f2922012-08-09 16:29:12 -0700419// ---------------------------------------------------------------------------
420
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421SurfaceComposerClient::SurfaceComposerClient()
Mathias Agopian698c0872011-06-28 19:09:31 -0700422 : mStatus(NO_INIT), mComposer(Composer::getInstance())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424}
425
Mathias Agopian698c0872011-06-28 19:09:31 -0700426void SurfaceComposerClient::onFirstRef() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700427 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700428 if (sm != 0) {
Mathias Agopian7e27f052010-05-28 14:22:23 -0700429 sp<ISurfaceComposerClient> conn = sm->createConnection();
Mathias Agopiand4784a32010-05-27 19:41:15 -0700430 if (conn != 0) {
431 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700432 mStatus = NO_ERROR;
433 }
434 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435}
436
Mathias Agopian698c0872011-06-28 19:09:31 -0700437SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -0700438 dispose();
439}
Mathias Agopiandd3423c2009-09-23 15:44:05 -0700440
Mathias Agopian698c0872011-06-28 19:09:31 -0700441status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442 return mStatus;
443}
444
Mathias Agopian698c0872011-06-28 19:09:31 -0700445sp<IBinder> SurfaceComposerClient::connection() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446 return (mClient != 0) ? mClient->asBinder() : 0;
447}
448
Mathias Agopiand4784a32010-05-27 19:41:15 -0700449status_t SurfaceComposerClient::linkToComposerDeath(
450 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -0700451 void* cookie, uint32_t flags) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700452 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700453 return sm->asBinder()->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454}
455
Mathias Agopian698c0872011-06-28 19:09:31 -0700456void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -0700458 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700459 Mutex::Autolock _lm(mLock);
460 if (mClient != 0) {
Mathias Agopiand4784a32010-05-27 19:41:15 -0700461 client = mClient; // hold ref while lock is held
462 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463 }
Mathias Agopiand4784a32010-05-27 19:41:15 -0700464 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800465}
466
Mathias Agopian698c0872011-06-28 19:09:31 -0700467sp<SurfaceControl> SurfaceComposerClient::createSurface(
Mathias Agopian698c0872011-06-28 19:09:31 -0700468 const String8& name,
Mathias Agopian698c0872011-06-28 19:09:31 -0700469 uint32_t w,
470 uint32_t h,
471 PixelFormat format,
472 uint32_t flags)
473{
474 sp<SurfaceControl> result;
475 if (mStatus == NO_ERROR) {
Mathias Agopianac9fa422013-02-11 16:40:36 -0800476 sp<ISurface> surface = mClient->createSurface(name, w, h, format, flags);
Mathias Agopian698c0872011-06-28 19:09:31 -0700477 if (surface != 0) {
Mathias Agopianac9fa422013-02-11 16:40:36 -0800478 result = new SurfaceControl(this, surface);
Mathias Agopian698c0872011-06-28 19:09:31 -0700479 }
480 }
481 return result;
482}
483
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700484sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
485 bool secure) {
486 return Composer::getInstance().createDisplay(displayName, secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700487}
488
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700489sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
490 return Composer::getInstance().getBuiltInDisplay(id);
491}
492
Mathias Agopianac9fa422013-02-11 16:40:36 -0800493status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700494 if (mStatus != NO_ERROR)
495 return mStatus;
496 status_t err = mClient->destroySurface(sid);
497 return err;
498}
499
500inline Composer& SurfaceComposerClient::getComposer() {
501 return mComposer;
502}
503
504// ----------------------------------------------------------------------------
505
506void SurfaceComposerClient::openGlobalTransaction() {
Jeff Brownf3f7db62012-08-31 02:18:38 -0700507 Composer::openGlobalTransaction();
Mathias Agopian698c0872011-06-28 19:09:31 -0700508}
509
Jamie Gennis28378392011-10-12 17:39:00 -0700510void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
511 Composer::closeGlobalTransaction(synchronous);
Mathias Agopian698c0872011-06-28 19:09:31 -0700512}
513
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700514void SurfaceComposerClient::setAnimationTransaction() {
515 Composer::setAnimationTransaction();
516}
517
Mathias Agopian698c0872011-06-28 19:09:31 -0700518// ----------------------------------------------------------------------------
519
Mathias Agopianac9fa422013-02-11 16:40:36 -0800520status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700521 return getComposer().setCrop(this, id, crop);
522}
523
Mathias Agopianac9fa422013-02-11 16:40:36 -0800524status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700525 return getComposer().setPosition(this, id, x, y);
526}
527
Mathias Agopianac9fa422013-02-11 16:40:36 -0800528status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700529 return getComposer().setSize(this, id, w, h);
530}
531
Mathias Agopianac9fa422013-02-11 16:40:36 -0800532status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700533 return getComposer().setLayer(this, id, z);
534}
535
Mathias Agopianac9fa422013-02-11 16:40:36 -0800536status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700537 return getComposer().setFlags(this, id,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700538 layer_state_t::eLayerHidden,
539 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700540}
541
Mathias Agopianac9fa422013-02-11 16:40:36 -0800542status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700543 return getComposer().setFlags(this, id,
544 0,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700545 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700546}
547
Mathias Agopianac9fa422013-02-11 16:40:36 -0800548status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700549 uint32_t mask) {
550 return getComposer().setFlags(this, id, flags, mask);
551}
552
Mathias Agopianac9fa422013-02-11 16:40:36 -0800553status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700554 const Region& transparentRegion) {
555 return getComposer().setTransparentRegionHint(this, id, transparentRegion);
556}
557
Mathias Agopianac9fa422013-02-11 16:40:36 -0800558status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700559 return getComposer().setAlpha(this, id, alpha);
560}
561
Mathias Agopianac9fa422013-02-11 16:40:36 -0800562status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700563 return getComposer().setLayerStack(this, id, layerStack);
564}
565
Mathias Agopianac9fa422013-02-11 16:40:36 -0800566status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
Mathias Agopian698c0872011-06-28 19:09:31 -0700567 float dsdy, float dtdy) {
568 return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
569}
570
571// ----------------------------------------------------------------------------
572
Mathias Agopiane57f2922012-08-09 16:29:12 -0700573void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
Andy McFadden2adaf042012-12-18 09:49:45 -0800574 const sp<IGraphicBufferProducer>& bufferProducer) {
575 Composer::getInstance().setDisplaySurface(token, bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700576}
577
578void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
579 uint32_t layerStack) {
580 Composer::getInstance().setDisplayLayerStack(token, layerStack);
581}
582
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700583void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
584 uint32_t orientation,
585 const Rect& layerStackRect,
586 const Rect& displayRect) {
587 Composer::getInstance().setDisplayProjection(token, orientation,
588 layerStackRect, displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700589}
590
591// ----------------------------------------------------------------------------
592
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593status_t SurfaceComposerClient::getDisplayInfo(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700594 const sp<IBinder>& display, DisplayInfo* info)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595{
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700596 return ComposerService::getComposerService()->getDisplayInfo(display, info);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597}
598
Jeff Brown2a09bb32012-10-08 19:13:57 -0700599void SurfaceComposerClient::blankDisplay(const sp<IBinder>& token) {
600 ComposerService::getComposerService()->blank(token);
601}
602
603void SurfaceComposerClient::unblankDisplay(const sp<IBinder>& token) {
604 ComposerService::getComposerService()->unblank(token);
605}
606
Mathias Agopian698c0872011-06-28 19:09:31 -0700607// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800609status_t ScreenshotClient::capture(
610 const sp<IBinder>& display,
611 const sp<IGraphicBufferProducer>& producer,
612 uint32_t reqWidth, uint32_t reqHeight,
613 uint32_t minLayerZ, uint32_t maxLayerZ) {
614 sp<ISurfaceComposer> s(ComposerService::getComposerService());
615 if (s == NULL) return NO_INIT;
616 return s->captureScreen(display, producer,
617 reqWidth, reqHeight, minLayerZ, maxLayerZ);
618}
619
Mathias Agopian74c40c02010-09-29 13:02:36 -0700620ScreenshotClient::ScreenshotClient()
621 : mWidth(0), mHeight(0), mFormat(PIXEL_FORMAT_NONE) {
622}
623
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700624status_t ScreenshotClient::update(const sp<IBinder>& display) {
Mathias Agopian74c40c02010-09-29 13:02:36 -0700625 sp<ISurfaceComposer> s(ComposerService::getComposerService());
626 if (s == NULL) return NO_INIT;
627 mHeap = 0;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700628 return s->captureScreen(display, &mHeap,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800629 &mWidth, &mHeight, &mFormat, 0, 0,
630 0, -1UL);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700631}
632
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700633status_t ScreenshotClient::update(const sp<IBinder>& display,
634 uint32_t reqWidth, uint32_t reqHeight) {
Mathias Agopian74c40c02010-09-29 13:02:36 -0700635 sp<ISurfaceComposer> s(ComposerService::getComposerService());
636 if (s == NULL) return NO_INIT;
637 mHeap = 0;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700638 return s->captureScreen(display, &mHeap,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800639 &mWidth, &mHeight, &mFormat, reqWidth, reqHeight,
640 0, -1UL);
641}
642
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700643status_t ScreenshotClient::update(const sp<IBinder>& display,
644 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800645 uint32_t minLayerZ, uint32_t maxLayerZ) {
646 sp<ISurfaceComposer> s(ComposerService::getComposerService());
647 if (s == NULL) return NO_INIT;
648 mHeap = 0;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700649 return s->captureScreen(display, &mHeap,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800650 &mWidth, &mHeight, &mFormat, reqWidth, reqHeight,
651 minLayerZ, maxLayerZ);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700652}
653
654void ScreenshotClient::release() {
655 mHeap = 0;
656}
657
658void const* ScreenshotClient::getPixels() const {
659 return mHeap->getBase();
660}
661
662uint32_t ScreenshotClient::getWidth() const {
663 return mWidth;
664}
665
666uint32_t ScreenshotClient::getHeight() const {
667 return mHeight;
668}
669
670PixelFormat ScreenshotClient::getFormat() const {
671 return mFormat;
672}
673
674uint32_t ScreenshotClient::getStride() const {
675 return mWidth;
676}
677
678size_t ScreenshotClient::getSize() const {
679 return mHeap->getSize();
680}
681
682// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683}; // namespace android