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