2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "SurfaceComposerClient"
20 #include <sys/types.h>
22 #include <utils/Errors.h>
23 #include <utils/Log.h>
24 #include <utils/Singleton.h>
25 #include <utils/SortedVector.h>
26 #include <utils/String8.h>
27 #include <utils/threads.h>
29 #include <binder/IMemory.h>
30 #include <binder/IServiceManager.h>
32 #include <ui/DisplayInfo.h>
34 #include <gui/ISurface.h>
35 #include <gui/ISurfaceComposer.h>
36 #include <gui/ISurfaceComposerClient.h>
37 #include <gui/SurfaceComposerClient.h>
39 #include <private/gui/ComposerService.h>
40 #include <private/gui/LayerState.h>
43 // ---------------------------------------------------------------------------
45 ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
47 ComposerService::ComposerService()
48 : Singleton<ComposerService>() {
49 Mutex::Autolock _l(mLock);
53 void ComposerService::connectLocked() {
54 const String16 name("SurfaceFlinger");
55 while (getService(name, &mComposerService) != NO_ERROR) {
58 assert(mComposerService != NULL);
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]",
66 mComposerService.composerServiceDied();
69 DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
72 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
73 mComposerService->asBinder()->linkToDeath(mDeathObserver);
76 /*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");
84 return instance.mComposerService;
87 void ComposerService::composerServiceDied()
89 Mutex::Autolock _l(mLock);
90 mComposerService = NULL;
91 mDeathObserver = NULL;
94 // ---------------------------------------------------------------------------
97 int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
98 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;
106 int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
107 return compare_type(lhs.token, rhs.token);
110 class Composer : public Singleton<Composer>
112 friend class Singleton<Composer>;
115 SortedVector<ComposerState> mComposerStates;
116 SortedVector<DisplayState > mDisplayStates;
117 uint32_t mForceSynchronous;
120 Composer() : Singleton<Composer>(),
121 mForceSynchronous(0),
125 void closeGlobalTransactionImpl(bool synchronous);
126 void setAnimationTransactionImpl();
128 layer_state_t* getLayerStateLocked(
129 const sp<SurfaceComposerClient>& client, SurfaceID id);
131 DisplayState& getDisplayStateLocked(const sp<IBinder>& token);
134 sp<IBinder> createDisplay(const String8& displayName);
135 sp<IBinder> getBuiltInDisplay(int32_t id);
137 status_t setPosition(const sp<SurfaceComposerClient>& client, SurfaceID id,
139 status_t setSize(const sp<SurfaceComposerClient>& client, SurfaceID id,
140 uint32_t w, uint32_t h);
141 status_t setLayer(const sp<SurfaceComposerClient>& client, SurfaceID id,
143 status_t setFlags(const sp<SurfaceComposerClient>& client, SurfaceID id,
144 uint32_t flags, uint32_t mask);
145 status_t setTransparentRegionHint(
146 const sp<SurfaceComposerClient>& client, SurfaceID id,
147 const Region& transparentRegion);
148 status_t setAlpha(const sp<SurfaceComposerClient>& client, SurfaceID id,
150 status_t setMatrix(const sp<SurfaceComposerClient>& client, SurfaceID id,
151 float dsdx, float dtdx, float dsdy, float dtdy);
152 status_t setOrientation(int orientation);
153 status_t setCrop(const sp<SurfaceComposerClient>& client, SurfaceID id,
155 status_t setLayerStack(const sp<SurfaceComposerClient>& client,
156 SurfaceID id, uint32_t layerStack);
158 void setDisplaySurface(const sp<IBinder>& token, const sp<ISurfaceTexture>& surface);
159 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
160 void setDisplayProjection(const sp<IBinder>& token,
161 uint32_t orientation,
162 const Rect& layerStackRect,
163 const Rect& displayRect);
165 static void setAnimationTransaction() {
166 Composer::getInstance().setAnimationTransactionImpl();
169 static void closeGlobalTransaction(bool synchronous) {
170 Composer::getInstance().closeGlobalTransactionImpl(synchronous);
174 ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
176 // ---------------------------------------------------------------------------
178 sp<IBinder> Composer::createDisplay(const String8& displayName) {
179 return ComposerService::getComposerService()->createDisplay(displayName);
182 sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
183 return ComposerService::getComposerService()->getBuiltInDisplay(id);
186 void Composer::closeGlobalTransactionImpl(bool synchronous) {
187 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
189 Vector<ComposerState> transaction;
190 Vector<DisplayState> displayTransaction;
193 { // scope for the lock
194 Mutex::Autolock _l(mLock);
195 transaction = mComposerStates;
196 mComposerStates.clear();
198 displayTransaction = mDisplayStates;
199 mDisplayStates.clear();
201 if (synchronous || mForceSynchronous) {
202 flags |= ISurfaceComposer::eSynchronous;
205 flags |= ISurfaceComposer::eAnimation;
208 mForceSynchronous = false;
212 sm->setTransactionState(transaction, displayTransaction, flags);
215 void Composer::setAnimationTransactionImpl() {
216 Mutex::Autolock _l(mLock);
220 layer_state_t* Composer::getLayerStateLocked(
221 const sp<SurfaceComposerClient>& client, SurfaceID id) {
224 s.client = client->mClient;
225 s.state.surface = id;
227 ssize_t index = mComposerStates.indexOf(s);
229 // we don't have it, add an initialized layer_state to our list
230 index = mComposerStates.add(s);
233 ComposerState* const out = mComposerStates.editArray();
234 return &(out[index].state);
237 status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
238 SurfaceID id, float x, float y) {
239 Mutex::Autolock _l(mLock);
240 layer_state_t* s = getLayerStateLocked(client, id);
243 s->what |= layer_state_t::ePositionChanged;
249 status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
250 SurfaceID id, uint32_t w, uint32_t h) {
251 Mutex::Autolock _l(mLock);
252 layer_state_t* s = getLayerStateLocked(client, id);
255 s->what |= layer_state_t::eSizeChanged;
259 // Resizing a surface makes the transaction synchronous.
260 mForceSynchronous = true;
265 status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
266 SurfaceID id, int32_t z) {
267 Mutex::Autolock _l(mLock);
268 layer_state_t* s = getLayerStateLocked(client, id);
271 s->what |= layer_state_t::eLayerChanged;
276 status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
277 SurfaceID id, uint32_t flags,
279 Mutex::Autolock _l(mLock);
280 layer_state_t* s = getLayerStateLocked(client, id);
283 s->what |= layer_state_t::eVisibilityChanged;
285 s->flags |= (flags & mask);
290 status_t Composer::setTransparentRegionHint(
291 const sp<SurfaceComposerClient>& client, SurfaceID id,
292 const Region& transparentRegion) {
293 Mutex::Autolock _l(mLock);
294 layer_state_t* s = getLayerStateLocked(client, id);
297 s->what |= layer_state_t::eTransparentRegionChanged;
298 s->transparentRegion = transparentRegion;
302 status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
303 SurfaceID id, float alpha) {
304 Mutex::Autolock _l(mLock);
305 layer_state_t* s = getLayerStateLocked(client, id);
308 s->what |= layer_state_t::eAlphaChanged;
313 status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
314 SurfaceID id, uint32_t layerStack) {
315 Mutex::Autolock _l(mLock);
316 layer_state_t* s = getLayerStateLocked(client, id);
319 s->what |= layer_state_t::eLayerStackChanged;
320 s->layerStack = layerStack;
324 status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
325 SurfaceID id, float dsdx, float dtdx,
326 float dsdy, float dtdy) {
327 Mutex::Autolock _l(mLock);
328 layer_state_t* s = getLayerStateLocked(client, id);
331 s->what |= layer_state_t::eMatrixChanged;
332 layer_state_t::matrix22_t matrix;
341 status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
342 SurfaceID id, const Rect& crop) {
343 Mutex::Autolock _l(mLock);
344 layer_state_t* s = getLayerStateLocked(client, id);
347 s->what |= layer_state_t::eCropChanged;
352 // ---------------------------------------------------------------------------
354 DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
357 ssize_t index = mDisplayStates.indexOf(s);
359 // we don't have it, add an initialized layer_state to our list
361 index = mDisplayStates.add(s);
363 return mDisplayStates.editItemAt(index);
366 void Composer::setDisplaySurface(const sp<IBinder>& token,
367 const sp<ISurfaceTexture>& surface) {
368 Mutex::Autolock _l(mLock);
369 DisplayState& s(getDisplayStateLocked(token));
371 s.what |= DisplayState::eSurfaceChanged;
374 void Composer::setDisplayLayerStack(const sp<IBinder>& token,
375 uint32_t layerStack) {
376 Mutex::Autolock _l(mLock);
377 DisplayState& s(getDisplayStateLocked(token));
378 s.layerStack = layerStack;
379 s.what |= DisplayState::eLayerStackChanged;
382 void Composer::setDisplayProjection(const sp<IBinder>& token,
383 uint32_t orientation,
384 const Rect& layerStackRect,
385 const Rect& displayRect) {
386 Mutex::Autolock _l(mLock);
387 DisplayState& s(getDisplayStateLocked(token));
388 s.orientation = orientation;
389 s.viewport = layerStackRect;
390 s.frame = displayRect;
391 s.what |= DisplayState::eDisplayProjectionChanged;
392 mForceSynchronous = true; // TODO: do we actually still need this?
395 // ---------------------------------------------------------------------------
397 SurfaceComposerClient::SurfaceComposerClient()
398 : mStatus(NO_INIT), mComposer(Composer::getInstance())
402 void SurfaceComposerClient::onFirstRef() {
403 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
405 sp<ISurfaceComposerClient> conn = sm->createConnection();
413 SurfaceComposerClient::~SurfaceComposerClient() {
417 status_t SurfaceComposerClient::initCheck() const {
421 sp<IBinder> SurfaceComposerClient::connection() const {
422 return (mClient != 0) ? mClient->asBinder() : 0;
425 status_t SurfaceComposerClient::linkToComposerDeath(
426 const sp<IBinder::DeathRecipient>& recipient,
427 void* cookie, uint32_t flags) {
428 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
429 return sm->asBinder()->linkToDeath(recipient, cookie, flags);
432 void SurfaceComposerClient::dispose() {
433 // this can be called more than once.
434 sp<ISurfaceComposerClient> client;
435 Mutex::Autolock _lm(mLock);
437 client = mClient; // hold ref while lock is held
443 sp<SurfaceControl> SurfaceComposerClient::createSurface(
450 sp<SurfaceControl> result;
451 if (mStatus == NO_ERROR) {
452 ISurfaceComposerClient::surface_data_t data;
453 sp<ISurface> surface = mClient->createSurface(&data, name,
454 w, h, format, flags);
456 result = new SurfaceControl(this, surface, data);
462 sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName) {
463 return Composer::getInstance().createDisplay(displayName);
466 sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
467 return Composer::getInstance().getBuiltInDisplay(id);
470 status_t SurfaceComposerClient::destroySurface(SurfaceID sid) {
471 if (mStatus != NO_ERROR)
473 status_t err = mClient->destroySurface(sid);
477 inline Composer& SurfaceComposerClient::getComposer() {
481 // ----------------------------------------------------------------------------
483 void SurfaceComposerClient::openGlobalTransaction() {
487 void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
488 Composer::closeGlobalTransaction(synchronous);
491 void SurfaceComposerClient::setAnimationTransaction() {
492 Composer::setAnimationTransaction();
495 // ----------------------------------------------------------------------------
497 status_t SurfaceComposerClient::setCrop(SurfaceID id, const Rect& crop) {
498 return getComposer().setCrop(this, id, crop);
501 status_t SurfaceComposerClient::setPosition(SurfaceID id, float x, float y) {
502 return getComposer().setPosition(this, id, x, y);
505 status_t SurfaceComposerClient::setSize(SurfaceID id, uint32_t w, uint32_t h) {
506 return getComposer().setSize(this, id, w, h);
509 status_t SurfaceComposerClient::setLayer(SurfaceID id, int32_t z) {
510 return getComposer().setLayer(this, id, z);
513 status_t SurfaceComposerClient::hide(SurfaceID id) {
514 return getComposer().setFlags(this, id,
515 layer_state_t::eLayerHidden,
516 layer_state_t::eLayerHidden);
519 status_t SurfaceComposerClient::show(SurfaceID id) {
520 return getComposer().setFlags(this, id,
522 layer_state_t::eLayerHidden);
525 status_t SurfaceComposerClient::setFlags(SurfaceID id, uint32_t flags,
527 return getComposer().setFlags(this, id, flags, mask);
530 status_t SurfaceComposerClient::setTransparentRegionHint(SurfaceID id,
531 const Region& transparentRegion) {
532 return getComposer().setTransparentRegionHint(this, id, transparentRegion);
535 status_t SurfaceComposerClient::setAlpha(SurfaceID id, float alpha) {
536 return getComposer().setAlpha(this, id, alpha);
539 status_t SurfaceComposerClient::setLayerStack(SurfaceID id, uint32_t layerStack) {
540 return getComposer().setLayerStack(this, id, layerStack);
543 status_t SurfaceComposerClient::setMatrix(SurfaceID id, float dsdx, float dtdx,
544 float dsdy, float dtdy) {
545 return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
548 // ----------------------------------------------------------------------------
550 void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
551 const sp<ISurfaceTexture>& surface) {
552 Composer::getInstance().setDisplaySurface(token, surface);
555 void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
556 uint32_t layerStack) {
557 Composer::getInstance().setDisplayLayerStack(token, layerStack);
560 void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
561 uint32_t orientation,
562 const Rect& layerStackRect,
563 const Rect& displayRect) {
564 Composer::getInstance().setDisplayProjection(token, orientation,
565 layerStackRect, displayRect);
568 // ----------------------------------------------------------------------------
570 status_t SurfaceComposerClient::getDisplayInfo(
571 const sp<IBinder>& display, DisplayInfo* info)
573 return ComposerService::getComposerService()->getDisplayInfo(display, info);
576 void SurfaceComposerClient::blankDisplay(const sp<IBinder>& token) {
577 ComposerService::getComposerService()->blank(token);
580 void SurfaceComposerClient::unblankDisplay(const sp<IBinder>& token) {
581 ComposerService::getComposerService()->unblank(token);
584 // ----------------------------------------------------------------------------
586 ScreenshotClient::ScreenshotClient()
587 : mWidth(0), mHeight(0), mFormat(PIXEL_FORMAT_NONE) {
590 status_t ScreenshotClient::update(const sp<IBinder>& display) {
591 sp<ISurfaceComposer> s(ComposerService::getComposerService());
592 if (s == NULL) return NO_INIT;
594 return s->captureScreen(display, &mHeap,
595 &mWidth, &mHeight, &mFormat, 0, 0,
599 status_t ScreenshotClient::update(const sp<IBinder>& display,
600 uint32_t reqWidth, uint32_t reqHeight) {
601 sp<ISurfaceComposer> s(ComposerService::getComposerService());
602 if (s == NULL) return NO_INIT;
604 return s->captureScreen(display, &mHeap,
605 &mWidth, &mHeight, &mFormat, reqWidth, reqHeight,
609 status_t ScreenshotClient::update(const sp<IBinder>& display,
610 uint32_t reqWidth, uint32_t reqHeight,
611 uint32_t minLayerZ, uint32_t maxLayerZ) {
612 sp<ISurfaceComposer> s(ComposerService::getComposerService());
613 if (s == NULL) return NO_INIT;
615 return s->captureScreen(display, &mHeap,
616 &mWidth, &mHeight, &mFormat, reqWidth, reqHeight,
617 minLayerZ, maxLayerZ);
620 void ScreenshotClient::release() {
624 void const* ScreenshotClient::getPixels() const {
625 return mHeap->getBase();
628 uint32_t ScreenshotClient::getWidth() const {
632 uint32_t ScreenshotClient::getHeight() const {
636 PixelFormat ScreenshotClient::getFormat() const {
640 uint32_t ScreenshotClient::getStride() const {
644 size_t ScreenshotClient::getSize() const {
645 return mHeap->getSize();
648 // ----------------------------------------------------------------------------
649 }; // namespace android