Home | History | Annotate | Download | only in surfaceflinger
      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_SF_SURFACE_H
     18 #define ANDROID_SF_SURFACE_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <utils/KeyedVector.h>
     24 #include <utils/RefBase.h>
     25 #include <utils/threads.h>
     26 
     27 #include <ui/PixelFormat.h>
     28 #include <ui/Region.h>
     29 #include <ui/egl/android_natives.h>
     30 
     31 #include <gui/SurfaceTextureClient.h>
     32 
     33 #include <surfaceflinger/ISurface.h>
     34 #include <surfaceflinger/ISurfaceComposerClient.h>
     35 
     36 #define ANDROID_VIEW_SURFACE_JNI_ID    "mNativeSurface"
     37 
     38 namespace android {
     39 
     40 // ---------------------------------------------------------------------------
     41 
     42 class ISurfaceTexture;
     43 class Surface;
     44 class SurfaceComposerClient;
     45 
     46 // ---------------------------------------------------------------------------
     47 
     48 class SurfaceControl : public RefBase
     49 {
     50 public:
     51     static bool isValid(const sp<SurfaceControl>& surface) {
     52         return (surface != 0) && surface->isValid();
     53     }
     54     bool isValid() {
     55         return mToken>=0 && mClient!=0;
     56     }
     57     static bool isSameSurface(
     58             const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
     59 
     60     uint32_t    getIdentity() const { return mIdentity; }
     61 
     62     // release surface data from java
     63     void        clear();
     64 
     65     status_t    setLayer(int32_t layer);
     66     status_t    setPosition(int32_t x, int32_t y);
     67     status_t    setSize(uint32_t w, uint32_t h);
     68     status_t    hide();
     69     status_t    show(int32_t layer = -1);
     70     status_t    freeze();
     71     status_t    unfreeze();
     72     status_t    setFlags(uint32_t flags, uint32_t mask);
     73     status_t    setTransparentRegionHint(const Region& transparent);
     74     status_t    setAlpha(float alpha=1.0f);
     75     status_t    setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
     76     status_t    setFreezeTint(uint32_t tint);
     77 
     78     static status_t writeSurfaceToParcel(
     79             const sp<SurfaceControl>& control, Parcel* parcel);
     80 
     81     sp<Surface> getSurface() const;
     82 
     83 private:
     84     // can't be copied
     85     SurfaceControl& operator = (SurfaceControl& rhs);
     86     SurfaceControl(const SurfaceControl& rhs);
     87 
     88     friend class SurfaceComposerClient;
     89     friend class Surface;
     90 
     91     SurfaceControl(
     92             const sp<SurfaceComposerClient>& client,
     93             const sp<ISurface>& surface,
     94             const ISurfaceComposerClient::surface_data_t& data);
     95 
     96     ~SurfaceControl();
     97 
     98     status_t validate() const;
     99     void destroy();
    100 
    101     sp<SurfaceComposerClient>   mClient;
    102     sp<ISurface>                mSurface;
    103     SurfaceID                   mToken;
    104     uint32_t                    mIdentity;
    105     mutable Mutex               mLock;
    106 
    107     mutable sp<Surface>         mSurfaceData;
    108 };
    109 
    110 // ---------------------------------------------------------------------------
    111 
    112 class Surface : public SurfaceTextureClient
    113 {
    114 public:
    115     struct SurfaceInfo {
    116         uint32_t    w;
    117         uint32_t    h;
    118         uint32_t    s;
    119         uint32_t    usage;
    120         PixelFormat format;
    121         void*       bits;
    122         uint32_t    reserved[2];
    123     };
    124 
    125     explicit Surface(const sp<ISurfaceTexture>& st);
    126 
    127     static status_t writeToParcel(const sp<Surface>& control, Parcel* parcel);
    128 
    129     static sp<Surface> readFromParcel(const Parcel& data);
    130     static bool isValid(const sp<Surface>& surface) {
    131         return (surface != 0) && surface->isValid();
    132     }
    133 
    134     bool        isValid();
    135     uint32_t    getIdentity() const { return mIdentity; }
    136     sp<ISurfaceTexture> getSurfaceTexture();
    137 
    138     // the lock/unlock APIs must be used from the same thread
    139     status_t    lock(SurfaceInfo* info, Region* dirty = NULL);
    140     status_t    unlockAndPost();
    141 
    142     sp<IBinder> asBinder() const;
    143 
    144 private:
    145     // this is just to be able to write some unit tests
    146     friend class Test;
    147     friend class SurfaceControl;
    148 
    149     // can't be copied
    150     Surface& operator = (Surface& rhs);
    151     Surface(const Surface& rhs);
    152 
    153     explicit Surface(const sp<SurfaceControl>& control);
    154     Surface(const Parcel& data, const sp<IBinder>& ref);
    155     ~Surface();
    156 
    157     /*
    158      *  private stuff...
    159      */
    160     void init(const sp<ISurfaceTexture>& surfaceTexture);
    161 
    162     static void cleanCachedSurfacesLocked();
    163 
    164     virtual int query(int what, int* value) const;
    165 
    166     // constants
    167     sp<ISurface>                mSurface;
    168     uint32_t                    mIdentity;
    169 
    170     // A cache of Surface objects that have been deserialized into this process.
    171     static Mutex sCachedSurfacesLock;
    172     static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
    173 };
    174 
    175 }; // namespace android
    176 
    177 #endif // ANDROID_SF_SURFACE_H
    178