blob: 5fe99e8555b2038d4fb6d2426b9be21dba2374f4 [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;
Andy McFadden4125a4f2014-01-29 17:17:11 -0800312 if (mask & layer_state_t::eLayerOpaque) {
313 s->what |= layer_state_t::eOpacityChanged;
314 }
315 if (mask & layer_state_t::eLayerHidden) {
316 s->what |= layer_state_t::eVisibilityChanged;
317 }
Mathias Agopian698c0872011-06-28 19:09:31 -0700318 s->flags &= ~mask;
319 s->flags |= (flags & mask);
320 s->mask |= mask;
321 return NO_ERROR;
322}
323
324status_t Composer::setTransparentRegionHint(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800325 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700326 const Region& transparentRegion) {
327 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::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700332 s->transparentRegion = transparentRegion;
333 return NO_ERROR;
334}
335
336status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800337 const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -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::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700343 s->alpha = alpha;
344 return NO_ERROR;
345}
346
Mathias Agopian87855782012-07-24 21:41:09 -0700347status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800348 const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700349 Mutex::Autolock _l(mLock);
350 layer_state_t* s = getLayerStateLocked(client, id);
351 if (!s)
352 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700353 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700354 s->layerStack = layerStack;
355 return NO_ERROR;
356}
357
Mathias Agopian698c0872011-06-28 19:09:31 -0700358status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800359 const sp<IBinder>& id, float dsdx, float dtdx,
Mathias Agopian698c0872011-06-28 19:09:31 -0700360 float dsdy, float dtdy) {
361 Mutex::Autolock _l(mLock);
362 layer_state_t* s = getLayerStateLocked(client, id);
363 if (!s)
364 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700365 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700366 layer_state_t::matrix22_t matrix;
367 matrix.dsdx = dsdx;
368 matrix.dtdx = dtdx;
369 matrix.dsdy = dsdy;
370 matrix.dtdy = dtdy;
371 s->matrix = matrix;
372 return NO_ERROR;
373}
374
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700375status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800376 const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700377 Mutex::Autolock _l(mLock);
378 layer_state_t* s = getLayerStateLocked(client, id);
379 if (!s)
380 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700381 s->what |= layer_state_t::eCropChanged;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700382 s->crop = crop;
383 return NO_ERROR;
384}
385
Mathias Agopian698c0872011-06-28 19:09:31 -0700386// ---------------------------------------------------------------------------
387
Mathias Agopiane57f2922012-08-09 16:29:12 -0700388DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
389 DisplayState s;
390 s.token = token;
391 ssize_t index = mDisplayStates.indexOf(s);
392 if (index < 0) {
393 // we don't have it, add an initialized layer_state to our list
394 s.what = 0;
395 index = mDisplayStates.add(s);
396 }
397 return mDisplayStates.editItemAt(index);
398}
399
400void Composer::setDisplaySurface(const sp<IBinder>& token,
Andy McFadden2adaf042012-12-18 09:49:45 -0800401 const sp<IGraphicBufferProducer>& bufferProducer) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700402 Mutex::Autolock _l(mLock);
403 DisplayState& s(getDisplayStateLocked(token));
Andy McFadden2adaf042012-12-18 09:49:45 -0800404 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700405 s.what |= DisplayState::eSurfaceChanged;
406}
407
408void Composer::setDisplayLayerStack(const sp<IBinder>& token,
409 uint32_t layerStack) {
410 Mutex::Autolock _l(mLock);
411 DisplayState& s(getDisplayStateLocked(token));
412 s.layerStack = layerStack;
413 s.what |= DisplayState::eLayerStackChanged;
414}
415
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700416void Composer::setDisplayProjection(const sp<IBinder>& token,
417 uint32_t orientation,
418 const Rect& layerStackRect,
419 const Rect& displayRect) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700420 Mutex::Autolock _l(mLock);
421 DisplayState& s(getDisplayStateLocked(token));
422 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700423 s.viewport = layerStackRect;
424 s.frame = displayRect;
425 s.what |= DisplayState::eDisplayProjectionChanged;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700426 mForceSynchronous = true; // TODO: do we actually still need this?
427}
428
Mathias Agopiane57f2922012-08-09 16:29:12 -0700429// ---------------------------------------------------------------------------
430
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431SurfaceComposerClient::SurfaceComposerClient()
Mathias Agopian698c0872011-06-28 19:09:31 -0700432 : mStatus(NO_INIT), mComposer(Composer::getInstance())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434}
435
Mathias Agopian698c0872011-06-28 19:09:31 -0700436void SurfaceComposerClient::onFirstRef() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700437 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700438 if (sm != 0) {
Mathias Agopian7e27f052010-05-28 14:22:23 -0700439 sp<ISurfaceComposerClient> conn = sm->createConnection();
Mathias Agopiand4784a32010-05-27 19:41:15 -0700440 if (conn != 0) {
441 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700442 mStatus = NO_ERROR;
443 }
444 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445}
446
Mathias Agopian698c0872011-06-28 19:09:31 -0700447SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -0700448 dispose();
449}
Mathias Agopiandd3423c2009-09-23 15:44:05 -0700450
Mathias Agopian698c0872011-06-28 19:09:31 -0700451status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452 return mStatus;
453}
454
Mathias Agopian698c0872011-06-28 19:09:31 -0700455sp<IBinder> SurfaceComposerClient::connection() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456 return (mClient != 0) ? mClient->asBinder() : 0;
457}
458
Mathias Agopiand4784a32010-05-27 19:41:15 -0700459status_t SurfaceComposerClient::linkToComposerDeath(
460 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -0700461 void* cookie, uint32_t flags) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700462 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700463 return sm->asBinder()->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464}
465
Mathias Agopian698c0872011-06-28 19:09:31 -0700466void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -0700468 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700469 Mutex::Autolock _lm(mLock);
470 if (mClient != 0) {
Mathias Agopiand4784a32010-05-27 19:41:15 -0700471 client = mClient; // hold ref while lock is held
472 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800473 }
Mathias Agopiand4784a32010-05-27 19:41:15 -0700474 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475}
476
Mathias Agopian698c0872011-06-28 19:09:31 -0700477sp<SurfaceControl> SurfaceComposerClient::createSurface(
Mathias Agopian698c0872011-06-28 19:09:31 -0700478 const String8& name,
Mathias Agopian698c0872011-06-28 19:09:31 -0700479 uint32_t w,
480 uint32_t h,
481 PixelFormat format,
482 uint32_t flags)
483{
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700484 sp<SurfaceControl> sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700485 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700486 sp<IBinder> handle;
487 sp<IGraphicBufferProducer> gbp;
488 status_t err = mClient->createSurface(name, w, h, format, flags,
489 &handle, &gbp);
490 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
491 if (err == NO_ERROR) {
492 sur = new SurfaceControl(this, handle, gbp);
Mathias Agopian698c0872011-06-28 19:09:31 -0700493 }
494 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700495 return sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700496}
497
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700498sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
499 bool secure) {
500 return Composer::getInstance().createDisplay(displayName, secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700501}
502
Jesse Hall6c913be2013-08-08 12:15:49 -0700503void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
504 Composer::getInstance().destroyDisplay(display);
505}
506
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700507sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
508 return Composer::getInstance().getBuiltInDisplay(id);
509}
510
Mathias Agopianac9fa422013-02-11 16:40:36 -0800511status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700512 if (mStatus != NO_ERROR)
513 return mStatus;
514 status_t err = mClient->destroySurface(sid);
515 return err;
516}
517
518inline Composer& SurfaceComposerClient::getComposer() {
519 return mComposer;
520}
521
522// ----------------------------------------------------------------------------
523
524void SurfaceComposerClient::openGlobalTransaction() {
Jeff Brownf3f7db62012-08-31 02:18:38 -0700525 Composer::openGlobalTransaction();
Mathias Agopian698c0872011-06-28 19:09:31 -0700526}
527
Jamie Gennis28378392011-10-12 17:39:00 -0700528void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
529 Composer::closeGlobalTransaction(synchronous);
Mathias Agopian698c0872011-06-28 19:09:31 -0700530}
531
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700532void SurfaceComposerClient::setAnimationTransaction() {
533 Composer::setAnimationTransaction();
534}
535
Mathias Agopian698c0872011-06-28 19:09:31 -0700536// ----------------------------------------------------------------------------
537
Mathias Agopianac9fa422013-02-11 16:40:36 -0800538status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700539 return getComposer().setCrop(this, id, crop);
540}
541
Mathias Agopianac9fa422013-02-11 16:40:36 -0800542status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700543 return getComposer().setPosition(this, id, x, y);
544}
545
Mathias Agopianac9fa422013-02-11 16:40:36 -0800546status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700547 return getComposer().setSize(this, id, w, h);
548}
549
Mathias Agopianac9fa422013-02-11 16:40:36 -0800550status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700551 return getComposer().setLayer(this, id, z);
552}
553
Mathias Agopianac9fa422013-02-11 16:40:36 -0800554status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700555 return getComposer().setFlags(this, id,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700556 layer_state_t::eLayerHidden,
557 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700558}
559
Mathias Agopianac9fa422013-02-11 16:40:36 -0800560status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700561 return getComposer().setFlags(this, id,
562 0,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700563 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700564}
565
Mathias Agopianac9fa422013-02-11 16:40:36 -0800566status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700567 uint32_t mask) {
568 return getComposer().setFlags(this, id, flags, mask);
569}
570
Mathias Agopianac9fa422013-02-11 16:40:36 -0800571status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700572 const Region& transparentRegion) {
573 return getComposer().setTransparentRegionHint(this, id, transparentRegion);
574}
575
Mathias Agopianac9fa422013-02-11 16:40:36 -0800576status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700577 return getComposer().setAlpha(this, id, alpha);
578}
579
Mathias Agopianac9fa422013-02-11 16:40:36 -0800580status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700581 return getComposer().setLayerStack(this, id, layerStack);
582}
583
Mathias Agopianac9fa422013-02-11 16:40:36 -0800584status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
Mathias Agopian698c0872011-06-28 19:09:31 -0700585 float dsdy, float dtdy) {
586 return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
587}
588
589// ----------------------------------------------------------------------------
590
Mathias Agopiane57f2922012-08-09 16:29:12 -0700591void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
Andy McFadden2adaf042012-12-18 09:49:45 -0800592 const sp<IGraphicBufferProducer>& bufferProducer) {
593 Composer::getInstance().setDisplaySurface(token, bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700594}
595
596void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
597 uint32_t layerStack) {
598 Composer::getInstance().setDisplayLayerStack(token, layerStack);
599}
600
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700601void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
602 uint32_t orientation,
603 const Rect& layerStackRect,
604 const Rect& displayRect) {
605 Composer::getInstance().setDisplayProjection(token, orientation,
606 layerStackRect, displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700607}
608
609// ----------------------------------------------------------------------------
610
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611status_t SurfaceComposerClient::getDisplayInfo(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700612 const sp<IBinder>& display, DisplayInfo* info)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613{
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700614 return ComposerService::getComposerService()->getDisplayInfo(display, info);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615}
616
Jeff Brown2a09bb32012-10-08 19:13:57 -0700617void SurfaceComposerClient::blankDisplay(const sp<IBinder>& token) {
618 ComposerService::getComposerService()->blank(token);
619}
620
621void SurfaceComposerClient::unblankDisplay(const sp<IBinder>& token) {
622 ComposerService::getComposerService()->unblank(token);
623}
624
Mathias Agopian698c0872011-06-28 19:09:31 -0700625// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800627status_t ScreenshotClient::capture(
628 const sp<IBinder>& display,
629 const sp<IGraphicBufferProducer>& producer,
630 uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -0800631 uint32_t minLayerZ, uint32_t maxLayerZ, bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800632 sp<ISurfaceComposer> s(ComposerService::getComposerService());
633 if (s == NULL) return NO_INIT;
634 return s->captureScreen(display, producer,
Dan Stozac7014012014-02-14 15:03:43 -0800635 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800636}
637
Mathias Agopian74c40c02010-09-29 13:02:36 -0700638ScreenshotClient::ScreenshotClient()
Mathias Agopianabe815d2013-03-19 22:22:21 -0700639 : mHaveBuffer(false) {
640 memset(&mBuffer, 0, sizeof(mBuffer));
Mathias Agopian74c40c02010-09-29 13:02:36 -0700641}
642
Mathias Agopian8000d062013-03-26 18:15:35 -0700643ScreenshotClient::~ScreenshotClient() {
644 ScreenshotClient::release();
645}
646
Mathias Agopianabe815d2013-03-19 22:22:21 -0700647sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
648 if (mCpuConsumer == NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700649 mBufferQueue = new BufferQueue();
650 mCpuConsumer = new CpuConsumer(mBufferQueue, 1);
Mathias Agopianabe815d2013-03-19 22:22:21 -0700651 mCpuConsumer->setName(String8("ScreenshotClient"));
652 }
653 return mCpuConsumer;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800654}
655
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700656status_t ScreenshotClient::update(const sp<IBinder>& display,
657 uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -0800658 uint32_t minLayerZ, uint32_t maxLayerZ,
659 bool useIdentityTransform) {
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800660 sp<ISurfaceComposer> s(ComposerService::getComposerService());
661 if (s == NULL) return NO_INIT;
Mathias Agopianabe815d2013-03-19 22:22:21 -0700662 sp<CpuConsumer> cpuConsumer = getCpuConsumer();
663
664 if (mHaveBuffer) {
665 mCpuConsumer->unlockBuffer(mBuffer);
666 memset(&mBuffer, 0, sizeof(mBuffer));
667 mHaveBuffer = false;
668 }
669
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700670 status_t err = s->captureScreen(display, mBufferQueue,
Dan Stozac7014012014-02-14 15:03:43 -0800671 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
Mathias Agopianabe815d2013-03-19 22:22:21 -0700672
673 if (err == NO_ERROR) {
674 err = mCpuConsumer->lockNextBuffer(&mBuffer);
675 if (err == NO_ERROR) {
676 mHaveBuffer = true;
677 }
678 }
679 return err;
680}
681
Dan Stozac7014012014-02-14 15:03:43 -0800682status_t ScreenshotClient::update(const sp<IBinder>& display,
683 bool useIdentityTransform) {
684 return ScreenshotClient::update(display, 0, 0, 0, -1UL,
685 useIdentityTransform);
Mathias Agopianabe815d2013-03-19 22:22:21 -0700686}
687
688status_t ScreenshotClient::update(const sp<IBinder>& display,
Dan Stozac7014012014-02-14 15:03:43 -0800689 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
690 return ScreenshotClient::update(display, reqWidth, reqHeight, 0, -1UL,
691 useIdentityTransform);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700692}
693
694void ScreenshotClient::release() {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700695 if (mHaveBuffer) {
696 mCpuConsumer->unlockBuffer(mBuffer);
697 memset(&mBuffer, 0, sizeof(mBuffer));
698 mHaveBuffer = false;
699 }
700 mCpuConsumer.clear();
Mathias Agopian74c40c02010-09-29 13:02:36 -0700701}
702
703void const* ScreenshotClient::getPixels() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700704 return mBuffer.data;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700705}
706
707uint32_t ScreenshotClient::getWidth() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700708 return mBuffer.width;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700709}
710
711uint32_t ScreenshotClient::getHeight() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700712 return mBuffer.height;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700713}
714
715PixelFormat ScreenshotClient::getFormat() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700716 return mBuffer.format;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700717}
718
719uint32_t ScreenshotClient::getStride() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700720 return mBuffer.stride;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700721}
722
723size_t ScreenshotClient::getSize() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700724 return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700725}
726
727// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728}; // namespace android