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