Stop leaking textures
authorMateusz Majewski <m.majewski2@samsung.com>
Tue, 25 Jun 2024 10:58:02 +0000 (12:58 +0200)
committerMateusz Majewski <m.majewski2@samsung.com>
Tue, 25 Jun 2024 10:58:02 +0000 (12:58 +0200)
Change-Id: I3024cfac635dbb8a8af623da0afb6fceb02771a7

tizen/src/ui/qt5.c
tizen/src/ui/qt5_supplement.cpp
tizen/src/ui/qt5_supplement.h

index e6d095fd74e45d8d70eebdd796866a97e9ab2d98..f1739b5837f3a3c6f497cf0722c7e78541821ced 100644 (file)
@@ -194,7 +194,11 @@ static void qt5_gl_refresh(DisplayChangeListener *dcl)
 
     graphic_hw_update(dcl->con);
     if (con->surface && !con->is_scanout && con->updated) {
-        con->surface->texture = qt5_gl_refresh_internal(con->surface->texture, surface_width(con->surface), surface_height(con->surface), con->gen);
+        bool should_free;
+        con->surface->texture = qt5_gl_refresh_internal(con->surface->texture, surface_width(con->surface), surface_height(con->surface), con->gen, &should_free);
+        if (should_free) {
+            surface_gl_destroy_texture(con->gls, con->surface);
+        }
         if (con->surface->texture == 0) {
             surface_gl_create_texture(con->gls, con->surface);
         }
@@ -274,7 +278,11 @@ static void qt5_gl_scanout_flush(DisplayChangeListener *dcl,
                       0, 0, surface_width(con->surface), surface_height(con->surface),
                       GL_COLOR_BUFFER_BIT, GL_NEAREST);
 
-    con->surface->texture = qt5_gl_refresh_internal(con->surface->texture, surface_width(con->surface), surface_height(con->surface), con->gen);
+    bool should_free;
+    con->surface->texture = qt5_gl_refresh_internal(con->surface->texture, surface_width(con->surface), surface_height(con->surface), con->gen, &should_free);
+    if (should_free) {
+        surface_gl_destroy_texture(con->gls, con->surface);
+    }
     if (con->surface->texture == 0) {
         surface_gl_create_texture(con->gls, con->surface);
     }
index e2cbfe214eddc2ec0276a8937c934d15c00c4e63..35354c2cfacc2e01426ff2dc903b01a5cf6ce0cb 100644 (file)
@@ -615,9 +615,10 @@ void qt5_update_texture(void *dpy_item)
 static dpy_item dpy_item_pool[DPY_ITEM_NO];
 static uint64_t dpy_item_gen[DPY_ITEM_NO];
 
-uint32_t qt5_gl_refresh_internal(uint32_t tex, uint32_t width, uint32_t height, uint64_t gen)
+uint32_t qt5_gl_refresh_internal(uint32_t tex, uint32_t width, uint32_t height, uint64_t gen, bool *should_free)
 {
     uint32_t ret = tex;
+    *should_free = false;
 
     if (mainwindow) {
         int item_id = 0;
@@ -627,12 +628,13 @@ uint32_t qt5_gl_refresh_internal(uint32_t tex, uint32_t width, uint32_t height,
             qemu_mutex_lock(&item->mutex);
             bool ok = dpy_item_gen[item_id] == 0 || item->available;
             if (ok) {
-                if (dpy_item_gen[item_id] == gen && /* HACK HACK HACK */ item->tex >= 25) {
-                    ret = item->tex;
-                } else {
-                    // TODO: we will leak a texture soon!
+                ret = item->tex;
+                if (dpy_item_gen[item_id] != gen || /* HACK HACK HACK */ item->tex < 25) {
+                    /* This tells the caller that the texture should not be used anymore.
+                     * Preferably we could handle this right here, but the caller can do it
+                     * conveniently and I couldn't get compilation to work. */
                     dpy_item_gen[item_id] = gen;
-                    ret = 0;
+                    *should_free = true;
                 }
                 item->tex = tex;
                 item->width = width;
index 8c5878e0de43ff00106a9e38c76e70406969b760..b5ec02a8c0ee11c36f41330d30bb39681ba073d8 100644 (file)
@@ -64,7 +64,7 @@ const char* qt5_get_version(void);
 void qt5_set_force_legacy(bool isLegacy);
 
 #ifdef CONFIG_OPENGL
-uint32_t qt5_gl_refresh_internal(uint32_t tex, uint32_t width, uint32_t height, uint64_t gen);
+uint32_t qt5_gl_refresh_internal(uint32_t tex, uint32_t width, uint32_t height, uint64_t gen, bool *should_free);
 void *qt5_gl_create_context_internal(int major, int minor);
 void qt5_gl_destroy_context_internal(void *);
 int qt5_gl_make_context_current_internal(void *_ctx);