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 <android/IGraphicBufferSource.h>
22 #include <binder/Parcel.h>
23 #include <hidl/HidlSupport.h>
24 #include <hidl/HybridInterface.h>
25 #include <gui/IGraphicBufferProducer.h>
26 #include <media/stagefright/foundation/ABase.h>
27 
28 using android::hidl::base::V1_0::IBase;
29 
30 namespace android {
31 
32 struct PersistentSurface : public RefBase {
PersistentSurfacePersistentSurface33     PersistentSurface() {}
34 
35     // create an OMX persistent surface
PersistentSurfacePersistentSurface36     PersistentSurface(
37             const sp<IGraphicBufferProducer>& bufferProducer,
38             const sp<IGraphicBufferSource>& bufferSource) :
39         mBufferProducer(bufferProducer),
40         mBufferSource(bufferSource) { }
41 
42     // create a HIDL persistent surface
PersistentSurfacePersistentSurface43     PersistentSurface(
44             const sp<IGraphicBufferProducer>& bufferProducer,
45             const sp<IBase>& hidlTarget) :
46         mBufferProducer(bufferProducer),
47         mHidlTarget(hidlTarget) { }
48 
getBufferProducerPersistentSurface49     sp<IGraphicBufferProducer> getBufferProducer() const {
50         return mBufferProducer;
51     }
52 
getBufferSourcePersistentSurface53     sp<IGraphicBufferSource> getBufferSource() const {
54         return mBufferSource;
55     }
56 
getHidlTargetPersistentSurface57     sp<IBase> getHidlTarget() const {
58         return mHidlTarget;
59     }
60 
writeToParcelPersistentSurface61     status_t writeToParcel(Parcel *parcel) const {
62         parcel->writeStrongBinder(IInterface::asBinder(mBufferProducer));
63         // this can handle null
64         parcel->writeStrongBinder(IInterface::asBinder(mBufferSource));
65         // write hidl target
66         if (mHidlTarget != nullptr) {
67             HalToken token;
68             bool result = createHalToken(mHidlTarget, &token);
69             parcel->writeBool(result);
70             if (result) {
71                 parcel->writeByteArray(token.size(), token.data());
72             }
73         } else {
74             parcel->writeBool(false);
75         }
76         return NO_ERROR;
77     }
78 
readFromParcelPersistentSurface79     status_t readFromParcel(const Parcel *parcel) {
80         mBufferProducer = interface_cast<IGraphicBufferProducer>(
81                 parcel->readStrongBinder());
82         mBufferSource = interface_cast<IGraphicBufferSource>(
83                 parcel->readStrongBinder());
84         // read hidl target
85         bool haveHidlTarget = parcel->readBool();
86         if (haveHidlTarget) {
87             std::vector<uint8_t> tokenVector;
88             parcel->readByteVector(&tokenVector);
89             HalToken token = HalToken(tokenVector);
90             mHidlTarget = retrieveHalInterface(token);
91             deleteHalToken(token);
92         } else {
93             mHidlTarget.clear();
94         }
95         return NO_ERROR;
96     }
97 
98 private:
99     sp<IGraphicBufferProducer> mBufferProducer;
100     sp<IGraphicBufferSource> mBufferSource;
101     sp<IBase> mHidlTarget;
102 
103     DISALLOW_EVIL_CONSTRUCTORS(PersistentSurface);
104 };
105 
106 }  // namespace android
107 
108 #endif  // PERSISTENT_SURFACE_H_
109