hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
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
#include "RKISP2FecUnit.h"
 
#include <dlfcn.h>
#include <drm_fourcc.h>
#include <stdio.h>
#include <sys/time.h>
 
#include <string>
 
#include "rt_log.h"
 
#define ALIGN(value, align) ((value + align - 1) & ~(align - 1))
#if 0
#if defined(__LP64__)
#define RK_XXX_PATH "/vendor/lib64/libdistortion_gl.so"
#else
#define RK_XXX_PATH "/vendor/lib/libdistortion_gl.so"
#endif
#else
#define RK_XXX_PATH "/oem/usr/lib/libdistortion.so"
#endif
#define LOGE RT_LOGE
#if 0
RKISP2FecUnit *RKISP2FecUnit::mInstance = nullptr;
 
RKISP2FecUnit *RKISP2FecUnit::getInstance() {
  if (mInstance == nullptr) {
    mInstance = new RKISP2FecUnit();
  }
  return mInstance;
}
#endif
 
static int64_t getNowUsTime() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000000.0 + tv.tv_usec;
}
 
RKISP2FecUnit::RKISP2FecUnit()
    : createFenceFd(nullptr), dso(nullptr), done_init(0),
      distortionByGpuInit(nullptr), distortionByGpuDeinit(nullptr),
      distortionByGpuProcess(nullptr), distortionSyncFenceFd(nullptr),
      waitFencfd(nullptr) {
  loadDistortionGlLibray();
}
 
RKISP2FecUnit::~RKISP2FecUnit() {
  distortionDeinit();
  if (dso)
    dlclose(dso);
#if 0
  if (mInstance != nullptr) {
    delete (mInstance);
    mInstance = nullptr;
  }
#endif
}
 
void RKISP2FecUnit::loadDistortionGlLibray() {
  if (dso == NULL) {
    dso = dlopen(RK_XXX_PATH, RTLD_NOW| RTLD_GLOBAL);
  }
  RT_LOGE("rk-debug[%s %d]  dso=%p", __FUNCTION__, __LINE__, dso);
  if (dso == 0) {
    LOGE("rk-debug can't not find %s ! error=%s ", RK_XXX_PATH, dlerror());
    return;
  }
  createGLClass = NULL;
  if (createGLClass == NULL)
    createGLClass = (__createGLClass)dlsym(dso, "createGLContext");
  if (createGLClass == NULL) {
    LOGE("rk_debug can't not find CreateGLClass function in "
         "/system/lib64/libdistortion_gl.so !");
    goto err;
  }
  if (distortionByGpuInit == NULL)
    distortionByGpuInit =
        (__distortionByGpuInit)dlsym(dso, "distortionByGpuInit");
  if (distortionByGpuInit == NULL) {
    LOGE("rk_debug can't not find distortionByGpuInit function in "
         "/system/lib64/libdistortion_gl.so !");
    goto err;
  }
 
  if (distortionByGpuProcess == NULL)
    distortionByGpuProcess =
        (__distortionByGpuProcess)dlsym(dso, "distortionByGpuProcess");
  if (distortionByGpuProcess == NULL) {
    LOGE("rk_debug can't not find distortionByGpuProcess function in "
         "/system/lib64/libdistortion_gl.so !");
    goto err;
  }
  if (distortionByGpuDeinit == NULL)
    distortionByGpuDeinit =
        (__distortionByGpuDeinit)dlsym(dso, "distortionByGpuDeinit");
  if (distortionByGpuDeinit == NULL) {
    LOGE("rk_debug can't not find distortionByGpuDeinit function in "
         "/system/lib64/libdistortion_gl.so !");
    goto err;
  }
  if (createFenceFd == NULL)
    createFenceFd = (__createFenceFd)dlsym(dso, "distortioncreateFence");
  if (createFenceFd == NULL) {
    LOGE("rk_debug can't not find createFencefd function in "
         "/system/lib64/libdistortion_gl.so !");
    goto err;
  }
  if (waitFencfd == NULL)
    waitFencfd = (__waitFence)dlsym(dso, "distortionWaitFence");
  if (waitFencfd == NULL) {
    LOGE("rk_debug can't not find waitFencfd function in "
         "/system/lib64/libdistortion_gl.so !");
    goto err;
  }
  return;
err:
  dlclose(dso);
}
 
void RKISP2FecUnit::calculateMeshGridSize(int width, int height, int &meshW,
                                          int &meshH) {
  int meshStepW, meshStepH;
  int alignW = ALIGN(width, 32);
  int alignH = ALIGN(height, 32);
 
  if (width <= 1920) {
    meshStepW = 16;
    meshStepH = 8;
  } else {
    meshStepW = 32;
    meshStepH = 16;
  }
 
  meshW = (alignW + meshStepW - 1) / meshStepW + 1;
  meshH = (alignH + meshStepH - 1) / meshStepH + 1;
  LOGE("meshW=%d meshH=%d  alignw=%d alignh=%d", meshW, meshH, alignW, alignH);
}
 
int RKISP2FecUnit::distortionInit(int width, int height) {
  std::unique_lock<std::mutex> lk(mtx);
  int success = 0;
  if (!done_init && distortionByGpuInit) {
    uint32_t w = (width & 0xffff) << 16 | 3840;
    uint32_t h = (height & 0xffff) << 16 | 2160;
    int meshGridW = 0, meshGridH = 0;
 
    done_init = 1;
    calculateMeshGridSize(width, height, meshGridW, meshGridH);
    glClass = createGLClass();
    if (glClass != nullptr) {
      success = distortionByGpuInit(glClass, w, h, meshGridW, meshGridH);
      LOGE("rk-debug: glclass = %p meshGridW=%d meshGridH=%d", glClass,
           meshGridW, meshGridH);
    } else {
      success = -1;
    }
  }
  return success;
}
 
int RKISP2FecUnit::distortionDeinit() {
  std::unique_lock<std::mutex> lk(mtx);
  int success = 0;
  if (distortionByGpuDeinit && done_init)
    success = distortionByGpuDeinit(glClass);
  done_init = 0;
  return success;
}
 
int RKISP2FecUnit::doFecProcess(int inW, int inH, int inFd, int inFormat,
                                int outW, int outH, int outFd, int outFormat,
                                int &fenceFd) {
#if 1
  if (distortionByGpuProcess) {
    void *gpu_fence_fd = NULL;
    distortionByGpuProcess(glClass, inFd, inW, inH, inFormat, outFd, outW, outH,
                           outFormat, 0);
#if 0
    if (fenceFd > 0) {
        int retireFenceFd = sync_merge("fec_and_camera", fenceFd, gpu_fence_fd);
        close(gpu_fence_fd);
        close(fenceFd);
        fenceFd = retireFenceFd
    }
#endif
    if (createFenceFd)
      gpu_fence_fd = createFenceFd(glClass);
    if (waitFencfd)
      waitFencfd(glClass, gpu_fence_fd, 0);
  }
#else
  RT_LOGE("waitFencefd");
  if (waitFencfd)
    waitFencfd(NULL, NULL, 0);
#endif
  return 0;
}