blob: 06eff8a5974e21423a4743c4f2dbe2db3f606049 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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
Mathias Agopian000479f2010-02-09 17:46:37 -080017#ifndef ANDROID_SF_SURFACE_H
18#define ANDROID_SF_SURFACE_H
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
20#include <stdint.h>
21#include <sys/types.h>
22
Jamie Gennis5ee65f02010-07-15 17:29:15 -070023#include <utils/KeyedVector.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <utils/RefBase.h>
25#include <utils/threads.h>
26
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <ui/PixelFormat.h>
28#include <ui/Region.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070029
Mathias Agopian949be322011-07-13 17:39:11 -070030#include <gui/SurfaceTextureClient.h>
31
Mathias Agopian000479f2010-02-09 17:46:37 -080032#include <surfaceflinger/ISurface.h>
Mathias Agopian770492c2010-05-28 14:22:23 -070033#include <surfaceflinger/ISurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080034
Mathias Agopian8b138322010-04-12 16:22:15 -070035#define ANDROID_VIEW_SURFACE_JNI_ID "mNativeSurface"
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037namespace android {
38
39// ---------------------------------------------------------------------------
40
tedbo4e8a5c92011-06-22 15:52:53 -070041class ISurfaceTexture;
Mathias Agopiandff8e582009-05-04 14:17:04 -070042class Surface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043class SurfaceComposerClient;
44
Mathias Agopian1473f462009-04-10 14:24:30 -070045// ---------------------------------------------------------------------------
46
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070047class SurfaceControl : public RefBase
48{
49public:
50 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian17f638b2009-04-16 20:04:08 -070051 return (surface != 0) && surface->isValid();
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070052 }
Mathias Agopian17f638b2009-04-16 20:04:08 -070053 bool isValid() {
54 return mToken>=0 && mClient!=0;
55 }
56 static bool isSameSurface(
57 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070058
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070059 uint32_t getIdentity() const { return mIdentity; }
60
61 // release surface data from java
62 void clear();
63
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070064 status_t setLayer(int32_t layer);
65 status_t setPosition(int32_t x, int32_t y);
66 status_t setSize(uint32_t w, uint32_t h);
67 status_t hide();
68 status_t show(int32_t layer = -1);
69 status_t freeze();
70 status_t unfreeze();
71 status_t setFlags(uint32_t flags, uint32_t mask);
72 status_t setTransparentRegionHint(const Region& transparent);
73 status_t setAlpha(float alpha=1.0f);
74 status_t setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
75 status_t setFreezeTint(uint32_t tint);
76
Mathias Agopian17f638b2009-04-16 20:04:08 -070077 static status_t writeSurfaceToParcel(
78 const sp<SurfaceControl>& control, Parcel* parcel);
79
80 sp<Surface> getSurface() const;
81
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070082private:
Mathias Agopian17f638b2009-04-16 20:04:08 -070083 // can't be copied
84 SurfaceControl& operator = (SurfaceControl& rhs);
85 SurfaceControl(const SurfaceControl& rhs);
86
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070087 friend class SurfaceComposerClient;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070088 friend class Surface;
Mathias Agopian17f638b2009-04-16 20:04:08 -070089
90 SurfaceControl(
91 const sp<SurfaceComposerClient>& client,
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070092 const sp<ISurface>& surface,
Mathias Agopian50c24a22011-07-20 16:46:11 -070093 const ISurfaceComposerClient::surface_data_t& data);
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070094
95 ~SurfaceControl();
Mathias Agopian17f638b2009-04-16 20:04:08 -070096
Mathias Agopian18e02602009-11-13 15:26:29 -080097 status_t validate() const;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070098 void destroy();
99
100 sp<SurfaceComposerClient> mClient;
101 sp<ISurface> mSurface;
102 SurfaceID mToken;
103 uint32_t mIdentity;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700104 mutable Mutex mLock;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700105
106 mutable sp<Surface> mSurfaceData;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700107};
108
109// ---------------------------------------------------------------------------
110
Mathias Agopian949be322011-07-13 17:39:11 -0700111class Surface : public SurfaceTextureClient
Mathias Agopian1473f462009-04-10 14:24:30 -0700112{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113public:
114 struct SurfaceInfo {
115 uint32_t w;
116 uint32_t h;
Mathias Agopian1473f462009-04-10 14:24:30 -0700117 uint32_t s;
118 uint32_t usage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 PixelFormat format;
120 void* bits;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 uint32_t reserved[2];
122 };
123
Ted Bonkenburg0de171b2011-07-15 15:10:10 -0700124 explicit Surface(const sp<ISurfaceTexture>& st);
125
Mathias Agopian50c24a22011-07-20 16:46:11 -0700126 static status_t writeToParcel(const sp<Surface>& control, Parcel* parcel);
Ted Bonkenburg0de171b2011-07-15 15:10:10 -0700127
Jamie Gennis5ee65f02010-07-15 17:29:15 -0700128 static sp<Surface> readFromParcel(const Parcel& data);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700129 static bool isValid(const sp<Surface>& surface) {
130 return (surface != 0) && surface->isValid();
131 }
Mathias Agopian321abdb2009-08-14 18:52:17 -0700132
Mathias Agopian321abdb2009-08-14 18:52:17 -0700133 bool isValid();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700134 uint32_t getIdentity() const { return mIdentity; }
tedbo4e8a5c92011-06-22 15:52:53 -0700135 sp<ISurfaceTexture> getSurfaceTexture();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700136
Mathias Agopian321abdb2009-08-14 18:52:17 -0700137 // the lock/unlock APIs must be used from the same thread
Mathias Agopian949be322011-07-13 17:39:11 -0700138 status_t lock(SurfaceInfo* info, Region* dirty = NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 status_t unlockAndPost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
Mathias Agopianc8a04b52011-04-05 15:44:20 -0700141 sp<IBinder> asBinder() const;
Mathias Agopianf590f702010-04-27 16:41:19 -0700142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143private:
Mathias Agopian321abdb2009-08-14 18:52:17 -0700144 // this is just to be able to write some unit tests
145 friend class Test;
Mathias Agopianf590f702010-04-27 16:41:19 -0700146 friend class SurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
Mathias Agopianf590f702010-04-27 16:41:19 -0700148 // can't be copied
149 Surface& operator = (Surface& rhs);
150 Surface(const Surface& rhs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151
Ted Bonkenburg0de171b2011-07-15 15:10:10 -0700152 explicit Surface(const sp<SurfaceControl>& control);
Mathias Agopianfae5cb22010-06-04 18:26:32 -0700153 Surface(const Parcel& data, const sp<IBinder>& ref);
Mathias Agopianf590f702010-04-27 16:41:19 -0700154 ~Surface();
155
Mathias Agopianf590f702010-04-27 16:41:19 -0700156 /*
157 * private stuff...
158 */
Ted Bonkenburg0de171b2011-07-15 15:10:10 -0700159 void init(const sp<ISurfaceTexture>& surfaceTexture);
Mathias Agopianf590f702010-04-27 16:41:19 -0700160
Mathias Agopian06f9ebf2010-12-13 16:47:31 -0800161 static void cleanCachedSurfacesLocked();
Jamie Gennis5ee65f02010-07-15 17:29:15 -0700162
Mathias Agopian949be322011-07-13 17:39:11 -0700163 virtual int query(int what, int* value) const;
164
Mathias Agopian321abdb2009-08-14 18:52:17 -0700165 // constants
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 sp<ISurface> mSurface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 uint32_t mIdentity;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700168
Jamie Gennis5ee65f02010-07-15 17:29:15 -0700169 // A cache of Surface objects that have been deserialized into this process.
170 static Mutex sCachedSurfacesLock;
171 static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172};
173
174}; // namespace android
175
Mathias Agopian000479f2010-02-09 17:46:37 -0800176#endif // ANDROID_SF_SURFACE_H