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 #pragma once
18 
19 #include <binder/Binder.h>
20 #include <utils/StrongPointer.h>
21 
22 namespace android {
23 class SurfaceFlinger;
24 class Layer;
25 } // namespace android
26 
27 namespace android::surfaceflinger {
28 
29 /*
30  * The layer handle is just a BBinder object passed to the client
31  * (remote process) -- we don't keep any reference on our side such that
32  * the dtor is called when the remote side let go of its reference.
33  *
34  * ~LayerHandle ensures that mFlinger->onLayerDestroyed() is called for
35  * this layer when the handle is destroyed.
36  */
37 class LayerHandle : public BBinder {
38 public:
39     LayerHandle(const sp<android::SurfaceFlinger>& flinger, const sp<android::Layer>& layer);
40     // for testing
LayerHandle(uint32_t layerId)41     LayerHandle(uint32_t layerId) : mFlinger(nullptr), mLayer(nullptr), mLayerId(layerId) {}
42     ~LayerHandle();
43 
44     // Static functions to access the layer and layer id safely from an incoming binder.
45     static sp<LayerHandle> fromIBinder(const sp<IBinder>& handle);
46     static sp<android::Layer> getLayer(const sp<IBinder>& handle);
47     static uint32_t getLayerId(const sp<IBinder>& handle);
48     static const String16 kDescriptor;
49 
getInterfaceDescriptor()50     const String16& getInterfaceDescriptor() const override { return kDescriptor; }
51 
52 private:
53     sp<android::SurfaceFlinger> mFlinger;
54     sp<android::Layer> mLayer;
55     const uint32_t mLayerId;
56 };
57 
58 } // namespace android::surfaceflinger
59