1 // Copyright 2017 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 "media/gpu/windows/d3d11_video_decoder_impl.h"
6 
7 #include "base/bind.h"
8 #include "gpu/command_buffer/common/sync_token.h"
9 #include "gpu/command_buffer/service/scheduler.h"
10 #include "gpu/ipc/service/gpu_channel.h"
11 #include "media/base/bind_to_current_loop.h"
12 #include "media/base/media_log.h"
13 #include "media/gpu/windows/d3d11_picture_buffer.h"
14 
15 namespace media {
16 
D3D11VideoDecoderImpl(std::unique_ptr<MediaLog> media_log,base::RepeatingCallback<scoped_refptr<CommandBufferHelper> ()> get_helper_cb)17 D3D11VideoDecoderImpl::D3D11VideoDecoderImpl(
18     std::unique_ptr<MediaLog> media_log,
19     base::RepeatingCallback<scoped_refptr<CommandBufferHelper>()> get_helper_cb)
20     : media_log_(std::move(media_log)),
21       get_helper_cb_(std::move(get_helper_cb)) {
22   // May be called from any thread.
23 }
24 
~D3D11VideoDecoderImpl()25 D3D11VideoDecoderImpl::~D3D11VideoDecoderImpl() {
26   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
27 }
28 
Initialize(InitCB init_cb)29 void D3D11VideoDecoderImpl::Initialize(InitCB init_cb) {
30   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
31 
32   // If have a helper, then we're as initialized as we need to be.
33   if (helper_) {
34     std::move(init_cb).Run(true, release_mailbox_cb_);
35     return;
36   }
37   helper_ = get_helper_cb_.Run();
38 
39   // Get the stub, register, and generally do stuff.
40   if (!helper_ || !helper_->MakeContextCurrent()) {
41     const char* reason = "Failed to make context current.";
42     DLOG(ERROR) << reason;
43     if (media_log_)
44       MEDIA_LOG(ERROR, media_log_) << reason;
45 
46     std::move(init_cb).Run(false, ReleaseMailboxCB());
47     return;
48   }
49 
50   release_mailbox_cb_ = BindToCurrentLoop(base::BindRepeating(
51       &D3D11VideoDecoderImpl::OnMailboxReleased, GetWeakPtr()));
52 
53   std::move(init_cb).Run(true, release_mailbox_cb_);
54 }
55 
OnMailboxReleased(base::OnceClosure wait_complete_cb,const gpu::SyncToken & sync_token)56 void D3D11VideoDecoderImpl::OnMailboxReleased(
57     base::OnceClosure wait_complete_cb,
58     const gpu::SyncToken& sync_token) {
59   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
60 
61   if (!helper_) {
62     std::move(wait_complete_cb).Run();
63     return;
64   }
65 
66   helper_->WaitForSyncToken(
67       sync_token, base::BindOnce(&D3D11VideoDecoderImpl::OnSyncTokenReleased,
68                                  GetWeakPtr(), std::move(wait_complete_cb)));
69 }
70 
OnSyncTokenReleased(base::OnceClosure wait_complete_cb)71 void D3D11VideoDecoderImpl::OnSyncTokenReleased(
72     base::OnceClosure wait_complete_cb) {
73   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
74 
75   std::move(wait_complete_cb).Run();
76 }
77 
GetWeakPtr()78 base::WeakPtr<D3D11VideoDecoderImpl> D3D11VideoDecoderImpl::GetWeakPtr() {
79   // May be called from any thread.
80   return weak_factory_.GetWeakPtr();
81 }
82 
83 }  // namespace media
84