blob: c4f9e53d5dcc57a6a0d6c7cf1bec729e44203296 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <errno.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23
Mathias Agopiancbb288b2009-09-07 16:32:45 -070024#include <utils/CallStack.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070025#include <utils/Errors.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080026#include <utils/Log.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070027#include <utils/threads.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080028
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <binder/IPCThreadState.h>
30
31#include <gui/SurfaceTextureClient.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
Mathias Agopian076b1cc2009-04-10 14:24:30 -070033#include <ui/DisplayInfo.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070034#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <ui/Rect.h>
36
Mathias Agopian9cce3252010-02-09 17:46:37 -080037#include <surfaceflinger/ISurface.h>
38#include <surfaceflinger/ISurfaceComposer.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070039#include <surfaceflinger/Surface.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080040#include <surfaceflinger/SurfaceComposerClient.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042namespace android {
43
Mathias Agopian62185b72009-04-16 16:19:50 -070044// ============================================================================
45// SurfaceControl
46// ============================================================================
47
Mathias Agopian01b76682009-04-16 20:04:08 -070048SurfaceControl::SurfaceControl(
49 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -070050 const sp<ISurface>& surface,
Mathias Agopianc10d9d92011-07-20 16:46:11 -070051 const ISurfaceComposerClient::surface_data_t& data)
Mathias Agopian62185b72009-04-16 16:19:50 -070052 : mClient(client), mSurface(surface),
Mathias Agopianc10d9d92011-07-20 16:46:11 -070053 mToken(data.token), mIdentity(data.identity)
Mathias Agopian62185b72009-04-16 16:19:50 -070054{
55}
Mathias Agopian18d84462009-04-16 20:30:22 -070056
Mathias Agopian62185b72009-04-16 16:19:50 -070057SurfaceControl::~SurfaceControl()
58{
59 destroy();
60}
61
62void SurfaceControl::destroy()
63{
Mathias Agopian18d84462009-04-16 20:30:22 -070064 if (isValid()) {
Mathias Agopian62185b72009-04-16 16:19:50 -070065 mClient->destroySurface(mToken);
66 }
67
68 // clear all references and trigger an IPC now, to make sure things
69 // happen without delay, since these resources are quite heavy.
70 mClient.clear();
71 mSurface.clear();
72 IPCThreadState::self()->flushCommands();
73}
74
75void SurfaceControl::clear()
76{
77 // here, the window manager tells us explicitly that we should destroy
78 // the surface's resource. Soon after this call, it will also release
79 // its last reference (which will call the dtor); however, it is possible
80 // that a client living in the same process still holds references which
81 // would delay the call to the dtor -- that is why we need this explicit
82 // "clear()" call.
83 destroy();
84}
85
Mathias Agopian62185b72009-04-16 16:19:50 -070086bool SurfaceControl::isSameSurface(
87 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
88{
89 if (lhs == 0 || rhs == 0)
90 return false;
91 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
92}
93
Mathias Agopian01b76682009-04-16 20:04:08 -070094status_t SurfaceControl::setLayer(int32_t layer) {
Mathias Agopian963abad2009-11-13 15:26:29 -080095 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -070096 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -070097 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -070098 return client->setLayer(mToken, layer);
99}
100status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800101 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700102 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700103 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700104 return client->setPosition(mToken, x, y);
105}
106status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800107 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700108 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700109 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700110 return client->setSize(mToken, w, h);
111}
112status_t SurfaceControl::hide() {
Mathias Agopian963abad2009-11-13 15:26:29 -0800113 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700114 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700115 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700116 return client->hide(mToken);
117}
118status_t SurfaceControl::show(int32_t layer) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800119 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700120 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700121 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700122 return client->show(mToken, layer);
123}
124status_t SurfaceControl::freeze() {
Mathias Agopian963abad2009-11-13 15:26:29 -0800125 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700126 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700127 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700128 return client->freeze(mToken);
129}
130status_t SurfaceControl::unfreeze() {
Mathias Agopian963abad2009-11-13 15:26:29 -0800131 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700132 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700133 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700134 return client->unfreeze(mToken);
135}
136status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800137 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700138 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700139 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700140 return client->setFlags(mToken, flags, mask);
141}
142status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800143 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700144 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700145 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700146 return client->setTransparentRegionHint(mToken, transparent);
147}
148status_t SurfaceControl::setAlpha(float alpha) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800149 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700150 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700151 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700152 return client->setAlpha(mToken, alpha);
153}
154status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800155 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700156 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700157 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700158 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
159}
160status_t SurfaceControl::setFreezeTint(uint32_t tint) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800161 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700162 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700163 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700164 return client->setFreezeTint(mToken, tint);
165}
Mathias Agopian62185b72009-04-16 16:19:50 -0700166
Mathias Agopian963abad2009-11-13 15:26:29 -0800167status_t SurfaceControl::validate() const
Mathias Agopian62185b72009-04-16 16:19:50 -0700168{
169 if (mToken<0 || mClient==0) {
170 LOGE("invalid token (%d, identity=%u) or client (%p)",
171 mToken, mIdentity, mClient.get());
172 return NO_INIT;
173 }
Mathias Agopian62185b72009-04-16 16:19:50 -0700174 return NO_ERROR;
175}
176
Mathias Agopian01b76682009-04-16 20:04:08 -0700177status_t SurfaceControl::writeSurfaceToParcel(
178 const sp<SurfaceControl>& control, Parcel* parcel)
179{
Mathias Agopian579b3f82010-06-08 19:54:15 -0700180 sp<ISurface> sur;
Mathias Agopian01b76682009-04-16 20:04:08 -0700181 uint32_t identity = 0;
Mathias Agopian01b76682009-04-16 20:04:08 -0700182 if (SurfaceControl::isValid(control)) {
Mathias Agopian01b76682009-04-16 20:04:08 -0700183 sur = control->mSurface;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700184 identity = control->mIdentity;
Mathias Agopian01b76682009-04-16 20:04:08 -0700185 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700186 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
Mathias Agopian01b76682009-04-16 20:04:08 -0700187 parcel->writeInt32(identity);
Mathias Agopian01b76682009-04-16 20:04:08 -0700188 return NO_ERROR;
189}
190
191sp<Surface> SurfaceControl::getSurface() const
192{
193 Mutex::Autolock _l(mLock);
194 if (mSurfaceData == 0) {
195 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
196 }
197 return mSurfaceData;
198}
199
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200// ============================================================================
201// Surface
202// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700204// ---------------------------------------------------------------------------
205
Mathias Agopian01b76682009-04-16 20:04:08 -0700206Surface::Surface(const sp<SurfaceControl>& surface)
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700207 : SurfaceTextureClient(),
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700208 mSurface(surface->mSurface),
Mathias Agopianc10d9d92011-07-20 16:46:11 -0700209 mIdentity(surface->mIdentity)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210{
Mathias Agopian01b76682009-04-16 20:04:08 -0700211 init();
212}
Mathias Agopian62185b72009-04-16 16:19:50 -0700213
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700214Surface::Surface(const Parcel& parcel, const sp<IBinder>& ref)
Mathias Agopianc10d9d92011-07-20 16:46:11 -0700215 : SurfaceTextureClient()
Mathias Agopian01b76682009-04-16 20:04:08 -0700216{
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700217 mSurface = interface_cast<ISurface>(ref);
Mathias Agopian01b76682009-04-16 20:04:08 -0700218 mIdentity = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700219 init();
220}
221
Mathias Agopian579b3f82010-06-08 19:54:15 -0700222status_t Surface::writeToParcel(
223 const sp<Surface>& surface, Parcel* parcel)
224{
225 sp<ISurface> sur;
226 uint32_t identity = 0;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700227 if (Surface::isValid(surface)) {
228 sur = surface->mSurface;
229 identity = surface->mIdentity;
Jamie Gennis89c2dd22010-08-10 16:37:53 -0700230 } else if (surface != 0 && surface->mSurface != 0) {
231 LOGW("Parceling invalid surface with non-NULL ISurface as NULL: "
Mathias Agopianc10d9d92011-07-20 16:46:11 -0700232 "mSurface = %p, mIdentity = %d",
233 surface->mSurface.get(), surface->mIdentity);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700234 }
235 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
236 parcel->writeInt32(identity);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700237 return NO_ERROR;
238
239}
240
Jamie Gennisaca4e222010-07-15 17:29:15 -0700241Mutex Surface::sCachedSurfacesLock;
Mathias Agopian455d18d2010-12-13 16:47:31 -0800242DefaultKeyedVector<wp<IBinder>, wp<Surface> > Surface::sCachedSurfaces;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700243
244sp<Surface> Surface::readFromParcel(const Parcel& data) {
245 Mutex::Autolock _l(sCachedSurfacesLock);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700246 sp<IBinder> binder(data.readStrongBinder());
Jamie Gennisaca4e222010-07-15 17:29:15 -0700247 sp<Surface> surface = sCachedSurfaces.valueFor(binder).promote();
248 if (surface == 0) {
249 surface = new Surface(data, binder);
250 sCachedSurfaces.add(binder, surface);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700251 }
Jamie Gennisaca4e222010-07-15 17:29:15 -0700252 if (surface->mSurface == 0) {
253 surface = 0;
254 }
Mathias Agopian455d18d2010-12-13 16:47:31 -0800255 cleanCachedSurfacesLocked();
Jamie Gennisaca4e222010-07-15 17:29:15 -0700256 return surface;
257}
258
259// Remove the stale entries from the surface cache. This should only be called
260// with sCachedSurfacesLock held.
Mathias Agopian455d18d2010-12-13 16:47:31 -0800261void Surface::cleanCachedSurfacesLocked() {
Jamie Gennisaca4e222010-07-15 17:29:15 -0700262 for (int i = sCachedSurfaces.size()-1; i >= 0; --i) {
263 wp<Surface> s(sCachedSurfaces.valueAt(i));
264 if (s == 0 || s.promote() == 0) {
265 sCachedSurfaces.removeItemsAt(i);
266 }
267 }
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700268}
269
Mathias Agopian01b76682009-04-16 20:04:08 -0700270void Surface::init()
271{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700272 if (mSurface != NULL) {
273 sp<ISurfaceTexture> surfaceTexture(mSurface->getSurfaceTexture());
274 LOGE_IF(surfaceTexture==0, "got a NULL ISurfaceTexture from ISurface");
275 if (surfaceTexture != NULL) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700276 setISurfaceTexture(surfaceTexture);
277 setUsage(GraphicBuffer::USAGE_HW_RENDER);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700278 }
Mathias Agopian631f3582010-05-25 17:51:34 -0700279
Mathias Agopiana67932f2011-04-20 14:20:59 -0700280 DisplayInfo dinfo;
281 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
282 const_cast<float&>(ANativeWindow::xdpi) = dinfo.xdpi;
283 const_cast<float&>(ANativeWindow::ydpi) = dinfo.ydpi;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700284 const_cast<uint32_t&>(ANativeWindow::flags) = 0;
Mathias Agopian631f3582010-05-25 17:51:34 -0700285 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286}
287
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288Surface::~Surface()
289{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700290 // clear all references and trigger an IPC now, to make sure things
291 // happen without delay, since these resources are quite heavy.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 IPCThreadState::self()->flushCommands();
294}
295
Mathias Agopian631f3582010-05-25 17:51:34 -0700296bool Surface::isValid() {
Mathias Agopianc10d9d92011-07-20 16:46:11 -0700297 return getISurfaceTexture() != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298}
299
tedbo1e7fa9e2011-06-22 15:52:53 -0700300sp<ISurfaceTexture> Surface::getSurfaceTexture() {
Mathias Agopianc10d9d92011-07-20 16:46:11 -0700301 return getISurfaceTexture();
tedbo1e7fa9e2011-06-22 15:52:53 -0700302}
303
Mathias Agopian47d87302011-04-05 15:44:20 -0700304sp<IBinder> Surface::asBinder() const {
305 return mSurface!=0 ? mSurface->asBinder() : 0;
Mathias Agopian631f3582010-05-25 17:51:34 -0700306}
307
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700308// ----------------------------------------------------------------------------
309
Mathias Agopiana67932f2011-04-20 14:20:59 -0700310int Surface::query(int what, int* value) const {
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700311 switch (what) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700312 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
Mathias Agopiana67932f2011-04-20 14:20:59 -0700313 *value = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700314 return NO_ERROR;
Jamie Gennis391bbe22011-03-14 15:00:06 -0700315 case NATIVE_WINDOW_CONCRETE_TYPE:
316 *value = NATIVE_WINDOW_SURFACE;
317 return NO_ERROR;
318 }
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700319 return SurfaceTextureClient::query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800320}
321
Mathias Agopiana138f892010-05-21 17:24:35 -0700322// ----------------------------------------------------------------------------
323
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700324status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn) {
325 ANativeWindow_Buffer outBuffer;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800326
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700327 ARect temp;
328 ARect* inOutDirtyBounds = NULL;
329 if (dirtyIn) {
330 temp = dirtyIn->getBounds();
331 inOutDirtyBounds = &temp;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800332 }
333
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700334 status_t err = SurfaceTextureClient::lock(&outBuffer, inOutDirtyBounds);
Mathias Agopian90147262010-01-22 11:47:55 -0800335
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700336 if (err == NO_ERROR) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700337 other->w = uint32_t(outBuffer.width);
338 other->h = uint32_t(outBuffer.height);
339 other->s = uint32_t(outBuffer.stride);
340 other->usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
341 other->format = uint32_t(outBuffer.format);
342 other->bits = outBuffer.bits;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700343 }
344 return err;
345}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700346
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700347status_t Surface::unlockAndPost() {
348 return SurfaceTextureClient::unlockAndPost();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349}
350
Mathias Agopiana138f892010-05-21 17:24:35 -0700351// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352}; // namespace android