2026-05-11 23:06:39 +03:00
|
|
|
/*
|
|
|
|
|
* InfiniPaint
|
|
|
|
|
* Copyright (C) 2025-2026 Yousef Khadadeh
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
#include "ImageResourceDisplay.hpp"
|
2026-02-17 14:41:08 +03:00
|
|
|
#include <chrono>
|
2025-07-06 20:43:53 +03:00
|
|
|
#include <include/codec/SkPngDecoder.h>
|
|
|
|
|
#include <include/codec/SkJpegDecoder.h>
|
|
|
|
|
#include <include/codec/SkBmpDecoder.h>
|
|
|
|
|
#include <include/codec/SkIcoDecoder.h>
|
|
|
|
|
#include <include/codec/SkGifDecoder.h>
|
2025-08-13 19:23:14 +03:00
|
|
|
//#include <include/codec/SkRawDecoder.h>
|
2025-08-11 21:34:01 +03:00
|
|
|
#include <include/codec/SkWbmpDecoder.h>
|
|
|
|
|
#include <include/codec/SkWebpDecoder.h>
|
2026-01-06 12:36:15 +03:00
|
|
|
#include <include/core/SkCanvas.h>
|
2025-07-06 20:43:53 +03:00
|
|
|
#include "../MainProgram.hpp"
|
|
|
|
|
#include <Helpers/Logger.hpp>
|
2026-02-19 15:21:47 +03:00
|
|
|
#include <include/core/SkImageInfo.h>
|
|
|
|
|
#include <include/core/SkSamplingOptions.h>
|
2026-02-19 22:02:18 +03:00
|
|
|
#include <bit>
|
|
|
|
|
#include <limits>
|
2025-07-06 20:43:53 +03:00
|
|
|
|
2026-02-16 18:19:17 +03:00
|
|
|
#ifdef USE_SKIA_BACKEND_GRAPHITE
|
|
|
|
|
#include <include/gpu/graphite/Surface.h>
|
|
|
|
|
#elif USE_SKIA_BACKEND_GANESH
|
|
|
|
|
#include <include/gpu/ganesh/GrDirectContext.h>
|
|
|
|
|
#include <include/gpu/ganesh/SkSurfaceGanesh.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-02-19 15:21:47 +03:00
|
|
|
std::array<const SkCodecs::Decoder, 7> decoders2 = {
|
2025-07-06 20:43:53 +03:00
|
|
|
SkBmpDecoder::Decoder(),
|
2025-08-11 21:34:01 +03:00
|
|
|
SkGifDecoder::Decoder(),
|
2025-07-06 20:43:53 +03:00
|
|
|
SkIcoDecoder::Decoder(),
|
2025-08-11 21:34:01 +03:00
|
|
|
SkJpegDecoder::Decoder(),
|
|
|
|
|
SkPngDecoder::Decoder(),
|
2025-08-13 19:23:14 +03:00
|
|
|
// SkRawDecoder::Decoder(),
|
2025-08-11 21:34:01 +03:00
|
|
|
SkWbmpDecoder::Decoder(),
|
|
|
|
|
SkWebpDecoder::Decoder()
|
2025-07-06 20:43:53 +03:00
|
|
|
};
|
|
|
|
|
|
2026-02-19 15:21:47 +03:00
|
|
|
std::unordered_map<std::shared_ptr<std::string>, sk_sp<SkImage>> ImageResourceDisplay::screenshotCache;
|
|
|
|
|
std::atomic<int> ImageResourceDisplay::imageLoadThreadCount = 0;
|
|
|
|
|
|
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
|
int ImageResourceDisplay::IMAGE_LOAD_THREAD_COUNT_MAX = 1;
|
|
|
|
|
#else
|
|
|
|
|
int ImageResourceDisplay::IMAGE_LOAD_THREAD_COUNT_MAX = 4;
|
|
|
|
|
#endif
|
2025-10-10 18:41:08 +03:00
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
bool ImageResourceDisplay::update_draw() const {
|
|
|
|
|
return mustUpdateDraw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageResourceDisplay::update(World& w) {
|
2026-02-19 15:21:47 +03:00
|
|
|
screenshotCache.clear();
|
|
|
|
|
|
|
|
|
|
for(size_t mipmapLevel = 0; mipmapLevel < mipmapLevelsStatus.size(); mipmapLevel++) {
|
|
|
|
|
if(mipmapLevelsStatus[mipmapLevel] == MipmapLevelStatus::ALLOCATED_JUST_SET)
|
|
|
|
|
mipmapLevelsStatus[mipmapLevel] = MipmapLevelStatus::ALLOCATED_WILL_REMOVE;
|
|
|
|
|
else if(mipmapLevelsStatus[mipmapLevel] == MipmapLevelStatus::ALLOCATED_WILL_REMOVE) {
|
|
|
|
|
mipmapLevelsStatus[mipmapLevel] = MipmapLevelStatus::UNALLOCATED;
|
|
|
|
|
for(auto& frame : frames)
|
|
|
|
|
frame.mipmapLevels[mipmapLevel] = nullptr;
|
|
|
|
|
}
|
2026-02-17 14:41:08 +03:00
|
|
|
}
|
|
|
|
|
|
2026-02-19 15:21:47 +03:00
|
|
|
if(mustUpdateDrawLoadThread) {
|
2025-10-10 18:41:08 +03:00
|
|
|
mustUpdateDraw = true;
|
2026-02-19 15:21:47 +03:00
|
|
|
mustUpdateDrawLoadThread = false;
|
2025-10-10 18:41:08 +03:00
|
|
|
}
|
2026-02-19 15:21:47 +03:00
|
|
|
else
|
|
|
|
|
mustUpdateDraw = false;
|
|
|
|
|
|
|
|
|
|
if(frames.size() == 1)
|
2025-07-06 20:43:53 +03:00
|
|
|
return;
|
2026-02-19 15:21:47 +03:00
|
|
|
|
|
|
|
|
currentTime += w.main.deltaTime;
|
2025-07-06 20:43:53 +03:00
|
|
|
for(;;) {
|
|
|
|
|
if(currentTime >= frames[frameIndex].duration) {
|
|
|
|
|
currentTime -= frames[frameIndex].duration;
|
|
|
|
|
frameIndex++;
|
|
|
|
|
mustUpdateDraw = true;
|
2026-02-19 22:02:18 +03:00
|
|
|
if(frameIndex >= frames.size())
|
2026-02-19 15:21:47 +03:00
|
|
|
frameIndex = 0;
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return;
|
|
|
|
|
if(frames[frameIndex].duration <= 0.0f)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 23:50:03 +03:00
|
|
|
sk_sp<SkImage> ImageResourceDisplay::load_frame_with_codec(const std::unique_ptr<SkCodec>& codec, unsigned mipmapLevel, unsigned frameIndexToLoad) {
|
2026-02-19 15:21:47 +03:00
|
|
|
SkCodec::Options imageOptions;
|
|
|
|
|
imageOptions.fFrameIndex = frameIndexToLoad;
|
|
|
|
|
auto decodeResult = codec->getImage(imageInfo, &imageOptions);
|
|
|
|
|
if(std::get<1>(decodeResult) != SkCodec::Result::kSuccess)
|
|
|
|
|
throw std::runtime_error("Could not decode image, got error code " + std::to_string(std::get<1>(decodeResult)) + ". Image decoded with dimensions " + std::to_string(imageInfo.width()) + " " + std::to_string(imageInfo.height()));
|
|
|
|
|
sk_sp<SkImage> ogImage = std::get<0>(decodeResult);
|
2026-02-19 23:50:03 +03:00
|
|
|
// Scale down by 50% in each iteration for better quality
|
|
|
|
|
for(size_t i = 1; i <= mipmapLevel; i++) {
|
|
|
|
|
Vector2i mipmapLevelDim = get_mipmap_level_image_dimensions(i);
|
|
|
|
|
SkImageInfo scaledImageInfo = imageInfo.makeDimensions({mipmapLevelDim.x(), mipmapLevelDim.y()});
|
|
|
|
|
ogImage = ogImage->makeScaled(scaledImageInfo, {SkFilterMode::kLinear, SkMipmapMode::kNone});
|
|
|
|
|
if(!ogImage)
|
|
|
|
|
throw std::runtime_error("Could not scale image.");
|
|
|
|
|
}
|
2026-02-20 00:01:28 +03:00
|
|
|
if(mipmapLevel == get_smallest_mipmap_level())
|
|
|
|
|
return ogImage->withDefaultMipmaps();
|
2026-02-19 23:50:03 +03:00
|
|
|
return ogImage;
|
2026-02-19 15:21:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageResourceDisplay::camera_view_update(const CoordSpaceHelper& compCoords, const SCollision::AABB<WorldScalar>& compAABB, const DrawData& drawData, const SkRect& imRect) {
|
|
|
|
|
if(!smallestMipmapLevelLoaded)
|
|
|
|
|
attempt_load_mipmap_in_separate_thread(get_smallest_mipmap_level());
|
|
|
|
|
else {
|
|
|
|
|
if(SCollision::collide(compAABB, drawData.cam.viewingAreaGenerousCollider)) {
|
2026-02-19 22:02:18 +03:00
|
|
|
unsigned mipmapLevel = get_exact_mipmap_level_for_image_component(drawData, compCoords, imRect);
|
2026-02-19 15:21:47 +03:00
|
|
|
if(mipmapLevel != get_smallest_mipmap_level()) {
|
|
|
|
|
if(mipmapLevelsStatus[mipmapLevel] == MipmapLevelStatus::UNALLOCATED) {
|
|
|
|
|
attempt_load_mipmap_in_separate_thread(mipmapLevel);
|
2026-02-19 22:02:18 +03:00
|
|
|
unsigned nextBestMipmapLevel = get_best_allocated_mipmap_level(mipmapLevel);
|
2026-02-19 15:21:47 +03:00
|
|
|
if(nextBestMipmapLevel != get_smallest_mipmap_level())
|
|
|
|
|
mipmapLevelsStatus[nextBestMipmapLevel] = MipmapLevelStatus::ALLOCATED_JUST_SET;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
mipmapLevelsStatus[mipmapLevel] = MipmapLevelStatus::ALLOCATED_JUST_SET;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ImageResourceDisplay::load(ResourceManager& rMan, const std::string& fileName, const std::shared_ptr<std::string>& fileData) {
|
|
|
|
|
this->fileData = fileData;
|
|
|
|
|
auto codec = SkCodec::MakeFromData(SkData::MakeWithoutCopy(this->fileData->c_str(), this->fileData->size()), decoders2);
|
2025-08-01 20:07:47 +03:00
|
|
|
mustUpdateDraw = true;
|
2026-02-19 15:21:47 +03:00
|
|
|
mustUpdateDrawLoadThread = true;
|
2025-07-06 20:43:53 +03:00
|
|
|
if(codec) {
|
|
|
|
|
auto origin = codec->getOrigin();
|
|
|
|
|
bool rotate = (origin == kLeftTop_SkEncodedOrigin || origin == kRightTop_SkEncodedOrigin || origin == kRightBottom_SkEncodedOrigin || origin == kLeftBottom_SkEncodedOrigin);
|
2025-10-10 18:41:08 +03:00
|
|
|
imageInfo = codec->getInfo();
|
2026-02-19 15:21:47 +03:00
|
|
|
|
|
|
|
|
mipmapLevelsStatus = std::vector<std::atomic<MipmapLevelStatus>>(calculate_smallest_mipmap_level());
|
|
|
|
|
for(auto& mipmapLevelStatus : mipmapLevelsStatus)
|
|
|
|
|
mipmapLevelStatus = MipmapLevelStatus::UNALLOCATED;
|
|
|
|
|
|
|
|
|
|
frames.resize(codec->getFrameCount());
|
|
|
|
|
for(size_t i = 0; i < frames.size(); i++) {
|
|
|
|
|
auto& frame = frames[i];
|
|
|
|
|
frame.mipmapLevels.resize(get_smallest_mipmap_level());
|
|
|
|
|
SkCodec::FrameInfo frameInfo;
|
|
|
|
|
if(codec->getFrameInfo(i, &frameInfo))
|
|
|
|
|
frame.duration = static_cast<float>(frameInfo.fDuration) / 1000.0f; // get frametime in seconds
|
|
|
|
|
else
|
|
|
|
|
frame.duration = -1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
if(rotate) {
|
2026-05-16 21:56:58 +03:00
|
|
|
Logger::get().log(Logger::LogType::INFO, "Image loaded is rotated, so must rotate dimensions");
|
2025-07-06 20:43:53 +03:00
|
|
|
imageInfo = imageInfo.makeDimensions(SkISize(imageInfo.height(), imageInfo.width()));
|
|
|
|
|
}
|
2026-02-19 15:21:47 +03:00
|
|
|
codec = nullptr;
|
|
|
|
|
attempt_load_mipmap_in_separate_thread(get_smallest_mipmap_level());
|
2025-07-06 20:43:53 +03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 22:02:18 +03:00
|
|
|
void ImageResourceDisplay::attempt_load_mipmap_in_separate_thread(unsigned mipmapLevel) {
|
2026-02-19 15:21:47 +03:00
|
|
|
if(imageLoadThreadCount >= IMAGE_LOAD_THREAD_COUNT_MAX)
|
|
|
|
|
return;
|
|
|
|
|
if(!loadThread) {
|
|
|
|
|
shutdownLoadThread = false;
|
|
|
|
|
imageLoadThreadCount++;
|
|
|
|
|
loadThread = std::make_unique<std::thread>(&ImageResourceDisplay::load_thread_func, this, mipmapLevel);
|
|
|
|
|
}
|
|
|
|
|
else if(shutdownLoadThread) {
|
|
|
|
|
loadThread->join();
|
|
|
|
|
shutdownLoadThread = false;
|
|
|
|
|
imageLoadThreadCount++;
|
|
|
|
|
loadThread = std::make_unique<std::thread>(&ImageResourceDisplay::load_thread_func, this, mipmapLevel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 22:02:18 +03:00
|
|
|
void ImageResourceDisplay::load_thread_func(unsigned mipmapLevel) {
|
2026-02-19 15:21:47 +03:00
|
|
|
auto codec = SkCodec::MakeFromData(SkData::MakeWithoutCopy(this->fileData->c_str(), this->fileData->size()), decoders2);
|
|
|
|
|
for(size_t i = 0; i < frames.size(); i++) {
|
|
|
|
|
if(shutdownLoadThread)
|
|
|
|
|
return;
|
|
|
|
|
auto& frame = frames[i];
|
|
|
|
|
auto& mipmapImage = (mipmapLevel == get_smallest_mipmap_level()) ? frame.smallestMipmapLevel : frame.mipmapLevels[mipmapLevel];
|
2026-02-19 23:50:03 +03:00
|
|
|
mipmapImage = load_frame_with_codec(codec, mipmapLevel, i);
|
2026-02-19 15:21:47 +03:00
|
|
|
}
|
|
|
|
|
if(mipmapLevel == get_smallest_mipmap_level())
|
|
|
|
|
smallestMipmapLevelLoaded = true;
|
|
|
|
|
else
|
|
|
|
|
mipmapLevelsStatus[mipmapLevel] = MipmapLevelStatus::ALLOCATED_JUST_SET;
|
|
|
|
|
mustUpdateDrawLoadThread = true;
|
|
|
|
|
imageLoadThreadCount--;
|
|
|
|
|
shutdownLoadThread = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
void ImageResourceDisplay::draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) {
|
2026-03-20 20:53:59 +03:00
|
|
|
if(!smallestMipmapLevelLoaded) {
|
|
|
|
|
SkPaint p({0.5f, 0.5f, 0.5f, 0.5f});
|
|
|
|
|
p.setAntiAlias(drawData.skiaAA);
|
|
|
|
|
canvas->drawRect(imRect, p);
|
|
|
|
|
}
|
2026-02-16 18:19:17 +03:00
|
|
|
else {
|
|
|
|
|
SkRect imRectPixelSize = canvas->getLocalToDeviceAs3x3().mapRect(imRect);
|
2026-02-19 22:02:18 +03:00
|
|
|
unsigned mipmapLevel = get_exact_mipmap_level_for_dimensions({imRectPixelSize.width(), imRectPixelSize.height()});
|
2026-02-16 18:19:17 +03:00
|
|
|
auto& frame = frames[frameIndex];
|
2026-02-19 22:02:18 +03:00
|
|
|
unsigned closestMipmapLevel = get_best_allocated_mipmap_level(mipmapLevel);
|
2026-02-25 13:26:35 +03:00
|
|
|
if(drawData.takingScreenshot) {
|
2026-02-19 15:21:47 +03:00
|
|
|
if(closestMipmapLevel == mipmapLevel) {
|
|
|
|
|
if(closestMipmapLevel == get_smallest_mipmap_level())
|
2026-02-20 00:01:28 +03:00
|
|
|
canvas->drawImageRect(frame.smallestMipmapLevel, imRect, {SkFilterMode::kLinear, SkMipmapMode::kLinear});
|
2026-02-19 15:21:47 +03:00
|
|
|
else
|
|
|
|
|
canvas->drawImageRect(frame.mipmapLevels[closestMipmapLevel], imRect, {SkFilterMode::kLinear, SkMipmapMode::kNone});
|
2026-02-16 18:19:17 +03:00
|
|
|
}
|
2026-02-17 14:41:08 +03:00
|
|
|
else {
|
2026-02-19 15:21:47 +03:00
|
|
|
if(screenshotCache.contains(fileData))
|
|
|
|
|
canvas->drawImageRect(screenshotCache[fileData], imRect, {SkFilterMode::kLinear, SkMipmapMode::kNone});
|
|
|
|
|
else {
|
|
|
|
|
auto codec = SkCodec::MakeFromData(SkData::MakeWithoutCopy(this->fileData->c_str(), this->fileData->size()), decoders2);
|
2026-02-19 23:50:03 +03:00
|
|
|
screenshotCache[fileData] = load_frame_with_codec(codec, 0, frameIndex);
|
2026-02-19 15:21:47 +03:00
|
|
|
canvas->drawImageRect(screenshotCache[fileData], imRect, {SkFilterMode::kLinear, SkMipmapMode::kNone});
|
|
|
|
|
}
|
2026-02-17 14:41:08 +03:00
|
|
|
}
|
2026-02-16 18:19:17 +03:00
|
|
|
}
|
2026-02-19 15:21:47 +03:00
|
|
|
else {
|
2026-04-01 18:00:22 +03:00
|
|
|
SkPaint p;
|
|
|
|
|
p.setAntiAlias(drawData.skiaAA);
|
2026-02-19 15:21:47 +03:00
|
|
|
if(closestMipmapLevel == get_smallest_mipmap_level())
|
2026-04-01 18:00:22 +03:00
|
|
|
canvas->drawImageRect(frame.smallestMipmapLevel, imRect, {SkFilterMode::kLinear, SkMipmapMode::kLinear}, &p);
|
2026-02-19 15:21:47 +03:00
|
|
|
else
|
2026-04-01 18:00:22 +03:00
|
|
|
canvas->drawImageRect(frame.mipmapLevels[closestMipmapLevel], imRect, {SkFilterMode::kLinear, SkMipmapMode::kNone}, &p);
|
2026-02-19 15:21:47 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 22:02:18 +03:00
|
|
|
unsigned ImageResourceDisplay::get_exact_mipmap_level_for_image_component(const DrawData& drawData, const CoordSpaceHelper& compCoords, const SkRect& imRect) {
|
|
|
|
|
unsigned minimumRectDim;
|
|
|
|
|
if(imRect.width() < imRect.height())
|
|
|
|
|
minimumRectDim = drawData.cam.c.scalar_to_space(compCoords.scalar_from_space(imRect.width()));
|
|
|
|
|
else
|
|
|
|
|
minimumRectDim = drawData.cam.c.scalar_to_space(compCoords.scalar_from_space(imRect.height()));
|
|
|
|
|
unsigned minimumImageDim = std::min(imageInfo.width(), imageInfo.height());
|
|
|
|
|
unsigned imDim = std::max<unsigned>(minimumRectDim, SMALLEST_MIPMAP_RESOLUTION);
|
|
|
|
|
unsigned divisionLevel = minimumImageDim / imDim;
|
|
|
|
|
return (divisionLevel <= 1) ? 0 : std::bit_width(divisionLevel - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned ImageResourceDisplay::get_exact_mipmap_level_for_dimensions(const Vector2i& dim) {
|
|
|
|
|
unsigned minimumImageDim = std::min(imageInfo.width(), imageInfo.height());
|
|
|
|
|
unsigned minimumRectDim = std::min(dim.x(), dim.y());
|
|
|
|
|
unsigned imDim = std::max<unsigned>(minimumRectDim, SMALLEST_MIPMAP_RESOLUTION);
|
|
|
|
|
unsigned divisionLevel = minimumImageDim / imDim;
|
|
|
|
|
return (divisionLevel <= 1) ? 0 : (std::bit_width(divisionLevel) - 1);
|
2026-02-19 15:21:47 +03:00
|
|
|
}
|
|
|
|
|
|
2026-02-19 22:02:18 +03:00
|
|
|
unsigned ImageResourceDisplay::get_best_allocated_mipmap_level(unsigned mipmapLevel) {
|
2026-02-19 15:21:47 +03:00
|
|
|
if(mipmapLevel == get_smallest_mipmap_level())
|
|
|
|
|
return mipmapLevel;
|
2026-02-19 22:02:18 +03:00
|
|
|
constexpr static unsigned NUMERIC_UNSIGNED_MAX = std::numeric_limits<unsigned>::max();
|
|
|
|
|
for(unsigned i = mipmapLevel; i != NUMERIC_UNSIGNED_MAX; i--) {
|
2026-02-19 15:21:47 +03:00
|
|
|
if(mipmapLevelsStatus[i] != MipmapLevelStatus::UNALLOCATED)
|
|
|
|
|
return i;
|
2026-02-16 18:19:17 +03:00
|
|
|
}
|
2026-02-19 22:02:18 +03:00
|
|
|
for(unsigned i = mipmapLevel; i < get_smallest_mipmap_level(); i++) {
|
2026-02-19 15:21:47 +03:00
|
|
|
if(mipmapLevelsStatus[i] != MipmapLevelStatus::UNALLOCATED)
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return get_smallest_mipmap_level();
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 22:02:18 +03:00
|
|
|
unsigned ImageResourceDisplay::calculate_smallest_mipmap_level() {
|
|
|
|
|
unsigned minimumImageDim = std::min(imageInfo.width(), imageInfo.height());
|
|
|
|
|
unsigned divisionLevel = minimumImageDim / SMALLEST_MIPMAP_RESOLUTION;
|
|
|
|
|
return (divisionLevel <= 1) ? 0 : std::bit_width(divisionLevel - 1);
|
2026-02-19 15:21:47 +03:00
|
|
|
}
|
|
|
|
|
|
2026-02-19 22:02:18 +03:00
|
|
|
unsigned ImageResourceDisplay::get_smallest_mipmap_level() const {
|
2026-02-19 15:21:47 +03:00
|
|
|
return mipmapLevelsStatus.size();
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 22:02:18 +03:00
|
|
|
Vector2i ImageResourceDisplay::get_mipmap_level_image_dimensions(unsigned mipmapLevel) const {
|
|
|
|
|
unsigned divideResolutionBy = 1 << mipmapLevel;
|
2026-02-19 15:21:47 +03:00
|
|
|
return {imageInfo.width() / divideResolutionBy, imageInfo.height() / divideResolutionBy};
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2f ImageResourceDisplay::get_dimensions() const {
|
2025-10-10 18:41:08 +03:00
|
|
|
return {imageInfo.width(), imageInfo.height()};
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float ImageResourceDisplay::get_dimension_scale() const {
|
|
|
|
|
return 0.3f;
|
|
|
|
|
}
|
2025-10-09 16:56:05 +03:00
|
|
|
|
|
|
|
|
ResourceDisplay::Type ImageResourceDisplay::get_type() const {
|
|
|
|
|
return ResourceDisplay::Type::IMAGE;
|
|
|
|
|
}
|
2025-10-10 18:41:08 +03:00
|
|
|
|
2026-02-19 22:29:24 +03:00
|
|
|
void ImageResourceDisplay::clear_cache() {
|
|
|
|
|
if(loadThread) {
|
|
|
|
|
shutdownLoadThread = true;
|
|
|
|
|
loadThread->join();
|
|
|
|
|
loadThread = nullptr;
|
|
|
|
|
}
|
|
|
|
|
for(auto& frame : frames) {
|
|
|
|
|
for(auto& mipmap : frame.mipmapLevels)
|
|
|
|
|
mipmap = nullptr;
|
|
|
|
|
}
|
|
|
|
|
for(auto& mipmapStatus : mipmapLevelsStatus)
|
|
|
|
|
mipmapStatus = MipmapLevelStatus::UNALLOCATED;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-10 18:41:08 +03:00
|
|
|
ImageResourceDisplay::~ImageResourceDisplay() {
|
|
|
|
|
if(loadThread) {
|
|
|
|
|
shutdownLoadThread = true;
|
|
|
|
|
loadThread->join();
|
|
|
|
|
}
|
|
|
|
|
}
|