| 1 | /* |
| 2 | Copyright (c) 2012, Broadcom Europe Ltd |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the copyright holder nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "mmal.h" |
| 29 | #include "mmal_buffer.h" |
| 30 | #include "core/mmal_buffer_private.h" |
| 31 | #include "mmal_logging.h" |
| 32 | |
| 33 | #define ROUND_UP(s,align) ((((unsigned long)(s)) & ~((align)-1)) + (align)) |
| 34 | #define DEFAULT_COMMAND_SIZE 256 /**< 256 bytes of space for commands */ |
| 35 | #define ALIGN 8 |
| 36 | |
| 37 | /** Acquire a buffer header */ |
| 38 | void (MMAL_BUFFER_HEADER_T *) |
| 39 | { |
| 40 | #ifdef ENABLE_MMAL_EXTRA_LOGGING |
| 41 | LOG_TRACE("%p (%i)" , header, (int)header->priv->refcount+1); |
| 42 | #endif |
| 43 | header->priv->refcount++; |
| 44 | } |
| 45 | |
| 46 | /** Reset a buffer header */ |
| 47 | void (MMAL_BUFFER_HEADER_T *) |
| 48 | { |
| 49 | header->length = 0; |
| 50 | header->offset = 0; |
| 51 | header->flags = 0; |
| 52 | header->pts = MMAL_TIME_UNKNOWN; |
| 53 | header->dts = MMAL_TIME_UNKNOWN; |
| 54 | } |
| 55 | |
| 56 | /** Release a buffer header */ |
| 57 | void (MMAL_BUFFER_HEADER_T *) |
| 58 | { |
| 59 | #ifdef ENABLE_MMAL_EXTRA_LOGGING |
| 60 | LOG_TRACE("%p (%i)" , header, (int)header->priv->refcount-1); |
| 61 | #endif |
| 62 | |
| 63 | if(--header->priv->refcount != 0) |
| 64 | return; |
| 65 | |
| 66 | if (header->priv->pf_pre_release) |
| 67 | { |
| 68 | if (header->priv->pf_pre_release(header, header->priv->pre_release_userdata)) |
| 69 | return; /* delay releasing the buffer */ |
| 70 | } |
| 71 | mmal_buffer_header_release_continue(header); |
| 72 | } |
| 73 | |
| 74 | /** Finalise buffer release following a pre-release event */ |
| 75 | void (MMAL_BUFFER_HEADER_T *) |
| 76 | { |
| 77 | mmal_buffer_header_reset(header); |
| 78 | if (header->priv->reference) |
| 79 | mmal_buffer_header_release(header->priv->reference); |
| 80 | header->priv->reference = 0; |
| 81 | header->priv->pf_release(header); |
| 82 | } |
| 83 | |
| 84 | /** Replicate a buffer header */ |
| 85 | MMAL_STATUS_T (MMAL_BUFFER_HEADER_T *dest, |
| 86 | MMAL_BUFFER_HEADER_T *src) |
| 87 | { |
| 88 | #ifdef ENABLE_MMAL_EXTRA_LOGGING |
| 89 | LOG_TRACE("dest: %p src: %p" , dest, src); |
| 90 | #endif |
| 91 | |
| 92 | if (!dest || !src || dest->priv->reference) |
| 93 | return MMAL_EINVAL; |
| 94 | |
| 95 | mmal_buffer_header_acquire(src); |
| 96 | dest->priv->reference = src; |
| 97 | |
| 98 | /* Copy all the relevant fields */ |
| 99 | dest->cmd = src->cmd; |
| 100 | dest->alloc_size = src->alloc_size; |
| 101 | dest->data = src->data; |
| 102 | dest->offset = src->offset; |
| 103 | dest->length = src->length; |
| 104 | dest->flags = src->flags; |
| 105 | dest->pts = src->pts; |
| 106 | dest->dts = src->dts; |
| 107 | *dest->type = *src->type; |
| 108 | return MMAL_SUCCESS; |
| 109 | } |
| 110 | |
| 111 | /** Get the size in bytes of a fully initialised MMAL_BUFFER_HEADER_T */ |
| 112 | unsigned int (MMAL_BUFFER_HEADER_T *) |
| 113 | { |
| 114 | unsigned int ; |
| 115 | |
| 116 | header_size = ROUND_UP(sizeof(*header), ALIGN); |
| 117 | header_size += ROUND_UP(sizeof(*header->type), ALIGN); |
| 118 | header_size += ROUND_UP(DEFAULT_COMMAND_SIZE, ALIGN); |
| 119 | header_size += ROUND_UP(sizeof(*header->priv), ALIGN); |
| 120 | return header_size; |
| 121 | } |
| 122 | |
| 123 | /** Initialise a MMAL_BUFFER_HEADER_T */ |
| 124 | MMAL_BUFFER_HEADER_T *(void *mem, unsigned int length) |
| 125 | { |
| 126 | MMAL_BUFFER_HEADER_T *; |
| 127 | unsigned int = mmal_buffer_header_size(0); |
| 128 | |
| 129 | if(length < header_size) |
| 130 | return 0; |
| 131 | |
| 132 | memset(mem, 0, header_size); |
| 133 | |
| 134 | header = (MMAL_BUFFER_HEADER_T *)mem; |
| 135 | header->type = (void *)&header[1]; |
| 136 | header->priv = (MMAL_BUFFER_HEADER_PRIVATE_T *)&header->type[1]; |
| 137 | return header; |
| 138 | } |
| 139 | |
| 140 | /** Return a pointer to the area reserved for the driver */ |
| 141 | MMAL_DRIVER_BUFFER_T *(MMAL_BUFFER_HEADER_T *) |
| 142 | { |
| 143 | return (MMAL_DRIVER_BUFFER_T *)header->priv->driver_area; |
| 144 | } |
| 145 | |
| 146 | /** Return a pointer to a referenced buffer header */ |
| 147 | MMAL_BUFFER_HEADER_T *(MMAL_BUFFER_HEADER_T *) |
| 148 | { |
| 149 | return header->priv->reference; |
| 150 | } |
| 151 | |
| 152 | #ifdef __VIDEOCORE__ |
| 153 | # include "vcfw/rtos/common/rtos_common_mem.h" |
| 154 | #endif |
| 155 | |
| 156 | /** Lock the data buffer contained in the buffer header */ |
| 157 | MMAL_STATUS_T (MMAL_BUFFER_HEADER_T *) |
| 158 | { |
| 159 | #ifdef __VIDEOCORE__ |
| 160 | uint8_t *data = mem_lock((MEM_HANDLE_T)header->data); |
| 161 | if (!data) |
| 162 | return MMAL_EINVAL; |
| 163 | header->priv->payload_handle = (void *)header->data; |
| 164 | header->data = data; |
| 165 | #else |
| 166 | MMAL_PARAM_UNUSED(header); |
| 167 | #endif |
| 168 | |
| 169 | return MMAL_SUCCESS; |
| 170 | } |
| 171 | |
| 172 | /** Unlock the data buffer contained in the buffer header */ |
| 173 | void (MMAL_BUFFER_HEADER_T *) |
| 174 | { |
| 175 | #ifdef __VIDEOCORE__ |
| 176 | mem_unlock((MEM_HANDLE_T)header->priv->payload_handle); |
| 177 | header->data = header->priv->payload_handle; |
| 178 | #else |
| 179 | MMAL_PARAM_UNUSED(header); |
| 180 | #endif |
| 181 | } |
| 182 | |
| 183 | /** Set a pre-release callback for a buffer header */ |
| 184 | void (MMAL_BUFFER_HEADER_T *, MMAL_BH_PRE_RELEASE_CB_T cb, void *userdata) |
| 185 | { |
| 186 | header->priv->pf_pre_release = cb; |
| 187 | header->priv->pre_release_userdata = userdata; |
| 188 | } |
| 189 | |