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.
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).
9 #ifndef OZONE_MEDIA_VAAPI_WRAPPER_H_
10 #define OZONE_MEDIA_VAAPI_WRAPPER_H_
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 "media/base/video_decoder_config.h"
18 #include "media/base/video_frame.h"
19 #include "third_party/libva/va/wayland/va_wayland.h"
20 #include "ui/gfx/size.h"
21 #include "va_surface.h"
25 // This class handles VA-API calls and ensures proper locking of VA-API calls
26 // to libva, the userspace shim to the HW decoder driver. libva is not
27 // thread-safe, so we have to perform locking ourselves. This class is fully
28 // synchronous and its methods can be called from any thread and may wait on
29 // the va_lock_ while other, concurrent calls run.
31 // This class is responsible for managing VAAPI connection, contexts and state.
32 // It is also responsible for managing and freeing VABuffers (not VASurfaces),
33 // which are used to queue decode parameters and slice data to the HW decoder,
34 // as well as underlying memory for VASurfaces themselves.
35 class CONTENT_EXPORT VaapiWrapper {
37 // |report_error_to_uma_cb| will be called independently from reporting
38 // errors to clients via method return values.
39 static scoped_ptr<VaapiWrapper> Create(
40 media::VideoCodecProfile profile,
42 const base::Closure& report_error_to_uma_cb);
46 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each
47 // of size |size|. Returns true when successful, with the created IDs in
48 // |va_surfaces| to be managed and later wrapped in VASurfaces.
49 // The client must DestroySurfaces() each time before calling this method
50 // again to free the allocated surfaces first, but is not required to do so
51 // at destruction time, as this will be done automatically from
53 bool CreateSurfaces(gfx::Size size,
55 std::vector<VASurfaceID>* va_surfaces);
57 // Free all memory allocated in CreateSurfaces.
58 void DestroySurfaces();
60 // Submit parameters or slice data of |va_buffer_type|, copying them from
61 // |buffer| of size |size|, into HW decoder. The data in |buffer| is no
62 // longer needed and can be freed after this method returns.
63 // Data submitted via this method awaits in the HW decoder until
64 // DecodeAndDestroyPendingBuffers is called to execute or
65 // DestroyPendingBuffers is used to cancel a pending decode.
66 bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer);
68 // Cancel and destroy all buffers queued to the HW decoder via SubmitBuffer.
69 // Useful when a pending decode is to be cancelled (on reset or error).
70 void DestroyPendingBuffers();
72 // Execute decode in hardware into |va_surface_id} and destroy pending
73 // buffers. Return false if SubmitDecode() fails.
74 bool DecodeAndDestroyPendingBuffers(VASurfaceID va_surface_id);
76 bool CreateRGBImage(gfx::Size size, VAImage* image);
77 void DestroyImage(VAImage* image);
79 bool MapImage(VAImage* image, void** buffer);
80 void UnmapImage(VAImage* image);
82 // Put data from |va_surface_id| into |va_image|, converting/scaling it.
83 bool PutSurfaceIntoImage(VASurfaceID va_surface_id,
85 // Returns true if the VAAPI version is less than the specified version.
86 bool VAAPIVersionLessThan(int major, int minor);
88 // Get a VAImage from a VASurface and map it into memory. The VAImage should
89 // be released using the ReturnVaImage function. Returns true when successful.
90 // This is intended for testing only.
91 bool GetVaImageForTesting(VASurfaceID va_surface_id,
95 // Release the VAImage (and the associated memory mapping) obtained from
96 // GetVaImage(). This is intended for testing only.
97 void ReturnVaImageForTesting(VAImage* image);
102 bool Initialize(media::VideoCodecProfile profile,
104 const base::Closure& report_error__to_uma_cb);
107 // Execute decode in hardware and destroy pending buffers. Return false if
108 // vaapi driver refuses to accept parameter or slice buffers submitted
109 // by client or if decode fails in hardware.
110 bool SubmitDecode(VASurfaceID va_surface_id);
112 // Attempt to set render mode to "render to texture.". Failure is non-fatal.
113 void TryToSetVADisplayAttributeToLocalGPU();
115 // Lazily initialize static data after sandbox is enabled. Return false on
117 static bool PostSandboxInitialization();
119 // Libva is not thread safe, so we have to do locking for it ourselves.
120 // This lock is to be taken for the duration of all VA-API calls and for
121 // the entire decode execution sequence in DecodeAndDestroyPendingBuffers().
124 // Allocated ids for VASurfaces.
125 std::vector<VASurfaceID> va_surface_ids_;
127 // The VAAPI version.
128 int major_version_, minor_version_;
131 // Both valid after successful Initialize() and until Deinitialize().
132 VADisplay va_display_;
133 VAConfigID va_config_id_;
134 // Created for the current set of va_surface_ids_ in CreateSurfaces() and
135 // valid until DestroySurfaces().
136 VAContextID va_context_id_;
138 // Data queued up for HW decoder, to be committed on next HW decode.
139 std::vector<VABufferID> pending_slice_bufs_;
140 std::vector<VABufferID> pending_va_bufs_;
142 // Called to report decoding errors to UMA. Errors to clients are reported via
143 // return values from public methods.
144 base::Closure report_error_to_uma_cb_;
146 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper);
151 #endif // OZONE_MEDIA_VAAPI_WRAPPER_H_