blob: 26fb6c67dd7aaf888e95bd29dac81a434288316b [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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041class FreezeLock;
42
43// ---------------------------------------------------------------------------
44
45class Layer : public LayerBaseClient
46{
Mathias Agopiand606de62010-05-10 20:06:11 -070047public:
48 // lcblk is (almost) only accessed from the main SF thread, in the places
49 // where it's not, a reference to Client must be held
50 SharedBufferServer* lcblk;
51
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052 Layer(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -070053 const sp<Client>& client, int32_t i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
55 virtual ~Layer();
56
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 Agopiancbb288b2009-09-07 16:32:45 -070060 void setDrawingSize(uint32_t w, uint32_t h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
62 virtual void onDraw(const Region& clip) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063 virtual uint32_t doTransaction(uint32_t transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064 virtual void lockPageFlip(bool& recomputeVisibleRegions);
65 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
66 virtual void finishPageFlip();
67 virtual bool needsBlending() const { return mNeedsBlending; }
Mathias Agopian401c2572009-09-23 19:16:27 -070068 virtual bool needsDithering() const { return mNeedsDithering; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 virtual bool isSecure() const { return mSecure; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070 virtual sp<Surface> createSurface() const;
Mathias Agopian9a112062009-04-17 19:36:26 -070071 virtual status_t ditch();
Mathias Agopiand606de62010-05-10 20:06:11 -070072 virtual void onRemoved();
Mathias Agopiancbb288b2009-09-07 16:32:45 -070073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 // only for debugging
Mathias Agopiand606de62010-05-10 20:06:11 -070075 inline sp<GraphicBuffer> getBuffer(int i) const { return mBufferManager.getBuffer(i); }
Mathias Agopiancbb288b2009-09-07 16:32:45 -070076 // only for debugging
77 inline const sp<FreezeLock>& getFreezeLock() const { return mFreezeLock; }
78 // only for debugging
79 inline PixelFormat pixelFormat() const { return mFormat; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080
Mathias Agopian1b5e1022010-04-20 17:55:49 -070081 virtual const char* getTypeId() const { return "Layer"; }
82
83protected:
84 virtual void dump(String8& result, char* scratch, size_t size) const;
85
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 void reloadTexture(const Region& dirty);
88
Mathias Agopian3330b202009-10-05 17:07:12 -070089 uint32_t getEffectiveUsage(uint32_t usage) const;
90
91 sp<GraphicBuffer> requestBuffer(int index, int usage);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070092 void destroy();
93
Mathias Agopiancbb288b2009-09-07 16:32:45 -070094 class SurfaceLayer : public LayerBaseClient::Surface {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070095 public:
Mathias Agopiancbb288b2009-09-07 16:32:45 -070096 SurfaceLayer(const sp<SurfaceFlinger>& flinger,
97 SurfaceID id, const sp<Layer>& owner);
98 ~SurfaceLayer();
Mathias Agopian076b1cc2009-04-10 14:24:30 -070099 private:
Mathias Agopian3330b202009-10-05 17:07:12 -0700100 virtual sp<GraphicBuffer> requestBuffer(int index, int usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101 sp<Layer> getOwner() const {
102 return static_cast<Layer*>(Surface::getOwner().get());
103 }
104 };
105 friend class SurfaceLayer;
106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 sp<Surface> mSurface;
108
109 bool mSecure;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 int32_t mFrontBufferIndex;
111 bool mNeedsBlending;
Mathias Agopian401c2572009-09-23 19:16:27 -0700112 bool mNeedsDithering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 Region mPostedDirtyRegion;
114 sp<FreezeLock> mFreezeLock;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700115 PixelFormat mFormat;
Mathias Agopiand606de62010-05-10 20:06:11 -0700116
117 class BufferManager {
118 static const size_t NUM_BUFFERS = 2;
119 struct BufferData {
120 sp<GraphicBuffer> buffer;
121 Texture texture;
122 };
123 mutable Mutex mLock;
124 BufferData mBufferData[NUM_BUFFERS];
125 Texture mFailoverTexture;
126 TextureManager& mTextureManager;
127 ssize_t mActiveBuffer;
128 bool mFailover;
129 static status_t destroyTexture(Texture* tex, EGLDisplay dpy);
130
131 public:
132 BufferManager(TextureManager& tm);
133
134 size_t getBufferCount() const;
135
136 // detach/attach buffer from/to given index
137 sp<GraphicBuffer> detachBuffer(size_t index);
138 status_t attachBuffer(size_t index, const sp<GraphicBuffer>& buffer);
139
140 // ----------------------------------------------
141 // must be called from GL thread
142
143 // set/get active buffer index
144 status_t setActiveBufferIndex(size_t index);
145 size_t getActiveBufferIndex() const;
146
147 // return the active buffer
148 sp<GraphicBuffer> getActiveBuffer() const;
149
150 // return the active texture (or fail-over)
151 Texture getActiveTexture() const;
152
153 // frees resources associated with all buffers
154 status_t destroy(EGLDisplay dpy);
155
156 // load bitmap data into the active buffer
157 status_t loadTexture(const Region& dirty, const GGLSurface& t);
158
159 // make active buffer an EGLImage if needed
160 status_t initEglImage(EGLDisplay dpy,
161 const sp<GraphicBuffer>& buffer);
162
163 // ----------------------------------------------
164 // only for debugging
165 sp<GraphicBuffer> getBuffer(size_t index) const;
166 };
167
168 TextureManager mTextureManager;
169 BufferManager mBufferManager;
170
171 mutable Mutex mLock;
172 uint32_t mWidth;
173 uint32_t mHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174};
175
176// ---------------------------------------------------------------------------
177
178}; // namespace android
179
180#endif // ANDROID_LAYER_H