blob: 71c219d4d0ff4ecb32effabf88f4b355adf9537a [file] [log] [blame]
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -08001/*
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +08002 * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -08003 * Not a Contribution.
4 *
5 * Copyright (C) 2013 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "offload_effect_virtualizer"
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -070021//#define LOG_NDEBUG 0
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080022
23#include <cutils/list.h>
24#include <cutils/log.h>
25#include <tinyalsa/asoundlib.h>
Subhash Chandra Bose Naripeddy090a2aa2014-01-30 14:03:12 -080026#include <sound/audio_effects.h>
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080027#include <audio_effects/effect_virtualizer.h>
28
29#include "effect_api.h"
30#include "virtualizer.h"
31
32/* Offload Virtualizer UUID: 509a4498-561a-4bea-b3b1-0002a5d5c51b */
33const effect_descriptor_t virtualizer_descriptor = {
34 {0x37cc2c00, 0xdddd, 0x11db, 0x8577, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
35 {0x509a4498, 0x561a, 0x4bea, 0xb3b1, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
36 EFFECT_CONTROL_API_VERSION,
37 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_DEVICE_IND | EFFECT_FLAG_HW_ACC_TUNNEL),
38 0, /* TODO */
39 1,
40 "MSM offload virtualizer",
41 "The Android Open Source Project",
42};
43
44/*
45 * Virtualizer operations
46 */
47
48int virtualizer_get_strength(virtualizer_context_t *context)
49{
Dhananjay Kumar574f3922014-03-25 17:41:44 +053050 ALOGV("%s: ctxt %p, strength: %d", __func__, context, context->strength);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080051 return context->strength;
52}
53
54int virtualizer_set_strength(virtualizer_context_t *context, uint32_t strength)
55{
Dhananjay Kumar574f3922014-03-25 17:41:44 +053056 ALOGV("%s: ctxt %p, strength: %d", __func__, context, strength);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080057 context->strength = strength;
58
Weiyin Jiangc8082d92015-07-21 11:26:02 +080059 /*
60 * Zero strength is not equivalent to disable state as down mix
61 * is still happening for multichannel inputs.
62 * For better user experience, explicitly disable virtualizer module
63 * when strength is 0.
64 */
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +053065 if (context->enabled_by_client)
66 offload_virtualizer_set_enable_flag(&(context->offload_virt),
Weiyin Jiangc8082d92015-07-21 11:26:02 +080067 ((strength > 0) && !(context->temp_disabled)) ?
68 true : false);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080069 offload_virtualizer_set_strength(&(context->offload_virt), strength);
70 if (context->ctl)
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -070071 offload_virtualizer_send_params(context->ctl, &context->offload_virt,
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080072 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
73 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -070074 if (context->hw_acc_fd > 0)
75 hw_acc_virtualizer_send_params(context->hw_acc_fd,
76 &context->offload_virt,
77 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
78 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080079 return 0;
80}
81
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +080082/*
83 * Check if an audio device is supported by this implementation
84 *
85 * [in]
86 * device device that is intented for processing (e.g. for binaural vs transaural)
87 * [out]
88 * false device is not applicable for effect
89 * true device is applicable for effect
90 */
91bool virtualizer_is_device_supported(audio_devices_t device) {
92 switch (device) {
93 case AUDIO_DEVICE_OUT_SPEAKER:
94 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
95 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
96#ifdef AFE_PROXY_ENABLED
97 case AUDIO_DEVICE_OUT_PROXY:
98#endif
99 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
100 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
101 case AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET:
102 return false;
103 default :
104 return true;
105 }
106}
107
108/*
109 * Check if a channel mask + audio device is supported by this implementation
110 *
111 * [in]
112 * channel_mask channel mask of input buffer
113 * device device that is intented for processing (e.g. for binaural vs transaural)
114 * [out]
115 * false if the configuration is not supported or it is unknown
116 * true if the configuration is supported
117 */
118bool virtualizer_is_configuration_supported(audio_channel_mask_t channel_mask,
119 audio_devices_t device) {
120 uint32_t channelCount = audio_channel_count_from_out_mask(channel_mask);
121 if ((channelCount == 0) || (channelCount > 2)) {
122 return false;
123 }
124
125 return virtualizer_is_device_supported(device);
126}
127
128/*
129 * Force the virtualization mode to that of the given audio device
130 *
131 * [in]
132 * context effect engine context
133 * forced_device device whose virtualization mode we'll always use
134 * [out]
135 * -EINVAL if the device is not supported or is unknown
136 * 0 if the device is supported and the virtualization mode forced
137 */
138int virtualizer_force_virtualization_mode(virtualizer_context_t *context,
139 audio_devices_t forced_device) {
140 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
141 int status = 0;
142 bool use_virt = false;
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530143 int is_virt_enabled = virt_ctxt->enabled_by_client;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800144
145 ALOGV("%s: ctxt %p, forcedDev=0x%x enabled=%d tmpDisabled=%d", __func__, virt_ctxt,
146 forced_device, is_virt_enabled, virt_ctxt->temp_disabled);
147
148 if (virtualizer_is_device_supported(forced_device) == false) {
149 if (forced_device != AUDIO_DEVICE_NONE) {
150 //forced device is not supported, make it behave as a reset of forced mode
151 forced_device = AUDIO_DEVICE_NONE;
152 // but return an error
153 status = -EINVAL;
154 }
155 }
156
157 if (forced_device == AUDIO_DEVICE_NONE) {
158 // disabling forced virtualization mode:
159 // verify whether the virtualization should be enabled or disabled
160 if (virtualizer_is_device_supported(virt_ctxt->device)) {
161 use_virt = (is_virt_enabled == true);
162 }
163 virt_ctxt->forced_device = AUDIO_DEVICE_NONE;
164 } else {
165 // forcing virtualization mode:
166 // TODO: we assume device is supported, so hard coded a fixed one.
167 virt_ctxt->forced_device = AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
168 // TODO: only enable for a supported mode, when the effect is enabled
169 use_virt = (is_virt_enabled == true);
170 }
171
172 if (use_virt) {
173 if (virt_ctxt->temp_disabled == true) {
174 if (effect_is_active(&virt_ctxt->common)) {
175 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true);
176 if (virt_ctxt->ctl)
177 offload_virtualizer_send_params(virt_ctxt->ctl,
178 &virt_ctxt->offload_virt,
179 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
180 if (virt_ctxt->hw_acc_fd > 0)
181 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
182 &virt_ctxt->offload_virt,
183 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
184 }
185 ALOGV("%s: re-enable VIRTUALIZER", __func__);
186 virt_ctxt->temp_disabled = false;
187 } else {
188 ALOGV("%s: leaving VIRTUALIZER enabled", __func__);
189 }
190 } else {
191 if (virt_ctxt->temp_disabled == false) {
192 if (effect_is_active(&virt_ctxt->common)) {
193 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false);
194 if (virt_ctxt->ctl)
195 offload_virtualizer_send_params(virt_ctxt->ctl,
196 &virt_ctxt->offload_virt,
197 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
198 if (virt_ctxt->hw_acc_fd > 0)
199 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
200 &virt_ctxt->offload_virt,
201 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
202 }
203 ALOGV("%s: disable VIRTUALIZER", __func__);
204 virt_ctxt->temp_disabled = true;
205 } else {
206 ALOGV("%s: leaving VIRTUALIZER disabled", __func__);
207 }
208 }
209
210 ALOGV("after %s: ctxt %p, enabled=%d tmpDisabled=%d", __func__, virt_ctxt,
211 is_virt_enabled, virt_ctxt->temp_disabled);
212
213 return status;
214}
215
216/*
217 * Get the virtual speaker angles for a channel mask + audio device configuration
218 * which is guaranteed to be supported by this implementation
219 *
220 * [in]
221 * channel_mask the channel mask of the input to virtualize
222 * device the type of device that affects the processing (e.g. for binaural vs transaural)
223 * [in/out]
224 * speaker_angles the array of integer where each speaker angle is written as a triplet in the
225 * following format:
226 * int32_t a bit mask with a single value selected for each speaker, following
227 * the convention of the audio_channel_mask_t type
228 * int32_t a value in degrees expressing the speaker azimuth, where 0 is in front
229 * of the user, 180 behind, -90 to the left, 90 to the right of the user
230 * int32_t a value in degrees expressing the speaker elevation, where 0 is the
231 * horizontal plane, +90 is directly above the user, -90 below
232 *
233 */
234void virtualizer_get_speaker_angles(audio_channel_mask_t channel_mask __unused,
235 audio_devices_t device __unused, int32_t *speaker_angles) {
236 // the channel count is guaranteed to be 1 or 2
237 // the device is guaranteed to be of type headphone
238 // this virtualizer is always 2in with speakers at -90 and 90deg of azimuth, 0deg of elevation
239 *speaker_angles++ = (int32_t) AUDIO_CHANNEL_OUT_FRONT_LEFT;
240 *speaker_angles++ = -90; // azimuth
241 *speaker_angles++ = 0; // elevation
242 *speaker_angles++ = (int32_t) AUDIO_CHANNEL_OUT_FRONT_RIGHT;
243 *speaker_angles++ = 90; // azimuth
244 *speaker_angles = 0; // elevation
245}
246
247/*
248 * Retrieve the current device whose processing mode is used by this effect
249 *
250 * [out]
251 * AUDIO_DEVICE_NONE if the effect is not virtualizing
252 * or the device type if the effect is virtualizing
253 */
254audio_devices_t virtualizer_get_virtualization_mode(virtualizer_context_t *context) {
255 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
256 audio_devices_t device = AUDIO_DEVICE_NONE;
257
258 if ((offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)))
259 && (virt_ctxt->temp_disabled == false)) {
260 if (virt_ctxt->forced_device != AUDIO_DEVICE_NONE) {
261 // virtualization mode is forced, return that device
262 device = virt_ctxt->forced_device;
263 } else {
264 // no forced mode, return the current device
265 device = virt_ctxt->device;
266 }
267 }
268 ALOGV("%s: returning 0x%x", __func__, device);
269 return device;
270}
271
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800272int virtualizer_get_parameter(effect_context_t *context, effect_param_t *p,
273 uint32_t *size)
274{
275 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
276 int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
277 int32_t *param_tmp = (int32_t *)p->data;
278 int32_t param = *param_tmp++;
279 void *value = p->data + voffset;
280 int i;
281
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530282 ALOGV("%s: ctxt %p, param %d", __func__, virt_ctxt, param);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800283
284 p->status = 0;
285
286 switch (param) {
287 case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED:
288 if (p->vsize < sizeof(uint32_t))
289 p->status = -EINVAL;
290 p->vsize = sizeof(uint32_t);
291 break;
292 case VIRTUALIZER_PARAM_STRENGTH:
293 if (p->vsize < sizeof(int16_t))
294 p->status = -EINVAL;
295 p->vsize = sizeof(int16_t);
296 break;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800297 case VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES:
298 // return value size can only be interpreted as relative to input value,
299 // deferring validity check to below
300 break;
301 case VIRTUALIZER_PARAM_VIRTUALIZATION_MODE:
302 if (p->vsize != sizeof(uint32_t))
303 p->status = -EINVAL;
304 p->vsize = sizeof(uint32_t);
305 break;
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800306 default:
307 p->status = -EINVAL;
308 }
309
310 *size = sizeof(effect_param_t) + voffset + p->vsize;
311
312 if (p->status != 0)
313 return 0;
314
315 switch (param) {
316 case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED:
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800317 *(uint32_t *)value = 1;
318 break;
319
320 case VIRTUALIZER_PARAM_STRENGTH:
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800321 *(int16_t *)value = virtualizer_get_strength(virt_ctxt);
322 break;
323
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800324 case VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES:
325 {
326 const audio_channel_mask_t channel_mask = (audio_channel_mask_t) *param_tmp++;
327 const audio_devices_t device = (audio_devices_t) *param_tmp;
328 uint32_t channel_cnt = audio_channel_count_from_out_mask(channel_mask);
329
330 if (p->vsize < 3 * channel_cnt * sizeof(int32_t)){
331 p->status = -EINVAL;
332 break;
333 }
334 // verify the configuration is supported
335 if(virtualizer_is_configuration_supported(channel_mask, device)) {
336 // configuration is supported, get the angles
337 virtualizer_get_speaker_angles(channel_mask, device, (int32_t *)value);
338 } else {
339 p->status = -EINVAL;
340 }
341
342 break;
343 }
344
345 case VIRTUALIZER_PARAM_VIRTUALIZATION_MODE:
346 *(uint32_t *)value = (uint32_t) virtualizer_get_virtualization_mode(virt_ctxt);
347 break;
348
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800349 default:
350 p->status = -EINVAL;
351 break;
352 }
353
354 return 0;
355}
356
357int virtualizer_set_parameter(effect_context_t *context, effect_param_t *p,
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800358 uint32_t size __unused)
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800359{
360 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
361 int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
362 void *value = p->data + voffset;
363 int32_t *param_tmp = (int32_t *)p->data;
364 int32_t param = *param_tmp++;
365 uint32_t strength;
366
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530367 ALOGV("%s: ctxt %p, param %d", __func__, virt_ctxt, param);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800368
369 p->status = 0;
370
371 switch (param) {
372 case VIRTUALIZER_PARAM_STRENGTH:
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800373 strength = (uint32_t)(*(int16_t *)value);
374 virtualizer_set_strength(virt_ctxt, strength);
375 break;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800376 case VIRTUALIZER_PARAM_FORCE_VIRTUALIZATION_MODE:
377 {
378 const audio_devices_t device = *(audio_devices_t *)value;
379 if (0 != virtualizer_force_virtualization_mode(virt_ctxt, device)) {
380 p->status = -EINVAL;
381 }
382 break;
383 }
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800384 default:
385 p->status = -EINVAL;
386 break;
387 }
388
389 return 0;
390}
391
392int virtualizer_set_device(effect_context_t *context, uint32_t device)
393{
394 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
395
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530396 ALOGV("%s: ctxt %p, device: 0x%x", __func__, virt_ctxt, device);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800397 virt_ctxt->device = device;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800398
399 if (virt_ctxt->forced_device == AUDIO_DEVICE_NONE) {
400 // default case unless configuration is forced
401 if (virtualizer_is_device_supported(device) == false) {
402 if (!virt_ctxt->temp_disabled) {
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530403 if (effect_is_active(&virt_ctxt->common) && virt_ctxt->enabled_by_client) {
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800404 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false);
405 if (virt_ctxt->ctl)
406 offload_virtualizer_send_params(virt_ctxt->ctl,
407 &virt_ctxt->offload_virt,
408 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
409 if (virt_ctxt->hw_acc_fd > 0)
410 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
411 &virt_ctxt->offload_virt,
412 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
413 }
414 virt_ctxt->temp_disabled = true;
415 ALOGI("%s: ctxt %p, disabled based on device", __func__, virt_ctxt);
wjiang50b81f42014-08-06 08:03:14 +0800416 }
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800417 } else {
418 if (virt_ctxt->temp_disabled) {
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530419 if (effect_is_active(&virt_ctxt->common) && virt_ctxt->enabled_by_client) {
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800420 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true);
421 if (virt_ctxt->ctl)
422 offload_virtualizer_send_params(virt_ctxt->ctl,
423 &virt_ctxt->offload_virt,
424 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
425 if (virt_ctxt->hw_acc_fd > 0)
426 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
427 &virt_ctxt->offload_virt,
428 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
429 }
430 virt_ctxt->temp_disabled = false;
wjiang50b81f42014-08-06 08:03:14 +0800431 }
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800432 }
433 }
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800434 // else virtualization mode is forced to a certain device, nothing to do
435
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800436 offload_virtualizer_set_device(&(virt_ctxt->offload_virt), device);
437 return 0;
438}
439
440int virtualizer_reset(effect_context_t *context)
441{
442 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
443
444 return 0;
445}
446
447int virtualizer_init(effect_context_t *context)
448{
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530449 ALOGV("%s: ctxt %p", __func__, context);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800450 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
451
452 context->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
453 context->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
454 context->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
455 context->config.inputCfg.samplingRate = 44100;
456 context->config.inputCfg.bufferProvider.getBuffer = NULL;
457 context->config.inputCfg.bufferProvider.releaseBuffer = NULL;
458 context->config.inputCfg.bufferProvider.cookie = NULL;
459 context->config.inputCfg.mask = EFFECT_CONFIG_ALL;
460 context->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
461 context->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
462 context->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
463 context->config.outputCfg.samplingRate = 44100;
464 context->config.outputCfg.bufferProvider.getBuffer = NULL;
465 context->config.outputCfg.bufferProvider.releaseBuffer = NULL;
466 context->config.outputCfg.bufferProvider.cookie = NULL;
467 context->config.outputCfg.mask = EFFECT_CONFIG_ALL;
468
469 set_config(context, &context->config);
470
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530471 virt_ctxt->enabled_by_client = false;
wjiang50b81f42014-08-06 08:03:14 +0800472 virt_ctxt->temp_disabled = false;
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700473 virt_ctxt->hw_acc_fd = -1;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800474 virt_ctxt->forced_device = AUDIO_DEVICE_NONE;
475 virt_ctxt->device = AUDIO_DEVICE_NONE;
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800476 memset(&(virt_ctxt->offload_virt), 0, sizeof(struct virtualizer_params));
477
478 return 0;
479}
480
481int virtualizer_enable(effect_context_t *context)
482{
483 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
484
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530485 ALOGV("%s: ctxt %p, strength %d", __func__, virt_ctxt, virt_ctxt->strength);
wjiang95d74c22014-03-28 12:29:58 +0800486
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530487 virt_ctxt->enabled_by_client = true;
wjiang95d74c22014-03-28 12:29:58 +0800488 if (!offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)) &&
489 !(virt_ctxt->temp_disabled)) {
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800490 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true);
wjiangd45948e2014-02-24 22:19:43 +0800491 if (virt_ctxt->ctl && virt_ctxt->strength)
492 offload_virtualizer_send_params(virt_ctxt->ctl,
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700493 &virt_ctxt->offload_virt,
wjiangd45948e2014-02-24 22:19:43 +0800494 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700495 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
496 if ((virt_ctxt->hw_acc_fd > 0) && virt_ctxt->strength)
497 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
498 &virt_ctxt->offload_virt,
499 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
500 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
wjiangd45948e2014-02-24 22:19:43 +0800501 }
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800502 return 0;
503}
504
505int virtualizer_disable(effect_context_t *context)
506{
507 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
508
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530509 ALOGV("%s: ctxt %p", __func__, virt_ctxt);
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530510
511 virt_ctxt->enabled_by_client = false;
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800512 if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))) {
513 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false);
514 if (virt_ctxt->ctl)
515 offload_virtualizer_send_params(virt_ctxt->ctl,
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700516 &virt_ctxt->offload_virt,
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800517 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700518 if (virt_ctxt->hw_acc_fd > 0)
519 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
520 &virt_ctxt->offload_virt,
521 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800522 }
523 return 0;
524}
525
526int virtualizer_start(effect_context_t *context, output_context_t *output)
527{
528 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
529
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530530 ALOGV("%s: ctxt %p, ctl %p", __func__, virt_ctxt, output->ctl);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800531 virt_ctxt->ctl = output->ctl;
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700532 if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))) {
Amit Shekhard02f2cd2014-01-16 16:51:43 -0800533 if (virt_ctxt->ctl)
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700534 offload_virtualizer_send_params(virt_ctxt->ctl, &virt_ctxt->offload_virt,
Amit Shekhard02f2cd2014-01-16 16:51:43 -0800535 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
536 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700537 if (virt_ctxt->hw_acc_fd > 0)
538 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
539 &virt_ctxt->offload_virt,
540 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
541 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
542 }
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800543 return 0;
544}
545
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700546int virtualizer_stop(effect_context_t *context, output_context_t *output __unused)
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800547{
548 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
549
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530550 ALOGV("%s: ctxt %p", __func__, virt_ctxt);
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700551 if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)) &&
552 virt_ctxt->ctl) {
553 struct virtualizer_params virt;
554 virt.enable_flag = false;
555 offload_virtualizer_send_params(virt_ctxt->ctl, &virt,
556 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
557 }
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800558 virt_ctxt->ctl = NULL;
559 return 0;
560}
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700561
562int virtualizer_set_mode(effect_context_t *context, int32_t hw_acc_fd)
563{
564 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
565
566 ALOGV("%s: ctxt %p", __func__, virt_ctxt);
567 virt_ctxt->hw_acc_fd = hw_acc_fd;
568 if ((virt_ctxt->hw_acc_fd > 0) &&
569 (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))))
570 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
571 &virt_ctxt->offload_virt,
572 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
573 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
574 return 0;
575}