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_COMPOSER_CLIENT_H 18 #define ANDROID_GUI_SURFACE_COMPOSER_CLIENT_H 19 20 #include <stdint.h> 21 #include <sys/types.h> 22 23 #include <binder/IBinder.h> 24 #include <binder/IMemory.h> 25 26 #include <utils/RefBase.h> 27 #include <utils/Singleton.h> 28 #include <utils/SortedVector.h> 29 #include <utils/threads.h> 30 31 #include <ui/PixelFormat.h> 32 33 #include <gui/CpuConsumer.h> 34 #include <gui/SurfaceControl.h> 35 36 namespace android { 37 38 // --------------------------------------------------------------------------- 39 40 class DisplayInfo; 41 class Composer; 42 class ISurfaceComposerClient; 43 class IGraphicBufferProducer; 44 class Region; 45 46 // --------------------------------------------------------------------------- 47 48 class SurfaceComposerClient : public RefBase 49 { 50 friend class Composer; 51 public: 52 SurfaceComposerClient(); 53 virtual ~SurfaceComposerClient(); 54 55 // Always make sure we could initialize 56 status_t initCheck() const; 57 58 // Return the connection of this client 59 sp<IBinder> connection() const; 60 61 // Forcibly remove connection before all references have gone away. 62 void dispose(); 63 64 // callback when the composer is dies 65 status_t linkToComposerDeath(const sp<IBinder::DeathRecipient>& recipient, 66 void* cookie = NULL, uint32_t flags = 0); 67 68 // Get information about a display 69 static status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info); 70 71 /* triggers screen off and waits for it to complete */ 72 static void blankDisplay(const sp<IBinder>& display); 73 74 /* triggers screen on and waits for it to complete */ 75 static void unblankDisplay(const sp<IBinder>& display); 76 77 // ------------------------------------------------------------------------ 78 // surface creation / destruction 79 80 //! Create a surface 81 sp<SurfaceControl> createSurface( 82 const String8& name,// name of the surface 83 uint32_t w, // width in pixel 84 uint32_t h, // height in pixel 85 PixelFormat format, // pixel-format desired 86 uint32_t flags = 0 // usage flags 87 ); 88 89 //! Create a virtual display 90 static sp<IBinder> createDisplay(const String8& displayName, bool secure); 91 92 //! Destroy a virtual display 93 static void destroyDisplay(const sp<IBinder>& display); 94 95 //! Get the token for the existing default displays. 96 //! Possible values for id are eDisplayIdMain and eDisplayIdHdmi. 97 static sp<IBinder> getBuiltInDisplay(int32_t id); 98 99 // ------------------------------------------------------------------------ 100 // Composer parameters 101 // All composer parameters must be changed within a transaction 102 // several surfaces can be updated in one transaction, all changes are 103 // committed at once when the transaction is closed. 104 // closeGlobalTransaction() requires an IPC with the server. 105 106 //! Open a composer transaction on all active SurfaceComposerClients. 107 static void openGlobalTransaction(); 108 109 //! Close a composer transaction on all active SurfaceComposerClients. 110 static void closeGlobalTransaction(bool synchronous = false); 111 112 //! Flag the currently open transaction as an animation transaction. 113 static void setAnimationTransaction(); 114 115 status_t hide(const sp<IBinder>& id); 116 status_t show(const sp<IBinder>& id); 117 status_t setFlags(const sp<IBinder>& id, uint32_t flags, uint32_t mask); 118 status_t setTransparentRegionHint(const sp<IBinder>& id, const Region& transparent); 119 status_t setLayer(const sp<IBinder>& id, int32_t layer); 120 status_t setAlpha(const sp<IBinder>& id, float alpha=1.0f); 121 status_t setMatrix(const sp<IBinder>& id, float dsdx, float dtdx, float dsdy, float dtdy); 122 status_t setPosition(const sp<IBinder>& id, float x, float y); 123 status_t setSize(const sp<IBinder>& id, uint32_t w, uint32_t h); 124 status_t setCrop(const sp<IBinder>& id, const Rect& crop); 125 status_t setLayerStack(const sp<IBinder>& id, uint32_t layerStack); 126 status_t destroySurface(const sp<IBinder>& id); 127 128 static void setDisplaySurface(const sp<IBinder>& token, 129 const sp<IGraphicBufferProducer>& bufferProducer); 130 static void setDisplayLayerStack(const sp<IBinder>& token, 131 uint32_t layerStack); 132 133 /* setDisplayProjection() defines the projection of layer stacks 134 * to a given display. 135 * 136 * - orientation defines the display's orientation. 137 * - layerStackRect defines which area of the window manager coordinate 138 * space will be used. 139 * - displayRect defines where on the display will layerStackRect be 140 * mapped to. displayRect is specified post-orientation, that is 141 * it uses the orientation seen by the end-user. 142 */ 143 static void setDisplayProjection(const sp<IBinder>& token, 144 uint32_t orientation, 145 const Rect& layerStackRect, 146 const Rect& displayRect); 147 148 private: 149 virtual void onFirstRef(); 150 Composer& getComposer(); 151 152 mutable Mutex mLock; 153 status_t mStatus; 154 sp<ISurfaceComposerClient> mClient; 155 Composer& mComposer; 156 }; 157 158 // --------------------------------------------------------------------------- 159 160 class ScreenshotClient 161 { 162 public: 163 static status_t capture( 164 const sp<IBinder>& display, 165 const sp<IGraphicBufferProducer>& producer, 166 uint32_t reqWidth, uint32_t reqHeight, 167 uint32_t minLayerZ, uint32_t maxLayerZ); 168 169 private: 170 mutable sp<CpuConsumer> mCpuConsumer; 171 mutable sp<BufferQueue> mBufferQueue; 172 CpuConsumer::LockedBuffer mBuffer; 173 bool mHaveBuffer; 174 175 public: 176 ScreenshotClient(); 177 ~ScreenshotClient(); 178 179 // frees the previous screenshot and capture a new one 180 status_t update(const sp<IBinder>& display); 181 status_t update(const sp<IBinder>& display, 182 uint32_t reqWidth, uint32_t reqHeight); 183 status_t update(const sp<IBinder>& display, 184 uint32_t reqWidth, uint32_t reqHeight, 185 uint32_t minLayerZ, uint32_t maxLayerZ); 186 187 sp<CpuConsumer> getCpuConsumer() const; 188 189 // release memory occupied by the screenshot 190 void release(); 191 192 // pixels are valid until this object is freed or 193 // release() or update() is called 194 void const* getPixels() const; 195 196 uint32_t getWidth() const; 197 uint32_t getHeight() const; 198 PixelFormat getFormat() const; 199 uint32_t getStride() const; 200 // size of allocated memory in bytes 201 size_t getSize() const; 202 }; 203 204 // --------------------------------------------------------------------------- 205 }; // namespace android 206 207 #endif // ANDROID_GUI_SURFACE_COMPOSER_CLIENT_H 208