2021-08-02 13:37:33 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2023-10-20 04:36:34 +00:00
|
|
|
* Copyright © 2017-2023 The Looking Glass Authors
|
2021-08-02 13:37:33 +00:00
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* 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 2 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, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "texture.h"
|
|
|
|
|
|
|
|
#include "texture_buffer.h"
|
|
|
|
#include "common/debug.h"
|
2021-08-07 06:10:30 +00:00
|
|
|
#include "common/KVMFR.h"
|
|
|
|
#include "common/rects.h"
|
|
|
|
|
|
|
|
struct TexDamage
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
FrameDamageRect rects[KVMFR_MAX_DAMAGE_RECTS];
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct TexFB
|
|
|
|
{
|
|
|
|
TextureBuffer base;
|
|
|
|
struct TexDamage damage[EGL_TEX_BUFFER_MAX];
|
|
|
|
}
|
|
|
|
TexFB;
|
|
|
|
|
2022-05-22 08:19:58 +00:00
|
|
|
static bool egl_texFBInit(EGL_Texture ** texture, EGL_TexType type,
|
|
|
|
EGLDisplay * display)
|
2021-08-07 06:10:30 +00:00
|
|
|
{
|
2021-08-15 22:46:37 +00:00
|
|
|
TexFB * this = calloc(1, sizeof(*this));
|
2021-08-07 06:10:30 +00:00
|
|
|
*texture = &this->base.base;
|
|
|
|
|
|
|
|
EGL_Texture * parent = &this->base.base;
|
2022-05-22 08:19:58 +00:00
|
|
|
if (!egl_texBufferStreamInit(&parent, type, display))
|
2021-08-07 06:10:30 +00:00
|
|
|
{
|
|
|
|
free(this);
|
|
|
|
*texture = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < EGL_TEX_BUFFER_MAX; ++i)
|
|
|
|
this->damage[i].count = -1;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-08-02 13:37:33 +00:00
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
void egl_texFBFree(EGL_Texture * texture)
|
2021-08-08 05:35:13 +00:00
|
|
|
{
|
|
|
|
TextureBuffer * parent = UPCAST(TextureBuffer, texture);
|
|
|
|
TexFB * this = UPCAST(TexFB , parent );
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
egl_texBufferFree(texture);
|
2021-08-08 05:35:13 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2021-08-10 03:37:02 +00:00
|
|
|
bool egl_texFBSetup(EGL_Texture * texture, const EGL_TexSetup * setup)
|
|
|
|
{
|
|
|
|
TextureBuffer * parent = UPCAST(TextureBuffer, texture);
|
|
|
|
TexFB * this = UPCAST(TexFB , parent );
|
|
|
|
|
|
|
|
for (int i = 0; i < EGL_TEX_BUFFER_MAX; ++i)
|
|
|
|
this->damage[i].count = -1;
|
|
|
|
|
|
|
|
return egl_texBufferStreamSetup(texture, setup);
|
|
|
|
}
|
|
|
|
|
2023-11-06 02:45:02 +00:00
|
|
|
static int scaleForBGR(int x)
|
|
|
|
{
|
|
|
|
return x * 3 / 4;
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
static bool egl_texFBUpdate(EGL_Texture * texture, const EGL_TexUpdate * update)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-07 23:35:56 +00:00
|
|
|
TextureBuffer * parent = UPCAST(TextureBuffer, texture);
|
|
|
|
TexFB * this = UPCAST(TexFB , parent );
|
|
|
|
|
2021-08-13 23:51:36 +00:00
|
|
|
DEBUG_ASSERT(update->type == EGL_TEXTYPE_FRAMEBUFFER);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
2021-08-07 23:35:56 +00:00
|
|
|
LG_LOCK(parent->copyLock);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
2021-08-07 23:35:56 +00:00
|
|
|
struct TexDamage * damage = this->damage + parent->bufIndex;
|
2021-08-07 06:10:30 +00:00
|
|
|
bool damageAll = !update->rects || update->rectCount == 0 || damage->count < 0 ||
|
|
|
|
damage->count + update->rectCount > KVMFR_MAX_DAMAGE_RECTS;
|
|
|
|
|
|
|
|
if (damageAll)
|
2023-11-02 20:03:32 +00:00
|
|
|
{
|
|
|
|
framebuffer_read(
|
2021-08-07 06:10:30 +00:00
|
|
|
update->frame,
|
2021-08-07 23:35:56 +00:00
|
|
|
parent->buf[parent->bufIndex].map,
|
2023-11-02 20:03:32 +00:00
|
|
|
texture->format.pitch,
|
2021-08-09 04:08:10 +00:00
|
|
|
texture->format.height,
|
|
|
|
texture->format.width,
|
|
|
|
texture->format.bpp,
|
2023-11-02 20:03:32 +00:00
|
|
|
texture->format.pitch
|
2021-08-07 06:10:30 +00:00
|
|
|
);
|
2023-11-02 20:03:32 +00:00
|
|
|
}
|
2021-08-07 06:10:30 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(damage->rects + damage->count, update->rects,
|
|
|
|
update->rectCount * sizeof(FrameDamageRect));
|
|
|
|
damage->count += update->rectCount;
|
2023-11-06 02:45:02 +00:00
|
|
|
|
|
|
|
FrameDamageRect scaledDamageRects[damage->count];
|
|
|
|
for (int i = 0; i < damage->count; i++)
|
|
|
|
{
|
|
|
|
FrameDamageRect rect = damage->rects[i];
|
|
|
|
int originalX = rect.x;
|
|
|
|
int scaledX = scaleForBGR(originalX);
|
|
|
|
rect.x = scaledX;
|
|
|
|
rect.width = scaleForBGR(originalX + rect.width) - scaledX;
|
|
|
|
scaledDamageRects[i] = rect;
|
|
|
|
}
|
|
|
|
|
2021-08-07 06:10:30 +00:00
|
|
|
rectsFramebufferToBuffer(
|
2023-11-06 02:45:02 +00:00
|
|
|
scaledDamageRects,
|
2021-08-07 06:10:30 +00:00
|
|
|
damage->count,
|
2023-10-21 17:00:44 +00:00
|
|
|
texture->format.bpp,
|
2021-08-07 23:35:56 +00:00
|
|
|
parent->buf[parent->bufIndex].map,
|
2023-11-02 20:03:32 +00:00
|
|
|
texture->format.pitch,
|
2021-08-09 04:08:10 +00:00
|
|
|
texture->format.height,
|
2021-08-07 06:10:30 +00:00
|
|
|
update->frame,
|
2023-11-02 20:03:32 +00:00
|
|
|
texture->format.pitch
|
2021-08-07 06:10:30 +00:00
|
|
|
);
|
|
|
|
}
|
2021-08-02 13:37:33 +00:00
|
|
|
|
2021-08-07 23:35:56 +00:00
|
|
|
parent->buf[parent->bufIndex].updated = true;
|
2021-08-07 06:10:30 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < EGL_TEX_BUFFER_MAX; ++i)
|
|
|
|
{
|
2021-08-07 23:35:56 +00:00
|
|
|
struct TexDamage * damage = this->damage + i;
|
|
|
|
if (i == parent->bufIndex)
|
2021-08-07 06:10:30 +00:00
|
|
|
damage->count = 0;
|
|
|
|
else if (update->rects && update->rectCount > 0 && damage->count >= 0 &&
|
|
|
|
damage->count + update->rectCount <= KVMFR_MAX_DAMAGE_RECTS)
|
|
|
|
{
|
|
|
|
memcpy(damage->rects + damage->count, update->rects,
|
|
|
|
update->rectCount * sizeof(FrameDamageRect));
|
|
|
|
damage->count += update->rectCount;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
damage->count = -1;
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:35:56 +00:00
|
|
|
LG_UNLOCK(parent->copyLock);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGL_TextureOps EGL_TextureFrameBuffer =
|
|
|
|
{
|
2021-08-09 04:08:10 +00:00
|
|
|
.init = egl_texFBInit,
|
|
|
|
.free = egl_texFBFree,
|
2021-08-10 03:37:02 +00:00
|
|
|
.setup = egl_texFBSetup,
|
2021-08-09 04:08:10 +00:00
|
|
|
.update = egl_texFBUpdate,
|
|
|
|
.process = egl_texBufferStreamProcess,
|
2023-03-08 22:20:01 +00:00
|
|
|
.get = egl_texBufferStreamGet,
|
|
|
|
.bind = egl_texBufferBind
|
2021-08-02 13:37:33 +00:00
|
|
|
};
|