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/IServiceManager.h>
31 #include <system/graphics.h>
33 #include <ui/DisplayInfo.h>
35 #include <gui/BufferItemConsumer.h>
36 #include <gui/CpuConsumer.h>
37 #include <gui/IGraphicBufferProducer.h>
38 #include <gui/ISurfaceComposer.h>
39 #include <gui/ISurfaceComposerClient.h>
40 #include <gui/Surface.h>
41 #include <gui/SurfaceComposerClient.h>
43 #include <private/gui/ComposerService.h>
44 #include <private/gui/LayerState.h>
47 // ---------------------------------------------------------------------------
49 ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
51 ComposerService::ComposerService()
52 : Singleton<ComposerService>() {
53 Mutex::Autolock _l(mLock);
57 void ComposerService::connectLocked() {
58 const String16 name("SurfaceFlinger");
59 while (getService(name, &mComposerService) != NO_ERROR) {
62 assert(mComposerService != NULL);
64 // Create the death listener.
65 class DeathObserver : public IBinder::DeathRecipient {
66 ComposerService& mComposerService;
67 virtual void binderDied(const wp<IBinder>& who) {
68 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
70 mComposerService.composerServiceDied();
73 explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
76 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
77 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
80 /*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
81 ComposerService& instance = ComposerService::getInstance();
82 Mutex::Autolock _l(instance.mLock);
83 if (instance.mComposerService == NULL) {
84 ComposerService::getInstance().connectLocked();
85 assert(instance.mComposerService != NULL);
86 ALOGD("ComposerService reconnected");
88 return instance.mComposerService;
91 void ComposerService::composerServiceDied()
93 Mutex::Autolock _l(mLock);
94 mComposerService = NULL;
95 mDeathObserver = NULL;
98 // ---------------------------------------------------------------------------
101 int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
102 if (lhs.client < rhs.client) return -1;
103 if (lhs.client > rhs.client) return 1;
104 if (lhs.state.surface < rhs.state.surface) return -1;
105 if (lhs.state.surface > rhs.state.surface) return 1;
110 int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
111 return compare_type(lhs.token, rhs.token);
114 class Composer : public Singleton<Composer>
116 friend class Singleton<Composer>;
119 SortedVector<ComposerState> mComposerStates;
120 SortedVector<DisplayState > mDisplayStates;
121 uint32_t mForceSynchronous;
122 uint32_t mTransactionNestCount;
125 Composer() : Singleton<Composer>(),
126 mForceSynchronous(0), mTransactionNestCount(0),
130 void openGlobalTransactionImpl();
131 void closeGlobalTransactionImpl(bool synchronous);
132 void setAnimationTransactionImpl();
133 status_t enableVSyncInjectionsImpl(bool enable);
134 status_t injectVSyncImpl(nsecs_t when);
136 layer_state_t* getLayerStateLocked(
137 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id);
139 DisplayState& getDisplayStateLocked(const sp<IBinder>& token);
142 sp<IBinder> createDisplay(const String8& displayName, bool secure);
143 void destroyDisplay(const sp<IBinder>& display);
144 sp<IBinder> getBuiltInDisplay(int32_t id);
146 status_t setPosition(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
148 status_t setSize(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
149 uint32_t w, uint32_t h);
150 status_t setLayer(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
152 status_t setRelativeLayer(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
153 const sp<IBinder>& relativeTo, int32_t z);
154 status_t setFlags(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
155 uint32_t flags, uint32_t mask);
156 status_t setTransparentRegionHint(
157 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
158 const Region& transparentRegion);
159 status_t setAlpha(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
161 status_t setMatrix(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
162 float dsdx, float dtdx, float dtdy, float dsdy);
163 status_t setOrientation(int orientation);
164 status_t setCrop(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
166 status_t setFinalCrop(const sp<SurfaceComposerClient>& client,
167 const sp<IBinder>& id, const Rect& crop);
168 status_t setLayerStack(const sp<SurfaceComposerClient>& client,
169 const sp<IBinder>& id, uint32_t layerStack);
170 status_t deferTransactionUntil(const sp<SurfaceComposerClient>& client,
171 const sp<IBinder>& id, const sp<IBinder>& handle,
172 uint64_t frameNumber);
173 status_t deferTransactionUntil(const sp<SurfaceComposerClient>& client,
174 const sp<IBinder>& id, const sp<Surface>& barrierSurface,
175 uint64_t frameNumber);
176 status_t reparentChildren(const sp<SurfaceComposerClient>& client,
177 const sp<IBinder>& id,
178 const sp<IBinder>& newParentHandle);
179 status_t detachChildren(const sp<SurfaceComposerClient>& client,
180 const sp<IBinder>& id);
181 status_t setOverrideScalingMode(const sp<SurfaceComposerClient>& client,
182 const sp<IBinder>& id, int32_t overrideScalingMode);
183 status_t setGeometryAppliesWithResize(const sp<SurfaceComposerClient>& client,
184 const sp<IBinder>& id);
186 status_t setDisplaySurface(const sp<IBinder>& token,
187 sp<IGraphicBufferProducer> bufferProducer);
188 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
189 void setDisplayProjection(const sp<IBinder>& token,
190 uint32_t orientation,
191 const Rect& layerStackRect,
192 const Rect& displayRect);
193 void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
195 static void setAnimationTransaction() {
196 Composer::getInstance().setAnimationTransactionImpl();
199 static void openGlobalTransaction() {
200 Composer::getInstance().openGlobalTransactionImpl();
203 static void closeGlobalTransaction(bool synchronous) {
204 Composer::getInstance().closeGlobalTransactionImpl(synchronous);
207 static status_t enableVSyncInjections(bool enable) {
208 return Composer::getInstance().enableVSyncInjectionsImpl(enable);
211 static status_t injectVSync(nsecs_t when) {
212 return Composer::getInstance().injectVSyncImpl(when);
216 ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
218 // ---------------------------------------------------------------------------
220 sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
221 return ComposerService::getComposerService()->createDisplay(displayName,
225 void Composer::destroyDisplay(const sp<IBinder>& display) {
226 return ComposerService::getComposerService()->destroyDisplay(display);
229 sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
230 return ComposerService::getComposerService()->getBuiltInDisplay(id);
233 void Composer::openGlobalTransactionImpl() {
234 { // scope for the lock
235 Mutex::Autolock _l(mLock);
236 mTransactionNestCount += 1;
240 void Composer::closeGlobalTransactionImpl(bool synchronous) {
241 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
243 Vector<ComposerState> transaction;
244 Vector<DisplayState> displayTransaction;
247 { // scope for the lock
248 Mutex::Autolock _l(mLock);
249 mForceSynchronous |= synchronous;
250 if (!mTransactionNestCount) {
251 ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
252 "call to openGlobalTransaction().");
253 } else if (--mTransactionNestCount) {
257 transaction = mComposerStates;
258 mComposerStates.clear();
260 displayTransaction = mDisplayStates;
261 mDisplayStates.clear();
263 if (mForceSynchronous) {
264 flags |= ISurfaceComposer::eSynchronous;
267 flags |= ISurfaceComposer::eAnimation;
270 mForceSynchronous = false;
274 sm->setTransactionState(transaction, displayTransaction, flags);
277 status_t Composer::enableVSyncInjectionsImpl(bool enable) {
278 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
279 return sm->enableVSyncInjections(enable);
282 status_t Composer::injectVSyncImpl(nsecs_t when) {
283 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
284 return sm->injectVSync(when);
287 void Composer::setAnimationTransactionImpl() {
288 Mutex::Autolock _l(mLock);
292 layer_state_t* Composer::getLayerStateLocked(
293 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
296 s.client = client->mClient;
297 s.state.surface = id;
299 ssize_t index = mComposerStates.indexOf(s);
301 // we don't have it, add an initialized layer_state to our list
302 index = mComposerStates.add(s);
305 ComposerState* const out = mComposerStates.editArray();
306 return &(out[index].state);
309 status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
310 const sp<IBinder>& id, float x, float y) {
311 Mutex::Autolock _l(mLock);
312 layer_state_t* s = getLayerStateLocked(client, id);
315 s->what |= layer_state_t::ePositionChanged;
321 status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
322 const sp<IBinder>& id, uint32_t w, uint32_t h) {
323 Mutex::Autolock _l(mLock);
324 layer_state_t* s = getLayerStateLocked(client, id);
327 s->what |= layer_state_t::eSizeChanged;
331 // Resizing a surface makes the transaction synchronous.
332 mForceSynchronous = true;
337 status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
338 const sp<IBinder>& id, int32_t z) {
339 Mutex::Autolock _l(mLock);
340 layer_state_t* s = getLayerStateLocked(client, id);
343 s->what |= layer_state_t::eLayerChanged;
348 status_t Composer::setRelativeLayer(const sp<SurfaceComposerClient>& client,
349 const sp<IBinder>& id, const sp<IBinder>& relativeTo,
351 Mutex::Autolock _l(mLock);
352 layer_state_t* s = getLayerStateLocked(client, id);
356 s->what |= layer_state_t::eRelativeLayerChanged;
357 s->relativeLayerHandle = relativeTo;
362 status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
363 const sp<IBinder>& id, uint32_t flags,
365 Mutex::Autolock _l(mLock);
366 layer_state_t* s = getLayerStateLocked(client, id);
369 if ((mask & layer_state_t::eLayerOpaque) ||
370 (mask & layer_state_t::eLayerHidden) ||
371 (mask & layer_state_t::eLayerSecure)) {
372 s->what |= layer_state_t::eFlagsChanged;
375 s->flags |= (flags & mask);
380 status_t Composer::setTransparentRegionHint(
381 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
382 const Region& transparentRegion) {
383 Mutex::Autolock _l(mLock);
384 layer_state_t* s = getLayerStateLocked(client, id);
387 s->what |= layer_state_t::eTransparentRegionChanged;
388 s->transparentRegion = transparentRegion;
392 status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
393 const sp<IBinder>& id, float alpha) {
394 Mutex::Autolock _l(mLock);
395 layer_state_t* s = getLayerStateLocked(client, id);
398 s->what |= layer_state_t::eAlphaChanged;
403 status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
404 const sp<IBinder>& id, uint32_t layerStack) {
405 Mutex::Autolock _l(mLock);
406 layer_state_t* s = getLayerStateLocked(client, id);
409 s->what |= layer_state_t::eLayerStackChanged;
410 s->layerStack = layerStack;
414 status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
415 const sp<IBinder>& id, float dsdx, float dtdx,
416 float dtdy, float dsdy) {
417 Mutex::Autolock _l(mLock);
418 layer_state_t* s = getLayerStateLocked(client, id);
421 s->what |= layer_state_t::eMatrixChanged;
422 layer_state_t::matrix22_t matrix;
431 status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
432 const sp<IBinder>& id, const Rect& crop) {
433 Mutex::Autolock _l(mLock);
434 layer_state_t* s = getLayerStateLocked(client, id);
437 s->what |= layer_state_t::eCropChanged;
442 status_t Composer::setFinalCrop(const sp<SurfaceComposerClient>& client,
443 const sp<IBinder>& id, const Rect& crop) {
444 Mutex::Autolock _l(mLock);
445 layer_state_t* s = getLayerStateLocked(client, id);
449 s->what |= layer_state_t::eFinalCropChanged;
454 status_t Composer::deferTransactionUntil(
455 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
456 const sp<IBinder>& handle, uint64_t frameNumber) {
457 Mutex::Autolock lock(mLock);
458 layer_state_t* s = getLayerStateLocked(client, id);
462 s->what |= layer_state_t::eDeferTransaction;
463 s->barrierHandle = handle;
464 s->frameNumber = frameNumber;
468 status_t Composer::deferTransactionUntil(
469 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
470 const sp<Surface>& barrierSurface, uint64_t frameNumber) {
471 Mutex::Autolock lock(mLock);
472 layer_state_t* s = getLayerStateLocked(client, id);
476 s->what |= layer_state_t::eDeferTransaction;
477 s->barrierGbp = barrierSurface->getIGraphicBufferProducer();
478 s->frameNumber = frameNumber;
482 status_t Composer::reparentChildren(
483 const sp<SurfaceComposerClient>& client,
484 const sp<IBinder>& id,
485 const sp<IBinder>& newParentHandle) {
486 Mutex::Autolock lock(mLock);
487 layer_state_t* s = getLayerStateLocked(client, id);
491 s->what |= layer_state_t::eReparentChildren;
492 s->reparentHandle = newParentHandle;
496 status_t Composer::detachChildren(
497 const sp<SurfaceComposerClient>& client,
498 const sp<IBinder>& id) {
499 Mutex::Autolock lock(mLock);
500 layer_state_t* s = getLayerStateLocked(client, id);
504 s->what |= layer_state_t::eDetachChildren;
508 status_t Composer::setOverrideScalingMode(
509 const sp<SurfaceComposerClient>& client,
510 const sp<IBinder>& id, int32_t overrideScalingMode) {
511 Mutex::Autolock lock(mLock);
512 layer_state_t* s = getLayerStateLocked(client, id);
517 switch (overrideScalingMode) {
518 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
519 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
520 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
521 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
525 ALOGE("unknown scaling mode: %d",
526 overrideScalingMode);
530 s->what |= layer_state_t::eOverrideScalingModeChanged;
531 s->overrideScalingMode = overrideScalingMode;
535 status_t Composer::setGeometryAppliesWithResize(
536 const sp<SurfaceComposerClient>& client,
537 const sp<IBinder>& id) {
538 Mutex::Autolock lock(mLock);
539 layer_state_t* s = getLayerStateLocked(client, id);
543 s->what |= layer_state_t::eGeometryAppliesWithResize;
547 // ---------------------------------------------------------------------------
549 DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
552 ssize_t index = mDisplayStates.indexOf(s);
554 // we don't have it, add an initialized layer_state to our list
556 index = mDisplayStates.add(s);
558 return mDisplayStates.editItemAt(static_cast<size_t>(index));
561 status_t Composer::setDisplaySurface(const sp<IBinder>& token,
562 sp<IGraphicBufferProducer> bufferProducer) {
563 if (bufferProducer.get() != nullptr) {
564 // Make sure that composition can never be stalled by a virtual display
565 // consumer that isn't processing buffers fast enough.
566 status_t err = bufferProducer->setAsyncMode(true);
567 if (err != NO_ERROR) {
568 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
569 "BufferQueue. This BufferQueue cannot be used for virtual "
570 "display. (%d)", err);
574 Mutex::Autolock _l(mLock);
575 DisplayState& s(getDisplayStateLocked(token));
576 s.surface = bufferProducer;
577 s.what |= DisplayState::eSurfaceChanged;
581 void Composer::setDisplayLayerStack(const sp<IBinder>& token,
582 uint32_t layerStack) {
583 Mutex::Autolock _l(mLock);
584 DisplayState& s(getDisplayStateLocked(token));
585 s.layerStack = layerStack;
586 s.what |= DisplayState::eLayerStackChanged;
589 void Composer::setDisplayProjection(const sp<IBinder>& token,
590 uint32_t orientation,
591 const Rect& layerStackRect,
592 const Rect& displayRect) {
593 Mutex::Autolock _l(mLock);
594 DisplayState& s(getDisplayStateLocked(token));
595 s.orientation = orientation;
596 s.viewport = layerStackRect;
597 s.frame = displayRect;
598 s.what |= DisplayState::eDisplayProjectionChanged;
599 mForceSynchronous = true; // TODO: do we actually still need this?
602 void Composer::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
603 Mutex::Autolock _l(mLock);
604 DisplayState& s(getDisplayStateLocked(token));
607 s.what |= DisplayState::eDisplaySizeChanged;
610 // ---------------------------------------------------------------------------
612 SurfaceComposerClient::SurfaceComposerClient()
613 : mStatus(NO_INIT), mComposer(Composer::getInstance())
617 SurfaceComposerClient::SurfaceComposerClient(const sp<IGraphicBufferProducer>& root)
618 : mStatus(NO_INIT), mComposer(Composer::getInstance()), mParent(root)
622 void SurfaceComposerClient::onFirstRef() {
623 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
625 auto rootProducer = mParent.promote();
626 sp<ISurfaceComposerClient> conn;
627 conn = (rootProducer != nullptr) ? sm->createScopedConnection(rootProducer) :
628 sm->createConnection();
636 SurfaceComposerClient::~SurfaceComposerClient() {
640 status_t SurfaceComposerClient::initCheck() const {
644 sp<IBinder> SurfaceComposerClient::connection() const {
645 return IInterface::asBinder(mClient);
648 status_t SurfaceComposerClient::linkToComposerDeath(
649 const sp<IBinder::DeathRecipient>& recipient,
650 void* cookie, uint32_t flags) {
651 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
652 return IInterface::asBinder(sm)->linkToDeath(recipient, cookie, flags);
655 void SurfaceComposerClient::dispose() {
656 // this can be called more than once.
657 sp<ISurfaceComposerClient> client;
658 Mutex::Autolock _lm(mLock);
660 client = mClient; // hold ref while lock is held
666 sp<SurfaceControl> SurfaceComposerClient::createSurface(
672 SurfaceControl* parent,
676 sp<SurfaceControl> sur;
677 if (mStatus == NO_ERROR) {
679 sp<IBinder> parentHandle;
680 sp<IGraphicBufferProducer> gbp;
682 if (parent != nullptr) {
683 parentHandle = parent->getHandle();
685 status_t err = mClient->createSurface(name, w, h, format, flags, parentHandle,
686 windowType, ownerUid, &handle, &gbp);
687 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
688 if (err == NO_ERROR) {
689 sur = new SurfaceControl(this, handle, gbp);
695 sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
697 return Composer::getInstance().createDisplay(displayName, secure);
700 void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
701 Composer::getInstance().destroyDisplay(display);
704 sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
705 return Composer::getInstance().getBuiltInDisplay(id);
708 status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
709 if (mStatus != NO_ERROR)
711 status_t err = mClient->destroySurface(sid);
715 status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
716 if (mStatus != NO_ERROR) {
719 return mClient->clearLayerFrameStats(token);
722 status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
723 FrameStats* outStats) const {
724 if (mStatus != NO_ERROR) {
727 return mClient->getLayerFrameStats(token, outStats);
730 inline Composer& SurfaceComposerClient::getComposer() {
734 // ----------------------------------------------------------------------------
736 void SurfaceComposerClient::openGlobalTransaction() {
737 Composer::openGlobalTransaction();
740 void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
741 Composer::closeGlobalTransaction(synchronous);
744 void SurfaceComposerClient::setAnimationTransaction() {
745 Composer::setAnimationTransaction();
748 status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
749 return Composer::enableVSyncInjections(enable);
752 status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
753 return Composer::injectVSync(when);
756 // ----------------------------------------------------------------------------
758 status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
759 return getComposer().setCrop(this, id, crop);
762 status_t SurfaceComposerClient::setFinalCrop(const sp<IBinder>& id,
764 return getComposer().setFinalCrop(this, id, crop);
767 status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
768 return getComposer().setPosition(this, id, x, y);
771 status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
772 return getComposer().setSize(this, id, w, h);
775 status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) {
776 return getComposer().setLayer(this, id, z);
779 status_t SurfaceComposerClient::setRelativeLayer(const sp<IBinder>& id,
780 const sp<IBinder>& relativeTo, int32_t z) {
781 return getComposer().setRelativeLayer(this, id, relativeTo, z);
784 status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
785 return getComposer().setFlags(this, id,
786 layer_state_t::eLayerHidden,
787 layer_state_t::eLayerHidden);
790 status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
791 return getComposer().setFlags(this, id,
793 layer_state_t::eLayerHidden);
796 status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
798 return getComposer().setFlags(this, id, flags, mask);
801 status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
802 const Region& transparentRegion) {
803 return getComposer().setTransparentRegionHint(this, id, transparentRegion);
806 status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
807 return getComposer().setAlpha(this, id, alpha);
810 status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
811 return getComposer().setLayerStack(this, id, layerStack);
814 status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
815 float dtdy, float dsdy) {
816 return getComposer().setMatrix(this, id, dsdx, dtdx, dtdy, dsdy);
819 status_t SurfaceComposerClient::deferTransactionUntil(const sp<IBinder>& id,
820 const sp<IBinder>& handle, uint64_t frameNumber) {
821 return getComposer().deferTransactionUntil(this, id, handle, frameNumber);
824 status_t SurfaceComposerClient::deferTransactionUntil(const sp<IBinder>& id,
825 const sp<Surface>& barrierSurface, uint64_t frameNumber) {
826 return getComposer().deferTransactionUntil(this, id, barrierSurface, frameNumber);
829 status_t SurfaceComposerClient::reparentChildren(const sp<IBinder>& id,
830 const sp<IBinder>& newParentHandle) {
831 return getComposer().reparentChildren(this, id, newParentHandle);
834 status_t SurfaceComposerClient::detachChildren(const sp<IBinder>& id) {
835 return getComposer().detachChildren(this, id);
838 status_t SurfaceComposerClient::setOverrideScalingMode(
839 const sp<IBinder>& id, int32_t overrideScalingMode) {
840 return getComposer().setOverrideScalingMode(
841 this, id, overrideScalingMode);
844 status_t SurfaceComposerClient::setGeometryAppliesWithResize(
845 const sp<IBinder>& id) {
846 return getComposer().setGeometryAppliesWithResize(this, id);
849 // ----------------------------------------------------------------------------
851 status_t SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
852 sp<IGraphicBufferProducer> bufferProducer) {
853 return Composer::getInstance().setDisplaySurface(token, bufferProducer);
856 void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
857 uint32_t layerStack) {
858 Composer::getInstance().setDisplayLayerStack(token, layerStack);
861 void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
862 uint32_t orientation,
863 const Rect& layerStackRect,
864 const Rect& displayRect) {
865 Composer::getInstance().setDisplayProjection(token, orientation,
866 layerStackRect, displayRect);
869 void SurfaceComposerClient::setDisplaySize(const sp<IBinder>& token,
870 uint32_t width, uint32_t height) {
871 Composer::getInstance().setDisplaySize(token, width, height);
874 // ----------------------------------------------------------------------------
876 status_t SurfaceComposerClient::getDisplayConfigs(
877 const sp<IBinder>& display, Vector<DisplayInfo>* configs)
879 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
882 status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
884 Vector<DisplayInfo> configs;
885 status_t result = getDisplayConfigs(display, &configs);
886 if (result != NO_ERROR) {
890 int activeId = getActiveConfig(display);
892 ALOGE("No active configuration found");
893 return NAME_NOT_FOUND;
896 *info = configs[static_cast<size_t>(activeId)];
900 int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
901 return ComposerService::getComposerService()->getActiveConfig(display);
904 status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
905 return ComposerService::getComposerService()->setActiveConfig(display, id);
908 status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
909 Vector<android_color_mode_t>* outColorModes) {
910 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
913 android_color_mode_t SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
914 return ComposerService::getComposerService()->getActiveColorMode(display);
917 status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
918 android_color_mode_t colorMode) {
919 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
922 void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
924 ComposerService::getComposerService()->setPowerMode(token, mode);
927 status_t SurfaceComposerClient::clearAnimationFrameStats() {
928 return ComposerService::getComposerService()->clearAnimationFrameStats();
931 status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
932 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
935 status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
936 HdrCapabilities* outCapabilities) {
937 return ComposerService::getComposerService()->getHdrCapabilities(display,
941 // ----------------------------------------------------------------------------
943 status_t ScreenshotClient::capture(
944 const sp<IBinder>& display,
945 const sp<IGraphicBufferProducer>& producer,
946 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
947 int32_t minLayerZ, int32_t maxLayerZ, bool useIdentityTransform) {
948 sp<ISurfaceComposer> s(ComposerService::getComposerService());
949 if (s == NULL) return NO_INIT;
950 return s->captureScreen(display, producer, sourceCrop,
951 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
954 status_t ScreenshotClient::captureToBuffer(const sp<IBinder>& display,
955 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
956 int32_t minLayerZ, int32_t maxLayerZ, bool useIdentityTransform,
958 sp<GraphicBuffer>* outBuffer) {
959 sp<ISurfaceComposer> s(ComposerService::getComposerService());
960 if (s == NULL) return NO_INIT;
962 sp<IGraphicBufferConsumer> gbpConsumer;
963 sp<IGraphicBufferProducer> producer;
964 BufferQueue::createBufferQueue(&producer, &gbpConsumer);
965 sp<BufferItemConsumer> consumer(new BufferItemConsumer(gbpConsumer,
966 GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_NEVER,
969 status_t ret = s->captureScreen(display, producer, sourceCrop, reqWidth, reqHeight,
970 minLayerZ, maxLayerZ, useIdentityTransform,
971 static_cast<ISurfaceComposer::Rotation>(rotation));
972 if (ret != NO_ERROR) {
976 consumer->acquireBuffer(&b, 0, true);
977 *outBuffer = b.mGraphicBuffer;
981 ScreenshotClient::ScreenshotClient()
982 : mHaveBuffer(false) {
983 memset(&mBuffer, 0, sizeof(mBuffer));
986 ScreenshotClient::~ScreenshotClient() {
987 ScreenshotClient::release();
990 sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
991 if (mCpuConsumer == NULL) {
992 sp<IGraphicBufferConsumer> consumer;
993 BufferQueue::createBufferQueue(&mProducer, &consumer);
994 mCpuConsumer = new CpuConsumer(consumer, 1);
995 mCpuConsumer->setName(String8("ScreenshotClient"));
1000 status_t ScreenshotClient::update(const sp<IBinder>& display,
1001 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
1002 int32_t minLayerZ, int32_t maxLayerZ,
1003 bool useIdentityTransform, uint32_t rotation) {
1004 sp<ISurfaceComposer> s(ComposerService::getComposerService());
1005 if (s == NULL) return NO_INIT;
1006 sp<CpuConsumer> cpuConsumer = getCpuConsumer();
1009 mCpuConsumer->unlockBuffer(mBuffer);
1010 memset(&mBuffer, 0, sizeof(mBuffer));
1011 mHaveBuffer = false;
1014 status_t err = s->captureScreen(display, mProducer, sourceCrop,
1015 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform,
1016 static_cast<ISurfaceComposer::Rotation>(rotation));
1018 if (err == NO_ERROR) {
1019 err = mCpuConsumer->lockNextBuffer(&mBuffer);
1020 if (err == NO_ERROR) {
1027 status_t ScreenshotClient::update(const sp<IBinder>& display,
1028 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
1029 int32_t minLayerZ, int32_t maxLayerZ,
1030 bool useIdentityTransform) {
1032 return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
1033 minLayerZ, maxLayerZ, useIdentityTransform, ISurfaceComposer::eRotateNone);
1036 status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
1037 bool useIdentityTransform) {
1038 return ScreenshotClient::update(display, sourceCrop, 0, 0,
1039 INT32_MIN, INT32_MAX,
1040 useIdentityTransform, ISurfaceComposer::eRotateNone);
1043 status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
1044 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
1045 return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
1046 INT32_MIN, INT32_MAX,
1047 useIdentityTransform, ISurfaceComposer::eRotateNone);
1050 void ScreenshotClient::release() {
1052 mCpuConsumer->unlockBuffer(mBuffer);
1053 memset(&mBuffer, 0, sizeof(mBuffer));
1054 mHaveBuffer = false;
1056 mCpuConsumer.clear();
1059 void const* ScreenshotClient::getPixels() const {
1060 return mBuffer.data;
1063 uint32_t ScreenshotClient::getWidth() const {
1064 return mBuffer.width;
1067 uint32_t ScreenshotClient::getHeight() const {
1068 return mBuffer.height;
1071 PixelFormat ScreenshotClient::getFormat() const {
1072 return mBuffer.format;
1075 uint32_t ScreenshotClient::getStride() const {
1076 return mBuffer.stride;
1079 size_t ScreenshotClient::getSize() const {
1080 return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
1083 android_dataspace ScreenshotClient::getDataSpace() const {
1084 return mBuffer.dataSpace;
1087 // ----------------------------------------------------------------------------
1088 }; // namespace android