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 return mClient->setLayerStack(mHandle, layerStack); 96} 97status_t SurfaceControl::setLayer(int32_t layer) { 98 status_t err = validate(); 99 if (err < 0) return err; 100 return mClient->setLayer(mHandle, layer); 101} 102status_t SurfaceControl::setPosition(float x, float y) { 103 status_t err = validate(); 104 if (err < 0) return err; 105 return mClient->setPosition(mHandle, x, y); 106} 107status_t SurfaceControl::setSize(uint32_t w, uint32_t h) { 108 status_t err = validate(); 109 if (err < 0) return err; 110 return mClient->setSize(mHandle, w, h); 111} 112status_t SurfaceControl::hide() { 113 status_t err = validate(); 114 if (err < 0) return err; 115 return mClient->hide(mHandle); 116} 117status_t SurfaceControl::show() { 118 status_t err = validate(); 119 if (err < 0) return err; 120 return mClient->show(mHandle); 121} 122status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) { 123 status_t err = validate(); 124 if (err < 0) return err; 125 return mClient->setFlags(mHandle, flags, mask); 126} 127status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) { 128 status_t err = validate(); 129 if (err < 0) return err; 130 return mClient->setTransparentRegionHint(mHandle, transparent); 131} 132status_t SurfaceControl::setAlpha(float alpha) { 133 status_t err = validate(); 134 if (err < 0) return err; 135 return mClient->setAlpha(mHandle, alpha); 136} 137status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) { 138 status_t err = validate(); 139 if (err < 0) return err; 140 return mClient->setMatrix(mHandle, dsdx, dtdx, dsdy, dtdy); 141} 142status_t SurfaceControl::setCrop(const Rect& crop) { 143 status_t err = validate(); 144 if (err < 0) return err; 145 return mClient->setCrop(mHandle, crop); 146} 147 148status_t SurfaceControl::clearLayerFrameStats() const { 149 status_t err = validate(); 150 if (err < 0) return err; 151 const sp<SurfaceComposerClient>& client(mClient); 152 return client->clearLayerFrameStats(mHandle); 153} 154 155status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const { 156 status_t err = validate(); 157 if (err < 0) return err; 158 const sp<SurfaceComposerClient>& client(mClient); 159 return client->getLayerFrameStats(mHandle, outStats); 160} 161 162status_t SurfaceControl::validate() const 163{ 164 if (mHandle==0 || mClient==0) { 165 ALOGE("invalid handle (%p) or client (%p)", 166 mHandle.get(), mClient.get()); 167 return NO_INIT; 168 } 169 return NO_ERROR; 170} 171 172status_t SurfaceControl::writeSurfaceToParcel( 173 const sp<SurfaceControl>& control, Parcel* parcel) 174{ 175 sp<IGraphicBufferProducer> bp; 176 if (control != NULL) { 177 bp = control->mGraphicBufferProducer; 178 } 179 return parcel->writeStrongBinder(bp->asBinder()); 180} 181 182sp<Surface> SurfaceControl::getSurface() const 183{ 184 Mutex::Autolock _l(mLock); 185 if (mSurfaceData == 0) { 186 // This surface is always consumed by SurfaceFlinger, so the 187 // producerControlledByApp value doesn't matter; using false. 188 mSurfaceData = new Surface(mGraphicBufferProducer, false); 189 } 190 return mSurfaceData; 191} 192 193// ---------------------------------------------------------------------------- 194}; // namespace android 195