sdm: gralloc: Add support for Secure Camera use-case

1. Add support for Secure Camera use-case in SDM and gralloc.
2. Set the appropriate ION Heap ID, secure camera/preview ION flags
   and the alignment requirement for Secure Camera use-case.

CRs-Fixed: 1065934
Change-Id: I9e80f3093d598eec7bf717ddd0e3adbbc3382d5e
diff --git a/libgralloc/alloc_controller.cpp b/libgralloc/alloc_controller.cpp
index c5ec276..ae167ba 100644
--- a/libgralloc/alloc_controller.cpp
+++ b/libgralloc/alloc_controller.cpp
@@ -59,16 +59,24 @@
 #define ION_FLAG_ALLOW_NON_CONTIG 0
 #endif
 
+#ifndef ION_FLAG_CP_CAMERA_PREVIEW
+#define ION_FLAG_CP_CAMERA_PREVIEW 0
+#endif
+
 #ifdef MASTER_SIDE_CP
 #define CP_HEAP_ID ION_SECURE_HEAP_ID
 #define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
 #define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
 #define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
+#define ION_SC_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA)
+#define ION_SC_PREVIEW_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA_PREVIEW)
 #else // SLAVE_SIDE_CP
 #define CP_HEAP_ID ION_CP_MM_HEAP_ID
 #define SD_HEAP_ID CP_HEAP_ID
 #define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
 #define ION_SD_FLAGS ION_SECURE
+#define ION_SC_FLAGS ION_SECURE
+#define ION_SC_PREVIEW_FLAGS ION_SECURE
 #endif
 
 using namespace gralloc;
@@ -473,6 +481,9 @@
              * VM. Please add it to the define once available.
              */
             ionFlags |= ION_SD_FLAGS;
+        } else if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
+            ionHeapId = ION_HEAP(SD_HEAP_ID);
+            ionFlags |= (usage & GRALLOC_USAGE_HW_COMPOSER) ? ION_SC_PREVIEW_FLAGS : ION_SC_FLAGS;
         } else {
             ionHeapId = ION_HEAP(CP_HEAP_ID);
             ionFlags |= ION_CP_FLAGS;
diff --git a/libgralloc/gpu.cpp b/libgralloc/gpu.cpp
index 8bae926..545903a 100644
--- a/libgralloc/gpu.cpp
+++ b/libgralloc/gpu.cpp
@@ -74,7 +74,8 @@
         data.align = getpagesize();
 
     if (usage & GRALLOC_USAGE_PROTECTED) {
-            if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
+            if ((usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) ||
+                (usage & GRALLOC_USAGE_HW_CAMERA_MASK)) {
                 /* The alignment here reflects qsee mmu V7L/V8L requirement */
                 data.align = SZ_2M;
             } else {
diff --git a/libgralloc1/gr_allocator.cpp b/libgralloc1/gr_allocator.cpp
index 1a269be..32bc4ef 100644
--- a/libgralloc1/gr_allocator.cpp
+++ b/libgralloc1/gr_allocator.cpp
@@ -48,16 +48,24 @@
 #define ION_FLAG_ALLOW_NON_CONTIG 0
 #endif
 
+#ifndef ION_FLAG_CP_CAMERA_PREVIEW
+#define ION_FLAG_CP_CAMERA_PREVIEW 0
+#endif
+
 #ifdef MASTER_SIDE_CP
 #define CP_HEAP_ID ION_SECURE_HEAP_ID
 #define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
 #define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
 #define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
+#define ION_SC_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA)
+#define ION_SC_PREVIEW_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA_PREVIEW)
 #else  // SLAVE_SIDE_CP
 #define CP_HEAP_ID ION_CP_MM_HEAP_ID
 #define SD_HEAP_ID CP_HEAP_ID
 #define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
 #define ION_SD_FLAGS ION_SECURE
+#define ION_SC_FLAGS ION_SECURE
+#define ION_SC_PREVIEW_FLAGS ION_SECURE
 #endif
 
 namespace gralloc1 {
@@ -599,6 +607,13 @@
        * VM. Please add it to the define once available.
        */
       flags |= ION_SD_FLAGS;
+    } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
+      heap_id = ION_HEAP(SD_HEAP_ID);
+      if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
+        flags |= ION_SC_PREVIEW_FLAGS;
+      } else {
+        flags |= ION_SC_FLAGS;
+      }
     } else {
       heap_id = ION_HEAP(CP_HEAP_ID);
       flags |= ION_CP_FLAGS;
diff --git a/libgralloc1/gr_buf_mgr.cpp b/libgralloc1/gr_buf_mgr.cpp
index 9bba82b..6c36345 100644
--- a/libgralloc1/gr_buf_mgr.cpp
+++ b/libgralloc1/gr_buf_mgr.cpp
@@ -285,7 +285,8 @@
   }
 
   if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) {
-    if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
+    if ((prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) ||
+        (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY)) {
       // The alignment here reflects qsee mmu V7L/V8L requirement
       align = SZ_2M;
     } else {
diff --git a/sdm/include/core/buffer_allocator.h b/sdm/include/core/buffer_allocator.h
index 1b47f79..86cc02a 100644
--- a/sdm/include/core/buffer_allocator.h
+++ b/sdm/include/core/buffer_allocator.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2015 - 2016, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
@@ -52,6 +52,8 @@
   bool secure = false;                        //!< Specifies buffer to be allocated from
                                               //!< secure region.
   bool cache = false;                         //!< Specifies whether the buffer needs to be cache.
+  bool secure_camera = false;                 //!< Specifies buffer to be allocated from specific
+                                              //!< secure heap and with a specific alignment.
 };
 
 /*! @brief Holds the information about the allocated buffer.
diff --git a/sdm/include/core/layer_buffer.h b/sdm/include/core/layer_buffer.h
index 55318eb..4f80f86 100644
--- a/sdm/include/core/layer_buffer.h
+++ b/sdm/include/core/layer_buffer.h
@@ -208,6 +208,10 @@
       uint32_t secure_display : 1;  //!< This flag shall be set by the client to indicate that the
                                     //!< secure display session is in progress. Secure display
                                     //!< session can not coexist with non-secure session.
+
+      uint32_t secure_camera : 1;   //!< This flag shall be set by the client to indicate that the
+                                    //!< buffer is associated with secure camera session. A secure
+                                    //!< camera layer can co-exist with non-secure layer(s).
     };
 
     uint32_t flags = 0;             //!< For initialization purpose only.
diff --git a/sdm/include/private/hw_info_types.h b/sdm/include/private/hw_info_types.h
index e3bd579..88384a8 100644
--- a/sdm/include/private/hw_info_types.h
+++ b/sdm/include/private/hw_info_types.h
@@ -265,6 +265,7 @@
   bool secure = false;
   uint32_t frame_rate = 0;
   LayerTransform transform;
+  bool secure_camera = false;
 
   bool operator==(const HWSessionConfig& config) const {
     return (src_rect == config.src_rect &&
@@ -272,7 +273,8 @@
             buffer_count == config.buffer_count &&
             secure == config.secure &&
             frame_rate == config.frame_rate &&
-            transform == config.transform);
+            transform == config.transform &&
+            secure_camera == config.secure_camera);
   }
 
   bool operator!=(const HWSessionConfig& config) const {
diff --git a/sdm/libs/core/fb/hw_device.cpp b/sdm/libs/core/fb/hw_device.cpp
index ecb5889..d06b161 100644
--- a/sdm/libs/core/fb/hw_device.cpp
+++ b/sdm/libs/core/fb/hw_device.cpp
@@ -744,7 +744,9 @@
     }
   }
 
-  if (input_buffer->flags.secure) {
+  if (input_buffer->flags.secure_camera) {
+    *mdp_flags |= MDP_LAYER_SECURE_CAMERA_SESSION;
+  } else if (input_buffer->flags.secure) {
     *mdp_flags |= MDP_LAYER_SECURE_SESSION;
   }
 
diff --git a/sdm/libs/core/fb/hw_device.h b/sdm/libs/core/fb/hw_device.h
index dce7648..72c7a13 100644
--- a/sdm/libs/core/fb/hw_device.h
+++ b/sdm/libs/core/fb/hw_device.h
@@ -45,6 +45,10 @@
 #define MDP_LAYER_MULTIRECT_PARALLEL_MODE 0
 #endif
 
+#ifndef MDP_LAYER_SECURE_CAMERA_SESSION
+#define MDP_LAYER_SECURE_CAMERA_SESSION 0
+#endif
+
 namespace sdm {
 class HWInfoInterface;
 
diff --git a/sdm/libs/hwc/hwc_buffer_allocator.cpp b/sdm/libs/hwc/hwc_buffer_allocator.cpp
index 7873627..906b96d 100644
--- a/sdm/libs/hwc/hwc_buffer_allocator.cpp
+++ b/sdm/libs/hwc/hwc_buffer_allocator.cpp
@@ -64,7 +64,11 @@
   int height = INT(buffer_config.height);
   int format;
 
-  if (buffer_config.secure) {
+  if (buffer_config.secure_camera) {
+    alloc_flags = GRALLOC_USAGE_HW_CAMERA_WRITE;
+    alloc_flags |= (GRALLOC_USAGE_PROTECTED | GRALLOC_USAGE_HW_COMPOSER);
+    data.align = SZ_2M;
+  } else if (buffer_config.secure) {
     alloc_flags = INT(GRALLOC_USAGE_PRIVATE_MM_HEAP);
     alloc_flags |= INT(GRALLOC_USAGE_PROTECTED);
     data.align = SECURE_ALIGN;
@@ -166,7 +170,11 @@
   int height = INT(buffer_config.height);
   int format;
 
-  if (buffer_config.secure) {
+  if (buffer_config.secure_camera) {
+    alloc_flags = GRALLOC_USAGE_HW_CAMERA_WRITE;
+    alloc_flags |= (GRALLOC_USAGE_PROTECTED | GRALLOC_USAGE_HW_COMPOSER);
+    align = SZ_2M;
+  } else if (buffer_config.secure) {
     alloc_flags = INT(GRALLOC_USAGE_PRIVATE_MM_HEAP);
     alloc_flags |= INT(GRALLOC_USAGE_PROTECTED);
     align = SECURE_ALIGN;
diff --git a/sdm/libs/hwc/hwc_display.cpp b/sdm/libs/hwc/hwc_display.cpp
index 3dafeb6..9c5c4b6 100644
--- a/sdm/libs/hwc/hwc_display.cpp
+++ b/sdm/libs/hwc/hwc_display.cpp
@@ -371,6 +371,9 @@
     if (pvt_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
       layer_stack_.flags.secure_present = true;
       layer_buffer->flags.secure = true;
+      if (pvt_handle->flags & private_handle_t::PRIV_FLAGS_CAMERA_WRITE) {
+        layer_buffer->flags.secure_camera = true;
+      }
     }
     // Gralloc Usage Protected Buffer - L3 - which needs to be treated as Secure & avoid fallback
     if (pvt_handle->flags & private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER) {
diff --git a/sdm/libs/hwc2/hwc_layers.cpp b/sdm/libs/hwc2/hwc_layers.cpp
index 3624c41..f4ae584 100644
--- a/sdm/libs/hwc2/hwc_layers.cpp
+++ b/sdm/libs/hwc2/hwc_layers.cpp
@@ -98,6 +98,9 @@
   // TZ Protected Buffer - L1
   if (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
     layer_buffer->flags.secure = true;
+    if (handle->flags & private_handle_t::PRIV_FLAGS_CAMERA_WRITE) {
+      layer_buffer->flags.secure_camera = true;
+    }
   }
   if (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY) {
     layer_buffer->flags.secure_display = true;