]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - sitara-linux/video-graphics-test.git/blob - cmem_buf.cpp
video graphics test : new demo application
[sitara-linux/video-graphics-test.git] / cmem_buf.cpp
1 /*
2 * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
3
4
5 *  Redistribution and use in source and binary forms, with or without 
6 *  modification, are permitted provided that the following conditions 
7 *  are met:
8 *
9 *    Redistributions of source code must retain the above copyright 
10 *    notice, this list of conditions and the following disclaimer.
11 *
12 *    Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the 
14 *    documentation and/or other materials provided with the   
15 *    distribution.
16 *
17 *    Neither the name of Texas Instruments Incorporated nor the names of
18 *    its contributors may be used to endorse or promote products derived
19 *    from this software without specific prior written permission.
20 *
21 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
35 /* This file defines function to allocate memory from CMEM pool */
37 #include <iostream>
38 #include <ti/cmem.h>
39 #include <linux/dma-buf.h>
40 #include <sys/ioctl.h>
41 #include "cmem_buf.h"
42 #include "error.h"
44 #define CMEM_BLOCKID CMEM_CMABLOCKID
46 CMEM_AllocParams cmem_alloc_params = {
47     CMEM_HEAP,      /* type */
48     CMEM_CACHED,    /* flags */
49     1               /* alignment */
50 };
52 bool g_is_cmem_init=false;
53 bool g_is_cmem_exit=false;
56 int BufObj::alloc_cmem_buffer(uint32_t size, uint32_t align, void **cmem_buf){
57     cmem_alloc_params.alignment = align;
59     *cmem_buf = CMEM_alloc2(CMEM_BLOCKID, size,
60         &cmem_alloc_params);
62     if(*cmem_buf == NULL){
63         ERROR("CMEM allocation failed");
64         return -1;
65     }
67     return CMEM_export_dmabuf(*cmem_buf);
68 }
70 void BufObj::free_cmem_buffer(void *cmem_buffer){
71     CMEM_free(cmem_buffer, &cmem_alloc_params);
72 }
74  /* If the user space need to access the CMEM buffer for CPU based processing, it can set
75     the CMEM buffer cache settings using DMA_BUF IOCTLS. 
76     Cahche_operation setting for invalidation is (DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ))
77         Cache operation settting for buffer read write is (DMA_BUF_SYNC_WRITE | DMA_BUF_SYNC_READ
78         This piece of code is not used in the application and hence not tested
79 */
80 int BufObj::dma_buf_do_cache_operation(int dma_buf_fd, uint32_t cache_operation){
81     int ret;
82     struct dma_buf_sync sync;
83     sync.flags = cache_operation;
85     ret = ioctl(dma_buf_fd, DMA_BUF_IOCTL_SYNC, &sync);
87     return ret;
88 }
90 BufObj::BufObj(uint32_t w, uint32_t h, uint32_t bpp,uint32_t fourcc, 
91                uint32_t align, uint32_t num_bufs){
92     m_width = w;
93     m_height = h;
95     /* Vivante HAL needs 16 pixel alignment in width and 4 pixel alignment in
96     * height and hence putting that restriction for now on all buffer allocation through CMEM.
97     */
98     m_stride = ((w + 15) & ~15) * bpp;
99     m_fourcc = fourcc;
100     m_num_bufs = num_bufs;
102     //Avoid calling CMEM init with every BufObj object
103     if(g_is_cmem_init == false){
104         CMEM_init();
105         g_is_cmem_init = true;
106     }
108     m_fd = (int *)malloc(num_bufs * sizeof(int));
110     if(m_fd == NULL){
111         ERROR("DispObj: m_fd array allocation failure\n");
112     }
114     m_fb_id = (uint32_t *)malloc(num_bufs * sizeof(int));
115     if(m_fb_id == NULL){
116         ERROR("DispObj: m_fb_id array allocation failure\n");
117     }
119     m_buf = (void **)malloc(num_bufs * sizeof(int));
120     if(m_buf == NULL){
121         ERROR("DispObj: m_buf array allocation failure\n");
122     }
124     for(uint32_t i = 0; i < num_bufs; i++){
126         /* Vivante HAL needs 16 pixel alignment in width and 4 pixel alignment in
127         * height and hence adjust the buffer size accrodingly.
128         */
129         uint32_t size = m_stride * ((m_height + 3) & ~3);
131         m_fd[i]  = alloc_cmem_buffer(size, align, &m_buf[i]);
133                 if(m_buf[i] == NULL){
134                         ERROR("CMEM buffer allocation failed");
135                 }
137         if(m_fd[i] < 0){
138             free_cmem_buffer(m_buf[i]);
139             ERROR("Cannot export CMEM buffer");
140         }
141     }
145 BufObj::~BufObj(){
147     uint32_t i;
149     for(i = 0; i < m_num_bufs; i++){
150         //Free the buffer
151         free_cmem_buffer(m_buf[i]);
152     }
154     free(m_fd);
155     free(m_fb_id);
156     free(m_buf);
158     if(g_is_cmem_exit == false){
159         CMEM_exit();
160         g_is_cmem_exit = true;
161     }