1 /*
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>
20 #include <sys/types.h>
21 
22 #include <utils/Errors.h>
23 #include <utils/Log.h>
24 #include <utils/SortedVector.h>
25 #include <utils/String8.h>
26 #include <utils/threads.h>
27 
28 #include <binder/IServiceManager.h>
29 
30 #include <system/graphics.h>
31 
32 #include <ui/DisplayInfo.h>
33 
34 #include <gui/BufferItemConsumer.h>
35 #include <gui/CpuConsumer.h>
36 #include <gui/IGraphicBufferProducer.h>
37 #include <gui/ISurfaceComposer.h>
38 #include <gui/ISurfaceComposerClient.h>
39 #include <gui/LayerState.h>
40 #include <gui/Surface.h>
41 #include <gui/SurfaceComposerClient.h>
42 
43 #include <private/gui/ComposerService.h>
44 
45 namespace android {
46 
47 using ui::ColorMode;
48 // ---------------------------------------------------------------------------
49 
50 ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
51 
ComposerService()52 ComposerService::ComposerService()
53 : Singleton<ComposerService>() {
54     Mutex::Autolock _l(mLock);
55     connectLocked();
56 }
57 
connectLocked()58 void ComposerService::connectLocked() {
59     const String16 name("SurfaceFlinger");
60     while (getService(name, &mComposerService) != NO_ERROR) {
61         usleep(250000);
62     }
63     assert(mComposerService != NULL);
64 
65     // Create the death listener.
66     class DeathObserver : public IBinder::DeathRecipient {
67         ComposerService& mComposerService;
68         virtual void binderDied(const wp<IBinder>& who) {
69             ALOGW("ComposerService remote (surfaceflinger) died [%p]",
70                   who.unsafe_get());
71             mComposerService.composerServiceDied();
72         }
73      public:
74         explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
75     };
76 
77     mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
78     IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
79 }
80 
getComposerService()81 /*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
82     ComposerService& instance = ComposerService::getInstance();
83     Mutex::Autolock _l(instance.mLock);
84     if (instance.mComposerService == NULL) {
85         ComposerService::getInstance().connectLocked();
86         assert(instance.mComposerService != NULL);
87         ALOGD("ComposerService reconnected");
88     }
89     return instance.mComposerService;
90 }
91 
composerServiceDied()92 void ComposerService::composerServiceDied()
93 {
94     Mutex::Autolock _l(mLock);
95     mComposerService = NULL;
96     mDeathObserver = NULL;
97 }
98 
99 // ---------------------------------------------------------------------------
100 
Transaction(const Transaction & other)101 SurfaceComposerClient::Transaction::Transaction(const Transaction& other) :
102     mForceSynchronous(other.mForceSynchronous),
103     mTransactionNestCount(other.mTransactionNestCount),
104     mAnimation(other.mAnimation),
105     mEarlyWakeup(other.mEarlyWakeup) {
106     mDisplayStates = other.mDisplayStates;
107     mComposerStates = other.mComposerStates;
108 }
109 
merge(Transaction && other)110 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) {
111     for (auto const& kv : other.mComposerStates) {
112         if (mComposerStates.count(kv.first) == 0) {
113             mComposerStates[kv.first] = kv.second;
114         } else {
115             mComposerStates[kv.first].state.merge(kv.second.state);
116         }
117     }
118     other.mComposerStates.clear();
119 
120     for (auto const& state : other.mDisplayStates) {
121         ssize_t index = mDisplayStates.indexOf(state);
122         if (index < 0) {
123             mDisplayStates.add(state);
124         } else {
125             mDisplayStates.editItemAt(static_cast<size_t>(index)).merge(state);
126         }
127     }
128     other.mDisplayStates.clear();
129 
130     return *this;
131 }
132 
apply(bool synchronous)133 status_t SurfaceComposerClient::Transaction::apply(bool synchronous) {
134     if (mStatus != NO_ERROR) {
135         return mStatus;
136     }
137 
138     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
139 
140     Vector<ComposerState> composerStates;
141     Vector<DisplayState> displayStates;
142     uint32_t flags = 0;
143 
144     mForceSynchronous |= synchronous;
145 
146     for (auto const& kv : mComposerStates){
147         composerStates.add(kv.second);
148     }
149 
150     mComposerStates.clear();
151 
152     displayStates = mDisplayStates;
153     mDisplayStates.clear();
154 
155     if (mForceSynchronous) {
156         flags |= ISurfaceComposer::eSynchronous;
157     }
158     if (mAnimation) {
159         flags |= ISurfaceComposer::eAnimation;
160     }
161     if (mEarlyWakeup) {
162         flags |= ISurfaceComposer::eEarlyWakeup;
163     }
164 
165     mForceSynchronous = false;
166     mAnimation = false;
167     mEarlyWakeup = false;
168 
169     sf->setTransactionState(composerStates, displayStates, flags);
170     mStatus = NO_ERROR;
171     return NO_ERROR;
172 }
173 
174 // ---------------------------------------------------------------------------
175 
createDisplay(const String8 & displayName,bool secure)176 sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure) {
177     return ComposerService::getComposerService()->createDisplay(displayName,
178             secure);
179 }
180 
destroyDisplay(const sp<IBinder> & display)181 void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
182     return ComposerService::getComposerService()->destroyDisplay(display);
183 }
184 
getBuiltInDisplay(int32_t id)185 sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
186     return ComposerService::getComposerService()->getBuiltInDisplay(id);
187 }
188 
setAnimationTransaction()189 void SurfaceComposerClient::Transaction::setAnimationTransaction() {
190     mAnimation = true;
191 }
192 
setEarlyWakeup()193 void SurfaceComposerClient::Transaction::setEarlyWakeup() {
194     mEarlyWakeup = true;
195 }
196 
getLayerState(const sp<SurfaceControl> & sc)197 layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) {
198     if (mComposerStates.count(sc) == 0) {
199         // we don't have it, add an initialized layer_state to our list
200         ComposerState s;
201         s.client = sc->getClient()->mClient;
202         s.state.surface = sc->getHandle();
203         mComposerStates[sc] = s;
204     }
205 
206     return &(mComposerStates[sc].state);
207 }
208 
setPosition(const sp<SurfaceControl> & sc,float x,float y)209 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
210         const sp<SurfaceControl>& sc, float x, float y) {
211     layer_state_t* s = getLayerState(sc);
212     if (!s) {
213         mStatus = BAD_INDEX;
214         return *this;
215     }
216     s->what |= layer_state_t::ePositionChanged;
217     s->x = x;
218     s->y = y;
219     return *this;
220 }
221 
show(const sp<SurfaceControl> & sc)222 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show(
223         const sp<SurfaceControl>& sc) {
224     return setFlags(sc, 0, layer_state_t::eLayerHidden);
225 }
226 
hide(const sp<SurfaceControl> & sc)227 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide(
228         const sp<SurfaceControl>& sc) {
229     return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
230 }
231 
setSize(const sp<SurfaceControl> & sc,uint32_t w,uint32_t h)232 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSize(
233         const sp<SurfaceControl>& sc, uint32_t w, uint32_t h) {
234     layer_state_t* s = getLayerState(sc);
235     if (!s) {
236         mStatus = BAD_INDEX;
237         return *this;
238     }
239     s->what |= layer_state_t::eSizeChanged;
240     s->w = w;
241     s->h = h;
242 
243     // Resizing a surface makes the transaction synchronous.
244     mForceSynchronous = true;
245 
246     return *this;
247 }
248 
setLayer(const sp<SurfaceControl> & sc,int32_t z)249 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
250         const sp<SurfaceControl>& sc, int32_t z) {
251     layer_state_t* s = getLayerState(sc);
252     if (!s) {
253         mStatus = BAD_INDEX;
254         return *this;
255     }
256     s->what |= layer_state_t::eLayerChanged;
257     s->z = z;
258     return *this;
259 }
260 
setRelativeLayer(const sp<SurfaceControl> & sc,const sp<IBinder> & relativeTo,int32_t z)261 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(const sp<SurfaceControl>& sc, const sp<IBinder>& relativeTo,
262         int32_t z) {
263     layer_state_t* s = getLayerState(sc);
264     if (!s) {
265         mStatus = BAD_INDEX;
266     }
267     s->what |= layer_state_t::eRelativeLayerChanged;
268     s->relativeLayerHandle = relativeTo;
269     s->z = z;
270     return *this;
271 }
272 
setFlags(const sp<SurfaceControl> & sc,uint32_t flags,uint32_t mask)273 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags(
274         const sp<SurfaceControl>& sc, uint32_t flags,
275         uint32_t mask) {
276     layer_state_t* s = getLayerState(sc);
277     if (!s) {
278         mStatus = BAD_INDEX;
279         return *this;
280     }
281     if ((mask & layer_state_t::eLayerOpaque) ||
282             (mask & layer_state_t::eLayerHidden) ||
283             (mask & layer_state_t::eLayerSecure)) {
284         s->what |= layer_state_t::eFlagsChanged;
285     }
286     s->flags &= ~mask;
287     s->flags |= (flags & mask);
288     s->mask |= mask;
289     return *this;
290 }
291 
setTransparentRegionHint(const sp<SurfaceControl> & sc,const Region & transparentRegion)292 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
293         const sp<SurfaceControl>& sc,
294         const Region& transparentRegion) {
295     layer_state_t* s = getLayerState(sc);
296     if (!s) {
297         mStatus = BAD_INDEX;
298         return *this;
299     }
300     s->what |= layer_state_t::eTransparentRegionChanged;
301     s->transparentRegion = transparentRegion;
302     return *this;
303 }
304 
setAlpha(const sp<SurfaceControl> & sc,float alpha)305 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
306         const sp<SurfaceControl>& sc, float alpha) {
307     layer_state_t* s = getLayerState(sc);
308     if (!s) {
309         mStatus = BAD_INDEX;
310         return *this;
311     }
312     s->what |= layer_state_t::eAlphaChanged;
313     s->alpha = alpha;
314     return *this;
315 }
316 
setLayerStack(const sp<SurfaceControl> & sc,uint32_t layerStack)317 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
318         const sp<SurfaceControl>& sc, uint32_t layerStack) {
319     layer_state_t* s = getLayerState(sc);
320     if (!s) {
321         mStatus = BAD_INDEX;
322         return *this;
323     }
324     s->what |= layer_state_t::eLayerStackChanged;
325     s->layerStack = layerStack;
326     return *this;
327 }
328 
setMatrix(const sp<SurfaceControl> & sc,float dsdx,float dtdx,float dtdy,float dsdy)329 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
330         const sp<SurfaceControl>& sc, float dsdx, float dtdx,
331         float dtdy, float dsdy) {
332     layer_state_t* s = getLayerState(sc);
333     if (!s) {
334         mStatus = BAD_INDEX;
335         return *this;
336     }
337     s->what |= layer_state_t::eMatrixChanged;
338     layer_state_t::matrix22_t matrix;
339     matrix.dsdx = dsdx;
340     matrix.dtdx = dtdx;
341     matrix.dsdy = dsdy;
342     matrix.dtdy = dtdy;
343     s->matrix = matrix;
344     return *this;
345 }
346 
setCrop(const sp<SurfaceControl> & sc,const Rect & crop)347 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
348         const sp<SurfaceControl>& sc, const Rect& crop) {
349     layer_state_t* s = getLayerState(sc);
350     if (!s) {
351         mStatus = BAD_INDEX;
352         return *this;
353     }
354     s->what |= layer_state_t::eCropChanged;
355     s->crop = crop;
356     return *this;
357 }
358 
setFinalCrop(const sp<SurfaceControl> & sc,const Rect & crop)359 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFinalCrop(const sp<SurfaceControl>& sc, const Rect& crop) {
360     layer_state_t* s = getLayerState(sc);
361     if (!s) {
362         mStatus = BAD_INDEX;
363         return *this;
364     }
365     s->what |= layer_state_t::eFinalCropChanged;
366     s->finalCrop = crop;
367     return *this;
368 }
369 
deferTransactionUntil(const sp<SurfaceControl> & sc,const sp<IBinder> & handle,uint64_t frameNumber)370 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::deferTransactionUntil(
371         const sp<SurfaceControl>& sc,
372         const sp<IBinder>& handle, uint64_t frameNumber) {
373     layer_state_t* s = getLayerState(sc);
374     if (!s) {
375         mStatus = BAD_INDEX;
376         return *this;
377     }
378     s->what |= layer_state_t::eDeferTransaction;
379     s->barrierHandle = handle;
380     s->frameNumber = frameNumber;
381     return *this;
382 }
383 
deferTransactionUntil(const sp<SurfaceControl> & sc,const sp<Surface> & barrierSurface,uint64_t frameNumber)384 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::deferTransactionUntil(
385         const sp<SurfaceControl>& sc,
386         const sp<Surface>& barrierSurface, uint64_t frameNumber) {
387     layer_state_t* s = getLayerState(sc);
388     if (!s) {
389         mStatus = BAD_INDEX;
390         return *this;
391     }
392     s->what |= layer_state_t::eDeferTransaction;
393     s->barrierGbp = barrierSurface->getIGraphicBufferProducer();
394     s->frameNumber = frameNumber;
395     return *this;
396 }
397 
reparentChildren(const sp<SurfaceControl> & sc,const sp<IBinder> & newParentHandle)398 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparentChildren(
399         const sp<SurfaceControl>& sc,
400         const sp<IBinder>& newParentHandle) {
401     layer_state_t* s = getLayerState(sc);
402     if (!s) {
403         mStatus = BAD_INDEX;
404         return *this;
405     }
406     s->what |= layer_state_t::eReparentChildren;
407     s->reparentHandle = newParentHandle;
408     return *this;
409 }
410 
reparent(const sp<SurfaceControl> & sc,const sp<IBinder> & newParentHandle)411 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
412         const sp<SurfaceControl>& sc,
413         const sp<IBinder>& newParentHandle) {
414     layer_state_t* s = getLayerState(sc);
415     if (!s) {
416         mStatus = BAD_INDEX;
417         return *this;
418     }
419     s->what |= layer_state_t::eReparent;
420     s->parentHandleForChild = newParentHandle;
421     return *this;
422 }
423 
setColor(const sp<SurfaceControl> & sc,const half3 & color)424 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
425         const sp<SurfaceControl>& sc,
426         const half3& color) {
427     layer_state_t* s = getLayerState(sc);
428     if (!s) {
429         mStatus = BAD_INDEX;
430         return *this;
431     }
432     s->what |= layer_state_t::eColorChanged;
433     s->color = color;
434     return *this;
435 }
436 
detachChildren(const sp<SurfaceControl> & sc)437 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::detachChildren(
438         const sp<SurfaceControl>& sc) {
439     layer_state_t* s = getLayerState(sc);
440     if (!s) {
441         mStatus = BAD_INDEX;
442     }
443     s->what |= layer_state_t::eDetachChildren;
444     return *this;
445 }
446 
setOverrideScalingMode(const sp<SurfaceControl> & sc,int32_t overrideScalingMode)447 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setOverrideScalingMode(
448         const sp<SurfaceControl>& sc, int32_t overrideScalingMode) {
449     layer_state_t* s = getLayerState(sc);
450     if (!s) {
451         mStatus = BAD_INDEX;
452         return *this;
453     }
454 
455     switch (overrideScalingMode) {
456         case NATIVE_WINDOW_SCALING_MODE_FREEZE:
457         case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
458         case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
459         case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
460         case -1:
461             break;
462         default:
463             ALOGE("unknown scaling mode: %d",
464                     overrideScalingMode);
465             mStatus = BAD_VALUE;
466             return *this;
467     }
468 
469     s->what |= layer_state_t::eOverrideScalingModeChanged;
470     s->overrideScalingMode = overrideScalingMode;
471     return *this;
472 }
473 
setGeometryAppliesWithResize(const sp<SurfaceControl> & sc)474 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometryAppliesWithResize(
475         const sp<SurfaceControl>& sc) {
476     layer_state_t* s = getLayerState(sc);
477     if (!s) {
478         mStatus = BAD_INDEX;
479         return *this;
480     }
481     s->what |= layer_state_t::eGeometryAppliesWithResize;
482     return *this;
483 }
484 
destroySurface(const sp<SurfaceControl> & sc)485 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::destroySurface(
486         const sp<SurfaceControl>& sc) {
487     layer_state_t* s = getLayerState(sc);
488     if (!s) {
489         mStatus = BAD_INDEX;
490         return *this;
491     }
492     s->what |= layer_state_t::eDestroySurface;
493     return *this;
494 }
495 
496 // ---------------------------------------------------------------------------
497 
getDisplayState(const sp<IBinder> & token)498 DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
499     DisplayState s;
500     s.token = token;
501     ssize_t index = mDisplayStates.indexOf(s);
502     if (index < 0) {
503         // we don't have it, add an initialized layer_state to our list
504         s.what = 0;
505         index = mDisplayStates.add(s);
506     }
507     return mDisplayStates.editItemAt(static_cast<size_t>(index));
508 }
509 
setDisplaySurface(const sp<IBinder> & token,const sp<IGraphicBufferProducer> & bufferProducer)510 status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
511         const sp<IGraphicBufferProducer>& bufferProducer) {
512     if (bufferProducer.get() != nullptr) {
513         // Make sure that composition can never be stalled by a virtual display
514         // consumer that isn't processing buffers fast enough.
515         status_t err = bufferProducer->setAsyncMode(true);
516         if (err != NO_ERROR) {
517             ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
518                     "BufferQueue. This BufferQueue cannot be used for virtual "
519                     "display. (%d)", err);
520             return err;
521         }
522     }
523     DisplayState& s(getDisplayState(token));
524     s.surface = bufferProducer;
525     s.what |= DisplayState::eSurfaceChanged;
526     return NO_ERROR;
527 }
528 
setDisplayLayerStack(const sp<IBinder> & token,uint32_t layerStack)529 void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
530         uint32_t layerStack) {
531     DisplayState& s(getDisplayState(token));
532     s.layerStack = layerStack;
533     s.what |= DisplayState::eLayerStackChanged;
534 }
535 
setDisplayProjection(const sp<IBinder> & token,uint32_t orientation,const Rect & layerStackRect,const Rect & displayRect)536 void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
537         uint32_t orientation,
538         const Rect& layerStackRect,
539         const Rect& displayRect) {
540     DisplayState& s(getDisplayState(token));
541     s.orientation = orientation;
542     s.viewport = layerStackRect;
543     s.frame = displayRect;
544     s.what |= DisplayState::eDisplayProjectionChanged;
545     mForceSynchronous = true; // TODO: do we actually still need this?
546 }
547 
setDisplaySize(const sp<IBinder> & token,uint32_t width,uint32_t height)548 void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
549     DisplayState& s(getDisplayState(token));
550     s.width = width;
551     s.height = height;
552     s.what |= DisplayState::eDisplaySizeChanged;
553 }
554 
555 // ---------------------------------------------------------------------------
556 
SurfaceComposerClient()557 SurfaceComposerClient::SurfaceComposerClient()
558     : mStatus(NO_INIT)
559 {
560 }
561 
SurfaceComposerClient(const sp<IGraphicBufferProducer> & root)562 SurfaceComposerClient::SurfaceComposerClient(const sp<IGraphicBufferProducer>& root)
563     : mStatus(NO_INIT), mParent(root)
564 {
565 }
566 
SurfaceComposerClient(const sp<ISurfaceComposerClient> & client)567 SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
568     : mStatus(NO_ERROR), mClient(client)
569 {
570 }
571 
onFirstRef()572 void SurfaceComposerClient::onFirstRef() {
573     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
574     if (sf != 0 && mStatus == NO_INIT) {
575         auto rootProducer = mParent.promote();
576         sp<ISurfaceComposerClient> conn;
577         conn = (rootProducer != nullptr) ? sf->createScopedConnection(rootProducer) :
578                 sf->createConnection();
579         if (conn != 0) {
580             mClient = conn;
581             mStatus = NO_ERROR;
582         }
583     }
584 }
585 
~SurfaceComposerClient()586 SurfaceComposerClient::~SurfaceComposerClient() {
587     dispose();
588 }
589 
initCheck() const590 status_t SurfaceComposerClient::initCheck() const {
591     return mStatus;
592 }
593 
connection() const594 sp<IBinder> SurfaceComposerClient::connection() const {
595     return IInterface::asBinder(mClient);
596 }
597 
linkToComposerDeath(const sp<IBinder::DeathRecipient> & recipient,void * cookie,uint32_t flags)598 status_t SurfaceComposerClient::linkToComposerDeath(
599         const sp<IBinder::DeathRecipient>& recipient,
600         void* cookie, uint32_t flags) {
601     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
602     return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
603 }
604 
dispose()605 void SurfaceComposerClient::dispose() {
606     // this can be called more than once.
607     sp<ISurfaceComposerClient> client;
608     Mutex::Autolock _lm(mLock);
609     if (mClient != 0) {
610         client = mClient; // hold ref while lock is held
611         mClient.clear();
612     }
613     mStatus = NO_INIT;
614 }
615 
createSurface(const String8 & name,uint32_t w,uint32_t h,PixelFormat format,uint32_t flags,SurfaceControl * parent,int32_t windowType,int32_t ownerUid)616 sp<SurfaceControl> SurfaceComposerClient::createSurface(
617         const String8& name,
618         uint32_t w,
619         uint32_t h,
620         PixelFormat format,
621         uint32_t flags,
622         SurfaceControl* parent,
623         int32_t windowType,
624         int32_t ownerUid)
625 {
626     sp<SurfaceControl> s;
627     createSurfaceChecked(name, w, h, format, &s, flags, parent, windowType, ownerUid);
628     return s;
629 }
630 
createSurfaceChecked(const String8 & name,uint32_t w,uint32_t h,PixelFormat format,sp<SurfaceControl> * outSurface,uint32_t flags,SurfaceControl * parent,int32_t windowType,int32_t ownerUid)631 status_t SurfaceComposerClient::createSurfaceChecked(
632         const String8& name,
633         uint32_t w,
634         uint32_t h,
635         PixelFormat format,
636         sp<SurfaceControl>* outSurface,
637         uint32_t flags,
638         SurfaceControl* parent,
639         int32_t windowType,
640         int32_t ownerUid)
641 {
642     sp<SurfaceControl> sur;
643     status_t err = mStatus;
644 
645     if (mStatus == NO_ERROR) {
646         sp<IBinder> handle;
647         sp<IBinder> parentHandle;
648         sp<IGraphicBufferProducer> gbp;
649 
650         if (parent != nullptr) {
651             parentHandle = parent->getHandle();
652         }
653         err = mClient->createSurface(name, w, h, format, flags, parentHandle,
654                 windowType, ownerUid, &handle, &gbp);
655         ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
656         if (err == NO_ERROR) {
657             *outSurface = new SurfaceControl(this, handle, gbp, true /* owned */);
658         }
659     }
660     return err;
661 }
662 
destroySurface(const sp<IBinder> & sid)663 status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
664     if (mStatus != NO_ERROR)
665         return mStatus;
666     status_t err = mClient->destroySurface(sid);
667     return err;
668 }
669 
clearLayerFrameStats(const sp<IBinder> & token) const670 status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
671     if (mStatus != NO_ERROR) {
672         return mStatus;
673     }
674     return mClient->clearLayerFrameStats(token);
675 }
676 
getLayerFrameStats(const sp<IBinder> & token,FrameStats * outStats) const677 status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
678         FrameStats* outStats) const {
679     if (mStatus != NO_ERROR) {
680         return mStatus;
681     }
682     return mClient->getLayerFrameStats(token, outStats);
683 }
684 
685 // ----------------------------------------------------------------------------
686 
enableVSyncInjections(bool enable)687 status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
688     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
689     return sf->enableVSyncInjections(enable);
690 }
691 
injectVSync(nsecs_t when)692 status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
693     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
694     return sf->injectVSync(when);
695 }
696 
getDisplayConfigs(const sp<IBinder> & display,Vector<DisplayInfo> * configs)697 status_t SurfaceComposerClient::getDisplayConfigs(
698         const sp<IBinder>& display, Vector<DisplayInfo>* configs)
699 {
700     return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
701 }
702 
getDisplayInfo(const sp<IBinder> & display,DisplayInfo * info)703 status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
704         DisplayInfo* info) {
705     Vector<DisplayInfo> configs;
706     status_t result = getDisplayConfigs(display, &configs);
707     if (result != NO_ERROR) {
708         return result;
709     }
710 
711     int activeId = getActiveConfig(display);
712     if (activeId < 0) {
713         ALOGE("No active configuration found");
714         return NAME_NOT_FOUND;
715     }
716 
717     *info = configs[static_cast<size_t>(activeId)];
718     return NO_ERROR;
719 }
720 
getActiveConfig(const sp<IBinder> & display)721 int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
722     return ComposerService::getComposerService()->getActiveConfig(display);
723 }
724 
setActiveConfig(const sp<IBinder> & display,int id)725 status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
726     return ComposerService::getComposerService()->setActiveConfig(display, id);
727 }
728 
getDisplayColorModes(const sp<IBinder> & display,Vector<ColorMode> * outColorModes)729 status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
730         Vector<ColorMode>* outColorModes) {
731     return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
732 }
733 
getActiveColorMode(const sp<IBinder> & display)734 ColorMode SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
735     return ComposerService::getComposerService()->getActiveColorMode(display);
736 }
737 
setActiveColorMode(const sp<IBinder> & display,ColorMode colorMode)738 status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
739         ColorMode colorMode) {
740     return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
741 }
742 
setDisplayPowerMode(const sp<IBinder> & token,int mode)743 void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
744         int mode) {
745     ComposerService::getComposerService()->setPowerMode(token, mode);
746 }
747 
clearAnimationFrameStats()748 status_t SurfaceComposerClient::clearAnimationFrameStats() {
749     return ComposerService::getComposerService()->clearAnimationFrameStats();
750 }
751 
getAnimationFrameStats(FrameStats * outStats)752 status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
753     return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
754 }
755 
getHdrCapabilities(const sp<IBinder> & display,HdrCapabilities * outCapabilities)756 status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
757         HdrCapabilities* outCapabilities) {
758     return ComposerService::getComposerService()->getHdrCapabilities(display,
759             outCapabilities);
760 }
761 
762 // ----------------------------------------------------------------------------
763 
capture(const sp<IBinder> & display,Rect sourceCrop,uint32_t reqWidth,uint32_t reqHeight,int32_t minLayerZ,int32_t maxLayerZ,bool useIdentityTransform,uint32_t rotation,sp<GraphicBuffer> * outBuffer)764 status_t ScreenshotClient::capture(const sp<IBinder>& display, Rect sourceCrop, uint32_t reqWidth,
765                                    uint32_t reqHeight, int32_t minLayerZ, int32_t maxLayerZ,
766                                    bool useIdentityTransform, uint32_t rotation,
767                                    sp<GraphicBuffer>* outBuffer) {
768     sp<ISurfaceComposer> s(ComposerService::getComposerService());
769     if (s == NULL) return NO_INIT;
770     status_t ret = s->captureScreen(display, outBuffer, sourceCrop, reqWidth, reqHeight, minLayerZ,
771                                     maxLayerZ, useIdentityTransform,
772                                     static_cast<ISurfaceComposer::Rotation>(rotation));
773     if (ret != NO_ERROR) {
774         return ret;
775     }
776     return ret;
777 }
778 
captureLayers(const sp<IBinder> & layerHandle,Rect sourceCrop,float frameScale,sp<GraphicBuffer> * outBuffer)779 status_t ScreenshotClient::captureLayers(const sp<IBinder>& layerHandle, Rect sourceCrop,
780                                          float frameScale, sp<GraphicBuffer>* outBuffer) {
781     sp<ISurfaceComposer> s(ComposerService::getComposerService());
782     if (s == NULL) return NO_INIT;
783     status_t ret = s->captureLayers(layerHandle, outBuffer, sourceCrop, frameScale,
784                                     false /* childrenOnly */);
785     return ret;
786 }
787 
captureChildLayers(const sp<IBinder> & layerHandle,Rect sourceCrop,float frameScale,sp<GraphicBuffer> * outBuffer)788 status_t ScreenshotClient::captureChildLayers(const sp<IBinder>& layerHandle, Rect sourceCrop,
789                                               float frameScale, sp<GraphicBuffer>* outBuffer) {
790     sp<ISurfaceComposer> s(ComposerService::getComposerService());
791     if (s == NULL) return NO_INIT;
792     status_t ret = s->captureLayers(layerHandle, outBuffer, sourceCrop, frameScale,
793                                     true /* childrenOnly */);
794     return ret;
795 }
796 // ----------------------------------------------------------------------------
797 }; // namespace android
798