Skip to content

Instantly share code, notes, and snippets.

@fischman
Created June 13, 2012 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fischman/2921204 to your computer and use it in GitHub Desktop.
Save fischman/2921204 to your computer and use it in GitHub Desktop.
commit 7dbe5ab20cdf7b014f1697d97908dd81100ca878
Author: Ami Fischman <fischman@chromium.org>
Date: Tue Jun 12 18:18:47 2012
Better impl of MakeCurrent
diff --git a/content/common/gpu/media/gpu_video_decode_accelerator.cc b/content/common/gpu/media/gpu_video_decode_accelerator.cc
index 39dd80f..e782235 100644
--- a/content/common/gpu/media/gpu_video_decode_accelerator.cc
+++ b/content/common/gpu/media/gpu_video_decode_accelerator.cc
@@ -45,6 +45,11 @@
using gpu::gles2::TextureManager;
+static void MakeDecoderContextCurrent(gpu::gles2::GLES2Decoder* decoder) {
+ bool success = decoder->MakeCurrent();
+ DCHECK(success);
+}
+
GpuVideoDecodeAccelerator::GpuVideoDecodeAccelerator(
IPC::Message::Sender* sender,
int32 host_route_id,
@@ -54,18 +59,17 @@ GpuVideoDecodeAccelerator::GpuVideoDecodeAccelerator(
host_route_id_(host_route_id),
stub_(stub),
video_decode_accelerator_(NULL) {
+ make_context_current_ =
+ base::Bind(&MakeDecoderContextCurrent, stub_->decoder());
}
GpuVideoDecodeAccelerator::~GpuVideoDecodeAccelerator() {
- stub_->decoder()->MakeCurrent();
if (video_decode_accelerator_)
video_decode_accelerator_->Destroy();
- stub_->decoder()->ReleaseCurrent();
}
bool GpuVideoDecodeAccelerator::OnMessageReceived(const IPC::Message& msg) {
bool handled = true;
- stub_->decoder()->MakeCurrent();
IPC_BEGIN_MESSAGE_MAP(GpuVideoDecodeAccelerator, msg)
IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Decode, OnDecode)
IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_AssignPictureBuffers,
@@ -77,7 +81,6 @@ bool GpuVideoDecodeAccelerator::OnMessageReceived(const IPC::Message& msg) {
IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Destroy, OnDestroy)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
- stub_->decoder()->ReleaseCurrent();
return handled;
}
@@ -140,12 +143,10 @@ void GpuVideoDecodeAccelerator::Initialize(
#if defined(OS_CHROMEOS) || defined(OS_WIN)
DCHECK(stub_ && stub_->decoder());
- stub_->decoder()->MakeCurrent();
#if defined(OS_WIN)
if (base::win::GetVersion() < base::win::VERSION_WIN7) {
NOTIMPLEMENTED() << "HW video decode acceleration not available.";
NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
- stub_->decoder()->ReleaseCurrent();
return;
}
DLOG(INFO) << "Initializing DXVA HW decoder for windows.";
@@ -160,7 +161,7 @@ void GpuVideoDecodeAccelerator::Initialize(
stub_->decoder()->GetGLContext()->GetHandle());
#elif defined(ARCH_CPU_X86_FAMILY)
VaapiVideoDecodeAccelerator* video_decoder =
- new VaapiVideoDecodeAccelerator(this);
+ new VaapiVideoDecodeAccelerator(make_context_current_, this);
gfx::GLContextGLX* glx_context =
static_cast<gfx::GLContextGLX*>(stub_->decoder()->GetGLContext());
GLXContext glx_context_handle =
@@ -171,7 +172,6 @@ void GpuVideoDecodeAccelerator::Initialize(
video_decode_accelerator_ = video_decoder;
if (!video_decode_accelerator_->Initialize(profile))
NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
- stub_->decoder()->ReleaseCurrent();
#else // Update RenderViewImpl::createMediaPlayer when adding clauses.
NOTIMPLEMENTED() << "HW video decode acceleration not available.";
NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
diff --git a/content/common/gpu/media/gpu_video_decode_accelerator.h b/content/common/gpu/media/gpu_video_decode_accelerator.h
index d7556d8..6e72065 100644
--- a/content/common/gpu/media/gpu_video_decode_accelerator.h
+++ b/content/common/gpu/media/gpu_video_decode_accelerator.h
@@ -84,6 +84,9 @@ class GpuVideoDecodeAccelerator
// Pointer to the underlying VideoDecodeAccelerator.
scoped_refptr<media::VideoDecodeAccelerator> video_decode_accelerator_;
+ // Closure for making the relevant context current for GL calls.
+ base::Closure make_context_current_;
+
DISALLOW_IMPLICIT_CONSTRUCTORS(GpuVideoDecodeAccelerator);
};
diff --git a/content/common/gpu/media/vaapi_h264_decoder.cc b/content/common/gpu/media/vaapi_h264_decoder.cc
index e2a0fb4..e0bb250 100644
--- a/content/common/gpu/media/vaapi_h264_decoder.cc
+++ b/content/common/gpu/media/vaapi_h264_decoder.cc
@@ -448,14 +448,16 @@ bool VaapiH264Decoder::InitializeFBConfig() {
bool VaapiH264Decoder::Initialize(media::VideoCodecProfile profile,
Display* x_display,
GLXContext glx_context,
+ const base::Closure& make_context_current,
const OutputPicCB& output_pic_cb) {
DCHECK_EQ(state_, kUninitialized);
output_pic_cb_ = output_pic_cb;
x_display_ = x_display;
- parent_glx_context_ = glx_context;
+ make_context_current_ = make_context_current;
+ make_context_current_.Run();
if (!SetProfile(profile)) {
DVLOG(1) << "Unsupported profile";
return false;
@@ -549,6 +551,7 @@ bool VaapiH264Decoder::CreateVASurfaces() {
DCHECK_NE(pic_width_, -1);
DCHECK_NE(pic_height_, -1);
DCHECK_EQ(state_, kInitialized);
+ make_context_current_.Run();
// Allocate VASurfaces in driver.
VAStatus va_res = VAAPI_CreateSurfaces(va_display_, pic_width_,
@@ -571,6 +574,7 @@ bool VaapiH264Decoder::CreateVASurfaces() {
void VaapiH264Decoder::DestroyVASurfaces() {
DCHECK(state_ == kDecoding || state_ == kError || state_ == kAfterReset);
+ make_context_current_.Run();
decode_surfaces_.clear();
@@ -686,6 +690,7 @@ VaapiH264Decoder::DecodeSurface* VaapiH264Decoder::UnassignSurfaceFromPoC(
// Fill a VAPictureParameterBufferH264 to be later sent to the HW decoder.
bool VaapiH264Decoder::SendPPS() {
+ make_context_current_.Run();
const H264PPS* pps = parser_.GetPPS(curr_pps_id_);
DCHECK(pps);
@@ -783,6 +788,7 @@ bool VaapiH264Decoder::SendPPS() {
// Fill a VAIQMatrixBufferH264 to be later sent to the HW decoder.
bool VaapiH264Decoder::SendIQMatrix() {
+ make_context_current_.Run();
const H264PPS* pps = parser_.GetPPS(curr_pps_id_);
DCHECK(pps);
@@ -829,6 +835,7 @@ bool VaapiH264Decoder::SendIQMatrix() {
}
bool VaapiH264Decoder::SendVASliceParam(H264SliceHeader* slice_hdr) {
+ make_context_current_.Run();
const H264PPS* pps = parser_.GetPPS(slice_hdr->pic_parameter_set_id);
DCHECK(pps);
@@ -932,6 +939,7 @@ bool VaapiH264Decoder::SendVASliceParam(H264SliceHeader* slice_hdr) {
bool VaapiH264Decoder::SendSliceData(const uint8* ptr, size_t size)
{
+ make_context_current_.Run();
// Can't help it, blame libva...
void* non_const_ptr = const_cast<uint8*>(ptr);
@@ -964,6 +972,7 @@ bool VaapiH264Decoder::QueueSlice(H264SliceHeader* slice_hdr) {
bool VaapiH264Decoder::DecodePicture() {
DCHECK(!frame_ready_at_hw_);
DCHECK(curr_pic_.get());
+ make_context_current_.Run();
static const size_t kMaxVABuffers = 32;
DCHECK_LE(pending_va_bufs_.size(), kMaxVABuffers);
@@ -2060,4 +2069,3 @@ size_t VaapiH264Decoder::GetRequiredNumOfPictures() {
}
} // namespace content
-
diff --git a/content/common/gpu/media/vaapi_h264_decoder.h b/content/common/gpu/media/vaapi_h264_decoder.h
index abf6738..a849e99 100644
--- a/content/common/gpu/media/vaapi_h264_decoder.h
+++ b/content/common/gpu/media/vaapi_h264_decoder.h
@@ -75,6 +75,7 @@ class VaapiH264Decoder {
bool Initialize(media::VideoCodecProfile profile,
Display* x_display,
GLXContext glx_context,
+ const base::Closure& make_context_current,
const OutputPicCB& output_pic_cb) WARN_UNUSED_RESULT;
void Destroy();
@@ -311,12 +312,14 @@ class VaapiH264Decoder {
// X/GLX handles.
Display* x_display_;
- GLXContext parent_glx_context_;
+ base::Closure make_context_current_;
GLXFBConfig fb_config_;
// VA handles.
VADisplay va_display_;
VAConfigID va_config_id_;
+ // Any method that uses this probably wants to make sure
+ // make_context_current_.Run() is called at the top of the method.
VAContextID va_context_id_;
VAProfile profile_;
@@ -332,4 +335,3 @@ class VaapiH264Decoder {
} // namespace content
#endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_H264_DECODER_H_
-
diff --git a/content/common/gpu/media/vaapi_video_decode_accelerator.cc b/content/common/gpu/media/vaapi_video_decode_accelerator.cc
index 9bf91ac..6b3533b 100644
--- a/content/common/gpu/media/vaapi_video_decode_accelerator.cc
+++ b/content/common/gpu/media/vaapi_video_decode_accelerator.cc
@@ -49,8 +49,10 @@ void VaapiVideoDecodeAccelerator::NotifyError(Error error) {
client_ = NULL;
}
-VaapiVideoDecodeAccelerator::VaapiVideoDecodeAccelerator(Client* client)
- : state_(kUninitialized),
+VaapiVideoDecodeAccelerator::VaapiVideoDecodeAccelerator(
+ const base::Closure& make_context_current, Client* client)
+ : make_context_current_(make_context_current),
+ state_(kUninitialized),
input_ready_(&lock_),
output_ready_(&lock_),
message_loop_(MessageLoop::current()),
@@ -79,7 +81,7 @@ bool VaapiVideoDecodeAccelerator::Initialize(
PLATFORM_FAILURE, false);
res = decoder_.Initialize(
- profile, x_display_, glx_context_,
+ profile, x_display_, glx_context_, make_context_current_,
base::Bind(&VaapiVideoDecodeAccelerator::OutputPicCallback, this));
RETURN_AND_NOTIFY_ON_FAILURE(res, "Failed initializing decoder",
PLATFORM_FAILURE, false);
diff --git a/content/common/gpu/media/vaapi_video_decode_accelerator.h b/content/common/gpu/media/vaapi_video_decode_accelerator.h
index 8b1d433..faff021 100644
--- a/content/common/gpu/media/vaapi_video_decode_accelerator.h
+++ b/content/common/gpu/media/vaapi_video_decode_accelerator.h
@@ -32,7 +32,8 @@
// Decoding tasks are performed in a separate decoding thread.
class VaapiVideoDecodeAccelerator : public media::VideoDecodeAccelerator {
public:
- VaapiVideoDecodeAccelerator(Client* client);
+ VaapiVideoDecodeAccelerator(const base::Closure& make_context_current,
+ Client* client);
// media::VideoDecodeAccelerator implementation.
virtual bool Initialize(media::VideoCodecProfile profile) OVERRIDE;
@@ -136,6 +137,7 @@ class VaapiVideoDecodeAccelerator : public media::VideoDecodeAccelerator {
// Client-provided X/GLX state.
Display* x_display_;
GLXContext glx_context_;
+ base::Closure make_context_current_;
// VAVDA state.
enum State {
@@ -205,4 +207,3 @@ class VaapiVideoDecodeAccelerator : public media::VideoDecodeAccelerator {
};
#endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_
-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment