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