Hyprland/src/hyprerror/HyprError.cpp

228 lines
8 KiB
C++
Raw Normal View History

#include <pango/pangocairo.h>
#include "HyprError.hpp"
#include "../Compositor.hpp"
#include "../config/ConfigValue.hpp"
#include "../config/ConfigManager.hpp"
#include "../render/pass/TexPassElement.hpp"
#include "../managers/animation/AnimationManager.hpp"
#include "../render/Renderer.hpp"
#include "../managers/HookSystemManager.hpp"
#include <hyprutils/utils/ScopeGuard.hpp>
using namespace Hyprutils::Animation;
2023-01-20 20:48:07 +01:00
CHyprError::CHyprError() {
g_pAnimationManager->createAnimation(0.f, m_fadeOpacity, g_pConfigManager->getAnimationPropertyConfig("fadeIn"), AVARDAMAGE_NONE);
2023-02-28 19:18:13 +00:00
2024-04-20 20:16:42 +01:00
static auto P = g_pHookSystem->hookDynamic("focusedMon", [&](void* self, SCallbackInfo& info, std::any param) {
if (!m_isCreated)
2023-02-28 19:18:13 +00:00
return;
g_pHyprRenderer->damageMonitor(g_pCompositor->m_lastMonitor.lock());
m_monitorChanged = true;
2023-02-28 19:18:13 +00:00
});
2024-04-20 20:16:42 +01:00
static auto P2 = g_pHookSystem->hookDynamic("preRender", [&](void* self, SCallbackInfo& info, std::any param) {
if (!m_isCreated)
2023-02-28 19:18:13 +00:00
return;
if (m_fadeOpacity->isBeingAnimated() || m_monitorChanged)
g_pHyprRenderer->damageBox(m_damageBox);
2023-02-28 19:18:13 +00:00
});
m_texture = makeShared<CTexture>();
2023-01-20 20:48:07 +01:00
}
void CHyprError::queueCreate(std::string message, const CHyprColor& color) {
m_queued = message;
m_queuedColor = color;
}
void CHyprError::createQueued() {
if (m_isCreated)
m_texture->destroyTexture();
m_fadeOpacity->setConfig(g_pConfigManager->getAnimationPropertyConfig("fadeIn"));
2023-01-23 12:04:48 +00:00
m_fadeOpacity->setValueAndWarp(0.f);
*m_fadeOpacity = 1.f;
2023-01-20 20:48:07 +01:00
const auto PMONITOR = g_pCompositor->m_monitors.front();
const auto SCALE = PMONITOR->m_scale;
2023-01-20 20:21:09 +01:00
const auto FONTSIZE = std::clamp(sc<int>(10.f * ((PMONITOR->m_pixelSize.x * SCALE) / 1920.f)), 8, 40);
2022-08-16 16:25:53 +02:00
const auto CAIROSURFACE = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, PMONITOR->m_pixelSize.x, PMONITOR->m_pixelSize.y);
const auto CAIRO = cairo_create(CAIROSURFACE);
// clear the pixmap
cairo_save(CAIRO);
cairo_set_operator(CAIRO, CAIRO_OPERATOR_CLEAR);
cairo_paint(CAIRO);
cairo_restore(CAIRO);
const auto LINECOUNT = Hyprlang::INT{1} + std::ranges::count(m_queued, '\n');
static auto LINELIMIT = CConfigValue<Hyprlang::INT>("debug:error_limit");
static auto BAR_POSITION = CConfigValue<Hyprlang::INT>("debug:error_position");
const bool TOPBAR = *BAR_POSITION == 0;
const auto VISLINECOUNT = std::min(LINECOUNT, *LINELIMIT);
const auto EXTRALINES = (VISLINECOUNT < LINECOUNT) ? 1 : 0;
2023-01-20 19:32:41 +01:00
const double DEGREES = M_PI / 180.0;
2023-01-20 20:21:09 +01:00
const double PAD = 10 * SCALE;
2023-01-20 19:32:41 +01:00
const double WIDTH = PMONITOR->m_pixelSize.x - PAD * 2;
const double HEIGHT = (FONTSIZE + 2 * (FONTSIZE / 10.0)) * (VISLINECOUNT + EXTRALINES) + 3;
const double RADIUS = PAD > HEIGHT / 2 ? HEIGHT / 2 - 1 : PAD;
const double X = PAD;
const double Y = TOPBAR ? PAD : PMONITOR->m_pixelSize.y - HEIGHT - PAD;
2023-01-20 19:32:41 +01:00
m_damageBox = {0, 0, sc<int>(PMONITOR->m_pixelSize.x), sc<int>(HEIGHT) + sc<int>(PAD) * 2};
2023-01-20 20:48:07 +01:00
2023-01-20 19:32:41 +01:00
cairo_new_sub_path(CAIRO);
cairo_arc(CAIRO, X + WIDTH - RADIUS, Y + RADIUS, RADIUS, -90 * DEGREES, 0 * DEGREES);
cairo_arc(CAIRO, X + WIDTH - RADIUS, Y + HEIGHT - RADIUS, RADIUS, 0 * DEGREES, 90 * DEGREES);
cairo_arc(CAIRO, X + RADIUS, Y + HEIGHT - RADIUS, RADIUS, 90 * DEGREES, 180 * DEGREES);
cairo_arc(CAIRO, X + RADIUS, Y + RADIUS, RADIUS, 180 * DEGREES, 270 * DEGREES);
cairo_close_path(CAIRO);
2024-08-02 18:42:05 +02:00
cairo_set_source_rgba(CAIRO, 0.06, 0.06, 0.06, 1.0);
2023-01-20 19:32:41 +01:00
cairo_fill_preserve(CAIRO);
cairo_set_source_rgba(CAIRO, m_queuedColor.r, m_queuedColor.g, m_queuedColor.b, m_queuedColor.a);
2023-01-20 19:32:41 +01:00
cairo_set_line_width(CAIRO, 2);
cairo_stroke(CAIRO);
// draw the text with a common font
const CHyprColor textColor = CHyprColor(0.9, 0.9, 0.9, 1.0);
cairo_set_source_rgba(CAIRO, textColor.r, textColor.g, textColor.b, textColor.a);
static auto fontFamily = CConfigValue<std::string>("misc:font_family");
PangoLayout* layoutText = pango_cairo_create_layout(CAIRO);
PangoFontDescription* pangoFD = pango_font_description_new();
pango_font_description_set_family(pangoFD, (*fontFamily).c_str());
pango_font_description_set_absolute_size(pangoFD, FONTSIZE * PANGO_SCALE);
pango_font_description_set_style(pangoFD, PANGO_STYLE_NORMAL);
pango_font_description_set_weight(pangoFD, PANGO_WEIGHT_NORMAL);
pango_layout_set_font_description(layoutText, pangoFD);
float yoffset = TOPBAR ? 0 : Y - PAD;
int renderedcnt = 0;
while (!m_queued.empty() && renderedcnt < VISLINECOUNT) {
std::string current = m_queued.substr(0, m_queued.find('\n'));
if (const auto NEWLPOS = m_queued.find('\n'); NEWLPOS != std::string::npos)
m_queued = m_queued.substr(NEWLPOS + 1);
else
m_queued = "";
2023-01-20 19:32:41 +01:00
cairo_move_to(CAIRO, PAD + 1 + RADIUS, yoffset + PAD + 1);
pango_layout_set_text(layoutText, current.c_str(), -1);
pango_cairo_show_layout(CAIRO, layoutText);
2022-08-16 16:25:53 +02:00
yoffset += FONTSIZE + (FONTSIZE / 10.f);
renderedcnt++;
}
if (VISLINECOUNT < LINECOUNT) {
std::string moreString = std::format("({} more...)", LINECOUNT - VISLINECOUNT);
cairo_move_to(CAIRO, PAD + 1 + RADIUS, yoffset + PAD + 1);
pango_layout_set_text(layoutText, moreString.c_str(), -1);
pango_cairo_show_layout(CAIRO, layoutText);
}
m_queued = "";
2022-09-25 20:07:48 +02:00
m_lastHeight = yoffset + PAD + 1 - (TOPBAR ? 0 : Y - PAD);
pango_font_description_free(pangoFD);
g_object_unref(layoutText);
cairo_surface_flush(CAIROSURFACE);
// copy the data to an OpenGL texture we have
const auto DATA = cairo_image_surface_get_data(CAIROSURFACE);
m_texture->allocate();
renderer: reduce a lot of glcalls and cache various states (#10757) * opengl: cache viewport state according to nvidia docs calling glViewPort unnecessarily on the same already set viewport is wasteful and can cause state changes when not needed. cache it in a struct and only call it when the viewport is actually changing. * opengl: cache glenable/gldisable state avoid making multiple glenable/gldisable calls on already set caps, can cause state changes and incur driver overhead. * opengl: cache glscissor box only call glscissor if the box actually has changed, try to avoid state changes. * opengl: cache gluniform calls cache the gluniform calls, the uniform values are cached in driver per program only the drawcalls setting the uniform yet again with the same value on same location is causing more overhead then caching it ourself and just no oping on it if no changes. * shader: rewrite handling of uniforms and state this is way faster as we don't need to mess with maps (hashing, etc) and instead can just use an array * opengl: stuff and 300 shaders * opengl: typo * opengl: get the uniform locations properly now that the legacy shaders are gone get the uniformlocations for SKIP_CM etc, so they can be properly set and used depending on if cm_enabled is set to false or true, before it was falling back to a legacy shader that didnt even have those uniforms. * opengl: check epsilon on float and remove extra glcall seems an extra unset glcall was added, remove it. and check the float epsilon on the glfloat. * opengl: remove instanced shader draw remove the instanced boolean from the vertex shader, might be neglible differences, needs more benchmark/work to see if its even worth it. * texture: cache texture paramaters parameters where occasionally set twice or more on same texture, short version wrap it and cache it. and move gpu churn to cpu churn. add a bind/unbind to texture aswell. * texture: use fast std::array caching cache the texparameter values in fast array lookups and incase we dont want it cached, apply it anyways. * shader: fix typo and hdr typo actually use Matrix4x2fv in the 4x2fv cache function, and send the proper float array for hdr. * texture: make caching not linear lookup make caching of texture params not linear. * minor style changes * opengl: revert drawarrays revert the mostly code style reduce loc change of drawarrays, and focus on the caching. its a if else case going wrong here breaking blur/contrast amongst others drawing. --------- Co-authored-by: Vaxry <vaxry@vaxry.net>
2025-06-25 12:42:32 +02:00
m_texture->bind();
m_texture->setTexParameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
m_texture->setTexParameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST);
m_texture->setTexParameter(GL_TEXTURE_SWIZZLE_R, GL_BLUE);
m_texture->setTexParameter(GL_TEXTURE_SWIZZLE_B, GL_RED);
2022-09-25 20:07:48 +02:00
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, PMONITOR->m_pixelSize.x, PMONITOR->m_pixelSize.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, DATA);
// delete cairo
cairo_destroy(CAIRO);
cairo_surface_destroy(CAIROSURFACE);
m_isCreated = true;
m_queued = "";
m_queuedColor = CHyprColor();
2022-05-12 12:46:38 +02:00
g_pHyprRenderer->damageMonitor(PMONITOR);
g_pHyprRenderer->arrangeLayersForMonitor(PMONITOR->m_id);
}
void CHyprError::draw() {
if (!m_isCreated || !m_queued.empty()) {
if (!m_queued.empty())
createQueued();
return;
}
if (m_queuedDestroy) {
if (!m_fadeOpacity->isBeingAnimated()) {
if (m_fadeOpacity->value() == 0.f) {
m_queuedDestroy = false;
m_texture->destroyTexture();
m_isCreated = false;
m_queued = "";
for (auto& m : g_pCompositor->m_monitors) {
g_pHyprRenderer->arrangeLayersForMonitor(m->m_id);
}
2023-01-20 20:48:07 +01:00
return;
} else {
m_fadeOpacity->setConfig(g_pConfigManager->getAnimationPropertyConfig("fadeOut"));
*m_fadeOpacity = 0.f;
2023-01-20 20:48:07 +01:00
}
}
}
const auto PMONITOR = g_pHyprOpenGL->m_renderData.pMonitor;
CBox monbox = {0, 0, PMONITOR->m_pixelSize.x, PMONITOR->m_pixelSize.y};
m_damageBox.x = sc<int>(PMONITOR->m_position.x);
m_damageBox.y = sc<int>(PMONITOR->m_position.y);
2023-01-20 20:48:07 +01:00
if (m_fadeOpacity->isBeingAnimated() || m_monitorChanged)
g_pHyprRenderer->damageBox(m_damageBox);
m_monitorChanged = false;
2023-02-28 19:18:13 +00:00
CTexPassElement::SRenderData data;
data.tex = m_texture;
data.box = monbox;
data.a = m_fadeOpacity->value();
g_pHyprRenderer->m_renderPass.add(makeUnique<CTexPassElement>(std::move(data)));
}
void CHyprError::destroy() {
if (m_isCreated)
m_queuedDestroy = true;
else
m_queued = "";
2022-09-25 20:07:48 +02:00
}
bool CHyprError::active() {
return m_isCreated;
}
float CHyprError::height() {
return m_lastHeight;
}