Skip to content

Commit

Permalink
Align format handling with Skia
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon committed Apr 19, 2024
1 parent 4b27261 commit ba69216
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
36 changes: 27 additions & 9 deletions package/android/cpp/rnskia-android/SkiaOpenGLSurfaceFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace RNSkia {

thread_local SkiaOpenGLContext ThreadContextHolder::ThreadSkiaOpenGLContext;

sk_sp<SkImage>
SkiaOpenGLSurfaceFactory::makeImageFromHardwareBuffer(void *buffer) {
sk_sp<SkImage> SkiaOpenGLSurfaceFactory::makeImageFromHardwareBuffer(
void *buffer, bool requireKnownFormat) {
#if __ANDROID_API__ >= 26
// Setup OpenGL and Skia:
if (!SkiaOpenGLHelper::createSkiaDirectContextIfNecessary(
Expand All @@ -31,14 +31,32 @@ SkiaOpenGLSurfaceFactory::makeImageFromHardwareBuffer(void *buffer) {

AHardwareBuffer_Desc description;
AHardwareBuffer_describe(hardwareBuffer, &description);
if (description.format != AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM)
[[unlikely]] {
throw std::runtime_error("AHardwareBuffer has unknown format (" +
std::to_string(description.format) +
") - cannot convert to SkImage!");
GrBackendFormat format;
switch (description.format) {
// TODO: find out if we can detect, which graphic buffers support
// GR_GL_TEXTURE_2D
case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
format = GrBackendFormats::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_EXTERNAL);
case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
format = GrBackendFormats::MakeGL(GR_GL_RGBA16F, GR_GL_TEXTURE_EXTERNAL);
case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
format = GrBackendFormats::MakeGL(GR_GL_RGB565, GR_GL_TEXTURE_EXTERNAL);
case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
format = GrBackendFormats::MakeGL(GR_GL_RGB10_A2, GR_GL_TEXTURE_EXTERNAL);
case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
format = GrBackendFormats::MakeGL(GR_GL_RGB8, GR_GL_TEXTURE_EXTERNAL);
#if __ANDROID_API__ >= 33
case AHARDWAREBUFFER_FORMAT_R8_UNORM:
format = GrBackendFormats::MakeGL(GR_GL_R8, GR_GL_TEXTURE_EXTERNAL);
#endif
default:
if (requireKnownFormat) {
format = GrBackendFormat();
} else {
format = GrBackendFormats::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_EXTERNAL);
}
}
GrBackendFormat format =
GrBackendFormats::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_EXTERNAL);

auto backendTex = MakeGLBackendTexture(
ThreadContextHolder::ThreadSkiaOpenGLContext.directContext.get(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class SkiaOpenGLSurfaceFactory {
*/
static sk_sp<SkSurface> makeOffscreenSurface(int width, int height);

static sk_sp<SkImage> makeImageFromHardwareBuffer(void *buffer);
static sk_sp<SkImage> makeImageFromHardwareBuffer(void *buffer, bool requireKnownFormat = false);

/**
* Creates a windowed Skia Surface holder.
Expand Down

0 comments on commit ba69216

Please sign in to comment.