Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/AllocationPolicy.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=2 sw=2 sts=2 et cindent: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef AllocationPolicy_h_
8
#define AllocationPolicy_h_
9
10
#include "MediaInfo.h"
11
#include "PlatformDecoderModule.h"
12
#include "TimeUnits.h"
13
#include "mozilla/MozPromise.h"
14
#include "mozilla/StaticMutex.h"
15
#include "mozilla/ReentrantMonitor.h"
16
#include <queue>
17
18
namespace mozilla {
19
20
/**
21
 * This is a singleton which controls the number of decoders that can be
22
 * created concurrently. Before calling PDMFactory::CreateDecoder(), Alloc()
23
 * must be called to get a token object as a permission to create a decoder.
24
 * The token should stay alive until Shutdown() is called on the decoder.
25
 * The destructor of the token will restore the decoder count so it is available
26
 * for next calls of Alloc().
27
 */
28
class GlobalAllocPolicy
29
{
30
public:
31
  class Token
32
  {
33
    NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Token)
34
  protected:
35
0
    virtual ~Token() {}
36
  };
37
38
  using Promise = MozPromise<RefPtr<Token>, bool, true>;
39
40
  // Acquire a token for decoder creation. Thread-safe.
41
  RefPtr<Promise> Alloc();
42
43
  // Called by ClearOnShutdown() to delete the singleton.
44
  void operator=(decltype(nullptr));
45
46
  // Get the singleton for the given track type. Thread-safe.
47
  static GlobalAllocPolicy& Instance(TrackInfo::TrackType aTrack);
48
49
private:
50
  class AutoDeallocToken;
51
  using PromisePrivate = Promise::Private;
52
  GlobalAllocPolicy();
53
  ~GlobalAllocPolicy();
54
  // Called by the destructor of TokenImpl to restore the decoder limit.
55
  void Dealloc();
56
  // Decrement the decoder limit and resolve a promise if available.
57
  void ResolvePromise(ReentrantMonitorAutoEnter& aProofOfLock);
58
59
  // Protect access to Instance().
60
  static StaticMutex sMutex;
61
62
  ReentrantMonitor mMonitor;
63
  // The number of decoders available for creation.
64
  int mDecoderLimit;
65
  // Requests to acquire tokens.
66
  std::queue<RefPtr<PromisePrivate>> mPromises;
67
};
68
69
class AllocationWrapper : public MediaDataDecoder
70
{
71
  using Token = GlobalAllocPolicy::Token;
72
73
public:
74
  AllocationWrapper(already_AddRefed<MediaDataDecoder> aDecoder,
75
                    already_AddRefed<Token> aToken);
76
  ~AllocationWrapper();
77
78
  RefPtr<InitPromise> Init() override { return mDecoder->Init(); }
79
  RefPtr<DecodePromise> Decode(MediaRawData* aSample) override
80
  {
81
    return mDecoder->Decode(aSample);
82
  }
83
  RefPtr<DecodePromise> Drain() override { return mDecoder->Drain(); }
84
  RefPtr<FlushPromise> Flush() override { return mDecoder->Flush(); }
85
  bool IsHardwareAccelerated(nsACString& aFailureReason) const override
86
  {
87
    return mDecoder->IsHardwareAccelerated(aFailureReason);
88
  }
89
  nsCString GetDescriptionName() const override
90
  {
91
    return mDecoder->GetDescriptionName();
92
  }
93
  void SetSeekThreshold(const media::TimeUnit& aTime) override
94
  {
95
    mDecoder->SetSeekThreshold(aTime);
96
  }
97
  bool SupportDecoderRecycling() const override
98
  {
99
    return mDecoder->SupportDecoderRecycling();
100
  }
101
  RefPtr<ShutdownPromise> Shutdown() override;
102
103
  typedef MozPromise<RefPtr<MediaDataDecoder>,
104
                     MediaResult,
105
                     /* IsExclusive = */ true>
106
    AllocateDecoderPromise;
107
  // Will create a decoder has soon as one can be created according to the
108
  // GlobalAllocPolicy.
109
  // Warning: all aParams members must be valid until the promise has been
110
  // resolved, as some contains raw pointers to objects.
111
  static RefPtr<AllocateDecoderPromise> CreateDecoder(
112
    const CreateDecoderParams& aParams);
113
114
private:
115
  RefPtr<MediaDataDecoder> mDecoder;
116
  RefPtr<Token> mToken;
117
};
118
119
} // namespace mozilla
120
121
#endif