From 453f01b4c76164f83e2173e45ce18d7334b99d57 Mon Sep 17 00:00:00 2001 From: Yousef Khadadeh Date: Thu, 7 Aug 2025 22:27:54 +0300 Subject: [PATCH] Disabled partial cache refresh for now due to many issues related to it, and also static images dont constantly require updating --- src/DrawingProgram/DrawingProgram.cpp | 1 + src/DrawingProgram/DrawingProgramCache.cpp | 96 +++++++++++--------- src/DrawingProgram/DrawingProgramCache.hpp | 2 +- src/ResourceDisplay/ImageResourceDisplay.cpp | 2 +- 4 files changed, 54 insertions(+), 47 deletions(-) diff --git a/src/DrawingProgram/DrawingProgram.cpp b/src/DrawingProgram/DrawingProgram.cpp index cef2837..091cc52 100644 --- a/src/DrawingProgram/DrawingProgram.cpp +++ b/src/DrawingProgram/DrawingProgram.cpp @@ -382,6 +382,7 @@ void DrawingProgram::add_file_to_canvas_by_path_execute(const std::string& fileP img->d.imageID = imageID; img->commit_update(*this); uint64_t placement = components.client_list().size(); + std::cout << "tiger place: " << placement << std::endl; auto objAdd = components.client_insert(placement, img); img->client_send_place(*this); add_undo_place_component(objAdd); diff --git a/src/DrawingProgram/DrawingProgramCache.cpp b/src/DrawingProgram/DrawingProgramCache.cpp index d745072..b3cdcd3 100644 --- a/src/DrawingProgram/DrawingProgramCache.cpp +++ b/src/DrawingProgram/DrawingProgramCache.cpp @@ -269,20 +269,23 @@ void DrawingProgramCache::invalidate_cache_at_optional_aabb_before_pos(const std void DrawingProgramCache::invalidate_cache_at_aabb_before_pos(const SCollision::AABB& aabb, uint64_t placementToInvalidateAt) { std::erase_if(nodesWithCachedSurfaces, [&](auto& n) { if(placementToInvalidateAt <= n->drawCache.value().lastDrawnComponentPlacement && SCollision::collide(aabb, n->bounds)) { - if(aabb.fully_contains_aabb(n->bounds)) { + //if(aabb.fully_contains_aabb(n->bounds)) { + // NOTE: For now, we will invalidate the entire cache when something needs updating + // Invalidating parts of the cache can lead to massive speed ups with eraser and GIFs, but it currently has accuracy issues + // related to determining what's been drawn last n->drawCache = std::nullopt; return true; - } - else { - auto& dCache = n->drawCache.value(); - if(dCache.invalidBounds.has_value()) { - auto& iBounds = dCache.invalidBounds.value(); - iBounds.include_aabb_in_bounds(aabb); - iBounds = n->bounds.get_intersection_between_aabbs(iBounds); - } - else - dCache.invalidBounds = n->bounds.get_intersection_between_aabbs(aabb); - } + //} + //else { + // auto& dCache = n->drawCache.value(); + // if(dCache.invalidBounds.has_value()) { + // auto& iBounds = dCache.invalidBounds.value(); + // iBounds.include_aabb_in_bounds(aabb); + // iBounds = n->bounds.get_intersection_between_aabbs(iBounds); + // } + // else + // dCache.invalidBounds = n->bounds.get_intersection_between_aabbs(aabb); + //} } return false; }); @@ -292,7 +295,7 @@ void DrawingProgramCache::refresh_all_draw_cache(const DrawData& drawData) { std::deque> nodeFlatList; // We want to render children before parents, so that parents can make use of the cached children traverse_bvh_run_function(drawData.cam.viewingAreaGenerousCollider, [&](std::shared_ptr node, const std::vector& comps) { if(node && node->coords.inverseScale <= drawData.cam.c.inverseScale) { - if(!node->drawCache || node->drawCache.value().invalidBounds) + if(!node->drawCache /*|| node->drawCache.value().invalidBounds*/) nodeFlatList.emplace_front(node); return false; } @@ -330,45 +333,48 @@ void DrawingProgramCache::refresh_draw_cache(const std::shared_ptrresolution.cast()); cacheDrawData.refresh_draw_optimizing_values(); - if(drawCache.invalidBounds) { - auto& iBounds = drawCache.invalidBounds.value(); - WorldVec bDim = bvhNode->bounds.dim(); + //if(drawCache.invalidBounds) { + // auto& iBounds = drawCache.invalidBounds.value(); + // WorldVec bDim = bvhNode->bounds.dim(); - Vector2f clipBoundMin{static_cast((iBounds.min.x() - bvhNode->bounds.min.x()) / bDim.x()) * bvhNode->resolution.x(), - static_cast((iBounds.min.y() - bvhNode->bounds.min.y()) / bDim.y()) * bvhNode->resolution.y()}; - Vector2f clipBoundMax{static_cast((iBounds.max.x() - bvhNode->bounds.min.x()) / bDim.x()) * bvhNode->resolution.x(), - static_cast((iBounds.max.y() - bvhNode->bounds.min.y()) / bDim.y()) * bvhNode->resolution.y()}; + // Vector2f clipBoundMin{static_cast((iBounds.min.x() - bvhNode->bounds.min.x()) / bDim.x()) * bvhNode->resolution.x(), + // static_cast((iBounds.min.y() - bvhNode->bounds.min.y()) / bDim.y()) * bvhNode->resolution.y()}; + // Vector2f clipBoundMax{static_cast((iBounds.max.x() - bvhNode->bounds.min.x()) / bDim.x()) * bvhNode->resolution.x(), + // static_cast((iBounds.max.y() - bvhNode->bounds.min.y()) / bDim.y()) * bvhNode->resolution.y()}; - SkIRect clipRect = SkIRect::MakeLTRB(clipBoundMin.x() - 2, - clipBoundMin.y() - 2, - clipBoundMax.x() + 2, - clipBoundMax.y() + 2); + // SkIRect clipRect = SkIRect::MakeLTRB(clipBoundMin.x() - 2, + // clipBoundMin.y() - 2, + // clipBoundMax.x() + 2, + // clipBoundMax.y() + 2); - SCollision::AABB clipRectBoundAABB{clipBoundMin - Vector2f{4, 4}, clipBoundMax + Vector2f{4, 4}}; - SCollision::AABB clipRectBoundAABBWorld{ - bvhNode->coords.from_space(clipRectBoundAABB.min), - bvhNode->coords.from_space(clipRectBoundAABB.max) - }; + // SCollision::AABB clipRectBoundAABB{clipBoundMin - Vector2f{4, 4}, clipBoundMax + Vector2f{4, 4}}; + // SCollision::AABB clipRectBoundAABBWorld{ + // bvhNode->coords.from_space(clipRectBoundAABB.min), + // bvhNode->coords.from_space(clipRectBoundAABB.max) + // }; - cacheCanvas->save(); - cacheCanvas->clipIRect(clipRect); - cacheCanvas->clear(SkColor4f{0, 0, 0, 0}); - uint64_t lastDrawnComponentPlacementNew = 0; - draw_components_to_canvas(cacheCanvas, cacheDrawData, { - .lastDrawnComponentPlacement = &lastDrawnComponentPlacementNew, - .drawBounds = clipRectBoundAABBWorld - }); - drawCache.lastDrawnComponentPlacement = std::max(lastDrawnComponentPlacementNew, drawCache.lastDrawnComponentPlacement); - cacheCanvas->restore(); - drawCache.invalidBounds = std::nullopt; - } - else { + // cacheCanvas->save(); + // cacheCanvas->clipIRect(clipRect); + // cacheCanvas->clear(SkColor4f{0, 0, 0, 0}); + // uint64_t lastDrawnComponentPlacementNew = 0; + // draw_components_to_canvas(cacheCanvas, cacheDrawData, { + // .lastDrawnComponentPlacement = &lastDrawnComponentPlacementNew, + // .drawBounds = clipRectBoundAABBWorld, + // .lastObjectPositionToDraw = drawCache.lastDrawnComponentPlacement + // }); + // // NOTE: We make sure that the last object position drawn remains the same, so we dont have to set it to a different value after we're done here + // // drawCache.lastDrawnComponentPlacement = std::max(lastDrawnComponentPlacementNew, drawCache.lastDrawnComponentPlacement); + // std::cout << drawCache.lastDrawnComponentPlacement << std::endl; + // cacheCanvas->restore(); + // drawCache.invalidBounds = std::nullopt; + //} + //else { cacheCanvas->clear(SkColor4f{0, 0, 0, 0}); draw_components_to_canvas(cacheCanvas, cacheDrawData, { .lastDrawnComponentPlacement = &drawCache.lastDrawnComponentPlacement, - .drawBounds = drawCache.invalidBounds + //.drawBounds = drawCache.invalidBounds }); - } + //} drawCache.lastRenderTime = std::chrono::steady_clock::now(); drawCache.attachedCache = this; @@ -399,7 +405,7 @@ void DrawingProgramCache::draw_components_to_canvas(SkCanvas* canvas, const Draw std::vector uncachedCompsToDraw; traverse_bvh_run_function(optData.drawBounds.has_value() ? optData.drawBounds.value() : drawData.cam.viewingAreaGenerousCollider, [&](const std::shared_ptr& node, const std::vector& comps) { - if(node && node->drawCache && !node->drawCache.value().invalidBounds && node->coords.inverseScale <= drawData.cam.c.inverseScale) { + if(node && node->drawCache /*&& !node->drawCache.value().invalidBounds*/ && node->coords.inverseScale <= drawData.cam.c.inverseScale) { cachedNodesToDraw.emplace_back(node); return false; } diff --git a/src/DrawingProgram/DrawingProgramCache.hpp b/src/DrawingProgram/DrawingProgramCache.hpp index d3e4c7c..ae781a9 100644 --- a/src/DrawingProgram/DrawingProgramCache.hpp +++ b/src/DrawingProgram/DrawingProgramCache.hpp @@ -22,7 +22,7 @@ class DrawingProgramCacheBVHNode { sk_sp surface; std::chrono::steady_clock::time_point lastRenderTime; DrawingProgramCache* attachedCache; - std::optional> invalidBounds; + //std::optional> invalidBounds; }; std::optional drawCache; diff --git a/src/ResourceDisplay/ImageResourceDisplay.cpp b/src/ResourceDisplay/ImageResourceDisplay.cpp index 08186e8..1177957 100644 --- a/src/ResourceDisplay/ImageResourceDisplay.cpp +++ b/src/ResourceDisplay/ImageResourceDisplay.cpp @@ -20,10 +20,10 @@ bool ImageResourceDisplay::update_draw() const { } void ImageResourceDisplay::update(World& w) { + mustUpdateDraw = false; if(frames.size() <= 1) return; currentTime += w.main.deltaTime * 1000.0; - mustUpdateDraw = false; for(;;) { if(currentTime >= frames[frameIndex].duration) { currentTime -= frames[frameIndex].duration;