// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later

// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later

#include <optional>

#include <boost/container/small_vector.hpp>

#include "core/hle/service/nvdrv/devices/nvdisp_disp0.h"
#include "core/hle/service/nvnflinger/buffer_item.h"
#include "core/hle/service/nvnflinger/buffer_item_consumer.h"
#include "core/hle/service/nvnflinger/hardware_composer.h"
#include "core/hle/service/nvnflinger/hwc_layer.h"
#include "core/hle/service/nvnflinger/ui/graphic_buffer.h"

namespace Service::Nvnflinger {

namespace {

s32 NormalizeSwapInterval(f32* out_speed_scale, s32 swap_interval) {
    if (swap_interval <= 0) {
        // As an extension, treat nonpositive swap interval as speed multiplier.
        if (out_speed_scale) {
            *out_speed_scale = 2.f * static_cast<f32>(1 - swap_interval);
        }

        swap_interval = 1;
    }

    if (swap_interval >= 5) {
        // As an extension, treat high swap interval as precise speed control.
        if (out_speed_scale) {
            *out_speed_scale = static_cast<f32>(swap_interval) / 100.f;
        }

        swap_interval = 1;
    }

    return swap_interval;
}

} // namespace

HardwareComposer::HardwareComposer() = default;
HardwareComposer::~HardwareComposer() = default;

u32 HardwareComposer::ComposeLocked(f32* out_speed_scale, Display& display,
                                    Nvidia::Devices::nvdisp_disp0& nvdisp) {
#if BOOST_VERSION >= 108100 || __GNUC__ > 12
    boost::container::small_vector<HwcLayer, 2> composition_stack;
#else //TODO: debian stable
    std::vector<HwcLayer> composition_stack;
#endif

    // Set default speed limit to 100%.
    *out_speed_scale = 1.0f;

    // If no layers are available, skip the logic.
    bool any_visible = false;
    for (auto& layer : display.stack.layers) {
        if (layer->visible) {
            any_visible = true;
            break;
        }
    }
    if (!any_visible) {
        *out_speed_scale = 1.0f;
        return 1;
    }

    // Determine the number of vsync periods to wait before composing again.
    std::optional<s32> swap_interval{};
    bool has_acquired_buffer{};

    // Acquire all necessary framebuffers.
    for (auto& layer : display.stack.layers) {
        auto consumer_id = layer->consumer_id;

        bool should_try_acquire = true;
        if (!layer->is_overlay) {
            auto fb_it = m_framebuffers.find(consumer_id);
            if (fb_it != m_framebuffers.end() && fb_it->second.is_acquired) {
                const u64 frames_since_last_acquire = m_frame_number - fb_it->second.last_acquire_frame;
                const s32 expected_interval = NormalizeSwapInterval(nullptr, fb_it->second.item.swap_interval);

                if (frames_since_last_acquire < static_cast<u64>(expected_interval)) {
                    should_try_acquire = false;
                }
            }
        }

        // Try to fetch the framebuffer (either new or stale).
        const auto result = should_try_acquire
            ? this->CacheFramebufferLocked(*layer, consumer_id)
            : (m_framebuffers.find(consumer_id) != m_framebuffers.end() && m_framebuffers[consumer_id].is_acquired
                ? CacheStatus::CachedBufferReused
                : CacheStatus::NoBufferAvailable);

        // If we failed, skip this layer.
        if (result == CacheStatus::NoBufferAvailable) {
            continue;
        }

        // If we acquired a new buffer, we need to present.
        if (result == CacheStatus::BufferAcquired) {
            has_acquired_buffer = true;
        }

        const auto& buffer = m_framebuffers[consumer_id];
        const auto& item = buffer.item;
        const auto& igbp_buffer = *item.graphic_buffer;

        if (layer->visible) {
            composition_stack.emplace_back(HwcLayer{
                .buffer_handle = igbp_buffer.BufferId(),
                .offset = igbp_buffer.Offset(),
                .format = igbp_buffer.ExternalFormat(),
                .width = igbp_buffer.Width(),
                .height = igbp_buffer.Height(),
                .stride = igbp_buffer.Stride(),
                .z_index = layer->z_index,
                .blending = layer->blending,
                .transform = static_cast<android::BufferTransformFlags>(item.transform),
                .crop_rect = item.crop,
                .acquire_fence = item.fence,
            });
        }

        // Overlay layers run at their own framerate independently of the game.
        // Skip them when calculating the swap interval for the main game.
        if (layer->is_overlay) {
            continue;
        }

        // We need to compose again either before this frame is supposed to
        // be released, or exactly on the vsync period it should be released.
        const s32 item_swap_interval = NormalizeSwapInterval(out_speed_scale, item.swap_interval);

        // TODO: handle cases where swap intervals are relatively prime. So far,
        // only swap intervals of 0, 1 and 2 have been observed, but if 3 were
        // to be introduced, this would cause an issue.
        if (swap_interval) {
            swap_interval = (std::min)(*swap_interval, item_swap_interval);
        } else {
            swap_interval = item_swap_interval;
        }
    }

    // If any new buffers were acquired, we can present.
    if (has_acquired_buffer && !composition_stack.empty()) {
        // Sort back-to-front: lower z first, higher z last so top-most draws last (on top).
        std::stable_sort(composition_stack.begin(), composition_stack.end(),
                         [&](const HwcLayer& l, const HwcLayer& r) { return l.z_index < r.z_index; });

        // Composite.
        nvdisp.Composite(composition_stack);
    }

    // Batch framebuffer releases, instead of one-into-one.
    std::vector<std::pair<Layer*, Framebuffer*>> to_release;
    for (auto& [layer_id, framebuffer] : m_framebuffers) {
        if (!framebuffer.is_acquired)
            continue;

        auto layer = display.stack.FindLayer(layer_id);
        if (!layer)
            continue;

        // Overlay layers always release after every compose
        // Non-overlay layers release based on their swap interval
        if (layer->is_overlay || framebuffer.release_frame_number <= m_frame_number) {
            to_release.emplace_back(layer.get(), &framebuffer);
        }
    }
    for (auto& [layer, framebuffer] : to_release) {
        layer->buffer_item_consumer->ReleaseBuffer(framebuffer->item, android::Fence::NoFence());
        framebuffer->is_acquired = false;
    }

    // Advance by 1 frame (60 FPS compositing)
    m_frame_number += 1;

    // Release any necessary framebuffers (non-overlay layers only, as overlays are already released above).
    for (auto& [layer_id, framebuffer] : m_framebuffers) {
        if (!framebuffer.is_acquired) {
            // Already released.
            continue;
        }

        if (framebuffer.release_frame_number > m_frame_number) {
            continue;
        }

        if (const auto layer = display.stack.FindLayer(layer_id); layer != nullptr) {
            // Skip overlay layers as they were already released above
            if (layer->is_overlay) {
                continue;
            }

            // TODO: support release fence
            // This is needed to prevent screen tearing
            layer->buffer_item_consumer->ReleaseBuffer(framebuffer.item, android::Fence::NoFence());
            framebuffer.is_acquired = false;
        }
    }

    return 1;
}

void HardwareComposer::RemoveLayerLocked(Display& display, ConsumerId consumer_id) {
    // Check if we are tracking a slot with this consumer_id.
    const auto it = m_framebuffers.find(consumer_id);
    if (it == m_framebuffers.end()) {
        return;
    }

    // Try to release the buffer item.
    const auto layer = display.stack.FindLayer(consumer_id);
    if (layer && it->second.is_acquired) {
        layer->buffer_item_consumer->ReleaseBuffer(it->second.item, android::Fence::NoFence());
    }

    // Erase the slot.
    m_framebuffers.erase(it);
}

bool HardwareComposer::TryAcquireFramebufferLocked(Layer& layer, Framebuffer& framebuffer) {
    // Attempt the update.
    const auto status = layer.buffer_item_consumer->AcquireBuffer(&framebuffer.item, {}, false);
    if (status != android::Status::NoError) {
        return false;
    }

    // We succeeded, so set the new release frame info.
    const s32 swap_interval = layer.is_overlay ? 1 : NormalizeSwapInterval(nullptr, framebuffer.item.swap_interval);
    framebuffer.release_frame_number = m_frame_number + swap_interval;
    framebuffer.last_acquire_frame = m_frame_number;
    framebuffer.is_acquired = true;

    return true;
}

HardwareComposer::CacheStatus HardwareComposer::CacheFramebufferLocked(Layer& layer,
                                                                       ConsumerId consumer_id) {
    // Check if this framebuffer is already present.
    const auto it = m_framebuffers.find(consumer_id);
    if (it != m_framebuffers.end()) {
        // If it's currently still acquired, we are done.
        if (it->second.is_acquired) {
            return CacheStatus::CachedBufferReused;
        }

        // Try to acquire a new item.
        if (this->TryAcquireFramebufferLocked(layer, it->second)) {
            // We got a new item.
            return CacheStatus::BufferAcquired;
        } else {
            // We didn't acquire a new item, but we can reuse the slot.
            return CacheStatus::CachedBufferReused;
        }
    }

    // Framebuffer is not present, so try to create it.
    Framebuffer framebuffer{};

    if (this->TryAcquireFramebufferLocked(layer, framebuffer)) {
        // Move the buffer item into a new slot.
        m_framebuffers.emplace(consumer_id, std::move(framebuffer));

        // We succeeded.
        return CacheStatus::BufferAcquired;
    }

    // We couldn't acquire the buffer item, so don't create a slot.
    return CacheStatus::NoBufferAvailable;
}

} // namespace Service::Nvnflinger
