1 /*
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/allocator/1.0/IAllocator.h>
23 #include <hidlmemory/mapping.h>
24 #include <utils/Log.h>
25
26 #include "EffectBufferHalHidl.h"
27
28 using ::android::hardware::Return;
29 using ::android::hidl::allocator::V1_0::IAllocator;
30
31 namespace android {
32 namespace effect {
33
34 // static
makeUniqueId()35 uint64_t EffectBufferHalHidl::makeUniqueId() {
36 static std::atomic<uint64_t> counter{1};
37 return counter++;
38 }
39
allocate(size_t size,sp<EffectBufferHalInterface> * buffer)40 status_t EffectBufferHalHidl::allocate(
41 size_t size, sp<EffectBufferHalInterface>* buffer) {
42 return mirror(nullptr, size, buffer);
43 }
44
mirror(void * external,size_t size,sp<EffectBufferHalInterface> * buffer)45 status_t EffectBufferHalHidl::mirror(
46 void* external, size_t size, sp<EffectBufferHalInterface>* buffer) {
47 sp<EffectBufferHalInterface> tempBuffer = new EffectBufferHalHidl(size);
48 status_t result = static_cast<EffectBufferHalHidl*>(tempBuffer.get())->init();
49 if (result == OK) {
50 tempBuffer->setExternalData(external);
51 *buffer = tempBuffer;
52 }
53 return result;
54 }
55
EffectBufferHalHidl(size_t size)56 EffectBufferHalHidl::EffectBufferHalHidl(size_t size)
57 : mBufferSize(size), mFrameCountChanged(false),
58 mExternalData(nullptr), mAudioBuffer{0, {nullptr}} {
59 mHidlBuffer.id = makeUniqueId();
60 mHidlBuffer.frameCount = 0;
61 }
62
~EffectBufferHalHidl()63 EffectBufferHalHidl::~EffectBufferHalHidl() {
64 }
65
init()66 status_t EffectBufferHalHidl::init() {
67 sp<IAllocator> ashmem = IAllocator::getService("ashmem");
68 if (ashmem == 0) {
69 ALOGE("Failed to retrieve ashmem allocator service");
70 return NO_INIT;
71 }
72 status_t retval = NO_MEMORY;
73 Return<void> result = ashmem->allocate(
74 mBufferSize,
75 [&](bool success, const hidl_memory& memory) {
76 if (success) {
77 mHidlBuffer.data = memory;
78 retval = OK;
79 }
80 });
81 if (result.isOk() && retval == OK) {
82 mMemory = hardware::mapMemory(mHidlBuffer.data);
83 if (mMemory != 0) {
84 mMemory->update();
85 mAudioBuffer.raw = static_cast<void*>(mMemory->getPointer());
86 memset(mAudioBuffer.raw, 0, mMemory->getSize());
87 mMemory->commit();
88 } else {
89 ALOGE("Failed to map allocated ashmem");
90 retval = NO_MEMORY;
91 }
92 } else {
93 ALOGE("Failed to allocate %d bytes from ashmem", (int)mBufferSize);
94 }
95 return result.isOk() ? retval : FAILED_TRANSACTION;
96 }
97
audioBuffer()98 audio_buffer_t* EffectBufferHalHidl::audioBuffer() {
99 return &mAudioBuffer;
100 }
101
externalData() const102 void* EffectBufferHalHidl::externalData() const {
103 return mExternalData;
104 }
105
setFrameCount(size_t frameCount)106 void EffectBufferHalHidl::setFrameCount(size_t frameCount) {
107 mHidlBuffer.frameCount = frameCount;
108 mAudioBuffer.frameCount = frameCount;
109 mFrameCountChanged = true;
110 }
111
checkFrameCountChange()112 bool EffectBufferHalHidl::checkFrameCountChange() {
113 bool result = mFrameCountChanged;
114 mFrameCountChanged = false;
115 return result;
116 }
117
setExternalData(void * external)118 void EffectBufferHalHidl::setExternalData(void* external) {
119 mExternalData = external;
120 }
121
update()122 void EffectBufferHalHidl::update() {
123 update(mBufferSize);
124 }
125
commit()126 void EffectBufferHalHidl::commit() {
127 commit(mBufferSize);
128 }
129
update(size_t size)130 void EffectBufferHalHidl::update(size_t size) {
131 if (mExternalData == nullptr) return;
132 mMemory->update();
133 if (size > mBufferSize) size = mBufferSize;
134 memcpy(mAudioBuffer.raw, mExternalData, size);
135 mMemory->commit();
136 }
137
commit(size_t size)138 void EffectBufferHalHidl::commit(size_t size) {
139 if (mExternalData == nullptr) return;
140 if (size > mBufferSize) size = mBufferSize;
141 memcpy(mExternalData, mAudioBuffer.raw, size);
142 }
143
144 } // namespace effect
145 } // namespace android
146