OSDN Git Service

8586ed21847167020ac4dd19555d28bd2480af35
[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/IMemory.h>
30 #include <binder/IServiceManager.h>
31
32 #include <ui/DisplayInfo.h>
33
34 #include <gui/ISurface.h>
35 #include <gui/ISurfaceComposer.h>
36 #include <gui/ISurfaceComposerClient.h>
37 #include <gui/SurfaceComposerClient.h>
38
39 #include <private/gui/ComposerService.h>
40 #include <private/gui/LayerState.h>
41
42 namespace android {
43 // ---------------------------------------------------------------------------
44
45 ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
46
47 ComposerService::ComposerService()
48 : Singleton<ComposerService>() {
49     Mutex::Autolock _l(mLock);
50     connectLocked();
51 }
52
53 void ComposerService::connectLocked() {
54     const String16 name("SurfaceFlinger");
55     while (getService(name, &mComposerService) != NO_ERROR) {
56         usleep(250000);
57     }
58     assert(mComposerService != NULL);
59
60     // Create the death listener.
61     class DeathObserver : public IBinder::DeathRecipient {
62         ComposerService& mComposerService;
63         virtual void binderDied(const wp<IBinder>& who) {
64             ALOGW("ComposerService remote (surfaceflinger) died [%p]",
65                   who.unsafe_get());
66             mComposerService.composerServiceDied();
67         }
68      public:
69         DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
70     };
71
72     mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
73     mComposerService->asBinder()->linkToDeath(mDeathObserver);
74 }
75
76 /*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
77     ComposerService& instance = ComposerService::getInstance();
78     Mutex::Autolock _l(instance.mLock);
79     if (instance.mComposerService == NULL) {
80         ComposerService::getInstance().connectLocked();
81         assert(instance.mComposerService != NULL);
82         ALOGD("ComposerService reconnected");
83     }
84     return instance.mComposerService;
85 }
86
87 void ComposerService::composerServiceDied()
88 {
89     Mutex::Autolock _l(mLock);
90     mComposerService = NULL;
91     mDeathObserver = NULL;
92 }
93
94 // ---------------------------------------------------------------------------
95
96 static inline
97 int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
98     if (lhs.client < rhs.client)  return -1;
99     if (lhs.client > rhs.client)  return 1;
100     if (lhs.state.surface < rhs.state.surface)  return -1;
101     if (lhs.state.surface > rhs.state.surface)  return 1;
102     return 0;
103 }
104
105 static inline
106 int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
107     return compare_type(lhs.token, rhs.token);
108 }
109
110 class Composer : public Singleton<Composer>
111 {
112     friend class Singleton<Composer>;
113
114     mutable Mutex               mLock;
115     SortedVector<ComposerState> mComposerStates;
116     SortedVector<DisplayState > mDisplayStates;
117     uint32_t                    mForceSynchronous;
118     bool                        mAnimation;
119
120     Composer() : Singleton<Composer>(),
121         mForceSynchronous(0),
122         mAnimation(false)
123     { }
124
125     void closeGlobalTransactionImpl(bool synchronous);
126     void setAnimationTransactionImpl();
127
128     layer_state_t* getLayerStateLocked(
129             const sp<SurfaceComposerClient>& client, SurfaceID id);
130
131     DisplayState& getDisplayStateLocked(const sp<IBinder>& token);
132
133 public:
134     sp<IBinder> createDisplay(const String8& displayName);
135     sp<IBinder> getBuiltInDisplay(int32_t id);
136
137     status_t setPosition(const sp<SurfaceComposerClient>& client, SurfaceID id,
138             float x, float y);
139     status_t setSize(const sp<SurfaceComposerClient>& client, SurfaceID id,
140             uint32_t w, uint32_t h);
141     status_t setLayer(const sp<SurfaceComposerClient>& client, SurfaceID id,
142             int32_t z);
143     status_t setFlags(const sp<SurfaceComposerClient>& client, SurfaceID id,
144             uint32_t flags, uint32_t mask);
145     status_t setTransparentRegionHint(
146             const sp<SurfaceComposerClient>& client, SurfaceID id,
147             const Region& transparentRegion);
148     status_t setAlpha(const sp<SurfaceComposerClient>& client, SurfaceID id,
149             float alpha);
150     status_t setMatrix(const sp<SurfaceComposerClient>& client, SurfaceID id,
151             float dsdx, float dtdx, float dsdy, float dtdy);
152     status_t setOrientation(int orientation);
153     status_t setCrop(const sp<SurfaceComposerClient>& client, SurfaceID id,
154             const Rect& crop);
155     status_t setLayerStack(const sp<SurfaceComposerClient>& client,
156             SurfaceID id, uint32_t layerStack);
157
158     void setDisplaySurface(const sp<IBinder>& token, const sp<ISurfaceTexture>& surface);
159     void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
160     void setDisplayProjection(const sp<IBinder>& token,
161             uint32_t orientation,
162             const Rect& layerStackRect,
163             const Rect& displayRect);
164
165     static void setAnimationTransaction() {
166         Composer::getInstance().setAnimationTransactionImpl();
167     }
168
169     static void closeGlobalTransaction(bool synchronous) {
170         Composer::getInstance().closeGlobalTransactionImpl(synchronous);
171     }
172 };
173
174 ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
175
176 // ---------------------------------------------------------------------------
177
178 sp<IBinder> Composer::createDisplay(const String8& displayName) {
179     return ComposerService::getComposerService()->createDisplay(displayName);
180 }
181
182 sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
183     return ComposerService::getComposerService()->getBuiltInDisplay(id);
184 }
185
186 void Composer::closeGlobalTransactionImpl(bool synchronous) {
187     sp<ISurfaceComposer> sm(ComposerService::getComposerService());
188
189     Vector<ComposerState> transaction;
190     Vector<DisplayState> displayTransaction;
191     uint32_t flags = 0;
192
193     { // scope for the lock
194         Mutex::Autolock _l(mLock);
195         transaction = mComposerStates;
196         mComposerStates.clear();
197
198         displayTransaction = mDisplayStates;
199         mDisplayStates.clear();
200
201         if (synchronous || mForceSynchronous) {
202             flags |= ISurfaceComposer::eSynchronous;
203         }
204         if (mAnimation) {
205             flags |= ISurfaceComposer::eAnimation;
206         }
207
208         mForceSynchronous = false;
209         mAnimation = false;
210     }
211
212    sm->setTransactionState(transaction, displayTransaction, flags);
213 }
214
215 void Composer::setAnimationTransactionImpl() {
216     Mutex::Autolock _l(mLock);
217     mAnimation = true;
218 }
219
220 layer_state_t* Composer::getLayerStateLocked(
221         const sp<SurfaceComposerClient>& client, SurfaceID id) {
222
223     ComposerState s;
224     s.client = client->mClient;
225     s.state.surface = id;
226
227     ssize_t index = mComposerStates.indexOf(s);
228     if (index < 0) {
229         // we don't have it, add an initialized layer_state to our list
230         index = mComposerStates.add(s);
231     }
232
233     ComposerState* const out = mComposerStates.editArray();
234     return &(out[index].state);
235 }
236
237 status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
238         SurfaceID id, float x, float y) {
239     Mutex::Autolock _l(mLock);
240     layer_state_t* s = getLayerStateLocked(client, id);
241     if (!s)
242         return BAD_INDEX;
243     s->what |= layer_state_t::ePositionChanged;
244     s->x = x;
245     s->y = y;
246     return NO_ERROR;
247 }
248
249 status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
250         SurfaceID id, uint32_t w, uint32_t h) {
251     Mutex::Autolock _l(mLock);
252     layer_state_t* s = getLayerStateLocked(client, id);
253     if (!s)
254         return BAD_INDEX;
255     s->what |= layer_state_t::eSizeChanged;
256     s->w = w;
257     s->h = h;
258
259     // Resizing a surface makes the transaction synchronous.
260     mForceSynchronous = true;
261
262     return NO_ERROR;
263 }
264
265 status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
266         SurfaceID id, int32_t z) {
267     Mutex::Autolock _l(mLock);
268     layer_state_t* s = getLayerStateLocked(client, id);
269     if (!s)
270         return BAD_INDEX;
271     s->what |= layer_state_t::eLayerChanged;
272     s->z = z;
273     return NO_ERROR;
274 }
275
276 status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
277         SurfaceID id, uint32_t flags,
278         uint32_t mask) {
279     Mutex::Autolock _l(mLock);
280     layer_state_t* s = getLayerStateLocked(client, id);
281     if (!s)
282         return BAD_INDEX;
283     s->what |= layer_state_t::eVisibilityChanged;
284     s->flags &= ~mask;
285     s->flags |= (flags & mask);
286     s->mask |= mask;
287     return NO_ERROR;
288 }
289
290 status_t Composer::setTransparentRegionHint(
291         const sp<SurfaceComposerClient>& client, SurfaceID id,
292         const Region& transparentRegion) {
293     Mutex::Autolock _l(mLock);
294     layer_state_t* s = getLayerStateLocked(client, id);
295     if (!s)
296         return BAD_INDEX;
297     s->what |= layer_state_t::eTransparentRegionChanged;
298     s->transparentRegion = transparentRegion;
299     return NO_ERROR;
300 }
301
302 status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
303         SurfaceID id, float alpha) {
304     Mutex::Autolock _l(mLock);
305     layer_state_t* s = getLayerStateLocked(client, id);
306     if (!s)
307         return BAD_INDEX;
308     s->what |= layer_state_t::eAlphaChanged;
309     s->alpha = alpha;
310     return NO_ERROR;
311 }
312
313 status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
314         SurfaceID id, uint32_t layerStack) {
315     Mutex::Autolock _l(mLock);
316     layer_state_t* s = getLayerStateLocked(client, id);
317     if (!s)
318         return BAD_INDEX;
319     s->what |= layer_state_t::eLayerStackChanged;
320     s->layerStack = layerStack;
321     return NO_ERROR;
322 }
323
324 status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
325         SurfaceID id, float dsdx, float dtdx,
326         float dsdy, float dtdy) {
327     Mutex::Autolock _l(mLock);
328     layer_state_t* s = getLayerStateLocked(client, id);
329     if (!s)
330         return BAD_INDEX;
331     s->what |= layer_state_t::eMatrixChanged;
332     layer_state_t::matrix22_t matrix;
333     matrix.dsdx = dsdx;
334     matrix.dtdx = dtdx;
335     matrix.dsdy = dsdy;
336     matrix.dtdy = dtdy;
337     s->matrix = matrix;
338     return NO_ERROR;
339 }
340
341 status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
342         SurfaceID id, const Rect& crop) {
343     Mutex::Autolock _l(mLock);
344     layer_state_t* s = getLayerStateLocked(client, id);
345     if (!s)
346         return BAD_INDEX;
347     s->what |= layer_state_t::eCropChanged;
348     s->crop = crop;
349     return NO_ERROR;
350 }
351
352 // ---------------------------------------------------------------------------
353
354 DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
355     DisplayState s;
356     s.token = token;
357     ssize_t index = mDisplayStates.indexOf(s);
358     if (index < 0) {
359         // we don't have it, add an initialized layer_state to our list
360         s.what = 0;
361         index = mDisplayStates.add(s);
362     }
363     return mDisplayStates.editItemAt(index);
364 }
365
366 void Composer::setDisplaySurface(const sp<IBinder>& token,
367         const sp<ISurfaceTexture>& surface) {
368     Mutex::Autolock _l(mLock);
369     DisplayState& s(getDisplayStateLocked(token));
370     s.surface = surface;
371     s.what |= DisplayState::eSurfaceChanged;
372 }
373
374 void Composer::setDisplayLayerStack(const sp<IBinder>& token,
375         uint32_t layerStack) {
376     Mutex::Autolock _l(mLock);
377     DisplayState& s(getDisplayStateLocked(token));
378     s.layerStack = layerStack;
379     s.what |= DisplayState::eLayerStackChanged;
380 }
381
382 void Composer::setDisplayProjection(const sp<IBinder>& token,
383         uint32_t orientation,
384         const Rect& layerStackRect,
385         const Rect& displayRect) {
386     Mutex::Autolock _l(mLock);
387     DisplayState& s(getDisplayStateLocked(token));
388     s.orientation = orientation;
389     s.viewport = layerStackRect;
390     s.frame = displayRect;
391     s.what |= DisplayState::eDisplayProjectionChanged;
392     mForceSynchronous = true; // TODO: do we actually still need this?
393 }
394
395 // ---------------------------------------------------------------------------
396
397 SurfaceComposerClient::SurfaceComposerClient()
398     : mStatus(NO_INIT), mComposer(Composer::getInstance())
399 {
400 }
401
402 void SurfaceComposerClient::onFirstRef() {
403     sp<ISurfaceComposer> sm(ComposerService::getComposerService());
404     if (sm != 0) {
405         sp<ISurfaceComposerClient> conn = sm->createConnection();
406         if (conn != 0) {
407             mClient = conn;
408             mStatus = NO_ERROR;
409         }
410     }
411 }
412
413 SurfaceComposerClient::~SurfaceComposerClient() {
414     dispose();
415 }
416
417 status_t SurfaceComposerClient::initCheck() const {
418     return mStatus;
419 }
420
421 sp<IBinder> SurfaceComposerClient::connection() const {
422     return (mClient != 0) ? mClient->asBinder() : 0;
423 }
424
425 status_t SurfaceComposerClient::linkToComposerDeath(
426         const sp<IBinder::DeathRecipient>& recipient,
427         void* cookie, uint32_t flags) {
428     sp<ISurfaceComposer> sm(ComposerService::getComposerService());
429     return sm->asBinder()->linkToDeath(recipient, cookie, flags);
430 }
431
432 void SurfaceComposerClient::dispose() {
433     // this can be called more than once.
434     sp<ISurfaceComposerClient> client;
435     Mutex::Autolock _lm(mLock);
436     if (mClient != 0) {
437         client = mClient; // hold ref while lock is held
438         mClient.clear();
439     }
440     mStatus = NO_INIT;
441 }
442
443 sp<SurfaceControl> SurfaceComposerClient::createSurface(
444         const String8& name,
445         uint32_t w,
446         uint32_t h,
447         PixelFormat format,
448         uint32_t flags)
449 {
450     sp<SurfaceControl> result;
451     if (mStatus == NO_ERROR) {
452         ISurfaceComposerClient::surface_data_t data;
453         sp<ISurface> surface = mClient->createSurface(&data, name,
454                 w, h, format, flags);
455         if (surface != 0) {
456             result = new SurfaceControl(this, surface, data);
457         }
458     }
459     return result;
460 }
461
462 sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName) {
463     return Composer::getInstance().createDisplay(displayName);
464 }
465
466 sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
467     return Composer::getInstance().getBuiltInDisplay(id);
468 }
469
470 status_t SurfaceComposerClient::destroySurface(SurfaceID sid) {
471     if (mStatus != NO_ERROR)
472         return mStatus;
473     status_t err = mClient->destroySurface(sid);
474     return err;
475 }
476
477 inline Composer& SurfaceComposerClient::getComposer() {
478     return mComposer;
479 }
480
481 // ----------------------------------------------------------------------------
482
483 void SurfaceComposerClient::openGlobalTransaction() {
484     // Currently a no-op
485 }
486
487 void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
488     Composer::closeGlobalTransaction(synchronous);
489 }
490
491 void SurfaceComposerClient::setAnimationTransaction() {
492     Composer::setAnimationTransaction();
493 }
494
495 // ----------------------------------------------------------------------------
496
497 status_t SurfaceComposerClient::setCrop(SurfaceID id, const Rect& crop) {
498     return getComposer().setCrop(this, id, crop);
499 }
500
501 status_t SurfaceComposerClient::setPosition(SurfaceID id, float x, float y) {
502     return getComposer().setPosition(this, id, x, y);
503 }
504
505 status_t SurfaceComposerClient::setSize(SurfaceID id, uint32_t w, uint32_t h) {
506     return getComposer().setSize(this, id, w, h);
507 }
508
509 status_t SurfaceComposerClient::setLayer(SurfaceID id, int32_t z) {
510     return getComposer().setLayer(this, id, z);
511 }
512
513 status_t SurfaceComposerClient::hide(SurfaceID id) {
514     return getComposer().setFlags(this, id,
515             layer_state_t::eLayerHidden,
516             layer_state_t::eLayerHidden);
517 }
518
519 status_t SurfaceComposerClient::show(SurfaceID id) {
520     return getComposer().setFlags(this, id,
521             0,
522             layer_state_t::eLayerHidden);
523 }
524
525 status_t SurfaceComposerClient::setFlags(SurfaceID id, uint32_t flags,
526         uint32_t mask) {
527     return getComposer().setFlags(this, id, flags, mask);
528 }
529
530 status_t SurfaceComposerClient::setTransparentRegionHint(SurfaceID id,
531         const Region& transparentRegion) {
532     return getComposer().setTransparentRegionHint(this, id, transparentRegion);
533 }
534
535 status_t SurfaceComposerClient::setAlpha(SurfaceID id, float alpha) {
536     return getComposer().setAlpha(this, id, alpha);
537 }
538
539 status_t SurfaceComposerClient::setLayerStack(SurfaceID id, uint32_t layerStack) {
540     return getComposer().setLayerStack(this, id, layerStack);
541 }
542
543 status_t SurfaceComposerClient::setMatrix(SurfaceID id, float dsdx, float dtdx,
544         float dsdy, float dtdy) {
545     return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
546 }
547
548 // ----------------------------------------------------------------------------
549
550 void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
551         const sp<ISurfaceTexture>& surface) {
552     Composer::getInstance().setDisplaySurface(token, surface);
553 }
554
555 void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
556         uint32_t layerStack) {
557     Composer::getInstance().setDisplayLayerStack(token, layerStack);
558 }
559
560 void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
561         uint32_t orientation,
562         const Rect& layerStackRect,
563         const Rect& displayRect) {
564     Composer::getInstance().setDisplayProjection(token, orientation,
565             layerStackRect, displayRect);
566 }
567
568 // ----------------------------------------------------------------------------
569
570 status_t SurfaceComposerClient::getDisplayInfo(
571         const sp<IBinder>& display, DisplayInfo* info)
572 {
573     return ComposerService::getComposerService()->getDisplayInfo(display, info);
574 }
575
576 void SurfaceComposerClient::blankDisplay(const sp<IBinder>& token) {
577     ComposerService::getComposerService()->blank(token);
578 }
579
580 void SurfaceComposerClient::unblankDisplay(const sp<IBinder>& token) {
581     ComposerService::getComposerService()->unblank(token);
582 }
583
584 // ----------------------------------------------------------------------------
585
586 ScreenshotClient::ScreenshotClient()
587     : mWidth(0), mHeight(0), mFormat(PIXEL_FORMAT_NONE) {
588 }
589
590 status_t ScreenshotClient::update(const sp<IBinder>& display) {
591     sp<ISurfaceComposer> s(ComposerService::getComposerService());
592     if (s == NULL) return NO_INIT;
593     mHeap = 0;
594     return s->captureScreen(display, &mHeap,
595             &mWidth, &mHeight, &mFormat, 0, 0,
596             0, -1UL);
597 }
598
599 status_t ScreenshotClient::update(const sp<IBinder>& display,
600         uint32_t reqWidth, uint32_t reqHeight) {
601     sp<ISurfaceComposer> s(ComposerService::getComposerService());
602     if (s == NULL) return NO_INIT;
603     mHeap = 0;
604     return s->captureScreen(display, &mHeap,
605             &mWidth, &mHeight, &mFormat, reqWidth, reqHeight,
606             0, -1UL);
607 }
608
609 status_t ScreenshotClient::update(const sp<IBinder>& display,
610         uint32_t reqWidth, uint32_t reqHeight,
611         uint32_t minLayerZ, uint32_t maxLayerZ) {
612     sp<ISurfaceComposer> s(ComposerService::getComposerService());
613     if (s == NULL) return NO_INIT;
614     mHeap = 0;
615     return s->captureScreen(display, &mHeap,
616             &mWidth, &mHeight, &mFormat, reqWidth, reqHeight,
617             minLayerZ, maxLayerZ);
618 }
619
620 void ScreenshotClient::release() {
621     mHeap = 0;
622 }
623
624 void const* ScreenshotClient::getPixels() const {
625     return mHeap->getBase();
626 }
627
628 uint32_t ScreenshotClient::getWidth() const {
629     return mWidth;
630 }
631
632 uint32_t ScreenshotClient::getHeight() const {
633     return mHeight;
634 }
635
636 PixelFormat ScreenshotClient::getFormat() const {
637     return mFormat;
638 }
639
640 uint32_t ScreenshotClient::getStride() const {
641     return mWidth;
642 }
643
644 size_t ScreenshotClient::getSize() const {
645     return mHeap->getSize();
646 }
647
648 // ----------------------------------------------------------------------------
649 }; // namespace android