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     mComposerService->asBinder()->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             int32_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,int32_t z)295 status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
296         const sp<IBinder>& id, int32_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         s->what |= layer_state_t::eOpacityChanged;
315     }
316     if (mask & layer_state_t::eLayerHidden) {
317         s->what |= layer_state_t::eVisibilityChanged;
318     }
319     s->flags &= ~mask;
320     s->flags |= (flags & mask);
321     s->mask |= mask;
322     return NO_ERROR;
323 }
324 
setTransparentRegionHint(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,const Region & transparentRegion)325 status_t Composer::setTransparentRegionHint(
326         const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
327         const Region& transparentRegion) {
328     Mutex::Autolock _l(mLock);
329     layer_state_t* s = getLayerStateLocked(client, id);
330     if (!s)
331         return BAD_INDEX;
332     s->what |= layer_state_t::eTransparentRegionChanged;
333     s->transparentRegion = transparentRegion;
334     return NO_ERROR;
335 }
336 
setAlpha(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,float alpha)337 status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
338         const sp<IBinder>& id, float alpha) {
339     Mutex::Autolock _l(mLock);
340     layer_state_t* s = getLayerStateLocked(client, id);
341     if (!s)
342         return BAD_INDEX;
343     s->what |= layer_state_t::eAlphaChanged;
344     s->alpha = alpha;
345     return NO_ERROR;
346 }
347 
setLayerStack(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,uint32_t layerStack)348 status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
349         const sp<IBinder>& id, uint32_t layerStack) {
350     Mutex::Autolock _l(mLock);
351     layer_state_t* s = getLayerStateLocked(client, id);
352     if (!s)
353         return BAD_INDEX;
354     s->what |= layer_state_t::eLayerStackChanged;
355     s->layerStack = layerStack;
356     return NO_ERROR;
357 }
358 
setMatrix(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,float dsdx,float dtdx,float dsdy,float dtdy)359 status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
360         const sp<IBinder>& id, float dsdx, float dtdx,
361         float dsdy, float dtdy) {
362     Mutex::Autolock _l(mLock);
363     layer_state_t* s = getLayerStateLocked(client, id);
364     if (!s)
365         return BAD_INDEX;
366     s->what |= layer_state_t::eMatrixChanged;
367     layer_state_t::matrix22_t matrix;
368     matrix.dsdx = dsdx;
369     matrix.dtdx = dtdx;
370     matrix.dsdy = dsdy;
371     matrix.dtdy = dtdy;
372     s->matrix = matrix;
373     return NO_ERROR;
374 }
375 
setCrop(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,const Rect & crop)376 status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
377         const sp<IBinder>& id, const Rect& crop) {
378     Mutex::Autolock _l(mLock);
379     layer_state_t* s = getLayerStateLocked(client, id);
380     if (!s)
381         return BAD_INDEX;
382     s->what |= layer_state_t::eCropChanged;
383     s->crop = crop;
384     return NO_ERROR;
385 }
386 
387 // ---------------------------------------------------------------------------
388 
getDisplayStateLocked(const sp<IBinder> & token)389 DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
390     DisplayState s;
391     s.token = token;
392     ssize_t index = mDisplayStates.indexOf(s);
393     if (index < 0) {
394         // we don't have it, add an initialized layer_state to our list
395         s.what = 0;
396         index = mDisplayStates.add(s);
397     }
398     return mDisplayStates.editItemAt(index);
399 }
400 
setDisplaySurface(const sp<IBinder> & token,const sp<IGraphicBufferProducer> & bufferProducer)401 void Composer::setDisplaySurface(const sp<IBinder>& token,
402         const sp<IGraphicBufferProducer>& bufferProducer) {
403     Mutex::Autolock _l(mLock);
404     DisplayState& s(getDisplayStateLocked(token));
405     s.surface = bufferProducer;
406     s.what |= DisplayState::eSurfaceChanged;
407 }
408 
setDisplayLayerStack(const sp<IBinder> & token,uint32_t layerStack)409 void Composer::setDisplayLayerStack(const sp<IBinder>& token,
410         uint32_t layerStack) {
411     Mutex::Autolock _l(mLock);
412     DisplayState& s(getDisplayStateLocked(token));
413     s.layerStack = layerStack;
414     s.what |= DisplayState::eLayerStackChanged;
415 }
416 
setDisplayProjection(const sp<IBinder> & token,uint32_t orientation,const Rect & layerStackRect,const Rect & displayRect)417 void Composer::setDisplayProjection(const sp<IBinder>& token,
418         uint32_t orientation,
419         const Rect& layerStackRect,
420         const Rect& displayRect) {
421     Mutex::Autolock _l(mLock);
422     DisplayState& s(getDisplayStateLocked(token));
423     s.orientation = orientation;
424     s.viewport = layerStackRect;
425     s.frame = displayRect;
426     s.what |= DisplayState::eDisplayProjectionChanged;
427     mForceSynchronous = true; // TODO: do we actually still need this?
428 }
429 
setDisplaySize(const sp<IBinder> & token,uint32_t width,uint32_t height)430 void Composer::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
431     Mutex::Autolock _l(mLock);
432     DisplayState& s(getDisplayStateLocked(token));
433     s.width = width;
434     s.height = height;
435     s.what |= DisplayState::eDisplaySizeChanged;
436 }
437 
438 // ---------------------------------------------------------------------------
439 
SurfaceComposerClient()440 SurfaceComposerClient::SurfaceComposerClient()
441     : mStatus(NO_INIT), mComposer(Composer::getInstance())
442 {
443 }
444 
onFirstRef()445 void SurfaceComposerClient::onFirstRef() {
446     sp<ISurfaceComposer> sm(ComposerService::getComposerService());
447     if (sm != 0) {
448         sp<ISurfaceComposerClient> conn = sm->createConnection();
449         if (conn != 0) {
450             mClient = conn;
451             mStatus = NO_ERROR;
452         }
453     }
454 }
455 
~SurfaceComposerClient()456 SurfaceComposerClient::~SurfaceComposerClient() {
457     dispose();
458 }
459 
initCheck() const460 status_t SurfaceComposerClient::initCheck() const {
461     return mStatus;
462 }
463 
connection() const464 sp<IBinder> SurfaceComposerClient::connection() const {
465     return (mClient != 0) ? mClient->asBinder() : 0;
466 }
467 
linkToComposerDeath(const sp<IBinder::DeathRecipient> & recipient,void * cookie,uint32_t flags)468 status_t SurfaceComposerClient::linkToComposerDeath(
469         const sp<IBinder::DeathRecipient>& recipient,
470         void* cookie, uint32_t flags) {
471     sp<ISurfaceComposer> sm(ComposerService::getComposerService());
472     return sm->asBinder()->linkToDeath(recipient, cookie, flags);
473 }
474 
dispose()475 void SurfaceComposerClient::dispose() {
476     // this can be called more than once.
477     sp<ISurfaceComposerClient> client;
478     Mutex::Autolock _lm(mLock);
479     if (mClient != 0) {
480         client = mClient; // hold ref while lock is held
481         mClient.clear();
482     }
483     mStatus = NO_INIT;
484 }
485 
createSurface(const String8 & name,uint32_t w,uint32_t h,PixelFormat format,uint32_t flags)486 sp<SurfaceControl> SurfaceComposerClient::createSurface(
487         const String8& name,
488         uint32_t w,
489         uint32_t h,
490         PixelFormat format,
491         uint32_t flags)
492 {
493     sp<SurfaceControl> sur;
494     if (mStatus == NO_ERROR) {
495         sp<IBinder> handle;
496         sp<IGraphicBufferProducer> gbp;
497         status_t err = mClient->createSurface(name, w, h, format, flags,
498                 &handle, &gbp);
499         ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
500         if (err == NO_ERROR) {
501             sur = new SurfaceControl(this, handle, gbp);
502         }
503     }
504     return sur;
505 }
506 
createDisplay(const String8 & displayName,bool secure)507 sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
508         bool secure) {
509     return Composer::getInstance().createDisplay(displayName, secure);
510 }
511 
destroyDisplay(const sp<IBinder> & display)512 void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
513     Composer::getInstance().destroyDisplay(display);
514 }
515 
getBuiltInDisplay(int32_t id)516 sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
517     return Composer::getInstance().getBuiltInDisplay(id);
518 }
519 
destroySurface(const sp<IBinder> & sid)520 status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
521     if (mStatus != NO_ERROR)
522         return mStatus;
523     status_t err = mClient->destroySurface(sid);
524     return err;
525 }
526 
clearLayerFrameStats(const sp<IBinder> & token) const527 status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
528     if (mStatus != NO_ERROR) {
529         return mStatus;
530     }
531     return mClient->clearLayerFrameStats(token);
532 }
533 
getLayerFrameStats(const sp<IBinder> & token,FrameStats * outStats) const534 status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
535         FrameStats* outStats) const {
536     if (mStatus != NO_ERROR) {
537         return mStatus;
538     }
539     return mClient->getLayerFrameStats(token, outStats);
540 }
541 
getComposer()542 inline Composer& SurfaceComposerClient::getComposer() {
543     return mComposer;
544 }
545 
546 // ----------------------------------------------------------------------------
547 
openGlobalTransaction()548 void SurfaceComposerClient::openGlobalTransaction() {
549     Composer::openGlobalTransaction();
550 }
551 
closeGlobalTransaction(bool synchronous)552 void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
553     Composer::closeGlobalTransaction(synchronous);
554 }
555 
setAnimationTransaction()556 void SurfaceComposerClient::setAnimationTransaction() {
557     Composer::setAnimationTransaction();
558 }
559 
560 // ----------------------------------------------------------------------------
561 
setCrop(const sp<IBinder> & id,const Rect & crop)562 status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
563     return getComposer().setCrop(this, id, crop);
564 }
565 
setPosition(const sp<IBinder> & id,float x,float y)566 status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
567     return getComposer().setPosition(this, id, x, y);
568 }
569 
setSize(const sp<IBinder> & id,uint32_t w,uint32_t h)570 status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
571     return getComposer().setSize(this, id, w, h);
572 }
573 
setLayer(const sp<IBinder> & id,int32_t z)574 status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) {
575     return getComposer().setLayer(this, id, z);
576 }
577 
hide(const sp<IBinder> & id)578 status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
579     return getComposer().setFlags(this, id,
580             layer_state_t::eLayerHidden,
581             layer_state_t::eLayerHidden);
582 }
583 
show(const sp<IBinder> & id)584 status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
585     return getComposer().setFlags(this, id,
586             0,
587             layer_state_t::eLayerHidden);
588 }
589 
setFlags(const sp<IBinder> & id,uint32_t flags,uint32_t mask)590 status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
591         uint32_t mask) {
592     return getComposer().setFlags(this, id, flags, mask);
593 }
594 
setTransparentRegionHint(const sp<IBinder> & id,const Region & transparentRegion)595 status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
596         const Region& transparentRegion) {
597     return getComposer().setTransparentRegionHint(this, id, transparentRegion);
598 }
599 
setAlpha(const sp<IBinder> & id,float alpha)600 status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
601     return getComposer().setAlpha(this, id, alpha);
602 }
603 
setLayerStack(const sp<IBinder> & id,uint32_t layerStack)604 status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
605     return getComposer().setLayerStack(this, id, layerStack);
606 }
607 
setMatrix(const sp<IBinder> & id,float dsdx,float dtdx,float dsdy,float dtdy)608 status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
609         float dsdy, float dtdy) {
610     return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
611 }
612 
613 // ----------------------------------------------------------------------------
614 
setDisplaySurface(const sp<IBinder> & token,const sp<IGraphicBufferProducer> & bufferProducer)615 void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
616         const sp<IGraphicBufferProducer>& bufferProducer) {
617     Composer::getInstance().setDisplaySurface(token, bufferProducer);
618 }
619 
setDisplayLayerStack(const sp<IBinder> & token,uint32_t layerStack)620 void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
621         uint32_t layerStack) {
622     Composer::getInstance().setDisplayLayerStack(token, layerStack);
623 }
624 
setDisplayProjection(const sp<IBinder> & token,uint32_t orientation,const Rect & layerStackRect,const Rect & displayRect)625 void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
626         uint32_t orientation,
627         const Rect& layerStackRect,
628         const Rect& displayRect) {
629     Composer::getInstance().setDisplayProjection(token, orientation,
630             layerStackRect, displayRect);
631 }
632 
setDisplaySize(const sp<IBinder> & token,uint32_t width,uint32_t height)633 void SurfaceComposerClient::setDisplaySize(const sp<IBinder>& token,
634         uint32_t width, uint32_t height) {
635     Composer::getInstance().setDisplaySize(token, width, height);
636 }
637 
638 // ----------------------------------------------------------------------------
639 
getDisplayConfigs(const sp<IBinder> & display,Vector<DisplayInfo> * configs)640 status_t SurfaceComposerClient::getDisplayConfigs(
641         const sp<IBinder>& display, Vector<DisplayInfo>* configs)
642 {
643     return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
644 }
645 
getDisplayInfo(const sp<IBinder> & display,DisplayInfo * info)646 status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
647         DisplayInfo* info) {
648     Vector<DisplayInfo> configs;
649     status_t result = getDisplayConfigs(display, &configs);
650     if (result != NO_ERROR) {
651         return result;
652     }
653 
654     int activeId = getActiveConfig(display);
655     if (activeId < 0) {
656         ALOGE("No active configuration found");
657         return NAME_NOT_FOUND;
658     }
659 
660     *info = configs[activeId];
661     return NO_ERROR;
662 }
663 
getActiveConfig(const sp<IBinder> & display)664 int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
665     return ComposerService::getComposerService()->getActiveConfig(display);
666 }
667 
setActiveConfig(const sp<IBinder> & display,int id)668 status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
669     return ComposerService::getComposerService()->setActiveConfig(display, id);
670 }
671 
setDisplayPowerMode(const sp<IBinder> & token,int mode)672 void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
673         int mode) {
674     ComposerService::getComposerService()->setPowerMode(token, mode);
675 }
676 
clearAnimationFrameStats()677 status_t SurfaceComposerClient::clearAnimationFrameStats() {
678     return ComposerService::getComposerService()->clearAnimationFrameStats();
679 }
680 
getAnimationFrameStats(FrameStats * outStats)681 status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
682     return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
683 }
684 
685 // ----------------------------------------------------------------------------
686 
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)687 status_t ScreenshotClient::capture(
688         const sp<IBinder>& display,
689         const sp<IGraphicBufferProducer>& producer,
690         Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
691         uint32_t minLayerZ, uint32_t maxLayerZ, bool useIdentityTransform) {
692     sp<ISurfaceComposer> s(ComposerService::getComposerService());
693     if (s == NULL) return NO_INIT;
694     return s->captureScreen(display, producer, sourceCrop,
695             reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
696 }
697 
ScreenshotClient()698 ScreenshotClient::ScreenshotClient()
699     : mHaveBuffer(false) {
700     memset(&mBuffer, 0, sizeof(mBuffer));
701 }
702 
~ScreenshotClient()703 ScreenshotClient::~ScreenshotClient() {
704     ScreenshotClient::release();
705 }
706 
getCpuConsumer() const707 sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
708     if (mCpuConsumer == NULL) {
709         sp<IGraphicBufferConsumer> consumer;
710         BufferQueue::createBufferQueue(&mProducer, &consumer);
711         mCpuConsumer = new CpuConsumer(consumer, 1);
712         mCpuConsumer->setName(String8("ScreenshotClient"));
713     }
714     return mCpuConsumer;
715 }
716 
update(const sp<IBinder> & display,Rect sourceCrop,uint32_t reqWidth,uint32_t reqHeight,uint32_t minLayerZ,uint32_t maxLayerZ,bool useIdentityTransform,uint32_t rotation)717 status_t ScreenshotClient::update(const sp<IBinder>& display,
718         Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
719         uint32_t minLayerZ, uint32_t maxLayerZ,
720         bool useIdentityTransform, uint32_t rotation) {
721     sp<ISurfaceComposer> s(ComposerService::getComposerService());
722     if (s == NULL) return NO_INIT;
723     sp<CpuConsumer> cpuConsumer = getCpuConsumer();
724 
725     if (mHaveBuffer) {
726         mCpuConsumer->unlockBuffer(mBuffer);
727         memset(&mBuffer, 0, sizeof(mBuffer));
728         mHaveBuffer = false;
729     }
730 
731     status_t err = s->captureScreen(display, mProducer, sourceCrop,
732             reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform,
733             static_cast<ISurfaceComposer::Rotation>(rotation));
734 
735     if (err == NO_ERROR) {
736         err = mCpuConsumer->lockNextBuffer(&mBuffer);
737         if (err == NO_ERROR) {
738             mHaveBuffer = true;
739         }
740     }
741     return err;
742 }
743 
update(const sp<IBinder> & display,Rect sourceCrop,uint32_t reqWidth,uint32_t reqHeight,uint32_t minLayerZ,uint32_t maxLayerZ,bool useIdentityTransform)744 status_t ScreenshotClient::update(const sp<IBinder>& display,
745         Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
746         uint32_t minLayerZ, uint32_t maxLayerZ,
747         bool useIdentityTransform) {
748 
749     return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
750             minLayerZ, maxLayerZ, useIdentityTransform, ISurfaceComposer::eRotateNone);
751 }
752 
update(const sp<IBinder> & display,Rect sourceCrop,bool useIdentityTransform)753 status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
754         bool useIdentityTransform) {
755     return ScreenshotClient::update(display, sourceCrop, 0, 0, 0, -1UL,
756             useIdentityTransform, ISurfaceComposer::eRotateNone);
757 }
758 
update(const sp<IBinder> & display,Rect sourceCrop,uint32_t reqWidth,uint32_t reqHeight,bool useIdentityTransform)759 status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
760         uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
761     return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
762             0, -1UL, useIdentityTransform, ISurfaceComposer::eRotateNone);
763 }
764 
release()765 void ScreenshotClient::release() {
766     if (mHaveBuffer) {
767         mCpuConsumer->unlockBuffer(mBuffer);
768         memset(&mBuffer, 0, sizeof(mBuffer));
769         mHaveBuffer = false;
770     }
771     mCpuConsumer.clear();
772 }
773 
getPixels() const774 void const* ScreenshotClient::getPixels() const {
775     return mBuffer.data;
776 }
777 
getWidth() const778 uint32_t ScreenshotClient::getWidth() const {
779     return mBuffer.width;
780 }
781 
getHeight() const782 uint32_t ScreenshotClient::getHeight() const {
783     return mBuffer.height;
784 }
785 
getFormat() const786 PixelFormat ScreenshotClient::getFormat() const {
787     return mBuffer.format;
788 }
789 
getStride() const790 uint32_t ScreenshotClient::getStride() const {
791     return mBuffer.stride;
792 }
793 
getSize() const794 size_t ScreenshotClient::getSize() const {
795     return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
796 }
797 
798 // ----------------------------------------------------------------------------
799 }; // namespace android
800