1 /*
2  * Copyright 2015 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 #ifndef PERSISTENT_SURFACE_H_
18 
19 #define PERSISTENT_SURFACE_H_
20 
21 #include <binder/Parcel.h>
22 #include <hidl/HidlSupport.h>
23 #include <hidl/HybridInterface.h>
24 #include <gui/IGraphicBufferProducer.h>
25 #include <media/stagefright/foundation/ABase.h>
26 
27 namespace android {
28 
29 struct PersistentSurface : public RefBase {
PersistentSurfacePersistentSurface30     PersistentSurface() {}
31 
32     // create a persistent surface
PersistentSurfacePersistentSurface33     PersistentSurface(
34             const sp<IGraphicBufferProducer>& bufferProducer,
35             const sp<hidl::base::V1_0::IBase>& hidlTarget) :
36         mBufferProducer(bufferProducer),
37         mHidlTarget(hidlTarget) { }
38 
getBufferProducerPersistentSurface39     sp<IGraphicBufferProducer> getBufferProducer() const {
40         return mBufferProducer;
41     }
42 
getHidlTargetPersistentSurface43     sp<hidl::base::V1_0::IBase> getHidlTarget() const {
44         return mHidlTarget;
45     }
46 
writeToParcelPersistentSurface47     status_t writeToParcel(Parcel *parcel) const {
48         parcel->writeStrongBinder(IInterface::asBinder(mBufferProducer));
49         // write hidl target
50         if (mHidlTarget != nullptr) {
51             HalToken token;
52             bool result = createHalToken(mHidlTarget, &token);
53             parcel->writeBool(result);
54             if (result) {
55                 parcel->writeByteArray(token.size(), token.data());
56             }
57         } else {
58             parcel->writeBool(false);
59         }
60         return NO_ERROR;
61     }
62 
readFromParcelPersistentSurface63     status_t readFromParcel(const Parcel *parcel) {
64         mBufferProducer = interface_cast<IGraphicBufferProducer>(
65                 parcel->readStrongBinder());
66         // read hidl target
67         bool haveHidlTarget = parcel->readBool();
68         if (haveHidlTarget) {
69             std::vector<uint8_t> tokenVector;
70             parcel->readByteVector(&tokenVector);
71             HalToken token = HalToken(tokenVector);
72             mHidlTarget = retrieveHalInterface(token);
73             deleteHalToken(token);
74         } else {
75             mHidlTarget.clear();
76         }
77         return NO_ERROR;
78     }
79 
80 private:
81     sp<IGraphicBufferProducer> mBufferProducer;
82     sp<hidl::base::V1_0::IBase> mHidlTarget;
83 
84     DISALLOW_EVIL_CONSTRUCTORS(PersistentSurface);
85 };
86 
87 }  // namespace android
88 
89 #endif  // PERSISTENT_SURFACE_H_
90