OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 "base/callback.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/synchronization/lock.h" |
| 15 #include "content/common/gpu/media/va_surface.h" |
| 16 #include "media/base/video_decoder_config.h" |
| 17 #include "third_party/libva/va/va.h" |
| 18 #include "third_party/libva/va/va_x11.h" |
| 19 #include "ui/gfx/size.h" |
| 20 |
| 21 namespace content { |
| 22 |
| 23 // This class handles VA-API calls and ensures proper locking of VA-API calls |
| 24 // to libva, the userspace shim to the HW decoder driver. libva is not |
| 25 // thread-safe, so we have to perform locking ourselves. This class is fully |
| 26 // synchronous and its methods can be called from any thread and may wait on |
| 27 // the va_lock_ while other, concurrent calls run. |
| 28 // |
| 29 // This class is responsible for managing VAAPI connection, contexts and state. |
| 30 // It is also responsible for managing and freeing VABuffers (not VASurfaces), |
| 31 // which are used to queue decode parameters and slice data to the HW decoder, |
| 32 // as well as underlying memory for VASurfaces themselves. |
| 33 class VaapiWrapper { |
| 34 public: |
| 35 // |report_error_to_uma_cb| will be called independently from reporting |
| 36 // errors to clients via method return values. |
| 37 static scoped_ptr<VaapiWrapper> Create( |
| 38 media::VideoCodecProfile profile, |
| 39 Display* x_display, |
| 40 const base::Closure& report_error_to_uma_cb); |
| 41 |
| 42 ~VaapiWrapper(); |
| 43 |
| 44 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each |
| 45 // of size |size|. Returns true when successful, with the created IDs in |
| 46 // |va_surfaces| to be managed and later wrapped in VASurfaces. |
| 47 // The client must DestroySurfaces() each time before calling this method |
| 48 // again to free the allocated surfaces first, but is not required to do so |
| 49 // at destruction time, as this will be done automatically from |
| 50 // the destructor. |
| 51 bool CreateSurfaces(gfx::Size size, |
| 52 size_t num_surfaces, |
| 53 std::vector<VASurfaceID>* va_surfaces); |
| 54 |
| 55 // Free all memory allocated in CreateSurfaces. |
| 56 void DestroySurfaces(); |
| 57 |
| 58 // Submit parameters or slice data of |va_buffer_type|, copying them from |
| 59 // |buffer| of size |size|, into HW decoder. The data in |buffer| is no |
| 60 // longer needed and can be freed after this method returns. |
| 61 // Data submitted via this method awaits in the HW decoder until |
| 62 // DecodeAndDestroyPendingBuffers is called to execute or |
| 63 // DestroyPendingBuffers is used to cancel a pending decode. |
| 64 bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer); |
| 65 |
| 66 // Cancel and destroy all buffers queued to the HW decoder via SubmitBuffer. |
| 67 // Useful when a pending decode is to be cancelled (on reset or error). |
| 68 void DestroyPendingBuffers(); |
| 69 |
| 70 // Execute decode in hardware into |va_surface_id} and destroy pending |
| 71 // buffers. Return false if SubmitDecode() fails. |
| 72 bool DecodeAndDestroyPendingBuffers(VASurfaceID va_surface_id); |
| 73 |
| 74 // Put data from |va_surface_id| into |x_pixmap| of size |size|, |
| 75 // converting/scaling to it. |
| 76 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id, |
| 77 Pixmap x_pixmap, |
| 78 gfx::Size dest_size); |
| 79 |
| 80 // Do any necessary initialization before the sandbox is enabled. |
| 81 static void PreSandboxInitialization(); |
| 82 |
| 83 private: |
| 84 VaapiWrapper(); |
| 85 |
| 86 bool Initialize(media::VideoCodecProfile profile, |
| 87 Display* x_display, |
| 88 const base::Closure& report_error__to_uma_cb); |
| 89 void Deinitialize(); |
| 90 |
| 91 // Execute decode in hardware and destroy pending buffers. Return false if |
| 92 // vaapi driver refuses to accept parameter or slice buffers submitted |
| 93 // by client or if decode fails in hardware. |
| 94 bool SubmitDecode(VASurfaceID va_surface_id); |
| 95 |
| 96 // Lazily initialize static data after sandbox is enabled. Return false on |
| 97 // init failure. |
| 98 static bool PostSandboxInitialization(); |
| 99 |
| 100 // Libva is not thread safe, so we have to do locking for it ourselves. |
| 101 // This lock is to be taken for the duration of all VA-API calls and for |
| 102 // the entire decode execution sequence in DecodeAndDestroyPendingBuffers(). |
| 103 base::Lock va_lock_; |
| 104 |
| 105 // Allocated ids for VASurfaces. |
| 106 std::vector<VASurfaceID> va_surface_ids_; |
| 107 |
| 108 // VA handles. |
| 109 // Both valid after successful Initialize() and until Deinitialize(). |
| 110 VADisplay va_display_; |
| 111 VAConfigID va_config_id_; |
| 112 // Created for the current set of va_surface_ids_ in CreateSurfaces() and |
| 113 // valid until DestroySurfaces(). |
| 114 VAContextID va_context_id_; |
| 115 |
| 116 // Data queued up for HW decoder, to be committed on next HW decode. |
| 117 std::vector<VABufferID> pending_slice_bufs_; |
| 118 std::vector<VABufferID> pending_va_bufs_; |
| 119 |
| 120 // Called to report decoding errors to UMA. Errors to clients are reported via |
| 121 // return values from public methods. |
| 122 base::Closure report_error_to_uma_cb_; |
| 123 |
| 124 // Has static initialization of pre-sandbox components completed successfully? |
| 125 static bool pre_sandbox_init_done_; |
| 126 |
| 127 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); |
| 128 }; |
| 129 |
| 130 } // namespace content |
| 131 |
| 132 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
OLD | NEW |