-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
587 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright 2020 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/shell/platform/linux/fl_external_texture_gl.h" | ||
|
||
#include <EGL/egl.h> | ||
#include <GL/gl.h> | ||
#include <gmodule.h> | ||
|
||
struct _FlExternalTextureGl { | ||
GLuint gl_texture_id; | ||
FlTextureCallback callback; | ||
struct { | ||
bool valid; | ||
void (*genTextures)(GLsizei n, GLuint* textures); | ||
void (*bindTexture)(GLenum target, GLuint texture); | ||
void (*texParameteri)(GLenum target, GLenum pname, GLenum param); | ||
void (*texImage2D)(GLenum target, | ||
GLint level, | ||
GLint internalformat, | ||
GLsizei width, | ||
GLsizei height, | ||
GLint border, | ||
GLenum format, | ||
GLenum type, | ||
const void* data); | ||
void (*deleteTextures)(GLsizei n, const GLuint* textures); | ||
} gl; | ||
void* user_data; | ||
}; | ||
|
||
G_DEFINE_TYPE(FlExternalTextureGl, fl_external_texture_gl, G_TYPE_OBJECT) | ||
|
||
static void fl_external_texture_gl_dispose(GObject* object) { | ||
FlExternalTextureGl* self = FL_EXTERNAL_TEXTURE_GL(object); | ||
if (self->gl.valid) { | ||
self->gl.deleteTextures(1, &self->gl_texture_id); | ||
} | ||
|
||
G_OBJECT_CLASS(fl_external_texture_gl_parent_class)->dispose(object); | ||
} | ||
|
||
static void fl_external_texture_gl_class_init(FlExternalTextureGlClass* klass) { | ||
G_OBJECT_CLASS(klass)->dispose = fl_external_texture_gl_dispose; | ||
} | ||
|
||
static void fl_external_texture_gl_init(FlExternalTextureGl* self) {} | ||
|
||
int64_t fl_external_texture_gl_texture_id(FlExternalTextureGl* self) { | ||
return reinterpret_cast<int64_t>(self); | ||
} | ||
|
||
bool fl_external_texture_gl_populate_texture( | ||
FlExternalTextureGl* self, | ||
size_t width, | ||
size_t height, | ||
FlutterOpenGLTexture* opengl_texture) { | ||
size_t real_width = width, real_height = height; | ||
if (!fl_external_texture_gl_copy_pixel_buffer(self, &real_width, | ||
&real_height)) | ||
return false; | ||
|
||
opengl_texture->target = GL_TEXTURE_2D; | ||
opengl_texture->name = self->gl_texture_id; | ||
opengl_texture->format = GL_RGBA8; | ||
opengl_texture->destruction_callback = nullptr; | ||
opengl_texture->user_data = static_cast<void*>(self); | ||
opengl_texture->width = real_width; | ||
opengl_texture->height = real_height; | ||
|
||
return true; | ||
} | ||
|
||
void fl_external_texture_gl_load_funcs(FlExternalTextureGl* self) { | ||
self->gl.genTextures = reinterpret_cast<void (*)(GLsizei, GLuint*)>( | ||
eglGetProcAddress("glGenTextures")); | ||
self->gl.bindTexture = reinterpret_cast<void (*)(GLenum, GLuint)>( | ||
eglGetProcAddress("glBindTexture")); | ||
self->gl.texParameteri = reinterpret_cast<void (*)(GLenum, GLenum, GLenum)>( | ||
eglGetProcAddress("glTexParameteri")); | ||
self->gl.texImage2D = | ||
reinterpret_cast<void (*)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, | ||
GLenum, GLenum, const void*)>( | ||
eglGetProcAddress("glTexImage2D")); | ||
self->gl.deleteTextures = reinterpret_cast<void (*)(GLsizei, const GLuint*)>( | ||
eglGetProcAddress("glDeleteTextures")); | ||
self->gl.valid = true; | ||
} | ||
|
||
bool fl_external_texture_gl_copy_pixel_buffer(FlExternalTextureGl* self, | ||
size_t* width, | ||
size_t* height) { | ||
const FlPixelBuffer* pixel_buffer = | ||
self->callback(*width, *height, self->user_data); | ||
if (!pixel_buffer || !pixel_buffer->buffer) | ||
return false; | ||
*width = pixel_buffer->width; | ||
*height = pixel_buffer->height; | ||
|
||
if (!self->gl.valid) | ||
fl_external_texture_gl_load_funcs(self); | ||
if (self->gl_texture_id == 0) { | ||
self->gl.genTextures(1, &self->gl_texture_id); | ||
self->gl.bindTexture(GL_TEXTURE_2D, self->gl_texture_id); | ||
self->gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, | ||
GL_CLAMP_TO_BORDER); | ||
self->gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, | ||
GL_CLAMP_TO_BORDER); | ||
self->gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | ||
self->gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | ||
} else { | ||
self->gl.bindTexture(GL_TEXTURE_2D, self->gl_texture_id); | ||
} | ||
self->gl.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixel_buffer->width, | ||
pixel_buffer->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, | ||
pixel_buffer->buffer); | ||
return true; | ||
} | ||
|
||
FlExternalTextureGl* fl_external_texture_gl_new( | ||
FlTextureCallback texture_callback, | ||
void* user_data) { | ||
FlExternalTextureGl* self = FL_EXTERNAL_TEXTURE_GL( | ||
g_object_new(fl_external_texture_gl_get_type(), nullptr)); | ||
|
||
self->callback = texture_callback; | ||
self->user_data = user_data; | ||
|
||
return self; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2020 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_SHELL_PLATFORM_LINUX_FL_EXTERNAL_TEXURE_GL_H | ||
#define FLUTTER_SHELL_PLATFORM_LINUX_FL_EXTERNAL_TEXURE_GL_H | ||
|
||
#include <glib-object.h> | ||
#include "flutter/shell/platform/embedder/embedder.h" | ||
#include "flutter/shell/platform/linux/public/flutter_linux/fl_texture_registrar.h" | ||
|
||
G_BEGIN_DECLS | ||
|
||
G_DECLARE_FINAL_TYPE(FlExternalTextureGl, | ||
fl_external_texture_gl, | ||
FL, | ||
EXTERNAL_TEXTURE_GL, | ||
GObject) | ||
|
||
/** | ||
* FlExternalTextureGl: | ||
* | ||
* #FlExternalTextureGl is an abstraction over OpenGL textures. | ||
*/ | ||
|
||
/** | ||
* fl_external_texture_gl_new: | ||
* @texture_callback: An #FlTextureCallback. | ||
* @user_data: A void*. | ||
* | ||
* Creates a new #FlExternalTextureGl. | ||
* | ||
* Returns: a new #FlExternalTextureGl. | ||
*/ | ||
FlExternalTextureGl* fl_external_texture_gl_new( | ||
FlTextureCallback texture_callback, | ||
void* user_data); | ||
|
||
/** | ||
* fl_external_texture_gl_populate_texture: | ||
* @width: a size_t. | ||
* @height: a size_t. | ||
* @opengl_texture: a FlutterOpenGLTexture*. | ||
* | ||
* Attempts to populate the specified |opengl_texture| with texture details | ||
* such as the name, width, height and the pixel format upon successfully | ||
* copying the buffer provided by |texture_callback_|. See | ||
* |fl_external_texture_gl_copy_pixel_buffer|. | ||
* | ||
* Returns true on success or false if the pixel buffer could not be copied. | ||
*/ | ||
bool fl_external_texture_gl_populate_texture( | ||
FlExternalTextureGl* self, | ||
size_t width, | ||
size_t height, | ||
FlutterOpenGLTexture* opengl_texture); | ||
|
||
/** | ||
* fl_external_texture_gl_texture_id: | ||
* | ||
* Retrieves the unique id of this texture. | ||
* | ||
* Returns an int64_t, which is the unique id of this texture. | ||
*/ | ||
int64_t fl_external_texture_gl_texture_id(FlExternalTextureGl* self); | ||
|
||
/** | ||
* fl_external_texture_gl_copy_pixel_buffer: | ||
* @width: a size_t. | ||
* @height: a size_t. | ||
* | ||
* Attempts to copy the pixel buffer returned by |texture_callback_| to | ||
* OpenGL. The |width| and |height| will be set to the actual bounds of the | ||
* copied pixel buffer. | ||
* | ||
* Returns true on success or false if the pixel buffer returned by | ||
* |texture_callback_| was invalid. | ||
*/ | ||
bool fl_external_texture_gl_copy_pixel_buffer(FlExternalTextureGl* self, | ||
size_t* width, | ||
size_t* height); | ||
|
||
G_END_DECLS | ||
|
||
#endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_EXTERNAL_TEXURE_GL_H |
Oops, something went wrong.