OSDN Git Service

Merge "Fix clang static anaylzer warnings." am: a97d58a21b am: 747d847ff1
[android-x86/frameworks-native.git] / libs / gui / SurfaceComposerClient.cpp
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/IServiceManager.h>
30
31 #include <system/graphics.h>
32
33 #include <ui/DisplayInfo.h>
34
35 #include <gui/BufferItemConsumer.h>
36 #include <gui/CpuConsumer.h>
37 #include <gui/IGraphicBufferProducer.h>
38 #include <gui/ISurfaceComposer.h>
39 #include <gui/ISurfaceComposerClient.h>
40 #include <gui/Surface.h>
41 #include <gui/SurfaceComposerClient.h>
42
43 #include <private/gui/ComposerService.h>
44 #include <private/gui/LayerState.h>
45
46 namespace android {
47 // ---------------------------------------------------------------------------
48
49 ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
50
51 ComposerService::ComposerService()
52 : Singleton<ComposerService>() {
53     Mutex::Autolock _l(mLock);
54     connectLocked();
55 }
56
57 void ComposerService::connectLocked() {
58     const String16 name("SurfaceFlinger");
59     while (getService(name, &mComposerService) != NO_ERROR) {
60         usleep(250000);
61     }
62     assert(mComposerService != NULL);
63
64     // Create the death listener.
65     class DeathObserver : public IBinder::DeathRecipient {
66         ComposerService& mComposerService;
67         virtual void binderDied(const wp<IBinder>& who) {
68             ALOGW("ComposerService remote (surfaceflinger) died [%p]",
69                   who.unsafe_get());
70             mComposerService.composerServiceDied();
71         }
72      public:
73         explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
74     };
75
76     mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
77     IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
78 }
79
80 /*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
81     ComposerService& instance = ComposerService::getInstance();
82     Mutex::Autolock _l(instance.mLock);
83     if (instance.mComposerService == NULL) {
84         ComposerService::getInstance().connectLocked();
85         assert(instance.mComposerService != NULL);
86         ALOGD("ComposerService reconnected");
87     }
88     return instance.mComposerService;
89 }
90
91 void ComposerService::composerServiceDied()
92 {
93     Mutex::Autolock _l(mLock);
94     mComposerService = NULL;
95     mDeathObserver = NULL;
96 }
97
98 // ---------------------------------------------------------------------------
99
100 static inline
101 int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
102     if (lhs.client < rhs.client)  return -1;
103     if (lhs.client > rhs.client)  return 1;
104     if (lhs.state.surface < rhs.state.surface)  return -1;
105     if (lhs.state.surface > rhs.state.surface)  return 1;
106     return 0;
107 }
108
109 static inline
110 int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
111     return compare_type(lhs.token, rhs.token);
112 }
113
114 class Composer : public Singleton<Composer>
115 {
116     friend class Singleton<Composer>;
117
118     mutable Mutex               mLock;
119     SortedVector<ComposerState> mComposerStates;
120     SortedVector<DisplayState > mDisplayStates;
121     uint32_t                    mForceSynchronous;
122     uint32_t                    mTransactionNestCount;
123     bool                        mAnimation;
124
125     Composer() : Singleton<Composer>(),
126         mForceSynchronous(0), mTransactionNestCount(0),
127         mAnimation(false)
128     { }
129
130     void openGlobalTransactionImpl();
131     void closeGlobalTransactionImpl(bool synchronous);
132     void setAnimationTransactionImpl();
133     status_t enableVSyncInjectionsImpl(bool enable);
134     status_t injectVSyncImpl(nsecs_t when);
135
136     layer_state_t* getLayerStateLocked(
137             const sp<SurfaceComposerClient>& client, const sp<IBinder>& id);
138
139     DisplayState& getDisplayStateLocked(const sp<IBinder>& token);
140
141 public:
142     sp<IBinder> createDisplay(const String8& displayName, bool secure);
143     void destroyDisplay(const sp<IBinder>& display);
144     sp<IBinder> getBuiltInDisplay(int32_t id);
145
146     status_t setPosition(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
147             float x, float y);
148     status_t setSize(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
149             uint32_t w, uint32_t h);
150     status_t setLayer(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
151             int32_t z);
152     status_t setRelativeLayer(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
153             const sp<IBinder>& relativeTo, int32_t z);
154     status_t setFlags(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
155             uint32_t flags, uint32_t mask);
156     status_t setTransparentRegionHint(
157             const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
158             const Region& transparentRegion);
159     status_t setAlpha(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
160             float alpha);
161     status_t setMatrix(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
162             float dsdx, float dtdx, float dtdy, float dsdy);
163     status_t setOrientation(int orientation);
164     status_t setCrop(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
165             const Rect& crop);
166     status_t setFinalCrop(const sp<SurfaceComposerClient>& client,
167             const sp<IBinder>& id, const Rect& crop);
168     status_t setLayerStack(const sp<SurfaceComposerClient>& client,
169             const sp<IBinder>& id, uint32_t layerStack);
170     status_t deferTransactionUntil(const sp<SurfaceComposerClient>& client,
171             const sp<IBinder>& id, const sp<IBinder>& handle,
172             uint64_t frameNumber);
173     status_t deferTransactionUntil(const sp<SurfaceComposerClient>& client,
174             const sp<IBinder>& id, const sp<Surface>& barrierSurface,
175             uint64_t frameNumber);
176     status_t reparentChildren(const sp<SurfaceComposerClient>& client,
177             const sp<IBinder>& id,
178             const sp<IBinder>& newParentHandle);
179     status_t detachChildren(const sp<SurfaceComposerClient>& client,
180             const sp<IBinder>& id);
181     status_t setOverrideScalingMode(const sp<SurfaceComposerClient>& client,
182             const sp<IBinder>& id, int32_t overrideScalingMode);
183     status_t setGeometryAppliesWithResize(const sp<SurfaceComposerClient>& client,
184             const sp<IBinder>& id);
185
186     status_t setDisplaySurface(const sp<IBinder>& token,
187             sp<IGraphicBufferProducer> bufferProducer);
188     void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
189     void setDisplayProjection(const sp<IBinder>& token,
190             uint32_t orientation,
191             const Rect& layerStackRect,
192             const Rect& displayRect);
193     void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
194
195     static void setAnimationTransaction() {
196         Composer::getInstance().setAnimationTransactionImpl();
197     }
198
199     static void openGlobalTransaction() {
200         Composer::getInstance().openGlobalTransactionImpl();
201     }
202
203     static void closeGlobalTransaction(bool synchronous) {
204         Composer::getInstance().closeGlobalTransactionImpl(synchronous);
205     }
206
207     static status_t enableVSyncInjections(bool enable) {
208         return Composer::getInstance().enableVSyncInjectionsImpl(enable);
209     }
210
211     static status_t injectVSync(nsecs_t when) {
212         return Composer::getInstance().injectVSyncImpl(when);
213     }
214 };
215
216 ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
217
218 // ---------------------------------------------------------------------------
219
220 sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
221     return ComposerService::getComposerService()->createDisplay(displayName,
222             secure);
223 }
224
225 void Composer::destroyDisplay(const sp<IBinder>& display) {
226     return ComposerService::getComposerService()->destroyDisplay(display);
227 }
228
229 sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
230     return ComposerService::getComposerService()->getBuiltInDisplay(id);
231 }
232
233 void Composer::openGlobalTransactionImpl() {
234     { // scope for the lock
235         Mutex::Autolock _l(mLock);
236         mTransactionNestCount += 1;
237     }
238 }
239
240 void Composer::closeGlobalTransactionImpl(bool synchronous) {
241     sp<ISurfaceComposer> sm(ComposerService::getComposerService());
242
243     Vector<ComposerState> transaction;
244     Vector<DisplayState> displayTransaction;
245     uint32_t flags = 0;
246
247     { // scope for the lock
248         Mutex::Autolock _l(mLock);
249         mForceSynchronous |= synchronous;
250         if (!mTransactionNestCount) {
251             ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
252                     "call to openGlobalTransaction().");
253         } else if (--mTransactionNestCount) {
254             return;
255         }
256
257         transaction = mComposerStates;
258         mComposerStates.clear();
259
260         displayTransaction = mDisplayStates;
261         mDisplayStates.clear();
262
263         if (mForceSynchronous) {
264             flags |= ISurfaceComposer::eSynchronous;
265         }
266         if (mAnimation) {
267             flags |= ISurfaceComposer::eAnimation;
268         }
269
270         mForceSynchronous = false;
271         mAnimation = false;
272     }
273
274    sm->setTransactionState(transaction, displayTransaction, flags);
275 }
276
277 status_t Composer::enableVSyncInjectionsImpl(bool enable) {
278     sp<ISurfaceComposer> sm(ComposerService::getComposerService());
279     return sm->enableVSyncInjections(enable);
280 }
281
282 status_t Composer::injectVSyncImpl(nsecs_t when) {
283     sp<ISurfaceComposer> sm(ComposerService::getComposerService());
284     return sm->injectVSync(when);
285 }
286
287 void Composer::setAnimationTransactionImpl() {
288     Mutex::Autolock _l(mLock);
289     mAnimation = true;
290 }
291
292 layer_state_t* Composer::getLayerStateLocked(
293         const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
294
295     ComposerState s;
296     s.client = client->mClient;
297     s.state.surface = id;
298
299     ssize_t index = mComposerStates.indexOf(s);
300     if (index < 0) {
301         // we don't have it, add an initialized layer_state to our list
302         index = mComposerStates.add(s);
303     }
304
305     ComposerState* const out = mComposerStates.editArray();
306     return &(out[index].state);
307 }
308
309 status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
310         const sp<IBinder>& id, float x, float y) {
311     Mutex::Autolock _l(mLock);
312     layer_state_t* s = getLayerStateLocked(client, id);
313     if (!s)
314         return BAD_INDEX;
315     s->what |= layer_state_t::ePositionChanged;
316     s->x = x;
317     s->y = y;
318     return NO_ERROR;
319 }
320
321 status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
322         const sp<IBinder>& id, uint32_t w, uint32_t h) {
323     Mutex::Autolock _l(mLock);
324     layer_state_t* s = getLayerStateLocked(client, id);
325     if (!s)
326         return BAD_INDEX;
327     s->what |= layer_state_t::eSizeChanged;
328     s->w = w;
329     s->h = h;
330
331     // Resizing a surface makes the transaction synchronous.
332     mForceSynchronous = true;
333
334     return NO_ERROR;
335 }
336
337 status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
338         const sp<IBinder>& id, int32_t z) {
339     Mutex::Autolock _l(mLock);
340     layer_state_t* s = getLayerStateLocked(client, id);
341     if (!s)
342         return BAD_INDEX;
343     s->what |= layer_state_t::eLayerChanged;
344     s->z = z;
345     return NO_ERROR;
346 }
347
348 status_t Composer::setRelativeLayer(const sp<SurfaceComposerClient>& client,
349         const sp<IBinder>& id, const sp<IBinder>& relativeTo,
350         int32_t z) {
351     Mutex::Autolock _l(mLock);
352     layer_state_t* s = getLayerStateLocked(client, id);
353     if (!s) {
354         return BAD_INDEX;
355     }
356     s->what |= layer_state_t::eRelativeLayerChanged;
357     s->relativeLayerHandle = relativeTo;
358     s->z = z;
359     return NO_ERROR;
360 }
361
362 status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
363         const sp<IBinder>& id, uint32_t flags,
364         uint32_t mask) {
365     Mutex::Autolock _l(mLock);
366     layer_state_t* s = getLayerStateLocked(client, id);
367     if (!s)
368         return BAD_INDEX;
369     if ((mask & layer_state_t::eLayerOpaque) ||
370             (mask & layer_state_t::eLayerHidden) ||
371             (mask & layer_state_t::eLayerSecure)) {
372         s->what |= layer_state_t::eFlagsChanged;
373     }
374     s->flags &= ~mask;
375     s->flags |= (flags & mask);
376     s->mask |= mask;
377     return NO_ERROR;
378 }
379
380 status_t Composer::setTransparentRegionHint(
381         const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
382         const Region& transparentRegion) {
383     Mutex::Autolock _l(mLock);
384     layer_state_t* s = getLayerStateLocked(client, id);
385     if (!s)
386         return BAD_INDEX;
387     s->what |= layer_state_t::eTransparentRegionChanged;
388     s->transparentRegion = transparentRegion;
389     return NO_ERROR;
390 }
391
392 status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
393         const sp<IBinder>& id, float alpha) {
394     Mutex::Autolock _l(mLock);
395     layer_state_t* s = getLayerStateLocked(client, id);
396     if (!s)
397         return BAD_INDEX;
398     s->what |= layer_state_t::eAlphaChanged;
399     s->alpha = alpha;
400     return NO_ERROR;
401 }
402
403 status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
404         const sp<IBinder>& id, uint32_t layerStack) {
405     Mutex::Autolock _l(mLock);
406     layer_state_t* s = getLayerStateLocked(client, id);
407     if (!s)
408         return BAD_INDEX;
409     s->what |= layer_state_t::eLayerStackChanged;
410     s->layerStack = layerStack;
411     return NO_ERROR;
412 }
413
414 status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
415         const sp<IBinder>& id, float dsdx, float dtdx,
416         float dtdy, float dsdy) {
417     Mutex::Autolock _l(mLock);
418     layer_state_t* s = getLayerStateLocked(client, id);
419     if (!s)
420         return BAD_INDEX;
421     s->what |= layer_state_t::eMatrixChanged;
422     layer_state_t::matrix22_t matrix;
423     matrix.dsdx = dsdx;
424     matrix.dtdx = dtdx;
425     matrix.dsdy = dsdy;
426     matrix.dtdy = dtdy;
427     s->matrix = matrix;
428     return NO_ERROR;
429 }
430
431 status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
432         const sp<IBinder>& id, const Rect& crop) {
433     Mutex::Autolock _l(mLock);
434     layer_state_t* s = getLayerStateLocked(client, id);
435     if (!s)
436         return BAD_INDEX;
437     s->what |= layer_state_t::eCropChanged;
438     s->crop = crop;
439     return NO_ERROR;
440 }
441
442 status_t Composer::setFinalCrop(const sp<SurfaceComposerClient>& client,
443         const sp<IBinder>& id, const Rect& crop) {
444     Mutex::Autolock _l(mLock);
445     layer_state_t* s = getLayerStateLocked(client, id);
446     if (!s) {
447         return BAD_INDEX;
448     }
449     s->what |= layer_state_t::eFinalCropChanged;
450     s->finalCrop = crop;
451     return NO_ERROR;
452 }
453
454 status_t Composer::deferTransactionUntil(
455         const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
456         const sp<IBinder>& handle, uint64_t frameNumber) {
457     Mutex::Autolock lock(mLock);
458     layer_state_t* s = getLayerStateLocked(client, id);
459     if (!s) {
460         return BAD_INDEX;
461     }
462     s->what |= layer_state_t::eDeferTransaction;
463     s->barrierHandle = handle;
464     s->frameNumber = frameNumber;
465     return NO_ERROR;
466 }
467
468 status_t Composer::deferTransactionUntil(
469         const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
470         const sp<Surface>& barrierSurface, uint64_t frameNumber) {
471     Mutex::Autolock lock(mLock);
472     layer_state_t* s = getLayerStateLocked(client, id);
473     if (!s) {
474         return BAD_INDEX;
475     }
476     s->what |= layer_state_t::eDeferTransaction;
477     s->barrierGbp = barrierSurface->getIGraphicBufferProducer();
478     s->frameNumber = frameNumber;
479     return NO_ERROR;
480 }
481
482 status_t Composer::reparentChildren(
483         const sp<SurfaceComposerClient>& client,
484         const sp<IBinder>& id,
485         const sp<IBinder>& newParentHandle) {
486     Mutex::Autolock lock(mLock);
487     layer_state_t* s = getLayerStateLocked(client, id);
488     if (!s) {
489         return BAD_INDEX;
490     }
491     s->what |= layer_state_t::eReparentChildren;
492     s->reparentHandle = newParentHandle;
493     return NO_ERROR;
494 }
495
496 status_t Composer::detachChildren(
497         const sp<SurfaceComposerClient>& client,
498         const sp<IBinder>& id) {
499     Mutex::Autolock lock(mLock);
500     layer_state_t* s = getLayerStateLocked(client, id);
501     if (!s) {
502         return BAD_INDEX;
503     }
504     s->what |= layer_state_t::eDetachChildren;
505     return NO_ERROR;
506 }
507
508 status_t Composer::setOverrideScalingMode(
509         const sp<SurfaceComposerClient>& client,
510         const sp<IBinder>& id, int32_t overrideScalingMode) {
511     Mutex::Autolock lock(mLock);
512     layer_state_t* s = getLayerStateLocked(client, id);
513     if (!s) {
514         return BAD_INDEX;
515     }
516
517     switch (overrideScalingMode) {
518         case NATIVE_WINDOW_SCALING_MODE_FREEZE:
519         case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
520         case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
521         case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
522         case -1:
523             break;
524         default:
525             ALOGE("unknown scaling mode: %d",
526                     overrideScalingMode);
527             return BAD_VALUE;
528     }
529
530     s->what |= layer_state_t::eOverrideScalingModeChanged;
531     s->overrideScalingMode = overrideScalingMode;
532     return NO_ERROR;
533 }
534
535 status_t Composer::setGeometryAppliesWithResize(
536         const sp<SurfaceComposerClient>& client,
537         const sp<IBinder>& id) {
538     Mutex::Autolock lock(mLock);
539     layer_state_t* s = getLayerStateLocked(client, id);
540     if (!s) {
541         return BAD_INDEX;
542     }
543     s->what |= layer_state_t::eGeometryAppliesWithResize;
544     return NO_ERROR;
545 }
546
547 // ---------------------------------------------------------------------------
548
549 DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
550     DisplayState s;
551     s.token = token;
552     ssize_t index = mDisplayStates.indexOf(s);
553     if (index < 0) {
554         // we don't have it, add an initialized layer_state to our list
555         s.what = 0;
556         index = mDisplayStates.add(s);
557     }
558     return mDisplayStates.editItemAt(static_cast<size_t>(index));
559 }
560
561 status_t Composer::setDisplaySurface(const sp<IBinder>& token,
562         sp<IGraphicBufferProducer> bufferProducer) {
563     if (bufferProducer.get() != nullptr) {
564         // Make sure that composition can never be stalled by a virtual display
565         // consumer that isn't processing buffers fast enough.
566         status_t err = bufferProducer->setAsyncMode(true);
567         if (err != NO_ERROR) {
568             ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
569                     "BufferQueue. This BufferQueue cannot be used for virtual "
570                     "display. (%d)", err);
571             return err;
572         }
573     }
574     Mutex::Autolock _l(mLock);
575     DisplayState& s(getDisplayStateLocked(token));
576     s.surface = bufferProducer;
577     s.what |= DisplayState::eSurfaceChanged;
578     return NO_ERROR;
579 }
580
581 void Composer::setDisplayLayerStack(const sp<IBinder>& token,
582         uint32_t layerStack) {
583     Mutex::Autolock _l(mLock);
584     DisplayState& s(getDisplayStateLocked(token));
585     s.layerStack = layerStack;
586     s.what |= DisplayState::eLayerStackChanged;
587 }
588
589 void Composer::setDisplayProjection(const sp<IBinder>& token,
590         uint32_t orientation,
591         const Rect& layerStackRect,
592         const Rect& displayRect) {
593     Mutex::Autolock _l(mLock);
594     DisplayState& s(getDisplayStateLocked(token));
595     s.orientation = orientation;
596     s.viewport = layerStackRect;
597     s.frame = displayRect;
598     s.what |= DisplayState::eDisplayProjectionChanged;
599     mForceSynchronous = true; // TODO: do we actually still need this?
600 }
601
602 void Composer::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
603     Mutex::Autolock _l(mLock);
604     DisplayState& s(getDisplayStateLocked(token));
605     s.width = width;
606     s.height = height;
607     s.what |= DisplayState::eDisplaySizeChanged;
608 }
609
610 // ---------------------------------------------------------------------------
611
612 SurfaceComposerClient::SurfaceComposerClient()
613     : mStatus(NO_INIT), mComposer(Composer::getInstance())
614 {
615 }
616
617 SurfaceComposerClient::SurfaceComposerClient(const sp<IGraphicBufferProducer>& root)
618     : mStatus(NO_INIT), mComposer(Composer::getInstance()), mParent(root)
619 {
620 }
621
622 void SurfaceComposerClient::onFirstRef() {
623     sp<ISurfaceComposer> sm(ComposerService::getComposerService());
624     if (sm != 0) {
625         auto rootProducer = mParent.promote();
626         sp<ISurfaceComposerClient> conn;
627         conn = (rootProducer != nullptr) ? sm->createScopedConnection(rootProducer) :
628                 sm->createConnection();
629         if (conn != 0) {
630             mClient = conn;
631             mStatus = NO_ERROR;
632         }
633     }
634 }
635
636 SurfaceComposerClient::~SurfaceComposerClient() {
637     dispose();
638 }
639
640 status_t SurfaceComposerClient::initCheck() const {
641     return mStatus;
642 }
643
644 sp<IBinder> SurfaceComposerClient::connection() const {
645     return IInterface::asBinder(mClient);
646 }
647
648 status_t SurfaceComposerClient::linkToComposerDeath(
649         const sp<IBinder::DeathRecipient>& recipient,
650         void* cookie, uint32_t flags) {
651     sp<ISurfaceComposer> sm(ComposerService::getComposerService());
652     return IInterface::asBinder(sm)->linkToDeath(recipient, cookie, flags);
653 }
654
655 void SurfaceComposerClient::dispose() {
656     // this can be called more than once.
657     sp<ISurfaceComposerClient> client;
658     Mutex::Autolock _lm(mLock);
659     if (mClient != 0) {
660         client = mClient; // hold ref while lock is held
661         mClient.clear();
662     }
663     mStatus = NO_INIT;
664 }
665
666 sp<SurfaceControl> SurfaceComposerClient::createSurface(
667         const String8& name,
668         uint32_t w,
669         uint32_t h,
670         PixelFormat format,
671         uint32_t flags,
672         SurfaceControl* parent,
673         uint32_t windowType,
674         uint32_t ownerUid)
675 {
676     sp<SurfaceControl> sur;
677     if (mStatus == NO_ERROR) {
678         sp<IBinder> handle;
679         sp<IBinder> parentHandle;
680         sp<IGraphicBufferProducer> gbp;
681
682         if (parent != nullptr) {
683             parentHandle = parent->getHandle();
684         }
685         status_t err = mClient->createSurface(name, w, h, format, flags, parentHandle,
686                 windowType, ownerUid, &handle, &gbp);
687         ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
688         if (err == NO_ERROR) {
689             sur = new SurfaceControl(this, handle, gbp);
690         }
691     }
692     return sur;
693 }
694
695 sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
696         bool secure) {
697     return Composer::getInstance().createDisplay(displayName, secure);
698 }
699
700 void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
701     Composer::getInstance().destroyDisplay(display);
702 }
703
704 sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
705     return Composer::getInstance().getBuiltInDisplay(id);
706 }
707
708 status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
709     if (mStatus != NO_ERROR)
710         return mStatus;
711     status_t err = mClient->destroySurface(sid);
712     return err;
713 }
714
715 status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
716     if (mStatus != NO_ERROR) {
717         return mStatus;
718     }
719     return mClient->clearLayerFrameStats(token);
720 }
721
722 status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
723         FrameStats* outStats) const {
724     if (mStatus != NO_ERROR) {
725         return mStatus;
726     }
727     return mClient->getLayerFrameStats(token, outStats);
728 }
729
730 inline Composer& SurfaceComposerClient::getComposer() {
731     return mComposer;
732 }
733
734 // ----------------------------------------------------------------------------
735
736 void SurfaceComposerClient::openGlobalTransaction() {
737     Composer::openGlobalTransaction();
738 }
739
740 void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
741     Composer::closeGlobalTransaction(synchronous);
742 }
743
744 void SurfaceComposerClient::setAnimationTransaction() {
745     Composer::setAnimationTransaction();
746 }
747
748 status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
749     return Composer::enableVSyncInjections(enable);
750 }
751
752 status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
753     return Composer::injectVSync(when);
754 }
755
756 // ----------------------------------------------------------------------------
757
758 status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
759     return getComposer().setCrop(this, id, crop);
760 }
761
762 status_t SurfaceComposerClient::setFinalCrop(const sp<IBinder>& id,
763         const Rect& crop) {
764     return getComposer().setFinalCrop(this, id, crop);
765 }
766
767 status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
768     return getComposer().setPosition(this, id, x, y);
769 }
770
771 status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
772     return getComposer().setSize(this, id, w, h);
773 }
774
775 status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) {
776     return getComposer().setLayer(this, id, z);
777 }
778
779 status_t SurfaceComposerClient::setRelativeLayer(const sp<IBinder>& id,
780         const sp<IBinder>& relativeTo, int32_t z) {
781     return getComposer().setRelativeLayer(this, id, relativeTo, z);
782 }
783
784 status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
785     return getComposer().setFlags(this, id,
786             layer_state_t::eLayerHidden,
787             layer_state_t::eLayerHidden);
788 }
789
790 status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
791     return getComposer().setFlags(this, id,
792             0,
793             layer_state_t::eLayerHidden);
794 }
795
796 status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
797         uint32_t mask) {
798     return getComposer().setFlags(this, id, flags, mask);
799 }
800
801 status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
802         const Region& transparentRegion) {
803     return getComposer().setTransparentRegionHint(this, id, transparentRegion);
804 }
805
806 status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
807     return getComposer().setAlpha(this, id, alpha);
808 }
809
810 status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
811     return getComposer().setLayerStack(this, id, layerStack);
812 }
813
814 status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
815         float dtdy, float dsdy) {
816     return getComposer().setMatrix(this, id, dsdx, dtdx, dtdy, dsdy);
817 }
818
819 status_t SurfaceComposerClient::deferTransactionUntil(const sp<IBinder>& id,
820         const sp<IBinder>& handle, uint64_t frameNumber) {
821     return getComposer().deferTransactionUntil(this, id, handle, frameNumber);
822 }
823
824 status_t SurfaceComposerClient::deferTransactionUntil(const sp<IBinder>& id,
825         const sp<Surface>& barrierSurface, uint64_t frameNumber) {
826     return getComposer().deferTransactionUntil(this, id, barrierSurface, frameNumber);
827 }
828
829 status_t SurfaceComposerClient::reparentChildren(const sp<IBinder>& id,
830         const sp<IBinder>& newParentHandle) {
831     return getComposer().reparentChildren(this, id, newParentHandle);
832 }
833
834 status_t SurfaceComposerClient::detachChildren(const sp<IBinder>& id) {
835     return getComposer().detachChildren(this, id);
836 }
837
838 status_t SurfaceComposerClient::setOverrideScalingMode(
839         const sp<IBinder>& id, int32_t overrideScalingMode) {
840     return getComposer().setOverrideScalingMode(
841             this, id, overrideScalingMode);
842 }
843
844 status_t SurfaceComposerClient::setGeometryAppliesWithResize(
845         const sp<IBinder>& id) {
846     return getComposer().setGeometryAppliesWithResize(this, id);
847 }
848
849 // ----------------------------------------------------------------------------
850
851 status_t SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
852         sp<IGraphicBufferProducer> bufferProducer) {
853     return Composer::getInstance().setDisplaySurface(token, bufferProducer);
854 }
855
856 void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
857         uint32_t layerStack) {
858     Composer::getInstance().setDisplayLayerStack(token, layerStack);
859 }
860
861 void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
862         uint32_t orientation,
863         const Rect& layerStackRect,
864         const Rect& displayRect) {
865     Composer::getInstance().setDisplayProjection(token, orientation,
866             layerStackRect, displayRect);
867 }
868
869 void SurfaceComposerClient::setDisplaySize(const sp<IBinder>& token,
870         uint32_t width, uint32_t height) {
871     Composer::getInstance().setDisplaySize(token, width, height);
872 }
873
874 // ----------------------------------------------------------------------------
875
876 status_t SurfaceComposerClient::getDisplayConfigs(
877         const sp<IBinder>& display, Vector<DisplayInfo>* configs)
878 {
879     return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
880 }
881
882 status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
883         DisplayInfo* info) {
884     Vector<DisplayInfo> configs;
885     status_t result = getDisplayConfigs(display, &configs);
886     if (result != NO_ERROR) {
887         return result;
888     }
889
890     int activeId = getActiveConfig(display);
891     if (activeId < 0) {
892         ALOGE("No active configuration found");
893         return NAME_NOT_FOUND;
894     }
895
896     *info = configs[static_cast<size_t>(activeId)];
897     return NO_ERROR;
898 }
899
900 int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
901     return ComposerService::getComposerService()->getActiveConfig(display);
902 }
903
904 status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
905     return ComposerService::getComposerService()->setActiveConfig(display, id);
906 }
907
908 status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
909         Vector<android_color_mode_t>* outColorModes) {
910     return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
911 }
912
913 android_color_mode_t SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
914     return ComposerService::getComposerService()->getActiveColorMode(display);
915 }
916
917 status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
918         android_color_mode_t colorMode) {
919     return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
920 }
921
922 void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
923         int mode) {
924     ComposerService::getComposerService()->setPowerMode(token, mode);
925 }
926
927 status_t SurfaceComposerClient::clearAnimationFrameStats() {
928     return ComposerService::getComposerService()->clearAnimationFrameStats();
929 }
930
931 status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
932     return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
933 }
934
935 status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
936         HdrCapabilities* outCapabilities) {
937     return ComposerService::getComposerService()->getHdrCapabilities(display,
938             outCapabilities);
939 }
940
941 // ----------------------------------------------------------------------------
942
943 status_t ScreenshotClient::capture(
944         const sp<IBinder>& display,
945         const sp<IGraphicBufferProducer>& producer,
946         Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
947         int32_t minLayerZ, int32_t maxLayerZ, bool useIdentityTransform) {
948     sp<ISurfaceComposer> s(ComposerService::getComposerService());
949     if (s == NULL) return NO_INIT;
950     return s->captureScreen(display, producer, sourceCrop,
951             reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
952 }
953
954 status_t ScreenshotClient::captureToBuffer(const sp<IBinder>& display,
955         Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
956         int32_t minLayerZ, int32_t maxLayerZ, bool useIdentityTransform,
957         uint32_t rotation,
958         sp<GraphicBuffer>* outBuffer) {
959     sp<ISurfaceComposer> s(ComposerService::getComposerService());
960     if (s == NULL) return NO_INIT;
961
962     sp<IGraphicBufferConsumer> gbpConsumer;
963     sp<IGraphicBufferProducer> producer;
964     BufferQueue::createBufferQueue(&producer, &gbpConsumer);
965     sp<BufferItemConsumer> consumer(new BufferItemConsumer(gbpConsumer,
966            GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_NEVER,
967            1, true));
968
969     status_t ret = s->captureScreen(display, producer, sourceCrop, reqWidth, reqHeight,
970             minLayerZ, maxLayerZ, useIdentityTransform,
971             static_cast<ISurfaceComposer::Rotation>(rotation));
972     if (ret != NO_ERROR) {
973         return ret;
974     }
975     BufferItem b;
976     consumer->acquireBuffer(&b, 0, true);
977     *outBuffer = b.mGraphicBuffer;
978     return ret;
979 }
980
981 ScreenshotClient::ScreenshotClient()
982     : mHaveBuffer(false) {
983     memset(&mBuffer, 0, sizeof(mBuffer));
984 }
985
986 ScreenshotClient::~ScreenshotClient() {
987     ScreenshotClient::release();
988 }
989
990 sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
991     if (mCpuConsumer == NULL) {
992         sp<IGraphicBufferConsumer> consumer;
993         BufferQueue::createBufferQueue(&mProducer, &consumer);
994         mCpuConsumer = new CpuConsumer(consumer, 1);
995         mCpuConsumer->setName(String8("ScreenshotClient"));
996     }
997     return mCpuConsumer;
998 }
999
1000 status_t ScreenshotClient::update(const sp<IBinder>& display,
1001         Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
1002         int32_t minLayerZ, int32_t maxLayerZ,
1003         bool useIdentityTransform, uint32_t rotation) {
1004     sp<ISurfaceComposer> s(ComposerService::getComposerService());
1005     if (s == NULL) return NO_INIT;
1006     sp<CpuConsumer> cpuConsumer = getCpuConsumer();
1007
1008     if (mHaveBuffer) {
1009         mCpuConsumer->unlockBuffer(mBuffer);
1010         memset(&mBuffer, 0, sizeof(mBuffer));
1011         mHaveBuffer = false;
1012     }
1013
1014     status_t err = s->captureScreen(display, mProducer, sourceCrop,
1015             reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform,
1016             static_cast<ISurfaceComposer::Rotation>(rotation));
1017
1018     if (err == NO_ERROR) {
1019         err = mCpuConsumer->lockNextBuffer(&mBuffer);
1020         if (err == NO_ERROR) {
1021             mHaveBuffer = true;
1022         }
1023     }
1024     return err;
1025 }
1026
1027 status_t ScreenshotClient::update(const sp<IBinder>& display,
1028         Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
1029         int32_t minLayerZ, int32_t maxLayerZ,
1030         bool useIdentityTransform) {
1031
1032     return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
1033             minLayerZ, maxLayerZ, useIdentityTransform, ISurfaceComposer::eRotateNone);
1034 }
1035
1036 status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
1037         bool useIdentityTransform) {
1038     return ScreenshotClient::update(display, sourceCrop, 0, 0,
1039             INT32_MIN, INT32_MAX,
1040             useIdentityTransform, ISurfaceComposer::eRotateNone);
1041 }
1042
1043 status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
1044         uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
1045     return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
1046             INT32_MIN, INT32_MAX,
1047             useIdentityTransform, ISurfaceComposer::eRotateNone);
1048 }
1049
1050 void ScreenshotClient::release() {
1051     if (mHaveBuffer) {
1052         mCpuConsumer->unlockBuffer(mBuffer);
1053         memset(&mBuffer, 0, sizeof(mBuffer));
1054         mHaveBuffer = false;
1055     }
1056     mCpuConsumer.clear();
1057 }
1058
1059 void const* ScreenshotClient::getPixels() const {
1060     return mBuffer.data;
1061 }
1062
1063 uint32_t ScreenshotClient::getWidth() const {
1064     return mBuffer.width;
1065 }
1066
1067 uint32_t ScreenshotClient::getHeight() const {
1068     return mBuffer.height;
1069 }
1070
1071 PixelFormat ScreenshotClient::getFormat() const {
1072     return mBuffer.format;
1073 }
1074
1075 uint32_t ScreenshotClient::getStride() const {
1076     return mBuffer.stride;
1077 }
1078
1079 size_t ScreenshotClient::getSize() const {
1080     return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
1081 }
1082
1083 android_dataspace ScreenshotClient::getDataSpace() const {
1084     return mBuffer.dataSpace;
1085 }
1086
1087 // ----------------------------------------------------------------------------
1088 }; // namespace android