Skip to content

Commit

Permalink
[android] Refactor SbDecodeTarget creation (#4598)
Browse files Browse the repository at this point in the history
Move the creation of the SbDecodeTargetPrivate from the function
DecodeTargetCreate() into the ctor of SbDecodeTargetPrivate.

Also no longer pass parameters (format, width, and height) with preset
values. There is no other functionality changes.

b/327287075
  • Loading branch information
xiaomings authored Dec 17, 2024
1 parent 4580609 commit fc163a7
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 188 deletions.
2 changes: 0 additions & 2 deletions starboard/android/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ static_library("starboard_platform") {
"configuration_public.h",
"crash_handler.cc",
"crash_handler.h",
"decode_target_create.cc",
"decode_target_create.h",
"decode_target_get_info.cc",
"decode_target_internal.cc",
"decode_target_internal.h",
Expand Down
147 changes: 0 additions & 147 deletions starboard/android/shared/decode_target_create.cc

This file was deleted.

34 changes: 0 additions & 34 deletions starboard/android/shared/decode_target_create.h

This file was deleted.

101 changes: 101 additions & 0 deletions starboard/android/shared/decode_target_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,111 @@

#include "starboard/android/shared/decode_target_internal.h"

#include <android/native_window_jni.h>
#include <jni.h>

#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>

#include <functional>

#include "starboard/android/shared/jni_env_ext.h"
#include "starboard/shared/gles/gl_call.h"

using starboard::android::shared::JniEnvExt;

namespace {

jobject CreateSurfaceTexture(int gl_texture_id) {
JniEnvExt* env = JniEnvExt::Get();

jobject local_surface_texture = env->NewObjectOrAbort(
"dev/cobalt/media/VideoSurfaceTexture", "(I)V", gl_texture_id);

jobject global_surface_texture =
env->ConvertLocalRefToGlobalRef(local_surface_texture);

return global_surface_texture;
}

jobject CreateSurfaceFromSurfaceTexture(jobject surface_texture) {
JniEnvExt* env = JniEnvExt::Get();

jobject local_surface = env->NewObjectOrAbort(
"android/view/Surface", "(Landroid/graphics/SurfaceTexture;)V",
surface_texture);

jobject global_surface = env->ConvertLocalRefToGlobalRef(local_surface);

return global_surface;
}

void RunOnContextRunner(void* context) {
std::function<void()>* closure = static_cast<std::function<void()>*>(context);
(*closure)();
}

} // namespace

SbDecodeTargetPrivate::SbDecodeTargetPrivate(
SbDecodeTargetGraphicsContextProvider* provider) {
std::function<void()> closure =
std::bind(&SbDecodeTargetPrivate::CreateOnContextRunner, this);
SbDecodeTargetRunInGlesContext(provider, &RunOnContextRunner, &closure);
}

SbDecodeTargetPrivate::SbDecodeTargetPrivate(const SbDecodeTargetPrivate& that)
: data(that.data) {}

void SbDecodeTargetPrivate::CreateOnContextRunner() {
// Setup the GL texture that Android's MediaCodec library will target with
// the decoder. We don't call glTexImage2d() on it, Android will handle
// the creation of the content when SurfaceTexture::updateTexImage() is
// called.
GLuint texture;
GL_CALL(glGenTextures(1, &texture));
GL_CALL(glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture));
GL_CALL(glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER,
GL_LINEAR));
GL_CALL(glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER,
GL_LINEAR));
GL_CALL(glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S,
GL_CLAMP_TO_EDGE));
GL_CALL(glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T,
GL_CLAMP_TO_EDGE));

data = new SbDecodeTargetPrivate::Data;

// Wrap the GL texture in an Android SurfaceTexture object.
data->surface_texture = CreateSurfaceTexture(texture);

// We will also need an Android Surface object in order to obtain a
// ANativeWindow object that we can pass into the AMediaCodec library.
data->surface = CreateSurfaceFromSurfaceTexture(data->surface_texture);

data->native_window =
ANativeWindow_fromSurface(JniEnvExt::Get(), data->surface);

// Setup our publicly accessible decode target information.
data->info.format = kSbDecodeTargetFormat1PlaneRGBA;
data->info.is_opaque = true;
data->info.width = 0;
data->info.height = 0;
data->info.planes[0].texture = texture;
data->info.planes[0].gl_texture_target = GL_TEXTURE_EXTERNAL_OES;
data->info.planes[0].width = 0;
data->info.planes[0].height = 0;

// These values will be initialized when SbPlayerGetCurrentFrame() is called.
data->info.planes[0].content_region.left = 0;
data->info.planes[0].content_region.right = 0;
data->info.planes[0].content_region.top = 0;
data->info.planes[0].content_region.bottom = 0;

GL_CALL(glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0));
}

SbDecodeTargetPrivate::Data::~Data() {
ANativeWindow_release(native_window);

Expand Down
7 changes: 7 additions & 0 deletions starboard/android/shared/decode_target_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include "starboard/decode_target.h"

struct SbDecodeTargetPrivate {
explicit SbDecodeTargetPrivate(
SbDecodeTargetGraphicsContextProvider* provider);
SbDecodeTargetPrivate(const SbDecodeTargetPrivate& that);

class Data : public starboard::RefCounted<Data> {
public:
Data() {}
Expand All @@ -42,6 +46,9 @@ struct SbDecodeTargetPrivate {
};

starboard::scoped_refptr<Data> data;

private:
void CreateOnContextRunner();
};

#endif // STARBOARD_ANDROID_SHARED_DECODE_TARGET_INTERNAL_H_
8 changes: 3 additions & 5 deletions starboard/android/shared/video_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <limits>
#include <list>

#include "starboard/android/shared/decode_target_create.h"
#include "starboard/android/shared/decode_target_internal.h"
#include "starboard/android/shared/jni_env_ext.h"
#include "starboard/android/shared/jni_utils.h"
Expand Down Expand Up @@ -685,8 +684,7 @@ bool VideoDecoder::InitializeCodec(const VideoStreamInfo& video_stream_info,
// done behind the scenes, the acquired texture is not actually backed
// by texture data until updateTexImage() is called on it.
SbDecodeTarget decode_target =
DecodeTargetCreate(decode_target_graphics_context_provider_,
kSbDecodeTargetFormat1PlaneRGBA, 0, 0);
new SbDecodeTargetPrivate(decode_target_graphics_context_provider_);
if (!SbDecodeTargetIsValid(decode_target)) {
*error_message = "Could not acquire a decode target from provider.";
SB_LOG(ERROR) << *error_message;
Expand Down Expand Up @@ -1064,8 +1062,8 @@ SbDecodeTarget VideoDecoder::GetCurrentDecodeTarget() {
}

if (first_texture_received_) {
SbDecodeTarget out_decode_target = new SbDecodeTargetPrivate;
out_decode_target->data = decode_target_->data;
SbDecodeTarget out_decode_target =
new SbDecodeTargetPrivate(*decode_target_);
return out_decode_target;
}
}
Expand Down

0 comments on commit fc163a7

Please sign in to comment.