2021-06-06 01:26:18 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2023-10-20 04:36:34 +00:00
|
|
|
* Copyright © 2017-2023 The Looking Glass Authors
|
2021-06-06 01:26:18 +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
|
|
|
|
*/
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2018-12-12 05:39:04 +00:00
|
|
|
#include "texture.h"
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
#include <stdbool.h>
|
2021-08-08 07:31:52 +00:00
|
|
|
#include <string.h>
|
2021-08-02 13:37:33 +00:00
|
|
|
#include "shader.h"
|
|
|
|
#include "common/framebuffer.h"
|
2021-08-09 04:08:10 +00:00
|
|
|
#include "common/debug.h"
|
2023-03-08 22:20:01 +00:00
|
|
|
#include "common/array.h"
|
2021-05-02 14:48:36 +00:00
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <EGL/eglext.h>
|
2020-05-29 05:48:59 +00:00
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
#include "texture_buffer.h"
|
2020-05-22 07:47:19 +00:00
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
extern const EGL_TextureOps EGL_TextureBuffer;
|
|
|
|
extern const EGL_TextureOps EGL_TextureBufferStream;
|
|
|
|
extern const EGL_TextureOps EGL_TextureFrameBuffer;
|
|
|
|
extern const EGL_TextureOps EGL_TextureDMABUF;
|
2020-05-29 05:48:59 +00:00
|
|
|
|
2021-08-11 08:53:36 +00:00
|
|
|
bool egl_textureInit(EGL_Texture ** texture_, EGLDisplay * display,
|
2022-05-22 08:19:58 +00:00
|
|
|
EGL_TexType type)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
2021-08-02 13:37:33 +00:00
|
|
|
const EGL_TextureOps * ops;
|
2020-05-29 06:54:25 +00:00
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
switch(type)
|
2018-09-23 10:45:20 +00:00
|
|
|
{
|
2021-08-02 13:37:33 +00:00
|
|
|
case EGL_TEXTYPE_BUFFER:
|
2022-05-22 08:19:58 +00:00
|
|
|
ops = &EGL_TextureBuffer;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_TEXTYPE_BUFFER_MAP:
|
|
|
|
case EGL_TEXTYPE_BUFFER_STREAM:
|
|
|
|
ops = &EGL_TextureBufferStream;
|
2018-12-04 10:23:28 +00:00
|
|
|
break;
|
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
case EGL_TEXTYPE_FRAMEBUFFER:
|
|
|
|
ops = &EGL_TextureFrameBuffer;
|
2018-12-04 10:23:28 +00:00
|
|
|
break;
|
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
case EGL_TEXTYPE_DMABUF:
|
|
|
|
ops = &EGL_TextureDMABUF;
|
2020-10-11 08:22:31 +00:00
|
|
|
break;
|
|
|
|
|
2018-09-23 10:45:20 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
*texture_ = NULL;
|
2022-05-22 08:19:58 +00:00
|
|
|
if (!ops->init(texture_, type, display))
|
2021-08-02 13:37:33 +00:00
|
|
|
return false;
|
2020-05-21 01:44:56 +00:00
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
EGL_Texture * this = *texture_;
|
|
|
|
memcpy(&this->ops, ops, sizeof(*ops));
|
|
|
|
|
|
|
|
glGenSamplers(1, &this->sampler);
|
|
|
|
glSamplerParameteri(this->sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glSamplerParameteri(this->sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glSamplerParameteri(this->sampler, GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE);
|
|
|
|
glSamplerParameteri(this->sampler, GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE);
|
2023-03-08 22:20:01 +00:00
|
|
|
|
2018-09-23 05:48:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:52:13 +00:00
|
|
|
void egl_textureFree(EGL_Texture ** tex)
|
2020-05-19 12:42:55 +00:00
|
|
|
{
|
2021-08-09 04:08:10 +00:00
|
|
|
EGL_Texture * this = *tex;
|
2022-05-22 02:05:02 +00:00
|
|
|
if (!this)
|
|
|
|
return;
|
2021-08-09 04:08:10 +00:00
|
|
|
|
|
|
|
glDeleteSamplers(1, &this->sampler);
|
|
|
|
|
|
|
|
this->ops.free(this);
|
2021-08-02 13:37:33 +00:00
|
|
|
*tex = NULL;
|
2020-05-19 12:42:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
bool egl_textureSetup(EGL_Texture * this, enum EGL_PixelFormat pixFmt,
|
2021-08-02 13:37:33 +00:00
|
|
|
size_t width, size_t height, size_t stride)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
2021-08-02 13:37:33 +00:00
|
|
|
const struct EGL_TexSetup setup =
|
|
|
|
{
|
|
|
|
.pixFmt = pixFmt,
|
|
|
|
.width = width,
|
|
|
|
.height = height,
|
|
|
|
.stride = stride
|
|
|
|
};
|
2021-08-09 04:08:10 +00:00
|
|
|
|
|
|
|
if (!egl_texUtilGetFormat(&setup, &this->format))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return this->ops.setup(this, &setup);
|
2018-09-23 05:48:44 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 08:19:58 +00:00
|
|
|
bool egl_textureUpdate(EGL_Texture * this, const uint8_t * buffer, bool topDown)
|
2019-10-01 13:17:20 +00:00
|
|
|
{
|
2021-08-02 13:37:33 +00:00
|
|
|
const struct EGL_TexUpdate update =
|
2020-05-19 12:42:55 +00:00
|
|
|
{
|
2022-05-22 08:19:58 +00:00
|
|
|
.type = EGL_TEXTYPE_BUFFER,
|
|
|
|
.x = 0,
|
|
|
|
.y = 0,
|
|
|
|
.width = this->format.width,
|
|
|
|
.height = this->format.height,
|
|
|
|
.pitch = this->format.pitch,
|
|
|
|
.stride = this->format.stride,
|
|
|
|
.topDown = topDown,
|
|
|
|
.buffer = buffer
|
2022-05-21 11:21:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return this->ops.update(this, &update);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool egl_textureUpdateRect(EGL_Texture * this,
|
|
|
|
int x, int y, int width, int height, int stride,
|
2022-05-22 08:19:58 +00:00
|
|
|
const uint8_t * buffer, bool topDown)
|
2022-05-21 11:21:16 +00:00
|
|
|
{
|
|
|
|
x = clamp(x , 0, this->format.width );
|
|
|
|
y = clamp(y , 0, this->format.height );
|
2022-05-22 01:53:46 +00:00
|
|
|
width = clamp(width , 0, this->format.width - x);
|
|
|
|
height = clamp(height, 0, this->format.height - y);
|
2022-05-21 11:21:16 +00:00
|
|
|
|
|
|
|
if (!width || !height)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const struct EGL_TexUpdate update =
|
|
|
|
{
|
2022-05-22 08:19:58 +00:00
|
|
|
.type = EGL_TEXTYPE_BUFFER,
|
|
|
|
.x = x,
|
|
|
|
.y = y,
|
|
|
|
.width = width,
|
|
|
|
.height = height,
|
|
|
|
.pitch = stride / this->format.bpp,
|
|
|
|
.stride = stride,
|
|
|
|
.topDown = topDown,
|
|
|
|
.buffer = buffer
|
2021-08-02 13:37:33 +00:00
|
|
|
};
|
2021-08-09 04:08:10 +00:00
|
|
|
|
2021-08-11 08:53:36 +00:00
|
|
|
return this->ops.update(this, &update);
|
2019-10-01 13:17:20 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
bool egl_textureUpdateFromFrame(EGL_Texture * this,
|
2021-08-07 06:10:30 +00:00
|
|
|
const FrameBuffer * frame, const FrameDamageRect * damageRects,
|
|
|
|
int damageRectsCount)
|
2020-10-29 15:32:25 +00:00
|
|
|
{
|
2021-08-02 13:37:33 +00:00
|
|
|
const struct EGL_TexUpdate update =
|
2020-10-29 15:32:25 +00:00
|
|
|
{
|
2021-08-07 06:10:30 +00:00
|
|
|
.type = EGL_TEXTYPE_FRAMEBUFFER,
|
2022-05-21 11:21:16 +00:00
|
|
|
.x = 0,
|
|
|
|
.y = 0,
|
|
|
|
.width = this->format.width,
|
|
|
|
.height = this->format.height,
|
|
|
|
.pitch = this->format.pitch,
|
|
|
|
.stride = this->format.stride,
|
2021-08-07 06:10:30 +00:00
|
|
|
.frame = frame,
|
|
|
|
.rects = damageRects,
|
|
|
|
.rectCount = damageRectsCount,
|
2021-08-02 13:37:33 +00:00
|
|
|
};
|
2021-08-09 04:08:10 +00:00
|
|
|
|
2021-08-11 08:53:36 +00:00
|
|
|
return this->ops.update(this, &update);
|
2021-08-02 13:37:33 +00:00
|
|
|
}
|
2020-10-29 15:32:25 +00:00
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
bool egl_textureUpdateFromDMA(EGL_Texture * this,
|
2021-08-02 13:37:33 +00:00
|
|
|
const FrameBuffer * frame, const int dmaFd)
|
|
|
|
{
|
|
|
|
const struct EGL_TexUpdate update =
|
2020-12-31 01:58:40 +00:00
|
|
|
{
|
2022-05-22 08:19:58 +00:00
|
|
|
.type = EGL_TEXTYPE_DMABUF,
|
|
|
|
.x = 0,
|
|
|
|
.y = 0,
|
|
|
|
.width = this->format.width,
|
|
|
|
.height = this->format.height,
|
|
|
|
.pitch = this->format.pitch,
|
|
|
|
.stride = this->format.stride,
|
|
|
|
.dmaFD = dmaFd
|
2021-08-02 13:37:33 +00:00
|
|
|
};
|
2020-10-29 15:32:25 +00:00
|
|
|
|
|
|
|
/* wait for completion */
|
2021-08-11 08:53:36 +00:00
|
|
|
framebuffer_wait(frame, this->format.bufferSize);
|
2021-08-09 04:08:10 +00:00
|
|
|
|
2021-08-11 08:53:36 +00:00
|
|
|
return this->ops.update(this, &update);
|
2020-10-29 15:32:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
enum EGL_TexStatus egl_textureProcess(EGL_Texture * this)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
2021-08-11 08:53:36 +00:00
|
|
|
return this->ops.process(this);
|
2021-08-09 04:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum EGL_TexStatus egl_textureBind(EGL_Texture * this)
|
|
|
|
{
|
2021-08-11 08:53:36 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindSampler(0, this->sampler);
|
2023-03-08 22:20:01 +00:00
|
|
|
return this->ops.bind(this);
|
2021-08-09 04:08:10 +00:00
|
|
|
}
|