ppb_video_decoder_shared.cc revision 010d83a9304c5a91596085d917d248abff47903a
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_Dev_API*
33PPB_VideoDecoder_Shared::AsPPB_VideoDecoder_Dev_API() {
34  return this;
35}
36
37void PPB_VideoDecoder_Shared::InitCommon(
38    PP_Resource graphics_context,
39    gpu::gles2::GLES2Implementation* gles2_impl) {
40  DCHECK(graphics_context);
41  DCHECK(!gles2_impl_ && !graphics_context_);
42  gles2_impl_ = gles2_impl;
43  PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(graphics_context);
44  graphics_context_ = graphics_context;
45}
46
47void PPB_VideoDecoder_Shared::Destroy() {
48  if (graphics_context_) {
49    PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(
50        graphics_context_);
51    graphics_context_ = 0;
52  }
53  gles2_impl_ = NULL;
54}
55
56bool PPB_VideoDecoder_Shared::SetFlushCallback(
57    scoped_refptr<TrackedCallback> callback) {
58  if (TrackedCallback::IsPending(flush_callback_))
59    return false;
60  flush_callback_ = callback;
61  return true;
62}
63
64bool PPB_VideoDecoder_Shared::SetResetCallback(
65    scoped_refptr<TrackedCallback> callback) {
66  if (TrackedCallback::IsPending(reset_callback_))
67    return false;
68  reset_callback_ = callback;
69  return true;
70}
71
72bool PPB_VideoDecoder_Shared::SetBitstreamBufferCallback(
73    int32 bitstream_buffer_id,
74    scoped_refptr<TrackedCallback> callback) {
75  return bitstream_buffer_callbacks_.insert(std::make_pair(bitstream_buffer_id,
76                                                           callback)).second;
77}
78
79void PPB_VideoDecoder_Shared::RunFlushCallback(int32 result) {
80  flush_callback_->Run(result);
81}
82
83void PPB_VideoDecoder_Shared::RunResetCallback(int32 result) {
84  reset_callback_->Run(result);
85}
86
87void PPB_VideoDecoder_Shared::RunBitstreamBufferCallback(
88    int32 bitstream_buffer_id,
89    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
98void 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