hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * Copyright (C) 2016 Rockchip Electronics Co., Ltd.
 * Authors:
 *  Zhiqin Wei <wzq@rock-chips.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#define LOG_NDEBUG 0
#define LOG_TAG "rgaSlt"
 
#include <stdint.h>
#include <sys/types.h>
#include <math.h>
#include <fcntl.h>
#include <utils/misc.h>
#include <signal.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <linux/stddef.h>
 
#include <utils/Atomic.h>
#include <utils/Errors.h>
#include <utils/Log.h>
#include <utils/Thread.h>
#include <cutils/properties.h>
#include <ui/PixelFormat.h>
#include <ui/GraphicBuffer.h>
#include <ui/GraphicBufferMapper.h>
#include <hardware/hardware.h>
 
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES/gl.h>
#include <GLES/glext.h>
 
#ifndef ANDROID_8
#include <binder/IPCThreadState.h>
#endif
 
#include "RockchipRga.h"
#include "RgaUtils.h"
 
///////////////////////////////////////////////////////
 
#define EN_GRAPHICBUFFER 0
 
using namespace android;
 
int main() {
    int ret = 0;
    int srcWidth,srcHeight,srcFormat;
    int dstWidth,dstHeight,dstFormat;
 
    srcWidth = 1280;
    srcHeight = 720;
    srcFormat = HAL_PIXEL_FORMAT_RGBA_8888;
 
    dstWidth = 1280;
    dstHeight = 720;
    dstFormat = HAL_PIXEL_FORMAT_RGBA_8888;
 
    RockchipRga& rkRga(RockchipRga::get());
 
#if EN_GRAPHICBUFFER
    char* buf = NULL;
//    GraphicBufferMapper &mgbMapper = GraphicBufferMapper::get();
 
    /********** apply for src_buffer **********/
#ifdef ANDROID_7_DRM
    sp<GraphicBuffer> gbs(new GraphicBuffer(srcWidth,srcHeight,srcFormat,
                                            GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_FB));
#else
    sp<GraphicBuffer> gbs(new GraphicBuffer(srcWidth,srcHeight,srcFormat,
                                            GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_OFTEN));
#endif
 
    if (gbs->initCheck()) {
        printf("GraphicBuffer_src error : %s\n",strerror(errno));
        return ret;
    } else
        printf("GraphicBuffer_src %s \n","ok");
 
    /********** apply for dst_buffer **********/
#ifdef ANDROID_7_DRM
    sp<GraphicBuffer> gbd(new GraphicBuffer(dstWidth,dstHeight,dstFormat,
                                            GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_FB));
#else
    sp<GraphicBuffer> gbd(new GraphicBuffer(dstWidth,dstHeight,dstFormat,
                                            GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_OFTEN));
#endif
 
    if (gbd->initCheck()) {
        printf("GraphicBuffer_dst error : %s\n",strerror(errno));
        return ret;
    } else
        printf("GraphicBuffer_dst %s \n","ok");
 
    /********** map buffer_address to userspace **********/
 
    /*
    #ifdef ANDROID_8
        buffer_handle_t importedHandle_src;
        buffer_handle_t importedHandle_dst;
        mgbMapper.importBuffer(gbs->handle, &importedHandle_src);
        mgbMapper.importBuffer(gbd->handle, &importedHandle_dst);
        gbs->handle = importedHandle_src;
        gbd->handle = importedHandle_dst;
    #else
        mgbMapper.registerBuffer(gbs->handle);
        mgbMapper.registerBuffer(gbd->handle);
    #endif
    */
 
    /********** write data to src_buffer or init buffer**********/
    ret = gbs->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&buf);
 
    if (ret) {
        printf("lock buffer_src error : %s\n",strerror(errno));
        return ret;
    } else
        printf("lock buffer_src %s \n","ok");
 
#if 1
    get_buf_from_file(buf, srcFormat, srcWidth, srcHeight, 0);
#else
    memset(buf,0x55,4*1280*720);
#endif
    ret = gbs->unlock();
    if (ret) {
        printf("unlock buffer_src error : %s\n",strerror(errno));
        return ret;
    } else
        printf("unlock buffer_src %s \n","ok");
 
    /********** write data to dst_buffer or init buffer **********/
    ret = gbd->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&buf);
    if (ret) {
        printf("lock buffer_dst error : %s\n",strerror(errno));
        return ret;
    } else
        printf("lock buffer_dst %s \n","ok");
 
    memset(buf,0x00,4*1280*720);
 
    ret = gbd->unlock();
    if (ret) {
        printf("unlock buffer_src error : %s\n",strerror(errno));
        return ret;
    } else
        printf("unlock buffer_src %s \n","ok");
 
    /********** rga_info_t Init **********/
    rga_info_t src;
    rga_info_t dst;
 
    memset(&src, 0, sizeof(rga_info_t));
    src.fd = -1;
    src.mmuFlag = 1;
    src.hnd = gbs->handle;
 
    memset(&dst, 0, sizeof(rga_info_t));
    dst.fd = -1;
    dst.mmuFlag = 1;
    dst.hnd = gbd->handle;
 
    /********** get src_Fd **********/
    ret = rkRga.RkRgaGetBufferFd(gbs->handle, &src.fd);
    printf("src.fd =%d\n",src.fd);
    ALOGD("src.fd =%d\n",src.fd);
    if (ret) {
        printf("rgaGetsrcFd fail : %s,hnd=%p \n",
               strerror(errno),(void*)(gbd->handle));
    }
    /********** get dst_Fd **********/
    ret = rkRga.RkRgaGetBufferFd(gbd->handle, &dst.fd);
    printf("dst.fd =%d \n",dst.fd);
    if (ret) {
        printf("rgaGetdstFd error : %s,hnd=%p\n",
               strerror(errno),(void*)(gbd->handle));
    }
    /********** if not fd, try to check phyAddr and virAddr **************/
#ifndef RK3188
    if(src.fd <= 0|| dst.fd <= 0)
 
    {
        /********** check phyAddr and virAddr ,if none to get virAddr **********/
        if (( src.phyAddr != 0 || src.virAddr != 0 ) || src.hnd != NULL ) {
            ret = RkRgaGetHandleMapAddress( gbs->handle, &src.virAddr );
            printf("src.virAddr =%p\n",src.virAddr);
            if(!src.virAddr) {
                printf("err! src has not fd and address for render ,Stop!\n");
                break;
            }
        }
 
        /********** check phyAddr and virAddr ,if none to get virAddr **********/
        if (( dst.phyAddr != 0 || dst.virAddr != 0 ) || dst.hnd != NULL ) {
            ret = RkRgaGetHandleMapAddress( gbd->handle, &dst.virAddr );
            printf("dst.virAddr =%p\n",dst.virAddr);
            if(!dst.virAddr) {
                printf("err! dst has not fd and address for render ,Stop!\n");
                break;
            }
        }
    }
#endif /* #ifndef RK3188 */
#else
    char *src_va = NULL;
    char *dst_va = NULL;
 
    src_va = (char *)malloc(srcWidth*srcHeight*get_bpp_from_format(srcFormat));
    dst_va = (char *)malloc(dstWidth*dstHeight*get_bpp_from_format(dstFormat));
 
#if 1
    get_buf_from_file(src_va, srcFormat, srcWidth, srcHeight, 0);
#else
    memset(src_va,0x00,srcWidth*srcHeight*get_bpp_from_format(srcFormat));
#endif
 
#if 0
    get_buf_from_file(dst_va, dstFormat, dstWidth, dstHeight, 0);
#else
    memset(dst_va,0x00,dstWidth*dstHeight*get_bpp_from_format(dstFormat));
#endif
 
    /********** rga_info_t Init **********/
    rga_info_t src;
    rga_info_t dst;
 
    memset(&src, 0, sizeof(rga_info_t));
    memset(&dst, 0, sizeof(rga_info_t));
 
    src.virAddr = src_va;
    src.mmuFlag = 1;
 
    dst.virAddr =dst_va;
    dst.mmuFlag = 1;
#endif /* #if EN_GRAPHICBUFFER */
 
    while(1) {
        /********** set the rect_info **********/
        rga_set_rect(&src.rect, 0,0,srcWidth,srcHeight,srcWidth/*stride*/,srcHeight,srcFormat);
        rga_set_rect(&dst.rect, 0,0,dstWidth,dstHeight,dstWidth/*stride*/,dstHeight,dstFormat);
 
        /************ set the rga_mod ,rotation\composition\scale\copy .... **********/
 
        /********** call rga_Interface **********/
        ret = rkRga.RkRgaBlit(&src, &dst, NULL);
        if (ret) {
            printf("rgaFillColor error : %s\n",
                   strerror(errno));
        }
 
#if EN_GRAPHICBUFFER
        {
            /********** output buf data to file **********/
            char* dstbuf = NULL;
 
            ret = gbd->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&dstbuf);
            output_buf_data_to_file(dstbuf, dstFormat, dstWidth, dstHeight, 0);
            ret = gbd->unlock();
 
            /********** rga slt check **********/
            ret = gbd->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&dstbuf);
            if (ret) {
                printf("lock buffer_dst error : %s\n",strerror(errno));
                return ret;
            } else
                printf("lock buffer_dst %s \n","ok");
 
            ret = gbs->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&buf);
            if (ret) {
                printf("lock buffer_src error : %s\n",strerror(errno));
                return ret;
            } else
                printf("lock buffer_src %s \n","ok");
 
            int size = dstWidth * dstHeight * 4;
            unsigned int *pstd = (unsigned int *)buf;
            unsigned int *pnow = (unsigned int *)dstbuf;
            int errCount = 0;
            int rightCount = 0;
            printf("[  num   : srcInfo    dstInfo ] \n");
            for (int i = 0; i < size / 4; i++) {
                if (*pstd != *pnow) {
                    printf("[X%.8d:0x%x 0x%x]  ", i, *pstd,*pnow);
                    if (i % 4 == 0 )
                        printf("\n");
                    errCount ++;
 
                } else {
                    if (i % (640*1024) == 0)
                        printf("[Y%.8d:0x%.8x 0x%.8x]\n", i, *pstd,*pnow);
                    rightCount++;
                }
                pstd++;
                pnow++;
                if (errCount > 64)
                    break;
            }
            ret = gbs->unlock();
            if (ret) {
                printf("unlock buffer_src error : %s\n",strerror(errno));
                return ret;
            }
 
            ret = gbd->unlock();
            if (ret) {
                printf("unlock buffer_dst error : %s\n",strerror(errno));
                return ret;
            }
 
            printf("errCount=%d,rightCount=%d\n", errCount, rightCount);
            if(errCount != 0) {
                printf("rga slt err !! \n");
                return 1;
            } else {
                printf("rga slt sucess !! \n");
                return 0;
            }
        }
#else
        {
            /********** output buf data to file **********/
            output_buf_data_to_file(dst_va, dstFormat, dstWidth, dstHeight, 0);
 
            int size = dstWidth * dstHeight * 4;
            unsigned int *pstd = (unsigned int *)src_va;
            unsigned int *pnow = (unsigned int *)dst_va;
            int errCount = 0;
            int rightCount = 0;
            printf("[  num   : srcInfo    dstInfo ] \n");
            for (int i = 0; i < size / 4; i++) {
                if (*pstd != *pnow) {
                    printf("[X%.8d:0x%x 0x%x]  ", i, *pstd,*pnow);
                    if (i % 4 == 0 )
                        printf("\n");
                    errCount ++;
 
                } else {
                    if (i % (640*1024) == 0)
                        printf("[Y%.8d:0x%.8x 0x%.8x]\n", i, *pstd,*pnow);
                    rightCount++;
                }
                pstd++;
                pnow++;
                if (errCount > 64)
                    break;
            }
 
            printf("errCount=%d,rightCount=%d\n", errCount, rightCount);
            if(errCount != 0) {
                printf("rga slt err !! \n");
                return 1;
            } else {
                printf("rga slt sucess !! \n");
                return 0;
            }
        }
#endif /* #if EN_GRAPHICBUFFER */
        break;
    }
 
#if !EN_GRAPHICBUFFER
    free(src_va);
    free(dst_va);
#endif
    return 0;
}