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