Wind Yuan | 0bb87ca | 2016-08-24 03:07:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * dma_video_buffer.cpp - dma buffer |
| 3 | * |
| 4 | * Copyright (c) 2016 Intel Corporation |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | * Author: Wind Yuan <feng.yuan@intel.com> |
| 19 | */ |
| 20 | |
Wind Yuan | 0bb87ca | 2016-08-24 03:07:36 -0400 | [diff] [blame] | 21 | #include "dma_video_buffer.h" |
| 22 | |
| 23 | namespace XCam { |
| 24 | |
Wind Yuan | 226b6af | 2017-03-07 05:16:31 -0500 | [diff] [blame] | 25 | class DmaVideoBufferPriv |
| 26 | : public DmaVideoBuffer |
| 27 | { |
| 28 | friend SmartPtr<DmaVideoBuffer> external_buf_to_dma_buf (XCamVideoBuffer *buf); |
| 29 | protected: |
| 30 | DmaVideoBufferPriv (const VideoBufferInfo &info, XCamVideoBuffer *buf); |
| 31 | ~DmaVideoBufferPriv (); |
| 32 | |
| 33 | private: |
| 34 | XCamVideoBuffer *_external_buf; |
| 35 | }; |
| 36 | |
Wind Yuan | 0bb87ca | 2016-08-24 03:07:36 -0400 | [diff] [blame] | 37 | DmaVideoBuffer::DmaVideoBuffer (const VideoBufferInfo &info, int dma_fd, bool need_close_fd) |
| 38 | : VideoBuffer (info) |
| 39 | , _dma_fd (dma_fd) |
| 40 | , _need_close_fd (need_close_fd) |
| 41 | { |
| 42 | XCAM_ASSERT (dma_fd >= 0); |
| 43 | } |
| 44 | |
| 45 | DmaVideoBuffer::~DmaVideoBuffer () |
| 46 | { |
| 47 | if (_need_close_fd && _dma_fd > 0) |
| 48 | close (_dma_fd); |
| 49 | } |
| 50 | |
| 51 | uint8_t * |
| 52 | DmaVideoBuffer::map () |
| 53 | { |
| 54 | XCAM_ASSERT (false && "DmaVideoBuffer::map not supported"); |
| 55 | return NULL; |
| 56 | } |
| 57 | bool |
| 58 | DmaVideoBuffer::unmap () |
| 59 | { |
| 60 | XCAM_ASSERT (false && "DmaVideoBuffer::map not supported"); |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | int |
| 65 | DmaVideoBuffer::get_fd () |
| 66 | { |
| 67 | return _dma_fd; |
| 68 | } |
| 69 | |
Wind Yuan | 226b6af | 2017-03-07 05:16:31 -0500 | [diff] [blame] | 70 | DmaVideoBufferPriv::DmaVideoBufferPriv (const VideoBufferInfo &info, XCamVideoBuffer *buf) |
| 71 | : DmaVideoBuffer (info, xcam_video_buffer_get_fd (buf), false) |
| 72 | , _external_buf (buf) |
| 73 | { |
| 74 | if (buf->ref) |
| 75 | xcam_video_buffer_ref (buf); |
| 76 | } |
| 77 | |
| 78 | DmaVideoBufferPriv::~DmaVideoBufferPriv () |
| 79 | { |
| 80 | if (_external_buf && _external_buf->unref && _external_buf->ref) |
| 81 | xcam_video_buffer_unref (_external_buf); |
| 82 | } |
| 83 | |
| 84 | SmartPtr<DmaVideoBuffer> |
| 85 | external_buf_to_dma_buf (XCamVideoBuffer *buf) |
| 86 | { |
| 87 | VideoBufferInfo buf_info; |
| 88 | SmartPtr<DmaVideoBuffer> video_buffer; |
| 89 | |
| 90 | XCAM_FAIL_RETURN ( |
| 91 | ERROR, buf, NULL, |
| 92 | "external_buf_to_dma_buf failed since buf is NULL"); |
| 93 | |
| 94 | int buffer_fd = 0; |
| 95 | if (buf->get_fd) |
| 96 | buffer_fd = xcam_video_buffer_get_fd(buf); |
| 97 | |
| 98 | XCAM_FAIL_RETURN ( |
| 99 | ERROR, buffer_fd > 0, NULL, |
| 100 | "external_buf_to_dma_buf failed, can't get buf file-handle"); |
| 101 | |
| 102 | buf_info.init (buf->info.format, buf->info.width, buf->info.height, |
| 103 | buf->info.aligned_width, buf->info.aligned_height, buf->info.size); |
| 104 | video_buffer = new DmaVideoBufferPriv (buf_info, buf); |
| 105 | XCAM_ASSERT (video_buffer.ptr ()); |
| 106 | return video_buffer; |
| 107 | } |
| 108 | |
Wind Yuan | 0bb87ca | 2016-08-24 03:07:36 -0400 | [diff] [blame] | 109 | } |