blob: cba11b8a44667aef916cb97de4b51a119f32a172 [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 Agopian90ac7992012-02-25 18:48:35 -080017#ifndef ANDROID_GUI_SURFACE_H
18#define ANDROID_GUI_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 Agopian076b1cc2009-04-10 14:24:30 -070029
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070030#include <gui/SurfaceTextureClient.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080031#include <gui/ISurface.h>
32#include <gui/ISurfaceComposerClient.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080033
Mathias Agopiane4e8cf12010-04-12 16:22:15 -070034#define ANDROID_VIEW_SURFACE_JNI_ID "mNativeSurface"
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036namespace android {
37
38// ---------------------------------------------------------------------------
39
Andy McFadden2adaf042012-12-18 09:49:45 -080040class IGraphicBufferProducer;
Mathias Agopian0926f502009-05-04 14:17:04 -070041class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042class SurfaceComposerClient;
43
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044// ---------------------------------------------------------------------------
45
Mathias Agopian62185b72009-04-16 16:19:50 -070046class SurfaceControl : public RefBase
47{
48public:
49 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian01b76682009-04-16 20:04:08 -070050 return (surface != 0) && surface->isValid();
Mathias Agopian62185b72009-04-16 16:19:50 -070051 }
Mathias Agopian01b76682009-04-16 20:04:08 -070052 bool isValid() {
53 return mToken>=0 && mClient!=0;
54 }
55 static bool isSameSurface(
56 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian62185b72009-04-16 16:19:50 -070057
Mathias Agopian62185b72009-04-16 16:19:50 -070058 uint32_t getIdentity() const { return mIdentity; }
59
60 // release surface data from java
61 void clear();
62
Jeff Brown9d4e3d22012-08-24 20:00:51 -070063 status_t setLayerStack(int32_t layerStack);
Mathias Agopian62185b72009-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();
Jeff Brown380223b2012-08-26 22:49:35 -070068 status_t show();
Mathias Agopian62185b72009-04-16 16:19:50 -070069 status_t setFlags(uint32_t flags, uint32_t mask);
70 status_t setTransparentRegionHint(const Region& transparent);
71 status_t setAlpha(float alpha=1.0f);
72 status_t setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
Jamie Gennisf15a83f2012-05-10 20:43:55 -070073 status_t setCrop(const Rect& crop);
Mathias Agopian62185b72009-04-16 16:19:50 -070074
Mathias Agopian01b76682009-04-16 20:04:08 -070075 static status_t writeSurfaceToParcel(
76 const sp<SurfaceControl>& control, Parcel* parcel);
77
78 sp<Surface> getSurface() const;
79
Mathias Agopian62185b72009-04-16 16:19:50 -070080private:
Mathias Agopian01b76682009-04-16 20:04:08 -070081 // can't be copied
82 SurfaceControl& operator = (SurfaceControl& rhs);
83 SurfaceControl(const SurfaceControl& rhs);
84
Mathias Agopian62185b72009-04-16 16:19:50 -070085 friend class SurfaceComposerClient;
Mathias Agopian62185b72009-04-16 16:19:50 -070086 friend class Surface;
Mathias Agopian01b76682009-04-16 20:04:08 -070087
88 SurfaceControl(
89 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -070090 const sp<ISurface>& surface,
Mathias Agopianc10d9d92011-07-20 16:46:11 -070091 const ISurfaceComposerClient::surface_data_t& data);
Mathias Agopian62185b72009-04-16 16:19:50 -070092
93 ~SurfaceControl();
Mathias Agopian01b76682009-04-16 20:04:08 -070094
Mathias Agopian963abad2009-11-13 15:26:29 -080095 status_t validate() const;
Mathias Agopian62185b72009-04-16 16:19:50 -070096 void destroy();
97
98 sp<SurfaceComposerClient> mClient;
99 sp<ISurface> mSurface;
100 SurfaceID mToken;
101 uint32_t mIdentity;
Mathias Agopian62185b72009-04-16 16:19:50 -0700102 mutable Mutex mLock;
Mathias Agopian01b76682009-04-16 20:04:08 -0700103
104 mutable sp<Surface> mSurfaceData;
Mathias Agopian62185b72009-04-16 16:19:50 -0700105};
106
107// ---------------------------------------------------------------------------
108
Andy McFadden0273adb2012-12-12 17:10:08 -0800109/*
110 * This is a small wrapper around SurfaceTextureClient that provides some
111 * helper classes for Binder interaction.
112 *
113 * TODO: rename to SurfaceJniHelper. May want to move SurfaceInfo and
114 * the associated lock() / unlockAndPost() calls to STC.
115 */
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700116class Surface : public SurfaceTextureClient
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700117{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118public:
119 struct SurfaceInfo {
120 uint32_t w;
121 uint32_t h;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700122 uint32_t s;
123 uint32_t usage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 PixelFormat format;
125 void* bits;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126 uint32_t reserved[2];
127 };
128
Andy McFadden2adaf042012-12-18 09:49:45 -0800129 explicit Surface(const sp<IGraphicBufferProducer>& bp);
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700130
Mathias Agopianc10d9d92011-07-20 16:46:11 -0700131 static status_t writeToParcel(const sp<Surface>& control, Parcel* parcel);
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700132
Jamie Gennisaca4e222010-07-15 17:29:15 -0700133 static sp<Surface> readFromParcel(const Parcel& data);
Mathias Agopian01b76682009-04-16 20:04:08 -0700134 static bool isValid(const sp<Surface>& surface) {
135 return (surface != 0) && surface->isValid();
136 }
Mathias Agopianba5972f2009-08-14 18:52:17 -0700137
Mathias Agopianba5972f2009-08-14 18:52:17 -0700138 bool isValid();
Mathias Agopian01b76682009-04-16 20:04:08 -0700139 uint32_t getIdentity() const { return mIdentity; }
Andy McFadden2adaf042012-12-18 09:49:45 -0800140 sp<IGraphicBufferProducer> getSurfaceTexture(); // TODO: rename this
Mathias Agopian01b76682009-04-16 20:04:08 -0700141
Mathias Agopianba5972f2009-08-14 18:52:17 -0700142 // the lock/unlock APIs must be used from the same thread
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700143 status_t lock(SurfaceInfo* info, Region* dirty = NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 status_t unlockAndPost();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145
Mathias Agopian47d87302011-04-05 15:44:20 -0700146 sp<IBinder> asBinder() const;
Mathias Agopianb2965332010-04-27 16:41:19 -0700147
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148private:
Mathias Agopianba5972f2009-08-14 18:52:17 -0700149 // this is just to be able to write some unit tests
150 friend class Test;
Mathias Agopianb2965332010-04-27 16:41:19 -0700151 friend class SurfaceControl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152
Mathias Agopianb2965332010-04-27 16:41:19 -0700153 // can't be copied
154 Surface& operator = (Surface& rhs);
155 Surface(const Surface& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700157 explicit Surface(const sp<SurfaceControl>& control);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700158 Surface(const Parcel& data, const sp<IBinder>& ref);
Mathias Agopianb2965332010-04-27 16:41:19 -0700159 ~Surface();
160
Mathias Agopianb2965332010-04-27 16:41:19 -0700161 /*
162 * private stuff...
163 */
Andy McFadden2adaf042012-12-18 09:49:45 -0800164 void init(const sp<IGraphicBufferProducer>& bufferProducer);
Mathias Agopianb2965332010-04-27 16:41:19 -0700165
Mathias Agopian455d18d2010-12-13 16:47:31 -0800166 static void cleanCachedSurfacesLocked();
Jamie Gennisaca4e222010-07-15 17:29:15 -0700167
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700168 virtual int query(int what, int* value) const;
169
Mathias Agopianba5972f2009-08-14 18:52:17 -0700170 // constants
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 sp<ISurface> mSurface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 uint32_t mIdentity;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700173
Jamie Gennisaca4e222010-07-15 17:29:15 -0700174 // A cache of Surface objects that have been deserialized into this process.
175 static Mutex sCachedSurfacesLock;
176 static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177};
178
179}; // namespace android
180
Mathias Agopian90ac7992012-02-25 18:48:35 -0800181#endif // ANDROID_GUI_SURFACE_H