C

Qt Quick Ultralite image_cache Example

# Copyright (C) 2026 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial
cmake_minimum_required(VERSION 3.21.1)

project(image_cache VERSION 0.0.1 LANGUAGES C CXX ASM)

find_package(QulApp)

# All photos have the same width, height and format. We display 4 photo slideshow.
# For user convenience we need UL-3093 and also need to clarify UL-3097.
if (QUL_PLATFORM MATCHES "^tviic2d4m")
# 276480 is decompressed photo size in bytes (QImage) for 16 bits per pixel
math(EXPR cache_size_in_bytes "276480 * 4")
else()
# 552960 is decompressed photo size in bytes (QImage) for 32 bits per pixel
math(EXPR cache_size_in_bytes "552960 * 4")
endif()

if(QUL_PLATFORM MATCHES "autosar")
    qul_add_library(image_cache)
else()
    qul_add_executable(image_cache)
endif()

set(IMAGE_CACHE_PHOTOS
    images/basse-terre-guadeloupe.jpg
    images/grand-canyon-arizona.jpg
    images/grand-canyon-national-park.jpg
    images/Jokulsarlon-glacier-lagoon-iceland.jpg
    images/reine-lofoten-islands.jpg
    images/sequoia-national-park.jpg
    images/sogn-og-fjordane.jpg
    images/yosemite-national-park.jpg
)

set_source_files_properties(${IMAGE_CACHE_PHOTOS} PROPERTIES
    QUL_RESOURCE_CACHE_POLICY "OnDemand"
)

if(QUL_PLATFORM MATCHES "^tviic2d4m")
    set_source_files_properties(${IMAGE_CACHE_PHOTOS} PROPERTIES
        QUL_RESOURCE_IMAGE_PIXEL_FORMAT "RGB565"
    )
elseif(QUL_PLATFORM MATCHES "^rh850-d1m1a")
    # RH850 doesn't have enough internal flash memory
    set_source_files_properties(${IMAGE_CACHE_PHOTOS} PROPERTIES
        QUL_RESOURCE_COMPRESSION ON
    )
endif()

qul_add_qml_module(image_cache
    URI ImageCacheApp
    QML_FILES image_cache.qml
    RESOURCES ${IMAGE_CACHE_PHOTOS}
    SOURCES
        ImageModel.cpp
        ImageModel.h
)

qul_set_maximum_resource_cache_size(image_cache CACHE_SIZE ${cache_size_in_bytes})

if(QUL_PLATFORM MATCHES "^ek-ra8d1")
    set_target_properties(image_cache PROPERTIES
        QUL_RESOURCE_STORAGE_SECTION "QulResourceDataInExternalFlash"
    )
endif()

# Set FreeRTOS heap size (needed for cache entries)
# STM32F769i has a limited amount of available heap memory in SRAM, but a custom
# memory allocator ensures that images are kept in the larger SDRAM area.
if (QUL_OS STREQUAL "FreeRTOS" AND NOT (QUL_PLATFORM MATCHES "^stm32f769i" OR QUL_PLATFORM MATCHES "^mimxrt10"))
    if (QUL_PLATFORM STREQUAL "mimxrt1170-evkb-freertos")
        # NXP 1170 needs additional 2*3.6MB for the screen layer
        qul_override_freertos_heap_size(image_cache "8 * 1024 * 1024 + ${cache_size_in_bytes}")
    else()
        qul_override_freertos_heap_size(image_cache "1024 * 1024 + ${cache_size_in_bytes}")
    endif()
endif()

app_target_setup_os(image_cache)
app_target_default_entrypoint(image_cache image_cache MODULE ImageCacheApp)