blob: 0a9aaad0b5aebc77ccd672359f6152ebafbeb4d0 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
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 "Surface"
18
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25
26#include <utils/Atomic.h>
27#include <utils/Errors.h>
28#include <utils/threads.h>
29#include <utils/IPCThreadState.h>
30#include <utils/IMemory.h>
31#include <utils/Log.h>
32
33#include <ui/ISurface.h>
34#include <ui/Surface.h>
35#include <ui/SurfaceComposerClient.h>
36#include <ui/Rect.h>
37
38#include <private/ui/SharedState.h>
39#include <private/ui/LayerState.h>
40
41namespace android {
42
43// ---------------------------------------------------------------------------
44
45Surface::Surface(const sp<SurfaceComposerClient>& client,
46 const sp<ISurface>& surface,
47 const ISurfaceFlingerClient::surface_data_t& data,
48 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
49 bool owner)
50 : mClient(client), mSurface(surface), mMemoryType(data.type),
51 mToken(data.token), mIdentity(data.identity),
52 mFormat(format), mFlags(flags), mOwner(owner)
53{
54 mSwapRectangle.makeInvalid();
55 mSurfaceHeapBase[0] = 0;
56 mSurfaceHeapBase[1] = 0;
57 mHeap[0] = data.heap[0];
58 mHeap[1] = data.heap[1];
59}
60
61Surface::Surface(Surface const* rhs)
62 : mOwner(false)
63{
64 mToken = rhs->mToken;
65 mIdentity= rhs->mIdentity;
66 mClient = rhs->mClient;
67 mSurface = rhs->mSurface;
68 mHeap[0] = rhs->mHeap[0];
69 mHeap[1] = rhs->mHeap[1];
70 mMemoryType = rhs->mMemoryType;
71 mFormat = rhs->mFormat;
72 mFlags = rhs->mFlags;
73 mSurfaceHeapBase[0] = rhs->mSurfaceHeapBase[0];
74 mSurfaceHeapBase[1] = rhs->mSurfaceHeapBase[1];
75 mSwapRectangle.makeInvalid();
76}
77
78Surface::~Surface()
79{
80 if (mOwner && mToken>=0 && mClient!=0) {
81 mClient->destroySurface(mToken);
82 }
83 mClient.clear();
84 mSurface.clear();
85 mHeap[0].clear();
86 mHeap[1].clear();
87 IPCThreadState::self()->flushCommands();
88}
89
90sp<Surface> Surface::dup() const
91{
92 Surface const * r = this;
93 if (this && mOwner) {
94 // the only reason we need to do this is because of Java's garbage
95 // collector: because we're creating a copy of the Surface
96 // instead of a reference, we can garantee that when our last
97 // reference goes away, the real surface will be deleted.
98 // Without this hack (the code is correct too), we'd have to
99 // wait for a GC for the surface to go away.
100 r = new Surface(this);
101 }
102 return const_cast<Surface*>(r);
103}
104
105status_t Surface::nextBuffer(SurfaceInfo* info) {
106 return mClient->nextBuffer(this, info);
107}
108
109status_t Surface::lock(SurfaceInfo* info, bool blocking) {
110 return Surface::lock(info, NULL, blocking);
111}
112
113status_t Surface::lock(SurfaceInfo* info, Region* dirty, bool blocking) {
114 if (heapBase(0) == 0) return INVALID_OPERATION;
115 if (heapBase(1) == 0) return INVALID_OPERATION;
116 return mClient->lockSurface(this, info, dirty, blocking);
117}
118
119status_t Surface::unlockAndPost() {
120 if (heapBase(0) == 0) return INVALID_OPERATION;
121 if (heapBase(1) == 0) return INVALID_OPERATION;
122 return mClient->unlockAndPostSurface(this);
123}
124
125status_t Surface::unlock() {
126 if (heapBase(0) == 0) return INVALID_OPERATION;
127 if (heapBase(1) == 0) return INVALID_OPERATION;
128 return mClient->unlockSurface(this);
129}
130
131status_t Surface::setLayer(int32_t layer) {
132 return mClient->setLayer(this, layer);
133}
134status_t Surface::setPosition(int32_t x, int32_t y) {
135 return mClient->setPosition(this, x, y);
136}
137status_t Surface::setSize(uint32_t w, uint32_t h) {
138 return mClient->setSize(this, w, h);
139}
140status_t Surface::hide() {
141 return mClient->hide(this);
142}
143status_t Surface::show(int32_t layer) {
144 return mClient->show(this, layer);
145}
146status_t Surface::freeze() {
147 return mClient->freeze(this);
148}
149status_t Surface::unfreeze() {
150 return mClient->unfreeze(this);
151}
152status_t Surface::setFlags(uint32_t flags, uint32_t mask) {
153 return mClient->setFlags(this, flags, mask);
154}
155status_t Surface::setTransparentRegionHint(const Region& transparent) {
156 return mClient->setTransparentRegionHint(this, transparent);
157}
158status_t Surface::setAlpha(float alpha) {
159 return mClient->setAlpha(this, alpha);
160}
161status_t Surface::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
162 return mClient->setMatrix(this, dsdx, dtdx, dsdy, dtdy);
163}
164status_t Surface::setFreezeTint(uint32_t tint) {
165 return mClient->setFreezeTint(this, tint);
166}
167
168Region Surface::dirtyRegion() const {
169 return mDirtyRegion;
170}
171void Surface::setDirtyRegion(const Region& region) const {
172 mDirtyRegion = region;
173}
174const Rect& Surface::swapRectangle() const {
175 return mSwapRectangle;
176}
177void Surface::setSwapRectangle(const Rect& r) {
178 mSwapRectangle = r;
179}
180
181sp<Surface> Surface::readFromParcel(Parcel* parcel)
182{
183 sp<SurfaceComposerClient> client;
184 ISurfaceFlingerClient::surface_data_t data;
185 sp<IBinder> clientBinder= parcel->readStrongBinder();
186 sp<ISurface> surface = interface_cast<ISurface>(parcel->readStrongBinder());
187 data.heap[0] = interface_cast<IMemoryHeap>(parcel->readStrongBinder());
188 data.heap[1] = interface_cast<IMemoryHeap>(parcel->readStrongBinder());
189 data.type = parcel->readInt32();
190 data.token = parcel->readInt32();
191 data.identity = parcel->readInt32();
192 PixelFormat format = parcel->readInt32();
193 uint32_t flags = parcel->readInt32();
194
195 if (clientBinder != NULL)
196 client = SurfaceComposerClient::clientForConnection(clientBinder);
197
198 return new Surface(client, surface, data, 0, 0, format, flags, false);
199}
200
201status_t Surface::writeToParcel(const sp<Surface>& surface, Parcel* parcel)
202{
203 uint32_t flags=0;
204 uint32_t format=0;
205 SurfaceID token = -1;
206 uint32_t identity = 0;
207 sp<SurfaceComposerClient> client;
208 sp<ISurface> sur;
209 sp<IMemoryHeap> heap[2];
210 int type = 0;
211 if (surface->isValid()) {
212 token = surface->mToken;
213 identity = surface->mIdentity;
214 client = surface->mClient;
215 sur = surface->mSurface;
216 heap[0] = surface->mHeap[0];
217 heap[1] = surface->mHeap[1];
218 type = surface->mMemoryType;
219 format = surface->mFormat;
220 flags = surface->mFlags;
221 }
222 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
223 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
224 parcel->writeStrongBinder(heap[0]!=0 ? heap[0]->asBinder() : NULL);
225 parcel->writeStrongBinder(heap[1]!=0 ? heap[1]->asBinder() : NULL);
226 parcel->writeInt32(type);
227 parcel->writeInt32(token);
228 parcel->writeInt32(identity);
229 parcel->writeInt32(format);
230 parcel->writeInt32(flags);
231 return NO_ERROR;
232}
233
234bool Surface::isSameSurface(const sp<Surface>& lhs, const sp<Surface>& rhs)
235{
236 if (lhs == 0 || rhs == 0)
237 return false;
238 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
239}
240
241void* Surface::heapBase(int i) const
242{
243 void* heapBase = mSurfaceHeapBase[i];
244 // map lazily so it doesn't get mapped in clients that don't need it
245 if (heapBase == 0) {
246 const sp<IMemoryHeap>& heap(mHeap[i]);
247 if (heap != 0) {
248 heapBase = static_cast<uint8_t*>(heap->base());
249 if (heapBase == MAP_FAILED) {
250 heapBase = NULL;
251 LOGE("Couldn't map Surface's heap (binder=%p, heap=%p)",
252 heap->asBinder().get(), heap.get());
253 }
254 mSurfaceHeapBase[i] = heapBase;
255 }
256 }
257 return heapBase;
258}
259
260}; // namespace android
261