| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "ppapi/c/dev/ppb_console_dev.h" | 14 #include "ppapi/c/dev/ppb_console_dev.h" |
| 15 #include "ppapi/c/pp_errors.h" | 15 #include "ppapi/c/pp_errors.h" |
| 16 #include "ppapi/c/ppb_opengles2.h" | 16 #include "ppapi/c/ppb_opengles2.h" |
| 17 #include "ppapi/cpp/dev/video_decoder_client_dev.h" | 17 #include "ppapi/cpp/dev/video_decoder_client_dev.h" |
| 18 #include "ppapi/cpp/dev/video_decoder_dev.h" | 18 #include "ppapi/cpp/dev/video_decoder_dev.h" |
| 19 #include "ppapi/cpp/graphics_3d.h" | 19 #include "ppapi/cpp/graphics_3d.h" |
| 20 #include "ppapi/cpp/graphics_3d_client.h" | 20 #include "ppapi/cpp/graphics_3d_client.h" |
| 21 #include "ppapi/cpp/instance.h" | 21 #include "ppapi/cpp/instance.h" |
| 22 #include "ppapi/cpp/module.h" | 22 #include "ppapi/cpp/module.h" |
| 23 #include "ppapi/cpp/rect.h" | 23 #include "ppapi/cpp/rect.h" |
| 24 #include "ppapi/cpp/var.h" | 24 #include "ppapi/cpp/var.h" |
| 25 #include "ppapi/examples/video_decode/testdata.h" | 25 #include "ppapi/examples/video_decode/testdata.h" |
| 26 #include "ppapi/lib/gl/include/GLES2/gl2.h" | 26 #include "ppapi/lib/gl/include/GLES2/gl2.h" |
| 27 #include "ppapi/lib/gl/include/GLES2/gl2ext.h" | |
| 28 #include "ppapi/utility/completion_callback_factory.h" | 27 #include "ppapi/utility/completion_callback_factory.h" |
| 29 | 28 |
| 30 // Use assert as a poor-man's CHECK, even in non-debug mode. | 29 // Use assert as a poor-man's CHECK, even in non-debug mode. |
| 31 // Since <assert.h> redefines assert on every inclusion (it doesn't use | 30 // Since <assert.h> redefines assert on every inclusion (it doesn't use |
| 32 // include-guards), make sure this is the last file #include'd in this file. | 31 // include-guards), make sure this is the last file #include'd in this file. |
| 33 #undef NDEBUG | 32 #undef NDEBUG |
| 34 #include <assert.h> | 33 #include <assert.h> |
| 35 | 34 |
| 36 // Assert |context_| isn't holding any GL Errors. Done as a macro instead of a | 35 // Assert |context_| isn't holding any GL Errors. Done as a macro instead of a |
| 37 // function to preserve line number information in the failure message. | 36 // function to preserve line number information in the failure message. |
| 38 #define assertNoGLError() \ | 37 #define assertNoGLError() \ |
| 39 assert(!gles2_if_->GetError(context_->pp_resource())); | 38 assert(!gles2_if_->GetError(context_->pp_resource())); |
| 40 | 39 |
| 41 namespace { | 40 namespace { |
| 42 | 41 |
| 43 struct PictureBufferInfo { | |
| 44 PP_PictureBuffer_Dev buffer; | |
| 45 GLenum texture_target; | |
| 46 }; | |
| 47 | |
| 48 struct Shader { | |
| 49 Shader() : program(0), | |
| 50 texcoord_scale_location(0) {} | |
| 51 | |
| 52 GLuint program; | |
| 53 GLint texcoord_scale_location; | |
| 54 }; | |
| 55 | |
| 56 class VideoDecodeDemoInstance : public pp::Instance, | 42 class VideoDecodeDemoInstance : public pp::Instance, |
| 57 public pp::Graphics3DClient, | 43 public pp::Graphics3DClient, |
| 58 public pp::VideoDecoderClient_Dev { | 44 public pp::VideoDecoderClient_Dev { |
| 59 public: | 45 public: |
| 60 VideoDecodeDemoInstance(PP_Instance instance, pp::Module* module); | 46 VideoDecodeDemoInstance(PP_Instance instance, pp::Module* module); |
| 61 virtual ~VideoDecodeDemoInstance(); | 47 virtual ~VideoDecodeDemoInstance(); |
| 62 | 48 |
| 63 // pp::Instance implementation (see PPP_Instance). | 49 // pp::Instance implementation (see PPP_Instance). |
| 64 virtual void DidChangeView(const pp::Rect& position, | 50 virtual void DidChangeView(const pp::Rect& position, |
| 65 const pp::Rect& clip_ignored); | 51 const pp::Rect& clip_ignored); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 83 |
| 98 void DecodeNextNALUs(); | 84 void DecodeNextNALUs(); |
| 99 | 85 |
| 100 // Per-decoder implementation of part of pp::VideoDecoderClient_Dev. | 86 // Per-decoder implementation of part of pp::VideoDecoderClient_Dev. |
| 101 void ProvidePictureBuffers( | 87 void ProvidePictureBuffers( |
| 102 uint32_t req_num_of_bufs, | 88 uint32_t req_num_of_bufs, |
| 103 PP_Size dimensions, | 89 PP_Size dimensions, |
| 104 uint32_t texture_target); | 90 uint32_t texture_target); |
| 105 void DismissPictureBuffer(int32_t picture_buffer_id); | 91 void DismissPictureBuffer(int32_t picture_buffer_id); |
| 106 | 92 |
| 107 const PictureBufferInfo& GetPictureBufferInfoById(int id); | 93 const PP_PictureBuffer_Dev& GetPictureBufferById(int id); |
| 108 pp::VideoDecoder_Dev* decoder() { return decoder_; } | 94 pp::VideoDecoder_Dev* decoder() { return decoder_; } |
| 109 | 95 |
| 110 private: | 96 private: |
| 111 void DecodeNextNALU(); | 97 void DecodeNextNALU(); |
| 112 static void GetNextNALUBoundary(size_t start_pos, size_t* end_pos); | 98 static void GetNextNALUBoundary(size_t start_pos, size_t* end_pos); |
| 113 void DecoderBitstreamDone(int32_t result, int bitstream_buffer_id); | 99 void DecoderBitstreamDone(int32_t result, int bitstream_buffer_id); |
| 114 void DecoderFlushDone(int32_t result); | 100 void DecoderFlushDone(int32_t result); |
| 115 | 101 |
| 116 VideoDecodeDemoInstance* gles2_; | 102 VideoDecodeDemoInstance* gles2_; |
| 117 pp::VideoDecoder_Dev* decoder_; | 103 pp::VideoDecoder_Dev* decoder_; |
| 118 pp::CompletionCallbackFactory<DecoderClient> callback_factory_; | 104 pp::CompletionCallbackFactory<DecoderClient> callback_factory_; |
| 119 int next_picture_buffer_id_; | 105 int next_picture_buffer_id_; |
| 120 int next_bitstream_buffer_id_; | 106 int next_bitstream_buffer_id_; |
| 121 size_t encoded_data_next_pos_to_decode_; | 107 size_t encoded_data_next_pos_to_decode_; |
| 122 std::set<int> bitstream_ids_at_decoder_; | 108 std::set<int> bitstream_ids_at_decoder_; |
| 123 // Map of texture buffers indexed by buffer id. | 109 // Map of texture buffers indexed by buffer id. |
| 124 typedef std::map<int, PictureBufferInfo> PictureBufferMap; | 110 typedef std::map<int, PP_PictureBuffer_Dev> PictureBufferMap; |
| 125 PictureBufferMap picture_buffers_by_id_; | 111 PictureBufferMap picture_buffers_by_id_; |
| 126 // Map of bitstream buffers indexed by id. | 112 // Map of bitstream buffers indexed by id. |
| 127 typedef std::map<int, pp::Buffer_Dev*> BitstreamBufferMap; | 113 typedef std::map<int, pp::Buffer_Dev*> BitstreamBufferMap; |
| 128 BitstreamBufferMap bitstream_buffers_by_id_; | 114 BitstreamBufferMap bitstream_buffers_by_id_; |
| 129 }; | 115 }; |
| 130 | 116 |
| 131 // Initialize Video Decoders. | 117 // Initialize Video Decoders. |
| 132 void InitializeDecoders(); | 118 void InitializeDecoders(); |
| 133 | 119 |
| 134 // GL-related functions. | 120 // GL-related functions. |
| 135 void InitGL(); | 121 void InitGL(); |
| 136 GLuint CreateTexture(int32_t width, int32_t height, GLenum texture_target); | 122 GLuint CreateTexture(int32_t width, int32_t height); |
| 137 void CreateGLObjects(); | 123 void CreateGLObjects(); |
| 138 Shader CreateProgram(const char* vertex_shader, | |
| 139 const char* fragment_shader); | |
| 140 void CreateShader(GLuint program, GLenum type, const char* source, int size); | 124 void CreateShader(GLuint program, GLenum type, const char* source, int size); |
| 141 void DeleteTexture(GLuint id); | 125 void DeleteTexture(GLuint id); |
| 142 void PaintFinished(int32_t result, PP_Resource decoder, | 126 void PaintFinished(int32_t result, PP_Resource decoder, |
| 143 int picture_buffer_id); | 127 int picture_buffer_id); |
| 144 | 128 |
| 145 // Log an error to the developer console and stderr (though the latter may be | 129 // Log an error to the developer console and stderr (though the latter may be |
| 146 // closed due to sandboxing or blackholed for other reasons) by creating a | 130 // closed due to sandboxing or blackholed for other reasons) by creating a |
| 147 // temporary of this type and streaming to it. Example usage: | 131 // temporary of this type and streaming to it. Example usage: |
| 148 // LogError(this).s() << "Hello world: " << 42; | 132 // LogError(this).s() << "Hello world: " << 42; |
| 149 class LogError { | 133 class LogError { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 177 | 161 |
| 178 // Unowned pointers. | 162 // Unowned pointers. |
| 179 const PPB_Console_Dev* console_if_; | 163 const PPB_Console_Dev* console_if_; |
| 180 const PPB_Core* core_if_; | 164 const PPB_Core* core_if_; |
| 181 const PPB_OpenGLES2* gles2_if_; | 165 const PPB_OpenGLES2* gles2_if_; |
| 182 | 166 |
| 183 // Owned data. | 167 // Owned data. |
| 184 pp::Graphics3D* context_; | 168 pp::Graphics3D* context_; |
| 185 typedef std::map<int, DecoderClient*> Decoders; | 169 typedef std::map<int, DecoderClient*> Decoders; |
| 186 Decoders video_decoders_; | 170 Decoders video_decoders_; |
| 187 | |
| 188 // Shader program to draw GL_TEXTURE_2D target. | |
| 189 Shader shader_2d_; | |
| 190 // Shader program to draw GL_TEXTURE_RECTANGLE_ARB target. | |
| 191 Shader shader_rectangle_arb_; | |
| 192 }; | 171 }; |
| 193 | 172 |
| 194 VideoDecodeDemoInstance::DecoderClient::DecoderClient( | 173 VideoDecodeDemoInstance::DecoderClient::DecoderClient( |
| 195 VideoDecodeDemoInstance* gles2, pp::VideoDecoder_Dev* decoder) | 174 VideoDecodeDemoInstance* gles2, pp::VideoDecoder_Dev* decoder) |
| 196 : gles2_(gles2), decoder_(decoder), callback_factory_(this), | 175 : gles2_(gles2), decoder_(decoder), callback_factory_(this), |
| 197 next_picture_buffer_id_(0), | 176 next_picture_buffer_id_(0), |
| 198 next_bitstream_buffer_id_(0), encoded_data_next_pos_to_decode_(0) { | 177 next_bitstream_buffer_id_(0), encoded_data_next_pos_to_decode_(0) { |
| 199 } | 178 } |
| 200 | 179 |
| 201 VideoDecodeDemoInstance::DecoderClient::~DecoderClient() { | 180 VideoDecodeDemoInstance::DecoderClient::~DecoderClient() { |
| 202 delete decoder_; | 181 delete decoder_; |
| 203 decoder_ = NULL; | 182 decoder_ = NULL; |
| 204 | 183 |
| 205 for (BitstreamBufferMap::iterator it = bitstream_buffers_by_id_.begin(); | 184 for (BitstreamBufferMap::iterator it = bitstream_buffers_by_id_.begin(); |
| 206 it != bitstream_buffers_by_id_.end(); ++it) { | 185 it != bitstream_buffers_by_id_.end(); ++it) { |
| 207 delete it->second; | 186 delete it->second; |
| 208 } | 187 } |
| 209 bitstream_buffers_by_id_.clear(); | 188 bitstream_buffers_by_id_.clear(); |
| 210 | 189 |
| 211 for (PictureBufferMap::iterator it = picture_buffers_by_id_.begin(); | 190 for (PictureBufferMap::iterator it = picture_buffers_by_id_.begin(); |
| 212 it != picture_buffers_by_id_.end(); ++it) { | 191 it != picture_buffers_by_id_.end(); ++it) { |
| 213 gles2_->DeleteTexture(it->second.buffer.texture_id); | 192 gles2_->DeleteTexture(it->second.texture_id); |
| 214 } | 193 } |
| 215 picture_buffers_by_id_.clear(); | 194 picture_buffers_by_id_.clear(); |
| 216 } | 195 } |
| 217 | 196 |
| 218 VideoDecodeDemoInstance::VideoDecodeDemoInstance(PP_Instance instance, | 197 VideoDecodeDemoInstance::VideoDecodeDemoInstance(PP_Instance instance, |
| 219 pp::Module* module) | 198 pp::Module* module) |
| 220 : pp::Instance(instance), pp::Graphics3DClient(this), | 199 : pp::Instance(instance), pp::Graphics3DClient(this), |
| 221 pp::VideoDecoderClient_Dev(this), | 200 pp::VideoDecoderClient_Dev(this), |
| 222 num_frames_rendered_(0), | 201 num_frames_rendered_(0), |
| 223 first_frame_delivered_ticks_(-1), | 202 first_frame_delivered_ticks_(-1), |
| 224 swap_ticks_(0), | 203 swap_ticks_(0), |
| 225 callback_factory_(this), | 204 callback_factory_(this), |
| 226 context_(NULL) { | 205 context_(NULL) { |
| 227 assert((console_if_ = static_cast<const PPB_Console_Dev*>( | 206 assert((console_if_ = static_cast<const PPB_Console_Dev*>( |
| 228 module->GetBrowserInterface(PPB_CONSOLE_DEV_INTERFACE)))); | 207 module->GetBrowserInterface(PPB_CONSOLE_DEV_INTERFACE)))); |
| 229 assert((core_if_ = static_cast<const PPB_Core*>( | 208 assert((core_if_ = static_cast<const PPB_Core*>( |
| 230 module->GetBrowserInterface(PPB_CORE_INTERFACE)))); | 209 module->GetBrowserInterface(PPB_CORE_INTERFACE)))); |
| 231 assert((gles2_if_ = static_cast<const PPB_OpenGLES2*>( | 210 assert((gles2_if_ = static_cast<const PPB_OpenGLES2*>( |
| 232 module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)))); | 211 module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)))); |
| 233 } | 212 } |
| 234 | 213 |
| 235 VideoDecodeDemoInstance::~VideoDecodeDemoInstance() { | 214 VideoDecodeDemoInstance::~VideoDecodeDemoInstance() { |
| 236 if (shader_2d_.program) | |
| 237 gles2_if_->DeleteProgram(context_->pp_resource(), shader_2d_.program); | |
| 238 if (shader_rectangle_arb_.program) { | |
| 239 gles2_if_->DeleteProgram( | |
| 240 context_->pp_resource(), shader_rectangle_arb_.program); | |
| 241 } | |
| 242 | |
| 243 for (Decoders::iterator it = video_decoders_.begin(); | 215 for (Decoders::iterator it = video_decoders_.begin(); |
| 244 it != video_decoders_.end(); ++it) { | 216 it != video_decoders_.end(); ++it) { |
| 245 delete it->second; | 217 delete it->second; |
| 246 } | 218 } |
| 247 video_decoders_.clear(); | 219 video_decoders_.clear(); |
| 248 delete context_; | 220 delete context_; |
| 249 } | 221 } |
| 250 | 222 |
| 251 void VideoDecodeDemoInstance::DidChangeView( | 223 void VideoDecodeDemoInstance::DidChangeView( |
| 252 const pp::Rect& position, const pp::Rect& clip_ignored) { | 224 const pp::Rect& position, const pp::Rect& clip_ignored) { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 uint32_t texture_target) { | 330 uint32_t texture_target) { |
| 359 DecoderClient* client = video_decoders_[decoder]; | 331 DecoderClient* client = video_decoders_[decoder]; |
| 360 assert(client); | 332 assert(client); |
| 361 client->ProvidePictureBuffers(req_num_of_bufs, dimensions, texture_target); | 333 client->ProvidePictureBuffers(req_num_of_bufs, dimensions, texture_target); |
| 362 } | 334 } |
| 363 | 335 |
| 364 void VideoDecodeDemoInstance::DecoderClient::ProvidePictureBuffers( | 336 void VideoDecodeDemoInstance::DecoderClient::ProvidePictureBuffers( |
| 365 uint32_t req_num_of_bufs, | 337 uint32_t req_num_of_bufs, |
| 366 PP_Size dimensions, | 338 PP_Size dimensions, |
| 367 uint32_t texture_target) { | 339 uint32_t texture_target) { |
| 340 // TODO(sail): Add support for GL_TEXTURE_RECTANGLE_ARB. |
| 341 assert(texture_target == GL_TEXTURE_2D); |
| 368 std::vector<PP_PictureBuffer_Dev> buffers; | 342 std::vector<PP_PictureBuffer_Dev> buffers; |
| 369 for (uint32_t i = 0; i < req_num_of_bufs; ++i) { | 343 for (uint32_t i = 0; i < req_num_of_bufs; ++i) { |
| 370 PictureBufferInfo info; | 344 PP_PictureBuffer_Dev buffer; |
| 371 info.buffer.size = dimensions; | 345 buffer.size = dimensions; |
| 372 info.texture_target = texture_target; | 346 buffer.texture_id = |
| 373 info.buffer.texture_id = gles2_->CreateTexture( | 347 gles2_->CreateTexture(dimensions.width, dimensions.height); |
| 374 dimensions.width, dimensions.height, info.texture_target); | |
| 375 int id = ++next_picture_buffer_id_; | 348 int id = ++next_picture_buffer_id_; |
| 376 info.buffer.id = id; | 349 buffer.id = id; |
| 377 buffers.push_back(info.buffer); | 350 buffers.push_back(buffer); |
| 378 assert(picture_buffers_by_id_.insert(std::make_pair(id, info)).second); | 351 assert(picture_buffers_by_id_.insert(std::make_pair(id, buffer)).second); |
| 379 } | 352 } |
| 380 decoder_->AssignPictureBuffers(buffers); | 353 decoder_->AssignPictureBuffers(buffers); |
| 381 } | 354 } |
| 382 | 355 |
| 383 const PictureBufferInfo& | 356 const PP_PictureBuffer_Dev& |
| 384 VideoDecodeDemoInstance::DecoderClient::GetPictureBufferInfoById( | 357 VideoDecodeDemoInstance::DecoderClient::GetPictureBufferById( |
| 385 int id) { | 358 int id) { |
| 386 PictureBufferMap::iterator it = picture_buffers_by_id_.find(id); | 359 PictureBufferMap::iterator it = picture_buffers_by_id_.find(id); |
| 387 assert(it != picture_buffers_by_id_.end()); | 360 assert(it != picture_buffers_by_id_.end()); |
| 388 return it->second; | 361 return it->second; |
| 389 } | 362 } |
| 390 | 363 |
| 391 void VideoDecodeDemoInstance::DismissPictureBuffer(PP_Resource decoder, | 364 void VideoDecodeDemoInstance::DismissPictureBuffer(PP_Resource decoder, |
| 392 int32_t picture_buffer_id) { | 365 int32_t picture_buffer_id) { |
| 393 DecoderClient* client = video_decoders_[decoder]; | 366 DecoderClient* client = video_decoders_[decoder]; |
| 394 assert(client); | 367 assert(client); |
| 395 client->DismissPictureBuffer(picture_buffer_id); | 368 client->DismissPictureBuffer(picture_buffer_id); |
| 396 } | 369 } |
| 397 | 370 |
| 398 void VideoDecodeDemoInstance::DecoderClient::DismissPictureBuffer( | 371 void VideoDecodeDemoInstance::DecoderClient::DismissPictureBuffer( |
| 399 int32_t picture_buffer_id) { | 372 int32_t picture_buffer_id) { |
| 400 gles2_->DeleteTexture(GetPictureBufferInfoById( | 373 gles2_->DeleteTexture(GetPictureBufferById(picture_buffer_id).texture_id); |
| 401 picture_buffer_id).buffer.texture_id); | |
| 402 picture_buffers_by_id_.erase(picture_buffer_id); | 374 picture_buffers_by_id_.erase(picture_buffer_id); |
| 403 } | 375 } |
| 404 | 376 |
| 405 void VideoDecodeDemoInstance::PictureReady(PP_Resource decoder, | 377 void VideoDecodeDemoInstance::PictureReady(PP_Resource decoder, |
| 406 const PP_Picture_Dev& picture) { | 378 const PP_Picture_Dev& picture) { |
| 407 if (first_frame_delivered_ticks_ == -1) | 379 if (first_frame_delivered_ticks_ == -1) |
| 408 assert((first_frame_delivered_ticks_ = core_if_->GetTimeTicks()) != -1); | 380 assert((first_frame_delivered_ticks_ = core_if_->GetTimeTicks()) != -1); |
| 409 if (is_painting_) { | 381 if (is_painting_) { |
| 410 pictures_pending_paint_.push_back(std::make_pair(decoder, picture)); | 382 pictures_pending_paint_.push_back(std::make_pair(decoder, picture)); |
| 411 return; | 383 return; |
| 412 } | 384 } |
| 413 DecoderClient* client = video_decoders_[decoder]; | 385 DecoderClient* client = video_decoders_[decoder]; |
| 414 assert(client); | 386 assert(client); |
| 415 const PictureBufferInfo& info = | 387 const PP_PictureBuffer_Dev& buffer = |
| 416 client->GetPictureBufferInfoById(picture.picture_buffer_id); | 388 client->GetPictureBufferById(picture.picture_buffer_id); |
| 417 assert(!is_painting_); | 389 assert(!is_painting_); |
| 418 is_painting_ = true; | 390 is_painting_ = true; |
| 419 int x = 0; | 391 int x = 0; |
| 420 int y = 0; | 392 int y = 0; |
| 421 if (client != video_decoders_.begin()->second) { | 393 if (client != video_decoders_.begin()->second) { |
| 422 x = plugin_size_.width() / kNumDecoders; | 394 x = plugin_size_.width() / kNumDecoders; |
| 423 y = plugin_size_.height() / kNumDecoders; | 395 y = plugin_size_.height() / kNumDecoders; |
| 424 } | 396 } |
| 425 | 397 |
| 426 if (info.texture_target == GL_TEXTURE_2D) { | |
| 427 gles2_if_->UseProgram(context_->pp_resource(), shader_2d_.program); | |
| 428 gles2_if_->Uniform2f( | |
| 429 context_->pp_resource(), shader_2d_.texcoord_scale_location, 1.0, 1.0); | |
| 430 } else { | |
| 431 assert(info.texture_target == GL_TEXTURE_RECTANGLE_ARB); | |
| 432 gles2_if_->UseProgram( | |
| 433 context_->pp_resource(), shader_rectangle_arb_.program); | |
| 434 gles2_if_->Uniform2f(context_->pp_resource(), | |
| 435 shader_rectangle_arb_.texcoord_scale_location, | |
| 436 info.buffer.size.width, | |
| 437 info.buffer.size.height); | |
| 438 } | |
| 439 | |
| 440 gles2_if_->Viewport(context_->pp_resource(), x, y, | 398 gles2_if_->Viewport(context_->pp_resource(), x, y, |
| 441 plugin_size_.width() / kNumDecoders, | 399 plugin_size_.width() / kNumDecoders, |
| 442 plugin_size_.height() / kNumDecoders); | 400 plugin_size_.height() / kNumDecoders); |
| 443 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0); | 401 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0); |
| 444 gles2_if_->BindTexture( | 402 gles2_if_->BindTexture( |
| 445 context_->pp_resource(), info.texture_target, info.buffer.texture_id); | 403 context_->pp_resource(), GL_TEXTURE_2D, buffer.texture_id); |
| 446 gles2_if_->DrawArrays(context_->pp_resource(), GL_TRIANGLE_STRIP, 0, 4); | 404 gles2_if_->DrawArrays(context_->pp_resource(), GL_TRIANGLE_STRIP, 0, 4); |
| 447 | |
| 448 gles2_if_->UseProgram(context_->pp_resource(), 0); | |
| 449 | |
| 450 pp::CompletionCallback cb = | 405 pp::CompletionCallback cb = |
| 451 callback_factory_.NewCallback( | 406 callback_factory_.NewCallback( |
| 452 &VideoDecodeDemoInstance::PaintFinished, decoder, info.buffer.id); | 407 &VideoDecodeDemoInstance::PaintFinished, decoder, buffer.id); |
| 453 last_swap_request_ticks_ = core_if_->GetTimeTicks(); | 408 last_swap_request_ticks_ = core_if_->GetTimeTicks(); |
| 454 assert(context_->SwapBuffers(cb) == PP_OK_COMPLETIONPENDING); | 409 assert(context_->SwapBuffers(cb) == PP_OK_COMPLETIONPENDING); |
| 455 } | 410 } |
| 456 | 411 |
| 457 void VideoDecodeDemoInstance::NotifyError(PP_Resource decoder, | 412 void VideoDecodeDemoInstance::NotifyError(PP_Resource decoder, |
| 458 PP_VideoDecodeError_Dev error) { | 413 PP_VideoDecodeError_Dev error) { |
| 459 LogError(this).s() << "Received error: " << error; | 414 LogError(this).s() << "Received error: " << error; |
| 460 assert(!"Unexpected error; see stderr for details"); | 415 assert(!"Unexpected error; see stderr for details"); |
| 461 } | 416 } |
| 462 | 417 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 if (client && client->decoder()) | 476 if (client && client->decoder()) |
| 522 client->decoder()->ReusePictureBuffer(picture_buffer_id); | 477 client->decoder()->ReusePictureBuffer(picture_buffer_id); |
| 523 if (!pictures_pending_paint_.empty()) { | 478 if (!pictures_pending_paint_.empty()) { |
| 524 std::pair<PP_Resource, PP_Picture_Dev> decoder_picture = | 479 std::pair<PP_Resource, PP_Picture_Dev> decoder_picture = |
| 525 pictures_pending_paint_.front(); | 480 pictures_pending_paint_.front(); |
| 526 pictures_pending_paint_.pop_front(); | 481 pictures_pending_paint_.pop_front(); |
| 527 PictureReady(decoder_picture.first, decoder_picture.second); | 482 PictureReady(decoder_picture.first, decoder_picture.second); |
| 528 } | 483 } |
| 529 } | 484 } |
| 530 | 485 |
| 531 GLuint VideoDecodeDemoInstance::CreateTexture(int32_t width, | 486 GLuint VideoDecodeDemoInstance::CreateTexture(int32_t width, int32_t height) { |
| 532 int32_t height, | |
| 533 GLenum texture_target) { | |
| 534 GLuint texture_id; | 487 GLuint texture_id; |
| 535 gles2_if_->GenTextures(context_->pp_resource(), 1, &texture_id); | 488 gles2_if_->GenTextures(context_->pp_resource(), 1, &texture_id); |
| 536 assertNoGLError(); | 489 assertNoGLError(); |
| 537 // Assign parameters. | 490 // Assign parameters. |
| 538 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0); | 491 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0); |
| 539 gles2_if_->BindTexture(context_->pp_resource(), texture_target, texture_id); | 492 gles2_if_->BindTexture(context_->pp_resource(), GL_TEXTURE_2D, texture_id); |
| 540 gles2_if_->TexParameteri( | 493 gles2_if_->TexParameteri( |
| 541 context_->pp_resource(), texture_target, GL_TEXTURE_MIN_FILTER, | 494 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 542 GL_NEAREST); | 495 GL_NEAREST); |
| 543 gles2_if_->TexParameteri( | 496 gles2_if_->TexParameteri( |
| 544 context_->pp_resource(), texture_target, GL_TEXTURE_MAG_FILTER, | 497 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, |
| 545 GL_NEAREST); | 498 GL_NEAREST); |
| 546 gles2_if_->TexParameterf( | 499 gles2_if_->TexParameterf( |
| 547 context_->pp_resource(), texture_target, GL_TEXTURE_WRAP_S, | 500 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, |
| 548 GL_CLAMP_TO_EDGE); | 501 GL_CLAMP_TO_EDGE); |
| 549 gles2_if_->TexParameterf( | 502 gles2_if_->TexParameterf( |
| 550 context_->pp_resource(), texture_target, GL_TEXTURE_WRAP_T, | 503 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, |
| 551 GL_CLAMP_TO_EDGE); | 504 GL_CLAMP_TO_EDGE); |
| 552 | 505 |
| 553 if (texture_target == GL_TEXTURE_2D) { | 506 gles2_if_->TexImage2D( |
| 554 gles2_if_->TexImage2D( | 507 context_->pp_resource(), GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, |
| 555 context_->pp_resource(), texture_target, 0, GL_RGBA, width, height, 0, | 508 GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 556 GL_RGBA, GL_UNSIGNED_BYTE, NULL); | |
| 557 } | |
| 558 assertNoGLError(); | 509 assertNoGLError(); |
| 559 return texture_id; | 510 return texture_id; |
| 560 } | 511 } |
| 561 | 512 |
| 562 void VideoDecodeDemoInstance::DeleteTexture(GLuint id) { | 513 void VideoDecodeDemoInstance::DeleteTexture(GLuint id) { |
| 563 gles2_if_->DeleteTextures(context_->pp_resource(), 1, &id); | 514 gles2_if_->DeleteTextures(context_->pp_resource(), 1, &id); |
| 564 } | 515 } |
| 565 | 516 |
| 566 void VideoDecodeDemoInstance::CreateGLObjects() { | 517 void VideoDecodeDemoInstance::CreateGLObjects() { |
| 567 // Code and constants for shader. | 518 // Code and constants for shader. |
| 568 static const char kVertexShader[] = | 519 static const char kVertexShader[] = |
| 569 "varying vec2 v_texCoord; \n" | 520 "varying vec2 v_texCoord; \n" |
| 570 "attribute vec4 a_position; \n" | 521 "attribute vec4 a_position; \n" |
| 571 "attribute vec2 a_texCoord; \n" | 522 "attribute vec2 a_texCoord; \n" |
| 572 "uniform vec2 v_scale; \n" | |
| 573 "void main() \n" | 523 "void main() \n" |
| 574 "{ \n" | 524 "{ \n" |
| 575 " v_texCoord = v_scale * a_texCoord; \n" | 525 " v_texCoord = a_texCoord; \n" |
| 576 " gl_Position = a_position; \n" | 526 " gl_Position = a_position; \n" |
| 577 "}"; | 527 "}"; |
| 578 | 528 |
| 579 static const char kFragmentShader2D[] = | 529 static const char kFragmentShader[] = |
| 580 "precision mediump float; \n" | 530 "precision mediump float; \n" |
| 581 "varying vec2 v_texCoord; \n" | 531 "varying vec2 v_texCoord; \n" |
| 582 "uniform sampler2D s_texture; \n" | 532 "uniform sampler2D s_texture; \n" |
| 583 "void main() \n" | 533 "void main() \n" |
| 584 "{" | 534 "{" |
| 585 " gl_FragColor = texture2D(s_texture, v_texCoord); \n" | 535 " gl_FragColor = texture2D(s_texture, v_texCoord); \n" |
| 586 "}"; | 536 "}"; |
| 587 | 537 |
| 588 static const char kFragmentShaderRectangle[] = | 538 // Create shader program. |
| 589 "#extension GL_ARB_texture_rectangle : require\n" | 539 GLuint program = gles2_if_->CreateProgram(context_->pp_resource()); |
| 590 "precision mediump float; \n" | 540 CreateShader(program, GL_VERTEX_SHADER, kVertexShader, sizeof(kVertexShader)); |
| 591 "varying vec2 v_texCoord; \n" | 541 CreateShader( |
| 592 "uniform sampler2DRect s_texture; \n" | 542 program, GL_FRAGMENT_SHADER, kFragmentShader, sizeof(kFragmentShader)); |
| 593 "void main() \n" | 543 gles2_if_->LinkProgram(context_->pp_resource(), program); |
| 594 "{" | 544 gles2_if_->UseProgram(context_->pp_resource(), program); |
| 595 " gl_FragColor = texture2DRect(s_texture, v_texCoord).rgba; \n" | 545 gles2_if_->DeleteProgram(context_->pp_resource(), program); |
| 596 "}"; | 546 gles2_if_->Uniform1i( |
| 547 context_->pp_resource(), |
| 548 gles2_if_->GetUniformLocation( |
| 549 context_->pp_resource(), program, "s_texture"), 0); |
| 550 assertNoGLError(); |
| 597 | 551 |
| 598 // Assign vertex positions and texture coordinates to buffers for use in | 552 // Assign vertex positions and texture coordinates to buffers for use in |
| 599 // shader program. | 553 // shader program. |
| 600 static const float kVertices[] = { | 554 static const float kVertices[] = { |
| 601 -1, 1, -1, -1, 1, 1, 1, -1, // Position coordinates. | 555 -1, 1, -1, -1, 1, 1, 1, -1, // Position coordinates. |
| 602 0, 1, 0, 0, 1, 1, 1, 0, // Texture coordinates. | 556 0, 1, 0, 0, 1, 1, 1, 0, // Texture coordinates. |
| 603 }; | 557 }; |
| 604 | 558 |
| 605 GLuint buffer; | 559 GLuint buffer; |
| 606 gles2_if_->GenBuffers(context_->pp_resource(), 1, &buffer); | 560 gles2_if_->GenBuffers(context_->pp_resource(), 1, &buffer); |
| 607 gles2_if_->BindBuffer(context_->pp_resource(), GL_ARRAY_BUFFER, buffer); | 561 gles2_if_->BindBuffer(context_->pp_resource(), GL_ARRAY_BUFFER, buffer); |
| 608 | |
| 609 gles2_if_->BufferData(context_->pp_resource(), GL_ARRAY_BUFFER, | 562 gles2_if_->BufferData(context_->pp_resource(), GL_ARRAY_BUFFER, |
| 610 sizeof(kVertices), kVertices, GL_STATIC_DRAW); | 563 sizeof(kVertices), kVertices, GL_STATIC_DRAW); |
| 611 assertNoGLError(); | 564 assertNoGLError(); |
| 612 | 565 GLint pos_location = gles2_if_->GetAttribLocation( |
| 613 shader_2d_ = CreateProgram(kVertexShader, kFragmentShader2D); | 566 context_->pp_resource(), program, "a_position"); |
| 614 shader_rectangle_arb_ = | 567 GLint tc_location = gles2_if_->GetAttribLocation( |
| 615 CreateProgram(kVertexShader, kFragmentShaderRectangle); | 568 context_->pp_resource(), program, "a_texCoord"); |
| 616 } | |
| 617 | |
| 618 Shader VideoDecodeDemoInstance::CreateProgram(const char* vertex_shader, | |
| 619 const char* fragment_shader) { | |
| 620 Shader shader; | |
| 621 | |
| 622 // Create shader program. | |
| 623 shader.program = gles2_if_->CreateProgram(context_->pp_resource()); | |
| 624 CreateShader(shader.program, GL_VERTEX_SHADER, vertex_shader, | |
| 625 strlen(vertex_shader)); | |
| 626 CreateShader(shader.program, GL_FRAGMENT_SHADER, fragment_shader, | |
| 627 strlen(fragment_shader)); | |
| 628 gles2_if_->LinkProgram(context_->pp_resource(), shader.program); | |
| 629 gles2_if_->UseProgram(context_->pp_resource(), shader.program); | |
| 630 gles2_if_->Uniform1i( | |
| 631 context_->pp_resource(), | |
| 632 gles2_if_->GetUniformLocation( | |
| 633 context_->pp_resource(), shader.program, "s_texture"), 0); | |
| 634 assertNoGLError(); | 569 assertNoGLError(); |
| 635 | |
| 636 shader.texcoord_scale_location = gles2_if_->GetUniformLocation( | |
| 637 context_->pp_resource(), shader.program, "v_scale"); | |
| 638 | |
| 639 GLint pos_location = gles2_if_->GetAttribLocation( | |
| 640 context_->pp_resource(), shader.program, "a_position"); | |
| 641 GLint tc_location = gles2_if_->GetAttribLocation( | |
| 642 context_->pp_resource(), shader.program, "a_texCoord"); | |
| 643 assertNoGLError(); | |
| 644 | |
| 645 gles2_if_->EnableVertexAttribArray(context_->pp_resource(), pos_location); | 570 gles2_if_->EnableVertexAttribArray(context_->pp_resource(), pos_location); |
| 646 gles2_if_->VertexAttribPointer(context_->pp_resource(), pos_location, 2, | 571 gles2_if_->VertexAttribPointer(context_->pp_resource(), pos_location, 2, |
| 647 GL_FLOAT, GL_FALSE, 0, 0); | 572 GL_FLOAT, GL_FALSE, 0, 0); |
| 648 gles2_if_->EnableVertexAttribArray(context_->pp_resource(), tc_location); | 573 gles2_if_->EnableVertexAttribArray(context_->pp_resource(), tc_location); |
| 649 gles2_if_->VertexAttribPointer( | 574 gles2_if_->VertexAttribPointer( |
| 650 context_->pp_resource(), tc_location, 2, GL_FLOAT, GL_FALSE, 0, | 575 context_->pp_resource(), tc_location, 2, GL_FLOAT, GL_FALSE, 0, |
| 651 static_cast<float*>(0) + 8); // Skip position coordinates. | 576 static_cast<float*>(0) + 8); // Skip position coordinates. |
| 652 | |
| 653 gles2_if_->UseProgram(context_->pp_resource(), 0); | |
| 654 assertNoGLError(); | 577 assertNoGLError(); |
| 655 return shader; | |
| 656 } | 578 } |
| 657 | 579 |
| 658 void VideoDecodeDemoInstance::CreateShader( | 580 void VideoDecodeDemoInstance::CreateShader( |
| 659 GLuint program, GLenum type, const char* source, int size) { | 581 GLuint program, GLenum type, const char* source, int size) { |
| 660 GLuint shader = gles2_if_->CreateShader(context_->pp_resource(), type); | 582 GLuint shader = gles2_if_->CreateShader(context_->pp_resource(), type); |
| 661 gles2_if_->ShaderSource(context_->pp_resource(), shader, 1, &source, &size); | 583 gles2_if_->ShaderSource(context_->pp_resource(), shader, 1, &source, &size); |
| 662 gles2_if_->CompileShader(context_->pp_resource(), shader); | 584 gles2_if_->CompileShader(context_->pp_resource(), shader); |
| 663 gles2_if_->AttachShader(context_->pp_resource(), program, shader); | 585 gles2_if_->AttachShader(context_->pp_resource(), program, shader); |
| 664 gles2_if_->DeleteShader(context_->pp_resource(), shader); | 586 gles2_if_->DeleteShader(context_->pp_resource(), shader); |
| 665 } | 587 } |
| 666 } // anonymous namespace | 588 } // anonymous namespace |
| 667 | 589 |
| 668 namespace pp { | 590 namespace pp { |
| 669 // Factory function for your specialization of the Module object. | 591 // Factory function for your specialization of the Module object. |
| 670 Module* CreateModule() { | 592 Module* CreateModule() { |
| 671 return new VideoDecodeDemoModule(); | 593 return new VideoDecodeDemoModule(); |
| 672 } | 594 } |
| 673 } // namespace pp | 595 } // namespace pp |
| OLD | NEW |