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