blob: 753823125b5cbd35f88727940c4de7bf0cdc2a39 [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
17#ifndef ANDROID_LAYER_H
18#define ANDROID_LAYER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
Mathias Agopian3330b202009-10-05 17:07:12 -070023#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <pixelflinger/pixelflinger.h>
26
Mathias Agopian076b1cc2009-04-10 14:24:30 -070027#include <EGL/egl.h>
28#include <EGL/eglext.h>
29#include <GLES/gl.h>
30#include <GLES/glext.h>
31
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include "LayerBase.h"
33#include "Transform.h"
Mathias Agopiand606de62010-05-10 20:06:11 -070034#include "TextureManager.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
36namespace android {
37
38// ---------------------------------------------------------------------------
39
Andreas Hubere049a952010-06-25 09:25:19 -070040class FreezeLock;
Mathias Agopian1f7bec62010-06-25 18:02:21 -070041class Client;
42class GLExtensions;
43class UserClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
45// ---------------------------------------------------------------------------
46
47class Layer : public LayerBaseClient
48{
Mathias Agopiand606de62010-05-10 20:06:11 -070049public:
Mathias Agopianb7e930d2010-06-01 15:12:58 -070050 Layer(SurfaceFlinger* flinger, DisplayID display,
51 const sp<Client>& client);
Mathias Agopiand606de62010-05-10 20:06:11 -070052
Mathias Agopianb7e930d2010-06-01 15:12:58 -070053 virtual ~Layer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopianb7e930d2010-06-01 15:12:58 -070055 virtual const char* getTypeId() const { return "Layer"; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopianb7e930d2010-06-01 15:12:58 -070057 // the this layer's size and format
Mathias Agopiancbb288b2009-09-07 16:32:45 -070058 status_t setBuffers(uint32_t w, uint32_t h,
59 PixelFormat format, uint32_t flags=0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
Mathias Agopianb7e930d2010-06-01 15:12:58 -070061 // associate a UserClient to this Layer
62 status_t setToken(const sp<UserClient>& uc, SharedClient* sc, int32_t idx);
63 int32_t getToken() const;
Mathias Agopian579b3f82010-06-08 19:54:15 -070064 sp<UserClient> getClient() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070065
66 // Set this Layer's buffers size
Mathias Agopiana138f892010-05-21 17:24:35 -070067 void setBufferSize(uint32_t w, uint32_t h);
68 bool isFixedSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
Mathias Agopianb7e930d2010-06-01 15:12:58 -070070 // LayerBase interface
Mathias Agopiana350ff92010-08-10 17:14:02 -070071 virtual void setGeometry(hwc_layer_t* hwcl);
72 virtual void setPerFrameData(hwc_layer_t* hwcl);
Mathias Agopian74c40c02010-09-29 13:02:36 -070073 virtual void drawForSreenShot() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 virtual void onDraw(const Region& clip) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 virtual uint32_t doTransaction(uint32_t transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076 virtual void lockPageFlip(bool& recomputeVisibleRegions);
77 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
Eric Hassoldac45e6b2011-02-10 14:41:26 -080078 virtual bool needsBlending(const sp<GraphicBuffer>& buffer) const;
79 virtual bool needsBlending() const;
Mathias Agopian401c2572009-09-23 19:16:27 -070080 virtual bool needsDithering() const { return mNeedsDithering; }
Mathias Agopiana7f66922010-05-26 22:08:52 -070081 virtual bool needsFiltering() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 virtual bool isSecure() const { return mSecure; }
Glenn Kasten16f04532011-01-19 15:27:27 -080083 virtual bool isProtectedByApp() const { return mProtectedByApp; }
84 virtual bool isProtectedByDRM() const { return mProtectedByDRM; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 virtual sp<Surface> createSurface() const;
Mathias Agopian9a112062009-04-17 19:36:26 -070086 virtual status_t ditch();
Mathias Agopiand606de62010-05-10 20:06:11 -070087 virtual void onRemoved();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088
Mathias Agopianb7e930d2010-06-01 15:12:58 -070089 // only for debugging
90 inline sp<GraphicBuffer> getBuffer(int i) const {
91 return mBufferManager.getBuffer(i); }
92 // only for debugging
93 inline const sp<FreezeLock>& getFreezeLock() const {
94 return mFreezeLock; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -070095
96protected:
97 virtual void dump(String8& result, char* scratch, size_t size) const;
98
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 void reloadTexture(const Region& dirty);
Mathias Agopian3330b202009-10-05 17:07:12 -0700101 uint32_t getEffectiveUsage(uint32_t usage) const;
Mathias Agopiana138f892010-05-21 17:24:35 -0700102 sp<GraphicBuffer> requestBuffer(int bufferIdx,
103 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700104 status_t setBufferCount(int bufferCount);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700105
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700106 // -----------------------------------------------------------------------
107
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700108 class SurfaceLayer : public LayerBaseClient::Surface {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700109 public:
Mathias Agopian96f08192010-06-02 23:28:45 -0700110 SurfaceLayer(const sp<SurfaceFlinger>& flinger, const sp<Layer>& owner);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700111 ~SurfaceLayer();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700112 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700113 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
114 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700115 virtual status_t setBufferCount(int bufferCount);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700116 sp<Layer> getOwner() const {
117 return static_cast<Layer*>(Surface::getOwner().get());
118 }
119 };
120 friend class SurfaceLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700122 // -----------------------------------------------------------------------
Mathias Agopiand606de62010-05-10 20:06:11 -0700123
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700124 class ClientRef {
125 ClientRef(const ClientRef& rhs);
126 ClientRef& operator = (const ClientRef& rhs);
127 mutable Mutex mLock;
128 // binder thread, page-flip thread
Mathias Agopian579b3f82010-06-08 19:54:15 -0700129 sp<SharedBufferServer> mControlBlock;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700130 wp<UserClient> mUserClient;
131 int32_t mToken;
132 public:
133 ClientRef();
134 ~ClientRef();
135 int32_t getToken() const;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700136 sp<UserClient> getClient() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700137 status_t setToken(const sp<UserClient>& uc,
Mathias Agopian579b3f82010-06-08 19:54:15 -0700138 const sp<SharedBufferServer>& sharedClient, int32_t token);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700139 sp<UserClient> getUserClientUnsafe() const;
140 class Access {
141 Access(const Access& rhs);
142 Access& operator = (const Access& rhs);
143 sp<UserClient> mUserClientStrongRef;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700144 sp<SharedBufferServer> mControlBlock;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700145 public:
146 Access(const ClientRef& ref);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700147 ~Access();
148 inline SharedBufferServer* get() const { return mControlBlock.get(); }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700149 };
150 friend class Access;
151 };
Mathias Agopiand606de62010-05-10 20:06:11 -0700152
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700153 // -----------------------------------------------------------------------
Mathias Agopiand606de62010-05-10 20:06:11 -0700154
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700155 class BufferManager {
156 static const size_t NUM_BUFFERS = 2;
157 struct BufferData {
158 sp<GraphicBuffer> buffer;
159 Image texture;
160 };
161 // this lock protect mBufferData[].buffer but since there
162 // is very little contention, we have only one like for
163 // the whole array, we also use it to protect mNumBuffers.
164 mutable Mutex mLock;
165 BufferData mBufferData[SharedBufferStack::NUM_BUFFER_MAX];
166 size_t mNumBuffers;
167 Texture mFailoverTexture;
168 TextureManager& mTextureManager;
Mathias Agopian420a2832010-12-14 20:30:37 -0800169 ssize_t mActiveBufferIndex;
170 sp<GraphicBuffer> mActiveBuffer;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700171 bool mFailover;
172 static status_t destroyTexture(Image* tex, EGLDisplay dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700173
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700174 public:
175 static size_t getDefaultBufferCount() { return NUM_BUFFERS; }
176 BufferManager(TextureManager& tm);
177 ~BufferManager();
Mathias Agopianbb641242010-05-18 17:06:55 -0700178
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700179 // detach/attach buffer from/to given index
180 sp<GraphicBuffer> detachBuffer(size_t index);
181 status_t attachBuffer(size_t index, const sp<GraphicBuffer>& buffer);
182 // resize the number of active buffers
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700183 status_t resize(size_t size, const sp<SurfaceFlinger>& flinger,
184 EGLDisplay dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700185
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700186 // ----------------------------------------------
187 // must be called from GL thread
Mathias Agopiand606de62010-05-10 20:06:11 -0700188
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700189 // set/get active buffer index
190 status_t setActiveBufferIndex(size_t index);
191 size_t getActiveBufferIndex() const;
192 // return the active buffer
193 sp<GraphicBuffer> getActiveBuffer() const;
Mathias Agopianda9584d2010-12-13 18:51:59 -0800194 // return wether we have an active buffer
195 bool hasActiveBuffer() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700196 // return the active texture (or fail-over)
197 Texture getActiveTexture() const;
198 // frees resources associated with all buffers
199 status_t destroy(EGLDisplay dpy);
200 // load bitmap data into the active buffer
201 status_t loadTexture(const Region& dirty, const GGLSurface& t);
202 // make active buffer an EGLImage if needed
203 status_t initEglImage(EGLDisplay dpy,
204 const sp<GraphicBuffer>& buffer);
Mathias Agopiand606de62010-05-10 20:06:11 -0700205
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700206 // ----------------------------------------------
207 // only for debugging
208 sp<GraphicBuffer> getBuffer(size_t index) const;
209 };
Mathias Agopiand606de62010-05-10 20:06:11 -0700210
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700211 // -----------------------------------------------------------------------
Mathias Agopiand606de62010-05-10 20:06:11 -0700212
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700213 // thread-safe
214 ClientRef mUserClientRef;
Mathias Agopiand606de62010-05-10 20:06:11 -0700215
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700216 // constants
217 sp<Surface> mSurface;
218 PixelFormat mFormat;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700219 const GLExtensions& mGLExtensions;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700220 bool mNeedsBlending;
221 bool mNeedsDithering;
Mathias Agopiand606de62010-05-10 20:06:11 -0700222
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700223 // page-flip thread (currently main thread)
Glenn Kasten16f04532011-01-19 15:27:27 -0800224 bool mSecure; // no screenshots
225 bool mProtectedByApp; // application requires protected path to external sink
226 bool mProtectedByDRM; // DRM agent requires protected path to external sink
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700227 Region mPostedDirtyRegion;
Mathias Agopiand606de62010-05-10 20:06:11 -0700228
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700229 // page-flip thread and transaction thread (currently main thread)
230 sp<FreezeLock> mFreezeLock;
Mathias Agopiand606de62010-05-10 20:06:11 -0700231
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700232 // see threading usage in declaration
233 TextureManager mTextureManager;
234 BufferManager mBufferManager;
Mathias Agopian96f08192010-06-02 23:28:45 -0700235
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700236 // binder thread, transaction thread
237 mutable Mutex mLock;
238 uint32_t mWidth;
239 uint32_t mHeight;
240 uint32_t mReqWidth;
241 uint32_t mReqHeight;
242 uint32_t mReqFormat;
Mathias Agopian733189d2010-12-02 21:32:29 -0800243 bool mNeedsScaling;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700244 bool mFixedSize;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245};
246
247// ---------------------------------------------------------------------------
248
249}; // namespace android
250
251#endif // ANDROID_LAYER_H