blob: aa7fe48a5292c21c99871a42b2093a6e653a5efc [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 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
17#ifndef ANDROID_GUI_SURFACETEXTURECLIENT_H
18#define ANDROID_GUI_SURFACETEXTURECLIENT_H
19
20#include <gui/ISurfaceTexture.h>
21#include <gui/SurfaceTexture.h>
22
Mathias Agopian5f2165f2012-02-24 18:25:41 -080023#include <ui/ANativeObjectBase.h>
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070024#include <ui/Region.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080025
26#include <utils/RefBase.h>
27#include <utils/threads.h>
28
29namespace android {
30
Mathias Agopian7a042bf2011-04-11 21:19:55 -070031class Surface;
32
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080033class SurfaceTextureClient
Mathias Agopian5f2165f2012-02-24 18:25:41 -080034 : public ANativeObjectBase<ANativeWindow, SurfaceTextureClient, RefBase>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080035{
36public:
37 SurfaceTextureClient(const sp<ISurfaceTexture>& surfaceTexture);
38
Jamie Gennisbae774e2011-03-14 15:08:53 -070039 sp<ISurfaceTexture> getISurfaceTexture() const;
40
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070041protected:
42 SurfaceTextureClient();
Mathias Agopiana36bcd52011-11-17 18:46:09 -080043 virtual ~SurfaceTextureClient();
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070044 void setISurfaceTexture(const sp<ISurfaceTexture>& surfaceTexture);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080045
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070046private:
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080047 // can't be copied
48 SurfaceTextureClient& operator = (const SurfaceTextureClient& rhs);
49 SurfaceTextureClient(const SurfaceTextureClient& rhs);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070050 void init();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080051
52 // ANativeWindow hooks
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070053 static int hook_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
54 static int hook_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer);
55 static int hook_lockBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
56 static int hook_perform(ANativeWindow* window, int operation, ...);
57 static int hook_query(const ANativeWindow* window, int what, int* value);
58 static int hook_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
59 static int hook_setSwapInterval(ANativeWindow* window, int interval);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080060
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080061 int dispatchConnect(va_list args);
62 int dispatchDisconnect(va_list args);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080063 int dispatchSetBufferCount(va_list args);
64 int dispatchSetBuffersGeometry(va_list args);
Jamie Gennisbee205f2011-07-01 13:12:07 -070065 int dispatchSetBuffersDimensions(va_list args);
66 int dispatchSetBuffersFormat(va_list args);
Mathias Agopian7734ebf2011-07-13 15:24:42 -070067 int dispatchSetScalingMode(va_list args);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080068 int dispatchSetBuffersTransform(va_list args);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080069 int dispatchSetBuffersTimestamp(va_list args);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -080070 int dispatchSetCrop(va_list args);
71 int dispatchSetUsage(va_list args);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070072 int dispatchLock(va_list args);
73 int dispatchUnlockAndPost(va_list args);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080074
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070075protected:
76 virtual int cancelBuffer(ANativeWindowBuffer* buffer);
77 virtual int dequeueBuffer(ANativeWindowBuffer** buffer);
78 virtual int lockBuffer(ANativeWindowBuffer* buffer);
79 virtual int perform(int operation, va_list args);
80 virtual int query(int what, int* value) const;
81 virtual int queueBuffer(ANativeWindowBuffer* buffer);
82 virtual int setSwapInterval(int interval);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080083
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070084 virtual int connect(int api);
85 virtual int disconnect(int api);
86 virtual int setBufferCount(int bufferCount);
87 virtual int setBuffersDimensions(int w, int h);
88 virtual int setBuffersFormat(int format);
Mathias Agopian7734ebf2011-07-13 15:24:42 -070089 virtual int setScalingMode(int mode);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070090 virtual int setBuffersTransform(int transform);
91 virtual int setBuffersTimestamp(int64_t timestamp);
92 virtual int setCrop(Rect const* rect);
93 virtual int setUsage(uint32_t reqUsage);
94 virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
95 virtual int unlockAndPost();
Mathias Agopian7a042bf2011-04-11 21:19:55 -070096
Jamie Gennis9d4d6c12011-02-27 14:10:20 -080097 enum { MIN_UNDEQUEUED_BUFFERS = SurfaceTexture::MIN_UNDEQUEUED_BUFFERS };
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080098 enum { NUM_BUFFER_SLOTS = SurfaceTexture::NUM_BUFFER_SLOTS };
99 enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
100
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700101private:
102 void freeAllBuffers();
103 int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
104
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800105 // mSurfaceTexture is the interface to the surface texture server. All
106 // operations on the surface texture client ultimately translate into
107 // interactions with the server using this interface.
108 sp<ISurfaceTexture> mSurfaceTexture;
109
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800110 // mSlots stores the buffers that have been allocated for each buffer slot.
111 // It is initialized to null pointers, and gets filled in with the result of
112 // ISurfaceTexture::requestBuffer when the client dequeues a buffer from a
113 // slot that has not yet been used. The buffer allocated to a slot will also
114 // be replaced if the requested buffer usage or geometry differs from that
115 // of the buffer allocated to a slot.
116 sp<GraphicBuffer> mSlots[NUM_BUFFER_SLOTS];
117
118 // mReqWidth is the buffer width that will be requested at the next dequeue
119 // operation. It is initialized to 1.
120 uint32_t mReqWidth;
121
122 // mReqHeight is the buffer height that will be requested at the next deuque
123 // operation. It is initialized to 1.
124 uint32_t mReqHeight;
125
126 // mReqFormat is the buffer pixel format that will be requested at the next
127 // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
128 uint32_t mReqFormat;
129
130 // mReqUsage is the set of buffer usage flags that will be requested
131 // at the next deuque operation. It is initialized to 0.
132 uint32_t mReqUsage;
133
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800134 // mTimestamp is the timestamp that will be used for the next buffer queue
135 // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
136 // a timestamp is auto-generated when queueBuffer is called.
137 int64_t mTimestamp;
138
Mathias Agopian97c602c2011-07-19 15:24:46 -0700139 // mDefaultWidth is default width of the window, regardless of the
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700140 // native_window_set_buffers_dimensions call
Mathias Agopian97c602c2011-07-19 15:24:46 -0700141 uint32_t mDefaultWidth;
142
143 // mDefaultHeight is default width of the window, regardless of the
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700144 // native_window_set_buffers_dimensions call
Mathias Agopian97c602c2011-07-19 15:24:46 -0700145 uint32_t mDefaultHeight;
146
147 // mTransformHint is the transform probably applied to buffers of this
148 // window. this is only a hint, actual transform may differ.
149 uint32_t mTransformHint;
150
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800151 // mMutex is the mutex used to prevent concurrent access to the member
152 // variables of SurfaceTexture objects. It must be locked whenever the
153 // member variables are accessed.
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700154 mutable Mutex mMutex;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700155
156 // must be used from the lock/unlock thread
157 sp<GraphicBuffer> mLockedBuffer;
158 sp<GraphicBuffer> mPostedBuffer;
159 mutable Region mOldDirtyRegion;
160 bool mConnectedToCpu;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800161};
162
163}; // namespace android
164
165#endif // ANDROID_GUI_SURFACETEXTURECLIENT_H