blob: 9c352ad9ab5398c8432ff94bd49d39530d57e7ee [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
Mathias Agopian9cce3252010-02-09 17:46:37 -080017#ifndef ANDROID_SF_SURFACE_H
18#define ANDROID_SF_SURFACE_H
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019
20#include <stdint.h>
21#include <sys/types.h>
22
Jamie Gennisaca4e222010-07-15 17:29:15 -070023#include <utils/KeyedVector.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <utils/RefBase.h>
25#include <utils/threads.h>
26
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <ui/PixelFormat.h>
28#include <ui/Region.h>
Mathias Agopian7189c002009-05-05 18:11:11 -070029#include <ui/egl/android_natives.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070030
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070031#include <gui/SurfaceTextureClient.h>
32
Mathias Agopian9cce3252010-02-09 17:46:37 -080033#include <surfaceflinger/ISurface.h>
Mathias Agopian7e27f052010-05-28 14:22:23 -070034#include <surfaceflinger/ISurfaceComposerClient.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080035
Mathias Agopiane4e8cf12010-04-12 16:22:15 -070036#define ANDROID_VIEW_SURFACE_JNI_ID "mNativeSurface"
37
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038namespace android {
39
40// ---------------------------------------------------------------------------
41
tedbo1e7fa9e2011-06-22 15:52:53 -070042class ISurfaceTexture;
Mathias Agopian0926f502009-05-04 14:17:04 -070043class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044class SurfaceComposerClient;
45
Mathias Agopian076b1cc2009-04-10 14:24:30 -070046// ---------------------------------------------------------------------------
47
Mathias Agopian62185b72009-04-16 16:19:50 -070048class SurfaceControl : public RefBase
49{
50public:
51 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian01b76682009-04-16 20:04:08 -070052 return (surface != 0) && surface->isValid();
Mathias Agopian62185b72009-04-16 16:19:50 -070053 }
Mathias Agopian01b76682009-04-16 20:04:08 -070054 bool isValid() {
55 return mToken>=0 && mClient!=0;
56 }
57 static bool isSameSurface(
58 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian62185b72009-04-16 16:19:50 -070059
Mathias Agopian62185b72009-04-16 16:19:50 -070060 uint32_t getIdentity() const { return mIdentity; }
61
62 // release surface data from java
63 void clear();
64
Mathias Agopian62185b72009-04-16 16:19:50 -070065 status_t setLayer(int32_t layer);
66 status_t setPosition(int32_t x, int32_t y);
67 status_t setSize(uint32_t w, uint32_t h);
68 status_t hide();
69 status_t show(int32_t layer = -1);
70 status_t freeze();
71 status_t unfreeze();
72 status_t setFlags(uint32_t flags, uint32_t mask);
73 status_t setTransparentRegionHint(const Region& transparent);
74 status_t setAlpha(float alpha=1.0f);
75 status_t setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
76 status_t setFreezeTint(uint32_t tint);
77
Mathias Agopian01b76682009-04-16 20:04:08 -070078 static status_t writeSurfaceToParcel(
79 const sp<SurfaceControl>& control, Parcel* parcel);
80
81 sp<Surface> getSurface() const;
82
Mathias Agopian62185b72009-04-16 16:19:50 -070083private:
Mathias Agopian01b76682009-04-16 20:04:08 -070084 // can't be copied
85 SurfaceControl& operator = (SurfaceControl& rhs);
86 SurfaceControl(const SurfaceControl& rhs);
87
Mathias Agopian62185b72009-04-16 16:19:50 -070088 friend class SurfaceComposerClient;
Mathias Agopian62185b72009-04-16 16:19:50 -070089 friend class Surface;
Mathias Agopian01b76682009-04-16 20:04:08 -070090
91 SurfaceControl(
92 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -070093 const sp<ISurface>& surface,
Mathias Agopianc10d9d92011-07-20 16:46:11 -070094 const ISurfaceComposerClient::surface_data_t& data);
Mathias Agopian62185b72009-04-16 16:19:50 -070095
96 ~SurfaceControl();
Mathias Agopian01b76682009-04-16 20:04:08 -070097
Mathias Agopian963abad2009-11-13 15:26:29 -080098 status_t validate() const;
Mathias Agopian62185b72009-04-16 16:19:50 -070099 void destroy();
100
101 sp<SurfaceComposerClient> mClient;
102 sp<ISurface> mSurface;
103 SurfaceID mToken;
104 uint32_t mIdentity;
Mathias Agopian62185b72009-04-16 16:19:50 -0700105 mutable Mutex mLock;
Mathias Agopian01b76682009-04-16 20:04:08 -0700106
107 mutable sp<Surface> mSurfaceData;
Mathias Agopian62185b72009-04-16 16:19:50 -0700108};
109
110// ---------------------------------------------------------------------------
111
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700112class Surface : public SurfaceTextureClient
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700113{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114public:
115 struct SurfaceInfo {
116 uint32_t w;
117 uint32_t h;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700118 uint32_t s;
119 uint32_t usage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 PixelFormat format;
121 void* bits;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 uint32_t reserved[2];
123 };
124
Mathias Agopianc10d9d92011-07-20 16:46:11 -0700125 static status_t writeToParcel(const sp<Surface>& control, Parcel* parcel);
Jamie Gennisaca4e222010-07-15 17:29:15 -0700126 static sp<Surface> readFromParcel(const Parcel& data);
Mathias Agopian01b76682009-04-16 20:04:08 -0700127 static bool isValid(const sp<Surface>& surface) {
128 return (surface != 0) && surface->isValid();
129 }
Mathias Agopianba5972f2009-08-14 18:52:17 -0700130
Mathias Agopianba5972f2009-08-14 18:52:17 -0700131 bool isValid();
Mathias Agopian01b76682009-04-16 20:04:08 -0700132 uint32_t getIdentity() const { return mIdentity; }
tedbo1e7fa9e2011-06-22 15:52:53 -0700133 sp<ISurfaceTexture> getSurfaceTexture();
Mathias Agopian01b76682009-04-16 20:04:08 -0700134
Mathias Agopianba5972f2009-08-14 18:52:17 -0700135 // the lock/unlock APIs must be used from the same thread
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700136 status_t lock(SurfaceInfo* info, Region* dirty = NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 status_t unlockAndPost();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138
Mathias Agopian47d87302011-04-05 15:44:20 -0700139 sp<IBinder> asBinder() const;
Mathias Agopianb2965332010-04-27 16:41:19 -0700140
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141private:
Mathias Agopianba5972f2009-08-14 18:52:17 -0700142 // this is just to be able to write some unit tests
143 friend class Test;
Mathias Agopianb2965332010-04-27 16:41:19 -0700144 friend class SurfaceControl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145
Mathias Agopianb2965332010-04-27 16:41:19 -0700146 // can't be copied
147 Surface& operator = (Surface& rhs);
148 Surface(const Surface& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149
Mathias Agopianb2965332010-04-27 16:41:19 -0700150 Surface(const sp<SurfaceControl>& control);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700151 Surface(const Parcel& data, const sp<IBinder>& ref);
Mathias Agopianb2965332010-04-27 16:41:19 -0700152 ~Surface();
153
Mathias Agopianb2965332010-04-27 16:41:19 -0700154 /*
155 * private stuff...
156 */
157 void init();
Mathias Agopianb2965332010-04-27 16:41:19 -0700158
Mathias Agopian455d18d2010-12-13 16:47:31 -0800159 static void cleanCachedSurfacesLocked();
Jamie Gennisaca4e222010-07-15 17:29:15 -0700160
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700161 virtual int query(int what, int* value) const;
162
Mathias Agopianba5972f2009-08-14 18:52:17 -0700163 // constants
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 sp<ISurface> mSurface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 uint32_t mIdentity;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700166
Jamie Gennisaca4e222010-07-15 17:29:15 -0700167 // A cache of Surface objects that have been deserialized into this process.
168 static Mutex sCachedSurfacesLock;
169 static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170};
171
172}; // namespace android
173
Mathias Agopian9cce3252010-02-09 17:46:37 -0800174#endif // ANDROID_SF_SURFACE_H