blob: d396ecfc36cc2b9be5e2ac67a5e2f797fddd1c4a [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
40class Client;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070041class UserClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042class FreezeLock;
43
44// ---------------------------------------------------------------------------
45
46class Layer : public LayerBaseClient
47{
Mathias Agopiand606de62010-05-10 20:06:11 -070048public:
Mathias Agopianb7e930d2010-06-01 15:12:58 -070049 Layer(SurfaceFlinger* flinger, DisplayID display,
50 const sp<Client>& client);
Mathias Agopiand606de62010-05-10 20:06:11 -070051
Mathias Agopianb7e930d2010-06-01 15:12:58 -070052 virtual ~Layer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopianb7e930d2010-06-01 15:12:58 -070054 virtual const char* getTypeId() const { return "Layer"; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopianb7e930d2010-06-01 15:12:58 -070056 // the this layer's size and format
Mathias Agopiancbb288b2009-09-07 16:32:45 -070057 status_t setBuffers(uint32_t w, uint32_t h,
58 PixelFormat format, uint32_t flags=0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
Mathias Agopianb7e930d2010-06-01 15:12:58 -070060 // associate a UserClient to this Layer
61 status_t setToken(const sp<UserClient>& uc, SharedClient* sc, int32_t idx);
62 int32_t getToken() const;
63
64 // Set this Layer's buffers size
Mathias Agopiana138f892010-05-21 17:24:35 -070065 void setBufferSize(uint32_t w, uint32_t h);
66 bool isFixedSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
Mathias Agopianb7e930d2010-06-01 15:12:58 -070068 // LayerBase interface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 virtual void onDraw(const Region& clip) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 virtual uint32_t doTransaction(uint32_t transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071 virtual void lockPageFlip(bool& recomputeVisibleRegions);
72 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
73 virtual void finishPageFlip();
74 virtual bool needsBlending() const { return mNeedsBlending; }
Mathias Agopian401c2572009-09-23 19:16:27 -070075 virtual bool needsDithering() const { return mNeedsDithering; }
Mathias Agopiana7f66922010-05-26 22:08:52 -070076 virtual bool needsFiltering() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 virtual bool isSecure() const { return mSecure; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078 virtual sp<Surface> createSurface() const;
Mathias Agopian9a112062009-04-17 19:36:26 -070079 virtual status_t ditch();
Mathias Agopiand606de62010-05-10 20:06:11 -070080 virtual void onRemoved();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081
Mathias Agopianb7e930d2010-06-01 15:12:58 -070082 // only for debugging
83 inline sp<GraphicBuffer> getBuffer(int i) const {
84 return mBufferManager.getBuffer(i); }
85 // only for debugging
86 inline const sp<FreezeLock>& getFreezeLock() const {
87 return mFreezeLock; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -070088
89protected:
90 virtual void dump(String8& result, char* scratch, size_t size) const;
91
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 void reloadTexture(const Region& dirty);
Mathias Agopian3330b202009-10-05 17:07:12 -070094 uint32_t getEffectiveUsage(uint32_t usage) const;
Mathias Agopiana138f892010-05-21 17:24:35 -070095 sp<GraphicBuffer> requestBuffer(int bufferIdx,
96 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -070097 status_t setBufferCount(int bufferCount);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070098
Mathias Agopianb7e930d2010-06-01 15:12:58 -070099 // -----------------------------------------------------------------------
100
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700101 class SurfaceLayer : public LayerBaseClient::Surface {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700102 public:
Mathias Agopian96f08192010-06-02 23:28:45 -0700103 SurfaceLayer(const sp<SurfaceFlinger>& flinger, const sp<Layer>& owner);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700104 ~SurfaceLayer();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700105 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700106 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
107 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700108 virtual status_t setBufferCount(int bufferCount);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700109 sp<Layer> getOwner() const {
110 return static_cast<Layer*>(Surface::getOwner().get());
111 }
112 };
113 friend class SurfaceLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700115 // -----------------------------------------------------------------------
Mathias Agopiand606de62010-05-10 20:06:11 -0700116
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700117 class ClientRef {
118 ClientRef(const ClientRef& rhs);
119 ClientRef& operator = (const ClientRef& rhs);
120 mutable Mutex mLock;
121 // binder thread, page-flip thread
122 SharedBufferServer* lcblk;
123 wp<UserClient> mUserClient;
124 int32_t mToken;
125 public:
126 ClientRef();
127 ~ClientRef();
128 int32_t getToken() const;
129 status_t setToken(const sp<UserClient>& uc,
130 SharedBufferServer* sharedClient, int32_t token);
131 sp<UserClient> getUserClientUnsafe() const;
132 class Access {
133 Access(const Access& rhs);
134 Access& operator = (const Access& rhs);
135 sp<UserClient> mUserClientStrongRef;
136 SharedBufferServer* lcblk;
137 public:
138 Access(const ClientRef& ref);
139 inline SharedBufferServer* get() const { return lcblk; }
140 };
141 friend class Access;
142 };
Mathias Agopiand606de62010-05-10 20:06:11 -0700143
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700144 // -----------------------------------------------------------------------
Mathias Agopiand606de62010-05-10 20:06:11 -0700145
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700146 class BufferManager {
147 static const size_t NUM_BUFFERS = 2;
148 struct BufferData {
149 sp<GraphicBuffer> buffer;
150 Image texture;
151 };
152 // this lock protect mBufferData[].buffer but since there
153 // is very little contention, we have only one like for
154 // the whole array, we also use it to protect mNumBuffers.
155 mutable Mutex mLock;
156 BufferData mBufferData[SharedBufferStack::NUM_BUFFER_MAX];
157 size_t mNumBuffers;
158 Texture mFailoverTexture;
159 TextureManager& mTextureManager;
160 ssize_t mActiveBuffer;
161 bool mFailover;
162 static status_t destroyTexture(Image* tex, EGLDisplay dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700163
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700164 public:
165 static size_t getDefaultBufferCount() { return NUM_BUFFERS; }
166 BufferManager(TextureManager& tm);
167 ~BufferManager();
Mathias Agopianbb641242010-05-18 17:06:55 -0700168
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700169 // detach/attach buffer from/to given index
170 sp<GraphicBuffer> detachBuffer(size_t index);
171 status_t attachBuffer(size_t index, const sp<GraphicBuffer>& buffer);
172 // resize the number of active buffers
173 status_t resize(size_t size);
Mathias Agopiand606de62010-05-10 20:06:11 -0700174
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700175 // ----------------------------------------------
176 // must be called from GL thread
Mathias Agopiand606de62010-05-10 20:06:11 -0700177
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700178 // set/get active buffer index
179 status_t setActiveBufferIndex(size_t index);
180 size_t getActiveBufferIndex() const;
181 // return the active buffer
182 sp<GraphicBuffer> getActiveBuffer() const;
183 // return the active texture (or fail-over)
184 Texture getActiveTexture() const;
185 // frees resources associated with all buffers
186 status_t destroy(EGLDisplay dpy);
187 // load bitmap data into the active buffer
188 status_t loadTexture(const Region& dirty, const GGLSurface& t);
189 // make active buffer an EGLImage if needed
190 status_t initEglImage(EGLDisplay dpy,
191 const sp<GraphicBuffer>& buffer);
Mathias Agopiand606de62010-05-10 20:06:11 -0700192
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700193 // ----------------------------------------------
194 // only for debugging
195 sp<GraphicBuffer> getBuffer(size_t index) const;
196 };
Mathias Agopiand606de62010-05-10 20:06:11 -0700197
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700198 // -----------------------------------------------------------------------
Mathias Agopiand606de62010-05-10 20:06:11 -0700199
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700200 // thread-safe
201 ClientRef mUserClientRef;
Mathias Agopiand606de62010-05-10 20:06:11 -0700202
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700203 // constants
204 sp<Surface> mSurface;
205 PixelFormat mFormat;
206 bool mNeedsBlending;
207 bool mNeedsDithering;
Mathias Agopiand606de62010-05-10 20:06:11 -0700208
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700209 // page-flip thread (currently main thread)
210 bool mSecure;
211 Region mPostedDirtyRegion;
Mathias Agopiand606de62010-05-10 20:06:11 -0700212
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700213 // page-flip thread and transaction thread (currently main thread)
214 sp<FreezeLock> mFreezeLock;
Mathias Agopiand606de62010-05-10 20:06:11 -0700215
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700216 // see threading usage in declaration
217 TextureManager mTextureManager;
218 BufferManager mBufferManager;
Mathias Agopian96f08192010-06-02 23:28:45 -0700219
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700220 // binder thread, transaction thread
221 mutable Mutex mLock;
222 uint32_t mWidth;
223 uint32_t mHeight;
224 uint32_t mReqWidth;
225 uint32_t mReqHeight;
226 uint32_t mReqFormat;
227 bool mFixedSize;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228};
229
230// ---------------------------------------------------------------------------
231
232}; // namespace android
233
234#endif // ANDROID_LAYER_H