Client.cpp revision 9d4e3d2f42e93e2d12bacabe97d307d30c3c20dd
1/*
2 * Copyright (C) 2012 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#include <stdint.h>
18#include <sys/types.h>
19
20#include <binder/PermissionCache.h>
21
22#include <private/android_filesystem_config.h>
23
24#include "Client.h"
25#include "Layer.h"
26#include "LayerBase.h"
27#include "SurfaceFlinger.h"
28
29namespace android {
30
31// ---------------------------------------------------------------------------
32
33const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
34
35// ---------------------------------------------------------------------------
36
37Client::Client(const sp<SurfaceFlinger>& flinger)
38    : mFlinger(flinger), mNameGenerator(1)
39{
40}
41
42Client::~Client()
43{
44    const size_t count = mLayers.size();
45    for (size_t i=0 ; i<count ; i++) {
46        sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
47        if (layer != 0) {
48            mFlinger->removeLayer(layer);
49        }
50    }
51}
52
53status_t Client::initCheck() const {
54    return NO_ERROR;
55}
56
57size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
58{
59    Mutex::Autolock _l(mLock);
60    size_t name = mNameGenerator++;
61    mLayers.add(name, layer);
62    return name;
63}
64
65void Client::detachLayer(const LayerBaseClient* layer)
66{
67    Mutex::Autolock _l(mLock);
68    // we do a linear search here, because this doesn't happen often
69    const size_t count = mLayers.size();
70    for (size_t i=0 ; i<count ; i++) {
71        if (mLayers.valueAt(i) == layer) {
72            mLayers.removeItemsAt(i, 1);
73            break;
74        }
75    }
76}
77sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
78{
79    Mutex::Autolock _l(mLock);
80    sp<LayerBaseClient> lbc;
81    wp<LayerBaseClient> layer(mLayers.valueFor(i));
82    if (layer != 0) {
83        lbc = layer.promote();
84        ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
85    }
86    return lbc;
87}
88
89
90status_t Client::onTransact(
91    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
92{
93    // these must be checked
94     IPCThreadState* ipc = IPCThreadState::self();
95     const int pid = ipc->getCallingPid();
96     const int uid = ipc->getCallingUid();
97     const int self_pid = getpid();
98     if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
99         // we're called from a different process, do the real check
100         if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
101         {
102             ALOGE("Permission Denial: "
103                     "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
104             return PERMISSION_DENIED;
105         }
106     }
107     return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
108}
109
110
111sp<ISurface> Client::createSurface(
112        ISurfaceComposerClient::surface_data_t* params,
113        const String8& name,
114        uint32_t w, uint32_t h, PixelFormat format,
115        uint32_t flags)
116{
117    /*
118     * createSurface must be called from the GL thread so that it can
119     * have access to the GL context.
120     */
121
122    class MessageCreateLayer : public MessageBase {
123        sp<ISurface> result;
124        SurfaceFlinger* flinger;
125        ISurfaceComposerClient::surface_data_t* params;
126        Client* client;
127        const String8& name;
128        uint32_t w, h;
129        PixelFormat format;
130        uint32_t flags;
131    public:
132        MessageCreateLayer(SurfaceFlinger* flinger,
133                ISurfaceComposerClient::surface_data_t* params,
134                const String8& name, Client* client,
135                uint32_t w, uint32_t h, PixelFormat format,
136                uint32_t flags)
137            : flinger(flinger), params(params), client(client), name(name),
138              w(w), h(h), format(format), flags(flags)
139        {
140        }
141        sp<ISurface> getResult() const { return result; }
142        virtual bool handler() {
143            // TODO don't require display id to create a layer
144            result = flinger->createLayer(params, name, client,
145                    ISurfaceComposer::eDisplayIdMain, w, h, format, flags);
146            return true;
147        }
148    };
149
150    sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
151            params, name, this, w, h, format, flags);
152    mFlinger->postMessageSync(msg);
153    return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
154}
155status_t Client::destroySurface(SurfaceID sid) {
156    return mFlinger->onLayerRemoved(this, sid);
157}
158
159// ---------------------------------------------------------------------------
160}; // namespace android
161