summaryrefslogtreecommitdiffstats
path: root/exynos5/hal/libfimg4x/FimgExynos5.cpp
blob: 59d83946c9db25c932947dfeacfceb26ae0c65b9 (plain)
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
/*
**
** Copyright 2009 Samsung Electronics Co, Ltd.
**
** 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 ALOG_NDEBUG 0
#define ALOG_TAG "FimgExynos5"
#include <utils/Log.h>

#include "FimgExynos5.h"

namespace android
{
Mutex      FimgV4x::m_instanceLock;
int        FimgV4x::m_curFimgV4xIndex = 0;
int        FimgV4x::m_numOfInstance    = 0;
FimgApi *  FimgV4x::m_ptrFimgApiList[NUMBER_FIMG_LIST] = {NULL, };

//---------------------------------------------------------------------------//

FimgV4x::FimgV4x()
         : m_g2dFd(0),
           m_g2dVirtAddr(NULL),
           m_g2dSize(0),
           m_g2dSrcVirtAddr(NULL),
           m_g2dSrcSize(0),
           m_g2dDstVirtAddr(NULL),
           m_g2dDstSize(0)
{
    m_lock = new Mutex(Mutex::SHARED, "FimgV4x");
}

FimgV4x::~FimgV4x()
{
    delete m_lock;
}

FimgApi *FimgV4x::CreateInstance()
{
    Mutex::Autolock autolock(m_instanceLock);

    FimgApi *ptrFimg = NULL;

    for(int i = m_curFimgV4xIndex; i < NUMBER_FIMG_LIST; i++) {
        if (m_ptrFimgApiList[i] == NULL)
            m_ptrFimgApiList[i] = new FimgV4x;

        if (m_ptrFimgApiList[i]->FlagCreate() == false) {
            if (m_ptrFimgApiList[i]->Create() == false) {
                PRINT("%s::Create(%d) fail\n", __func__, i);
                goto CreateInstance_End;
            }
            else
                m_numOfInstance++;
        }

        if (i < NUMBER_FIMG_LIST - 1)
            m_curFimgV4xIndex = i + 1;
        else
            m_curFimgV4xIndex = 0;

        ptrFimg = m_ptrFimgApiList[i];
        goto CreateInstance_End;
    }

CreateInstance_End :

    return ptrFimg;
}

void FimgV4x::DestroyInstance(FimgApi * ptrFimgApi)
{
    Mutex::Autolock autolock(m_instanceLock);

    for(int i = 0; i < NUMBER_FIMG_LIST; i++) {
        if (m_ptrFimgApiList[i] != NULL && m_ptrFimgApiList[i] == ptrFimgApi) {
            if (m_ptrFimgApiList[i]->FlagCreate() == true && m_ptrFimgApiList[i]->Destroy() == false) {
                PRINT("%s::Destroy() fail\n", __func__);
            } else {
                FimgV4x * tempFimgV4x = (FimgV4x *)m_ptrFimgApiList[i];
                delete tempFimgV4x;
                m_ptrFimgApiList[i] = NULL;

                m_numOfInstance--;
            }

            break;
        }
    }
}

void FimgV4x::DestroyAllInstance(void)
{
    Mutex::Autolock autolock(m_instanceLock);

    for(int i = 0; i < NUMBER_FIMG_LIST; i++) {
        if (m_ptrFimgApiList[i] != NULL) {
            if (m_ptrFimgApiList[i]->FlagCreate() == true
               && m_ptrFimgApiList[i]->Destroy() == false) {
                    PRINT("%s::Destroy() fail\n", __func__);
            } else {
                FimgV4x * tempFimgV4x = (FimgV4x *)m_ptrFimgApiList[i];
                delete tempFimgV4x;
                m_ptrFimgApiList[i] = NULL;
            }
        }
    }
}

bool FimgV4x::t_Create(void)
{
    bool ret = true;

    if (m_CreateG2D() == false) {
        PRINT("%s::m_CreateG2D() fail \n", __func__);

        if (m_DestroyG2D() == false)
            PRINT("%s::m_DestroyG2D() fail \n", __func__);

        ret = false;
    }

    return ret;
}

bool FimgV4x::t_Destroy(void)
{
    bool ret = true;

    if (m_DestroyG2D() == false) {
        PRINT("%s::m_DestroyG2D() fail \n", __func__);
        ret = false;
    }

    return ret;
}

bool FimgV4x::t_Stretch(struct fimg2d_blit *cmd)
{
#ifdef CHECK_FIMGV4x_PERFORMANCE
#define NUM_OF_STEP (10)
    StopWatch   stopWatch("CHECK_FIMGV4x_PERFORMANCE");
    const char *stopWatchName[NUM_OF_STEP];
    nsecs_t     stopWatchTime[NUM_OF_STEP];
    int         stopWatchIndex = 0;
#endif // CHECK_FIMGV4x_PERFORMANCE

    if (m_DoG2D(cmd) == false) {
        goto STRETCH_FAIL;
    }

#ifdef G2D_NONE_BLOCKING_MODE
    if (m_PollG2D(&m_g2dPoll) == false)
    {
        PRINT("%s::m_PollG2D() fail\n", __func__);
        goto STRETCH_FAIL;
    }
#endif

    #ifdef CHECK_FIMGV4x_PERFORMANCE
        m_PrintFimgV4xPerformance(src, dst, stopWatchIndex, stopWatchName, stopWatchTime);
    #endif // CHECK_FIMGV4x_PERFORMANCE

    return true;

STRETCH_FAIL:
    return false;

}

bool FimgV4x::t_Sync(void)
{
    if (m_PollG2D(&m_g2dPoll) == false)
    {
        PRINT("%s::m_PollG2D() fail\n", __func__);
        goto SYNC_FAIL;
    }
    return true;

SYNC_FAIL:
    return false;

}

bool FimgV4x::t_Lock(void)
{
    m_lock->lock();
    return true;
}

bool FimgV4x::t_UnLock(void)
{
    m_lock->unlock();
    return true;
}

bool FimgV4x::m_CreateG2D(void)
{
    void * mmap_base;

    if (m_g2dFd != 0) {
        PRINT("%s::m_g2dFd(%d) is not 0 fail\n", __func__, m_g2dFd);
        return false;
    }

#ifdef G2D_NONE_BLOCKING_MODE
    m_g2dFd = open(SEC_G2D_DEV_NAME, O_RDWR | O_NONBLOCK);
#else
    m_g2dFd = open(SEC_G2D_DEV_NAME, O_RDWR);
#endif
    if (m_g2dFd < 0) {
        PRINT("%s::open(%s) fail(%s)\n", __func__, SEC_G2D_DEV_NAME, strerror(errno));
        m_g2dFd = 0;
        return false;
    }

    memset(&m_g2dPoll, 0, sizeof(m_g2dPoll));
    m_g2dPoll.fd     = m_g2dFd;
    m_g2dPoll.events = POLLOUT | POLLERR;

    return true;
}

bool FimgV4x::m_DestroyG2D(void)
{
    if (m_g2dVirtAddr != NULL) {
        munmap(m_g2dVirtAddr, m_g2dSize);
        m_g2dVirtAddr = NULL;
        m_g2dSize = 0;
    }

    if (0 < m_g2dFd) {
        close(m_g2dFd);
    }
    m_g2dFd = 0;

    return true;
}

bool FimgV4x::m_DoG2D(struct fimg2d_blit *cmd)
{

    if (ioctl(m_g2dFd, FIMG2D_BITBLT_BLIT, cmd) < 0)
        return false;

    return true;
}

inline bool FimgV4x::m_PollG2D(struct pollfd * events)
{
#define G2D_POLL_TIME (1000)

    int ret;

    ret = poll(events, 1, G2D_POLL_TIME);

    if (ret < 0) {
        PRINT("%s::poll fail \n", __func__);
        return false;
    }
    else if (ret == 0) {
        PRINT("%s::No data in %d milli secs..\n", __func__, G2D_POLL_TIME);
        return false;
    }

    return true;
}

//---------------------------------------------------------------------------//
// extern function
//---------------------------------------------------------------------------//
extern "C" struct FimgApi * createFimgApi()
{
    if (fimgApiAutoFreeThread == 0)
        fimgApiAutoFreeThread = new FimgApiAutoFreeThread();
    else
        fimgApiAutoFreeThread->SetOneMoreSleep();

    return FimgV4x::CreateInstance();
}

extern "C" void destroyFimgApi(FimgApi * ptrFimgApi)
{
    // Dont' call DestroyInstance.
}

}; // namespace android