The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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_BASE_H |
| 18 | #define ANDROID_LAYER_BASE_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 23 | #include <EGL/egl.h> |
| 24 | #include <EGL/eglext.h> |
| 25 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 26 | #include <private/ui/SharedBufferStack.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <private/ui/LayerState.h> |
| 28 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 29 | #include <utils/RefBase.h> |
| 30 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include <ui/Region.h> |
| 32 | #include <ui/Overlay.h> |
| 33 | |
| 34 | #include <pixelflinger/pixelflinger.h> |
| 35 | |
| 36 | #include "Transform.h" |
| 37 | |
| 38 | namespace android { |
| 39 | |
| 40 | // --------------------------------------------------------------------------- |
| 41 | |
| 42 | class SurfaceFlinger; |
| 43 | class DisplayHardware; |
| 44 | class GraphicPlane; |
| 45 | class Client; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 46 | class SurfaceBuffer; |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 47 | class Buffer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
| 49 | // --------------------------------------------------------------------------- |
| 50 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 51 | class LayerBase : public RefBase |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | { |
| 53 | // poor man's dynamic_cast below |
| 54 | template<typename T> |
| 55 | struct getTypeInfoOfAnyType { |
| 56 | static uint32_t get() { return T::typeInfo; } |
| 57 | }; |
| 58 | |
| 59 | template<typename T> |
| 60 | struct getTypeInfoOfAnyType<T*> { |
| 61 | static uint32_t get() { return getTypeInfoOfAnyType<T>::get(); } |
| 62 | }; |
| 63 | |
| 64 | public: |
| 65 | static const uint32_t typeInfo; |
| 66 | static const char* const typeID; |
| 67 | virtual char const* getTypeID() const { return typeID; } |
| 68 | virtual uint32_t getTypeInfo() const { return typeInfo; } |
| 69 | |
| 70 | template<typename T> |
| 71 | static T dynamicCast(LayerBase* base) { |
| 72 | uint32_t mostDerivedInfo = base->getTypeInfo(); |
| 73 | uint32_t castToInfo = getTypeInfoOfAnyType<T>::get(); |
| 74 | if ((mostDerivedInfo & castToInfo) == castToInfo) |
| 75 | return static_cast<T>(base); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | LayerBase(SurfaceFlinger* flinger, DisplayID display); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | |
| 82 | DisplayID dpy; |
| 83 | mutable bool contentDirty; |
| 84 | Region visibleRegionScreen; |
| 85 | Region transparentRegionScreen; |
| 86 | Region coveredRegionScreen; |
| 87 | |
| 88 | struct State { |
| 89 | uint32_t w; |
| 90 | uint32_t h; |
| 91 | uint32_t z; |
| 92 | uint8_t alpha; |
| 93 | uint8_t flags; |
| 94 | uint8_t reserved[2]; |
| 95 | int32_t sequence; // changes when visible regions can change |
| 96 | uint32_t tint; |
| 97 | Transform transform; |
| 98 | Region transparentRegion; |
| 99 | }; |
| 100 | |
| 101 | // modify current state |
| 102 | bool setPosition(int32_t x, int32_t y); |
| 103 | bool setLayer(uint32_t z); |
| 104 | bool setSize(uint32_t w, uint32_t h); |
| 105 | bool setAlpha(uint8_t alpha); |
| 106 | bool setMatrix(const layer_state_t::matrix22_t& matrix); |
| 107 | bool setTransparentRegionHint(const Region& opaque); |
| 108 | bool setFlags(uint8_t flags, uint8_t mask); |
| 109 | |
Mathias Agopian | ba6be54 | 2009-09-29 22:32:36 -0700 | [diff] [blame^] | 110 | void commitTransaction(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | bool requestTransaction(); |
| 112 | void forceVisibilityTransaction(); |
| 113 | |
| 114 | uint32_t getTransactionFlags(uint32_t flags); |
| 115 | uint32_t setTransactionFlags(uint32_t flags); |
| 116 | |
| 117 | Rect visibleBounds() const; |
| 118 | void drawRegion(const Region& reg) const; |
| 119 | |
| 120 | void invalidate(); |
| 121 | |
| 122 | /** |
| 123 | * draw - performs some global clipping optimizations |
| 124 | * and calls onDraw(). |
| 125 | * Typically this method is not overridden, instead implement onDraw() |
| 126 | * to perform the actual drawing. |
| 127 | */ |
| 128 | virtual void draw(const Region& clip) const; |
| 129 | |
| 130 | /** |
| 131 | * onDraw - draws the surface. |
| 132 | */ |
| 133 | virtual void onDraw(const Region& clip) const = 0; |
| 134 | |
| 135 | /** |
| 136 | * initStates - called just after construction |
| 137 | */ |
| 138 | virtual void initStates(uint32_t w, uint32_t h, uint32_t flags); |
| 139 | |
| 140 | /** |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | * doTransaction - process the transaction. This is a good place to figure |
| 142 | * out which attributes of the surface have changed. |
| 143 | */ |
| 144 | virtual uint32_t doTransaction(uint32_t transactionFlags); |
| 145 | |
| 146 | /** |
| 147 | * setVisibleRegion - called to set the new visible region. This gives |
| 148 | * a chance to update the new visible region or record the fact it changed. |
| 149 | */ |
| 150 | virtual void setVisibleRegion(const Region& visibleRegion); |
| 151 | |
| 152 | /** |
| 153 | * setCoveredRegion - called when the covered region changes. The covered |
| 154 | * region correspond to any area of the surface that is covered |
| 155 | * (transparently or not) by another surface. |
| 156 | */ |
| 157 | virtual void setCoveredRegion(const Region& coveredRegion); |
| 158 | |
| 159 | /** |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | * validateVisibility - cache a bunch of things |
| 161 | */ |
| 162 | virtual void validateVisibility(const Transform& globalTransform); |
| 163 | |
| 164 | /** |
| 165 | * lockPageFlip - called each time the screen is redrawn and returns whether |
| 166 | * the visible regions need to be recomputed (this is a fairly heavy |
| 167 | * operation, so this should be set only if needed). Typically this is used |
| 168 | * to figure out if the content or size of a surface has changed. |
| 169 | */ |
| 170 | virtual void lockPageFlip(bool& recomputeVisibleRegions); |
| 171 | |
| 172 | /** |
| 173 | * unlockPageFlip - called each time the screen is redrawn. updates the |
| 174 | * final dirty region wrt the planeTransform. |
| 175 | * At this point, all visible regions, surface position and size, etc... are |
| 176 | * correct. |
| 177 | */ |
| 178 | virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion); |
| 179 | |
| 180 | /** |
| 181 | * finishPageFlip - called after all surfaces have drawn. |
| 182 | */ |
| 183 | virtual void finishPageFlip(); |
| 184 | |
| 185 | /** |
| 186 | * needsBlending - true if this surface needs blending |
| 187 | */ |
| 188 | virtual bool needsBlending() const { return false; } |
| 189 | |
| 190 | /** |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 191 | * needsDithering - true if this surface needs dithering |
| 192 | */ |
| 193 | virtual bool needsDithering() const { return false; } |
| 194 | |
| 195 | /** |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | * transformed -- true is this surface needs a to be transformed |
| 197 | */ |
| 198 | virtual bool transformed() const { return mTransformed; } |
| 199 | |
| 200 | /** |
| 201 | * isSecure - true if this surface is secure, that is if it prevents |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 202 | * screenshots or VNC servers. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | */ |
| 204 | virtual bool isSecure() const { return false; } |
| 205 | |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 206 | /** signal this layer that it's not needed any longer. called from the |
| 207 | * main thread */ |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 208 | virtual status_t ditch() { return NO_ERROR; } |
| 209 | |
| 210 | |
| 211 | |
| 212 | enum { // flags for doTransaction() |
| 213 | eVisibleRegion = 0x00000002, |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 214 | }; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | |
| 216 | |
| 217 | inline const State& drawingState() const { return mDrawingState; } |
| 218 | inline const State& currentState() const { return mCurrentState; } |
| 219 | inline State& currentState() { return mCurrentState; } |
| 220 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 221 | static int compareCurrentStateZ( |
| 222 | sp<LayerBase> const * layerA, |
| 223 | sp<LayerBase> const * layerB) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | return layerA[0]->currentState().z - layerB[0]->currentState().z; |
| 225 | } |
| 226 | |
| 227 | int32_t getOrientation() const { return mOrientation; } |
| 228 | int tx() const { return mLeft; } |
| 229 | int ty() const { return mTop; } |
| 230 | |
| 231 | protected: |
| 232 | const GraphicPlane& graphicPlane(int dpy) const; |
| 233 | GraphicPlane& graphicPlane(int dpy); |
| 234 | |
| 235 | GLuint createTexture() const; |
| 236 | |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 237 | struct Texture { |
| 238 | Texture() : name(-1U), width(0), height(0), |
| 239 | image(EGL_NO_IMAGE_KHR), transform(0), dirty(true) { } |
| 240 | GLuint name; |
| 241 | GLuint width; |
| 242 | GLuint height; |
| 243 | EGLImageKHR image; |
| 244 | uint32_t transform; |
| 245 | bool dirty; |
| 246 | }; |
Rebecca Schultz Zavin | 29aa74c | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 247 | |
| 248 | void clearWithOpenGL(const Region& clip, GLclampx r, GLclampx g, |
| 249 | GLclampx b, GLclampx alpha) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | void clearWithOpenGL(const Region& clip) const; |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 251 | void drawWithOpenGL(const Region& clip, const Texture& texture) const; |
| 252 | void loadTexture(Texture* texture, GLint textureName, |
| 253 | const Region& dirty, const GGLSurface& t) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 255 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 256 | sp<SurfaceFlinger> mFlinger; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | uint32_t mFlags; |
| 258 | |
| 259 | // cached during validateVisibility() |
| 260 | bool mTransformed; |
Mathias Agopian | a2fe0a2 | 2009-09-23 18:34:53 -0700 | [diff] [blame] | 261 | bool mUseLinearFiltering; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | int32_t mOrientation; |
| 263 | GLfixed mVertices[4][2]; |
| 264 | Rect mTransformedBounds; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | int mLeft; |
| 266 | int mTop; |
| 267 | |
| 268 | // these are protected by an external lock |
| 269 | State mCurrentState; |
| 270 | State mDrawingState; |
| 271 | volatile int32_t mTransactionFlags; |
| 272 | |
| 273 | // don't change, don't need a lock |
| 274 | bool mPremultipliedAlpha; |
| 275 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | // atomic |
| 277 | volatile int32_t mInvalidate; |
| 278 | |
| 279 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 280 | protected: |
| 281 | virtual ~LayerBase(); |
| 282 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | private: |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 284 | LayerBase(const LayerBase& rhs); |
| 285 | void validateTexture(GLint textureName) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | |
| 289 | // --------------------------------------------------------------------------- |
| 290 | |
| 291 | class LayerBaseClient : public LayerBase |
| 292 | { |
| 293 | public: |
| 294 | class Surface; |
| 295 | static const uint32_t typeInfo; |
| 296 | static const char* const typeID; |
| 297 | virtual char const* getTypeID() const { return typeID; } |
| 298 | virtual uint32_t getTypeInfo() const { return typeInfo; } |
| 299 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 300 | // lcblk is (almost) only accessed from the main SF thread, in the places |
| 301 | // where it's not, a reference to Client must be held |
| 302 | SharedBufferServer* lcblk; |
| 303 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | LayerBaseClient(SurfaceFlinger* flinger, DisplayID display, |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 305 | const sp<Client>& client, int32_t i); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | virtual ~LayerBaseClient(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 307 | virtual void onFirstRef(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 309 | const wp<Client> client; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 311 | inline uint32_t getIdentity() const { return mIdentity; } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | inline int32_t clientIndex() const { return mIndex; } |
| 313 | int32_t serverIndex() const; |
| 314 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 316 | sp<Surface> getSurface(); |
| 317 | virtual sp<Surface> createSurface() const; |
| 318 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 319 | virtual void onRemoved() { } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | |
| 321 | class Surface : public BnSurface |
| 322 | { |
| 323 | public: |
Mathias Agopian | 1c97d2e | 2009-08-19 17:46:26 -0700 | [diff] [blame] | 324 | int32_t getToken() const { return mToken; } |
| 325 | int32_t getIdentity() const { return mIdentity; } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 326 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 327 | protected: |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 328 | Surface(const sp<SurfaceFlinger>& flinger, |
| 329 | SurfaceID id, int identity, |
| 330 | const sp<LayerBaseClient>& owner); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 331 | virtual ~Surface(); |
| 332 | virtual status_t onTransact(uint32_t code, const Parcel& data, |
| 333 | Parcel* reply, uint32_t flags); |
| 334 | sp<LayerBaseClient> getOwner() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | |
| 336 | private: |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 337 | virtual sp<SurfaceBuffer> requestBuffer(int index, int usage); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 338 | virtual status_t registerBuffers(const ISurface::BufferHeap& buffers); |
| 339 | virtual void postBuffer(ssize_t offset); |
| 340 | virtual void unregisterBuffers(); |
| 341 | virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h, |
| 342 | int32_t format); |
| 343 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 344 | protected: |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 345 | friend class LayerBaseClient; |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 346 | sp<SurfaceFlinger> mFlinger; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 347 | int32_t mToken; |
| 348 | int32_t mIdentity; |
| 349 | wp<LayerBaseClient> mOwner; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | }; |
| 351 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 352 | friend class Surface; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 354 | private: |
| 355 | int32_t mIndex; |
| 356 | mutable Mutex mLock; |
| 357 | mutable wp<Surface> mClientSurface; |
Mathias Agopian | 2e12324 | 2009-06-23 20:06:46 -0700 | [diff] [blame] | 358 | // only read |
| 359 | const uint32_t mIdentity; |
| 360 | static int32_t sIdentity; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | // --------------------------------------------------------------------------- |
| 364 | |
| 365 | }; // namespace android |
| 366 | |
| 367 | #endif // ANDROID_LAYER_BASE_H |