mirror of
https://gitlab.freedesktop.org/wlroots/wlroots.git
synced 2026-04-21 06:46:46 -04:00
Merge branch 'single-pixel-optimize' into 'master'
Draft: wlr_renderer: Add single pixel optimization See merge request wlroots/wlroots!3650
This commit is contained in:
commit
e3cc1dda94
39 changed files with 1149 additions and 972 deletions
|
|
@ -246,15 +246,17 @@ static void gles2_scissor(struct wlr_renderer *wlr_renderer,
|
|||
pop_gles2_debug(renderer);
|
||||
}
|
||||
|
||||
static bool gles2_render_subtexture_with_matrix(
|
||||
struct wlr_renderer *wlr_renderer, struct wlr_texture *wlr_texture,
|
||||
static bool gles2_render_subraster_with_matrix(
|
||||
struct wlr_renderer *wlr_renderer, struct wlr_raster *wlr_raster,
|
||||
const struct wlr_fbox *box, const float matrix[static 9],
|
||||
float alpha) {
|
||||
struct wlr_gles2_renderer *renderer =
|
||||
gles2_get_renderer_in_context(wlr_renderer);
|
||||
struct wlr_gles2_texture *texture =
|
||||
gles2_get_texture(wlr_texture);
|
||||
assert(texture->renderer == renderer);
|
||||
gles2_raster_upload(renderer, wlr_raster);
|
||||
if (!texture) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct wlr_gles2_tex_shader *shader = NULL;
|
||||
|
||||
|
|
@ -305,10 +307,10 @@ static bool gles2_render_subtexture_with_matrix(
|
|||
glUniform1i(shader->tex, 0);
|
||||
glUniform1f(shader->alpha, alpha);
|
||||
|
||||
const GLfloat x1 = box->x / wlr_texture->width;
|
||||
const GLfloat y1 = box->y / wlr_texture->height;
|
||||
const GLfloat x2 = (box->x + box->width) / wlr_texture->width;
|
||||
const GLfloat y2 = (box->y + box->height) / wlr_texture->height;
|
||||
const GLfloat x1 = box->x / wlr_raster->width;
|
||||
const GLfloat y1 = box->y / wlr_raster->height;
|
||||
const GLfloat x2 = (box->x + box->width) / wlr_raster->width;
|
||||
const GLfloat y2 = (box->y + box->height) / wlr_raster->height;
|
||||
const GLfloat texcoord[] = {
|
||||
x2, y1, // top right
|
||||
x1, y1, // top left
|
||||
|
|
@ -529,6 +531,13 @@ static void gles2_destroy(struct wlr_renderer *wlr_renderer) {
|
|||
free(renderer);
|
||||
}
|
||||
|
||||
static bool _gles2_raster_upload(struct wlr_renderer *wlr_renderer,
|
||||
struct wlr_raster *raster) {
|
||||
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
||||
struct wlr_gles2_texture *texture = gles2_raster_upload(renderer, raster);
|
||||
return texture;
|
||||
}
|
||||
|
||||
static const struct wlr_renderer_impl renderer_impl = {
|
||||
.destroy = gles2_destroy,
|
||||
.bind_buffer = gles2_bind_buffer,
|
||||
|
|
@ -536,7 +545,8 @@ static const struct wlr_renderer_impl renderer_impl = {
|
|||
.end = gles2_end,
|
||||
.clear = gles2_clear,
|
||||
.scissor = gles2_scissor,
|
||||
.render_subtexture_with_matrix = gles2_render_subtexture_with_matrix,
|
||||
.raster_upload = _gles2_raster_upload,
|
||||
.render_subraster_with_matrix = gles2_render_subraster_with_matrix,
|
||||
.render_quad_with_matrix = gles2_render_quad_with_matrix,
|
||||
.get_shm_texture_formats = gles2_get_shm_texture_formats,
|
||||
.get_dmabuf_texture_formats = gles2_get_dmabuf_texture_formats,
|
||||
|
|
@ -545,7 +555,6 @@ static const struct wlr_renderer_impl renderer_impl = {
|
|||
.read_pixels = gles2_read_pixels,
|
||||
.get_drm_fd = gles2_get_drm_fd,
|
||||
.get_render_buffer_caps = gles2_get_render_buffer_caps,
|
||||
.texture_from_buffer = gles2_texture_from_buffer,
|
||||
};
|
||||
|
||||
void push_gles2_debug_(struct wlr_gles2_renderer *renderer,
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ bool wlr_texture_is_gles2(struct wlr_texture *wlr_texture) {
|
|||
return wlr_texture->impl == &texture_impl;
|
||||
}
|
||||
|
||||
struct wlr_gles2_texture *gles2_get_texture(
|
||||
static struct wlr_gles2_texture *gles2_get_texture(
|
||||
struct wlr_texture *wlr_texture) {
|
||||
assert(wlr_texture_is_gles2(wlr_texture));
|
||||
return (struct wlr_gles2_texture *)wlr_texture;
|
||||
|
|
@ -44,14 +44,25 @@ static bool check_stride(const struct wlr_pixel_format_info *fmt,
|
|||
return true;
|
||||
}
|
||||
|
||||
static bool gles2_texture_write_pixels(struct wlr_texture *wlr_texture,
|
||||
uint32_t stride, uint32_t width, uint32_t height,
|
||||
uint32_t src_x, uint32_t src_y, uint32_t dst_x, uint32_t dst_y,
|
||||
const void *data) {
|
||||
static bool gles2_texture_update_from_raster(struct wlr_texture *wlr_texture,
|
||||
struct wlr_raster *raster, pixman_region32_t *damage) {
|
||||
struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture);
|
||||
struct wlr_buffer *buffer = raster->buffer;
|
||||
|
||||
if (texture->target != GL_TEXTURE_2D || texture->image != EGL_NO_IMAGE_KHR) {
|
||||
wlr_log(WLR_ERROR, "Cannot write pixels to immutable texture");
|
||||
return false;
|
||||
}
|
||||
|
||||
void *data;
|
||||
uint32_t format;
|
||||
size_t stride;
|
||||
if (!wlr_buffer_begin_data_ptr_access(buffer,
|
||||
WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (format != texture->drm_format) {
|
||||
wlr_buffer_end_data_ptr_access(buffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -63,24 +74,37 @@ static bool gles2_texture_write_pixels(struct wlr_texture *wlr_texture,
|
|||
drm_get_pixel_format_info(texture->drm_format);
|
||||
assert(drm_fmt);
|
||||
|
||||
if (!check_stride(drm_fmt, stride, width)) {
|
||||
if (!check_stride(drm_fmt, stride, buffer->width)) {
|
||||
wlr_buffer_end_data_ptr_access(buffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
struct wlr_gles2_renderer *renderer =
|
||||
gles2_get_renderer(texture->wlr_texture.renderer);
|
||||
|
||||
struct wlr_egl_context prev_ctx;
|
||||
wlr_egl_save_context(&prev_ctx);
|
||||
wlr_egl_make_current(texture->renderer->egl);
|
||||
wlr_egl_make_current(renderer->egl);
|
||||
|
||||
push_gles2_debug(texture->renderer);
|
||||
push_gles2_debug(renderer);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texture->tex);
|
||||
|
||||
glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride / (drm_fmt->bpp / 8));
|
||||
glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, src_x);
|
||||
glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, src_y);
|
||||
int rects_len = 0;
|
||||
pixman_box32_t *rects = pixman_region32_rectangles(damage, &rects_len);
|
||||
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, dst_x, dst_y, width, height,
|
||||
fmt->gl_format, fmt->gl_type, data);
|
||||
for (int i = 0; i < rects_len; i++) {
|
||||
pixman_box32_t rect = rects[i];
|
||||
|
||||
glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride / (drm_fmt->bpp / 8));
|
||||
glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, rect.x1);
|
||||
glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, rect.y1);
|
||||
|
||||
int width = rect.x2 - rect.x1;
|
||||
int height = rect.y2 - rect.y1;
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x1, rect.y1, width, height,
|
||||
fmt->gl_format, fmt->gl_type, data);
|
||||
}
|
||||
|
||||
glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
|
||||
glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
|
||||
|
|
@ -88,10 +112,12 @@ static bool gles2_texture_write_pixels(struct wlr_texture *wlr_texture,
|
|||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
pop_gles2_debug(texture->renderer);
|
||||
pop_gles2_debug(renderer);
|
||||
|
||||
wlr_egl_restore_context(&prev_ctx);
|
||||
|
||||
wlr_buffer_end_data_ptr_access(buffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -104,18 +130,21 @@ static bool gles2_texture_invalidate(struct wlr_gles2_texture *texture) {
|
|||
return true;
|
||||
}
|
||||
|
||||
struct wlr_gles2_renderer *renderer =
|
||||
gles2_get_renderer(texture->wlr_texture.renderer);
|
||||
|
||||
struct wlr_egl_context prev_ctx;
|
||||
wlr_egl_save_context(&prev_ctx);
|
||||
wlr_egl_make_current(texture->renderer->egl);
|
||||
wlr_egl_make_current(renderer->egl);
|
||||
|
||||
push_gles2_debug(texture->renderer);
|
||||
push_gles2_debug(renderer);
|
||||
|
||||
glBindTexture(texture->target, texture->tex);
|
||||
texture->renderer->procs.glEGLImageTargetTexture2DOES(texture->target,
|
||||
renderer->procs.glEGLImageTargetTexture2DOES(texture->target,
|
||||
texture->image);
|
||||
glBindTexture(texture->target, 0);
|
||||
|
||||
pop_gles2_debug(texture->renderer);
|
||||
pop_gles2_debug(renderer);
|
||||
|
||||
wlr_egl_restore_context(&prev_ctx);
|
||||
|
||||
|
|
@ -128,16 +157,20 @@ void gles2_texture_destroy(struct wlr_gles2_texture *texture) {
|
|||
wlr_addon_finish(&texture->buffer_addon);
|
||||
}
|
||||
|
||||
struct wlr_gles2_renderer *renderer =
|
||||
gles2_get_renderer(texture->wlr_texture.renderer);
|
||||
|
||||
|
||||
struct wlr_egl_context prev_ctx;
|
||||
wlr_egl_save_context(&prev_ctx);
|
||||
wlr_egl_make_current(texture->renderer->egl);
|
||||
wlr_egl_make_current(renderer->egl);
|
||||
|
||||
push_gles2_debug(texture->renderer);
|
||||
push_gles2_debug(renderer);
|
||||
|
||||
glDeleteTextures(1, &texture->tex);
|
||||
wlr_egl_destroy_image(texture->renderer->egl, texture->image);
|
||||
wlr_egl_destroy_image(renderer->egl, texture->image);
|
||||
|
||||
pop_gles2_debug(texture->renderer);
|
||||
pop_gles2_debug(renderer);
|
||||
|
||||
wlr_egl_restore_context(&prev_ctx);
|
||||
|
||||
|
|
@ -156,7 +189,7 @@ static void gles2_texture_unref(struct wlr_texture *wlr_texture) {
|
|||
}
|
||||
|
||||
static const struct wlr_texture_impl texture_impl = {
|
||||
.write_pixels = gles2_texture_write_pixels,
|
||||
.update_from_raster = gles2_texture_update_from_raster,
|
||||
.destroy = gles2_texture_unref,
|
||||
};
|
||||
|
||||
|
|
@ -168,18 +201,16 @@ static struct wlr_gles2_texture *gles2_texture_create(
|
|||
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
||||
return NULL;
|
||||
}
|
||||
wlr_texture_init(&texture->wlr_texture, &texture_impl, width, height);
|
||||
texture->renderer = renderer;
|
||||
wlr_texture_init(&texture->wlr_texture, &renderer->wlr_renderer,
|
||||
&texture_impl, width, height);
|
||||
wl_list_insert(&renderer->textures, &texture->link);
|
||||
return texture;
|
||||
}
|
||||
|
||||
static struct wlr_texture *gles2_texture_from_pixels(
|
||||
struct wlr_renderer *wlr_renderer,
|
||||
static struct wlr_gles2_texture *gles2_texture_from_pixels(
|
||||
struct wlr_gles2_renderer *renderer,
|
||||
uint32_t drm_format, uint32_t stride, uint32_t width,
|
||||
uint32_t height, const void *data) {
|
||||
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
||||
|
||||
const struct wlr_gles2_pixel_format *fmt =
|
||||
get_gles2_format_from_drm(drm_format);
|
||||
if (fmt == NULL) {
|
||||
|
|
@ -231,7 +262,7 @@ static struct wlr_texture *gles2_texture_from_pixels(
|
|||
|
||||
wlr_egl_restore_context(&prev_ctx);
|
||||
|
||||
return &texture->wlr_texture;
|
||||
return texture;
|
||||
}
|
||||
|
||||
static struct wlr_texture *gles2_texture_from_dmabuf(
|
||||
|
|
@ -303,7 +334,7 @@ static const struct wlr_addon_interface texture_addon_impl = {
|
|||
.destroy = texture_handle_buffer_destroy,
|
||||
};
|
||||
|
||||
static struct wlr_texture *gles2_texture_from_dmabuf_buffer(
|
||||
static struct wlr_gles2_texture *gles2_texture_from_dmabuf_buffer(
|
||||
struct wlr_gles2_renderer *renderer, struct wlr_buffer *buffer,
|
||||
struct wlr_dmabuf_attributes *dmabuf) {
|
||||
struct wlr_addon *addon =
|
||||
|
|
@ -313,16 +344,16 @@ static struct wlr_texture *gles2_texture_from_dmabuf_buffer(
|
|||
wl_container_of(addon, texture, buffer_addon);
|
||||
if (!gles2_texture_invalidate(texture)) {
|
||||
wlr_log(WLR_ERROR, "Failed to invalidate texture");
|
||||
return false;
|
||||
return NULL;
|
||||
}
|
||||
wlr_buffer_lock(texture->buffer);
|
||||
return &texture->wlr_texture;
|
||||
return texture;
|
||||
}
|
||||
|
||||
struct wlr_texture *wlr_texture =
|
||||
gles2_texture_from_dmabuf(&renderer->wlr_renderer, dmabuf);
|
||||
if (wlr_texture == NULL) {
|
||||
return false;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture);
|
||||
|
|
@ -330,28 +361,50 @@ static struct wlr_texture *gles2_texture_from_dmabuf_buffer(
|
|||
wlr_addon_init(&texture->buffer_addon, &buffer->addons,
|
||||
renderer, &texture_addon_impl);
|
||||
|
||||
return &texture->wlr_texture;
|
||||
return texture;
|
||||
}
|
||||
|
||||
struct wlr_texture *gles2_texture_from_buffer(struct wlr_renderer *wlr_renderer,
|
||||
struct wlr_buffer *buffer) {
|
||||
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
||||
struct wlr_gles2_texture *gles2_raster_upload(struct wlr_gles2_renderer *renderer,
|
||||
struct wlr_raster *wlr_raster) {
|
||||
struct wlr_texture *raster_texture;
|
||||
wl_list_for_each(raster_texture, &wlr_raster->sources, link) {
|
||||
if (wlr_texture_is_gles2(raster_texture)) {
|
||||
struct wlr_gles2_texture *gles2_tex =
|
||||
(struct wlr_gles2_texture *)raster_texture;
|
||||
if (gles2_tex->wlr_texture.renderer != &renderer->wlr_renderer) {
|
||||
continue;
|
||||
}
|
||||
return gles2_tex;
|
||||
}
|
||||
}
|
||||
|
||||
struct wlr_buffer *buffer = wlr_raster->buffer;
|
||||
if (!wlr_raster->buffer) {
|
||||
// we could possibly do a blit with another texture from another renderer,
|
||||
// but this is unsupported currently.
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct wlr_gles2_texture *texture = NULL;
|
||||
|
||||
void *data;
|
||||
uint32_t format;
|
||||
size_t stride;
|
||||
struct wlr_dmabuf_attributes dmabuf;
|
||||
if (wlr_buffer_get_dmabuf(buffer, &dmabuf)) {
|
||||
return gles2_texture_from_dmabuf_buffer(renderer, buffer, &dmabuf);
|
||||
texture = gles2_texture_from_dmabuf_buffer(renderer, buffer, &dmabuf);
|
||||
} else if (wlr_buffer_begin_data_ptr_access(buffer,
|
||||
WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) {
|
||||
struct wlr_texture *tex = gles2_texture_from_pixels(wlr_renderer,
|
||||
texture = gles2_texture_from_pixels(renderer,
|
||||
format, stride, buffer->width, buffer->height, data);
|
||||
wlr_buffer_end_data_ptr_access(buffer);
|
||||
return tex;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (texture) {
|
||||
wlr_raster_attach(wlr_raster, &texture->wlr_texture);
|
||||
}
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
void wlr_gles2_texture_get_attribs(struct wlr_texture *wlr_texture,
|
||||
|
|
|
|||
|
|
@ -40,14 +40,92 @@ bool wlr_texture_is_pixman(struct wlr_texture *texture) {
|
|||
return texture->impl == &texture_impl;
|
||||
}
|
||||
|
||||
static struct wlr_pixman_texture *get_texture(
|
||||
struct wlr_texture *wlr_texture) {
|
||||
assert(wlr_texture_is_pixman(wlr_texture));
|
||||
return (struct wlr_pixman_texture *)wlr_texture;
|
||||
static struct wlr_pixman_texture *pixman_texture_create(
|
||||
struct wlr_pixman_renderer *renderer, uint32_t drm_format,
|
||||
uint32_t width, uint32_t height) {
|
||||
struct wlr_pixman_texture *texture =
|
||||
calloc(1, sizeof(struct wlr_pixman_texture));
|
||||
if (texture == NULL) {
|
||||
wlr_log_errno(WLR_ERROR, "Failed to allocate pixman texture");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wlr_texture_init(&texture->wlr_texture, &renderer->wlr_renderer,
|
||||
&texture_impl, width, height);
|
||||
|
||||
texture->format_info = drm_get_pixel_format_info(drm_format);
|
||||
if (!texture->format_info) {
|
||||
wlr_log(WLR_ERROR, "Unsupported drm format 0x%"PRIX32, drm_format);
|
||||
free(texture);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
texture->format = get_pixman_format_from_drm(drm_format);
|
||||
if (texture->format == 0) {
|
||||
wlr_log(WLR_ERROR, "Unsupported pixman drm format 0x%"PRIX32,
|
||||
drm_format);
|
||||
free(texture);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wl_list_insert(&renderer->textures, &texture->link);
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
static struct wlr_pixman_texture *raster_upload(
|
||||
struct wlr_pixman_renderer *renderer, struct wlr_raster *wlr_raster) {
|
||||
struct wlr_texture *raster_texture;
|
||||
wl_list_for_each(raster_texture, &wlr_raster->sources, link) {
|
||||
if (wlr_texture_is_pixman(raster_texture)) {
|
||||
struct wlr_pixman_texture *pixman_tex =
|
||||
(struct wlr_pixman_texture *)raster_texture;
|
||||
if (pixman_tex->wlr_texture.renderer != &renderer->wlr_renderer) {
|
||||
continue;
|
||||
}
|
||||
return pixman_tex;
|
||||
}
|
||||
}
|
||||
|
||||
struct wlr_buffer *buffer = wlr_raster->buffer;
|
||||
if (!buffer) {
|
||||
// we could possibly do a blit with another texture from another renderer,
|
||||
// but this is unsupported currently.
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *data = NULL;
|
||||
uint32_t drm_format;
|
||||
size_t stride;
|
||||
if (!wlr_buffer_begin_data_ptr_access(buffer,
|
||||
WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &drm_format, &stride)) {
|
||||
return NULL;
|
||||
}
|
||||
wlr_buffer_end_data_ptr_access(buffer);
|
||||
|
||||
struct wlr_pixman_texture *texture = pixman_texture_create(renderer,
|
||||
drm_format, buffer->width, buffer->height);
|
||||
if (texture == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
texture->image = pixman_image_create_bits_no_clear(texture->format,
|
||||
buffer->width, buffer->height, data, stride);
|
||||
if (!texture->image) {
|
||||
wlr_log(WLR_ERROR, "Failed to create pixman image");
|
||||
wl_list_remove(&texture->link);
|
||||
free(texture);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
texture->buffer = wlr_buffer_lock(buffer);
|
||||
|
||||
wlr_raster_attach(wlr_raster, &texture->wlr_texture);
|
||||
return texture;
|
||||
}
|
||||
|
||||
static void texture_destroy(struct wlr_texture *wlr_texture) {
|
||||
struct wlr_pixman_texture *texture = get_texture(wlr_texture);
|
||||
struct wlr_pixman_texture *texture = (struct wlr_pixman_texture *)wlr_texture;
|
||||
wl_list_remove(&texture->link);
|
||||
pixman_image_unref(texture->image);
|
||||
wlr_buffer_unlock(texture->buffer);
|
||||
|
|
@ -215,13 +293,16 @@ static void matrix_to_pixman_transform(struct pixman_transform *transform,
|
|||
pixman_transform_from_pixman_f_transform(transform, &ftr);
|
||||
}
|
||||
|
||||
static bool pixman_render_subtexture_with_matrix(
|
||||
struct wlr_renderer *wlr_renderer, struct wlr_texture *wlr_texture,
|
||||
static bool pixman_render_subraster_with_matrix(
|
||||
struct wlr_renderer *wlr_renderer, struct wlr_raster *wlr_raster,
|
||||
const struct wlr_fbox *fbox, const float matrix[static 9],
|
||||
float alpha) {
|
||||
struct wlr_pixman_renderer *renderer = get_renderer(wlr_renderer);
|
||||
struct wlr_pixman_texture *texture = get_texture(wlr_texture);
|
||||
struct wlr_pixman_buffer *buffer = renderer->current_buffer;
|
||||
struct wlr_pixman_texture *texture = raster_upload(renderer, wlr_raster);
|
||||
if (!texture){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (texture->buffer != NULL) {
|
||||
void *data;
|
||||
|
|
@ -334,72 +415,6 @@ static const struct wlr_drm_format_set *pixman_get_render_formats(
|
|||
return &renderer->drm_formats;
|
||||
}
|
||||
|
||||
static struct wlr_pixman_texture *pixman_texture_create(
|
||||
struct wlr_pixman_renderer *renderer, uint32_t drm_format,
|
||||
uint32_t width, uint32_t height) {
|
||||
struct wlr_pixman_texture *texture =
|
||||
calloc(1, sizeof(struct wlr_pixman_texture));
|
||||
if (texture == NULL) {
|
||||
wlr_log_errno(WLR_ERROR, "Failed to allocate pixman texture");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wlr_texture_init(&texture->wlr_texture, &texture_impl, width, height);
|
||||
texture->renderer = renderer;
|
||||
|
||||
texture->format_info = drm_get_pixel_format_info(drm_format);
|
||||
if (!texture->format_info) {
|
||||
wlr_log(WLR_ERROR, "Unsupported drm format 0x%"PRIX32, drm_format);
|
||||
free(texture);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
texture->format = get_pixman_format_from_drm(drm_format);
|
||||
if (texture->format == 0) {
|
||||
wlr_log(WLR_ERROR, "Unsupported pixman drm format 0x%"PRIX32,
|
||||
drm_format);
|
||||
free(texture);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wl_list_insert(&renderer->textures, &texture->link);
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
static struct wlr_texture *pixman_texture_from_buffer(
|
||||
struct wlr_renderer *wlr_renderer, struct wlr_buffer *buffer) {
|
||||
struct wlr_pixman_renderer *renderer = get_renderer(wlr_renderer);
|
||||
|
||||
void *data = NULL;
|
||||
uint32_t drm_format;
|
||||
size_t stride;
|
||||
if (!wlr_buffer_begin_data_ptr_access(buffer, WLR_BUFFER_DATA_PTR_ACCESS_READ,
|
||||
&data, &drm_format, &stride)) {
|
||||
return NULL;
|
||||
}
|
||||
wlr_buffer_end_data_ptr_access(buffer);
|
||||
|
||||
struct wlr_pixman_texture *texture = pixman_texture_create(renderer,
|
||||
drm_format, buffer->width, buffer->height);
|
||||
if (texture == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
texture->image = pixman_image_create_bits_no_clear(texture->format,
|
||||
buffer->width, buffer->height, data, stride);
|
||||
if (!texture->image) {
|
||||
wlr_log(WLR_ERROR, "Failed to create pixman image");
|
||||
wl_list_remove(&texture->link);
|
||||
free(texture);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
texture->buffer = wlr_buffer_lock(buffer);
|
||||
|
||||
return &texture->wlr_texture;
|
||||
}
|
||||
|
||||
static bool pixman_bind_buffer(struct wlr_renderer *wlr_renderer,
|
||||
struct wlr_buffer *wlr_buffer) {
|
||||
struct wlr_pixman_renderer *renderer = get_renderer(wlr_renderer);
|
||||
|
|
@ -488,16 +503,23 @@ static uint32_t pixman_get_render_buffer_caps(struct wlr_renderer *renderer) {
|
|||
return WLR_BUFFER_CAP_DATA_PTR;
|
||||
}
|
||||
|
||||
static bool pixman_raster_upload(struct wlr_renderer *wlr_renderer,
|
||||
struct wlr_raster *raster) {
|
||||
struct wlr_pixman_renderer *renderer = get_renderer(wlr_renderer);
|
||||
struct wlr_pixman_texture *texture = raster_upload(renderer, raster);
|
||||
return texture;
|
||||
}
|
||||
|
||||
static const struct wlr_renderer_impl renderer_impl = {
|
||||
.begin = pixman_begin,
|
||||
.end = pixman_end,
|
||||
.clear = pixman_clear,
|
||||
.scissor = pixman_scissor,
|
||||
.render_subtexture_with_matrix = pixman_render_subtexture_with_matrix,
|
||||
.raster_upload = pixman_raster_upload,
|
||||
.render_subraster_with_matrix = pixman_render_subraster_with_matrix,
|
||||
.render_quad_with_matrix = pixman_render_quad_with_matrix,
|
||||
.get_shm_texture_formats = pixman_get_shm_texture_formats,
|
||||
.get_render_formats = pixman_get_render_formats,
|
||||
.texture_from_buffer = pixman_texture_from_buffer,
|
||||
.bind_buffer = pixman_bind_buffer,
|
||||
.destroy = pixman_destroy,
|
||||
.preferred_read_format = pixman_preferred_read_format,
|
||||
|
|
@ -528,11 +550,6 @@ struct wlr_renderer *wlr_pixman_renderer_create(void) {
|
|||
return &renderer->wlr_renderer;
|
||||
}
|
||||
|
||||
pixman_image_t *wlr_pixman_texture_get_image(struct wlr_texture *wlr_texture) {
|
||||
struct wlr_pixman_texture *texture = get_texture(wlr_texture);
|
||||
return texture->image;
|
||||
}
|
||||
|
||||
pixman_image_t *wlr_pixman_renderer_get_current_image(
|
||||
struct wlr_renderer *wlr_renderer) {
|
||||
struct wlr_pixman_renderer *renderer = get_renderer(wlr_renderer);
|
||||
|
|
|
|||
|
|
@ -740,14 +740,17 @@ static void vulkan_end(struct wlr_renderer *wlr_renderer) {
|
|||
}
|
||||
}
|
||||
|
||||
static bool vulkan_render_subtexture_with_matrix(struct wlr_renderer *wlr_renderer,
|
||||
struct wlr_texture *wlr_texture, const struct wlr_fbox *box,
|
||||
static bool vulkan_render_subraster_with_matrix(struct wlr_renderer *wlr_renderer,
|
||||
struct wlr_raster *wlr_raster, const struct wlr_fbox *box,
|
||||
const float matrix[static 9], float alpha) {
|
||||
struct wlr_vk_renderer *renderer = vulkan_get_renderer(wlr_renderer);
|
||||
VkCommandBuffer cb = renderer->cb;
|
||||
|
||||
struct wlr_vk_texture *texture = vulkan_get_texture(wlr_texture);
|
||||
assert(texture->renderer == renderer);
|
||||
struct wlr_vk_texture *texture = vulkan_raster_upload(renderer, wlr_raster);
|
||||
if (!texture) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (texture->dmabuf_imported && !texture->owned) {
|
||||
// Store this texture in the list of textures that need to be
|
||||
// acquired before rendering and released after rendering.
|
||||
|
|
@ -776,10 +779,10 @@ static bool vulkan_render_subtexture_with_matrix(struct wlr_renderer *wlr_render
|
|||
struct vert_pcr_data vert_pcr_data;
|
||||
mat3_to_mat4(final_matrix, vert_pcr_data.mat4);
|
||||
|
||||
vert_pcr_data.uv_off[0] = box->x / wlr_texture->width;
|
||||
vert_pcr_data.uv_off[1] = box->y / wlr_texture->height;
|
||||
vert_pcr_data.uv_size[0] = box->width / wlr_texture->width;
|
||||
vert_pcr_data.uv_size[1] = box->height / wlr_texture->height;
|
||||
vert_pcr_data.uv_off[0] = box->x / wlr_raster->width;
|
||||
vert_pcr_data.uv_off[1] = box->y / wlr_raster->height;
|
||||
vert_pcr_data.uv_size[0] = box->width / wlr_raster->width;
|
||||
vert_pcr_data.uv_size[1] = box->height / wlr_raster->height;
|
||||
|
||||
vkCmdPushConstants(cb, renderer->pipe_layout,
|
||||
VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(vert_pcr_data), &vert_pcr_data);
|
||||
|
|
@ -976,13 +979,21 @@ static uint32_t vulkan_get_render_buffer_caps(struct wlr_renderer *wlr_renderer)
|
|||
return WLR_BUFFER_CAP_DMABUF;
|
||||
}
|
||||
|
||||
static bool _vulkan_raster_upload(struct wlr_renderer *wlr_renderer,
|
||||
struct wlr_raster *raster) {
|
||||
struct wlr_vk_renderer *renderer = vulkan_get_renderer(wlr_renderer);
|
||||
struct wlr_vk_texture *texture = vulkan_raster_upload(renderer, raster);
|
||||
return texture;
|
||||
}
|
||||
|
||||
static const struct wlr_renderer_impl renderer_impl = {
|
||||
.bind_buffer = vulkan_bind_buffer,
|
||||
.begin = vulkan_begin,
|
||||
.end = vulkan_end,
|
||||
.clear = vulkan_clear,
|
||||
.scissor = vulkan_scissor,
|
||||
.render_subtexture_with_matrix = vulkan_render_subtexture_with_matrix,
|
||||
.raster_upload = _vulkan_raster_upload,
|
||||
.render_subraster_with_matrix = vulkan_render_subraster_with_matrix,
|
||||
.render_quad_with_matrix = vulkan_render_quad_with_matrix,
|
||||
.get_shm_texture_formats = vulkan_get_shm_texture_formats,
|
||||
.get_dmabuf_texture_formats = vulkan_get_dmabuf_texture_formats,
|
||||
|
|
@ -992,7 +1003,6 @@ static const struct wlr_renderer_impl renderer_impl = {
|
|||
.destroy = vulkan_destroy,
|
||||
.get_drm_fd = vulkan_get_drm_fd,
|
||||
.get_render_buffer_caps = vulkan_get_render_buffer_caps,
|
||||
.texture_from_buffer = vulkan_texture_from_buffer,
|
||||
};
|
||||
|
||||
// Initializes the VkDescriptorSetLayout and VkPipelineLayout needed
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ bool wlr_texture_is_vk(struct wlr_texture *wlr_texture) {
|
|||
return wlr_texture->impl == &texture_impl;
|
||||
}
|
||||
|
||||
struct wlr_vk_texture *vulkan_get_texture(struct wlr_texture *wlr_texture) {
|
||||
static struct wlr_vk_texture *vulkan_get_texture(struct wlr_texture *wlr_texture) {
|
||||
assert(wlr_texture_is_vk(wlr_texture));
|
||||
return (struct wlr_vk_texture *)wlr_texture;
|
||||
}
|
||||
|
|
@ -42,8 +42,9 @@ static bool write_pixels(struct wlr_texture *wlr_texture,
|
|||
VkAccessFlags src_access) {
|
||||
VkResult res;
|
||||
struct wlr_vk_texture *texture = vulkan_get_texture(wlr_texture);
|
||||
struct wlr_vk_renderer *renderer = texture->renderer;
|
||||
VkDevice dev = texture->renderer->dev->dev;
|
||||
struct wlr_vk_renderer *renderer =
|
||||
vulkan_get_renderer(wlr_texture->renderer);
|
||||
VkDevice dev = renderer->dev->dev;
|
||||
|
||||
// make sure assumptions are met
|
||||
assert(src_x + width <= texture->wlr_texture.width);
|
||||
|
|
@ -136,27 +137,59 @@ static bool write_pixels(struct wlr_texture *wlr_texture,
|
|||
return true;
|
||||
}
|
||||
|
||||
static bool vulkan_texture_write_pixels(struct wlr_texture *wlr_texture,
|
||||
uint32_t stride, uint32_t width, uint32_t height, uint32_t src_x,
|
||||
uint32_t src_y, uint32_t dst_x, uint32_t dst_y, const void *vdata) {
|
||||
return write_pixels(wlr_texture, stride, width, height, src_x, src_y,
|
||||
dst_x, dst_y, vdata, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
||||
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT);
|
||||
static bool vulkan_texture_update_from_raster(struct wlr_texture *wlr_texture,
|
||||
struct wlr_raster *raster, pixman_region32_t *damage) {
|
||||
struct wlr_vk_texture *texture = vulkan_get_texture(wlr_texture);
|
||||
struct wlr_buffer *buffer = raster->buffer;
|
||||
|
||||
void *data;
|
||||
uint32_t format;
|
||||
size_t stride;
|
||||
if (!wlr_buffer_begin_data_ptr_access(buffer,
|
||||
WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok = true;
|
||||
|
||||
if (format != texture->format->drm_format) {
|
||||
ok = false;
|
||||
goto out;
|
||||
}
|
||||
|
||||
int rects_len = 0;
|
||||
pixman_box32_t *rects = pixman_region32_rectangles(damage, &rects_len);
|
||||
|
||||
for (int i = 0; i < rects_len; i++) {
|
||||
pixman_box32_t rect = rects[i];
|
||||
uint32_t width = rect.x2 - rect.x1;
|
||||
uint32_t height = rect.y2 - rect.y1;
|
||||
|
||||
// TODO: only map memory once
|
||||
ok = write_pixels(wlr_texture, stride, width, height, rect.x1, rect.y1,
|
||||
rect.x1, rect.y1, data, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
||||
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT);
|
||||
if (!ok) {
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
wlr_buffer_end_data_ptr_access(buffer);
|
||||
return ok;
|
||||
}
|
||||
|
||||
void vulkan_texture_destroy(struct wlr_vk_texture *texture) {
|
||||
if (!texture->renderer) {
|
||||
free(texture);
|
||||
return;
|
||||
}
|
||||
struct wlr_vk_renderer *renderer =
|
||||
vulkan_get_renderer(texture->wlr_texture.renderer);
|
||||
|
||||
// when we recorded a command to fill this image _this_ frame,
|
||||
// it has to be executed before the texture can be destroyed.
|
||||
// Add it to the renderer->destroy_textures list, destroying
|
||||
// _after_ the stage command buffer has exectued
|
||||
if (texture->last_used == texture->renderer->frame) {
|
||||
if (texture->last_used == renderer->frame) {
|
||||
assert(texture->destroy_link.next == NULL); // not already inserted
|
||||
wl_list_insert(&texture->renderer->destroy_textures,
|
||||
wl_list_insert(&renderer->destroy_textures,
|
||||
&texture->destroy_link);
|
||||
return;
|
||||
}
|
||||
|
|
@ -164,9 +197,9 @@ void vulkan_texture_destroy(struct wlr_vk_texture *texture) {
|
|||
wl_list_remove(&texture->link);
|
||||
wl_list_remove(&texture->buffer_destroy.link);
|
||||
|
||||
VkDevice dev = texture->renderer->dev->dev;
|
||||
VkDevice dev = renderer->dev->dev;
|
||||
if (texture->ds && texture->ds_pool) {
|
||||
vulkan_free_ds(texture->renderer, texture->ds_pool, texture->ds);
|
||||
vulkan_free_ds(renderer, texture->ds_pool, texture->ds);
|
||||
}
|
||||
|
||||
vkDestroyImageView(dev, texture->image_view, NULL);
|
||||
|
|
@ -191,7 +224,7 @@ static void vulkan_texture_unref(struct wlr_texture *wlr_texture) {
|
|||
}
|
||||
|
||||
static const struct wlr_texture_impl texture_impl = {
|
||||
.write_pixels = vulkan_texture_write_pixels,
|
||||
.update_from_raster = vulkan_texture_update_from_raster,
|
||||
.destroy = vulkan_texture_unref,
|
||||
};
|
||||
|
||||
|
|
@ -203,18 +236,16 @@ static struct wlr_vk_texture *vulkan_texture_create(
|
|||
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
||||
return NULL;
|
||||
}
|
||||
wlr_texture_init(&texture->wlr_texture, &texture_impl, width, height);
|
||||
texture->renderer = renderer;
|
||||
wlr_texture_init(&texture->wlr_texture, &renderer->wlr_renderer,
|
||||
&texture_impl, width, height);
|
||||
wl_list_insert(&renderer->textures, &texture->link);
|
||||
wl_list_init(&texture->buffer_destroy.link);
|
||||
return texture;
|
||||
}
|
||||
|
||||
static struct wlr_texture *vulkan_texture_from_pixels(struct wlr_renderer *wlr_renderer,
|
||||
static struct wlr_vk_texture *vulkan_texture_from_pixels(struct wlr_vk_renderer *renderer,
|
||||
uint32_t drm_fmt, uint32_t stride, uint32_t width,
|
||||
uint32_t height, const void *data) {
|
||||
struct wlr_vk_renderer *renderer = vulkan_get_renderer(wlr_renderer);
|
||||
|
||||
VkResult res;
|
||||
VkDevice dev = renderer->dev->dev;
|
||||
|
||||
|
|
@ -338,7 +369,7 @@ static struct wlr_texture *vulkan_texture_from_pixels(struct wlr_renderer *wlr_r
|
|||
goto error;
|
||||
}
|
||||
|
||||
return &texture->wlr_texture;
|
||||
return texture;
|
||||
|
||||
error:
|
||||
vulkan_texture_destroy(texture);
|
||||
|
|
@ -573,7 +604,7 @@ error_image:
|
|||
return VK_NULL_HANDLE;
|
||||
}
|
||||
|
||||
static struct wlr_texture *vulkan_texture_from_dmabuf(struct wlr_renderer *wlr_renderer,
|
||||
static struct wlr_vk_texture *vulkan_texture_from_dmabuf(struct wlr_renderer *wlr_renderer,
|
||||
struct wlr_dmabuf_attributes *attribs) {
|
||||
struct wlr_vk_renderer *renderer = vulkan_get_renderer(wlr_renderer);
|
||||
|
||||
|
|
@ -648,7 +679,7 @@ static struct wlr_texture *vulkan_texture_from_dmabuf(struct wlr_renderer *wlr_r
|
|||
vkUpdateDescriptorSets(dev, 1, &ds_write, 0, NULL);
|
||||
texture->dmabuf_imported = true;
|
||||
|
||||
return &texture->wlr_texture;
|
||||
return texture;
|
||||
|
||||
error:
|
||||
vulkan_texture_destroy(texture);
|
||||
|
|
@ -662,50 +693,69 @@ static void texture_handle_buffer_destroy(struct wl_listener *listener,
|
|||
vulkan_texture_destroy(texture);
|
||||
}
|
||||
|
||||
static struct wlr_texture *vulkan_texture_from_dmabuf_buffer(
|
||||
static struct wlr_vk_texture *vulkan_texture_from_dmabuf_buffer(
|
||||
struct wlr_vk_renderer *renderer, struct wlr_buffer *buffer,
|
||||
struct wlr_dmabuf_attributes *dmabuf) {
|
||||
struct wlr_vk_texture *texture;
|
||||
wl_list_for_each(texture, &renderer->textures, link) {
|
||||
if (texture->buffer == buffer) {
|
||||
wlr_buffer_lock(texture->buffer);
|
||||
return &texture->wlr_texture;
|
||||
return texture;
|
||||
}
|
||||
}
|
||||
|
||||
struct wlr_texture *wlr_texture =
|
||||
vulkan_texture_from_dmabuf(&renderer->wlr_renderer, dmabuf);
|
||||
if (wlr_texture == NULL) {
|
||||
return false;
|
||||
texture = vulkan_texture_from_dmabuf(&renderer->wlr_renderer, dmabuf);
|
||||
if (!texture) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
texture = vulkan_get_texture(wlr_texture);
|
||||
texture->buffer = wlr_buffer_lock(buffer);
|
||||
|
||||
texture->buffer_destroy.notify = texture_handle_buffer_destroy;
|
||||
wl_signal_add(&buffer->events.destroy, &texture->buffer_destroy);
|
||||
|
||||
return &texture->wlr_texture;
|
||||
return texture;
|
||||
}
|
||||
|
||||
struct wlr_texture *vulkan_texture_from_buffer(
|
||||
struct wlr_renderer *wlr_renderer,
|
||||
struct wlr_buffer *buffer) {
|
||||
struct wlr_vk_renderer *renderer = vulkan_get_renderer(wlr_renderer);
|
||||
struct wlr_vk_texture *vulkan_raster_upload(struct wlr_vk_renderer *renderer,
|
||||
struct wlr_raster *wlr_raster) {
|
||||
struct wlr_texture *raster_texture;
|
||||
wl_list_for_each(raster_texture, &wlr_raster->sources, link) {
|
||||
if (wlr_texture_is_vk(raster_texture)) {
|
||||
struct wlr_vk_texture *vk_tex =
|
||||
(struct wlr_vk_texture *)raster_texture;
|
||||
if (vk_tex->wlr_texture.renderer != &renderer->wlr_renderer) {
|
||||
continue;
|
||||
}
|
||||
return vk_tex;
|
||||
}
|
||||
}
|
||||
|
||||
struct wlr_buffer *buffer = wlr_raster->buffer;
|
||||
if (!wlr_raster->buffer) {
|
||||
// we could possibly do a blit with another texture from another renderer,
|
||||
// but this is unsupported currently.
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct wlr_vk_texture *texture = NULL;
|
||||
|
||||
void *data;
|
||||
uint32_t format;
|
||||
size_t stride;
|
||||
struct wlr_dmabuf_attributes dmabuf;
|
||||
if (wlr_buffer_get_dmabuf(buffer, &dmabuf)) {
|
||||
return vulkan_texture_from_dmabuf_buffer(renderer, buffer, &dmabuf);
|
||||
texture = vulkan_texture_from_dmabuf_buffer(renderer, buffer, &dmabuf);
|
||||
} else if (wlr_buffer_begin_data_ptr_access(buffer,
|
||||
WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) {
|
||||
struct wlr_texture *tex = vulkan_texture_from_pixels(wlr_renderer,
|
||||
texture = vulkan_texture_from_pixels(renderer,
|
||||
format, stride, buffer->width, buffer->height, data);
|
||||
wlr_buffer_end_data_ptr_access(buffer);
|
||||
return tex;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (texture) {
|
||||
wlr_raster_attach(wlr_raster, &texture->wlr_texture);
|
||||
}
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <drm_fourcc.h>
|
||||
#include <wlr/render/interface.h>
|
||||
#include <wlr/render/pixman.h>
|
||||
#include <wlr/render/wlr_renderer.h>
|
||||
|
|
@ -35,7 +36,8 @@ void wlr_renderer_init(struct wlr_renderer *renderer,
|
|||
assert(impl->begin);
|
||||
assert(impl->clear);
|
||||
assert(impl->scissor);
|
||||
assert(impl->render_subtexture_with_matrix);
|
||||
assert(impl->raster_upload);
|
||||
assert(impl->render_subraster_with_matrix);
|
||||
assert(impl->render_quad_with_matrix);
|
||||
assert(impl->get_shm_texture_formats);
|
||||
assert(impl->get_render_buffer_caps);
|
||||
|
|
@ -113,39 +115,92 @@ void wlr_renderer_scissor(struct wlr_renderer *r, struct wlr_box *box) {
|
|||
r->impl->scissor(r, box);
|
||||
}
|
||||
|
||||
bool wlr_render_texture(struct wlr_renderer *r, struct wlr_texture *texture,
|
||||
bool wlr_renderer_raster_upload(struct wlr_renderer *r,
|
||||
struct wlr_raster *raster) {
|
||||
return r->impl->raster_upload(r, raster);
|
||||
}
|
||||
|
||||
bool wlr_render_raster(struct wlr_renderer *r, struct wlr_raster *raster,
|
||||
const float projection[static 9], int x, int y, float alpha) {
|
||||
struct wlr_box box = {
|
||||
.x = x,
|
||||
.y = y,
|
||||
.width = texture->width,
|
||||
.height = texture->height,
|
||||
.width = raster->width,
|
||||
.height = raster->height,
|
||||
};
|
||||
|
||||
float matrix[9];
|
||||
wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, 0,
|
||||
projection);
|
||||
|
||||
return wlr_render_texture_with_matrix(r, texture, matrix, alpha);
|
||||
return wlr_render_raster_with_matrix(r, raster, matrix, alpha);
|
||||
}
|
||||
|
||||
bool wlr_render_texture_with_matrix(struct wlr_renderer *r,
|
||||
struct wlr_texture *texture, const float matrix[static 9],
|
||||
bool wlr_render_raster_with_matrix(struct wlr_renderer *r,
|
||||
struct wlr_raster *raster, const float matrix[static 9],
|
||||
float alpha) {
|
||||
struct wlr_fbox box = {
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.width = texture->width,
|
||||
.height = texture->height,
|
||||
.width = raster->width,
|
||||
.height = raster->height,
|
||||
};
|
||||
return wlr_render_subtexture_with_matrix(r, texture, &box, matrix, alpha);
|
||||
return wlr_render_subraster_with_matrix(r, raster, &box, matrix, alpha);
|
||||
}
|
||||
|
||||
bool wlr_render_subtexture_with_matrix(struct wlr_renderer *r,
|
||||
struct wlr_texture *texture, const struct wlr_fbox *box,
|
||||
static bool try_single_pixel(struct wlr_buffer *buffer,
|
||||
float color[static 4], const struct wlr_fbox *box, float alpha) {
|
||||
if (box->width != 1.f || box->height != 1.f ||
|
||||
box->x < 0.f || box->y < 0.f ||
|
||||
box->x >= buffer->width || box->y >= buffer->height) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t x = floor(box->x);
|
||||
uint32_t y = floor(box->y);
|
||||
|
||||
if (box->x != (float)x || box->y != (float)y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void *data;
|
||||
uint32_t format;
|
||||
size_t stride;
|
||||
if (!wlr_buffer_begin_data_ptr_access(buffer,
|
||||
WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (format != DRM_FORMAT_ARGB8888) {
|
||||
wlr_buffer_end_data_ptr_access(buffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t pixel_stride = stride / buffer->width;
|
||||
uint8_t *data_color = &((uint8_t *)data)[pixel_stride * x + stride * y];
|
||||
color[0] = data_color[3] * alpha / 255.f;
|
||||
color[1] = data_color[2] * alpha / 255.f;
|
||||
color[2] = data_color[1] * alpha / 255.f;
|
||||
color[3] = data_color[0] * alpha / 255.f;
|
||||
|
||||
wlr_buffer_end_data_ptr_access(buffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool wlr_render_subraster_with_matrix(struct wlr_renderer *r,
|
||||
struct wlr_raster *raster, const struct wlr_fbox *box,
|
||||
const float matrix[static 9], float alpha) {
|
||||
assert(r->rendering);
|
||||
return r->impl->render_subtexture_with_matrix(r, texture,
|
||||
|
||||
float color[4];
|
||||
if (raster->buffer &&
|
||||
try_single_pixel(raster->buffer, (float *)&color, box, alpha)) {
|
||||
wlr_render_quad_with_matrix(r, color, matrix);
|
||||
return true;
|
||||
}
|
||||
|
||||
return r->impl->render_subraster_with_matrix(r, raster,
|
||||
box, matrix, alpha);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,80 +4,49 @@
|
|||
#include <string.h>
|
||||
#include <wlr/render/interface.h>
|
||||
#include <wlr/render/wlr_texture.h>
|
||||
#include <wlr/types/wlr_raster.h>
|
||||
#include "types/wlr_buffer.h"
|
||||
|
||||
void wlr_texture_init(struct wlr_texture *texture,
|
||||
void wlr_texture_init(struct wlr_texture *texture, struct wlr_renderer *renderer,
|
||||
const struct wlr_texture_impl *impl, uint32_t width, uint32_t height) {
|
||||
assert(renderer);
|
||||
|
||||
memset(texture, 0, sizeof(*texture));
|
||||
texture->renderer = renderer;
|
||||
texture->impl = impl;
|
||||
texture->width = width;
|
||||
texture->height = height;
|
||||
}
|
||||
|
||||
void wlr_texture_destroy(struct wlr_texture *texture) {
|
||||
if (texture && texture->impl && texture->impl->destroy) {
|
||||
if (!texture) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (texture->raster) {
|
||||
wlr_raster_detach(texture->raster, texture);
|
||||
texture->raster = NULL;
|
||||
}
|
||||
|
||||
if (texture->impl && texture->impl->destroy) {
|
||||
texture->impl->destroy(texture);
|
||||
} else {
|
||||
free(texture);
|
||||
}
|
||||
}
|
||||
|
||||
struct wlr_texture *wlr_texture_from_pixels(struct wlr_renderer *renderer,
|
||||
uint32_t fmt, uint32_t stride, uint32_t width, uint32_t height,
|
||||
const void *data) {
|
||||
assert(width > 0);
|
||||
assert(height > 0);
|
||||
assert(stride > 0);
|
||||
assert(data);
|
||||
|
||||
struct wlr_readonly_data_buffer *buffer =
|
||||
readonly_data_buffer_create(fmt, stride, width, height, data);
|
||||
if (buffer == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct wlr_texture *texture =
|
||||
wlr_texture_from_buffer(renderer, &buffer->base);
|
||||
|
||||
// By this point, the renderer should have locked the buffer if it still
|
||||
// needs to access it in the future.
|
||||
readonly_data_buffer_drop(buffer);
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
struct wlr_texture *wlr_texture_from_dmabuf(struct wlr_renderer *renderer,
|
||||
struct wlr_dmabuf_attributes *attribs) {
|
||||
struct wlr_dmabuf_buffer *buffer = dmabuf_buffer_create(attribs);
|
||||
if (buffer == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct wlr_texture *texture =
|
||||
wlr_texture_from_buffer(renderer, &buffer->base);
|
||||
|
||||
// By this point, the renderer should have locked the buffer if it still
|
||||
// needs to access it in the future.
|
||||
dmabuf_buffer_drop(buffer);
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
struct wlr_texture *wlr_texture_from_buffer(struct wlr_renderer *renderer,
|
||||
struct wlr_buffer *buffer) {
|
||||
if (!renderer->impl->texture_from_buffer) {
|
||||
return NULL;
|
||||
}
|
||||
return renderer->impl->texture_from_buffer(renderer, buffer);
|
||||
}
|
||||
|
||||
bool wlr_texture_write_pixels(struct wlr_texture *texture,
|
||||
uint32_t stride, uint32_t width, uint32_t height,
|
||||
uint32_t src_x, uint32_t src_y, uint32_t dst_x, uint32_t dst_y,
|
||||
const void *data) {
|
||||
if (!texture->impl->write_pixels) {
|
||||
bool wlr_texture_update_from_raster(struct wlr_texture *texture,
|
||||
struct wlr_raster *raster, pixman_region32_t *damage) {
|
||||
if (!texture->impl->update_from_raster) {
|
||||
return false;
|
||||
}
|
||||
return texture->impl->write_pixels(texture, stride, width, height,
|
||||
src_x, src_y, dst_x, dst_y, data);
|
||||
if (texture->width != raster->width || texture->height != raster->height) {
|
||||
return false;
|
||||
}
|
||||
const pixman_box32_t *extents = pixman_region32_extents(damage);
|
||||
if (extents->x1 < 0 || extents->y1 < 0 || extents->x2 > (int32_t)raster->width ||
|
||||
extents->y2 > (int32_t)raster->height) {
|
||||
return false;
|
||||
}
|
||||
return texture->impl->update_from_raster(texture, raster, damage);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue