c2cd7c694cb07876b89eb09750e415c285cb66dc
[platform/framework/web/crosswalk.git] / src / content / common / gpu / media / vaapi_wrapper.h
1 // Copyright 2013 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 // This file contains an implementation of VaapiWrapper, used by
6 // VaapiVideoDecodeAccelerator and VaapiH264Decoder to interface
7 // with libva (VA-API library for hardware video decode).
8
9 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_
10 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_
11
12 #include <vector>
13 #include "base/callback.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/synchronization/lock.h"
16 #include "content/common/content_export.h"
17 #include "content/common/gpu/media/va_surface.h"
18 #include "media/base/video_decoder_config.h"
19 #include "media/base/video_frame.h"
20 #if defined (USE_OZONE)
21 #include "third_party/libva/va/wayland/va_wayland.h"
22 #else
23 #include "third_party/libva/va/va_x11.h"
24 #endif
25 #include "ui/gfx/size.h"
26
27 namespace content {
28
29 // This class handles VA-API calls and ensures proper locking of VA-API calls
30 // to libva, the userspace shim to the HW decoder driver. libva is not
31 // thread-safe, so we have to perform locking ourselves. This class is fully
32 // synchronous and its methods can be called from any thread and may wait on
33 // the va_lock_ while other, concurrent calls run.
34 //
35 // This class is responsible for managing VAAPI connection, contexts and state.
36 // It is also responsible for managing and freeing VABuffers (not VASurfaces),
37 // which are used to queue decode parameters and slice data to the HW decoder,
38 // as well as underlying memory for VASurfaces themselves.
39 class CONTENT_EXPORT VaapiWrapper {
40  public:
41   // |report_error_to_uma_cb| will be called independently from reporting
42   // errors to clients via method return values.
43   static scoped_ptr<VaapiWrapper> Create(
44       media::VideoCodecProfile profile,
45       void* display,
46       const base::Closure& report_error_to_uma_cb);
47
48   ~VaapiWrapper();
49
50   // Create |num_surfaces| backing surfaces in driver for VASurfaces, each
51   // of size |size|. Returns true when successful, with the created IDs in
52   // |va_surfaces| to be managed and later wrapped in VASurfaces.
53   // The client must DestroySurfaces() each time before calling this method
54   // again to free the allocated surfaces first, but is not required to do so
55   // at destruction time, as this will be done automatically from
56   // the destructor.
57   bool CreateSurfaces(gfx::Size size,
58                       size_t num_surfaces,
59                       std::vector<VASurfaceID>* va_surfaces);
60
61   // Free all memory allocated in CreateSurfaces.
62   void DestroySurfaces();
63
64   // Submit parameters or slice data of |va_buffer_type|, copying them from
65   // |buffer| of size |size|, into HW decoder. The data in |buffer| is no
66   // longer needed and can be freed after this method returns.
67   // Data submitted via this method awaits in the HW decoder until
68   // DecodeAndDestroyPendingBuffers is called to execute or
69   // DestroyPendingBuffers is used to cancel a pending decode.
70   bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer);
71
72   // Cancel and destroy all buffers queued to the HW decoder via SubmitBuffer.
73   // Useful when a pending decode is to be cancelled (on reset or error).
74   void DestroyPendingBuffers();
75
76   // Execute decode in hardware into |va_surface_id} and destroy pending
77   // buffers. Return false if SubmitDecode() fails.
78   bool DecodeAndDestroyPendingBuffers(VASurfaceID va_surface_id);
79
80 #if defined (USE_OZONE)
81   bool CreateRGBImage(gfx::Size size, VAImage* image);
82   void DestroyImage(VAImage* image);
83
84   bool MapImage(VAImage* image, void** buffer);
85   void UnmapImage(VAImage* image);
86
87   // Put data from |va_surface_id| into |va_image|, converting/scaling it.
88   bool PutSurfaceIntoImage(VASurfaceID va_surface_id,
89                            VAImage* va_image);
90 #else
91   // Put data from |va_surface_id| into |x_pixmap| of size |size|,
92   // converting/scaling to it.
93   bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id,
94                             Pixmap x_pixmap,
95                             gfx::Size dest_size);
96 #endif
97   // Returns true if the VAAPI version is less than the specified version.
98   bool VAAPIVersionLessThan(int major, int minor);
99
100   // Get a VAImage from a VASurface and map it into memory. The VAImage should
101   // be released using the ReturnVaImage function. Returns true when successful.
102   // This is intended for testing only.
103   bool GetVaImageForTesting(VASurfaceID va_surface_id,
104                             VAImage* image,
105                             void** mem);
106
107   // Release the VAImage (and the associated memory mapping) obtained from
108   // GetVaImage(). This is intended for testing only.
109   void ReturnVaImageForTesting(VAImage* image);
110
111  private:
112   VaapiWrapper();
113
114   bool Initialize(media::VideoCodecProfile profile,
115                   void* display,
116                   const base::Closure& report_error__to_uma_cb);
117   void Deinitialize();
118
119   // Execute decode in hardware and destroy pending buffers. Return false if
120   // vaapi driver refuses to accept parameter or slice buffers submitted
121   // by client or if decode fails in hardware.
122   bool SubmitDecode(VASurfaceID va_surface_id);
123
124   // Attempt to set render mode to "render to texture.". Failure is non-fatal.
125   void TryToSetVADisplayAttributeToLocalGPU();
126
127   // Lazily initialize static data after sandbox is enabled.  Return false on
128   // init failure.
129   static bool PostSandboxInitialization();
130
131   // Libva is not thread safe, so we have to do locking for it ourselves.
132   // This lock is to be taken for the duration of all VA-API calls and for
133   // the entire decode execution sequence in DecodeAndDestroyPendingBuffers().
134   base::Lock va_lock_;
135
136   // Allocated ids for VASurfaces.
137   std::vector<VASurfaceID> va_surface_ids_;
138
139   // The VAAPI version.
140   int major_version_, minor_version_;
141
142   // VA handles.
143   // Both valid after successful Initialize() and until Deinitialize().
144   VADisplay va_display_;
145   VAConfigID va_config_id_;
146   // Created for the current set of va_surface_ids_ in CreateSurfaces() and
147   // valid until DestroySurfaces().
148   VAContextID va_context_id_;
149
150   // Data queued up for HW decoder, to be committed on next HW decode.
151   std::vector<VABufferID> pending_slice_bufs_;
152   std::vector<VABufferID> pending_va_bufs_;
153
154   // Called to report decoding errors to UMA. Errors to clients are reported via
155   // return values from public methods.
156   base::Closure report_error_to_uma_cb_;
157
158   DISALLOW_COPY_AND_ASSIGN(VaapiWrapper);
159 };
160
161 }  // namespace content
162
163 #endif  // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_