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