15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This file defines the V4L2Device interface which is used by the
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// V4L2DecodeAccelerator class to delegate/pass the device specific
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// handling of any of the functionalities.
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef CONTENT_COMMON_GPU_MEDIA_V4L2_VIDEO_DEVICE_H_
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define CONTENT_COMMON_GPU_MEDIA_V4L2_VIDEO_DEVICE_H_
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
12010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "media/base/video_decoder_config.h"
13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "media/base/video_frame.h"
14e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "ui/gfx/size.h"
15e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "ui/gl/gl_bindings.h"
16e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace content {
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class V4L2Device {
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
21010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Utility format conversion functions
22010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  static media::VideoFrame::Format V4L2PixFmtToVideoFrameFormat(uint32 format);
23010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  static uint32 VideoFrameFormatToV4L2PixFmt(media::VideoFrame::Format format);
24010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  static uint32 VideoCodecProfileToV4L2PixFmt(media::VideoCodecProfile profile);
25010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Convert format requirements requested by a V4L2 device to gfx::Size.
26010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  static gfx::Size CodedSizeFromV4L2Format(struct v4l2_format format);
27010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~V4L2Device();
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
30010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  enum Type {
31010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    kDecoder,
32010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    kEncoder,
33010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    kImageProcessor,
34010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  };
35010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
36010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Creates and initializes an appropriate V4L2Device of |type| for the
37010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // current platform and returns a scoped_ptr<V4L2Device> on success, or NULL.
38010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  static scoped_ptr<V4L2Device> Create(Type type);
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Parameters and return value are the same as for the standard ioctl() system
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // call.
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual int Ioctl(int request, void* arg) = 0;
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This method sleeps until either:
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // - SetDevicePollInterrupt() is called (on another thread),
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // - |poll_device| is true, and there is new data to be read from the device,
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //   or an event from the device has arrived; in the latter case
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //   |*event_pending| will be set to true.
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns false on error, true otherwise.
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This method should be called from a separate thread.
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool Poll(bool poll_device, bool* event_pending) = 0;
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // These methods are used to interrupt the thread sleeping on Poll() and force
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // it to return regardless of device state, which is usually when the client
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // is no longer interested in what happens with the device (on cleanup,
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // client state change, etc.). When SetDevicePollInterrupt() is called, Poll()
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // will return immediately, and any subsequent calls to it will also do so
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // until ClearDevicePollInterrupt() is called.
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool SetDevicePollInterrupt() = 0;
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool ClearDevicePollInterrupt() = 0;
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Wrappers for standard mmap/munmap system calls.
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void* Mmap(void* addr,
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     unsigned int len,
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     int prot,
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     int flags,
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     unsigned int offset) = 0;
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void Munmap(void* addr, unsigned int len) = 0;
69e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
70010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Initializes the V4L2Device to operate as a device of |type|.
71010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Returns true on success.
72e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual bool Initialize() = 0;
73e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
74e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Creates an EGLImageKHR since each V4L2Device may use a different method of
75e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // acquiring one and associating it to the given texture. The texture_id is
76e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // used to bind the texture to the returned EGLImageKHR. buffer_index can be
77e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // used to associate the returned EGLImageKHR by the underlying V4L2Device
78e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // implementation.
79e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual EGLImageKHR CreateEGLImage(EGLDisplay egl_display,
80010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                     EGLContext egl_context,
81e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                                     GLuint texture_id,
82e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                                     gfx::Size frame_buffer_size,
83e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                                     unsigned int buffer_index,
84e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                                     size_t planes_count) = 0;
85e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
86e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Destroys the EGLImageKHR.
87e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual EGLBoolean DestroyEGLImage(EGLDisplay egl_display,
88e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                                     EGLImageKHR egl_image) = 0;
89e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
90e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Returns the supported texture target for the V4L2Device.
91e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual GLenum GetTextureTarget() = 0;
92e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
93010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Returns the preferred V4L2 input format or 0 if don't care.
94010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  virtual uint32 PreferredInputFormat() = 0;
95010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
96010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Returns the preferred V4L2 output format or 0 if don't care.
97e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual uint32 PreferredOutputFormat() = 0;
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
99e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  //  namespace content
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  //  CONTENT_COMMON_GPU_MEDIA_V4L2_VIDEO_DEVICE_H_
103