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