Home | History | Annotate | Download | only in gui
      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/BufferQueueCore.h>
     37 #include <gui/ISurfaceComposer.h>
     38 #include <gui/Surface.h>
     39 #include <gui/SurfaceComposerClient.h>
     40 #include <gui/SurfaceControl.h>
     41 
     42 namespace android {
     43 
     44 // ============================================================================
     45 //  SurfaceControl
     46 // ============================================================================
     47 
     48 SurfaceControl::SurfaceControl(
     49         const sp<SurfaceComposerClient>& client,
     50         const sp<IBinder>& handle,
     51         const sp<IGraphicBufferProducer>& gbp)
     52     : mClient(client), mHandle(handle), mGraphicBufferProducer(gbp)
     53 {
     54 }
     55 
     56 SurfaceControl::~SurfaceControl()
     57 {
     58     destroy();
     59 }
     60 
     61 void SurfaceControl::destroy()
     62 {
     63     if (isValid()) {
     64         mClient->destroySurface(mHandle);
     65     }
     66     // clear all references and trigger an IPC now, to make sure things
     67     // happen without delay, since these resources are quite heavy.
     68     mClient.clear();
     69     mHandle.clear();
     70     mGraphicBufferProducer.clear();
     71     IPCThreadState::self()->flushCommands();
     72 }
     73 
     74 void SurfaceControl::clear()
     75 {
     76     // here, the window manager tells us explicitly that we should destroy
     77     // the surface's resource. Soon after this call, it will also release
     78     // its last reference (which will call the dtor); however, it is possible
     79     // that a client living in the same process still holds references which
     80     // would delay the call to the dtor -- that is why we need this explicit
     81     // "clear()" call.
     82     destroy();
     83 }
     84 
     85 void SurfaceControl::disconnect() {
     86     if (mGraphicBufferProducer != NULL) {
     87         mGraphicBufferProducer->disconnect(
     88                 BufferQueueCore::CURRENTLY_CONNECTED_API);
     89     }
     90 }
     91 
     92 bool SurfaceControl::isSameSurface(
     93         const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
     94 {
     95     if (lhs == 0 || rhs == 0)
     96         return false;
     97     return lhs->mHandle == rhs->mHandle;
     98 }
     99 
    100 status_t SurfaceControl::setLayerStack(uint32_t layerStack) {
    101     status_t err = validate();
    102     if (err < 0) return err;
    103     return mClient->setLayerStack(mHandle, layerStack);
    104 }
    105 
    106 status_t SurfaceControl::setLayer(int32_t layer) {
    107     status_t err = validate();
    108     if (err < 0) return err;
    109     return mClient->setLayer(mHandle, layer);
    110 }
    111 
    112 status_t SurfaceControl::setRelativeLayer(const sp<IBinder>& relativeTo, int32_t layer) {
    113     status_t err = validate();
    114     if (err < 0) return err;
    115     return mClient->setRelativeLayer(mHandle, relativeTo, layer);
    116 }
    117 
    118 status_t SurfaceControl::setPosition(float x, float y) {
    119     status_t err = validate();
    120     if (err < 0) return err;
    121     return mClient->setPosition(mHandle, x, y);
    122 }
    123 status_t SurfaceControl::setGeometryAppliesWithResize() {
    124     status_t err = validate();
    125     if (err < 0) return err;
    126     return mClient->setGeometryAppliesWithResize(mHandle);
    127 }
    128 status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
    129     status_t err = validate();
    130     if (err < 0) return err;
    131     return mClient->setSize(mHandle, w, h);
    132 }
    133 status_t SurfaceControl::hide() {
    134     status_t err = validate();
    135     if (err < 0) return err;
    136     return mClient->hide(mHandle);
    137 }
    138 status_t SurfaceControl::show() {
    139     status_t err = validate();
    140     if (err < 0) return err;
    141     return mClient->show(mHandle);
    142 }
    143 status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
    144     status_t err = validate();
    145     if (err < 0) return err;
    146     return mClient->setFlags(mHandle, flags, mask);
    147 }
    148 status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
    149     status_t err = validate();
    150     if (err < 0) return err;
    151     return mClient->setTransparentRegionHint(mHandle, transparent);
    152 }
    153 status_t SurfaceControl::setAlpha(float alpha) {
    154     status_t err = validate();
    155     if (err < 0) return err;
    156     return mClient->setAlpha(mHandle, alpha);
    157 }
    158 status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dtdy, float dsdy) {
    159     status_t err = validate();
    160     if (err < 0) return err;
    161     return mClient->setMatrix(mHandle, dsdx, dtdx, dtdy, dsdy);
    162 }
    163 status_t SurfaceControl::setCrop(const Rect& crop) {
    164     status_t err = validate();
    165     if (err < 0) return err;
    166     return mClient->setCrop(mHandle, crop);
    167 }
    168 status_t SurfaceControl::setFinalCrop(const Rect& crop) {
    169     status_t err = validate();
    170     if (err < 0) return err;
    171     return mClient->setFinalCrop(mHandle, crop);
    172 }
    173 
    174 status_t SurfaceControl::deferTransactionUntil(const sp<IBinder>& handle,
    175         uint64_t frameNumber) {
    176     status_t err = validate();
    177     if (err < 0) return err;
    178     return mClient->deferTransactionUntil(mHandle, handle, frameNumber);
    179 }
    180 
    181 status_t SurfaceControl::deferTransactionUntil(const sp<Surface>& handle,
    182         uint64_t frameNumber) {
    183     status_t err = validate();
    184     if (err < 0) return err;
    185     return mClient->deferTransactionUntil(mHandle, handle, frameNumber);
    186 }
    187 
    188 status_t SurfaceControl::reparentChildren(const sp<IBinder>& newParentHandle) {
    189     status_t err = validate();
    190     if (err < 0) return err;
    191     return mClient->reparentChildren(mHandle, newParentHandle);
    192 }
    193 
    194 status_t SurfaceControl::detachChildren() {
    195     status_t err = validate();
    196     if (err < 0) return err;
    197     return mClient->detachChildren(mHandle);
    198 }
    199 
    200 status_t SurfaceControl::setOverrideScalingMode(int32_t overrideScalingMode) {
    201     status_t err = validate();
    202     if (err < 0) return err;
    203     return mClient->setOverrideScalingMode(mHandle, overrideScalingMode);
    204 }
    205 
    206 status_t SurfaceControl::clearLayerFrameStats() const {
    207     status_t err = validate();
    208     if (err < 0) return err;
    209     const sp<SurfaceComposerClient>& client(mClient);
    210     return client->clearLayerFrameStats(mHandle);
    211 }
    212 
    213 status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
    214     status_t err = validate();
    215     if (err < 0) return err;
    216     const sp<SurfaceComposerClient>& client(mClient);
    217     return client->getLayerFrameStats(mHandle, outStats);
    218 }
    219 
    220 status_t SurfaceControl::validate() const
    221 {
    222     if (mHandle==0 || mClient==0) {
    223         ALOGE("invalid handle (%p) or client (%p)",
    224                 mHandle.get(), mClient.get());
    225         return NO_INIT;
    226     }
    227     return NO_ERROR;
    228 }
    229 
    230 status_t SurfaceControl::writeSurfaceToParcel(
    231         const sp<SurfaceControl>& control, Parcel* parcel)
    232 {
    233     sp<IGraphicBufferProducer> bp;
    234     if (control != NULL) {
    235         bp = control->mGraphicBufferProducer;
    236     }
    237     return parcel->writeStrongBinder(IInterface::asBinder(bp));
    238 }
    239 
    240 sp<Surface> SurfaceControl::generateSurfaceLocked() const
    241 {
    242     // This surface is always consumed by SurfaceFlinger, so the
    243     // producerControlledByApp value doesn't matter; using false.
    244     mSurfaceData = new Surface(mGraphicBufferProducer, false);
    245 
    246     return mSurfaceData;
    247 }
    248 
    249 sp<Surface> SurfaceControl::getSurface() const
    250 {
    251     Mutex::Autolock _l(mLock);
    252     if (mSurfaceData == 0) {
    253         return generateSurfaceLocked();
    254     }
    255     return mSurfaceData;
    256 }
    257 
    258 sp<Surface> SurfaceControl::createSurface() const
    259 {
    260     Mutex::Autolock _l(mLock);
    261     return generateSurfaceLocked();
    262 }
    263 
    264 sp<IBinder> SurfaceControl::getHandle() const
    265 {
    266     Mutex::Autolock lock(mLock);
    267     return mHandle;
    268 }
    269 
    270 // ----------------------------------------------------------------------------
    271 }; // namespace android
    272