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 setFinalCrop(const sp<SurfaceComposerClient>& client,
160             const sp<IBinder>& id, const Rect& crop);
161     status_t setLayerStack(const sp<SurfaceComposerClient>& client,
162             const sp<IBinder>& id, uint32_t layerStack);
163     status_t deferTransactionUntil(const sp<SurfaceComposerClient>& client,
164             const sp<IBinder>& id, const sp<IBinder>& handle,
165             uint64_t frameNumber);
166     status_t setOverrideScalingMode(const sp<SurfaceComposerClient>& client,
167             const sp<IBinder>& id, int32_t overrideScalingMode);
168     status_t setPositionAppliesWithResize(const sp<SurfaceComposerClient>& client,
169             const sp<IBinder>& id);
170 
171     void setDisplaySurface(const sp<IBinder>& token,
172             const sp<IGraphicBufferProducer>& bufferProducer);
173     void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
174     void setDisplayProjection(const sp<IBinder>& token,
175             uint32_t orientation,
176             const Rect& layerStackRect,
177             const Rect& displayRect);
178     void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
179 
setAnimationTransaction()180     static void setAnimationTransaction() {
181         Composer::getInstance().setAnimationTransactionImpl();
182     }
183 
openGlobalTransaction()184     static void openGlobalTransaction() {
185         Composer::getInstance().openGlobalTransactionImpl();
186     }
187 
closeGlobalTransaction(bool synchronous)188     static void closeGlobalTransaction(bool synchronous) {
189         Composer::getInstance().closeGlobalTransactionImpl(synchronous);
190     }
191 };
192 
193 ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
194 
195 // ---------------------------------------------------------------------------
196 
createDisplay(const String8 & displayName,bool secure)197 sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
198     return ComposerService::getComposerService()->createDisplay(displayName,
199             secure);
200 }
201 
destroyDisplay(const sp<IBinder> & display)202 void Composer::destroyDisplay(const sp<IBinder>& display) {
203     return ComposerService::getComposerService()->destroyDisplay(display);
204 }
205 
getBuiltInDisplay(int32_t id)206 sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
207     return ComposerService::getComposerService()->getBuiltInDisplay(id);
208 }
209 
openGlobalTransactionImpl()210 void Composer::openGlobalTransactionImpl() {
211     { // scope for the lock
212         Mutex::Autolock _l(mLock);
213         mTransactionNestCount += 1;
214     }
215 }
216 
closeGlobalTransactionImpl(bool synchronous)217 void Composer::closeGlobalTransactionImpl(bool synchronous) {
218     sp<ISurfaceComposer> sm(ComposerService::getComposerService());
219 
220     Vector<ComposerState> transaction;
221     Vector<DisplayState> displayTransaction;
222     uint32_t flags = 0;
223 
224     { // scope for the lock
225         Mutex::Autolock _l(mLock);
226         mForceSynchronous |= synchronous;
227         if (!mTransactionNestCount) {
228             ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
229                     "call to openGlobalTransaction().");
230         } else if (--mTransactionNestCount) {
231             return;
232         }
233 
234         transaction = mComposerStates;
235         mComposerStates.clear();
236 
237         displayTransaction = mDisplayStates;
238         mDisplayStates.clear();
239 
240         if (mForceSynchronous) {
241             flags |= ISurfaceComposer::eSynchronous;
242         }
243         if (mAnimation) {
244             flags |= ISurfaceComposer::eAnimation;
245         }
246 
247         mForceSynchronous = false;
248         mAnimation = false;
249     }
250 
251    sm->setTransactionState(transaction, displayTransaction, flags);
252 }
253 
setAnimationTransactionImpl()254 void Composer::setAnimationTransactionImpl() {
255     Mutex::Autolock _l(mLock);
256     mAnimation = true;
257 }
258 
getLayerStateLocked(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id)259 layer_state_t* Composer::getLayerStateLocked(
260         const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
261 
262     ComposerState s;
263     s.client = client->mClient;
264     s.state.surface = id;
265 
266     ssize_t index = mComposerStates.indexOf(s);
267     if (index < 0) {
268         // we don't have it, add an initialized layer_state to our list
269         index = mComposerStates.add(s);
270     }
271 
272     ComposerState* const out = mComposerStates.editArray();
273     return &(out[index].state);
274 }
275 
setPosition(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,float x,float y)276 status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
277         const sp<IBinder>& id, float x, float y) {
278     Mutex::Autolock _l(mLock);
279     layer_state_t* s = getLayerStateLocked(client, id);
280     if (!s)
281         return BAD_INDEX;
282     s->what |= layer_state_t::ePositionChanged;
283     s->x = x;
284     s->y = y;
285     return NO_ERROR;
286 }
287 
setSize(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,uint32_t w,uint32_t h)288 status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
289         const sp<IBinder>& id, uint32_t w, uint32_t h) {
290     Mutex::Autolock _l(mLock);
291     layer_state_t* s = getLayerStateLocked(client, id);
292     if (!s)
293         return BAD_INDEX;
294     s->what |= layer_state_t::eSizeChanged;
295     s->w = w;
296     s->h = h;
297 
298     // Resizing a surface makes the transaction synchronous.
299     mForceSynchronous = true;
300 
301     return NO_ERROR;
302 }
303 
setLayer(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,uint32_t z)304 status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
305         const sp<IBinder>& id, uint32_t z) {
306     Mutex::Autolock _l(mLock);
307     layer_state_t* s = getLayerStateLocked(client, id);
308     if (!s)
309         return BAD_INDEX;
310     s->what |= layer_state_t::eLayerChanged;
311     s->z = z;
312     return NO_ERROR;
313 }
314 
setFlags(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,uint32_t flags,uint32_t mask)315 status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
316         const sp<IBinder>& id, uint32_t flags,
317         uint32_t mask) {
318     Mutex::Autolock _l(mLock);
319     layer_state_t* s = getLayerStateLocked(client, id);
320     if (!s)
321         return BAD_INDEX;
322     if ((mask & layer_state_t::eLayerOpaque) ||
323             (mask & layer_state_t::eLayerHidden) ||
324             (mask & layer_state_t::eLayerSecure)) {
325         s->what |= layer_state_t::eFlagsChanged;
326     }
327     s->flags &= ~mask;
328     s->flags |= (flags & mask);
329     s->mask |= mask;
330     return NO_ERROR;
331 }
332 
setTransparentRegionHint(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,const Region & transparentRegion)333 status_t Composer::setTransparentRegionHint(
334         const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
335         const Region& transparentRegion) {
336     Mutex::Autolock _l(mLock);
337     layer_state_t* s = getLayerStateLocked(client, id);
338     if (!s)
339         return BAD_INDEX;
340     s->what |= layer_state_t::eTransparentRegionChanged;
341     s->transparentRegion = transparentRegion;
342     return NO_ERROR;
343 }
344 
setAlpha(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,float alpha)345 status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
346         const sp<IBinder>& id, float alpha) {
347     Mutex::Autolock _l(mLock);
348     layer_state_t* s = getLayerStateLocked(client, id);
349     if (!s)
350         return BAD_INDEX;
351     s->what |= layer_state_t::eAlphaChanged;
352     s->alpha = alpha;
353     return NO_ERROR;
354 }
355 
setLayerStack(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,uint32_t layerStack)356 status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
357         const sp<IBinder>& id, uint32_t layerStack) {
358     Mutex::Autolock _l(mLock);
359     layer_state_t* s = getLayerStateLocked(client, id);
360     if (!s)
361         return BAD_INDEX;
362     s->what |= layer_state_t::eLayerStackChanged;
363     s->layerStack = layerStack;
364     return NO_ERROR;
365 }
366 
setMatrix(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,float dsdx,float dtdx,float dsdy,float dtdy)367 status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
368         const sp<IBinder>& id, float dsdx, float dtdx,
369         float dsdy, float dtdy) {
370     Mutex::Autolock _l(mLock);
371     layer_state_t* s = getLayerStateLocked(client, id);
372     if (!s)
373         return BAD_INDEX;
374     s->what |= layer_state_t::eMatrixChanged;
375     layer_state_t::matrix22_t matrix;
376     matrix.dsdx = dsdx;
377     matrix.dtdx = dtdx;
378     matrix.dsdy = dsdy;
379     matrix.dtdy = dtdy;
380     s->matrix = matrix;
381     return NO_ERROR;
382 }
383 
setCrop(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,const Rect & crop)384 status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
385         const sp<IBinder>& id, const Rect& crop) {
386     Mutex::Autolock _l(mLock);
387     layer_state_t* s = getLayerStateLocked(client, id);
388     if (!s)
389         return BAD_INDEX;
390     s->what |= layer_state_t::eCropChanged;
391     s->crop = crop;
392     return NO_ERROR;
393 }
394 
setFinalCrop(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,const Rect & crop)395 status_t Composer::setFinalCrop(const sp<SurfaceComposerClient>& client,
396         const sp<IBinder>& id, const Rect& crop) {
397     Mutex::Autolock _l(mLock);
398     layer_state_t* s = getLayerStateLocked(client, id);
399     if (!s) {
400         return BAD_INDEX;
401     }
402     s->what |= layer_state_t::eFinalCropChanged;
403     s->finalCrop = crop;
404     return NO_ERROR;
405 }
406 
deferTransactionUntil(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,const sp<IBinder> & handle,uint64_t frameNumber)407 status_t Composer::deferTransactionUntil(
408         const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
409         const sp<IBinder>& handle, uint64_t frameNumber) {
410     Mutex::Autolock lock(mLock);
411     layer_state_t* s = getLayerStateLocked(client, id);
412     if (!s) {
413         return BAD_INDEX;
414     }
415     s->what |= layer_state_t::eDeferTransaction;
416     s->handle = handle;
417     s->frameNumber = frameNumber;
418     return NO_ERROR;
419 }
420 
setOverrideScalingMode(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id,int32_t overrideScalingMode)421 status_t Composer::setOverrideScalingMode(
422         const sp<SurfaceComposerClient>& client,
423         const sp<IBinder>& id, int32_t overrideScalingMode) {
424     Mutex::Autolock lock(mLock);
425     layer_state_t* s = getLayerStateLocked(client, id);
426     if (!s) {
427         return BAD_INDEX;
428     }
429 
430     switch (overrideScalingMode) {
431         case NATIVE_WINDOW_SCALING_MODE_FREEZE:
432         case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
433         case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
434         case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
435         case -1:
436             break;
437         default:
438             ALOGE("unknown scaling mode: %d",
439                     overrideScalingMode);
440             return BAD_VALUE;
441     }
442 
443     s->what |= layer_state_t::eOverrideScalingModeChanged;
444     s->overrideScalingMode = overrideScalingMode;
445     return NO_ERROR;
446 }
447 
setPositionAppliesWithResize(const sp<SurfaceComposerClient> & client,const sp<IBinder> & id)448 status_t Composer::setPositionAppliesWithResize(
449         const sp<SurfaceComposerClient>& client,
450         const sp<IBinder>& id) {
451     Mutex::Autolock lock(mLock);
452     layer_state_t* s = getLayerStateLocked(client, id);
453     if (!s) {
454         return BAD_INDEX;
455     }
456     s->what |= layer_state_t::ePositionAppliesWithResize;
457     return NO_ERROR;
458 }
459 
460 // ---------------------------------------------------------------------------
461 
getDisplayStateLocked(const sp<IBinder> & token)462 DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
463     DisplayState s;
464     s.token = token;
465     ssize_t index = mDisplayStates.indexOf(s);
466     if (index < 0) {
467         // we don't have it, add an initialized layer_state to our list
468         s.what = 0;
469         index = mDisplayStates.add(s);
470     }
471     return mDisplayStates.editItemAt(static_cast<size_t>(index));
472 }
473 
setDisplaySurface(const sp<IBinder> & token,const sp<IGraphicBufferProducer> & bufferProducer)474 void Composer::setDisplaySurface(const sp<IBinder>& token,
475         const sp<IGraphicBufferProducer>& bufferProducer) {
476     Mutex::Autolock _l(mLock);
477     DisplayState& s(getDisplayStateLocked(token));
478     s.surface = bufferProducer;
479     s.what |= DisplayState::eSurfaceChanged;
480 }
481 
setDisplayLayerStack(const sp<IBinder> & token,uint32_t layerStack)482 void Composer::setDisplayLayerStack(const sp<IBinder>& token,
483         uint32_t layerStack) {
484     Mutex::Autolock _l(mLock);
485     DisplayState& s(getDisplayStateLocked(token));
486     s.layerStack = layerStack;
487     s.what |= DisplayState::eLayerStackChanged;
488 }
489 
setDisplayProjection(const sp<IBinder> & token,uint32_t orientation,const Rect & layerStackRect,const Rect & displayRect)490 void Composer::setDisplayProjection(const sp<IBinder>& token,
491         uint32_t orientation,
492         const Rect& layerStackRect,
493         const Rect& displayRect) {
494     Mutex::Autolock _l(mLock);
495     DisplayState& s(getDisplayStateLocked(token));
496     s.orientation = orientation;
497     s.viewport = layerStackRect;
498     s.frame = displayRect;
499     s.what |= DisplayState::eDisplayProjectionChanged;
500     mForceSynchronous = true; // TODO: do we actually still need this?
501 }
502 
setDisplaySize(const sp<IBinder> & token,uint32_t width,uint32_t height)503 void Composer::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
504     Mutex::Autolock _l(mLock);
505     DisplayState& s(getDisplayStateLocked(token));
506     s.width = width;
507     s.height = height;
508     s.what |= DisplayState::eDisplaySizeChanged;
509 }
510 
511 // ---------------------------------------------------------------------------
512 
SurfaceComposerClient()513 SurfaceComposerClient::SurfaceComposerClient()
514     : mStatus(NO_INIT), mComposer(Composer::getInstance())
515 {
516 }
517 
onFirstRef()518 void SurfaceComposerClient::onFirstRef() {
519     sp<ISurfaceComposer> sm(ComposerService::getComposerService());
520     if (sm != 0) {
521         sp<ISurfaceComposerClient> conn = sm->createConnection();
522         if (conn != 0) {
523             mClient = conn;
524             mStatus = NO_ERROR;
525         }
526     }
527 }
528 
~SurfaceComposerClient()529 SurfaceComposerClient::~SurfaceComposerClient() {
530     dispose();
531 }
532 
initCheck() const533 status_t SurfaceComposerClient::initCheck() const {
534     return mStatus;
535 }
536 
connection() const537 sp<IBinder> SurfaceComposerClient::connection() const {
538     return IInterface::asBinder(mClient);
539 }
540 
linkToComposerDeath(const sp<IBinder::DeathRecipient> & recipient,void * cookie,uint32_t flags)541 status_t SurfaceComposerClient::linkToComposerDeath(
542         const sp<IBinder::DeathRecipient>& recipient,
543         void* cookie, uint32_t flags) {
544     sp<ISurfaceComposer> sm(ComposerService::getComposerService());
545     return IInterface::asBinder(sm)->linkToDeath(recipient, cookie, flags);
546 }
547 
dispose()548 void SurfaceComposerClient::dispose() {
549     // this can be called more than once.
550     sp<ISurfaceComposerClient> client;
551     Mutex::Autolock _lm(mLock);
552     if (mClient != 0) {
553         client = mClient; // hold ref while lock is held
554         mClient.clear();
555     }
556     mStatus = NO_INIT;
557 }
558 
createSurface(const String8 & name,uint32_t w,uint32_t h,PixelFormat format,uint32_t flags)559 sp<SurfaceControl> SurfaceComposerClient::createSurface(
560         const String8& name,
561         uint32_t w,
562         uint32_t h,
563         PixelFormat format,
564         uint32_t flags)
565 {
566     sp<SurfaceControl> sur;
567     if (mStatus == NO_ERROR) {
568         sp<IBinder> handle;
569         sp<IGraphicBufferProducer> gbp;
570         status_t err = mClient->createSurface(name, w, h, format, flags,
571                 &handle, &gbp);
572         ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
573         if (err == NO_ERROR) {
574             sur = new SurfaceControl(this, handle, gbp);
575         }
576     }
577     return sur;
578 }
579 
createDisplay(const String8 & displayName,bool secure)580 sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
581         bool secure) {
582     return Composer::getInstance().createDisplay(displayName, secure);
583 }
584 
destroyDisplay(const sp<IBinder> & display)585 void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
586     Composer::getInstance().destroyDisplay(display);
587 }
588 
getBuiltInDisplay(int32_t id)589 sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
590     return Composer::getInstance().getBuiltInDisplay(id);
591 }
592 
destroySurface(const sp<IBinder> & sid)593 status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
594     if (mStatus != NO_ERROR)
595         return mStatus;
596     status_t err = mClient->destroySurface(sid);
597     return err;
598 }
599 
clearLayerFrameStats(const sp<IBinder> & token) const600 status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
601     if (mStatus != NO_ERROR) {
602         return mStatus;
603     }
604     return mClient->clearLayerFrameStats(token);
605 }
606 
getLayerFrameStats(const sp<IBinder> & token,FrameStats * outStats) const607 status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
608         FrameStats* outStats) const {
609     if (mStatus != NO_ERROR) {
610         return mStatus;
611     }
612     return mClient->getLayerFrameStats(token, outStats);
613 }
614 
getComposer()615 inline Composer& SurfaceComposerClient::getComposer() {
616     return mComposer;
617 }
618 
619 // ----------------------------------------------------------------------------
620 
openGlobalTransaction()621 void SurfaceComposerClient::openGlobalTransaction() {
622     Composer::openGlobalTransaction();
623 }
624 
closeGlobalTransaction(bool synchronous)625 void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
626     Composer::closeGlobalTransaction(synchronous);
627 }
628 
setAnimationTransaction()629 void SurfaceComposerClient::setAnimationTransaction() {
630     Composer::setAnimationTransaction();
631 }
632 
633 // ----------------------------------------------------------------------------
634 
setCrop(const sp<IBinder> & id,const Rect & crop)635 status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
636     return getComposer().setCrop(this, id, crop);
637 }
638 
setFinalCrop(const sp<IBinder> & id,const Rect & crop)639 status_t SurfaceComposerClient::setFinalCrop(const sp<IBinder>& id,
640         const Rect& crop) {
641     return getComposer().setFinalCrop(this, id, crop);
642 }
643 
setPosition(const sp<IBinder> & id,float x,float y)644 status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
645     return getComposer().setPosition(this, id, x, y);
646 }
647 
setSize(const sp<IBinder> & id,uint32_t w,uint32_t h)648 status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
649     return getComposer().setSize(this, id, w, h);
650 }
651 
setLayer(const sp<IBinder> & id,uint32_t z)652 status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, uint32_t z) {
653     return getComposer().setLayer(this, id, z);
654 }
655 
hide(const sp<IBinder> & id)656 status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
657     return getComposer().setFlags(this, id,
658             layer_state_t::eLayerHidden,
659             layer_state_t::eLayerHidden);
660 }
661 
show(const sp<IBinder> & id)662 status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
663     return getComposer().setFlags(this, id,
664             0,
665             layer_state_t::eLayerHidden);
666 }
667 
setFlags(const sp<IBinder> & id,uint32_t flags,uint32_t mask)668 status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
669         uint32_t mask) {
670     return getComposer().setFlags(this, id, flags, mask);
671 }
672 
setTransparentRegionHint(const sp<IBinder> & id,const Region & transparentRegion)673 status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
674         const Region& transparentRegion) {
675     return getComposer().setTransparentRegionHint(this, id, transparentRegion);
676 }
677 
setAlpha(const sp<IBinder> & id,float alpha)678 status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
679     return getComposer().setAlpha(this, id, alpha);
680 }
681 
setLayerStack(const sp<IBinder> & id,uint32_t layerStack)682 status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
683     return getComposer().setLayerStack(this, id, layerStack);
684 }
685 
setMatrix(const sp<IBinder> & id,float dsdx,float dtdx,float dsdy,float dtdy)686 status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
687         float dsdy, float dtdy) {
688     return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
689 }
690 
deferTransactionUntil(const sp<IBinder> & id,const sp<IBinder> & handle,uint64_t frameNumber)691 status_t SurfaceComposerClient::deferTransactionUntil(const sp<IBinder>& id,
692         const sp<IBinder>& handle, uint64_t frameNumber) {
693     return getComposer().deferTransactionUntil(this, id, handle, frameNumber);
694 }
695 
setOverrideScalingMode(const sp<IBinder> & id,int32_t overrideScalingMode)696 status_t SurfaceComposerClient::setOverrideScalingMode(
697         const sp<IBinder>& id, int32_t overrideScalingMode) {
698     return getComposer().setOverrideScalingMode(
699             this, id, overrideScalingMode);
700 }
701 
setPositionAppliesWithResize(const sp<IBinder> & id)702 status_t SurfaceComposerClient::setPositionAppliesWithResize(
703         const sp<IBinder>& id) {
704     return getComposer().setPositionAppliesWithResize(this, id);
705 }
706 
707 // ----------------------------------------------------------------------------
708 
setDisplaySurface(const sp<IBinder> & token,const sp<IGraphicBufferProducer> & bufferProducer)709 void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
710         const sp<IGraphicBufferProducer>& bufferProducer) {
711     Composer::getInstance().setDisplaySurface(token, bufferProducer);
712 }
713 
setDisplayLayerStack(const sp<IBinder> & token,uint32_t layerStack)714 void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
715         uint32_t layerStack) {
716     Composer::getInstance().setDisplayLayerStack(token, layerStack);
717 }
718 
setDisplayProjection(const sp<IBinder> & token,uint32_t orientation,const Rect & layerStackRect,const Rect & displayRect)719 void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
720         uint32_t orientation,
721         const Rect& layerStackRect,
722         const Rect& displayRect) {
723     Composer::getInstance().setDisplayProjection(token, orientation,
724             layerStackRect, displayRect);
725 }
726 
setDisplaySize(const sp<IBinder> & token,uint32_t width,uint32_t height)727 void SurfaceComposerClient::setDisplaySize(const sp<IBinder>& token,
728         uint32_t width, uint32_t height) {
729     Composer::getInstance().setDisplaySize(token, width, height);
730 }
731 
732 // ----------------------------------------------------------------------------
733 
getDisplayConfigs(const sp<IBinder> & display,Vector<DisplayInfo> * configs)734 status_t SurfaceComposerClient::getDisplayConfigs(
735         const sp<IBinder>& display, Vector<DisplayInfo>* configs)
736 {
737     return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
738 }
739 
getDisplayInfo(const sp<IBinder> & display,DisplayInfo * info)740 status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
741         DisplayInfo* info) {
742     Vector<DisplayInfo> configs;
743     status_t result = getDisplayConfigs(display, &configs);
744     if (result != NO_ERROR) {
745         return result;
746     }
747 
748     int activeId = getActiveConfig(display);
749     if (activeId < 0) {
750         ALOGE("No active configuration found");
751         return NAME_NOT_FOUND;
752     }
753 
754     *info = configs[static_cast<size_t>(activeId)];
755     return NO_ERROR;
756 }
757 
getActiveConfig(const sp<IBinder> & display)758 int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
759     return ComposerService::getComposerService()->getActiveConfig(display);
760 }
761 
setActiveConfig(const sp<IBinder> & display,int id)762 status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
763     return ComposerService::getComposerService()->setActiveConfig(display, id);
764 }
765 
setDisplayPowerMode(const sp<IBinder> & token,int mode)766 void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
767         int mode) {
768     ComposerService::getComposerService()->setPowerMode(token, mode);
769 }
770 
clearAnimationFrameStats()771 status_t SurfaceComposerClient::clearAnimationFrameStats() {
772     return ComposerService::getComposerService()->clearAnimationFrameStats();
773 }
774 
getAnimationFrameStats(FrameStats * outStats)775 status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
776     return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
777 }
778 
getHdrCapabilities(const sp<IBinder> & display,HdrCapabilities * outCapabilities)779 status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
780         HdrCapabilities* outCapabilities) {
781     return ComposerService::getComposerService()->getHdrCapabilities(display,
782             outCapabilities);
783 }
784 
785 // ----------------------------------------------------------------------------
786 
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)787 status_t ScreenshotClient::capture(
788         const sp<IBinder>& display,
789         const sp<IGraphicBufferProducer>& producer,
790         Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
791         uint32_t minLayerZ, uint32_t maxLayerZ, bool useIdentityTransform) {
792     sp<ISurfaceComposer> s(ComposerService::getComposerService());
793     if (s == NULL) return NO_INIT;
794     return s->captureScreen(display, producer, sourceCrop,
795             reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
796 }
797 
ScreenshotClient()798 ScreenshotClient::ScreenshotClient()
799     : mHaveBuffer(false) {
800     memset(&mBuffer, 0, sizeof(mBuffer));
801 }
802 
~ScreenshotClient()803 ScreenshotClient::~ScreenshotClient() {
804     ScreenshotClient::release();
805 }
806 
getCpuConsumer() const807 sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
808     if (mCpuConsumer == NULL) {
809         sp<IGraphicBufferConsumer> consumer;
810         BufferQueue::createBufferQueue(&mProducer, &consumer);
811         mCpuConsumer = new CpuConsumer(consumer, 1);
812         mCpuConsumer->setName(String8("ScreenshotClient"));
813     }
814     return mCpuConsumer;
815 }
816 
update(const sp<IBinder> & display,Rect sourceCrop,uint32_t reqWidth,uint32_t reqHeight,uint32_t minLayerZ,uint32_t maxLayerZ,bool useIdentityTransform,uint32_t rotation)817 status_t ScreenshotClient::update(const sp<IBinder>& display,
818         Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
819         uint32_t minLayerZ, uint32_t maxLayerZ,
820         bool useIdentityTransform, uint32_t rotation) {
821     sp<ISurfaceComposer> s(ComposerService::getComposerService());
822     if (s == NULL) return NO_INIT;
823     sp<CpuConsumer> cpuConsumer = getCpuConsumer();
824 
825     if (mHaveBuffer) {
826         mCpuConsumer->unlockBuffer(mBuffer);
827         memset(&mBuffer, 0, sizeof(mBuffer));
828         mHaveBuffer = false;
829     }
830 
831     status_t err = s->captureScreen(display, mProducer, sourceCrop,
832             reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform,
833             static_cast<ISurfaceComposer::Rotation>(rotation));
834 
835     if (err == NO_ERROR) {
836         err = mCpuConsumer->lockNextBuffer(&mBuffer);
837         if (err == NO_ERROR) {
838             mHaveBuffer = true;
839         }
840     }
841     return err;
842 }
843 
update(const sp<IBinder> & display,Rect sourceCrop,uint32_t reqWidth,uint32_t reqHeight,uint32_t minLayerZ,uint32_t maxLayerZ,bool useIdentityTransform)844 status_t ScreenshotClient::update(const sp<IBinder>& display,
845         Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
846         uint32_t minLayerZ, uint32_t maxLayerZ,
847         bool useIdentityTransform) {
848 
849     return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
850             minLayerZ, maxLayerZ, useIdentityTransform, ISurfaceComposer::eRotateNone);
851 }
852 
update(const sp<IBinder> & display,Rect sourceCrop,bool useIdentityTransform)853 status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
854         bool useIdentityTransform) {
855     return ScreenshotClient::update(display, sourceCrop, 0, 0, 0, -1U,
856             useIdentityTransform, ISurfaceComposer::eRotateNone);
857 }
858 
update(const sp<IBinder> & display,Rect sourceCrop,uint32_t reqWidth,uint32_t reqHeight,bool useIdentityTransform)859 status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
860         uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
861     return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
862             0, -1U, useIdentityTransform, ISurfaceComposer::eRotateNone);
863 }
864 
release()865 void ScreenshotClient::release() {
866     if (mHaveBuffer) {
867         mCpuConsumer->unlockBuffer(mBuffer);
868         memset(&mBuffer, 0, sizeof(mBuffer));
869         mHaveBuffer = false;
870     }
871     mCpuConsumer.clear();
872 }
873 
getPixels() const874 void const* ScreenshotClient::getPixels() const {
875     return mBuffer.data;
876 }
877 
getWidth() const878 uint32_t ScreenshotClient::getWidth() const {
879     return mBuffer.width;
880 }
881 
getHeight() const882 uint32_t ScreenshotClient::getHeight() const {
883     return mBuffer.height;
884 }
885 
getFormat() const886 PixelFormat ScreenshotClient::getFormat() const {
887     return mBuffer.format;
888 }
889 
getStride() const890 uint32_t ScreenshotClient::getStride() const {
891     return mBuffer.stride;
892 }
893 
getSize() const894 size_t ScreenshotClient::getSize() const {
895     return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
896 }
897 
898 // ----------------------------------------------------------------------------
899 }; // namespace android
900