blob: fd69ef13580a5ba5121cc6a38135e6d5da957065 [file] [log] [blame]
Phil Burk204a1632017-01-03 17:23:43 -08001/*
2 * Copyright 2016 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
Phil Burkfbf031e2017-10-12 15:58:31 -070017#define LOG_TAG "SharedRegionParcelable"
Phil Burkc0c70e32017-02-09 13:18:38 -080018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burk204a1632017-01-03 17:23:43 -080021#include <stdint.h>
22
23#include <sys/mman.h>
24#include <binder/Parcelable.h>
25
Phil Burka4eb0d82017-04-12 15:44:06 -070026#include <aaudio/AAudio.h>
Phil Burka5891f42018-03-12 17:21:11 -070027#include <utility/AAudioUtilities.h>
Phil Burk204a1632017-01-03 17:23:43 -080028
29#include "binding/SharedMemoryParcelable.h"
30#include "binding/SharedRegionParcelable.h"
31
Phil Burk204a1632017-01-03 17:23:43 -080032using android::status_t;
Phil Burk204a1632017-01-03 17:23:43 -080033
Phil Burk5ed503c2017-02-01 09:38:15 -080034using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080035
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070036SharedRegionParcelable::SharedRegionParcelable(const SharedRegion& parcelable)
37 : mSharedMemoryIndex(parcelable.sharedMemoryIndex),
38 mOffsetInBytes(parcelable.offsetInBytes),
39 mSizeInBytes(parcelable.sizeInBytes) {}
40
41SharedRegion SharedRegionParcelable::parcelable() const {
42 SharedRegion result;
43 result.sharedMemoryIndex = mSharedMemoryIndex;
44 result.offsetInBytes = mOffsetInBytes;
45 result.sizeInBytes = mSizeInBytes;
46 return result;
47}
Phil Burk204a1632017-01-03 17:23:43 -080048
jiabinfc791ee2023-02-15 19:43:40 +000049void SharedRegionParcelable::setup(MemoryInfoTuple memoryInfoTuple) {
50 mSharedMemoryIndex = std::get<MEMORY_INDEX>(memoryInfoTuple);
51 mOffsetInBytes = std::get<OFFSET>(memoryInfoTuple);
52 mSizeInBytes = std::get<SIZE>(memoryInfoTuple);
53}
54
55SharedRegionParcelable::MemoryInfoTuple SharedRegionParcelable::getMemoryInfo(
56 const std::map<int32_t, int32_t>* memoryIndexMap) const {
57 return {memoryIndexMap == nullptr ? mSharedMemoryIndex : memoryIndexMap->at(mSharedMemoryIndex),
58 mOffsetInBytes,
59 mSizeInBytes};
Phil Burk204a1632017-01-03 17:23:43 -080060}
61
Phil Burk5ed503c2017-02-01 09:38:15 -080062aaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels,
Phil Burk204a1632017-01-03 17:23:43 -080063 void **regionAddressPtr) {
64 if (mSizeInBytes == 0) {
65 *regionAddressPtr = nullptr;
Phil Burk5ed503c2017-02-01 09:38:15 -080066 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -080067 }
68 if (mSharedMemoryIndex < 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070069 ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
Phil Burk5ed503c2017-02-01 09:38:15 -080070 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -080071 }
72 SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex];
73 return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr);
74}
75
Phil Burka5891f42018-03-12 17:21:11 -070076aaudio_result_t SharedRegionParcelable::validate() const {
Phil Burk3df348f2017-02-08 11:41:55 -080077 if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
Phil Burkfbf031e2017-10-12 15:58:31 -070078 ALOGE("invalid mSizeInBytes = %d", mSizeInBytes);
Phil Burk3df348f2017-02-08 11:41:55 -080079 return AAUDIO_ERROR_OUT_OF_RANGE;
Phil Burk204a1632017-01-03 17:23:43 -080080 }
81 if (mSizeInBytes > 0) {
Phil Burk3df348f2017-02-08 11:41:55 -080082 if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) {
Phil Burkfbf031e2017-10-12 15:58:31 -070083 ALOGE("invalid mOffsetInBytes = %d", mOffsetInBytes);
Phil Burk3df348f2017-02-08 11:41:55 -080084 return AAUDIO_ERROR_OUT_OF_RANGE;
Phil Burk204a1632017-01-03 17:23:43 -080085 }
86 if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) {
Phil Burkfbf031e2017-10-12 15:58:31 -070087 ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
Phil Burk5ed503c2017-02-01 09:38:15 -080088 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -080089 }
90 }
Phil Burk5ed503c2017-02-01 09:38:15 -080091 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -080092}
93
94void SharedRegionParcelable::dump() {
Phil Burkfbf031e2017-10-12 15:58:31 -070095 ALOGD("mSizeInBytes = %d -----", mSizeInBytes);
Phil Burk204a1632017-01-03 17:23:43 -080096 if (mSizeInBytes > 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070097 ALOGD("mSharedMemoryIndex = %d", mSharedMemoryIndex);
98 ALOGD("mOffsetInBytes = %d", mOffsetInBytes);
Phil Burk204a1632017-01-03 17:23:43 -080099 }
100}