blob: eedeca18d1b500030b59b2c2b669343044fb5b64 [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
Svetoslavd85084b2014-03-20 10:28:31 -0700518status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
519 if (mStatus != NO_ERROR) {
520 return mStatus;
521 }
522 return mClient->clearLayerFrameStats(token);
523}
524
525status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
526 FrameStats* outStats) const {
527 if (mStatus != NO_ERROR) {
528 return mStatus;
529 }
530 return mClient->getLayerFrameStats(token, outStats);
531}
532
Mathias Agopian698c0872011-06-28 19:09:31 -0700533inline Composer& SurfaceComposerClient::getComposer() {
534 return mComposer;
535}
536
537// ----------------------------------------------------------------------------
538
539void SurfaceComposerClient::openGlobalTransaction() {
Jeff Brownf3f7db62012-08-31 02:18:38 -0700540 Composer::openGlobalTransaction();
Mathias Agopian698c0872011-06-28 19:09:31 -0700541}
542
Jamie Gennis28378392011-10-12 17:39:00 -0700543void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
544 Composer::closeGlobalTransaction(synchronous);
Mathias Agopian698c0872011-06-28 19:09:31 -0700545}
546
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700547void SurfaceComposerClient::setAnimationTransaction() {
548 Composer::setAnimationTransaction();
549}
550
Mathias Agopian698c0872011-06-28 19:09:31 -0700551// ----------------------------------------------------------------------------
552
Mathias Agopianac9fa422013-02-11 16:40:36 -0800553status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700554 return getComposer().setCrop(this, id, crop);
555}
556
Mathias Agopianac9fa422013-02-11 16:40:36 -0800557status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700558 return getComposer().setPosition(this, id, x, y);
559}
560
Mathias Agopianac9fa422013-02-11 16:40:36 -0800561status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700562 return getComposer().setSize(this, id, w, h);
563}
564
Mathias Agopianac9fa422013-02-11 16:40:36 -0800565status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700566 return getComposer().setLayer(this, id, z);
567}
568
Mathias Agopianac9fa422013-02-11 16:40:36 -0800569status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700570 return getComposer().setFlags(this, id,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700571 layer_state_t::eLayerHidden,
572 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700573}
574
Mathias Agopianac9fa422013-02-11 16:40:36 -0800575status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700576 return getComposer().setFlags(this, id,
577 0,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700578 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700579}
580
Mathias Agopianac9fa422013-02-11 16:40:36 -0800581status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700582 uint32_t mask) {
583 return getComposer().setFlags(this, id, flags, mask);
584}
585
Mathias Agopianac9fa422013-02-11 16:40:36 -0800586status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700587 const Region& transparentRegion) {
588 return getComposer().setTransparentRegionHint(this, id, transparentRegion);
589}
590
Mathias Agopianac9fa422013-02-11 16:40:36 -0800591status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700592 return getComposer().setAlpha(this, id, alpha);
593}
594
Mathias Agopianac9fa422013-02-11 16:40:36 -0800595status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700596 return getComposer().setLayerStack(this, id, layerStack);
597}
598
Mathias Agopianac9fa422013-02-11 16:40:36 -0800599status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
Mathias Agopian698c0872011-06-28 19:09:31 -0700600 float dsdy, float dtdy) {
601 return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
602}
603
604// ----------------------------------------------------------------------------
605
Mathias Agopiane57f2922012-08-09 16:29:12 -0700606void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
Andy McFadden2adaf042012-12-18 09:49:45 -0800607 const sp<IGraphicBufferProducer>& bufferProducer) {
608 Composer::getInstance().setDisplaySurface(token, bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700609}
610
611void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
612 uint32_t layerStack) {
613 Composer::getInstance().setDisplayLayerStack(token, layerStack);
614}
615
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700616void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
617 uint32_t orientation,
618 const Rect& layerStackRect,
619 const Rect& displayRect) {
620 Composer::getInstance().setDisplayProjection(token, orientation,
621 layerStackRect, displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700622}
623
624// ----------------------------------------------------------------------------
625
Dan Stoza7f7da322014-05-02 15:26:25 -0700626status_t SurfaceComposerClient::getDisplayConfigs(
627 const sp<IBinder>& display, Vector<DisplayInfo>* configs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628{
Dan Stoza7f7da322014-05-02 15:26:25 -0700629 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
630}
631
632status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
633 DisplayInfo* info) {
634 Vector<DisplayInfo> configs;
635 status_t result = getDisplayConfigs(display, &configs);
636 if (result != NO_ERROR) {
637 return result;
638 }
639
640 int activeId = getActiveConfig(display);
641 if (activeId < 0) {
642 ALOGE("No active configuration found");
643 return NAME_NOT_FOUND;
644 }
645
646 *info = configs[activeId];
647 return NO_ERROR;
648}
649
650int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
651 return ComposerService::getComposerService()->getActiveConfig(display);
652}
653
654status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
655 return ComposerService::getComposerService()->setActiveConfig(display, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656}
657
Jeff Brown2a09bb32012-10-08 19:13:57 -0700658void SurfaceComposerClient::blankDisplay(const sp<IBinder>& token) {
659 ComposerService::getComposerService()->blank(token);
660}
661
662void SurfaceComposerClient::unblankDisplay(const sp<IBinder>& token) {
663 ComposerService::getComposerService()->unblank(token);
664}
665
Svetoslavd85084b2014-03-20 10:28:31 -0700666status_t SurfaceComposerClient::clearAnimationFrameStats() {
667 return ComposerService::getComposerService()->clearAnimationFrameStats();
668}
669
670status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
671 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
672}
673
Mathias Agopian698c0872011-06-28 19:09:31 -0700674// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800676status_t ScreenshotClient::capture(
677 const sp<IBinder>& display,
678 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -0700679 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -0800680 uint32_t minLayerZ, uint32_t maxLayerZ, bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800681 sp<ISurfaceComposer> s(ComposerService::getComposerService());
682 if (s == NULL) return NO_INIT;
Dan Stozac1879002014-05-22 15:59:05 -0700683 return s->captureScreen(display, producer, sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -0800684 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800685}
686
Mathias Agopian74c40c02010-09-29 13:02:36 -0700687ScreenshotClient::ScreenshotClient()
Mathias Agopianabe815d2013-03-19 22:22:21 -0700688 : mHaveBuffer(false) {
689 memset(&mBuffer, 0, sizeof(mBuffer));
Mathias Agopian74c40c02010-09-29 13:02:36 -0700690}
691
Mathias Agopian8000d062013-03-26 18:15:35 -0700692ScreenshotClient::~ScreenshotClient() {
693 ScreenshotClient::release();
694}
695
Mathias Agopianabe815d2013-03-19 22:22:21 -0700696sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
697 if (mCpuConsumer == NULL) {
Dan Stoza6d5a7bb2014-03-13 11:39:09 -0700698 sp<IGraphicBufferConsumer> consumer;
699 BufferQueue::createBufferQueue(&mProducer, &consumer);
700 mCpuConsumer = new CpuConsumer(consumer, 1);
Mathias Agopianabe815d2013-03-19 22:22:21 -0700701 mCpuConsumer->setName(String8("ScreenshotClient"));
702 }
703 return mCpuConsumer;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800704}
705
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700706status_t ScreenshotClient::update(const sp<IBinder>& display,
Dan Stozac1879002014-05-22 15:59:05 -0700707 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -0800708 uint32_t minLayerZ, uint32_t maxLayerZ,
709 bool useIdentityTransform) {
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800710 sp<ISurfaceComposer> s(ComposerService::getComposerService());
711 if (s == NULL) return NO_INIT;
Mathias Agopianabe815d2013-03-19 22:22:21 -0700712 sp<CpuConsumer> cpuConsumer = getCpuConsumer();
713
714 if (mHaveBuffer) {
715 mCpuConsumer->unlockBuffer(mBuffer);
716 memset(&mBuffer, 0, sizeof(mBuffer));
717 mHaveBuffer = false;
718 }
719
Dan Stozac1879002014-05-22 15:59:05 -0700720 status_t err = s->captureScreen(display, mProducer, sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -0800721 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
Mathias Agopianabe815d2013-03-19 22:22:21 -0700722
723 if (err == NO_ERROR) {
724 err = mCpuConsumer->lockNextBuffer(&mBuffer);
725 if (err == NO_ERROR) {
726 mHaveBuffer = true;
727 }
728 }
729 return err;
730}
731
Dan Stozac1879002014-05-22 15:59:05 -0700732status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -0800733 bool useIdentityTransform) {
Dan Stozac1879002014-05-22 15:59:05 -0700734 return ScreenshotClient::update(display, sourceCrop, 0, 0, 0, -1UL,
Dan Stozac7014012014-02-14 15:03:43 -0800735 useIdentityTransform);
Mathias Agopianabe815d2013-03-19 22:22:21 -0700736}
737
Dan Stozac1879002014-05-22 15:59:05 -0700738status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -0800739 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
Dan Stozac1879002014-05-22 15:59:05 -0700740 return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
741 0, -1UL, useIdentityTransform);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700742}
743
744void ScreenshotClient::release() {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700745 if (mHaveBuffer) {
746 mCpuConsumer->unlockBuffer(mBuffer);
747 memset(&mBuffer, 0, sizeof(mBuffer));
748 mHaveBuffer = false;
749 }
750 mCpuConsumer.clear();
Mathias Agopian74c40c02010-09-29 13:02:36 -0700751}
752
753void const* ScreenshotClient::getPixels() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700754 return mBuffer.data;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700755}
756
757uint32_t ScreenshotClient::getWidth() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700758 return mBuffer.width;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700759}
760
761uint32_t ScreenshotClient::getHeight() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700762 return mBuffer.height;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700763}
764
765PixelFormat ScreenshotClient::getFormat() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700766 return mBuffer.format;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700767}
768
769uint32_t ScreenshotClient::getStride() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700770 return mBuffer.stride;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700771}
772
773size_t ScreenshotClient::getSize() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700774 return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700775}
776
777// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778}; // namespace android