hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
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
/*
 * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
 * Authors:
 *  PutinLee <putin.lee@rock-chips.com>
 *  Cerf Yu <cerf.yu@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 "rgaColorPalette"
 
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
 
#include <ui/PixelFormat.h>
#include <ui/GraphicBuffer.h>
#include <ui/GraphicBufferMapper.h>
#include <hardware/hardware.h>
#include <RockchipRga.h>
#include "RgaUtils.h"
#include "core/NormalRga.h"
 
#include "im2d_api/im2d.hpp"
 
using namespace android;
 
#define IS_IM2D_API 1
 
sp<GraphicBuffer> GraphicBuffer_Init(int width, int height,int format)
{
#ifdef ANDROID_7_DRM
    sp<GraphicBuffer> gb(new GraphicBuffer(width,height,format,
        GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_FB));
#else
    sp<GraphicBuffer> gb(new GraphicBuffer(width,height,format,
        GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_OFTEN));
#endif
 
    if (gb->initCheck())
    {
        printf("GraphicBuffer check error : %s\n",strerror(errno));
        return NULL;
    }
 
    return gb;
}
 
/********** write data to buffer or init buffer**********/
int GraphicBuffer_Fill(sp<GraphicBuffer> gb, int flag, int index) {
    int ret;
    char* buf = NULL;
    ret = gb->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&buf);
    if (ret) {
        printf("lock buffer error : %s\n",strerror(errno));
        return -1;
    } else
        printf("lock buffer %s \n","ok");
 
    if(flag) {
        memset(buf,0xFF,get_bpp_from_format(RkRgaGetRgaFormatFromAndroid(gb->getPixelFormat()))*gb->getWidth()*gb->getHeight());
    }
    else {
        ret = get_buf_from_file(buf, RkRgaGetRgaFormatFromAndroid(gb->getPixelFormat()), gb->getWidth(), gb->getHeight(), index);
        if (!ret)
            printf("open file %s \n", "ok");
        else {
            printf ("open file %s \n", "fault");
            return -1;
        }
    }
 
    ret = gb->unlock();
    if (ret) {
        printf("unlock buffer error : %s\n",strerror(errno));
        return -1;
    } else
        printf("unlock buffer %s \n","ok");
 
    return 0;
}
 
int GraphicBuffer_Fill_LUT(sp<GraphicBuffer> gb, int flag, int index) {
    int ret, i;
    char* buf = NULL;
    ret = gb->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&buf);
 
    if (ret) {
        printf("lock buffer error : %s\n",strerror(errno));
        return -1;
    } else
        printf("lock buffer %s \n","ok");
 
   /*Draw lut table*/
    if(flag) {
          for (i = 0; i < 32*4; i+=4) {
              buf[i+0] = 0xFF;
              buf[i+1] = 0x00;
              buf[i+2] = 0x00;
              buf[i+3] = 0x00;
          }
          for (; i < 32*2*4; i+=4) {
              buf[i+0] = 0x00;
              buf[i+1] = 0xFF;
              buf[i+2] = 0x00;
              buf[i+3] = 0x00;
          }
          for (; i < 32*3*4; i+=4) {
              buf[i+0] = 0x00;
              buf[i+1] = 0x00;
              buf[i+2] = 0xFF;
              buf[i+3] = 0x00;
          }
          for (; i < 32*4*4; i+=4) {
              buf[i+0] = 0xaf;
              buf[i+1] = 0xaf;
              buf[i+2] = 0xaf;
              buf[i+3] = 0x00;
          }
          for (; i < 32*5*4; i+=4) {
              buf[i+0] = 0xFF;
              buf[i+1] = 0xFF;
              buf[i+2] = 0x00;
              buf[i+3] = 0x00;
          }
          for (; i < 32*6*4; i+=4) {
              buf[i+0] = 0xFF;
              buf[i+1] = 0x00;
              buf[i+2] = 0xFF;
              buf[i+3] = 0x00;
          }
          for (; i < 32*7*4; i+=4) {
              buf[i+0] = 0x00;
              buf[i+1] = 0xFF;
              buf[i+2] = 0xFF;
              buf[i+3] = 0x00;
          }
          for (; i < 32*8*4; i+=4) {
              buf[i+0] = 0xFF;
              buf[i+1] = 0xFF;
              buf[i+2] = 0xFF;
              buf[i+3] = 0x00;
          }
    }
    else {
        ret = get_buf_from_file(buf, gb->getPixelFormat(), gb->getWidth(), gb->getHeight(), index);
        if (!ret)
            printf("open file %s \n", "ok");
        else {
            printf ("open file %s \n", "fault");
            return -1;
        }
    }
 
    ret = gb->unlock();
    if (ret) {
        printf("unlock buffer error : %s\n",strerror(errno));
        return -1;
    } else
        printf("unlock buffer %s \n","ok");
 
    return 0;
}
 
/********** output buf data to file **********/
int Out2Bin(sp<GraphicBuffer> gb, int index)
{
    char* dstbuf = NULL;
    int ret = 0;
    if (gb != NULL)
    {
        ret = gb->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&dstbuf);
        if (ret)
            {
                  printf("lock buffer_src error : %s\n",strerror(errno));
                  return ret;
            }
        output_buf_data_to_file(dstbuf, gb->getPixelFormat(), gb->getWidth(), gb->getHeight(), index);
        ret = gb->unlock();
        if (ret)
            {
                  printf("unlock buffer_dst error : %s\n",strerror(errno));
                  return ret;
            }
    }
    return 0;
}
 
int main()
{
    int ret = 0;
    int srcWidth,srcHeight,srcFormat;
    int dstWidth,dstHeight,dstFormat;
    int lutWidth,lutHeight,lutFormat;
 
    sp<GraphicBuffer> src_buf;
    sp<GraphicBuffer> dst_buf;
    sp<GraphicBuffer> lut_buf;
 
#if !IS_IM2D_API
    rga_info_t src;
    rga_info_t dst;
    rga_info_t lut;
 
    RockchipRga& rkRga(RockchipRga::get());
#else
    rga_buffer_t imsrc;
    rga_buffer_t imdst;
    rga_buffer_t imlut;
#endif
 
   /********** SrcInfo set **********/
    srcWidth = 1280;
    srcHeight = 720;
    srcFormat = HAL_PIXEL_FORMAT_BPP_8;
 
    /********** DstInfo set **********/
    dstWidth = 1280;
    dstHeight = 720;
    dstFormat = HAL_PIXEL_FORMAT_RGBA_8888;
 
    /*
     * The minimum page size set by mmu is 64k,
     * and mmu will report an error if it is
     * smaller than this size. Actually only
     * use 256*1*4 byte.
     */
    /********** LutInfo set **********/
    lutWidth = 256;
    lutHeight = 64;
    lutFormat = HAL_PIXEL_FORMAT_RGBA_8888;
 
    /*********** Init GraphicBuffer ***********/
    src_buf = GraphicBuffer_Init(srcWidth, srcHeight, srcFormat);
    dst_buf = GraphicBuffer_Init(dstWidth, dstHeight, dstFormat);
    lut_buf = GraphicBuffer_Init(lutWidth, lutHeight, lutFormat);
    if (src_buf == NULL || dst_buf == NULL || lut_buf == NULL)
    {
        printf("GraphicBuff init error!\n");
        return -1;
    }
 
   /*********** fill or empty GraphicBuffer ***********/
    if(-1 == GraphicBuffer_Fill(src_buf, 0, 0))
    {
        printf("%s, src write Graphicbuffer error!\n", __FUNCTION__);
        return -1;
    }
    if(-1 == GraphicBuffer_Fill(dst_buf, 1, 0))
    {
        printf("%s, dst empty Graphicbuffer error!\n", __FUNCTION__);
        return -1;
    }
    if(-1 == GraphicBuffer_Fill_LUT(lut_buf, 1, 0))
    {
        printf("%s, lut table write Graphicbuffer error!\n", __FUNCTION__);
        return -1;
    }
 
   /*********** color palette ***********/
#if !IS_IM2D_API
    memset(&src, 0, sizeof(rga_info_t));
    memset(&dst, 0, sizeof(rga_info_t));
    memset(&lut, 0, sizeof(rga_info_t));
 
    rga_set_rect(&src.rect, 0,0,srcWidth,srcHeight,srcWidth/*stride*/,srcHeight,srcFormat);
    rga_set_rect(&dst.rect, 0, 0,srcWidth,srcHeight,dstWidth/*stride*/,dstHeight,dstFormat);
    rga_set_rect(&lut.rect, 0, 0,lutWidth,lutHeight,lutWidth/*stride*/,lutHeight,lutFormat);
 
    src.hnd = src_buf->handle;
    dst.hnd = dst_buf->handle;
    lut.hnd = lut_buf->handle;
 
    ret = rkRga.RkRgaCollorPalette(&src, &dst, &lut);
#else
    imsrc = wrapbuffer_GraphicBuffer(src_buf);
    imdst = wrapbuffer_GraphicBuffer(dst_buf);
    imlut = wrapbuffer_GraphicBuffer(lut_buf);
    if(imsrc.width == 0 || imdst.width == 0) {
        printf("%s, %s\n", __FUNCTION__, imStrError());
        return -1;
    }
 
    ret = impalette(imsrc, imdst, imlut);
    printf("paletting .... %s\n", imStrError(ret));
#endif
 
   /*********** Out to binary ***********/
    Out2Bin(lut_buf, 0);
    Out2Bin(dst_buf, 0);
}