blob: 0bafc925dfbfa9261af3788e98e10a67a013cf2e [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 Agopian90ac7992012-02-25 18:48:35 -080034#include <gui/ISurface.h>
35#include <gui/ISurfaceComposer.h>
36#include <gui/ISurfaceComposerClient.h>
37#include <gui/SurfaceComposerClient.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038
Mathias Agopian41f673c2011-11-17 17:48:35 -080039#include <private/gui/ComposerService.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080040#include <private/gui/LayerState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
42namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043// ---------------------------------------------------------------------------
44
Mathias Agopian7e27f052010-05-28 14:22:23 -070045ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
46
Mathias Agopianb7e930d2010-06-01 15:12:58 -070047ComposerService::ComposerService()
48: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-06 18:45:56 -070049 Mutex::Autolock _l(mLock);
50 connectLocked();
51}
52
53void ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -070054 const String16 name("SurfaceFlinger");
55 while (getService(name, &mComposerService) != NO_ERROR) {
56 usleep(250000);
57 }
Andy McFadden6652b3e2012-09-06 18:45:56 -070058 assert(mComposerService != NULL);
59
60 // Create the death listener.
61 class DeathObserver : public IBinder::DeathRecipient {
62 ComposerService& mComposerService;
63 virtual void binderDied(const wp<IBinder>& who) {
64 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
65 who.unsafe_get());
66 mComposerService.composerServiceDied();
67 }
68 public:
69 DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
70 };
71
72 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
73 mComposerService->asBinder()->linkToDeath(mDeathObserver);
Mathias Agopianb7e930d2010-06-01 15:12:58 -070074}
75
Andy McFadden6652b3e2012-09-06 18:45:56 -070076/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
77 ComposerService& instance = ComposerService::getInstance();
78 Mutex::Autolock _l(instance.mLock);
79 if (instance.mComposerService == NULL) {
80 ComposerService::getInstance().connectLocked();
81 assert(instance.mComposerService != NULL);
82 ALOGD("ComposerService reconnected");
83 }
84 return instance.mComposerService;
85}
86
87void ComposerService::composerServiceDied()
88{
89 Mutex::Autolock _l(mLock);
90 mComposerService = NULL;
91 mDeathObserver = NULL;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070092}
93
Mathias Agopian7e27f052010-05-28 14:22:23 -070094// ---------------------------------------------------------------------------
95
Mathias Agopian698c0872011-06-28 19:09:31 -070096static inline
Mathias Agopiane57f2922012-08-09 16:29:12 -070097int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
Mathias Agopian698c0872011-06-28 19:09:31 -070098 if (lhs.client < rhs.client) return -1;
99 if (lhs.client > rhs.client) return 1;
100 if (lhs.state.surface < rhs.state.surface) return -1;
101 if (lhs.state.surface > rhs.state.surface) return 1;
102 return 0;
103}
104
Mathias Agopiane57f2922012-08-09 16:29:12 -0700105static inline
106int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
107 return compare_type(lhs.token, rhs.token);
108}
109
Mathias Agopian7e27f052010-05-28 14:22:23 -0700110class Composer : public Singleton<Composer>
111{
Mathias Agopiand4784a32010-05-27 19:41:15 -0700112 friend class Singleton<Composer>;
113
Mathias Agopian698c0872011-06-28 19:09:31 -0700114 mutable Mutex mLock;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700115 SortedVector<ComposerState> mComposerStates;
116 SortedVector<DisplayState > mDisplayStates;
Jamie Gennis28378392011-10-12 17:39:00 -0700117 uint32_t mForceSynchronous;
Jeff Brownf3f7db62012-08-31 02:18:38 -0700118 uint32_t mTransactionNestCount;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700119 bool mAnimation;
Mathias Agopian698c0872011-06-28 19:09:31 -0700120
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700121 Composer() : Singleton<Composer>(),
Jeff Brownf3f7db62012-08-31 02:18:38 -0700122 mForceSynchronous(0), mTransactionNestCount(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700123 mAnimation(false)
Jamie Gennis28378392011-10-12 17:39:00 -0700124 { }
Mathias Agopian698c0872011-06-28 19:09:31 -0700125
Jeff Brownf3f7db62012-08-31 02:18:38 -0700126 void openGlobalTransactionImpl();
Jamie Gennis28378392011-10-12 17:39:00 -0700127 void closeGlobalTransactionImpl(bool synchronous);
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700128 void setAnimationTransactionImpl();
Mathias Agopian698c0872011-06-28 19:09:31 -0700129
130 layer_state_t* getLayerStateLocked(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800131 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700132
Mathias Agopiane57f2922012-08-09 16:29:12 -0700133 DisplayState& getDisplayStateLocked(const sp<IBinder>& token);
134
Mathias Agopiand4784a32010-05-27 19:41:15 -0700135public:
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700136 sp<IBinder> createDisplay(const String8& displayName, bool secure);
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700137 sp<IBinder> getBuiltInDisplay(int32_t id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700138
Mathias Agopianac9fa422013-02-11 16:40:36 -0800139 status_t setPosition(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700140 float x, float y);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800141 status_t setSize(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700142 uint32_t w, uint32_t h);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800143 status_t setLayer(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700144 int32_t z);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800145 status_t setFlags(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700146 uint32_t flags, uint32_t mask);
147 status_t setTransparentRegionHint(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800148 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700149 const Region& transparentRegion);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800150 status_t setAlpha(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700151 float alpha);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800152 status_t setMatrix(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700153 float dsdx, float dtdx, float dsdy, float dtdy);
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700154 status_t setOrientation(int orientation);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800155 status_t setCrop(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700156 const Rect& crop);
Mathias Agopian87855782012-07-24 21:41:09 -0700157 status_t setLayerStack(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800158 const sp<IBinder>& id, uint32_t layerStack);
Mathias Agopian698c0872011-06-28 19:09:31 -0700159
Andy McFadden2adaf042012-12-18 09:49:45 -0800160 void setDisplaySurface(const sp<IBinder>& token,
161 const sp<IGraphicBufferProducer>& bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700162 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700163 void setDisplayProjection(const sp<IBinder>& token,
164 uint32_t orientation,
165 const Rect& layerStackRect,
166 const Rect& displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700167
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700168 static void setAnimationTransaction() {
169 Composer::getInstance().setAnimationTransactionImpl();
170 }
171
Jeff Brownf3f7db62012-08-31 02:18:38 -0700172 static void openGlobalTransaction() {
173 Composer::getInstance().openGlobalTransactionImpl();
174 }
175
Jamie Gennis28378392011-10-12 17:39:00 -0700176 static void closeGlobalTransaction(bool synchronous) {
177 Composer::getInstance().closeGlobalTransactionImpl(synchronous);
Mathias Agopiand4784a32010-05-27 19:41:15 -0700178 }
179};
180
181ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183// ---------------------------------------------------------------------------
184
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700185sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
186 return ComposerService::getComposerService()->createDisplay(displayName,
187 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700188}
189
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700190sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
191 return ComposerService::getComposerService()->getBuiltInDisplay(id);
192}
193
Jeff Brownf3f7db62012-08-31 02:18:38 -0700194void Composer::openGlobalTransactionImpl() {
195 { // scope for the lock
196 Mutex::Autolock _l(mLock);
197 mTransactionNestCount += 1;
198 }
199}
200
Jamie Gennis28378392011-10-12 17:39:00 -0700201void Composer::closeGlobalTransactionImpl(bool synchronous) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700202 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopian698c0872011-06-28 19:09:31 -0700203
204 Vector<ComposerState> transaction;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700205 Vector<DisplayState> displayTransaction;
Jamie Gennis28378392011-10-12 17:39:00 -0700206 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -0700207
208 { // scope for the lock
209 Mutex::Autolock _l(mLock);
Jeff Brownf3f7db62012-08-31 02:18:38 -0700210 mForceSynchronous |= synchronous;
211 if (!mTransactionNestCount) {
212 ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
213 "call to openGlobalTransaction().");
214 } else if (--mTransactionNestCount) {
215 return;
216 }
217
Mathias Agopiane57f2922012-08-09 16:29:12 -0700218 transaction = mComposerStates;
219 mComposerStates.clear();
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700220
Mathias Agopiane57f2922012-08-09 16:29:12 -0700221 displayTransaction = mDisplayStates;
222 mDisplayStates.clear();
Jamie Gennis28378392011-10-12 17:39:00 -0700223
Jeff Brownf3f7db62012-08-31 02:18:38 -0700224 if (mForceSynchronous) {
Jamie Gennis28378392011-10-12 17:39:00 -0700225 flags |= ISurfaceComposer::eSynchronous;
226 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700227 if (mAnimation) {
228 flags |= ISurfaceComposer::eAnimation;
229 }
230
Jamie Gennis28378392011-10-12 17:39:00 -0700231 mForceSynchronous = false;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700232 mAnimation = false;
Mathias Agopian698c0872011-06-28 19:09:31 -0700233 }
234
Mathias Agopian8b33f032012-07-24 20:43:54 -0700235 sm->setTransactionState(transaction, displayTransaction, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236}
237
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700238void Composer::setAnimationTransactionImpl() {
239 Mutex::Autolock _l(mLock);
240 mAnimation = true;
241}
242
Mathias Agopian698c0872011-06-28 19:09:31 -0700243layer_state_t* Composer::getLayerStateLocked(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800244 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700245
246 ComposerState s;
247 s.client = client->mClient;
248 s.state.surface = id;
249
Mathias Agopiane57f2922012-08-09 16:29:12 -0700250 ssize_t index = mComposerStates.indexOf(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700251 if (index < 0) {
252 // we don't have it, add an initialized layer_state to our list
Mathias Agopiane57f2922012-08-09 16:29:12 -0700253 index = mComposerStates.add(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700254 }
255
Mathias Agopiane57f2922012-08-09 16:29:12 -0700256 ComposerState* const out = mComposerStates.editArray();
Mathias Agopian698c0872011-06-28 19:09:31 -0700257 return &(out[index].state);
258}
259
260status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800261 const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700262 Mutex::Autolock _l(mLock);
263 layer_state_t* s = getLayerStateLocked(client, id);
264 if (!s)
265 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700266 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700267 s->x = x;
268 s->y = y;
269 return NO_ERROR;
270}
271
272status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800273 const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700274 Mutex::Autolock _l(mLock);
275 layer_state_t* s = getLayerStateLocked(client, id);
276 if (!s)
277 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700278 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700279 s->w = w;
280 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700281
282 // Resizing a surface makes the transaction synchronous.
283 mForceSynchronous = true;
284
Mathias Agopian698c0872011-06-28 19:09:31 -0700285 return NO_ERROR;
286}
287
288status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800289 const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700290 Mutex::Autolock _l(mLock);
291 layer_state_t* s = getLayerStateLocked(client, id);
292 if (!s)
293 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700294 s->what |= layer_state_t::eLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700295 s->z = z;
296 return NO_ERROR;
297}
298
299status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800300 const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700301 uint32_t mask) {
302 Mutex::Autolock _l(mLock);
303 layer_state_t* s = getLayerStateLocked(client, id);
304 if (!s)
305 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700306 s->what |= layer_state_t::eVisibilityChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700307 s->flags &= ~mask;
308 s->flags |= (flags & mask);
309 s->mask |= mask;
310 return NO_ERROR;
311}
312
313status_t Composer::setTransparentRegionHint(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800314 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700315 const Region& transparentRegion) {
316 Mutex::Autolock _l(mLock);
317 layer_state_t* s = getLayerStateLocked(client, id);
318 if (!s)
319 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700320 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700321 s->transparentRegion = transparentRegion;
322 return NO_ERROR;
323}
324
325status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800326 const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700327 Mutex::Autolock _l(mLock);
328 layer_state_t* s = getLayerStateLocked(client, id);
329 if (!s)
330 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700331 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700332 s->alpha = alpha;
333 return NO_ERROR;
334}
335
Mathias Agopian87855782012-07-24 21:41:09 -0700336status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800337 const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700338 Mutex::Autolock _l(mLock);
339 layer_state_t* s = getLayerStateLocked(client, id);
340 if (!s)
341 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700342 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700343 s->layerStack = layerStack;
344 return NO_ERROR;
345}
346
Mathias Agopian698c0872011-06-28 19:09:31 -0700347status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800348 const sp<IBinder>& id, float dsdx, float dtdx,
Mathias Agopian698c0872011-06-28 19:09:31 -0700349 float dsdy, float dtdy) {
350 Mutex::Autolock _l(mLock);
351 layer_state_t* s = getLayerStateLocked(client, id);
352 if (!s)
353 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700354 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700355 layer_state_t::matrix22_t matrix;
356 matrix.dsdx = dsdx;
357 matrix.dtdx = dtdx;
358 matrix.dsdy = dsdy;
359 matrix.dtdy = dtdy;
360 s->matrix = matrix;
361 return NO_ERROR;
362}
363
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700364status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800365 const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700366 Mutex::Autolock _l(mLock);
367 layer_state_t* s = getLayerStateLocked(client, id);
368 if (!s)
369 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700370 s->what |= layer_state_t::eCropChanged;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700371 s->crop = crop;
372 return NO_ERROR;
373}
374
Mathias Agopian698c0872011-06-28 19:09:31 -0700375// ---------------------------------------------------------------------------
376
Mathias Agopiane57f2922012-08-09 16:29:12 -0700377DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
378 DisplayState s;
379 s.token = token;
380 ssize_t index = mDisplayStates.indexOf(s);
381 if (index < 0) {
382 // we don't have it, add an initialized layer_state to our list
383 s.what = 0;
384 index = mDisplayStates.add(s);
385 }
386 return mDisplayStates.editItemAt(index);
387}
388
389void Composer::setDisplaySurface(const sp<IBinder>& token,
Andy McFadden2adaf042012-12-18 09:49:45 -0800390 const sp<IGraphicBufferProducer>& bufferProducer) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700391 Mutex::Autolock _l(mLock);
392 DisplayState& s(getDisplayStateLocked(token));
Andy McFadden2adaf042012-12-18 09:49:45 -0800393 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700394 s.what |= DisplayState::eSurfaceChanged;
395}
396
397void Composer::setDisplayLayerStack(const sp<IBinder>& token,
398 uint32_t layerStack) {
399 Mutex::Autolock _l(mLock);
400 DisplayState& s(getDisplayStateLocked(token));
401 s.layerStack = layerStack;
402 s.what |= DisplayState::eLayerStackChanged;
403}
404
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700405void Composer::setDisplayProjection(const sp<IBinder>& token,
406 uint32_t orientation,
407 const Rect& layerStackRect,
408 const Rect& displayRect) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700409 Mutex::Autolock _l(mLock);
410 DisplayState& s(getDisplayStateLocked(token));
411 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700412 s.viewport = layerStackRect;
413 s.frame = displayRect;
414 s.what |= DisplayState::eDisplayProjectionChanged;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700415 mForceSynchronous = true; // TODO: do we actually still need this?
416}
417
Mathias Agopiane57f2922012-08-09 16:29:12 -0700418// ---------------------------------------------------------------------------
419
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420SurfaceComposerClient::SurfaceComposerClient()
Mathias Agopian698c0872011-06-28 19:09:31 -0700421 : mStatus(NO_INIT), mComposer(Composer::getInstance())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423}
424
Mathias Agopian698c0872011-06-28 19:09:31 -0700425void SurfaceComposerClient::onFirstRef() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700426 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700427 if (sm != 0) {
Mathias Agopian7e27f052010-05-28 14:22:23 -0700428 sp<ISurfaceComposerClient> conn = sm->createConnection();
Mathias Agopiand4784a32010-05-27 19:41:15 -0700429 if (conn != 0) {
430 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700431 mStatus = NO_ERROR;
432 }
433 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434}
435
Mathias Agopian698c0872011-06-28 19:09:31 -0700436SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -0700437 dispose();
438}
Mathias Agopiandd3423c2009-09-23 15:44:05 -0700439
Mathias Agopian698c0872011-06-28 19:09:31 -0700440status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441 return mStatus;
442}
443
Mathias Agopian698c0872011-06-28 19:09:31 -0700444sp<IBinder> SurfaceComposerClient::connection() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 return (mClient != 0) ? mClient->asBinder() : 0;
446}
447
Mathias Agopiand4784a32010-05-27 19:41:15 -0700448status_t SurfaceComposerClient::linkToComposerDeath(
449 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -0700450 void* cookie, uint32_t flags) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700451 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700452 return sm->asBinder()->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453}
454
Mathias Agopian698c0872011-06-28 19:09:31 -0700455void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -0700457 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700458 Mutex::Autolock _lm(mLock);
459 if (mClient != 0) {
Mathias Agopiand4784a32010-05-27 19:41:15 -0700460 client = mClient; // hold ref while lock is held
461 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800462 }
Mathias Agopiand4784a32010-05-27 19:41:15 -0700463 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464}
465
Mathias Agopian698c0872011-06-28 19:09:31 -0700466sp<SurfaceControl> SurfaceComposerClient::createSurface(
Mathias Agopian698c0872011-06-28 19:09:31 -0700467 const String8& name,
Mathias Agopian698c0872011-06-28 19:09:31 -0700468 uint32_t w,
469 uint32_t h,
470 PixelFormat format,
471 uint32_t flags)
472{
473 sp<SurfaceControl> result;
474 if (mStatus == NO_ERROR) {
Mathias Agopianac9fa422013-02-11 16:40:36 -0800475 sp<ISurface> surface = mClient->createSurface(name, w, h, format, flags);
Mathias Agopian698c0872011-06-28 19:09:31 -0700476 if (surface != 0) {
Mathias Agopianac9fa422013-02-11 16:40:36 -0800477 result = new SurfaceControl(this, surface);
Mathias Agopian698c0872011-06-28 19:09:31 -0700478 }
479 }
480 return result;
481}
482
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700483sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
484 bool secure) {
485 return Composer::getInstance().createDisplay(displayName, secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700486}
487
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700488sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
489 return Composer::getInstance().getBuiltInDisplay(id);
490}
491
Mathias Agopianac9fa422013-02-11 16:40:36 -0800492status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700493 if (mStatus != NO_ERROR)
494 return mStatus;
495 status_t err = mClient->destroySurface(sid);
496 return err;
497}
498
499inline Composer& SurfaceComposerClient::getComposer() {
500 return mComposer;
501}
502
503// ----------------------------------------------------------------------------
504
505void SurfaceComposerClient::openGlobalTransaction() {
Jeff Brownf3f7db62012-08-31 02:18:38 -0700506 Composer::openGlobalTransaction();
Mathias Agopian698c0872011-06-28 19:09:31 -0700507}
508
Jamie Gennis28378392011-10-12 17:39:00 -0700509void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
510 Composer::closeGlobalTransaction(synchronous);
Mathias Agopian698c0872011-06-28 19:09:31 -0700511}
512
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700513void SurfaceComposerClient::setAnimationTransaction() {
514 Composer::setAnimationTransaction();
515}
516
Mathias Agopian698c0872011-06-28 19:09:31 -0700517// ----------------------------------------------------------------------------
518
Mathias Agopianac9fa422013-02-11 16:40:36 -0800519status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700520 return getComposer().setCrop(this, id, crop);
521}
522
Mathias Agopianac9fa422013-02-11 16:40:36 -0800523status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700524 return getComposer().setPosition(this, id, x, y);
525}
526
Mathias Agopianac9fa422013-02-11 16:40:36 -0800527status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700528 return getComposer().setSize(this, id, w, h);
529}
530
Mathias Agopianac9fa422013-02-11 16:40:36 -0800531status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700532 return getComposer().setLayer(this, id, z);
533}
534
Mathias Agopianac9fa422013-02-11 16:40:36 -0800535status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700536 return getComposer().setFlags(this, id,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700537 layer_state_t::eLayerHidden,
538 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700539}
540
Mathias Agopianac9fa422013-02-11 16:40:36 -0800541status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700542 return getComposer().setFlags(this, id,
543 0,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700544 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700545}
546
Mathias Agopianac9fa422013-02-11 16:40:36 -0800547status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700548 uint32_t mask) {
549 return getComposer().setFlags(this, id, flags, mask);
550}
551
Mathias Agopianac9fa422013-02-11 16:40:36 -0800552status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700553 const Region& transparentRegion) {
554 return getComposer().setTransparentRegionHint(this, id, transparentRegion);
555}
556
Mathias Agopianac9fa422013-02-11 16:40:36 -0800557status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700558 return getComposer().setAlpha(this, id, alpha);
559}
560
Mathias Agopianac9fa422013-02-11 16:40:36 -0800561status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700562 return getComposer().setLayerStack(this, id, layerStack);
563}
564
Mathias Agopianac9fa422013-02-11 16:40:36 -0800565status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
Mathias Agopian698c0872011-06-28 19:09:31 -0700566 float dsdy, float dtdy) {
567 return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
568}
569
570// ----------------------------------------------------------------------------
571
Mathias Agopiane57f2922012-08-09 16:29:12 -0700572void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
Andy McFadden2adaf042012-12-18 09:49:45 -0800573 const sp<IGraphicBufferProducer>& bufferProducer) {
574 Composer::getInstance().setDisplaySurface(token, bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700575}
576
577void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
578 uint32_t layerStack) {
579 Composer::getInstance().setDisplayLayerStack(token, layerStack);
580}
581
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700582void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
583 uint32_t orientation,
584 const Rect& layerStackRect,
585 const Rect& displayRect) {
586 Composer::getInstance().setDisplayProjection(token, orientation,
587 layerStackRect, displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700588}
589
590// ----------------------------------------------------------------------------
591
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592status_t SurfaceComposerClient::getDisplayInfo(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700593 const sp<IBinder>& display, DisplayInfo* info)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594{
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700595 return ComposerService::getComposerService()->getDisplayInfo(display, info);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596}
597
Jeff Brown2a09bb32012-10-08 19:13:57 -0700598void SurfaceComposerClient::blankDisplay(const sp<IBinder>& token) {
599 ComposerService::getComposerService()->blank(token);
600}
601
602void SurfaceComposerClient::unblankDisplay(const sp<IBinder>& token) {
603 ComposerService::getComposerService()->unblank(token);
604}
605
Mathias Agopian698c0872011-06-28 19:09:31 -0700606// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607
Mathias Agopian74c40c02010-09-29 13:02:36 -0700608ScreenshotClient::ScreenshotClient()
609 : mWidth(0), mHeight(0), mFormat(PIXEL_FORMAT_NONE) {
610}
611
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700612status_t ScreenshotClient::update(const sp<IBinder>& display) {
Mathias Agopian74c40c02010-09-29 13:02:36 -0700613 sp<ISurfaceComposer> s(ComposerService::getComposerService());
614 if (s == NULL) return NO_INIT;
615 mHeap = 0;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700616 return s->captureScreen(display, &mHeap,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800617 &mWidth, &mHeight, &mFormat, 0, 0,
618 0, -1UL);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700619}
620
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700621status_t ScreenshotClient::update(const sp<IBinder>& display,
622 uint32_t reqWidth, uint32_t reqHeight) {
Mathias Agopian74c40c02010-09-29 13:02:36 -0700623 sp<ISurfaceComposer> s(ComposerService::getComposerService());
624 if (s == NULL) return NO_INIT;
625 mHeap = 0;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700626 return s->captureScreen(display, &mHeap,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800627 &mWidth, &mHeight, &mFormat, reqWidth, reqHeight,
628 0, -1UL);
629}
630
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700631status_t ScreenshotClient::update(const sp<IBinder>& display,
632 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800633 uint32_t minLayerZ, uint32_t maxLayerZ) {
634 sp<ISurfaceComposer> s(ComposerService::getComposerService());
635 if (s == NULL) return NO_INIT;
636 mHeap = 0;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700637 return s->captureScreen(display, &mHeap,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800638 &mWidth, &mHeight, &mFormat, reqWidth, reqHeight,
639 minLayerZ, maxLayerZ);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700640}
641
642void ScreenshotClient::release() {
643 mHeap = 0;
644}
645
646void const* ScreenshotClient::getPixels() const {
647 return mHeap->getBase();
648}
649
650uint32_t ScreenshotClient::getWidth() const {
651 return mWidth;
652}
653
654uint32_t ScreenshotClient::getHeight() const {
655 return mHeight;
656}
657
658PixelFormat ScreenshotClient::getFormat() const {
659 return mFormat;
660}
661
662uint32_t ScreenshotClient::getStride() const {
663 return mWidth;
664}
665
666size_t ScreenshotClient::getSize() const {
667 return mHeap->getSize();
668}
669
670// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671}; // namespace android