diff --git a/include/hasp_png.h b/include/hasp_mem.h similarity index 66% rename from include/hasp_png.h rename to include/hasp_mem.h index ee1325cd..1c87d68c 100644 --- a/include/hasp_png.h +++ b/include/hasp_mem.h @@ -1,26 +1,32 @@ /* MIT License - Copyright (c) 2019-2021 Francis Van Roie For full license information read the LICENSE file in the project folder */ -#ifndef HASP_PNG_H -#define HASP_PNG_H +#ifndef HASP_MEM_H +#define HASP_MEM_H #include -#ifdef LODEPNG_NO_COMPILE_ALLOCATORS #ifdef __cplusplus extern "C" { #endif +#ifdef LODEPNG_NO_COMPILE_ALLOCATORS void* lodepng_calloc(size_t num,size_t size); void* lodepng_malloc(size_t size); void* lodepng_realloc(void* ptr, size_t new_size); void lodepng_free(void* ptr); +#endif // LODEPNG_NO_COMPILE_ALLOCATORS + +bool hasp_use_psram(); +void* hasp_calloc(size_t num,size_t size); +void* hasp_malloc(size_t size); +void* hasp_realloc(void* ptr, size_t new_size); +void hasp_free(void* ptr); #ifdef __cplusplus } #endif -#endif // LODEPNG_NO_COMPILE_ALLOCATORS -#endif // HASP_PNG_H \ No newline at end of file +#endif // HASP_MEM_H \ No newline at end of file diff --git a/lib/freetype b/lib/freetype index aa40dd17..626fd657 160000 --- a/lib/freetype +++ b/lib/freetype @@ -1 +1 @@ -Subproject commit aa40dd17b51500448a5601478a4278d21ac74386 +Subproject commit 626fd6572742635926a1af501fc27b35db6585f8 diff --git a/src/font/hasp_font_loader.cpp b/src/font/hasp_font_loader.cpp index b1977894..fc8baec3 100644 --- a/src/font/hasp_font_loader.cpp +++ b/src/font/hasp_font_loader.cpp @@ -412,14 +412,7 @@ static int32_t load_glyph(lv_fs_file_t* fp, lv_font_fmt_txt_dsc_t* font_dsc, uin } uint8_t* glyph_bmp; -#ifdef ESP32 - if(psramFound()) - glyph_bmp = (uint8_t*)ps_malloc(sizeof(uint8_t) * cur_bmp_size); - else - glyph_bmp = (uint8_t*)malloc(sizeof(uint8_t) * cur_bmp_size); -#else - glyph_bmp = (uint8_t*)malloc(sizeof(uint8_t) * cur_bmp_size); -#endif + glyph_bmp = (uint8_t*)hasp_malloc(sizeof(uint8_t) * cur_bmp_size); font_dsc->glyph_bitmap = glyph_bmp; diff --git a/src/hasp/hasp_attribute.cpp b/src/hasp/hasp_attribute.cpp index cdb7a8d4..74905946 100644 --- a/src/hasp/hasp_attribute.cpp +++ b/src/hasp/hasp_attribute.cpp @@ -9,12 +9,6 @@ #include "hasp_attribute_helper.h" /*** Image Improvement ***/ -#if defined(ARDUINO_ARCH_ESP8266) -#include -#define lodepng_malloc malloc -#define lodepng_free free -#endif - #if defined(ARDUINO_ARCH_ESP32) #include #endif @@ -22,7 +16,6 @@ #if HASP_USE_PNGDECODE > 0 #include "lv_png.h" #include "lodepng.h" -#include "hasp_png.h" #endif /*** Image Improvement ***/ @@ -1071,7 +1064,7 @@ static hasp_attribute_type_t special_attribute_src(lv_obj_t* obj, const char* pa int len = total; int read = 0; lv_img_dsc_t* img_dsc = (lv_img_dsc_t*)lv_mem_alloc(sizeof(lv_img_dsc_t)); - uint8_t* img_buf = (uint8_t*)(len > 0 ? lodepng_malloc(len) : NULL); + uint8_t* img_buf = (uint8_t*)(len > 0 ? hasp_malloc(len) : NULL); LOG_VERBOSE(TAG_ATTR, "HTTP OK: buffer created of %d bytes", len); diff --git a/src/hasp_png.cpp b/src/hasp/hasp_mem.cpp similarity index 57% rename from src/hasp_png.cpp rename to src/hasp/hasp_mem.cpp index ef9517a7..f42992b8 100644 --- a/src/hasp_png.cpp +++ b/src/hasp/hasp_mem.cpp @@ -2,23 +2,49 @@ /* MIT License - Copyright (c) 2019-2021 Francis Van Roie For full license information read the LICENSE file in the project folder */ -#ifdef LODEPNG_NO_COMPILE_ALLOCATORS - #include #include "hasplib.h" -#include "hasp_png.h" +#include "hasp_mem.h" +#ifdef ESP32 +bool hasp_use_psram() +{ + return psramFound() && ESP.getPsramSize() > 0; +} +#endif + +void* hasp_malloc(size_t size) +{ +#ifdef ESP32 + return hasp_use_psram() ? ps_malloc(size) : malloc(size); +#else + return malloc(size); +#endif +} + +/* NOTE: when realloc returns NULL, it leaves the original memory untouched */ +void* hasp_realloc(void* ptr, size_t new_size) +{ +#ifdef ESP32 + return hasp_use_psram() ? ps_realloc(ptr, new_size) : realloc(ptr, new_size); +#else + return realloc(ptr, new_size); +#endif +} + +void hasp_free(void* ptr) +{ + free(ptr); +} + +#ifdef LODEPNG_NO_COMPILE_ALLOCATORS void* lodepng_malloc(size_t size) { #ifdef LODEPNG_MAX_ALLOC if(size > LODEPNG_MAX_ALLOC) return 0; #endif -#ifdef ESP32 - return psramFound() ? ps_malloc(size) : malloc(size); -#else - return malloc(size); -#endif + return hasp_malloc(size); } /* NOTE: when realloc returns NULL, it leaves the original memory untouched */ @@ -28,16 +54,11 @@ void* lodepng_realloc(void* ptr, size_t new_size) if(new_size > LODEPNG_MAX_ALLOC) return 0; #endif -#ifdef ESP32 - return psramFound() ? ps_realloc(ptr, new_size) : realloc(ptr, new_size); -#else - return realloc(ptr, new_size); -#endif + return hasp_realloc(ptr, new_size); } void lodepng_free(void* ptr) { - free(ptr); + hasp_free(ptr); } - #endif // LODEPNG_NO_COMPILE_ALLOCATORS \ No newline at end of file diff --git a/src/hasplib.h b/src/hasplib.h index ba584bc2..793ea762 100644 --- a/src/hasplib.h +++ b/src/hasplib.h @@ -11,6 +11,7 @@ #include #include "hasp_conf.h" +#include "hasp_mem.h" #include "lv_conf.h" #include "lvgl.h"