SurfaceControl.cpp revision 3b3ba78c3c586c2b09b11e0f1ab5347ff51a21e0
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    return mClient->setLayerStack(mHandle, layerStack);
96}
97status_t SurfaceControl::setLayer(int32_t layer) {
98    status_t err = validate();
99    if (err < 0) return err;
100    return mClient->setLayer(mHandle, layer);
101}
102status_t SurfaceControl::setPosition(float x, float y) {
103    status_t err = validate();
104    if (err < 0) return err;
105    return mClient->setPosition(mHandle, x, y);
106}
107status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
108    status_t err = validate();
109    if (err < 0) return err;
110    return mClient->setSize(mHandle, w, h);
111}
112status_t SurfaceControl::hide() {
113    status_t err = validate();
114    if (err < 0) return err;
115    return mClient->hide(mHandle);
116}
117status_t SurfaceControl::show() {
118    status_t err = validate();
119    if (err < 0) return err;
120    return mClient->show(mHandle);
121}
122status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
123    status_t err = validate();
124    if (err < 0) return err;
125    return mClient->setFlags(mHandle, flags, mask);
126}
127status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
128    status_t err = validate();
129    if (err < 0) return err;
130    return mClient->setTransparentRegionHint(mHandle, transparent);
131}
132status_t SurfaceControl::setAlpha(float alpha) {
133    status_t err = validate();
134    if (err < 0) return err;
135    return mClient->setAlpha(mHandle, alpha);
136}
137status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
138    status_t err = validate();
139    if (err < 0) return err;
140    return mClient->setMatrix(mHandle, dsdx, dtdx, dsdy, dtdy);
141}
142status_t SurfaceControl::setCrop(const Rect& crop) {
143    status_t err = validate();
144    if (err < 0) return err;
145    return mClient->setCrop(mHandle, crop);
146}
147
148status_t SurfaceControl::validate() const
149{
150    if (mHandle==0 || mClient==0) {
151        ALOGE("invalid handle (%p) or client (%p)",
152                mHandle.get(), mClient.get());
153        return NO_INIT;
154    }
155    return NO_ERROR;
156}
157
158status_t SurfaceControl::writeSurfaceToParcel(
159        const sp<SurfaceControl>& control, Parcel* parcel)
160{
161    sp<IGraphicBufferProducer> bp;
162    if (control != NULL) {
163        bp = control->mGraphicBufferProducer;
164    }
165    return parcel->writeStrongBinder(bp->asBinder());
166}
167
168sp<Surface> SurfaceControl::getSurface() const
169{
170    Mutex::Autolock _l(mLock);
171    if (mSurfaceData == 0) {
172        // This surface is always consumed by SurfaceFlinger, so the
173        // producerControlledByApp value doesn't matter; using false.
174        mSurfaceData = new Surface(mGraphicBufferProducer, false);
175    }
176    return mSurfaceData;
177}
178
179// ----------------------------------------------------------------------------
180}; // namespace android
181