blob: c25847ce6e1204671abd8573c2b7999f4fee3baa [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002 * Copyright (C) 2010 The Android Open Source Project
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003 *
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
Mathias Agopiane3c697f2013-02-14 17:11:02 -080020#include <gui/IGraphicBufferProducer.h>
21#include <gui/GLConsumer.h>
22#include <gui/BufferQueue.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
Mathias Agopiane3c697f2013-02-14 17:11:02 -080024#include <ui/ANativeObjectBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <ui/Region.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070026
Mathias Agopiane3c697f2013-02-14 17:11:02 -080027#include <utils/RefBase.h>
28#include <utils/threads.h>
29#include <utils/KeyedVector.h>
30
31struct ANativeWindow_Buffer;
Mathias Agopian9cce3252010-02-09 17:46:37 -080032
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033namespace android {
34
Mathias Agopiane3c697f2013-02-14 17:11:02 -080035/*
36 * An implementation of ANativeWindow that feeds graphics buffers into a
37 * BufferQueue.
38 *
39 * This is typically used by programs that want to render frames through
40 * some means (maybe OpenGL, a software renderer, or a hardware decoder)
41 * and have the frames they create forwarded to SurfaceFlinger for
42 * compositing. For example, a video decoder could render a frame and call
43 * eglSwapBuffers(), which invokes ANativeWindow callbacks defined by
44 * Surface. Surface then forwards the buffers through Binder IPC
45 * to the BufferQueue's producer interface, providing the new frame to a
46 * consumer such as GLConsumer.
47 */
48class Surface
49 : public ANativeObjectBase<ANativeWindow, Surface, RefBase>
Mathias Agopian62185b72009-04-16 16:19:50 -070050{
51public:
Mathias Agopian62185b72009-04-16 16:19:50 -070052
Mathias Agopiancf0b8c82013-02-19 18:24:40 -080053 /*
54 * creates a Surface from the given IGraphicBufferProducer (which concrete
55 * implementation is a BufferQueue).
56 *
57 * Surface is mainly state-less while it's disconnected, it can be
58 * viewed as a glorified IGraphicBufferProducer holder. It's therefore
59 * safe to create other Surfaces from the same IGraphicBufferProducer.
60 *
61 * However, once a Surface is connected, it'll prevent other Surfaces
62 * referring to the same IGraphicBufferProducer to become connected and
63 * therefore prevent them to be used as actual producers of buffers.
64 */
Mathias Agopiane3c697f2013-02-14 17:11:02 -080065 Surface(const sp<IGraphicBufferProducer>& bufferProducer);
Mathias Agopian01b76682009-04-16 20:04:08 -070066
Mathias Agopiancf0b8c82013-02-19 18:24:40 -080067 /* getIGraphicBufferProducer() returns the IGraphicBufferProducer this
68 * Surface was created with. Usually it's an error to use the
69 * IGraphicBufferProducer while the Surface is connected.
70 */
Mathias Agopiane3c697f2013-02-14 17:11:02 -080071 sp<IGraphicBufferProducer> getIGraphicBufferProducer() const;
72
Mathias Agopiancf0b8c82013-02-19 18:24:40 -080073 /* convenience function to check that the given surface is non NULL as
74 * well as its IGraphicBufferProducer */
Mathias Agopianc4905eb2013-02-15 16:34:04 -080075 static bool isValid(const sp<Surface>& surface) {
Mathias Agopianf25c5082013-02-15 14:59:09 -080076 return surface != NULL && surface->getIGraphicBufferProducer() != NULL;
77 }
78
Mathias Agopiane3c697f2013-02-14 17:11:02 -080079protected:
Mathias Agopiane3c697f2013-02-14 17:11:02 -080080 virtual ~Surface();
Mathias Agopian01b76682009-04-16 20:04:08 -070081
Mathias Agopian62185b72009-04-16 16:19:50 -070082private:
Mathias Agopian01b76682009-04-16 20:04:08 -070083 // can't be copied
Mathias Agopiane3c697f2013-02-14 17:11:02 -080084 Surface& operator = (const Surface& rhs);
85 Surface(const Surface& rhs);
Mathias Agopian01b76682009-04-16 20:04:08 -070086
Mathias Agopiane3c697f2013-02-14 17:11:02 -080087 // ANativeWindow hooks
88 static int hook_cancelBuffer(ANativeWindow* window,
89 ANativeWindowBuffer* buffer, int fenceFd);
90 static int hook_dequeueBuffer(ANativeWindow* window,
91 ANativeWindowBuffer** buffer, int* fenceFd);
92 static int hook_perform(ANativeWindow* window, int operation, ...);
93 static int hook_query(const ANativeWindow* window, int what, int* value);
94 static int hook_queueBuffer(ANativeWindow* window,
95 ANativeWindowBuffer* buffer, int fenceFd);
96 static int hook_setSwapInterval(ANativeWindow* window, int interval);
Mathias Agopian01b76682009-04-16 20:04:08 -070097
Mathias Agopiane3c697f2013-02-14 17:11:02 -080098 static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
99 ANativeWindowBuffer* buffer);
100 static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
101 ANativeWindowBuffer** buffer);
102 static int hook_lockBuffer_DEPRECATED(ANativeWindow* window,
103 ANativeWindowBuffer* buffer);
104 static int hook_queueBuffer_DEPRECATED(ANativeWindow* window,
105 ANativeWindowBuffer* buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700106
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800107 int dispatchConnect(va_list args);
108 int dispatchDisconnect(va_list args);
109 int dispatchSetBufferCount(va_list args);
110 int dispatchSetBuffersGeometry(va_list args);
111 int dispatchSetBuffersDimensions(va_list args);
112 int dispatchSetBuffersUserDimensions(va_list args);
113 int dispatchSetBuffersFormat(va_list args);
114 int dispatchSetScalingMode(va_list args);
115 int dispatchSetBuffersTransform(va_list args);
116 int dispatchSetBuffersTimestamp(va_list args);
117 int dispatchSetCrop(va_list args);
118 int dispatchSetPostTransformCrop(va_list args);
119 int dispatchSetUsage(va_list args);
120 int dispatchLock(va_list args);
121 int dispatchUnlockAndPost(va_list args);
Mathias Agopian01b76682009-04-16 20:04:08 -0700122
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800123protected:
124 virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd);
125 virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd);
126 virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd);
127 virtual int perform(int operation, va_list args);
128 virtual int query(int what, int* value) const;
129 virtual int setSwapInterval(int interval);
Mathias Agopian62185b72009-04-16 16:19:50 -0700130
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800131 virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer);
132
133 virtual int connect(int api);
134 virtual int disconnect(int api);
135 virtual int setBufferCount(int bufferCount);
136 virtual int setBuffersDimensions(int w, int h);
137 virtual int setBuffersUserDimensions(int w, int h);
138 virtual int setBuffersFormat(int format);
139 virtual int setScalingMode(int mode);
140 virtual int setBuffersTransform(int transform);
141 virtual int setBuffersTimestamp(int64_t timestamp);
142 virtual int setCrop(Rect const* rect);
143 virtual int setUsage(uint32_t reqUsage);
144
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145public:
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800146 virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
147 virtual int unlockAndPost();
148
149protected:
150 enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS };
151 enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
152
153private:
154 void freeAllBuffers();
155 int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
156
157 struct BufferSlot {
158 sp<GraphicBuffer> buffer;
159 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 };
161
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800162 // mSurfaceTexture is the interface to the surface texture server. All
163 // operations on the surface texture client ultimately translate into
164 // interactions with the server using this interface.
165 // TODO: rename to mBufferProducer
166 sp<IGraphicBufferProducer> mGraphicBufferProducer;
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700167
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800168 // mSlots stores the buffers that have been allocated for each buffer slot.
169 // It is initialized to null pointers, and gets filled in with the result of
170 // IGraphicBufferProducer::requestBuffer when the client dequeues a buffer from a
171 // slot that has not yet been used. The buffer allocated to a slot will also
172 // be replaced if the requested buffer usage or geometry differs from that
173 // of the buffer allocated to a slot.
174 BufferSlot mSlots[NUM_BUFFER_SLOTS];
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700175
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800176 // mReqWidth is the buffer width that will be requested at the next dequeue
177 // operation. It is initialized to 1.
178 uint32_t mReqWidth;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700179
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800180 // mReqHeight is the buffer height that will be requested at the next
181 // dequeue operation. It is initialized to 1.
182 uint32_t mReqHeight;
Mathias Agopian01b76682009-04-16 20:04:08 -0700183
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800184 // mReqFormat is the buffer pixel format that will be requested at the next
185 // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
186 uint32_t mReqFormat;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800188 // mReqUsage is the set of buffer usage flags that will be requested
189 // at the next deuque operation. It is initialized to 0.
190 uint32_t mReqUsage;
Mathias Agopianb2965332010-04-27 16:41:19 -0700191
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800192 // mTimestamp is the timestamp that will be used for the next buffer queue
193 // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
194 // a timestamp is auto-generated when queueBuffer is called.
195 int64_t mTimestamp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800197 // mCrop is the crop rectangle that will be used for the next buffer
198 // that gets queued. It is set by calling setCrop.
199 Rect mCrop;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800201 // mScalingMode is the scaling mode that will be used for the next
202 // buffers that get queued. It is set by calling setScalingMode.
203 int mScalingMode;
Mathias Agopianb2965332010-04-27 16:41:19 -0700204
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800205 // mTransform is the transform identifier that will be used for the next
206 // buffer that gets queued. It is set by calling setTransform.
207 uint32_t mTransform;
Mathias Agopianb2965332010-04-27 16:41:19 -0700208
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800209 // mDefaultWidth is default width of the buffers, regardless of the
210 // native_window_set_buffers_dimensions call.
211 uint32_t mDefaultWidth;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700212
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800213 // mDefaultHeight is default height of the buffers, regardless of the
214 // native_window_set_buffers_dimensions call.
215 uint32_t mDefaultHeight;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700216
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800217 // mUserWidth, if non-zero, is an application-specified override
218 // of mDefaultWidth. This is lower priority than the width set by
219 // native_window_set_buffers_dimensions.
220 uint32_t mUserWidth;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700221
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800222 // mUserHeight, if non-zero, is an application-specified override
223 // of mDefaultHeight. This is lower priority than the height set
224 // by native_window_set_buffers_dimensions.
225 uint32_t mUserHeight;
226
227 // mTransformHint is the transform probably applied to buffers of this
228 // window. this is only a hint, actual transform may differ.
229 uint32_t mTransformHint;
230
231 // mConsumerRunningBehind whether the consumer is running more than
232 // one buffer behind the producer.
233 mutable bool mConsumerRunningBehind;
234
235 // mMutex is the mutex used to prevent concurrent access to the member
236 // variables of Surface objects. It must be locked whenever the
237 // member variables are accessed.
238 mutable Mutex mMutex;
239
240 // must be used from the lock/unlock thread
241 sp<GraphicBuffer> mLockedBuffer;
242 sp<GraphicBuffer> mPostedBuffer;
243 bool mConnectedToCpu;
244
245 // must be accessed from lock/unlock thread only
246 Region mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247};
248
249}; // namespace android
250
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800251#endif // ANDROID_GUI_SURFACE_H