OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <GLES2/gl2.h> | |
6 | |
7 #include "base/message_loop.h" | |
8 #include "chrome/renderer/ggl/ggl.h" | |
9 #include "chrome/renderer/media/gles2_video_decode_context.h" | |
10 | |
11 Gles2VideoDecodeContext::Gles2VideoDecodeContext( | |
12 MessageLoop* message_loop, bool memory_mapped, ggl::Context* context) | |
13 : message_loop_(message_loop), | |
14 memory_mapped_(memory_mapped), | |
15 context_(context) { | |
16 } | |
17 | |
18 Gles2VideoDecodeContext::~Gles2VideoDecodeContext() { | |
19 } | |
20 | |
21 void* Gles2VideoDecodeContext::GetDevice() { | |
22 // This decode context is used inside the renderer and so hardware decoder | |
23 // device handler should not be used. | |
24 return NULL; | |
25 } | |
26 | |
27 void Gles2VideoDecodeContext::AllocateVideoFrames( | |
28 int num_frames, size_t width, size_t height, | |
29 media::VideoFrame::Format format, | |
30 std::vector<scoped_refptr<media::VideoFrame> >* frames_out, Task* task) { | |
31 if (MessageLoop::current() != message_loop_) { | |
32 message_loop_->PostTask( | |
33 FROM_HERE, | |
34 NewRunnableMethod(this, &Gles2VideoDecodeContext::AllocateVideoFrames, | |
35 num_frames, width, height, format, frames_out, | |
36 task)); | |
37 return; | |
38 } | |
39 | |
40 // In this method we need to make the ggl context current and then generate | |
41 // textures for each video frame. We also need to allocate memory for each | |
42 // texture generated. | |
43 bool ret = ggl::MakeCurrent(context_); | |
44 CHECK(ret) << "Failed to switch context"; | |
45 | |
46 frames_.resize(num_frames); | |
47 for (int i = 0; i < num_frames; ++i) { | |
48 int planes = media::VideoFrame::GetNumberOfPlanes(format); | |
49 media::VideoFrame::GlTexture textures[media::VideoFrame::kMaxPlanes]; | |
50 | |
51 // Set the color format of the textures. | |
52 DCHECK(format == media::VideoFrame::RGBA || | |
53 format == media::VideoFrame::YV12); | |
54 int gl_format = format == media::VideoFrame::RGBA ? GL_RGBA : GL_LUMINANCE; | |
55 | |
56 glGenTextures(planes, textures); | |
57 for (int j = 0; j < planes; ++j) { | |
58 glBindTexture(GL_TEXTURE_2D, textures[j]); | |
59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
63 glTexImage2D(GL_TEXTURE_2D, 0, gl_format, width, height, 0, gl_format, | |
64 GL_UNSIGNED_BYTE, NULL); | |
65 } | |
66 glFlush(); | |
67 | |
68 scoped_refptr<media::VideoFrame> frame; | |
69 media::VideoFrame::CreateFrameGlTexture(format, width, height, textures, | |
70 &frame); | |
71 frames_[i] = frame; | |
72 } | |
73 *frames_out = frames_; | |
74 | |
75 task->Run(); | |
76 delete task; | |
77 } | |
78 | |
79 void Gles2VideoDecodeContext::ReleaseAllVideoFrames() { | |
80 if (MessageLoop::current() != message_loop_) { | |
81 message_loop_->PostTask( | |
82 FROM_HERE, | |
83 NewRunnableMethod(this, | |
84 &Gles2VideoDecodeContext::ReleaseAllVideoFrames)); | |
85 return; | |
86 } | |
87 | |
88 // Make the context current and then release the video frames. | |
89 bool ret = ggl::MakeCurrent(context_); | |
90 CHECK(ret) << "Failed to switch context"; | |
91 | |
92 for (size_t i = 0; i < frames_.size(); ++i) { | |
93 for (size_t j = 0; j < frames_[i]->planes(); ++j) { | |
94 media::VideoFrame::GlTexture texture = frames_[i]->gl_texture(j); | |
95 glDeleteTextures(1, &texture); | |
96 } | |
97 } | |
98 frames_.clear(); | |
99 } | |
100 | |
101 void Gles2VideoDecodeContext::ConvertToVideoFrame( | |
102 void* buffer, scoped_refptr<media::VideoFrame> frame, Task* task) { | |
103 DCHECK(memory_mapped_); | |
104 // TODO(hclam): Implement. | |
105 } | |
106 | |
107 void Gles2VideoDecodeContext::Destroy(Task* task) { | |
108 if (MessageLoop::current() != message_loop_) { | |
109 message_loop_->PostTask( | |
110 FROM_HERE, | |
111 NewRunnableMethod(this, &Gles2VideoDecodeContext::Destroy, task)); | |
112 return; | |
113 } | |
114 | |
115 ReleaseAllVideoFrames(); | |
116 DCHECK_EQ(0u, frames_.size()); | |
117 | |
118 task->Run(); | |
119 delete task; | |
120 } | |
121 | |
122 DISABLE_RUNNABLE_METHOD_REFCOUNT(Gles2VideoDecodeContext); | |
OLD | NEW |