1 /*
2  * Copyright (C) 2022 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 <algorithm>
18 #include <cstdint>
19 #include <cstring>
20 #include <sys/mman.h>
21 #define LOG_TAG "EffectBufferHalAidl"
22 //#define LOG_NDEBUG 0
23 
24 #include <cutils/ashmem.h>
25 #include <utils/Log.h>
26 
27 #include "EffectBufferHalAidl.h"
28 
29 using ndk::ScopedFileDescriptor;
30 
31 namespace android {
32 namespace effect {
33 
34 // static
allocate(size_t size,sp<EffectBufferHalInterface> * buffer)35 status_t EffectBufferHalAidl::allocate(size_t size, sp<EffectBufferHalInterface>* buffer) {
36     return mirror(nullptr, size, buffer);
37 }
38 
mirror(void * external,size_t size,sp<EffectBufferHalInterface> * buffer)39 status_t EffectBufferHalAidl::mirror(void* external, size_t size,
40                                      sp<EffectBufferHalInterface>* buffer) {
41     sp<EffectBufferHalAidl> tempBuffer = new EffectBufferHalAidl(size);
42     status_t status = tempBuffer.get()->init();
43     if (status != OK) {
44         ALOGE("%s init failed %d", __func__, status);
45         return status;
46     }
47 
48     tempBuffer->setExternalData(external);
49     *buffer = tempBuffer;
50     return OK;
51 }
52 
EffectBufferHalAidl(size_t size)53 EffectBufferHalAidl::EffectBufferHalAidl(size_t size)
54     : mBufferSize(size),
55       mFrameCountChanged(false),
56       mExternalData(nullptr),
57       mAudioBuffer{0, {nullptr}} {
58 }
59 
~EffectBufferHalAidl()60 EffectBufferHalAidl::~EffectBufferHalAidl() {
61     if (mAudioBuffer.raw) free(mAudioBuffer.raw);
62 }
63 
init()64 status_t EffectBufferHalAidl::init() {
65     if (0 != posix_memalign(&mAudioBuffer.raw, 32, mBufferSize)) {
66         return NO_MEMORY;
67     }
68 
69     return OK;
70 }
71 
audioBuffer()72 audio_buffer_t* EffectBufferHalAidl::audioBuffer() {
73     return &mAudioBuffer;
74 }
75 
externalData() const76 void* EffectBufferHalAidl::externalData() const {
77     return mExternalData;
78 }
79 
setFrameCount(size_t frameCount)80 void EffectBufferHalAidl::setFrameCount(size_t frameCount) {
81     mAudioBuffer.frameCount = frameCount;
82     mFrameCountChanged = true;
83 }
84 
checkFrameCountChange()85 bool EffectBufferHalAidl::checkFrameCountChange() {
86     bool result = mFrameCountChanged;
87     mFrameCountChanged = false;
88     return result;
89 }
90 
setExternalData(void * external)91 void EffectBufferHalAidl::setExternalData(void* external) {
92     mExternalData = external;
93 }
94 
update()95 void EffectBufferHalAidl::update() {
96     update(mBufferSize);
97 }
98 
commit()99 void EffectBufferHalAidl::commit() {
100     commit(mBufferSize);
101 }
102 
copy(void * dst,const void * src,size_t n) const103 void EffectBufferHalAidl::copy(void* dst, const void* src, size_t n) const {
104     if (!dst || !src) {
105         return;
106     }
107     std::memcpy(dst, src, std::min(n, mBufferSize));
108 }
109 
update(size_t n)110 void EffectBufferHalAidl::update(size_t n) {
111     copy(mAudioBuffer.raw, mExternalData, n);
112 }
113 
commit(size_t n)114 void EffectBufferHalAidl::commit(size_t n) {
115     copy(mExternalData, mAudioBuffer.raw, n);
116 }
117 
118 } // namespace effect
119 } // namespace android
120