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 3 // found in the LICENSE file. 4 5 #include "ppapi/shared_impl/ppb_video_decoder_shared.h" 6 7 #include "base/logging.h" 8 #include "gpu/command_buffer/client/gles2_implementation.h" 9 #include "ppapi/c/pp_errors.h" 10 #include "ppapi/shared_impl/ppb_graphics_3d_shared.h" 11 #include "ppapi/shared_impl/resource_tracker.h" 12 #include "ppapi/thunk/enter.h" 13 14 namespace ppapi { 15 16 PPB_VideoDecoder_Shared::PPB_VideoDecoder_Shared(PP_Instance instance) 17 : Resource(OBJECT_IS_IMPL, instance), 18 graphics_context_(0), 19 gles2_impl_(NULL) { 20 } 21 22 PPB_VideoDecoder_Shared::PPB_VideoDecoder_Shared( 23 const HostResource& host_resource) 24 : Resource(OBJECT_IS_PROXY, host_resource), 25 graphics_context_(0), 26 gles2_impl_(NULL) { 27 } 28 29 PPB_VideoDecoder_Shared::~PPB_VideoDecoder_Shared() { 30 // Destroy() must be called before the object is destroyed. 31 DCHECK(graphics_context_ == 0); 32 } 33 34 thunk::PPB_VideoDecoder_API* PPB_VideoDecoder_Shared::AsPPB_VideoDecoder_API() { 35 return this; 36 } 37 38 void PPB_VideoDecoder_Shared::InitCommon( 39 PP_Resource graphics_context, 40 gpu::gles2::GLES2Implementation* gles2_impl) { 41 DCHECK(graphics_context); 42 DCHECK(!gles2_impl_ && !graphics_context_); 43 gles2_impl_ = gles2_impl; 44 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(graphics_context); 45 graphics_context_ = graphics_context; 46 } 47 48 void PPB_VideoDecoder_Shared::Destroy() { 49 if (graphics_context_) { 50 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource( 51 graphics_context_); 52 graphics_context_ = 0; 53 } 54 gles2_impl_ = NULL; 55 } 56 57 bool PPB_VideoDecoder_Shared::SetFlushCallback( 58 scoped_refptr<TrackedCallback> callback) { 59 if (TrackedCallback::IsPending(flush_callback_)) 60 return false; 61 flush_callback_ = callback; 62 return true; 63 } 64 65 bool PPB_VideoDecoder_Shared::SetResetCallback( 66 scoped_refptr<TrackedCallback> callback) { 67 if (TrackedCallback::IsPending(reset_callback_)) 68 return false; 69 reset_callback_ = callback; 70 return true; 71 } 72 73 bool PPB_VideoDecoder_Shared::SetBitstreamBufferCallback( 74 int32 bitstream_buffer_id, 75 scoped_refptr<TrackedCallback> callback) { 76 return bitstream_buffer_callbacks_.insert( 77 std::make_pair(bitstream_buffer_id, callback)).second; 78 } 79 80 void PPB_VideoDecoder_Shared::RunFlushCallback(int32 result) { 81 flush_callback_->Run(result); 82 } 83 84 void PPB_VideoDecoder_Shared::RunResetCallback(int32 result) { 85 reset_callback_->Run(result); 86 } 87 88 void PPB_VideoDecoder_Shared::RunBitstreamBufferCallback( 89 int32 bitstream_buffer_id, int32 result) { 90 CallbackById::iterator it = 91 bitstream_buffer_callbacks_.find(bitstream_buffer_id); 92 DCHECK(it != bitstream_buffer_callbacks_.end()); 93 scoped_refptr<TrackedCallback> cc = it->second; 94 bitstream_buffer_callbacks_.erase(it); 95 cc->Run(PP_OK); 96 } 97 98 void PPB_VideoDecoder_Shared::FlushCommandBuffer() { 99 // Ensure that graphics_context is still live before using gles2_impl_. 100 // Our "plugin reference" is not enough to keep graphics_context alive if 101 // DidDeleteInstance() has been called. 102 if (PpapiGlobals::Get()->GetResourceTracker()->GetResource( 103 graphics_context_)) { 104 if (gles2_impl_) 105 gles2_impl_->Flush(); 106 } 107 } 108 109 } // namespace ppapi 110