blob: 0c6881a69d9f18870d3caedde5cf0ab6fd31317b [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceComposerClient"
18
19#include <stdint.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <utils/Errors.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/Log.h>
Mathias Agopiand4784a32010-05-27 19:41:15 -070024#include <utils/Singleton.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070025#include <utils/SortedVector.h>
26#include <utils/String8.h>
27#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
Mathias Agopian9cce3252010-02-09 17:46:37 -080029#include <binder/IMemory.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <binder/IServiceManager.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080031
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <ui/DisplayInfo.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
Mathias Agopian90ac7992012-02-25 18:48:35 -080034#include <gui/ISurface.h>
35#include <gui/ISurfaceComposer.h>
36#include <gui/ISurfaceComposerClient.h>
37#include <gui/SurfaceComposerClient.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038
Mathias Agopian41f673c2011-11-17 17:48:35 -080039#include <private/gui/ComposerService.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080040#include <private/gui/LayerState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
42namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043// ---------------------------------------------------------------------------
44
Mathias Agopian7e27f052010-05-28 14:22:23 -070045ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
46
Mathias Agopianb7e930d2010-06-01 15:12:58 -070047ComposerService::ComposerService()
48: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-06 18:45:56 -070049 Mutex::Autolock _l(mLock);
50 connectLocked();
51}
52
53void ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -070054 const String16 name("SurfaceFlinger");
55 while (getService(name, &mComposerService) != NO_ERROR) {
56 usleep(250000);
57 }
Andy McFadden6652b3e2012-09-06 18:45:56 -070058 assert(mComposerService != NULL);
59
60 // Create the death listener.
61 class DeathObserver : public IBinder::DeathRecipient {
62 ComposerService& mComposerService;
63 virtual void binderDied(const wp<IBinder>& who) {
64 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
65 who.unsafe_get());
66 mComposerService.composerServiceDied();
67 }
68 public:
69 DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
70 };
71
72 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
73 mComposerService->asBinder()->linkToDeath(mDeathObserver);
Mathias Agopianb7e930d2010-06-01 15:12:58 -070074}
75
Andy McFadden6652b3e2012-09-06 18:45:56 -070076/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
77 ComposerService& instance = ComposerService::getInstance();
78 Mutex::Autolock _l(instance.mLock);
79 if (instance.mComposerService == NULL) {
80 ComposerService::getInstance().connectLocked();
81 assert(instance.mComposerService != NULL);
82 ALOGD("ComposerService reconnected");
83 }
84 return instance.mComposerService;
85}
86
87void ComposerService::composerServiceDied()
88{
89 Mutex::Autolock _l(mLock);
90 mComposerService = NULL;
91 mDeathObserver = NULL;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070092}
93
Mathias Agopian7e27f052010-05-28 14:22:23 -070094// ---------------------------------------------------------------------------
95
Mathias Agopian698c0872011-06-28 19:09:31 -070096static inline
Mathias Agopiane57f2922012-08-09 16:29:12 -070097int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
Mathias Agopian698c0872011-06-28 19:09:31 -070098 if (lhs.client < rhs.client) return -1;
99 if (lhs.client > rhs.client) return 1;
100 if (lhs.state.surface < rhs.state.surface) return -1;
101 if (lhs.state.surface > rhs.state.surface) return 1;
102 return 0;
103}
104
Mathias Agopiane57f2922012-08-09 16:29:12 -0700105static inline
106int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
107 return compare_type(lhs.token, rhs.token);
108}
109
Mathias Agopian7e27f052010-05-28 14:22:23 -0700110class Composer : public Singleton<Composer>
111{
Mathias Agopiand4784a32010-05-27 19:41:15 -0700112 friend class Singleton<Composer>;
113
Mathias Agopian698c0872011-06-28 19:09:31 -0700114 mutable Mutex mLock;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700115 SortedVector<ComposerState> mComposerStates;
116 SortedVector<DisplayState > mDisplayStates;
Jamie Gennis28378392011-10-12 17:39:00 -0700117 uint32_t mForceSynchronous;
Jeff Brownf3f7db62012-08-31 02:18:38 -0700118 uint32_t mTransactionNestCount;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700119 bool mAnimation;
Mathias Agopian698c0872011-06-28 19:09:31 -0700120
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700121 Composer() : Singleton<Composer>(),
Jeff Brownf3f7db62012-08-31 02:18:38 -0700122 mForceSynchronous(0), mTransactionNestCount(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700123 mAnimation(false)
Jamie Gennis28378392011-10-12 17:39:00 -0700124 { }
Mathias Agopian698c0872011-06-28 19:09:31 -0700125
Jeff Brownf3f7db62012-08-31 02:18:38 -0700126 void openGlobalTransactionImpl();
Jamie Gennis28378392011-10-12 17:39:00 -0700127 void closeGlobalTransactionImpl(bool synchronous);
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700128 void setAnimationTransactionImpl();
Mathias Agopian698c0872011-06-28 19:09:31 -0700129
130 layer_state_t* getLayerStateLocked(
131 const sp<SurfaceComposerClient>& client, SurfaceID id);
132
Mathias Agopiane57f2922012-08-09 16:29:12 -0700133 DisplayState& getDisplayStateLocked(const sp<IBinder>& token);
134
Mathias Agopiand4784a32010-05-27 19:41:15 -0700135public:
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700136 sp<IBinder> createDisplay(const String8& displayName, bool secure);
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700137 sp<IBinder> getBuiltInDisplay(int32_t id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700138
139 status_t setPosition(const sp<SurfaceComposerClient>& client, SurfaceID id,
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700140 float x, float y);
Mathias Agopian698c0872011-06-28 19:09:31 -0700141 status_t setSize(const sp<SurfaceComposerClient>& client, SurfaceID id,
142 uint32_t w, uint32_t h);
143 status_t setLayer(const sp<SurfaceComposerClient>& client, SurfaceID id,
144 int32_t z);
145 status_t setFlags(const sp<SurfaceComposerClient>& client, SurfaceID id,
146 uint32_t flags, uint32_t mask);
147 status_t setTransparentRegionHint(
148 const sp<SurfaceComposerClient>& client, SurfaceID id,
149 const Region& transparentRegion);
150 status_t setAlpha(const sp<SurfaceComposerClient>& client, SurfaceID id,
151 float alpha);
152 status_t setMatrix(const sp<SurfaceComposerClient>& client, SurfaceID id,
153 float dsdx, float dtdx, float dsdy, float dtdy);
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700154 status_t setOrientation(int orientation);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700155 status_t setCrop(const sp<SurfaceComposerClient>& client, SurfaceID id,
156 const Rect& crop);
Mathias Agopian87855782012-07-24 21:41:09 -0700157 status_t setLayerStack(const sp<SurfaceComposerClient>& client,
158 SurfaceID id, uint32_t layerStack);
Mathias Agopian698c0872011-06-28 19:09:31 -0700159
Andy McFadden2adaf042012-12-18 09:49:45 -0800160 void setDisplaySurface(const sp<IBinder>& token,
161 const sp<IGraphicBufferProducer>& bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700162 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700163 void setDisplayProjection(const sp<IBinder>& token,
164 uint32_t orientation,
165 const Rect& layerStackRect,
166 const Rect& displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700167
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700168 static void setAnimationTransaction() {
169 Composer::getInstance().setAnimationTransactionImpl();
170 }
171
Jeff Brownf3f7db62012-08-31 02:18:38 -0700172 static void openGlobalTransaction() {
173 Composer::getInstance().openGlobalTransactionImpl();
174 }
175
Jamie Gennis28378392011-10-12 17:39:00 -0700176 static void closeGlobalTransaction(bool synchronous) {
177 Composer::getInstance().closeGlobalTransactionImpl(synchronous);
Mathias Agopiand4784a32010-05-27 19:41:15 -0700178 }
179};
180
181ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183// ---------------------------------------------------------------------------
184
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700185sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
186 return ComposerService::getComposerService()->createDisplay(displayName,
187 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700188}
189
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700190sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
191 return ComposerService::getComposerService()->getBuiltInDisplay(id);
192}
193
Jeff Brownf3f7db62012-08-31 02:18:38 -0700194void Composer::openGlobalTransactionImpl() {
195 { // scope for the lock
196 Mutex::Autolock _l(mLock);
197 mTransactionNestCount += 1;
198 }
199}
200
Jamie Gennis28378392011-10-12 17:39:00 -0700201void Composer::closeGlobalTransactionImpl(bool synchronous) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700202 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopian698c0872011-06-28 19:09:31 -0700203
204 Vector<ComposerState> transaction;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700205 Vector<DisplayState> displayTransaction;
Jamie Gennis28378392011-10-12 17:39:00 -0700206 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -0700207
208 { // scope for the lock
209 Mutex::Autolock _l(mLock);
Jeff Brownf3f7db62012-08-31 02:18:38 -0700210 mForceSynchronous |= synchronous;
211 if (!mTransactionNestCount) {
212 ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
213 "call to openGlobalTransaction().");
214 } else if (--mTransactionNestCount) {
215 return;
216 }
217
Mathias Agopiane57f2922012-08-09 16:29:12 -0700218 transaction = mComposerStates;
219 mComposerStates.clear();
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700220
Mathias Agopiane57f2922012-08-09 16:29:12 -0700221 displayTransaction = mDisplayStates;
222 mDisplayStates.clear();
Jamie Gennis28378392011-10-12 17:39:00 -0700223
Jeff Brownf3f7db62012-08-31 02:18:38 -0700224 if (mForceSynchronous) {
Jamie Gennis28378392011-10-12 17:39:00 -0700225 flags |= ISurfaceComposer::eSynchronous;
226 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700227 if (mAnimation) {
228 flags |= ISurfaceComposer::eAnimation;
229 }
230
Jamie Gennis28378392011-10-12 17:39:00 -0700231 mForceSynchronous = false;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700232 mAnimation = false;
Mathias Agopian698c0872011-06-28 19:09:31 -0700233 }
234
Mathias Agopian8b33f032012-07-24 20:43:54 -0700235 sm->setTransactionState(transaction, displayTransaction, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236}
237
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700238void Composer::setAnimationTransactionImpl() {
239 Mutex::Autolock _l(mLock);
240 mAnimation = true;
241}
242
Mathias Agopian698c0872011-06-28 19:09:31 -0700243layer_state_t* Composer::getLayerStateLocked(
244 const sp<SurfaceComposerClient>& client, SurfaceID id) {
245
246 ComposerState s;
247 s.client = client->mClient;
248 s.state.surface = id;
249
Mathias Agopiane57f2922012-08-09 16:29:12 -0700250 ssize_t index = mComposerStates.indexOf(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700251 if (index < 0) {
252 // we don't have it, add an initialized layer_state to our list
Mathias Agopiane57f2922012-08-09 16:29:12 -0700253 index = mComposerStates.add(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700254 }
255
Mathias Agopiane57f2922012-08-09 16:29:12 -0700256 ComposerState* const out = mComposerStates.editArray();
Mathias Agopian698c0872011-06-28 19:09:31 -0700257 return &(out[index].state);
258}
259
260status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700261 SurfaceID id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700262 Mutex::Autolock _l(mLock);
263 layer_state_t* s = getLayerStateLocked(client, id);
264 if (!s)
265 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700266 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700267 s->x = x;
268 s->y = y;
269 return NO_ERROR;
270}
271
272status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
273 SurfaceID id, uint32_t w, uint32_t h) {
274 Mutex::Autolock _l(mLock);
275 layer_state_t* s = getLayerStateLocked(client, id);
276 if (!s)
277 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700278 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700279 s->w = w;
280 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700281
282 // Resizing a surface makes the transaction synchronous.
283 mForceSynchronous = true;
284
Mathias Agopian698c0872011-06-28 19:09:31 -0700285 return NO_ERROR;
286}
287
288status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
289 SurfaceID id, int32_t z) {
290 Mutex::Autolock _l(mLock);
291 layer_state_t* s = getLayerStateLocked(client, id);
292 if (!s)
293 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700294 s->what |= layer_state_t::eLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700295 s->z = z;
296 return NO_ERROR;
297}
298
299status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
300 SurfaceID id, uint32_t flags,
301 uint32_t mask) {
302 Mutex::Autolock _l(mLock);
303 layer_state_t* s = getLayerStateLocked(client, id);
304 if (!s)
305 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700306 s->what |= layer_state_t::eVisibilityChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700307 s->flags &= ~mask;
308 s->flags |= (flags & mask);
309 s->mask |= mask;
310 return NO_ERROR;
311}
312
313status_t Composer::setTransparentRegionHint(
314 const sp<SurfaceComposerClient>& client, SurfaceID id,
315 const Region& transparentRegion) {
316 Mutex::Autolock _l(mLock);
317 layer_state_t* s = getLayerStateLocked(client, id);
318 if (!s)
319 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700320 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700321 s->transparentRegion = transparentRegion;
322 return NO_ERROR;
323}
324
325status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
326 SurfaceID id, float alpha) {
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::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700332 s->alpha = alpha;
333 return NO_ERROR;
334}
335
Mathias Agopian87855782012-07-24 21:41:09 -0700336status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
337 SurfaceID id, uint32_t layerStack) {
338 Mutex::Autolock _l(mLock);
339 layer_state_t* s = getLayerStateLocked(client, id);
340 if (!s)
341 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700342 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700343 s->layerStack = layerStack;
344 return NO_ERROR;
345}
346
Mathias Agopian698c0872011-06-28 19:09:31 -0700347status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
348 SurfaceID id, float dsdx, float dtdx,
349 float dsdy, float dtdy) {
350 Mutex::Autolock _l(mLock);
351 layer_state_t* s = getLayerStateLocked(client, id);
352 if (!s)
353 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700354 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700355 layer_state_t::matrix22_t matrix;
356 matrix.dsdx = dsdx;
357 matrix.dtdx = dtdx;
358 matrix.dsdy = dsdy;
359 matrix.dtdy = dtdy;
360 s->matrix = matrix;
361 return NO_ERROR;
362}
363
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700364status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
365 SurfaceID id, const Rect& crop) {
366 Mutex::Autolock _l(mLock);
367 layer_state_t* s = getLayerStateLocked(client, id);
368 if (!s)
369 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700370 s->what |= layer_state_t::eCropChanged;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700371 s->crop = crop;
372 return NO_ERROR;
373}
374
Mathias Agopian698c0872011-06-28 19:09:31 -0700375// ---------------------------------------------------------------------------
376
Mathias Agopiane57f2922012-08-09 16:29:12 -0700377DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
378 DisplayState s;
379 s.token = token;
380 ssize_t index = mDisplayStates.indexOf(s);
381 if (index < 0) {
382 // we don't have it, add an initialized layer_state to our list
383 s.what = 0;
384 index = mDisplayStates.add(s);
385 }
386 return mDisplayStates.editItemAt(index);
387}
388
389void Composer::setDisplaySurface(const sp<IBinder>& token,
Andy McFadden2adaf042012-12-18 09:49:45 -0800390 const sp<IGraphicBufferProducer>& bufferProducer) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700391 Mutex::Autolock _l(mLock);
392 DisplayState& s(getDisplayStateLocked(token));
Andy McFadden2adaf042012-12-18 09:49:45 -0800393 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700394 s.what |= DisplayState::eSurfaceChanged;
395}
396
397void Composer::setDisplayLayerStack(const sp<IBinder>& token,
398 uint32_t layerStack) {
399 Mutex::Autolock _l(mLock);
400 DisplayState& s(getDisplayStateLocked(token));
401 s.layerStack = layerStack;
402 s.what |= DisplayState::eLayerStackChanged;
403}
404
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700405void Composer::setDisplayProjection(const sp<IBinder>& token,
406 uint32_t orientation,
407 const Rect& layerStackRect,
408 const Rect& displayRect) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700409 Mutex::Autolock _l(mLock);
410 DisplayState& s(getDisplayStateLocked(token));
411 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700412 s.viewport = layerStackRect;
413 s.frame = displayRect;
414 s.what |= DisplayState::eDisplayProjectionChanged;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700415 mForceSynchronous = true; // TODO: do we actually still need this?
416}
417
Mathias Agopiane57f2922012-08-09 16:29:12 -0700418// ---------------------------------------------------------------------------
419
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420SurfaceComposerClient::SurfaceComposerClient()
Mathias Agopian698c0872011-06-28 19:09:31 -0700421 : mStatus(NO_INIT), mComposer(Composer::getInstance())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423}
424
Mathias Agopian698c0872011-06-28 19:09:31 -0700425void SurfaceComposerClient::onFirstRef() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700426 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700427 if (sm != 0) {
Mathias Agopian7e27f052010-05-28 14:22:23 -0700428 sp<ISurfaceComposerClient> conn = sm->createConnection();
Mathias Agopiand4784a32010-05-27 19:41:15 -0700429 if (conn != 0) {
430 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700431 mStatus = NO_ERROR;
432 }
433 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434}
435
Mathias Agopian698c0872011-06-28 19:09:31 -0700436SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -0700437 dispose();
438}
Mathias Agopiandd3423c2009-09-23 15:44:05 -0700439
Mathias Agopian698c0872011-06-28 19:09:31 -0700440status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441 return mStatus;
442}
443
Mathias Agopian698c0872011-06-28 19:09:31 -0700444sp<IBinder> SurfaceComposerClient::connection() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 return (mClient != 0) ? mClient->asBinder() : 0;
446}
447
Mathias Agopiand4784a32010-05-27 19:41:15 -0700448status_t SurfaceComposerClient::linkToComposerDeath(
449 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -0700450 void* cookie, uint32_t flags) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700451 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700452 return sm->asBinder()->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453}
454
Mathias Agopian698c0872011-06-28 19:09:31 -0700455void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -0700457 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700458 Mutex::Autolock _lm(mLock);
459 if (mClient != 0) {
Mathias Agopiand4784a32010-05-27 19:41:15 -0700460 client = mClient; // hold ref while lock is held
461 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800462 }
Mathias Agopiand4784a32010-05-27 19:41:15 -0700463 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464}
465
Mathias Agopian698c0872011-06-28 19:09:31 -0700466sp<SurfaceControl> SurfaceComposerClient::createSurface(
Mathias Agopian698c0872011-06-28 19:09:31 -0700467 const String8& name,
Mathias Agopian698c0872011-06-28 19:09:31 -0700468 uint32_t w,
469 uint32_t h,
470 PixelFormat format,
471 uint32_t flags)
472{
473 sp<SurfaceControl> result;
474 if (mStatus == NO_ERROR) {
475 ISurfaceComposerClient::surface_data_t data;
476 sp<ISurface> surface = mClient->createSurface(&data, name,
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700477 w, h, format, flags);
Mathias Agopian698c0872011-06-28 19:09:31 -0700478 if (surface != 0) {
Mathias Agopianc10d9d92011-07-20 16:46:11 -0700479 result = new SurfaceControl(this, surface, data);
Mathias Agopian698c0872011-06-28 19:09:31 -0700480 }
481 }
482 return result;
483}
484
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700485sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
486 bool secure) {
487 return Composer::getInstance().createDisplay(displayName, secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700488}
489
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700490sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
491 return Composer::getInstance().getBuiltInDisplay(id);
492}
493
Mathias Agopian698c0872011-06-28 19:09:31 -0700494status_t SurfaceComposerClient::destroySurface(SurfaceID sid) {
495 if (mStatus != NO_ERROR)
496 return mStatus;
497 status_t err = mClient->destroySurface(sid);
498 return err;
499}
500
501inline Composer& SurfaceComposerClient::getComposer() {
502 return mComposer;
503}
504
505// ----------------------------------------------------------------------------
506
507void SurfaceComposerClient::openGlobalTransaction() {
Jeff Brownf3f7db62012-08-31 02:18:38 -0700508 Composer::openGlobalTransaction();
Mathias Agopian698c0872011-06-28 19:09:31 -0700509}
510
Jamie Gennis28378392011-10-12 17:39:00 -0700511void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
512 Composer::closeGlobalTransaction(synchronous);
Mathias Agopian698c0872011-06-28 19:09:31 -0700513}
514
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700515void SurfaceComposerClient::setAnimationTransaction() {
516 Composer::setAnimationTransaction();
517}
518
Mathias Agopian698c0872011-06-28 19:09:31 -0700519// ----------------------------------------------------------------------------
520
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700521status_t SurfaceComposerClient::setCrop(SurfaceID id, const Rect& crop) {
522 return getComposer().setCrop(this, id, crop);
523}
524
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700525status_t SurfaceComposerClient::setPosition(SurfaceID id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700526 return getComposer().setPosition(this, id, x, y);
527}
528
529status_t SurfaceComposerClient::setSize(SurfaceID id, uint32_t w, uint32_t h) {
530 return getComposer().setSize(this, id, w, h);
531}
532
533status_t SurfaceComposerClient::setLayer(SurfaceID id, int32_t z) {
534 return getComposer().setLayer(this, id, z);
535}
536
537status_t SurfaceComposerClient::hide(SurfaceID id) {
538 return getComposer().setFlags(this, id,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700539 layer_state_t::eLayerHidden,
540 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700541}
542
Jeff Brown380223b2012-08-26 22:49:35 -0700543status_t SurfaceComposerClient::show(SurfaceID id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700544 return getComposer().setFlags(this, id,
545 0,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700546 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700547}
548
Mathias Agopian698c0872011-06-28 19:09:31 -0700549status_t SurfaceComposerClient::setFlags(SurfaceID id, uint32_t flags,
550 uint32_t mask) {
551 return getComposer().setFlags(this, id, flags, mask);
552}
553
554status_t SurfaceComposerClient::setTransparentRegionHint(SurfaceID id,
555 const Region& transparentRegion) {
556 return getComposer().setTransparentRegionHint(this, id, transparentRegion);
557}
558
559status_t SurfaceComposerClient::setAlpha(SurfaceID id, float alpha) {
560 return getComposer().setAlpha(this, id, alpha);
561}
562
Mathias Agopian87855782012-07-24 21:41:09 -0700563status_t SurfaceComposerClient::setLayerStack(SurfaceID id, uint32_t layerStack) {
564 return getComposer().setLayerStack(this, id, layerStack);
565}
566
Mathias Agopian698c0872011-06-28 19:09:31 -0700567status_t SurfaceComposerClient::setMatrix(SurfaceID id, float dsdx, float dtdx,
568 float dsdy, float dtdy) {
569 return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
570}
571
572// ----------------------------------------------------------------------------
573
Mathias Agopiane57f2922012-08-09 16:29:12 -0700574void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
Andy McFadden2adaf042012-12-18 09:49:45 -0800575 const sp<IGraphicBufferProducer>& bufferProducer) {
576 Composer::getInstance().setDisplaySurface(token, bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700577}
578
579void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
580 uint32_t layerStack) {
581 Composer::getInstance().setDisplayLayerStack(token, layerStack);
582}
583
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700584void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
585 uint32_t orientation,
586 const Rect& layerStackRect,
587 const Rect& displayRect) {
588 Composer::getInstance().setDisplayProjection(token, orientation,
589 layerStackRect, displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700590}
591
592// ----------------------------------------------------------------------------
593
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594status_t SurfaceComposerClient::getDisplayInfo(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700595 const sp<IBinder>& display, DisplayInfo* info)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596{
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700597 return ComposerService::getComposerService()->getDisplayInfo(display, info);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598}
599
Jeff Brown2a09bb32012-10-08 19:13:57 -0700600void SurfaceComposerClient::blankDisplay(const sp<IBinder>& token) {
601 ComposerService::getComposerService()->blank(token);
602}
603
604void SurfaceComposerClient::unblankDisplay(const sp<IBinder>& token) {
605 ComposerService::getComposerService()->unblank(token);
606}
607
Mathias Agopian698c0872011-06-28 19:09:31 -0700608// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609
Mathias Agopian74c40c02010-09-29 13:02:36 -0700610ScreenshotClient::ScreenshotClient()
611 : mWidth(0), mHeight(0), mFormat(PIXEL_FORMAT_NONE) {
612}
613
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700614status_t ScreenshotClient::update(const sp<IBinder>& display) {
Mathias Agopian74c40c02010-09-29 13:02:36 -0700615 sp<ISurfaceComposer> s(ComposerService::getComposerService());
616 if (s == NULL) return NO_INIT;
617 mHeap = 0;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700618 return s->captureScreen(display, &mHeap,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800619 &mWidth, &mHeight, &mFormat, 0, 0,
620 0, -1UL);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700621}
622
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700623status_t ScreenshotClient::update(const sp<IBinder>& display,
624 uint32_t reqWidth, uint32_t reqHeight) {
Mathias Agopian74c40c02010-09-29 13:02:36 -0700625 sp<ISurfaceComposer> s(ComposerService::getComposerService());
626 if (s == NULL) return NO_INIT;
627 mHeap = 0;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700628 return s->captureScreen(display, &mHeap,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800629 &mWidth, &mHeight, &mFormat, reqWidth, reqHeight,
630 0, -1UL);
631}
632
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700633status_t ScreenshotClient::update(const sp<IBinder>& display,
634 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800635 uint32_t minLayerZ, uint32_t maxLayerZ) {
636 sp<ISurfaceComposer> s(ComposerService::getComposerService());
637 if (s == NULL) return NO_INIT;
638 mHeap = 0;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700639 return s->captureScreen(display, &mHeap,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800640 &mWidth, &mHeight, &mFormat, reqWidth, reqHeight,
641 minLayerZ, maxLayerZ);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700642}
643
644void ScreenshotClient::release() {
645 mHeap = 0;
646}
647
648void const* ScreenshotClient::getPixels() const {
649 return mHeap->getBase();
650}
651
652uint32_t ScreenshotClient::getWidth() const {
653 return mWidth;
654}
655
656uint32_t ScreenshotClient::getHeight() const {
657 return mHeight;
658}
659
660PixelFormat ScreenshotClient::getFormat() const {
661 return mFormat;
662}
663
664uint32_t ScreenshotClient::getStride() const {
665 return mWidth;
666}
667
668size_t ScreenshotClient::getSize() const {
669 return mHeap->getSize();
670}
671
672// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673}; // namespace android