OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string.h> | 5 #include <string.h> |
6 | 6 |
7 #include <iostream> | 7 #include <iostream> |
8 #include <sstream> | 8 #include <sstream> |
9 #include <list> | 9 #include <list> |
10 #include <map> | 10 #include <map> |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
66 PP_Size dimensions); | 66 PP_Size dimensions); |
67 virtual void DismissPictureBuffer(PP_Resource decoder, | 67 virtual void DismissPictureBuffer(PP_Resource decoder, |
68 int32_t picture_buffer_id); | 68 int32_t picture_buffer_id); |
69 virtual void PictureReady(PP_Resource decoder, const PP_Picture_Dev& picture); | 69 virtual void PictureReady(PP_Resource decoder, const PP_Picture_Dev& picture); |
70 virtual void EndOfStream(PP_Resource decoder); | 70 virtual void EndOfStream(PP_Resource decoder); |
71 virtual void NotifyError(PP_Resource decoder, PP_VideoDecodeError_Dev error); | 71 virtual void NotifyError(PP_Resource decoder, PP_VideoDecodeError_Dev error); |
72 | 72 |
73 private: | 73 private: |
74 enum { kNumConcurrentDecodes = 7 }; | 74 enum { kNumConcurrentDecodes = 7 }; |
75 | 75 |
76 // Initialize Video Decoder. | 76 // State of a single decoder. |
77 void InitializeDecoder(); | 77 struct DecoderState { |
78 DecoderState(GLES2DemoInstance* gles2, pp::VideoDecoder_Dev* decoder); | |
79 ~DecoderState(); | |
78 | 80 |
79 // Callbacks passed into pp:VideoDecoder_Dev functions. | 81 // Cleanup helpers. |
80 void DecoderInitDone(int32_t result); | 82 void DeleteOutstandingBitstreamBuffers(); |
81 void DecoderBitstreamDone(int32_t result, int bitstream_buffer_id); | 83 void DeleteOutstandingTextures(); |
82 void DecoderFlushDone(int32_t result); | |
83 | 84 |
84 // Decode helpers. | 85 // Decode helpers. |
85 void DecodeNextNALUs(); | 86 void DecodeNextNALUs(); |
86 void DecodeNextNALU(); | 87 void DecodeNextNALU(); |
87 void GetNextNALUBoundary(size_t start_pos, size_t* end_pos); | 88 static void GetNextNALUBoundary(size_t start_pos, size_t* end_pos); |
88 void PaintStart(const PP_Picture_Dev& picture); | 89 void DecoderBitstreamDone(int32_t result, int bitstream_buffer_id); |
89 void DeleteOutstandingBitstreamBuffers(); | 90 void DecoderFlushDone(int32_t result); |
91 | |
92 GLES2DemoInstance* gles2_; | |
93 pp::VideoDecoder_Dev* decoder_; | |
94 pp::CompletionCallbackFactory<DecoderState> callback_factory_; | |
95 int next_picture_buffer_id_; | |
96 int next_bitstream_buffer_id_; | |
97 size_t encoded_data_next_pos_to_decode_; | |
98 std::set<int> bitstream_ids_at_decoder_; | |
99 // Map of texture buffers indexed by buffer id. | |
100 typedef std::map<int, PP_PictureBuffer_Dev> PictureBufferMap; | |
101 PictureBufferMap buffers_by_id_; | |
102 // Map of bitstream buffers indexed by id. | |
103 typedef std::map<int, pp::Buffer_Dev*> BitstreamBufferMap; | |
104 BitstreamBufferMap bitstream_buffers_by_id_; | |
105 }; | |
106 | |
107 // Initialize Video Decoders. | |
108 void InitializeDecoders(); | |
90 | 109 |
91 // GL-related functions. | 110 // GL-related functions. |
92 void InitGL(); | 111 void InitGL(); |
93 GLuint CreateTexture(int32_t width, int32_t height); | 112 GLuint CreateTexture(int32_t width, int32_t height); |
94 void CreateGLObjects(); | 113 void CreateGLObjects(); |
95 void CreateShader(GLuint program, GLenum type, const char* source, int size); | 114 void CreateShader(GLuint program, GLenum type, const char* source, int size); |
96 void DeleteTexture(GLuint id); | 115 void DeleteTexture(GLuint id); |
97 void DeleteOutstandingTextures(); | 116 void PaintFinished(int32_t result, PP_Resource decoder, |
98 void PaintFinished(int32_t result, int picture_buffer_id); | 117 int picture_buffer_id); |
99 | 118 |
100 // Log an error to the developer console and stderr (though the latter may be | 119 // Log an error to the developer console and stderr (though the latter may be |
101 // closed due to sandboxing or blackholed for other reasons) by creating a | 120 // closed due to sandboxing or blackholed for other reasons) by creating a |
102 // temporary of this type and streaming to it. Example usage: | 121 // temporary of this type and streaming to it. Example usage: |
103 // LogError(this).s() << "Hello world: " << 42; | 122 // LogError(this).s() << "Hello world: " << 42; |
104 class LogError { | 123 class LogError { |
105 public: | 124 public: |
106 LogError(GLES2DemoInstance* demo) : demo_(demo) {} | 125 LogError(GLES2DemoInstance* demo) : demo_(demo) {} |
107 ~LogError() { | 126 ~LogError() { |
108 const std::string& msg = stream_.str(); | 127 const std::string& msg = stream_.str(); |
109 demo_->console_if_->Log(demo_->pp_instance(), PP_LOGLEVEL_ERROR, | 128 demo_->console_if_->Log(demo_->pp_instance(), PP_LOGLEVEL_ERROR, |
110 pp::Var(msg).pp_var()); | 129 pp::Var(msg).pp_var()); |
111 std::cerr << msg << std::endl; | 130 std::cerr << msg << std::endl; |
112 } | 131 } |
113 // Impl note: it would have been nicer to have LogError derive from | 132 // Impl note: it would have been nicer to have LogError derive from |
114 // std::ostringstream so that it can be streamed to directly, but lookup | 133 // std::ostringstream so that it can be streamed to directly, but lookup |
115 // rules turn streamed string literals to hex pointers on output. | 134 // rules turn streamed string literals to hex pointers on output. |
116 std::ostringstream& s() { return stream_; } | 135 std::ostringstream& s() { return stream_; } |
117 private: | 136 private: |
118 GLES2DemoInstance* demo_; // Unowned. | 137 GLES2DemoInstance* demo_; // Unowned. |
119 std::ostringstream stream_; | 138 std::ostringstream stream_; |
120 }; | 139 }; |
121 | 140 |
122 pp::Size position_size_; | 141 pp::Size position_size_; |
vrk (LEFT CHROMIUM)
2011/08/16 22:06:21
nit: rename position_size_ to viewport_size_? (If
Ami GONE FROM CHROMIUM
2011/08/16 22:56:26
It's not.
The size is that of the position argumen
| |
123 int next_picture_buffer_id_; | |
124 int next_bitstream_buffer_id_; | |
125 bool is_painting_; | 142 bool is_painting_; |
126 pp::CompletionCallbackFactory<GLES2DemoInstance> callback_factory_; | |
127 size_t encoded_data_next_pos_to_decode_; | |
128 std::set<int> bitstream_ids_at_decoder_; | |
129 // When decode outpaces render, we queue up decoded pictures for later | 143 // When decode outpaces render, we queue up decoded pictures for later |
130 // painting. | 144 // painting. Elements are <decoder,picture>. |
131 std::list<PP_Picture_Dev> pictures_pending_paint_; | 145 std::list<std::pair<PP_Resource, PP_Picture_Dev> > pictures_pending_paint_; |
132 int num_frames_rendered_; | 146 int num_frames_rendered_; |
133 | |
134 // Map of texture buffers indexed by buffer id. | |
135 typedef std::map<int, PP_PictureBuffer_Dev> PictureBufferMap; | |
136 PictureBufferMap buffers_by_id_; | |
137 // Map of bitstream buffers indexed by id. | |
138 typedef std::map<int, pp::Buffer_Dev*> BitstreamBufferMap; | |
139 BitstreamBufferMap bitstream_buffers_by_id_; | |
140 PP_TimeTicks first_frame_delivered_ticks_; | 147 PP_TimeTicks first_frame_delivered_ticks_; |
141 PP_TimeTicks last_swap_request_ticks_; | 148 PP_TimeTicks last_swap_request_ticks_; |
142 PP_TimeTicks swap_ticks_; | 149 PP_TimeTicks swap_ticks_; |
150 pp::CompletionCallbackFactory<GLES2DemoInstance> callback_factory_; | |
143 | 151 |
144 // Unowned pointers. | 152 // Unowned pointers. |
145 const struct PPB_Console_Dev* console_if_; | 153 const struct PPB_Console_Dev* console_if_; |
146 const struct PPB_Core* core_if_; | 154 const struct PPB_Core* core_if_; |
147 const struct PPB_OpenGLES2_Dev* gles2_if_; | 155 const struct PPB_OpenGLES2_Dev* gles2_if_; |
148 | 156 |
149 // Owned data. | 157 // Owned data. |
150 pp::Context3D_Dev* context_; | 158 pp::Context3D_Dev* context_; |
151 pp::Surface3D_Dev* surface_; | 159 pp::Surface3D_Dev* surface_; |
152 pp::VideoDecoder_Dev* video_decoder_; | 160 typedef std::map<int, DecoderState*> Decoders; |
161 Decoders video_decoders_; | |
153 }; | 162 }; |
154 | 163 |
164 GLES2DemoInstance::DecoderState::DecoderState(GLES2DemoInstance* gles2, | |
165 pp::VideoDecoder_Dev* decoder) | |
166 : gles2_(gles2), decoder_(decoder), callback_factory_(this), | |
167 next_picture_buffer_id_(0), | |
168 next_bitstream_buffer_id_(0), encoded_data_next_pos_to_decode_(0) { | |
169 }; | |
170 | |
171 GLES2DemoInstance::DecoderState::~DecoderState() { | |
172 delete decoder_; | |
173 decoder_ = NULL; | |
174 } | |
175 | |
176 void GLES2DemoInstance::DecoderState::DeleteOutstandingTextures() { | |
177 for (PictureBufferMap::iterator it = buffers_by_id_.begin(); | |
178 it != buffers_by_id_.end(); ++it) { | |
179 gles2_->DeleteTexture(it->second.texture_id); | |
180 } | |
181 buffers_by_id_.clear(); | |
182 } | |
183 | |
184 void GLES2DemoInstance::DecoderState::DeleteOutstandingBitstreamBuffers() { | |
185 for (BitstreamBufferMap::iterator it = bitstream_buffers_by_id_.begin(); | |
186 it != bitstream_buffers_by_id_.end(); ++it) { | |
187 delete it->second; | |
188 } | |
189 bitstream_buffers_by_id_.clear(); | |
190 } | |
191 | |
155 GLES2DemoInstance::GLES2DemoInstance(PP_Instance instance, pp::Module* module) | 192 GLES2DemoInstance::GLES2DemoInstance(PP_Instance instance, pp::Module* module) |
156 : pp::Instance(instance), pp::Graphics3DClient_Dev(this), | 193 : pp::Instance(instance), pp::Graphics3DClient_Dev(this), |
157 pp::VideoDecoderClient_Dev(this), | 194 pp::VideoDecoderClient_Dev(this), |
158 next_picture_buffer_id_(0), | |
159 next_bitstream_buffer_id_(0), | |
160 callback_factory_(this), | |
161 encoded_data_next_pos_to_decode_(0), | |
162 num_frames_rendered_(0), | 195 num_frames_rendered_(0), |
163 first_frame_delivered_ticks_(-1), | 196 first_frame_delivered_ticks_(-1), |
164 swap_ticks_(0), | 197 swap_ticks_(0), |
198 callback_factory_(this), | |
165 context_(NULL), | 199 context_(NULL), |
166 surface_(NULL), | 200 surface_(NULL) { |
167 video_decoder_(NULL) { | |
168 assert((console_if_ = static_cast<const struct PPB_Console_Dev*>( | 201 assert((console_if_ = static_cast<const struct PPB_Console_Dev*>( |
169 module->GetBrowserInterface(PPB_CONSOLE_DEV_INTERFACE)))); | 202 module->GetBrowserInterface(PPB_CONSOLE_DEV_INTERFACE)))); |
170 assert((core_if_ = static_cast<const struct PPB_Core*>( | 203 assert((core_if_ = static_cast<const struct PPB_Core*>( |
171 module->GetBrowserInterface(PPB_CORE_INTERFACE)))); | 204 module->GetBrowserInterface(PPB_CORE_INTERFACE)))); |
172 assert((gles2_if_ = static_cast<const struct PPB_OpenGLES2_Dev*>( | 205 assert((gles2_if_ = static_cast<const struct PPB_OpenGLES2_Dev*>( |
173 module->GetBrowserInterface(PPB_OPENGLES2_DEV_INTERFACE)))); | 206 module->GetBrowserInterface(PPB_OPENGLES2_DEV_INTERFACE)))); |
174 } | 207 } |
175 | 208 |
176 GLES2DemoInstance::~GLES2DemoInstance() { | 209 GLES2DemoInstance::~GLES2DemoInstance() { |
177 delete video_decoder_; // May be NULL, which is fine. | 210 for (Decoders::iterator it = video_decoders_.begin(); |
178 DeleteOutstandingBitstreamBuffers(); | 211 it != video_decoders_.end(); ++it) { |
179 DeleteOutstandingTextures(); | 212 it->second->DeleteOutstandingBitstreamBuffers(); |
vrk (LEFT CHROMIUM)
2011/08/16 22:06:21
Any reason not to push these DeleteOutstandingBits
Ami GONE FROM CHROMIUM
2011/08/16 22:56:26
Done (and in-lined).
| |
213 it->second->DeleteOutstandingTextures(); | |
214 delete it->second; | |
215 } | |
216 video_decoders_.clear(); | |
180 delete surface_; | 217 delete surface_; |
181 delete context_; | 218 delete context_; |
182 } | 219 } |
183 | 220 |
184 void GLES2DemoInstance::DeleteOutstandingTextures() { | |
185 for (PictureBufferMap::iterator it = buffers_by_id_.begin(); | |
186 it != buffers_by_id_.end(); ++it) { | |
187 DeleteTexture(it->second.texture_id); | |
188 } | |
189 buffers_by_id_.clear(); | |
190 } | |
191 | |
192 void GLES2DemoInstance::DeleteOutstandingBitstreamBuffers() { | |
193 for (BitstreamBufferMap::iterator it = bitstream_buffers_by_id_.begin(); | |
194 it != bitstream_buffers_by_id_.end(); ++it) { | |
195 delete it->second; | |
196 } | |
197 bitstream_buffers_by_id_.clear(); | |
198 } | |
199 | |
200 void GLES2DemoInstance::DidChangeView( | 221 void GLES2DemoInstance::DidChangeView( |
201 const pp::Rect& position, const pp::Rect& clip_ignored) { | 222 const pp::Rect& position, const pp::Rect& clip_ignored) { |
202 if (position.width() == 0 || position.height() == 0) | 223 if (position.width() == 0 || position.height() == 0) |
203 return; | 224 return; |
204 if (position_size_.width()) { | 225 if (position_size_.width()) { |
205 assert(position.size() == position_size_); | 226 assert(position.size() == position_size_); |
206 return; | 227 return; |
207 } | 228 } |
208 position_size_ = position.size(); | 229 position_size_ = position.size(); |
209 | 230 |
210 // Initialize graphics. | 231 // Initialize graphics. |
211 InitGL(); | 232 InitGL(); |
212 InitializeDecoder(); | 233 InitializeDecoders(); |
213 } | 234 } |
214 | 235 |
215 void GLES2DemoInstance::InitializeDecoder() { | 236 void GLES2DemoInstance::InitializeDecoders() { |
216 PP_VideoConfigElement configs = PP_VIDEOATTR_DICTIONARY_TERMINATOR; | 237 PP_VideoConfigElement configs = PP_VIDEOATTR_DICTIONARY_TERMINATOR; |
217 | 238 |
218 assert(!video_decoder_); | 239 assert(video_decoders_.empty()); |
219 video_decoder_ = new pp::VideoDecoder_Dev(*this, *context_, &configs); | 240 for (int i = 0; i < 2; ++i) { |
vrk (LEFT CHROMIUM)
2011/08/16 22:06:21
Instead of 2, make into an enum constant? (like kN
Ami GONE FROM CHROMIUM
2011/08/16 22:56:26
Done.
(I previously hadn't b/c the /2 was still ha
| |
220 assert(!video_decoder_->is_null()); | 241 DecoderState* state = new DecoderState( |
221 | 242 this, new pp::VideoDecoder_Dev(*this, *context_, &configs)); |
222 DecodeNextNALUs(); | 243 assert(!state->decoder_->is_null()); |
vrk (LEFT CHROMIUM)
2011/08/16 22:06:21
If you're going the DecoderClient route, could pro
Ami GONE FROM CHROMIUM
2011/08/16 22:56:26
Done.
| |
244 assert(video_decoders_.insert(std::make_pair( | |
245 state->decoder_->pp_resource(), state)).second); | |
246 state->DecodeNextNALUs(); | |
247 } | |
223 } | 248 } |
224 | 249 |
225 void GLES2DemoInstance::DecoderBitstreamDone( | 250 void GLES2DemoInstance::DecoderState::DecoderBitstreamDone( |
226 int32_t result, int bitstream_buffer_id) { | 251 int32_t result, int bitstream_buffer_id) { |
227 assert(bitstream_ids_at_decoder_.erase(bitstream_buffer_id) == 1); | 252 assert(bitstream_ids_at_decoder_.erase(bitstream_buffer_id) == 1); |
228 BitstreamBufferMap::iterator it = | 253 BitstreamBufferMap::iterator it = |
229 bitstream_buffers_by_id_.find(bitstream_buffer_id); | 254 bitstream_buffers_by_id_.find(bitstream_buffer_id); |
230 assert(it != bitstream_buffers_by_id_.end()); | 255 assert(it != bitstream_buffers_by_id_.end()); |
231 delete it->second; | 256 delete it->second; |
232 bitstream_buffers_by_id_.erase(it); | 257 bitstream_buffers_by_id_.erase(it); |
233 DecodeNextNALUs(); | 258 DecodeNextNALUs(); |
234 } | 259 } |
235 | 260 |
236 void GLES2DemoInstance::DecoderFlushDone(int32_t result) { | 261 void GLES2DemoInstance::DecoderState::DecoderFlushDone(int32_t result) { |
262 assert(result == PP_OK); | |
237 // Check that each bitstream buffer ID we handed to the decoder got handed | 263 // Check that each bitstream buffer ID we handed to the decoder got handed |
238 // back to us. | 264 // back to us. |
239 assert(bitstream_ids_at_decoder_.empty()); | 265 assert(bitstream_ids_at_decoder_.empty()); |
240 delete video_decoder_; | 266 delete decoder_; |
241 video_decoder_ = NULL; | 267 decoder_ = NULL; |
242 } | 268 } |
243 | 269 |
244 static bool LookingAtNAL(const unsigned char* encoded, size_t pos) { | 270 static bool LookingAtNAL(const unsigned char* encoded, size_t pos) { |
245 return pos + 3 < kDataLen && | 271 return pos + 3 < kDataLen && |
246 encoded[pos] == 0 && encoded[pos + 1] == 0 && | 272 encoded[pos] == 0 && encoded[pos + 1] == 0 && |
247 encoded[pos + 2] == 0 && encoded[pos + 3] == 1; | 273 encoded[pos + 2] == 0 && encoded[pos + 3] == 1; |
248 } | 274 } |
249 | 275 |
250 void GLES2DemoInstance::GetNextNALUBoundary( | 276 void GLES2DemoInstance::DecoderState::GetNextNALUBoundary( |
251 size_t start_pos, size_t* end_pos) { | 277 size_t start_pos, size_t* end_pos) { |
252 assert(LookingAtNAL(kData, start_pos)); | 278 assert(LookingAtNAL(kData, start_pos)); |
253 *end_pos = start_pos; | 279 *end_pos = start_pos; |
254 *end_pos += 4; | 280 *end_pos += 4; |
255 while (*end_pos + 3 < kDataLen && | 281 while (*end_pos + 3 < kDataLen && |
256 !LookingAtNAL(kData, *end_pos)) { | 282 !LookingAtNAL(kData, *end_pos)) { |
257 ++*end_pos; | 283 ++*end_pos; |
258 } | 284 } |
259 if (*end_pos + 3 >= kDataLen) { | 285 if (*end_pos + 3 >= kDataLen) { |
260 *end_pos = kDataLen; | 286 *end_pos = kDataLen; |
261 return; | 287 return; |
262 } | 288 } |
263 } | 289 } |
264 | 290 |
265 void GLES2DemoInstance::DecodeNextNALUs() { | 291 void GLES2DemoInstance::DecoderState::DecodeNextNALUs() { |
266 while (encoded_data_next_pos_to_decode_ <= kDataLen && | 292 while (encoded_data_next_pos_to_decode_ <= kDataLen && |
267 bitstream_ids_at_decoder_.size() < kNumConcurrentDecodes) { | 293 bitstream_ids_at_decoder_.size() < kNumConcurrentDecodes) { |
268 DecodeNextNALU(); | 294 DecodeNextNALU(); |
269 } | 295 } |
270 } | 296 } |
271 | 297 |
272 void GLES2DemoInstance::DecodeNextNALU() { | 298 void GLES2DemoInstance::DecoderState::DecodeNextNALU() { |
273 if (encoded_data_next_pos_to_decode_ == kDataLen) { | 299 if (encoded_data_next_pos_to_decode_ == kDataLen) { |
274 ++encoded_data_next_pos_to_decode_; | 300 ++encoded_data_next_pos_to_decode_; |
275 pp::CompletionCallback cb = | 301 pp::CompletionCallback cb = callback_factory_.NewCallback( |
276 callback_factory_.NewCallback(&GLES2DemoInstance::DecoderFlushDone); | 302 &GLES2DemoInstance::DecoderState::DecoderFlushDone); |
277 video_decoder_->Flush(cb); | 303 decoder_->Flush(cb); |
278 return; | 304 return; |
279 } | 305 } |
280 size_t start_pos = encoded_data_next_pos_to_decode_; | 306 size_t start_pos = encoded_data_next_pos_to_decode_; |
281 size_t end_pos; | 307 size_t end_pos; |
282 GetNextNALUBoundary(start_pos, &end_pos); | 308 GetNextNALUBoundary(start_pos, &end_pos); |
283 pp::Buffer_Dev* buffer = new pp::Buffer_Dev(this, end_pos - start_pos); | 309 pp::Buffer_Dev* buffer = new pp::Buffer_Dev(gles2_, end_pos - start_pos); |
284 PP_VideoBitstreamBuffer_Dev bitstream_buffer; | 310 PP_VideoBitstreamBuffer_Dev bitstream_buffer; |
285 int id = ++next_bitstream_buffer_id_; | 311 int id = ++next_bitstream_buffer_id_; |
286 bitstream_buffer.id = id; | 312 bitstream_buffer.id = id; |
287 bitstream_buffer.size = end_pos - start_pos; | 313 bitstream_buffer.size = end_pos - start_pos; |
288 bitstream_buffer.data = buffer->pp_resource(); | 314 bitstream_buffer.data = buffer->pp_resource(); |
289 memcpy(buffer->data(), kData + start_pos, end_pos - start_pos); | 315 memcpy(buffer->data(), kData + start_pos, end_pos - start_pos); |
290 assert(bitstream_buffers_by_id_.insert(std::make_pair(id, buffer)).second); | 316 assert(bitstream_buffers_by_id_.insert(std::make_pair(id, buffer)).second); |
291 | 317 |
292 pp::CompletionCallback cb = | 318 pp::CompletionCallback cb = |
293 callback_factory_.NewCallback( | 319 callback_factory_.NewCallback( |
294 &GLES2DemoInstance::DecoderBitstreamDone, id); | 320 &GLES2DemoInstance::DecoderState::DecoderBitstreamDone, id); |
295 assert(bitstream_ids_at_decoder_.insert(id).second); | 321 assert(bitstream_ids_at_decoder_.insert(id).second); |
296 video_decoder_->Decode(bitstream_buffer, cb); | |
297 encoded_data_next_pos_to_decode_ = end_pos; | 322 encoded_data_next_pos_to_decode_ = end_pos; |
323 decoder_->Decode(bitstream_buffer, cb); | |
298 } | 324 } |
299 | 325 |
300 void GLES2DemoInstance::ProvidePictureBuffers( | 326 void GLES2DemoInstance::ProvidePictureBuffers( |
301 PP_Resource decoder, uint32_t req_num_of_bufs, PP_Size dimensions) { | 327 PP_Resource decoder, uint32_t req_num_of_bufs, PP_Size dimensions) { |
302 assert(decoder == video_decoder_->pp_resource()); | |
303 std::vector<PP_PictureBuffer_Dev> buffers; | 328 std::vector<PP_PictureBuffer_Dev> buffers; |
304 for (uint32_t i = 0; i < req_num_of_bufs; i++) { | 329 DecoderState* state = video_decoders_[decoder]; |
330 assert(state); | |
331 for (uint32_t i = 0; i < req_num_of_bufs; ++i) { | |
vrk (LEFT CHROMIUM)
2011/08/16 22:06:21
Put this behavior in DecoderState object if not to
Ami GONE FROM CHROMIUM
2011/08/16 22:56:26
Done.
| |
305 PP_PictureBuffer_Dev buffer; | 332 PP_PictureBuffer_Dev buffer; |
306 buffer.texture_id = CreateTexture(dimensions.width, dimensions.height); | 333 buffer.texture_id = CreateTexture(dimensions.width, dimensions.height); |
307 int id = ++next_picture_buffer_id_; | 334 int id = ++state->next_picture_buffer_id_; |
308 buffer.id = id; | 335 buffer.id = id; |
309 buffers.push_back(buffer); | 336 buffers.push_back(buffer); |
310 assert(buffers_by_id_.insert(std::make_pair(id, buffer)).second); | 337 assert(state->buffers_by_id_.insert(std::make_pair(id, buffer)).second); |
311 } | 338 } |
312 video_decoder_->AssignPictureBuffers(buffers); | 339 state->decoder_->AssignPictureBuffers(buffers); |
313 } | 340 } |
314 | 341 |
315 void GLES2DemoInstance::DismissPictureBuffer(PP_Resource decoder, | 342 void GLES2DemoInstance::DismissPictureBuffer(PP_Resource decoder, |
316 int32_t picture_buffer_id) { | 343 int32_t picture_buffer_id) { |
317 assert(decoder == video_decoder_->pp_resource()); | 344 DecoderState* state = video_decoders_[decoder]; |
318 PictureBufferMap::iterator it = buffers_by_id_.find(picture_buffer_id); | 345 assert(state); |
319 assert(it != buffers_by_id_.end()); | 346 DecoderState::PictureBufferMap::iterator it = |
vrk (LEFT CHROMIUM)
2011/08/16 22:06:21
Put this behavior in DecoderState object if not to
Ami GONE FROM CHROMIUM
2011/08/16 22:56:26
Done.
| |
347 state->buffers_by_id_.find(picture_buffer_id); | |
348 assert(it != state->buffers_by_id_.end()); | |
320 DeleteTexture(it->second.texture_id); | 349 DeleteTexture(it->second.texture_id); |
321 buffers_by_id_.erase(it); | 350 state->buffers_by_id_.erase(it); |
322 } | 351 } |
323 | 352 |
324 void GLES2DemoInstance::PictureReady(PP_Resource decoder, | 353 void GLES2DemoInstance::PictureReady(PP_Resource decoder, |
325 const PP_Picture_Dev& picture) { | 354 const PP_Picture_Dev& picture) { |
326 assert(decoder == video_decoder_->pp_resource()); | |
327 PaintStart(picture); | |
328 } | |
329 | |
330 void GLES2DemoInstance::PaintStart(const PP_Picture_Dev& picture) { | |
331 if (first_frame_delivered_ticks_ == -1) | 355 if (first_frame_delivered_ticks_ == -1) |
332 assert((first_frame_delivered_ticks_ = core_if_->GetTimeTicks()) != -1); | 356 assert((first_frame_delivered_ticks_ = core_if_->GetTimeTicks()) != -1); |
333 if (is_painting_) { | 357 if (is_painting_) { |
334 pictures_pending_paint_.push_back(picture); | 358 pictures_pending_paint_.push_back(std::make_pair(decoder, picture)); |
335 return; | 359 return; |
336 } | 360 } |
337 PictureBufferMap::iterator it = | 361 DecoderState* state = video_decoders_[decoder]; |
338 buffers_by_id_.find(picture.picture_buffer_id); | 362 assert(state); |
339 assert(it != buffers_by_id_.end()); | 363 DecoderState::PictureBufferMap::iterator it = |
vrk (LEFT CHROMIUM)
2011/08/16 22:06:21
If you're going the DecoderClient route, smush thi
Ami GONE FROM CHROMIUM
2011/08/16 22:56:26
Done.
| |
364 state->buffers_by_id_.find(picture.picture_buffer_id); | |
365 assert(it != state->buffers_by_id_.end()); | |
340 const PP_PictureBuffer_Dev& buffer = it->second; | 366 const PP_PictureBuffer_Dev& buffer = it->second; |
341 assert(!is_painting_); | 367 assert(!is_painting_); |
342 is_painting_ = true; | 368 is_painting_ = true; |
369 int x = 0; | |
370 int y = 0; | |
371 if (state != video_decoders_.begin()->second) { | |
372 x = position_size_.width() / 2; | |
373 y = position_size_.height() / 2; | |
374 } | |
375 gles2_if_->Viewport(context_->pp_resource(), x, y, | |
376 position_size_.width() / 2, position_size_.height() / 2); | |
343 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0); | 377 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0); |
344 gles2_if_->BindTexture( | 378 gles2_if_->BindTexture( |
345 context_->pp_resource(), GL_TEXTURE_2D, buffer.texture_id); | 379 context_->pp_resource(), GL_TEXTURE_2D, buffer.texture_id); |
346 gles2_if_->DrawArrays(context_->pp_resource(), GL_TRIANGLE_STRIP, 0, 4); | 380 gles2_if_->DrawArrays(context_->pp_resource(), GL_TRIANGLE_STRIP, 0, 4); |
347 pp::CompletionCallback cb = | 381 pp::CompletionCallback cb = |
348 callback_factory_.NewCallback( | 382 callback_factory_.NewCallback( |
349 &GLES2DemoInstance::PaintFinished, buffer.id); | 383 &GLES2DemoInstance::PaintFinished, decoder, buffer.id); |
350 last_swap_request_ticks_ = core_if_->GetTimeTicks(); | 384 last_swap_request_ticks_ = core_if_->GetTimeTicks(); |
351 assert(surface_->SwapBuffers(cb) == PP_OK_COMPLETIONPENDING); | 385 assert(surface_->SwapBuffers(cb) == PP_OK_COMPLETIONPENDING); |
352 } | 386 } |
353 | 387 |
354 void GLES2DemoInstance::EndOfStream(PP_Resource decoder) { | 388 void GLES2DemoInstance::EndOfStream(PP_Resource decoder) { |
355 assert(decoder == video_decoder_->pp_resource()); | |
356 } | 389 } |
357 | 390 |
358 void GLES2DemoInstance::NotifyError(PP_Resource decoder, | 391 void GLES2DemoInstance::NotifyError(PP_Resource decoder, |
359 PP_VideoDecodeError_Dev error) { | 392 PP_VideoDecodeError_Dev error) { |
360 assert(decoder == video_decoder_->pp_resource()); | |
361 LogError(this).s() << "Received error: " << error; | 393 LogError(this).s() << "Received error: " << error; |
362 assert(!"Unexpected error; see stderr for details"); | 394 assert(!"Unexpected error; see stderr for details"); |
363 } | 395 } |
364 | 396 |
365 // This object is the global object representing this plugin library as long | 397 // This object is the global object representing this plugin library as long |
366 // as it is loaded. | 398 // as it is loaded. |
367 class GLES2DemoModule : public pp::Module { | 399 class GLES2DemoModule : public pp::Module { |
368 public: | 400 public: |
369 GLES2DemoModule() : pp::Module() {} | 401 GLES2DemoModule() : pp::Module() {} |
370 virtual ~GLES2DemoModule() {} | 402 virtual ~GLES2DemoModule() {} |
(...skipping 14 matching lines...) Expand all Loading... | |
385 int32_t surface_attributes[] = { | 417 int32_t surface_attributes[] = { |
386 PP_GRAPHICS3DATTRIB_WIDTH, position_size_.width(), | 418 PP_GRAPHICS3DATTRIB_WIDTH, position_size_.width(), |
387 PP_GRAPHICS3DATTRIB_HEIGHT, position_size_.height(), | 419 PP_GRAPHICS3DATTRIB_HEIGHT, position_size_.height(), |
388 PP_GRAPHICS3DATTRIB_NONE | 420 PP_GRAPHICS3DATTRIB_NONE |
389 }; | 421 }; |
390 surface_ = new pp::Surface3D_Dev(*this, 0, surface_attributes); | 422 surface_ = new pp::Surface3D_Dev(*this, 0, surface_attributes); |
391 assert(!surface_->is_null()); | 423 assert(!surface_->is_null()); |
392 | 424 |
393 assert(!context_->BindSurfaces(*surface_, *surface_)); | 425 assert(!context_->BindSurfaces(*surface_, *surface_)); |
394 | 426 |
395 // Set viewport window size and clear color bit. | 427 // Clear color bit. |
396 gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT); | 428 gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT); |
397 gles2_if_->Viewport(context_->pp_resource(), 0, 0, | |
398 position_size_.width(), position_size_.height()); | |
399 | 429 |
400 assert(BindGraphics(*surface_)); | 430 assert(BindGraphics(*surface_)); |
401 assertNoGLError(); | 431 assertNoGLError(); |
402 | 432 |
403 CreateGLObjects(); | 433 CreateGLObjects(); |
404 } | 434 } |
405 | 435 |
406 void GLES2DemoInstance::PaintFinished(int32_t result, int picture_buffer_id) { | 436 void GLES2DemoInstance::PaintFinished(int32_t result, PP_Resource decoder, |
437 int picture_buffer_id) { | |
407 swap_ticks_ += core_if_->GetTimeTicks() - last_swap_request_ticks_; | 438 swap_ticks_ += core_if_->GetTimeTicks() - last_swap_request_ticks_; |
408 is_painting_ = false; | 439 is_painting_ = false; |
409 ++num_frames_rendered_; | 440 ++num_frames_rendered_; |
410 if (num_frames_rendered_ % 50 == 0) { | 441 if (num_frames_rendered_ % 50 == 0) { |
411 double elapsed = core_if_->GetTimeTicks() - first_frame_delivered_ticks_; | 442 double elapsed = core_if_->GetTimeTicks() - first_frame_delivered_ticks_; |
412 double fps = (elapsed > 0) ? num_frames_rendered_ / elapsed : 1000; | 443 double fps = (elapsed > 0) ? num_frames_rendered_ / elapsed : 1000; |
413 double ms_per_swap = (swap_ticks_ * 1e3) / num_frames_rendered_; | 444 double ms_per_swap = (swap_ticks_ * 1e3) / num_frames_rendered_; |
414 LogError(this).s() << "Rendered frames: " << num_frames_rendered_ | 445 LogError(this).s() << "Rendered frames: " << num_frames_rendered_ |
415 << ", fps: " << fps << ", with average ms/swap of: " | 446 << ", fps: " << fps << ", with average ms/swap of: " |
416 << ms_per_swap; | 447 << ms_per_swap; |
417 } | 448 } |
418 if (video_decoder_) | 449 DecoderState* state = video_decoders_[decoder]; |
419 video_decoder_->ReusePictureBuffer(picture_buffer_id); | 450 if (state && state->decoder_) |
vrk (LEFT CHROMIUM)
2011/08/16 22:06:21
If you're going the DecoderClient route, here too
Ami GONE FROM CHROMIUM
2011/08/16 22:56:26
Done.
| |
451 state->decoder_->ReusePictureBuffer(picture_buffer_id); | |
420 if (!pictures_pending_paint_.empty()) { | 452 if (!pictures_pending_paint_.empty()) { |
421 PP_Picture_Dev picture = pictures_pending_paint_.front(); | 453 std::pair<PP_Resource, PP_Picture_Dev> decoder_picture = |
454 pictures_pending_paint_.front(); | |
422 pictures_pending_paint_.pop_front(); | 455 pictures_pending_paint_.pop_front(); |
423 PaintStart(picture); | 456 PictureReady(decoder_picture.first, decoder_picture.second); |
424 } | 457 } |
425 } | 458 } |
426 | 459 |
427 GLuint GLES2DemoInstance::CreateTexture(int32_t width, int32_t height) { | 460 GLuint GLES2DemoInstance::CreateTexture(int32_t width, int32_t height) { |
428 GLuint texture_id; | 461 GLuint texture_id; |
429 gles2_if_->GenTextures(context_->pp_resource(), 1, &texture_id); | 462 gles2_if_->GenTextures(context_->pp_resource(), 1, &texture_id); |
430 assertNoGLError(); | 463 assertNoGLError(); |
431 // Assign parameters. | 464 // Assign parameters. |
432 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0); | 465 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0); |
433 gles2_if_->BindTexture( | 466 gles2_if_->BindTexture( |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
529 gles2_if_->DeleteShader(context_->pp_resource(), shader); | 562 gles2_if_->DeleteShader(context_->pp_resource(), shader); |
530 } | 563 } |
531 } // anonymous namespace | 564 } // anonymous namespace |
532 | 565 |
533 namespace pp { | 566 namespace pp { |
534 // Factory function for your specialization of the Module object. | 567 // Factory function for your specialization of the Module object. |
535 Module* CreateModule() { | 568 Module* CreateModule() { |
536 return new GLES2DemoModule(); | 569 return new GLES2DemoModule(); |
537 } | 570 } |
538 } // namespace pp | 571 } // namespace pp |
OLD | NEW |