SurfaceControl.cpp revision d142f4b787abae7a5c392ded0dd2741574a0bce2
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#define LOG_TAG "SurfaceControl" 18 19#include <stdint.h> 20#include <errno.h> 21#include <sys/types.h> 22#include <sys/stat.h> 23 24#include <android/native_window.h> 25 26#include <utils/Errors.h> 27#include <utils/Log.h> 28#include <utils/threads.h> 29 30#include <binder/IPCThreadState.h> 31 32#include <ui/DisplayInfo.h> 33#include <ui/GraphicBuffer.h> 34#include <ui/Rect.h> 35 36#include <gui/ISurfaceComposer.h> 37#include <gui/Surface.h> 38#include <gui/SurfaceComposerClient.h> 39#include <gui/SurfaceControl.h> 40 41namespace android { 42 43// ============================================================================ 44// SurfaceControl 45// ============================================================================ 46 47SurfaceControl::SurfaceControl( 48 const sp<SurfaceComposerClient>& client, 49 const sp<IBinder>& handle, 50 const sp<IGraphicBufferProducer>& gbp) 51 : mClient(client), mHandle(handle), mGraphicBufferProducer(gbp) 52{ 53} 54 55SurfaceControl::~SurfaceControl() 56{ 57 destroy(); 58} 59 60void SurfaceControl::destroy() 61{ 62 if (isValid()) { 63 mClient->destroySurface(mHandle); 64 } 65 // clear all references and trigger an IPC now, to make sure things 66 // happen without delay, since these resources are quite heavy. 67 mClient.clear(); 68 mHandle.clear(); 69 mGraphicBufferProducer.clear(); 70 IPCThreadState::self()->flushCommands(); 71} 72 73void SurfaceControl::clear() 74{ 75 // here, the window manager tells us explicitly that we should destroy 76 // the surface's resource. Soon after this call, it will also release 77 // its last reference (which will call the dtor); however, it is possible 78 // that a client living in the same process still holds references which 79 // would delay the call to the dtor -- that is why we need this explicit 80 // "clear()" call. 81 destroy(); 82} 83 84bool SurfaceControl::isSameSurface( 85 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs) 86{ 87 if (lhs == 0 || rhs == 0) 88 return false; 89 return lhs->mHandle == rhs->mHandle; 90} 91 92status_t SurfaceControl::setLayerStack(int32_t layerStack) { 93 status_t err = validate(); 94 if (err < 0) return err; 95 const sp<SurfaceComposerClient>& client(mClient); 96 return client->setLayerStack(mHandle, layerStack); 97} 98status_t SurfaceControl::setLayer(int32_t layer) { 99 status_t err = validate(); 100 if (err < 0) return err; 101 const sp<SurfaceComposerClient>& client(mClient); 102 return client->setLayer(mHandle, layer); 103} 104status_t SurfaceControl::setPosition(float x, float y) { 105 status_t err = validate(); 106 if (err < 0) return err; 107 const sp<SurfaceComposerClient>& client(mClient); 108 return client->setPosition(mHandle, x, y); 109} 110status_t SurfaceControl::setSize(uint32_t w, uint32_t h) { 111 status_t err = validate(); 112 if (err < 0) return err; 113 const sp<SurfaceComposerClient>& client(mClient); 114 return client->setSize(mHandle, w, h); 115} 116status_t SurfaceControl::hide() { 117 status_t err = validate(); 118 if (err < 0) return err; 119 const sp<SurfaceComposerClient>& client(mClient); 120 return client->hide(mHandle); 121} 122status_t SurfaceControl::show() { 123 status_t err = validate(); 124 if (err < 0) return err; 125 const sp<SurfaceComposerClient>& client(mClient); 126 return client->show(mHandle); 127} 128status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) { 129 status_t err = validate(); 130 if (err < 0) return err; 131 const sp<SurfaceComposerClient>& client(mClient); 132 return client->setFlags(mHandle, flags, mask); 133} 134status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) { 135 status_t err = validate(); 136 if (err < 0) return err; 137 const sp<SurfaceComposerClient>& client(mClient); 138 return client->setTransparentRegionHint(mHandle, transparent); 139} 140status_t SurfaceControl::setAlpha(float alpha) { 141 status_t err = validate(); 142 if (err < 0) return err; 143 const sp<SurfaceComposerClient>& client(mClient); 144 return client->setAlpha(mHandle, alpha); 145} 146status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) { 147 status_t err = validate(); 148 if (err < 0) return err; 149 const sp<SurfaceComposerClient>& client(mClient); 150 return client->setMatrix(mHandle, dsdx, dtdx, dsdy, dtdy); 151} 152status_t SurfaceControl::setCrop(const Rect& crop) { 153 status_t err = validate(); 154 if (err < 0) return err; 155 const sp<SurfaceComposerClient>& client(mClient); 156 return client->setCrop(mHandle, crop); 157} 158 159status_t SurfaceControl::validate() const 160{ 161 if (mHandle==0 || mClient==0) { 162 ALOGE("invalid handle (%p) or client (%p)", 163 mHandle.get(), mClient.get()); 164 return NO_INIT; 165 } 166 return NO_ERROR; 167} 168 169status_t SurfaceControl::writeSurfaceToParcel( 170 const sp<SurfaceControl>& control, Parcel* parcel) 171{ 172 sp<IGraphicBufferProducer> bp; 173 if (control != NULL) { 174 bp = control->mGraphicBufferProducer; 175 } 176 return parcel->writeStrongBinder(bp->asBinder()); 177} 178 179sp<Surface> SurfaceControl::getSurface() const 180{ 181 Mutex::Autolock _l(mLock); 182 if (mSurfaceData == 0) { 183 mSurfaceData = new Surface(mGraphicBufferProducer); 184 } 185 return mSurfaceData; 186} 187 188// ---------------------------------------------------------------------------- 189}; // namespace android 190