blob: aafc4d255f33e4d808a435d8c9078deedf93222d [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 Agopianabe815d2013-03-19 22:22:21 -070034#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080035#include <gui/IGraphicBufferProducer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080036#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);
Jesse Hall6c913be2013-08-08 12:15:49 -0700138 void destroyDisplay(const sp<IBinder>& display);
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700139 sp<IBinder> getBuiltInDisplay(int32_t id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700140
Mathias Agopianac9fa422013-02-11 16:40:36 -0800141 status_t setPosition(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700142 float x, float y);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800143 status_t setSize(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700144 uint32_t w, uint32_t h);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800145 status_t setLayer(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700146 int32_t z);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800147 status_t setFlags(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700148 uint32_t flags, uint32_t mask);
149 status_t setTransparentRegionHint(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800150 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700151 const Region& transparentRegion);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800152 status_t setAlpha(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700153 float alpha);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800154 status_t setMatrix(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700155 float dsdx, float dtdx, float dsdy, float dtdy);
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700156 status_t setOrientation(int orientation);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800157 status_t setCrop(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700158 const Rect& crop);
Mathias Agopian87855782012-07-24 21:41:09 -0700159 status_t setLayerStack(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800160 const sp<IBinder>& id, uint32_t layerStack);
Mathias Agopian698c0872011-06-28 19:09:31 -0700161
Andy McFadden2adaf042012-12-18 09:49:45 -0800162 void setDisplaySurface(const sp<IBinder>& token,
163 const sp<IGraphicBufferProducer>& bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700164 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700165 void setDisplayProjection(const sp<IBinder>& token,
166 uint32_t orientation,
167 const Rect& layerStackRect,
168 const Rect& displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700169
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700170 static void setAnimationTransaction() {
171 Composer::getInstance().setAnimationTransactionImpl();
172 }
173
Jeff Brownf3f7db62012-08-31 02:18:38 -0700174 static void openGlobalTransaction() {
175 Composer::getInstance().openGlobalTransactionImpl();
176 }
177
Jamie Gennis28378392011-10-12 17:39:00 -0700178 static void closeGlobalTransaction(bool synchronous) {
179 Composer::getInstance().closeGlobalTransactionImpl(synchronous);
Mathias Agopiand4784a32010-05-27 19:41:15 -0700180 }
181};
182
183ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
184
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185// ---------------------------------------------------------------------------
186
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700187sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
188 return ComposerService::getComposerService()->createDisplay(displayName,
189 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700190}
191
Jesse Hall6c913be2013-08-08 12:15:49 -0700192void Composer::destroyDisplay(const sp<IBinder>& display) {
193 return ComposerService::getComposerService()->destroyDisplay(display);
194}
195
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700196sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
197 return ComposerService::getComposerService()->getBuiltInDisplay(id);
198}
199
Jeff Brownf3f7db62012-08-31 02:18:38 -0700200void Composer::openGlobalTransactionImpl() {
201 { // scope for the lock
202 Mutex::Autolock _l(mLock);
203 mTransactionNestCount += 1;
204 }
205}
206
Jamie Gennis28378392011-10-12 17:39:00 -0700207void Composer::closeGlobalTransactionImpl(bool synchronous) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700208 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopian698c0872011-06-28 19:09:31 -0700209
210 Vector<ComposerState> transaction;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700211 Vector<DisplayState> displayTransaction;
Jamie Gennis28378392011-10-12 17:39:00 -0700212 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -0700213
214 { // scope for the lock
215 Mutex::Autolock _l(mLock);
Jeff Brownf3f7db62012-08-31 02:18:38 -0700216 mForceSynchronous |= synchronous;
217 if (!mTransactionNestCount) {
218 ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
219 "call to openGlobalTransaction().");
220 } else if (--mTransactionNestCount) {
221 return;
222 }
223
Mathias Agopiane57f2922012-08-09 16:29:12 -0700224 transaction = mComposerStates;
225 mComposerStates.clear();
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700226
Mathias Agopiane57f2922012-08-09 16:29:12 -0700227 displayTransaction = mDisplayStates;
228 mDisplayStates.clear();
Jamie Gennis28378392011-10-12 17:39:00 -0700229
Jeff Brownf3f7db62012-08-31 02:18:38 -0700230 if (mForceSynchronous) {
Jamie Gennis28378392011-10-12 17:39:00 -0700231 flags |= ISurfaceComposer::eSynchronous;
232 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700233 if (mAnimation) {
234 flags |= ISurfaceComposer::eAnimation;
235 }
236
Jamie Gennis28378392011-10-12 17:39:00 -0700237 mForceSynchronous = false;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700238 mAnimation = false;
Mathias Agopian698c0872011-06-28 19:09:31 -0700239 }
240
Mathias Agopian8b33f032012-07-24 20:43:54 -0700241 sm->setTransactionState(transaction, displayTransaction, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242}
243
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700244void Composer::setAnimationTransactionImpl() {
245 Mutex::Autolock _l(mLock);
246 mAnimation = true;
247}
248
Mathias Agopian698c0872011-06-28 19:09:31 -0700249layer_state_t* Composer::getLayerStateLocked(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800250 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700251
252 ComposerState s;
253 s.client = client->mClient;
254 s.state.surface = id;
255
Mathias Agopiane57f2922012-08-09 16:29:12 -0700256 ssize_t index = mComposerStates.indexOf(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700257 if (index < 0) {
258 // we don't have it, add an initialized layer_state to our list
Mathias Agopiane57f2922012-08-09 16:29:12 -0700259 index = mComposerStates.add(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700260 }
261
Mathias Agopiane57f2922012-08-09 16:29:12 -0700262 ComposerState* const out = mComposerStates.editArray();
Mathias Agopian698c0872011-06-28 19:09:31 -0700263 return &(out[index].state);
264}
265
266status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800267 const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700268 Mutex::Autolock _l(mLock);
269 layer_state_t* s = getLayerStateLocked(client, id);
270 if (!s)
271 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700272 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700273 s->x = x;
274 s->y = y;
275 return NO_ERROR;
276}
277
278status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800279 const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700280 Mutex::Autolock _l(mLock);
281 layer_state_t* s = getLayerStateLocked(client, id);
282 if (!s)
283 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700284 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700285 s->w = w;
286 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700287
288 // Resizing a surface makes the transaction synchronous.
289 mForceSynchronous = true;
290
Mathias Agopian698c0872011-06-28 19:09:31 -0700291 return NO_ERROR;
292}
293
294status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800295 const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700296 Mutex::Autolock _l(mLock);
297 layer_state_t* s = getLayerStateLocked(client, id);
298 if (!s)
299 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700300 s->what |= layer_state_t::eLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700301 s->z = z;
302 return NO_ERROR;
303}
304
305status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800306 const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700307 uint32_t mask) {
308 Mutex::Autolock _l(mLock);
309 layer_state_t* s = getLayerStateLocked(client, id);
310 if (!s)
311 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700312 s->what |= layer_state_t::eVisibilityChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700313 s->flags &= ~mask;
314 s->flags |= (flags & mask);
315 s->mask |= mask;
316 return NO_ERROR;
317}
318
319status_t Composer::setTransparentRegionHint(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800320 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700321 const Region& transparentRegion) {
322 Mutex::Autolock _l(mLock);
323 layer_state_t* s = getLayerStateLocked(client, id);
324 if (!s)
325 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700326 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700327 s->transparentRegion = transparentRegion;
328 return NO_ERROR;
329}
330
331status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800332 const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700333 Mutex::Autolock _l(mLock);
334 layer_state_t* s = getLayerStateLocked(client, id);
335 if (!s)
336 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700337 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700338 s->alpha = alpha;
339 return NO_ERROR;
340}
341
Mathias Agopian87855782012-07-24 21:41:09 -0700342status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800343 const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700344 Mutex::Autolock _l(mLock);
345 layer_state_t* s = getLayerStateLocked(client, id);
346 if (!s)
347 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700348 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700349 s->layerStack = layerStack;
350 return NO_ERROR;
351}
352
Mathias Agopian698c0872011-06-28 19:09:31 -0700353status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800354 const sp<IBinder>& id, float dsdx, float dtdx,
Mathias Agopian698c0872011-06-28 19:09:31 -0700355 float dsdy, float dtdy) {
356 Mutex::Autolock _l(mLock);
357 layer_state_t* s = getLayerStateLocked(client, id);
358 if (!s)
359 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700360 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700361 layer_state_t::matrix22_t matrix;
362 matrix.dsdx = dsdx;
363 matrix.dtdx = dtdx;
364 matrix.dsdy = dsdy;
365 matrix.dtdy = dtdy;
366 s->matrix = matrix;
367 return NO_ERROR;
368}
369
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700370status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800371 const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700372 Mutex::Autolock _l(mLock);
373 layer_state_t* s = getLayerStateLocked(client, id);
374 if (!s)
375 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700376 s->what |= layer_state_t::eCropChanged;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700377 s->crop = crop;
378 return NO_ERROR;
379}
380
Mathias Agopian698c0872011-06-28 19:09:31 -0700381// ---------------------------------------------------------------------------
382
Mathias Agopiane57f2922012-08-09 16:29:12 -0700383DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
384 DisplayState s;
385 s.token = token;
386 ssize_t index = mDisplayStates.indexOf(s);
387 if (index < 0) {
388 // we don't have it, add an initialized layer_state to our list
389 s.what = 0;
390 index = mDisplayStates.add(s);
391 }
392 return mDisplayStates.editItemAt(index);
393}
394
395void Composer::setDisplaySurface(const sp<IBinder>& token,
Andy McFadden2adaf042012-12-18 09:49:45 -0800396 const sp<IGraphicBufferProducer>& bufferProducer) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700397 Mutex::Autolock _l(mLock);
398 DisplayState& s(getDisplayStateLocked(token));
Andy McFadden2adaf042012-12-18 09:49:45 -0800399 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700400 s.what |= DisplayState::eSurfaceChanged;
401}
402
403void Composer::setDisplayLayerStack(const sp<IBinder>& token,
404 uint32_t layerStack) {
405 Mutex::Autolock _l(mLock);
406 DisplayState& s(getDisplayStateLocked(token));
407 s.layerStack = layerStack;
408 s.what |= DisplayState::eLayerStackChanged;
409}
410
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700411void Composer::setDisplayProjection(const sp<IBinder>& token,
412 uint32_t orientation,
413 const Rect& layerStackRect,
414 const Rect& displayRect) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700415 Mutex::Autolock _l(mLock);
416 DisplayState& s(getDisplayStateLocked(token));
417 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700418 s.viewport = layerStackRect;
419 s.frame = displayRect;
420 s.what |= DisplayState::eDisplayProjectionChanged;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700421 mForceSynchronous = true; // TODO: do we actually still need this?
422}
423
Mathias Agopiane57f2922012-08-09 16:29:12 -0700424// ---------------------------------------------------------------------------
425
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426SurfaceComposerClient::SurfaceComposerClient()
Mathias Agopian698c0872011-06-28 19:09:31 -0700427 : mStatus(NO_INIT), mComposer(Composer::getInstance())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429}
430
Mathias Agopian698c0872011-06-28 19:09:31 -0700431void SurfaceComposerClient::onFirstRef() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700432 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700433 if (sm != 0) {
Mathias Agopian7e27f052010-05-28 14:22:23 -0700434 sp<ISurfaceComposerClient> conn = sm->createConnection();
Mathias Agopiand4784a32010-05-27 19:41:15 -0700435 if (conn != 0) {
436 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700437 mStatus = NO_ERROR;
438 }
439 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440}
441
Mathias Agopian698c0872011-06-28 19:09:31 -0700442SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -0700443 dispose();
444}
Mathias Agopiandd3423c2009-09-23 15:44:05 -0700445
Mathias Agopian698c0872011-06-28 19:09:31 -0700446status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447 return mStatus;
448}
449
Mathias Agopian698c0872011-06-28 19:09:31 -0700450sp<IBinder> SurfaceComposerClient::connection() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451 return (mClient != 0) ? mClient->asBinder() : 0;
452}
453
Mathias Agopiand4784a32010-05-27 19:41:15 -0700454status_t SurfaceComposerClient::linkToComposerDeath(
455 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -0700456 void* cookie, uint32_t flags) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700457 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700458 return sm->asBinder()->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459}
460
Mathias Agopian698c0872011-06-28 19:09:31 -0700461void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800462 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -0700463 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700464 Mutex::Autolock _lm(mLock);
465 if (mClient != 0) {
Mathias Agopiand4784a32010-05-27 19:41:15 -0700466 client = mClient; // hold ref while lock is held
467 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468 }
Mathias Agopiand4784a32010-05-27 19:41:15 -0700469 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470}
471
Mathias Agopian698c0872011-06-28 19:09:31 -0700472sp<SurfaceControl> SurfaceComposerClient::createSurface(
Mathias Agopian698c0872011-06-28 19:09:31 -0700473 const String8& name,
Mathias Agopian698c0872011-06-28 19:09:31 -0700474 uint32_t w,
475 uint32_t h,
476 PixelFormat format,
477 uint32_t flags)
478{
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700479 sp<SurfaceControl> sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700480 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700481 sp<IBinder> handle;
482 sp<IGraphicBufferProducer> gbp;
483 status_t err = mClient->createSurface(name, w, h, format, flags,
484 &handle, &gbp);
485 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
486 if (err == NO_ERROR) {
487 sur = new SurfaceControl(this, handle, gbp);
Mathias Agopian698c0872011-06-28 19:09:31 -0700488 }
489 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700490 return sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700491}
492
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700493sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
494 bool secure) {
495 return Composer::getInstance().createDisplay(displayName, secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700496}
497
Jesse Hall6c913be2013-08-08 12:15:49 -0700498void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
499 Composer::getInstance().destroyDisplay(display);
500}
501
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700502sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
503 return Composer::getInstance().getBuiltInDisplay(id);
504}
505
Mathias Agopianac9fa422013-02-11 16:40:36 -0800506status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700507 if (mStatus != NO_ERROR)
508 return mStatus;
509 status_t err = mClient->destroySurface(sid);
510 return err;
511}
512
513inline Composer& SurfaceComposerClient::getComposer() {
514 return mComposer;
515}
516
517// ----------------------------------------------------------------------------
518
519void SurfaceComposerClient::openGlobalTransaction() {
Jeff Brownf3f7db62012-08-31 02:18:38 -0700520 Composer::openGlobalTransaction();
Mathias Agopian698c0872011-06-28 19:09:31 -0700521}
522
Jamie Gennis28378392011-10-12 17:39:00 -0700523void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
524 Composer::closeGlobalTransaction(synchronous);
Mathias Agopian698c0872011-06-28 19:09:31 -0700525}
526
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700527void SurfaceComposerClient::setAnimationTransaction() {
528 Composer::setAnimationTransaction();
529}
530
Mathias Agopian698c0872011-06-28 19:09:31 -0700531// ----------------------------------------------------------------------------
532
Mathias Agopianac9fa422013-02-11 16:40:36 -0800533status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700534 return getComposer().setCrop(this, id, crop);
535}
536
Mathias Agopianac9fa422013-02-11 16:40:36 -0800537status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700538 return getComposer().setPosition(this, id, x, y);
539}
540
Mathias Agopianac9fa422013-02-11 16:40:36 -0800541status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700542 return getComposer().setSize(this, id, w, h);
543}
544
Mathias Agopianac9fa422013-02-11 16:40:36 -0800545status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700546 return getComposer().setLayer(this, id, z);
547}
548
Mathias Agopianac9fa422013-02-11 16:40:36 -0800549status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700550 return getComposer().setFlags(this, id,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700551 layer_state_t::eLayerHidden,
552 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700553}
554
Mathias Agopianac9fa422013-02-11 16:40:36 -0800555status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700556 return getComposer().setFlags(this, id,
557 0,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700558 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700559}
560
Mathias Agopianac9fa422013-02-11 16:40:36 -0800561status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700562 uint32_t mask) {
563 return getComposer().setFlags(this, id, flags, mask);
564}
565
Mathias Agopianac9fa422013-02-11 16:40:36 -0800566status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700567 const Region& transparentRegion) {
568 return getComposer().setTransparentRegionHint(this, id, transparentRegion);
569}
570
Mathias Agopianac9fa422013-02-11 16:40:36 -0800571status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700572 return getComposer().setAlpha(this, id, alpha);
573}
574
Mathias Agopianac9fa422013-02-11 16:40:36 -0800575status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700576 return getComposer().setLayerStack(this, id, layerStack);
577}
578
Mathias Agopianac9fa422013-02-11 16:40:36 -0800579status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
Mathias Agopian698c0872011-06-28 19:09:31 -0700580 float dsdy, float dtdy) {
581 return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
582}
583
584// ----------------------------------------------------------------------------
585
Mathias Agopiane57f2922012-08-09 16:29:12 -0700586void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
Andy McFadden2adaf042012-12-18 09:49:45 -0800587 const sp<IGraphicBufferProducer>& bufferProducer) {
588 Composer::getInstance().setDisplaySurface(token, bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700589}
590
591void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
592 uint32_t layerStack) {
593 Composer::getInstance().setDisplayLayerStack(token, layerStack);
594}
595
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700596void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
597 uint32_t orientation,
598 const Rect& layerStackRect,
599 const Rect& displayRect) {
600 Composer::getInstance().setDisplayProjection(token, orientation,
601 layerStackRect, displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700602}
603
604// ----------------------------------------------------------------------------
605
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606status_t SurfaceComposerClient::getDisplayInfo(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700607 const sp<IBinder>& display, DisplayInfo* info)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608{
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700609 return ComposerService::getComposerService()->getDisplayInfo(display, info);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610}
611
Jeff Brown2a09bb32012-10-08 19:13:57 -0700612void SurfaceComposerClient::blankDisplay(const sp<IBinder>& token) {
613 ComposerService::getComposerService()->blank(token);
614}
615
616void SurfaceComposerClient::unblankDisplay(const sp<IBinder>& token) {
617 ComposerService::getComposerService()->unblank(token);
618}
619
Mathias Agopian698c0872011-06-28 19:09:31 -0700620// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800622status_t ScreenshotClient::capture(
623 const sp<IBinder>& display,
624 const sp<IGraphicBufferProducer>& producer,
625 uint32_t reqWidth, uint32_t reqHeight,
626 uint32_t minLayerZ, uint32_t maxLayerZ) {
627 sp<ISurfaceComposer> s(ComposerService::getComposerService());
628 if (s == NULL) return NO_INIT;
629 return s->captureScreen(display, producer,
Mathias Agopian3ca76f42013-08-06 16:07:33 -0700630 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800631}
632
Mathias Agopian74c40c02010-09-29 13:02:36 -0700633ScreenshotClient::ScreenshotClient()
Mathias Agopianabe815d2013-03-19 22:22:21 -0700634 : mHaveBuffer(false) {
635 memset(&mBuffer, 0, sizeof(mBuffer));
Mathias Agopian74c40c02010-09-29 13:02:36 -0700636}
637
Mathias Agopian8000d062013-03-26 18:15:35 -0700638ScreenshotClient::~ScreenshotClient() {
639 ScreenshotClient::release();
640}
641
Mathias Agopianabe815d2013-03-19 22:22:21 -0700642sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
643 if (mCpuConsumer == NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700644 mBufferQueue = new BufferQueue();
645 mCpuConsumer = new CpuConsumer(mBufferQueue, 1);
Mathias Agopianabe815d2013-03-19 22:22:21 -0700646 mCpuConsumer->setName(String8("ScreenshotClient"));
647 }
648 return mCpuConsumer;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800649}
650
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700651status_t ScreenshotClient::update(const sp<IBinder>& display,
652 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800653 uint32_t minLayerZ, uint32_t maxLayerZ) {
654 sp<ISurfaceComposer> s(ComposerService::getComposerService());
655 if (s == NULL) return NO_INIT;
Mathias Agopianabe815d2013-03-19 22:22:21 -0700656 sp<CpuConsumer> cpuConsumer = getCpuConsumer();
657
658 if (mHaveBuffer) {
659 mCpuConsumer->unlockBuffer(mBuffer);
660 memset(&mBuffer, 0, sizeof(mBuffer));
661 mHaveBuffer = false;
662 }
663
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700664 status_t err = s->captureScreen(display, mBufferQueue,
Mathias Agopian3ca76f42013-08-06 16:07:33 -0700665 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopianabe815d2013-03-19 22:22:21 -0700666
667 if (err == NO_ERROR) {
668 err = mCpuConsumer->lockNextBuffer(&mBuffer);
669 if (err == NO_ERROR) {
670 mHaveBuffer = true;
671 }
672 }
673 return err;
674}
675
676status_t ScreenshotClient::update(const sp<IBinder>& display) {
677 return ScreenshotClient::update(display, 0, 0, 0, -1UL);
678}
679
680status_t ScreenshotClient::update(const sp<IBinder>& display,
681 uint32_t reqWidth, uint32_t reqHeight) {
682 return ScreenshotClient::update(display, reqWidth, reqHeight, 0, -1UL);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700683}
684
685void ScreenshotClient::release() {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700686 if (mHaveBuffer) {
687 mCpuConsumer->unlockBuffer(mBuffer);
688 memset(&mBuffer, 0, sizeof(mBuffer));
689 mHaveBuffer = false;
690 }
691 mCpuConsumer.clear();
Mathias Agopian74c40c02010-09-29 13:02:36 -0700692}
693
694void const* ScreenshotClient::getPixels() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700695 return mBuffer.data;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700696}
697
698uint32_t ScreenshotClient::getWidth() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700699 return mBuffer.width;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700700}
701
702uint32_t ScreenshotClient::getHeight() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700703 return mBuffer.height;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700704}
705
706PixelFormat ScreenshotClient::getFormat() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700707 return mBuffer.format;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700708}
709
710uint32_t ScreenshotClient::getStride() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700711 return mBuffer.stride;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700712}
713
714size_t ScreenshotClient::getSize() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700715 return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700716}
717
718// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719}; // namespace android