blob: ec75d4e9f9641b5b0c8e168920d4825802abcf1a [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034namespace android {
35
36// ---------------------------------------------------------------------------
37
Andy McFadden2adaf042012-12-18 09:49:45 -080038class IGraphicBufferProducer;
Mathias Agopian0926f502009-05-04 14:17:04 -070039class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040class SurfaceComposerClient;
41
Mathias Agopian076b1cc2009-04-10 14:24:30 -070042// ---------------------------------------------------------------------------
43
Mathias Agopian62185b72009-04-16 16:19:50 -070044class SurfaceControl : public RefBase
45{
46public:
47 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian01b76682009-04-16 20:04:08 -070048 return (surface != 0) && surface->isValid();
Mathias Agopian62185b72009-04-16 16:19:50 -070049 }
Mathias Agopian01b76682009-04-16 20:04:08 -070050 bool isValid() {
Mathias Agopianac9fa422013-02-11 16:40:36 -080051 return mSurface!=0 && mClient!=0;
Mathias Agopian01b76682009-04-16 20:04:08 -070052 }
53 static bool isSameSurface(
54 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian62185b72009-04-16 16:19:50 -070055
Mathias Agopian62185b72009-04-16 16:19:50 -070056 // release surface data from java
57 void clear();
58
Jeff Brown9d4e3d22012-08-24 20:00:51 -070059 status_t setLayerStack(int32_t layerStack);
Mathias Agopian62185b72009-04-16 16:19:50 -070060 status_t setLayer(int32_t layer);
61 status_t setPosition(int32_t x, int32_t y);
62 status_t setSize(uint32_t w, uint32_t h);
63 status_t hide();
Jeff Brown380223b2012-08-26 22:49:35 -070064 status_t show();
Mathias Agopian62185b72009-04-16 16:19:50 -070065 status_t setFlags(uint32_t flags, uint32_t mask);
66 status_t setTransparentRegionHint(const Region& transparent);
67 status_t setAlpha(float alpha=1.0f);
68 status_t setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
Jamie Gennisf15a83f2012-05-10 20:43:55 -070069 status_t setCrop(const Rect& crop);
Mathias Agopian62185b72009-04-16 16:19:50 -070070
Mathias Agopian01b76682009-04-16 20:04:08 -070071 static status_t writeSurfaceToParcel(
72 const sp<SurfaceControl>& control, Parcel* parcel);
73
74 sp<Surface> getSurface() const;
75
Mathias Agopian62185b72009-04-16 16:19:50 -070076private:
Mathias Agopian01b76682009-04-16 20:04:08 -070077 // can't be copied
78 SurfaceControl& operator = (SurfaceControl& rhs);
79 SurfaceControl(const SurfaceControl& rhs);
80
Mathias Agopian62185b72009-04-16 16:19:50 -070081 friend class SurfaceComposerClient;
Mathias Agopian62185b72009-04-16 16:19:50 -070082 friend class Surface;
Mathias Agopian01b76682009-04-16 20:04:08 -070083
84 SurfaceControl(
85 const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -080086 const sp<ISurface>& surface);
Mathias Agopian62185b72009-04-16 16:19:50 -070087
88 ~SurfaceControl();
Mathias Agopian01b76682009-04-16 20:04:08 -070089
Mathias Agopian963abad2009-11-13 15:26:29 -080090 status_t validate() const;
Mathias Agopian62185b72009-04-16 16:19:50 -070091 void destroy();
92
93 sp<SurfaceComposerClient> mClient;
Mathias Agopianac9fa422013-02-11 16:40:36 -080094 sp<IBinder> mSurface;
Mathias Agopian62185b72009-04-16 16:19:50 -070095 mutable Mutex mLock;
Mathias Agopian01b76682009-04-16 20:04:08 -070096 mutable sp<Surface> mSurfaceData;
Mathias Agopian62185b72009-04-16 16:19:50 -070097};
98
99// ---------------------------------------------------------------------------
100
Andy McFadden0273adb2012-12-12 17:10:08 -0800101/*
Andy McFadden882e3a32013-01-08 16:06:15 -0800102 * This is a small wrapper around SurfaceTextureClient.
Andy McFadden0273adb2012-12-12 17:10:08 -0800103 *
Andy McFadden882e3a32013-01-08 16:06:15 -0800104 * TODO: rename and/or merge with STC.
Andy McFadden0273adb2012-12-12 17:10:08 -0800105 */
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700106class Surface : public SurfaceTextureClient
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700107{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108public:
109 struct SurfaceInfo {
110 uint32_t w;
111 uint32_t h;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700112 uint32_t s;
113 uint32_t usage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 PixelFormat format;
115 void* bits;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 uint32_t reserved[2];
117 };
118
Andy McFadden2adaf042012-12-18 09:49:45 -0800119 explicit Surface(const sp<IGraphicBufferProducer>& bp);
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700120
Mathias Agopianc10d9d92011-07-20 16:46:11 -0700121 static status_t writeToParcel(const sp<Surface>& control, Parcel* parcel);
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700122
Jamie Gennisaca4e222010-07-15 17:29:15 -0700123 static sp<Surface> readFromParcel(const Parcel& data);
Mathias Agopian01b76682009-04-16 20:04:08 -0700124 static bool isValid(const sp<Surface>& surface) {
125 return (surface != 0) && surface->isValid();
126 }
Mathias Agopianba5972f2009-08-14 18:52:17 -0700127
Mathias Agopianba5972f2009-08-14 18:52:17 -0700128 bool isValid();
Mathias Agopian01b76682009-04-16 20:04:08 -0700129 uint32_t getIdentity() const { return mIdentity; }
Andy McFadden2adaf042012-12-18 09:49:45 -0800130 sp<IGraphicBufferProducer> getSurfaceTexture(); // TODO: rename this
Mathias Agopian01b76682009-04-16 20:04:08 -0700131
Mathias Agopianba5972f2009-08-14 18:52:17 -0700132 // the lock/unlock APIs must be used from the same thread
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700133 status_t lock(SurfaceInfo* info, Region* dirty = NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 status_t unlockAndPost();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135
Mathias Agopian47d87302011-04-05 15:44:20 -0700136 sp<IBinder> asBinder() const;
Mathias Agopianb2965332010-04-27 16:41:19 -0700137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138private:
Mathias Agopianba5972f2009-08-14 18:52:17 -0700139 // this is just to be able to write some unit tests
140 friend class Test;
Mathias Agopianb2965332010-04-27 16:41:19 -0700141 friend class SurfaceControl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142
Mathias Agopianb2965332010-04-27 16:41:19 -0700143 // can't be copied
144 Surface& operator = (Surface& rhs);
145 Surface(const Surface& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700147 explicit Surface(const sp<SurfaceControl>& control);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700148 Surface(const Parcel& data, const sp<IBinder>& ref);
Mathias Agopianb2965332010-04-27 16:41:19 -0700149 ~Surface();
150
Mathias Agopianb2965332010-04-27 16:41:19 -0700151 /*
152 * private stuff...
153 */
Andy McFadden2adaf042012-12-18 09:49:45 -0800154 void init(const sp<IGraphicBufferProducer>& bufferProducer);
Mathias Agopianb2965332010-04-27 16:41:19 -0700155
Mathias Agopian455d18d2010-12-13 16:47:31 -0800156 static void cleanCachedSurfacesLocked();
Jamie Gennisaca4e222010-07-15 17:29:15 -0700157
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700158 virtual int query(int what, int* value) const;
159
Mathias Agopianba5972f2009-08-14 18:52:17 -0700160 // constants
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 sp<ISurface> mSurface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 uint32_t mIdentity;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700163
Jamie Gennisaca4e222010-07-15 17:29:15 -0700164 // A cache of Surface objects that have been deserialized into this process.
165 static Mutex sCachedSurfacesLock;
166 static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167};
168
169}; // namespace android
170
Mathias Agopian90ac7992012-02-25 18:48:35 -0800171#endif // ANDROID_GUI_SURFACE_H