/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define LOG_TAG "SurfaceControl" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace android { // ============================================================================ // SurfaceControl // ============================================================================ SurfaceControl::SurfaceControl(const sp& client, const sp& handle, const sp& gbp, int32_t layerId, uint32_t transform) : mClient(client), mHandle(handle), mGraphicBufferProducer(gbp), mLayerId(layerId), mTransformHint(transform) {} SurfaceControl::SurfaceControl(const sp& other) { mClient = other->mClient; mHandle = other->mHandle; mGraphicBufferProducer = other->mGraphicBufferProducer; mTransformHint = other->mTransformHint; mLayerId = other->mLayerId; } SurfaceControl::~SurfaceControl() { // Trigger an IPC now, to make sure things // happen without delay, since these resources are quite heavy. mClient.clear(); mHandle.clear(); mGraphicBufferProducer.clear(); IPCThreadState::self()->flushCommands(); } void SurfaceControl::disconnect() { if (mGraphicBufferProducer != nullptr) { mGraphicBufferProducer->disconnect( BufferQueueCore::CURRENTLY_CONNECTED_API); } } bool SurfaceControl::isSameSurface( const sp& lhs, const sp& rhs) { if (lhs == nullptr || rhs == nullptr) return false; return lhs->mHandle == rhs->mHandle; } status_t SurfaceControl::clearLayerFrameStats() const { status_t err = validate(); if (err != NO_ERROR) return err; const sp& client(mClient); return client->clearLayerFrameStats(mHandle); } status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const { status_t err = validate(); if (err != NO_ERROR) return err; const sp& client(mClient); return client->getLayerFrameStats(mHandle, outStats); } status_t SurfaceControl::validate() const { if (mHandle==nullptr || mClient==nullptr) { ALOGE("invalid handle (%p) or client (%p)", mHandle.get(), mClient.get()); return NO_INIT; } return NO_ERROR; } status_t SurfaceControl::writeSurfaceToParcel( const sp& control, Parcel* parcel) { sp bp; if (control != nullptr) { bp = control->mGraphicBufferProducer; } return parcel->writeStrongBinder(IInterface::asBinder(bp)); } sp SurfaceControl::generateSurfaceLocked() const { // This surface is always consumed by SurfaceFlinger, so the // producerControlledByApp value doesn't matter; using false. mSurfaceData = new Surface(mGraphicBufferProducer, false); return mSurfaceData; } sp SurfaceControl::getSurface() const { Mutex::Autolock _l(mLock); if (mSurfaceData == nullptr) { return generateSurfaceLocked(); } return mSurfaceData; } sp SurfaceControl::createSurface() const { Mutex::Autolock _l(mLock); return generateSurfaceLocked(); } sp SurfaceControl::getHandle() const { return mHandle; } int32_t SurfaceControl::getLayerId() const { return mLayerId; } sp SurfaceControl::getIGraphicBufferProducer() const { Mutex::Autolock _l(mLock); return mGraphicBufferProducer; } sp SurfaceControl::getClient() const { return mClient; } uint32_t SurfaceControl::getTransformHint() const { Mutex::Autolock _l(mLock); return mTransformHint; } void SurfaceControl::setTransformHint(uint32_t hint) { Mutex::Autolock _l(mLock); mTransformHint = hint; } status_t SurfaceControl::writeToParcel(Parcel& parcel) { SAFE_PARCEL(parcel.writeStrongBinder, ISurfaceComposerClient::asBinder(mClient->getClient())); SAFE_PARCEL(parcel.writeStrongBinder, mHandle); SAFE_PARCEL(parcel.writeStrongBinder, IGraphicBufferProducer::asBinder(mGraphicBufferProducer)); SAFE_PARCEL(parcel.writeInt32, mLayerId); SAFE_PARCEL(parcel.writeUint32, mTransformHint); return NO_ERROR; } status_t SurfaceControl::readFromParcel(const Parcel& parcel, sp* outSurfaceControl) { sp client; sp handle; sp gbp; int32_t layerId; uint32_t transformHint; SAFE_PARCEL(parcel.readStrongBinder, &client); SAFE_PARCEL(parcel.readStrongBinder, &handle); SAFE_PARCEL(parcel.readNullableStrongBinder, &gbp); SAFE_PARCEL(parcel.readInt32, &layerId); SAFE_PARCEL(parcel.readUint32, &transformHint); // We aren't the original owner of the surface. *outSurfaceControl = new SurfaceControl(new SurfaceComposerClient( interface_cast(client)), handle.get(), interface_cast(gbp), layerId, transformHint); return NO_ERROR; } status_t SurfaceControl::readNullableFromParcel(const Parcel& parcel, sp* outSurfaceControl) { bool isNotNull; SAFE_PARCEL(parcel.readBool, &isNotNull); if (isNotNull) { SAFE_PARCEL(SurfaceControl::readFromParcel, parcel, outSurfaceControl); } return NO_ERROR; } status_t SurfaceControl::writeNullableToParcel(Parcel& parcel, const sp& surfaceControl) { auto isNotNull = surfaceControl != nullptr; SAFE_PARCEL(parcel.writeBool, isNotNull); if (isNotNull) { SAFE_PARCEL(surfaceControl->writeToParcel, parcel); } return NO_ERROR; } // ---------------------------------------------------------------------------- }; // namespace android