1 /*
2 * Copyright 2022 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 #include "LayerHandle.h"
18 #include <cstdint>
19 #include "Layer.h"
20 #include "LayerCreationArgs.h"
21 #include "SurfaceFlinger.h"
22
23 namespace android::surfaceflinger {
24
LayerHandle(const sp<android::SurfaceFlinger> & flinger,const sp<android::Layer> & layer)25 LayerHandle::LayerHandle(const sp<android::SurfaceFlinger>& flinger,
26 const sp<android::Layer>& layer)
27 : mFlinger(flinger), mLayer(layer), mLayerId(static_cast<uint32_t>(layer->getSequence())) {}
28
~LayerHandle()29 LayerHandle::~LayerHandle() {
30 if (mFlinger) {
31 mFlinger->onHandleDestroyed(this, mLayer, mLayerId);
32 }
33 }
34
35 const String16 LayerHandle::kDescriptor = String16("android.Layer.LayerHandle");
36
fromIBinder(const sp<IBinder> & binder)37 sp<LayerHandle> LayerHandle::fromIBinder(const sp<IBinder>& binder) {
38 if (binder == nullptr) {
39 return nullptr;
40 }
41
42 BBinder* b = binder->localBinder();
43 if (b == nullptr || b->getInterfaceDescriptor() != LayerHandle::kDescriptor) {
44 ALOGD("handle does not have a valid descriptor");
45 return nullptr;
46 }
47
48 // We can safely cast this binder since its local and we verified its interface descriptor.
49 return sp<LayerHandle>::cast(binder);
50 }
51
getLayer(const sp<IBinder> & binder)52 sp<android::Layer> LayerHandle::getLayer(const sp<IBinder>& binder) {
53 sp<LayerHandle> handle = LayerHandle::fromIBinder(binder);
54 return handle ? handle->mLayer : nullptr;
55 }
56
getLayerId(const sp<IBinder> & binder)57 uint32_t LayerHandle::getLayerId(const sp<IBinder>& binder) {
58 sp<LayerHandle> handle = LayerHandle::fromIBinder(binder);
59 return handle ? handle->mLayerId : UNASSIGNED_LAYER_ID;
60 }
61
62 } // namespace android::surfaceflinger
63