blob: a3534616aefe38d75042c41f4b8489aff0d9f70f [file] [log] [blame]
Mikhail Naganov022b9952017-01-04 16:36:51 -08001/*
2 * Copyright (C) 2017 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#include <atomic>
18
19#define LOG_TAG "EffectBufferHalHidl"
20//#define LOG_NDEBUG 0
21
22#include <android/hidl/memory/1.0/IAllocator.h>
23#include <hidlmemory/mapping.h>
24#include <utils/Log.h>
25
26#include "ConversionHelperHidl.h"
27#include "EffectBufferHalHidl.h"
28
29using ::android::hardware::Return;
30using ::android::hardware::Status;
31using ::android::hidl::memory::V1_0::IAllocator;
32
33namespace android {
34
35// static
36uint64_t EffectBufferHalHidl::makeUniqueId() {
37 static std::atomic<uint64_t> counter{1};
38 return counter++;
39}
40
41// static
42status_t EffectBufferHalInterface::allocate(
43 size_t size, sp<EffectBufferHalInterface>* buffer) {
44 return mirror(nullptr, size, buffer);
45}
46
47// static
48status_t EffectBufferHalInterface::mirror(
49 void* external, size_t size, sp<EffectBufferHalInterface>* buffer) {
50 sp<EffectBufferHalInterface> tempBuffer = new EffectBufferHalHidl(size);
51 status_t result = reinterpret_cast<EffectBufferHalHidl*>(tempBuffer.get())->init();
52 if (result == OK) {
53 tempBuffer->setExternalData(external);
54 *buffer = tempBuffer;
55 }
56 return result;
57}
58
59EffectBufferHalHidl::EffectBufferHalHidl(size_t size)
60 : mBufferSize(size), mExternalData(nullptr), mAudioBuffer{0, {nullptr}} {
61 mHidlBuffer.id = makeUniqueId();
62 mHidlBuffer.frameCount = 0;
63}
64
65EffectBufferHalHidl::~EffectBufferHalHidl() {
66}
67
68status_t EffectBufferHalHidl::init() {
69 sp<IAllocator> ashmem = IAllocator::getService("ashmem");
70 if (ashmem == 0) {
71 ALOGE("Failed to retrieve ashmem allocator service");
72 return NO_INIT;
73 }
74 status_t retval = NO_MEMORY;
75 Return<void> result = ashmem->allocate(
76 mBufferSize,
77 [&](bool success, const hidl_memory& memory) {
78 if (success) {
79 mHidlBuffer.data = memory;
80 retval = OK;
81 }
82 });
Mikhail Naganove67e8932017-01-24 10:37:10 -080083 if (result.isOk() && retval == OK) {
Mikhail Naganov022b9952017-01-04 16:36:51 -080084 mMemory = hardware::mapMemory(mHidlBuffer.data);
85 if (mMemory != 0) {
86 mMemory->update();
87 mAudioBuffer.raw = static_cast<void*>(mMemory->getPointer());
88 memset(mAudioBuffer.raw, 0, mMemory->getSize());
89 mMemory->commit();
90 } else {
91 ALOGE("Failed to map allocated ashmem");
92 retval = NO_MEMORY;
93 }
Mikhail Naganove67e8932017-01-24 10:37:10 -080094 } else {
95 ALOGE("Failed to allocate %d bytes from ashmem", (int)mBufferSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -080096 }
Mikhail Naganove67e8932017-01-24 10:37:10 -080097 return result.isOk() ? retval : FAILED_TRANSACTION;
Mikhail Naganov022b9952017-01-04 16:36:51 -080098}
99
100audio_buffer_t* EffectBufferHalHidl::audioBuffer() {
101 return &mAudioBuffer;
102}
103
104void* EffectBufferHalHidl::externalData() const {
105 return mExternalData;
106}
107
108void EffectBufferHalHidl::setFrameCount(size_t frameCount) {
109 mHidlBuffer.frameCount = frameCount;
110 mAudioBuffer.frameCount = frameCount;
111}
112
113void EffectBufferHalHidl::setExternalData(void* external) {
114 mExternalData = external;
115}
116
117void EffectBufferHalHidl::update() {
118 if (mExternalData == nullptr) return;
119 mMemory->update();
120 memcpy(mAudioBuffer.raw, mExternalData, mBufferSize);
121 mMemory->commit();
122}
123
124void EffectBufferHalHidl::commit() {
125 if (mExternalData == nullptr) return;
126 memcpy(mExternalData, mAudioBuffer.raw, mBufferSize);
127}
128
129} // namespace android