1 /* 2 * Copyright (C) 2020 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 // This module contains utilities for interfacing between legacy code that is using IMemory and new 20 // code that is using android.os.SharedFileRegion. 21 22 #include <optional> 23 24 #include "android/media/SharedFileRegion.h" 25 #include "binder/IMemory.h" 26 #include "utils/StrongPointer.h" 27 28 namespace android { 29 namespace media { 30 31 /** 32 * Converts a SharedFileRegion parcelable to an IMemory instance. 33 * @param shmem The SharedFileRegion instance. 34 * @param result The resulting IMemory instance. May not be null. 35 * @return true if the conversion is successful (should always succeed under normal circumstances, 36 * failure usually means corrupt data). 37 */ 38 bool convertSharedFileRegionToIMemory(const SharedFileRegion& shmem, 39 sp<IMemory>* result); 40 41 /** 42 * Converts a nullable SharedFileRegion parcelable to an IMemory instance. 43 * @param shmem The SharedFileRegion instance. 44 * @param result The resulting IMemory instance. May not be null. Pointee assigned to null, 45 * if the input is null. 46 * @return true if the conversion is successful (should always succeed under normal circumstances, 47 * failure usually means corrupt data). 48 */ 49 bool convertNullableSharedFileRegionToIMemory(const std::optional<SharedFileRegion>& shmem, 50 sp<IMemory>* result); 51 52 /** 53 * Converts an IMemory instance to SharedFileRegion. 54 * @param mem The IMemory instance. May not be null. 55 * @param result The resulting SharedFileRegion instance. 56 * @return true if the conversion is successful (should always succeed under normal circumstances, 57 * failure usually means corrupt data). 58 */ 59 bool convertIMemoryToSharedFileRegion(const sp<IMemory>& mem, 60 SharedFileRegion* result); 61 62 /** 63 * Converts a nullable IMemory instance to a nullable SharedFileRegion. 64 * @param mem The IMemory instance. May be null. 65 * @param result The resulting SharedFileRegion instance. May not be null. Assigned to empty, 66 * if the input is null. 67 * @return true if the conversion is successful (should always succeed under normal circumstances, 68 * failure usually means corrupt data). 69 */ 70 bool convertNullableIMemoryToSharedFileRegion(const sp<IMemory>& mem, 71 std::optional<SharedFileRegion>* result); 72 73 } // namespace media 74 } // namespace android 75