Changes for VR HAL on SDM845

Adding sdm845 specific file and making changes in make file

Change-Id: I03f303a2cd544729c749b2e53e82c064cbb02bbb
diff --git a/Android.mk b/Android.mk
index 89f2915..383208d 100644
--- a/Android.mk
+++ b/Android.mk
@@ -17,11 +17,19 @@
 include $(CLEAR_VARS)
 
 LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../device/qcom/common/thermal-engine
-LOCAL_MODULE := vr.msm8998
+LOCAL_MODULE := vr.$(TARGET_BOARD_PLATFORM)
 LOCAL_MODULE_RELATIVE_PATH := hw
 LOCAL_PROPRIETARY_MODULE := true
 LOCAL_SRC_FILES := vr.c
+
+ifeq ($(call is-board-platform-in-list,msm8998), true)
 LOCAL_SRC_FILES += vr-8998.c
+endif
+
+ifeq ($(call is-board-platform-in-list,sdm845), true)
+LOCAL_SRC_FILES += vr-845.c
+endif
+
 LOCAL_SHARED_LIBRARIES := liblog libcutils
 LOCAL_MODULE_TAGS := optional
 LOCAL_CFLAGS += -Wno-unused-parameter
diff --git a/vr-845.c b/vr-845.c
new file mode 100644
index 0000000..e1409ec
--- /dev/null
+++ b/vr-845.c
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2017, The Linux Foundation. All rights reserved.
+ * Not a contribution
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "vr_int.h"
+
+// List thermal configs in format {name, algo_type}
+// This list is manually synced with the thermal config
+
+#define THERMAL_CONFIG_SYNCED 1
+
+#if THERMAL_CONFIG_SYNCED
+
+#define NUM_NON_VR_CONFIGS 4
+static thermal_algo_info_t non_vr_thermal_configs[NUM_NON_VR_CONFIGS] =
+    {{.config_name = "SKIN-HIGH-FLOOR", .algo_name = "ss"},
+     {.config_name = "SKIN-MID-FLOOR", .algo_name = "ss"},
+     {.config_name = "SKIN-LOW-FLOOR", .algo_name = "ss"},
+     {.config_name = "VIRTUAL-SS-GPU-SKIN", .algo_name = "ss"}};
+
+#define NUM_VR_CONFIGS 1
+static thermal_algo_info_t vr_thermal_configs[NUM_VR_CONFIGS] =
+    {{.config_name = "VR-EMMC", .algo_name = "monitor"}};
+
+int load_thermal_cfg_info(thermal_cfg_info_t *non_vr, thermal_cfg_info_t *vr) {
+    if(!non_vr || !vr)
+        return -1;
+
+    non_vr->num_cfgs = NUM_NON_VR_CONFIGS;
+    non_vr->cfgs = &non_vr_thermal_configs[0];
+
+    vr->num_cfgs = NUM_VR_CONFIGS;
+    vr->cfgs = &vr_thermal_configs[0];
+
+    return 0;
+}
+
+#endif
diff --git a/vr.c b/vr.c
index 4ba87e6..d8e2d03 100644
--- a/vr.c
+++ b/vr.c
@@ -41,14 +41,14 @@
 static int max_string_size = 36;
 static int error_state = 0; //global error state - don't do anything if set!
 
-#define DEBUG 1
+#define DEBUG 0
 
 static thermal_cfg_info_t non_vr_thermal_cfgs;
 static thermal_cfg_info_t vr_thermal_cfgs;
 
 int __attribute__ ((weak)) load_thermal_cfg_info(thermal_cfg_info_t *non_vr,
                                                  thermal_cfg_info_t *vr) {
-
+    ALOGD("Entering %s",__func__);
     non_vr->num_cfgs = 0;
     vr->num_cfgs = 0;
 
@@ -84,6 +84,7 @@
  * Debug function for printing out all instances of "ss" and "monitor" algos
  */
 static void query_thermal_config(){
+    ALOGD("Entering %s",__func__);
     struct config_instance *instances;
 
     int num_configs = (*p_thermal_client_config_query)("ss", &instances);
@@ -113,8 +114,8 @@
  * Load the thermal client library
  * returns 0 on success
  */
-static int load_thermal_client(void)
-{
+static int load_thermal_client(void){
+    ALOGD("Entering %s",__func__);
     char *thermal_client_so = "vendor/lib64/libthermalclient.so";
 
     dlhandle = dlopen(thermal_client_so, RTLD_NOW | RTLD_LOCAL);
@@ -157,39 +158,74 @@
 }
 
 /**
- *  Allocate a new struct config_instance for modifying the disable field
- */
-static struct config_instance *allocate_config_instance(){
-    struct config_instance *config = (struct config_instance *)malloc(sizeof(struct config_instance));
-    memset(config, 0, sizeof(*config));
-
-    config->cfg_desc = (char *)malloc(sizeof(char) * max_string_size);
-    memset(config->cfg_desc, 0, sizeof(char)*max_string_size);
-
-    config->algo_type = (char *)malloc(sizeof(char) * max_string_size);
-    memset(config->algo_type, 0, sizeof(char) * max_string_size);
-
-    config->fields = (struct field_data *)malloc(sizeof(struct field_data));
-    memset(config->fields, 0, sizeof(*config->fields));
-
-    config->fields[0].field_name = (char*)malloc(sizeof(char) * max_string_size);
-    memset(config->fields[0].field_name, 0, sizeof(char) * max_string_size);
-
-    config->fields[0].data = (void*)malloc(sizeof(int));
-
-    return config;
-}
-/**
  *  Free the config_instance as allocated in allocate_config_instance
  */
 static void free_config_instance(struct config_instance *config){
 
-    free(config->fields[0].data);
-    free(config->fields[0].field_name);
-    free(config->fields);
-    free(config->algo_type);
-    free(config->cfg_desc);
-    free(config);
+    if (config) {
+        if (config->fields) {
+            free(config->fields[0].data);
+            free(config->fields[0].field_name);
+            free(config->fields);
+        }
+        free(config->algo_type);
+        free(config->cfg_desc);
+        free(config);
+    }
+}
+
+/**
+ *  Allocate a new struct config_instance for modifying the disable field
+ */
+static struct config_instance *allocate_config_instance(){
+    ALOGD("Entering %s",__func__);
+    struct config_instance *config = (struct config_instance *)malloc(sizeof(struct config_instance));
+    if (!config) {
+        ALOGE("Unable to allocate memory for config");
+        return NULL;
+    }
+    memset(config, 0, sizeof(*config));
+
+    config->cfg_desc = (char *)malloc(sizeof(char) * max_string_size);
+    if (!config->cfg_desc) {
+        free_config_instance(config);
+        ALOGE("Unable to allocate memory for config->cfg_desc");
+        return NULL;
+    }
+    memset(config->cfg_desc, 0, sizeof(char)*max_string_size);
+
+    config->algo_type = (char *)malloc(sizeof(char) * max_string_size);
+    if (!config->algo_type) {
+        free_config_instance(config);
+        ALOGE("Unable to allocate memory for config->algo_type");
+        return NULL;
+    }
+    memset(config->algo_type, 0, sizeof(char) * max_string_size);
+
+    config->fields = (struct field_data *)malloc(sizeof(struct field_data));
+    if (!config->fields) {
+        free_config_instance(config);
+        ALOGE("Unable to allocate memory for config->fields");
+        return NULL;
+    }
+    memset(config->fields, 0, sizeof(*config->fields));
+
+    config->fields[0].field_name = (char*)malloc(sizeof(char) * max_string_size);
+    if (!config->fields[0].field_name) {
+        free_config_instance(config);
+        ALOGE("Unable to allocate memory for config->fields[0].field_name");
+        return NULL;
+    }
+    memset(config->fields[0].field_name, 0, sizeof(char) * max_string_size);
+
+    config->fields[0].data = (void*)malloc(sizeof(int));
+    if (!config->fields[0].data) {
+        free_config_instance(config);
+        ALOGE("Unable to allocate memory for config->fields[0].data");
+        return NULL;
+    }
+
+    return config;
 }
 
 /**
@@ -197,11 +233,15 @@
  *  returns 1 on success, anything else is a failure
  */
 static int disable_config(char *config_name, char *algo_type){
+    ALOGD("Entering %s",__func__);
     int result = 0;
     if (error_state) {
         return 0;
     }
     struct config_instance *config = allocate_config_instance();
+    if (!config) {
+        return 0;
+    }
     strlcpy(config->cfg_desc, config_name, max_string_size);
     strlcpy(config->algo_type, algo_type, max_string_size);
     strlcpy(config->fields[0].field_name, "disable", max_string_size);
@@ -227,11 +267,15 @@
  *  returns 1 on success, anything else is failure
  */
 static int enable_config(char *config_name, char *algo_type){
+    ALOGD("Entering %s",__func__);
     int result = 0;
     if (error_state) {
         return 0;
     }
     struct config_instance *config = allocate_config_instance();
+    if (!config) {
+        return 0;
+    }
     strlcpy(config->cfg_desc, config_name, max_string_size);
     strlcpy(config->algo_type, algo_type, max_string_size);
     strlcpy(config->fields[0].field_name, "disable", max_string_size);
@@ -258,6 +302,7 @@
  * Attempts to clean up any outstanding thermal config state
  */
 static void error_cleanup(){
+    ALOGD("Entering %s",__func__);
     //disable VR configs, best-effort so ignore return values
     for (unsigned int i = 0; i < vr_thermal_cfgs.num_cfgs; i++) {
         disable_config(vr_thermal_cfgs.cfgs[i].config_name, vr_thermal_cfgs.cfgs[i].algo_name);
@@ -276,6 +321,7 @@
  * Set global display/GPU/scheduler configuration to used for VR apps.
  */
 static void set_vr_thermal_configuration() {
+    ALOGD("Entering %s",__func__);
     int result = 1;
     if (error_state) {
         return;
@@ -313,6 +359,7 @@
  * Reset to default global display/GPU/scheduler configuration.
  */
 static void unset_vr_thermal_configuration() {
+    ALOGD("Entering %s",__func__);
     int result = 1;
     if (error_state) {
         return;
@@ -345,8 +392,8 @@
     return;
 }
 
-static void vr_init(struct vr_module *module) 
-{
+static void vr_init(struct vr_module *module){
+    ALOGD("Entering %s",__func__);
     int success = load_thermal_client();
     if (success != 0) {
         ALOGE("failed to load thermal client");
@@ -360,8 +407,8 @@
 
 }
 
-static void vr_set_vr_mode(struct vr_module *module, bool enabled) 
-{
+static void vr_set_vr_mode(struct vr_module *module, bool enabled){
+    ALOGD("Entering %s",__func__);
     if (enabled) {
         set_vr_thermal_configuration();
     } else {