SurfaceControl.cpp revision d85084b2b65828442eafaff9b811e9b6c9ca9fad
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 "SurfaceControl"
18
19#include <stdint.h>
20#include <errno.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23
24#include <android/native_window.h>
25
26#include <utils/Errors.h>
27#include <utils/Log.h>
28#include <utils/threads.h>
29
30#include <binder/IPCThreadState.h>
31
32#include <ui/DisplayInfo.h>
33#include <ui/GraphicBuffer.h>
34#include <ui/Rect.h>
35
36#include <gui/ISurfaceComposer.h>
37#include <gui/Surface.h>
38#include <gui/SurfaceComposerClient.h>
39#include <gui/SurfaceControl.h>
40
41namespace android {
42
43// ============================================================================
44//  SurfaceControl
45// ============================================================================
46
47SurfaceControl::SurfaceControl(
48        const sp<SurfaceComposerClient>& client,
49        const sp<IBinder>& handle,
50        const sp<IGraphicBufferProducer>& gbp)
51    : mClient(client), mHandle(handle), mGraphicBufferProducer(gbp)
52{
53}
54
55SurfaceControl::~SurfaceControl()
56{
57    destroy();
58}
59
60void SurfaceControl::destroy()
61{
62    if (isValid()) {
63        mClient->destroySurface(mHandle);
64    }
65    // clear all references and trigger an IPC now, to make sure things
66    // happen without delay, since these resources are quite heavy.
67    mClient.clear();
68    mHandle.clear();
69    mGraphicBufferProducer.clear();
70    IPCThreadState::self()->flushCommands();
71}
72
73void SurfaceControl::clear()
74{
75    // here, the window manager tells us explicitly that we should destroy
76    // the surface's resource. Soon after this call, it will also release
77    // its last reference (which will call the dtor); however, it is possible
78    // that a client living in the same process still holds references which
79    // would delay the call to the dtor -- that is why we need this explicit
80    // "clear()" call.
81    destroy();
82}
83
84bool SurfaceControl::isSameSurface(
85        const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
86{
87    if (lhs == 0 || rhs == 0)
88        return false;
89    return lhs->mHandle == rhs->mHandle;
90}
91
92status_t SurfaceControl::setLayerStack(int32_t layerStack) {
93    status_t err = validate();
94    if (err < 0) return err;
95    const sp<SurfaceComposerClient>& client(mClient);
96    return client->setLayerStack(mHandle, layerStack);
97}
98status_t SurfaceControl::setLayer(int32_t layer) {
99    status_t err = validate();
100    if (err < 0) return err;
101    const sp<SurfaceComposerClient>& client(mClient);
102    return client->setLayer(mHandle, layer);
103}
104status_t SurfaceControl::setPosition(float x, float y) {
105    status_t err = validate();
106    if (err < 0) return err;
107    const sp<SurfaceComposerClient>& client(mClient);
108    return client->setPosition(mHandle, x, y);
109}
110status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
111    status_t err = validate();
112    if (err < 0) return err;
113    const sp<SurfaceComposerClient>& client(mClient);
114    return client->setSize(mHandle, w, h);
115}
116status_t SurfaceControl::hide() {
117    status_t err = validate();
118    if (err < 0) return err;
119    const sp<SurfaceComposerClient>& client(mClient);
120    return client->hide(mHandle);
121}
122status_t SurfaceControl::show() {
123    status_t err = validate();
124    if (err < 0) return err;
125    const sp<SurfaceComposerClient>& client(mClient);
126    return client->show(mHandle);
127}
128status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
129    status_t err = validate();
130    if (err < 0) return err;
131    const sp<SurfaceComposerClient>& client(mClient);
132    return client->setFlags(mHandle, flags, mask);
133}
134status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
135    status_t err = validate();
136    if (err < 0) return err;
137    const sp<SurfaceComposerClient>& client(mClient);
138    return client->setTransparentRegionHint(mHandle, transparent);
139}
140status_t SurfaceControl::setAlpha(float alpha) {
141    status_t err = validate();
142    if (err < 0) return err;
143    const sp<SurfaceComposerClient>& client(mClient);
144    return client->setAlpha(mHandle, alpha);
145}
146status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
147    status_t err = validate();
148    if (err < 0) return err;
149    const sp<SurfaceComposerClient>& client(mClient);
150    return client->setMatrix(mHandle, dsdx, dtdx, dsdy, dtdy);
151}
152status_t SurfaceControl::setCrop(const Rect& crop) {
153    status_t err = validate();
154    if (err < 0) return err;
155    const sp<SurfaceComposerClient>& client(mClient);
156    return client->setCrop(mHandle, crop);
157}
158
159status_t SurfaceControl::clearLayerFrameStats() const {
160    status_t err = validate();
161    if (err < 0) return err;
162    const sp<SurfaceComposerClient>& client(mClient);
163    return client->clearLayerFrameStats(mHandle);
164}
165
166status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
167    status_t err = validate();
168    if (err < 0) return err;
169    const sp<SurfaceComposerClient>& client(mClient);
170    return client->getLayerFrameStats(mHandle, outStats);
171}
172
173status_t SurfaceControl::validate() const
174{
175    if (mHandle==0 || mClient==0) {
176        ALOGE("invalid handle (%p) or client (%p)",
177                mHandle.get(), mClient.get());
178        return NO_INIT;
179    }
180    return NO_ERROR;
181}
182
183status_t SurfaceControl::writeSurfaceToParcel(
184        const sp<SurfaceControl>& control, Parcel* parcel)
185{
186    sp<IGraphicBufferProducer> bp;
187    if (control != NULL) {
188        bp = control->mGraphicBufferProducer;
189    }
190    return parcel->writeStrongBinder(bp->asBinder());
191}
192
193sp<Surface> SurfaceControl::getSurface() const
194{
195    Mutex::Autolock _l(mLock);
196    if (mSurfaceData == 0) {
197        // This surface is always consumed by SurfaceFlinger, so the
198        // producerControlledByApp value doesn't matter; using false.
199        mSurfaceData = new Surface(mGraphicBufferProducer, false);
200    }
201    return mSurfaceData;
202}
203
204// ----------------------------------------------------------------------------
205}; // namespace android
206