# Copyright (C) 2011 Amlogic Inc
#
# 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.

#
# This file is the build configuration for a full Android
# build for Meson reference board.
#

#ATV version, need compile DRM related modules
ifneq ($(BOARD_COMPILE_ATV),false)
BOARD_COMPILE_CTS := true
endif

PRODUCT_DIR := planck

ifneq ($(ATV_LAUNCHER), amati)
$(warning build AndroidTV)
CONFIG_DEVICE_LOW_RAM := true
KERNEL_A32_SUPPORT :=true
else
$(warning ATV_LAUNCHER = $(ATV_LAUNCHER))
endif

########################################################################
# put ms12 ddp dtshd dovi related to oem partitions.
########################################################################
TARGET_BUILD_OEM_WITH_LICENSE_FILES := true

ifeq ($(CONFIG_DEVICE_LOW_RAM),true)
# Enable DM file pre-opting to reduce first boot time
PRODUCT_DEX_PREOPT_GENERATE_DM_FILES := true
PRODUCT_DEX_PREOPT_DEFAULT_COMPILER_FILTER := verify

PRODUCT_PROPERTY_OVERRIDES += \
    dalvik.vm.heapgrowthlimit=160m \
    dalvik.vm.heapmaxfree=4m \
    dalvik.vm.heapsize=224m \
    dalvik.vm.heapstartsize=4m

ifneq ($(BOARD_COMPILE_ATV), false)
 PRODUCT_PROPERTY_OVERRIDES += \
     ro.sf.lcd_density=213
 else
 PRODUCT_PROPERTY_OVERRIDES += \
     ro.sf.lcd_density=160
endif

# config of surfaceflinger
PRODUCT_PROPERTY_OVERRIDES += \
    ro.surface_flinger.max_graphics_width=1280 \
    ro.surface_flinger.max_graphics_height=720 \
    ro.surface_flinger.max_frame_buffer_acquired_buffers=3 \
    ro.sf.disable_triple_buffer=0

# Inherit Go default properties, sets is-low-ram-device flag etc.
#call build/make/target/product/go_defaults.mk
PRODUCT_PROPERTY_OVERRIDES += \
     ro.config.low_ram=true \
     ro.config.per_app_memcg=false

#overlay for lowram
PRODUCT_PACKAGES += \
    LowRamOverlay

# Speed profile services and wifi-service to reduce RAM and storage.
PRODUCT_SYSTEM_SERVER_COMPILER_FILTER := speed-profile

# Always preopt extracted APKs to prevent extracting out of the APK for gms
# modules.
PRODUCT_ALWAYS_PREOPT_EXTRACTED_APK := true

# Use a profile based boot image for this device. Note that this is currently a
# generic profile and not Android Go optimized.
PRODUCT_USE_PROFILE_FOR_BOOT_IMAGE := true
PRODUCT_DEX_PREOPT_BOOT_IMAGE_PROFILE_LOCATION := frameworks/base/config/boot-image-profile.txt

# Do not generate libartd.
PRODUCT_ART_TARGET_INCLUDE_DEBUG_BUILD := false

# Strip the local variable table and the local variable type table to reduce
# the size of the system image. This has no bearing on stack traces, but will
# leave less information available via JDWP.
PRODUCT_MINIMIZE_JAVA_DEBUG_INFO := true

# Disable Scudo outside of eng builds to save RAM.
ifneq (,$(filter eng, $(TARGET_BUILD_VARIANT)))
  PRODUCT_DISABLE_SCUDO := true
endif

# Add the system properties.
PRODUCT_PROPERTY_OVERRIDES += \
	ro.lmk.critical_upgrade=true \
	ro.lmk.upgrade_pressure=40 \
	ro.lmk.downgrade_pressure=60 \
	ro.lmk.kill_heaviest_task=false \
	pm.dexopt.downgrade_after_inactive_days=10 \
	pm.dexopt.shared=quicken

# Dedupe VNDK libraries with identical core variants.
TARGET_VNDK_USE_CORE_VARIANT := true
# Inherit Go default properties end

# Reduces GC frequency of foreground apps by 50%
#PRODUCT_PROPERTY_OVERRIDES += dalvik.vm.foreground-heap-growth-multiplier=2.0

#DONT_UNCOMPRESS_PRIV_APPS_DEXS := true

else
BOARD_ENABLE_FAR_FIELD_AEC := true
endif
#########################################################################
#
#                          CCodec
#
##########################################################################

########################################################################
#
##                            HDR
#
#########################################################################
#hdr10_tmo
HDR10_TMO_MODULE := true
include device/amlogic/common/video_algorithm/hdr10_tmo/hdr10_tmo.mk


# use malloc svelte to save memory
MALLOC_SVELTE := true

########################################################################
#
#                            TV
#
########################################################################
PRODUCT_SUPPORT_DTVKIT := false
#SUPPORT_CBS := true

ifeq ($(PRODUCT_SUPPORT_DTVKIT),true)
SUPPORT_DTVKIT_IN_VENDOR := true
PRODUCT_DTVKIT_SUPPORT_ISDBT := true
TARGET_BUILD_LIVETV := true
endif

ifeq ($(SUPPORT_CBS),true)
SUPPORT_TUNERHAL := true
CBS_VERSION := V3
endif

ifeq ($(TARGET_BUILD_LIVETV),true)
PRODUCT_PACKAGES += \
    droidlogic.tv.software.core.xml

#dvbstack
BOARD_HAS_ADTV := true
#tuner
TUNER_MODULE := cxd2856 r836 av2018

TARGET_BUILD_LIBDVR := true
ifeq ($(TARGET_BUILD_LIBDVR),true)
PRODUCT_PACKAGES += \
    libamdvr
endif
endif


$(call inherit-product, device/amlogic/common/products/mbox/product_mbox.mk)
$(call inherit-product, device/amlogic/$(PRODUCT_DIR)/device.mk)
$(call inherit-product, device/amlogic/$(PRODUCT_DIR)/vendor_prop.mk)
$(call inherit-product-if-exists, vendor/amlogic/$(PRODUCT_DIR)/device-vendor.mk)

#########################################################################
#
#                                                CTC
#
#########################################################################
BUILD_WITH_CTC_MEDIAPROCESSOR := true

#########################################################################
#
#  Amlogic Media Platform API
#
#########################################################################
BUILD_WITH_AML_MP := true

#########################################################################
#
#  Media extension
#
#########################################################################
TARGET_WITH_MEDIA_EXT_LEVEL := 4


PRODUCT_NAME := $(TARGET_PRODUCT)
PRODUCT_DEVICE := $(TARGET_PRODUCT)
PRODUCT_BRAND := Amlogic
PRODUCT_MODEL := $(TARGET_PRODUCT)
PRODUCT_MANUFACTURER := Amlogic

PRODUCT_PROPERTY_OVERRIDES += \
    ro.soc.manufacturer=Amlogic \
    ro.soc.model=AMLS805X2

PRODUCT_TYPE := mbox

BOARD_AML_VENDOR_PATH := vendor/amlogic/common/
BOARD_WIDEVINE_TA_PATH := vendor/amlogic/

OTA_UP_PART_NUM_CHANGED := true

PLATFORM_TDK_VERSION := 38
BOARD_AML_SOC_TYPE ?= S805X2
BOARD_AML_TDK_KEY_PATH := device/amlogic/common/tdk_keys/
BUILD_WITH_AVB := true
BUILD_WITH_UDC := false
BOARD_USES_VBMETA_SYSTEM ?= true

#LAUNCH_ON_S is to identify a devices is
#launch on s,if the device is upgraded from Q to S
#LAUNCH_ON_S should set false
LAUNCH_ON_S ?= true
TARGET_FROM_Q_AB ?= false
TARGET_OLD_DEVICE ?= false

ifeq ($(TARGET_FROM_Q_AB), true)
LAUNCH_ON_S := false
BOARD_USES_VBMETA_SYSTEM := false
TARGET_OLD_DEVICE := true
AB_OTA_UPDATER ?=true
endif

ifeq ($(LAUNCH_ON_S),true)
TARGET_GPT_PART ?= true
AB_OTA_UPDATER ?=true
endif

ifeq ($(TARGET_GPT_PART),true)
AML_GPT_PART := device/amlogic/planck/part_table.txt
AML_EMMC_SIZE := 16G
endif

BOARD_USES_ODM_EXTIMAGE := true

ifneq ($(TARGET_OLD_DEVICE), true)
AB_OTA_UPDATER ?=true
BOARD_USES_VENDOR_DLKMIMAGE := true
BOARD_USES_ODM_DLKMIMAGE := true
else
LAUNCH_ON_S := false
endif

ifneq ($(TARGET_BUILD_KERNEL_4_9),true)
BUILDING_VENDOR_BOOT_IMAGE ?= true
endif

ifeq ($(AB_OTA_UPDATER),true)
ifeq ($(BUILDING_VENDOR_BOOT_IMAGE),true)
$(call inherit-product, $(SRC_TARGET_DIR)/product/virtual_ab_ota/compression.mk)
$(call inherit-product, $(SRC_TARGET_DIR)/product/virtual_ab_ota/launch_with_vendor_ramdisk.mk)
else
$(call inherit-product, $(SRC_TARGET_DIR)/product/virtual_ab_ota.mk)
endif
endif

PRODUCT_USE_DYNAMIC_PARTITIONS := true
#BOARD_BUILD_SYSTEM_ROOT_IMAGE := true

#########################################################################
#
#  SECURE BOOT V3
#
#########################################################################
#########Support compiling out encrypted zip/aml_upgrade_package.img directly

#need be modified later
ifeq ($(TARGET_PRODUCT),newton)
BOARD_AML_SECUREBOOT_KEY_DIR := ./bootloader/uboot-repo/bl33/v2015/board/amlogic/sm1_ac215_v1/aml-key
else ifeq ($(TARGET_PRODUCT),newton_hybrid)
BOARD_AML_SECUREBOOT_KEY_DIR := ./bootloader/uboot-repo/bl33/v2015/board/amlogic/sm1_ac232_v1/aml-key
else ifeq ($(TARGET_PRODUCT),ac212)
BOARD_AML_SECUREBOOT_KEY_DIR := ./bootloader/uboot-repo/bl33/v2015/board/amlogic/sm1_ac212_v1/aml-key
else
BOARD_AML_SECUREBOOT_KEY_DIR := ./bootloader/uboot-repo/bl33/v2015/board/amlogic/sm1_ac214_v1/aml-key
endif

BOARD_AML_SECUREBOOT_SOC_TYPE := sc2

PRODUCT_GOOGLEREF_SECURE_BOOT := false
ifeq ($(PRODUCT_GOOGLEREF_SECURE_BOOT),true)
PRODUCT_GOOGLEREF_SECURE_BOOT_TOOL := ./device/amlogic/$(PRODUCT_DIR)/tools/amlogic-sign-planck.sh
endif

#########################################################################
#
#  Dm-Verity
#
#########################################################################
#TARGET_USE_SECURITY_DM_VERITY_MODE_WITH_TOOL := true

#########################################################################
#
#                      WiFi and Bluetooth
#
#########################################################################
include vendor/amlogic/common/wifi_bt/wifi/configs/wifi.mk
BOARD_HAVE_BLUETOOTH := true
include vendor/amlogic/common/wifi_bt/bluetooth/configs/bluetooth.mk

#########################################################################
#
# Audio
#
#########################################################################
BOARD_ALSA_AUDIO=tiny
include device/amlogic/common/audio.mk

#########################################################################
#
#  PlayReady DRM
#
#########################################################################
#export BOARD_PLAYREADY_LEVEL=3 for PlayReady+NOTVP
#export BOARD_PLAYREADY_LEVEL=1 for PlayReady+OPTEE+TVP

#########################################################################
#
#  Verimatrix DRM
#
##########################################################################
#verimatrix web
BUILD_WITH_VIEWRIGHT_WEB := false
#verimatrix stb
BUILD_WITH_VIEWRIGHT_STB := false

#########################################################################

#########################################################################
#
#  WifiDisplay
#
##########################################################################
ifeq ($(BOARD_COMPILE_ATV), false)
BUILD_WITH_MIRACAST := true
endif

#########################################################################

########################################################################
#
#                        dsp_util
#
########################################################################
PRODUCT_PACKAGES += \
    dsp_util \
    hifi4_rpc_test \
    hifi4rpc_client_test

########################################################################
#
#                          Netflix
#
#########################################################################
#TARGET_BUILD_NETFLIX:= true
#TARGET_BUILD_NETFLIX_MGKID := true
#TARGET_BUILD_NETFLIX_MODELGROUP:= XXXXX

#######################################################################
#
#           enable MIC toggle if FFM is enabled
#
#######################################################################
BOARD_ENABLE_LIGHT_CONTROL := true
ifeq ($(BOARD_ENABLE_FAR_FIELD_AEC), true)
BOARD_HAS_MIC_TOGGLE := true
endif

########################################################################
#
#                          Audio License Decoder
#  Dolby license:
#  TARGET_DOLBY_VERSION: use to control the dolby version
#  ms12_v2: support ms12 v2.4
#  ms12_v1: support ms12 v1.3
#  ddp_only: only support ddp decoder
#  non_dolby: not support dolby decoder
#
#  DTS license:
#  TARGET_DTS_VERSION: use to control the dts version
#  dtshd: support dts hd decoder
#  non_dts: not support dts decoder
########################################################################
TARGET_DOLBY_VERSION ?= non_dolby
ifeq ($(TARGET_DOLBY_VERSION), ms12_v2)
    TARGET_BUILD_DOLBY_MS12_V2 := true
else ifeq ($(TARGET_DOLBY_VERSION), ms12_v1)
    TARGET_BUILD_DOLBY_MS12_V1 := true
else ifeq ($(TARGET_DOLBY_VERSION), ddp_only)
    TARGET_BUILD_DOLBY_DDP := true
else
    TARGET_BUILD_DOLBY_DDP := false
endif

TARGET_DTS_VERSION ?= non_dts
ifeq ($(TARGET_DTS_VERSION), dtshd)
    TARGET_BUILD_DTSHD := true
else
    TARGET_BUILD_DTSHD := false
endif

########################################################################
#  This control decide whether it need to be compatible
#  between ms12_v2 and non_dolby version
########################################################################

ifneq ($(TARGET_BUILD_DOLBY_MS12_V1), true)

TARGET_BUILD_COMPATIBLE_MS12_V2 ?= true

ifeq ($(TARGET_BUILD_DOLBY_MS12_V2), true)
    TARGET_INSTALL_DOLBY_MS12_V2 :=true
else ifeq ($(TARGET_BUILD_COMPATIBLE_MS12_V2), true)
    TARGET_INSTALL_DOLBY_MS12_V2 :=true
    $(warning "This non ms12 build, but it is compatible with ms12")
endif
endif

#TARGET_BUILD_WITH_DOVI := true

#################################################################################
#
#  DEFAULT LOWMEMORYKILLER CONFIG
#
#################################################################################
BUILD_WITH_LOWMEM_COMMON_CONFIG := true

BOARD_USES_USB_PM := true

#########################################################################
#
#           OEM Partitions based dynamic fingerprint
#
#########################################################################
BOARD_USES_DYNAMIC_FINGERPRINT ?= true

ifeq ($(AB_OTA_UPDATER),true)
my_src_fstab := fstab.ab
else
my_src_fstab := fstab.system
endif

ifeq ($(BOARD_USES_DYNAMIC_FINGERPRINT),true)
my_src_fstab := $(my_src_fstab)_oem
endif

ifeq ($(TARGET_OLD_DEVICE), true)
my_src_fstab := fstab.ab.old
endif

my_dst_fstab := $(TARGET_COPY_OUT_VENDOR_RAMDISK)/first_stage_ramdisk/fstab.amlogic

PRODUCT_COPY_FILES += \
    device/amlogic/$(PRODUCT_DIR)/$(my_src_fstab).amlogic:$(TARGET_COPY_OUT_VENDOR)/etc/fstab.amlogic \
    device/amlogic/$(PRODUCT_DIR)/$(my_src_fstab).amlogic:$(my_dst_fstab)

$(call inherit-product, device/amlogic/common/media.mk)

#1G platform can't support vulkan, cts case need at least 1073741824 byte memory
BOARD_INSTALL_VULKAN := false

include device/amlogic/common/gpu/dvalin-user-arm64.mk

include device/amlogic/common/products/mbox/s4/s4.mk

#########################################################################
#
##                                     Auto Patch
#                          must put in the end of mk files
##########################################################################
AUTO_PATCH_SHELL_FILE := vendor/amlogic/common/pre_submit_for_google/auto_patch.sh
HAVE_WRITED_SHELL_FILE := $(shell test -f $(AUTO_PATCH_SHELL_FILE) && echo yes)

ifeq ($(IS_REFERENCE_PROJECT), true)
REFERENCE_PARAMS := true
else
REFERENCE_PARAMS := false
endif

ifneq ($(BOARD_COMPILE_ATV), false)
ATV_PARAMS := true
else
ATV_PARAMS := false
endif

ifeq ($(TARGET_BUILD_LIVETV), true)
LIVETV_PARAMS := true
else
LIVETV_PARAMS := false
endif

ifeq ($(HAVE_WRITED_SHELL_FILE),yes)
SCRIPT_RESULT :=$(shell ($(AUTO_PATCH_SHELL_FILE) $(REFERENCE_PARAMS) $(LIVETV_PARAMS)  $(ATV_PARAMS) ))
ifeq ($(filter Error,$(SCRIPT_RESULT)), Error)
$(error $(SCRIPT_RESULT))
else
$(warning $(SCRIPT_RESULT))
endif
endif
PRODUCT_COPY_FILES += \
    device/amlogic/$(PRODUCT_DIR)/ueventd.parallel.rc:$(TARGET_COPY_OUT_ODM)/ueventd.rc
