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