liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <stdio.h>
#include <fcntl.h>
#include <utils/Log.h>
#include <stdlib.h>
#include <string.h>
#include <cutils/properties.h>
#include <errno.h>
#include "g2d_driver.h"
#include <memory/memoryAdapter.h>
#include <memory/sc_interface.h>
//#include "memory/ionMemory/ionAllocEntry.h"
#include "G2dApi.h"
 
int g2dInit()
{
    int g2dHandle = open("/dev/g2d", O_RDWR, 0);
    if (g2dHandle < 0)
    {
         ALOGE("open /dev/g2d failed");
         return -1;
    }
 
    //ALOGD("------------------------------g2dInit------------------------------------------g2dHandle:%d",g2dHandle);
    return g2dHandle;
}
 
int g2dUnit(int g2dHandle)
{
    if(g2dHandle > 0)
    {
        close(g2dHandle);
    }
    return 0;
}
 
int g2dAllocBuff(stV4L2BUF* bufHandle,int width, int hight)
{
    struct ScCamMemOpsS* memOps  = MemCamAdapterGetOpsS();
 
    if (memOps == NULL)
    {
        ALOGE("------------------------g2dAllocBuff memOps is NULL");
        return -1;
    }
    int ret = memOps->open_cam();
    if (ret < 0)
    {
        ALOGE("------------------------g2dAllocBuff ion_alloc_open failed");
        return -1;
    }
 
    int size = width*hight*3/2;
    bufHandle->addrVirY = (unsigned long)memOps->palloc_cam(size,&bufHandle->nShareBufFd);
    bufHandle->addrPhyY = (unsigned long)memOps->cpu_get_phyaddr_cam((void*)bufHandle->addrVirY);
    memset((void*)bufHandle->addrVirY, 0x10, width*hight);
    memset((void*)(bufHandle->addrVirY + width*hight),
                0x80, width*hight/2);
    ALOGD("------------------------------g2dAllocBuff ion_alloc success");
    return 0;
}
 
int g2dFreeBuff(stV4L2BUF* bufHandle)
{
    struct ScCamMemOpsS* memOps  = MemCamAdapterGetOpsS();
 
    if (memOps == NULL)
    {
        ALOGE("------------------------g2dAllocBuff memOps is NULL");
        return -1;
    }
 
    memOps->pfree_cam((void*)bufHandle->addrVirY);
    bufHandle->addrPhyY = 0;
    memOps->close_cam();
    return 0;
}
 
 
int g2dScale(int g2dHandle,unsigned int psrc, int src_w, int src_h, int src_x, int src_y, int src_rectw, int src_recth, unsigned int pdst, int dst_w, int dst_h, int dst_x, int dst_y,int dst_rectw, int dst_recth)
{
    g2d_stretchblt stret_para;
   int err = 0;
 
    if(g2dHandle<=0)
    {
        ALOGE("g2dScale g2dHandle error!");
        return -1;
    }
 
   stret_para.flag = G2D_BLT_NONE;
    stret_para.src_image.addr[0] = (int)psrc;
   stret_para.src_image.addr[1] = (int)psrc + src_w * src_h;
   stret_para.src_image.w = src_w;          /* src buffer width in pixel units */
   stret_para.src_image.h = src_h;          /* src buffer height in pixel units */
 
   stret_para.src_image.format = G2D_FMT_PYUV420UVC;
 
   stret_para.src_image.pixel_seq = G2D_SEQ_NORMAL;          /* not use now */
   stret_para.src_rect.x = src_x;                        /* src rect->x in pixel */
   stret_para.src_rect.y = src_y;                        /* src rect->y in pixel */
   stret_para.src_rect.w = src_rectw;            /* src rect->w in pixel */
   stret_para.src_rect.h = src_recth;            /* src rect->h in pixel */
 
   stret_para.dst_image.addr[0] = (int)pdst;//yuv420;
   stret_para.dst_image.addr[1] = (int)pdst + dst_w * dst_h;
   stret_para.dst_image.w = dst_w;          /* dst buffer width in pixel units */
   stret_para.dst_image.h = dst_h;          /* dst buffer height in pixel units */
 
   stret_para.dst_image.format = G2D_FMT_PYUV420UVC;//G2D_FMT_PYUV422UVC;
 
   stret_para.dst_image.pixel_seq = G2D_SEQ_VYUY;//G2D_SEQ_NORMAL;          /* not use now */
   stret_para.dst_rect.x= dst_x;
   stret_para.dst_rect.y= dst_y;
   stret_para.dst_rect.w = dst_rectw;
   stret_para.dst_rect.h = dst_recth;
 
   err = ioctl(g2dHandle, G2D_CMD_STRETCHBLT, (unsigned long)&stret_para);
 
   if(0 > err)
   {
       ALOGE("g2d_scale: failed to call G2D_CMD_STRETCHBLT!!!\n");
       return -1;
   }
 
   return 0;
}
 
int g2dClip(int g2dHandle,void* psrc, int src_w, int src_h, int src_x, int src_y, int width, int height, void* pdst, int dst_w, int dst_h, int dst_x, int dst_y)
{
    g2d_blt     blit_para;
    int         err;
 
    if(g2dHandle<=0)
        return -1;
    blit_para.src_image.addr[0]      = (int)psrc;
    blit_para.src_image.addr[1]      = (int)psrc + src_w * src_h;
    blit_para.src_image.w            = src_w;
    blit_para.src_image.h            = src_h;
    blit_para.src_image.format       = G2D_FMT_PYUV420UVC;
    blit_para.src_image.pixel_seq    = G2D_SEQ_NORMAL;
    blit_para.src_rect.x             = src_x;
    blit_para.src_rect.y             = src_y;
    blit_para.src_rect.w             = width;
    blit_para.src_rect.h             = height;
 
    blit_para.dst_image.addr[0]      = (int)pdst;
    blit_para.dst_image.addr[1]      = (int)pdst + dst_w * dst_h;
    blit_para.dst_image.w            = dst_w;
    blit_para.dst_image.h            = dst_h;
    blit_para.dst_image.format       = G2D_FMT_PYUV420UVC;
    blit_para.dst_image.pixel_seq    = G2D_SEQ_NORMAL;
    blit_para.dst_x                  = dst_x;
    blit_para.dst_y                  = dst_y;
    blit_para.color                  = 0xff;
    blit_para.alpha                  = 0xff;
 
    blit_para.flag = G2D_BLT_NONE;
 
    err = ioctl(g2dHandle, G2D_CMD_BITBLT, (unsigned long)&blit_para);
    if(err < 0)
    {
        ALOGE("-------------------------------------------------g2d_clip: failed to call G2D_CMD_BITBLT!!!\n");
        return -1;
    }
 
    return 0;
}
 
 
int g2dRotate(int g2dHandle,g2dRotateAngle angle, unsigned char *src, int src_width, int src_height, int src_x, int src_y, int width, int height,unsigned char *dst, int dst_width, int dst_height,int dst_x, int dst_y)
{
    g2d_blt    blit_para;
 
    if(g2dHandle<=0)
        return -1;
    switch(angle)
    {
        case G2D_ROTATE90:
            blit_para.flag = G2D_BLT_ROTATE90;
            break;
        case G2D_ROTATE180:
            blit_para.flag = G2D_BLT_ROTATE180;
            break;
        case G2D_ROTATE270:
            blit_para.flag = G2D_BLT_ROTATE270;
            break;
        case G2D_FLIP_HORIZONTAL:
            blit_para.flag = G2D_BLT_FLIP_HORIZONTAL;
            break;
        case G2D_FLIP_VERTICAL:
            blit_para.flag = G2D_BLT_FLIP_VERTICAL;
            break;
        case G2D_MIRROR45:
            blit_para.flag = G2D_BLT_MIRROR45;
            break;
        case G2D_MIRROR135:
            blit_para.flag = G2D_BLT_MIRROR135;
            break;
    }
    //str.flag = G2D_BLT_NONE;//G2D_BLT_NONE;//G2D_BLT_PIXEL_ALPHA|G2D_BLT_ROTATE90;
   blit_para.src_image.addr[0] = (int)src;
   blit_para.src_image.addr[1] = (int)src + src_width * src_height;
   blit_para.src_image.w = src_width;          /* src buffer width in pixel units */
   blit_para.src_image.h = src_height;          /* src buffer height in pixel units */
 
    blit_para.src_image.format = G2D_FMT_PYUV420UVC;//G2D_FMT_PYUV422UVC;
    blit_para.src_image.pixel_seq = G2D_SEQ_NORMAL;          /* not use now */
   blit_para.src_rect.x = src_x;                        /* src rect->x in pixel */
   blit_para.src_rect.y = src_y;                        /* src rect->y in pixel */
   blit_para.src_rect.w = width;            /* src rect->w in pixel */
   blit_para.src_rect.h = height;            /* src rect->h in pixel */
 
    blit_para.dst_image.addr[0] = (int)dst;//yuv420;
   blit_para.dst_image.addr[1] = (int)dst + dst_width * dst_height;
   //blit_para.dst_image.addr[1] = (int)pRecorderBuffer->top_c;//yuv420 + width * height;
   blit_para.dst_image.w = dst_width;          /* dst buffer width in pixel units */
   blit_para.dst_image.h = dst_height;          /* dst buffer height in pixel units */
   blit_para.dst_image.format = G2D_FMT_PYUV420UVC;//G2D_FMT_PYUV422UVC;
   blit_para.dst_image.pixel_seq = G2D_SEQ_VYUY;          /* not use now */
   blit_para.dst_x = dst_x;
   blit_para.dst_y = dst_y;
 
   blit_para.color = 0xFF;                          /* fix me*/
   blit_para.alpha = 0xFF;                /* globe alpha */
 
   if(ioctl(g2dHandle, G2D_CMD_BITBLT, (unsigned long)&blit_para) < 0)
   {
        ALOGE("--------------------------------------------g2dRotate: failed to call G2D_CMD_BITBLT!!!\n");
        return -1;
    }
    return 0;
}
 
struct G2dOpsS _G2dOpsS =
{
    .fpG2dInit=g2dInit,
    .fpG2dUnit=g2dUnit,
    .fpG2dAllocBuff=g2dAllocBuff,
    .fpG2dFreeBuff=g2dFreeBuff,
    .fpG2dScale=g2dScale,
    .fpG2dClip=g2dClip,
    .fpG2dRotate=g2dRotate
};
 
struct G2dOpsS* GetG2dOpsS()
{
    ALOGD("------------------------------_GetG2dOpsS------------------------------------------");
    //ALOGD("*** _GetG2dOpsS ***");
    return &_G2dOpsS;
}
 
//};