1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Contains implementation of a class EmulatedFakeCameraDevice that encapsulates
19 * fake camera device.
20 */
21
22#define LOG_NDEBUG 0
23#define LOG_TAG "EmulatedCamera_FakeDevice"
24#include <cutils/log.h>
25#include "EmulatedFakeCamera.h"
26#include "EmulatedFakeCameraDevice.h"
27
28namespace android {
29
30EmulatedFakeCameraDevice::EmulatedFakeCameraDevice(EmulatedFakeCamera* camera_hal)
31    : EmulatedCameraDevice(camera_hal),
32      mBlackYUV(kBlack32),
33      mWhiteYUV(kWhite32),
34      mRedYUV(kRed8),
35      mGreenYUV(kGreen8),
36      mBlueYUV(kBlue8),
37      mLastRedrawn(0),
38      mCheckX(0),
39      mCheckY(0),
40      mCcounter(0)
41#if EFCD_ROTATE_FRAME
42      , mLastRotatedAt(0),
43        mCurrentFrameType(0),
44        mCurrentColor(&mWhiteYUV)
45#endif  // EFCD_ROTATE_FRAME
46{
47    // Makes the image with the original exposure compensation darker.
48    // So the effects of changing the exposure compensation can be seen.
49    mBlackYUV.Y = mBlackYUV.Y / 4;
50    mWhiteYUV.Y = mWhiteYUV.Y / 4;
51    mRedYUV.Y = mRedYUV.Y / 4;
52    mGreenYUV.Y = mGreenYUV.Y / 4;
53    mBlueYUV.Y = mBlueYUV.Y / 4;
54}
55
56EmulatedFakeCameraDevice::~EmulatedFakeCameraDevice()
57{
58}
59
60/****************************************************************************
61 * Emulated camera device abstract interface implementation.
62 ***************************************************************************/
63
64status_t EmulatedFakeCameraDevice::connectDevice()
65{
66    LOGV("%s", __FUNCTION__);
67
68    Mutex::Autolock locker(&mObjectLock);
69    if (!isInitialized()) {
70        LOGE("%s: Fake camera device is not initialized.", __FUNCTION__);
71        return EINVAL;
72    }
73    if (isConnected()) {
74        LOGW("%s: Fake camera device is already connected.", __FUNCTION__);
75        return NO_ERROR;
76    }
77
78    /* There is no device to connect to. */
79    mState = ECDS_CONNECTED;
80
81    return NO_ERROR;
82}
83
84status_t EmulatedFakeCameraDevice::disconnectDevice()
85{
86    LOGV("%s", __FUNCTION__);
87
88    Mutex::Autolock locker(&mObjectLock);
89    if (!isConnected()) {
90        LOGW("%s: Fake camera device is already disconnected.", __FUNCTION__);
91        return NO_ERROR;
92    }
93    if (isStarted()) {
94        LOGE("%s: Cannot disconnect from the started device.", __FUNCTION__);
95        return EINVAL;
96    }
97
98    /* There is no device to disconnect from. */
99    mState = ECDS_INITIALIZED;
100
101    return NO_ERROR;
102}
103
104status_t EmulatedFakeCameraDevice::startDevice(int width,
105                                               int height,
106                                               uint32_t pix_fmt)
107{
108    LOGV("%s", __FUNCTION__);
109
110    Mutex::Autolock locker(&mObjectLock);
111    if (!isConnected()) {
112        LOGE("%s: Fake camera device is not connected.", __FUNCTION__);
113        return EINVAL;
114    }
115    if (isStarted()) {
116        LOGE("%s: Fake camera device is already started.", __FUNCTION__);
117        return EINVAL;
118    }
119
120    /* Initialize the base class. */
121    const status_t res =
122        EmulatedCameraDevice::commonStartDevice(width, height, pix_fmt);
123    if (res == NO_ERROR) {
124        /* Calculate U/V panes inside the framebuffer. */
125        switch (mPixelFormat) {
126            case V4L2_PIX_FMT_YVU420:
127                mFrameV = mCurrentFrame + mTotalPixels;
128                mFrameU = mFrameU + mTotalPixels / 4;
129                mUVStep = 1;
130                mUVTotalNum = mTotalPixels / 4;
131                break;
132
133            case V4L2_PIX_FMT_YUV420:
134                mFrameU = mCurrentFrame + mTotalPixels;
135                mFrameV = mFrameU + mTotalPixels / 4;
136                mUVStep = 1;
137                mUVTotalNum = mTotalPixels / 4;
138                break;
139
140            case V4L2_PIX_FMT_NV21:
141                /* Interleaved UV pane, V first. */
142                mFrameV = mCurrentFrame + mTotalPixels;
143                mFrameU = mFrameV + 1;
144                mUVStep = 2;
145                mUVTotalNum = mTotalPixels / 4;
146                break;
147
148            case V4L2_PIX_FMT_NV12:
149                /* Interleaved UV pane, U first. */
150                mFrameU = mCurrentFrame + mTotalPixels;
151                mFrameV = mFrameU + 1;
152                mUVStep = 2;
153                mUVTotalNum = mTotalPixels / 4;
154                break;
155
156            default:
157                LOGE("%s: Unknown pixel format %.4s", __FUNCTION__,
158                     reinterpret_cast<const char*>(&mPixelFormat));
159                return EINVAL;
160        }
161        /* Number of items in a single row inside U/V panes. */
162        mUVInRow = (width / 2) * mUVStep;
163        mState = ECDS_STARTED;
164    } else {
165        LOGE("%s: commonStartDevice failed", __FUNCTION__);
166    }
167
168    return res;
169}
170
171status_t EmulatedFakeCameraDevice::stopDevice()
172{
173    LOGV("%s", __FUNCTION__);
174
175    Mutex::Autolock locker(&mObjectLock);
176    if (!isStarted()) {
177        LOGW("%s: Fake camera device is not started.", __FUNCTION__);
178        return NO_ERROR;
179    }
180
181    mFrameU = mFrameV = NULL;
182    EmulatedCameraDevice::commonStopDevice();
183    mState = ECDS_CONNECTED;
184
185    return NO_ERROR;
186}
187
188/****************************************************************************
189 * Worker thread management overrides.
190 ***************************************************************************/
191
192bool EmulatedFakeCameraDevice::inWorkerThread()
193{
194    /* Wait till FPS timeout expires, or thread exit message is received. */
195    WorkerThread::SelectRes res =
196        getWorkerThread()->Select(-1, 1000000 / mEmulatedFPS);
197    if (res == WorkerThread::EXIT_THREAD) {
198        LOGV("%s: Worker thread has been terminated.", __FUNCTION__);
199        return false;
200    }
201
202    /* Lets see if we need to generate a new frame. */
203    if ((systemTime(SYSTEM_TIME_MONOTONIC) - mLastRedrawn) >= mRedrawAfter) {
204        /*
205         * Time to generate a new frame.
206         */
207
208#if EFCD_ROTATE_FRAME
209        const int frame_type = rotateFrame();
210        switch (frame_type) {
211            case 0:
212                drawCheckerboard();
213                break;
214            case 1:
215                drawStripes();
216                break;
217            case 2:
218                drawSolid(mCurrentColor);
219                break;
220        }
221#else
222        /* Draw the checker board. */
223        drawCheckerboard();
224
225#endif  // EFCD_ROTATE_FRAME
226
227        mLastRedrawn = systemTime(SYSTEM_TIME_MONOTONIC);
228    }
229
230    /* Timestamp the current frame, and notify the camera HAL about new frame. */
231    mCurFrameTimestamp = systemTime(SYSTEM_TIME_MONOTONIC);
232    mCameraHAL->onNextFrameAvailable(mCurrentFrame, mCurFrameTimestamp, this);
233
234    return true;
235}
236
237/****************************************************************************
238 * Fake camera device private API
239 ***************************************************************************/
240
241void EmulatedFakeCameraDevice::drawCheckerboard()
242{
243    const int size = mFrameWidth / 10;
244    bool black = true;
245
246    if((mCheckX / size) & 1)
247        black = false;
248    if((mCheckY / size) & 1)
249        black = !black;
250
251    int county = mCheckY % size;
252    int checkxremainder = mCheckX % size;
253    uint8_t* Y = mCurrentFrame;
254    uint8_t* U_pos = mFrameU;
255    uint8_t* V_pos = mFrameV;
256    uint8_t* U = U_pos;
257    uint8_t* V = V_pos;
258
259    for(int y = 0; y < mFrameHeight; y++) {
260        int countx = checkxremainder;
261        bool current = black;
262        for(int x = 0; x < mFrameWidth; x += 2) {
263            if (current) {
264                mBlackYUV.get(Y, U, V);
265            } else {
266                mWhiteYUV.get(Y, U, V);
267            }
268            *Y = changeExposure(*Y);
269            Y[1] = *Y;
270            Y += 2; U += mUVStep; V += mUVStep;
271            countx += 2;
272            if(countx >= size) {
273                countx = 0;
274                current = !current;
275            }
276        }
277        if (y & 0x1) {
278            U_pos = U;
279            V_pos = V;
280        } else {
281            U = U_pos;
282            V = V_pos;
283        }
284        if(county++ >= size) {
285            county = 0;
286            black = !black;
287        }
288    }
289    mCheckX += 3;
290    mCheckY++;
291
292    /* Run the square. */
293    int sqx = ((mCcounter * 3) & 255);
294    if(sqx > 128) sqx = 255 - sqx;
295    int sqy = ((mCcounter * 5) & 255);
296    if(sqy > 128) sqy = 255 - sqy;
297    const int sqsize = mFrameWidth / 10;
298    drawSquare(sqx * sqsize / 32, sqy * sqsize / 32, (sqsize * 5) >> 1,
299               (mCcounter & 0x100) ? &mRedYUV : &mGreenYUV);
300    mCcounter++;
301}
302
303void EmulatedFakeCameraDevice::drawSquare(int x,
304                                          int y,
305                                          int size,
306                                          const YUVPixel* color)
307{
308    const int square_xstop = min(mFrameWidth, x + size);
309    const int square_ystop = min(mFrameHeight, y + size);
310    uint8_t* Y_pos = mCurrentFrame + y * mFrameWidth + x;
311
312    // Draw the square.
313    for (; y < square_ystop; y++) {
314        const int iUV = (y / 2) * mUVInRow + (x / 2) * mUVStep;
315        uint8_t* sqU = mFrameU + iUV;
316        uint8_t* sqV = mFrameV + iUV;
317        uint8_t* sqY = Y_pos;
318        for (int i = x; i < square_xstop; i += 2) {
319            color->get(sqY, sqU, sqV);
320            *sqY = changeExposure(*sqY);
321            sqY[1] = *sqY;
322            sqY += 2; sqU += mUVStep; sqV += mUVStep;
323        }
324        Y_pos += mFrameWidth;
325    }
326}
327
328#if EFCD_ROTATE_FRAME
329
330void EmulatedFakeCameraDevice::drawSolid(YUVPixel* color)
331{
332    /* All Ys are the same. */
333    memset(mCurrentFrame, changeExposure(color->Y), mTotalPixels);
334
335    /* Fill U, and V panes. */
336    uint8_t* U = mFrameU;
337    uint8_t* V = mFrameV;
338    for (int k = 0; k < mUVTotalNum; k++, U += mUVStep, V += mUVStep) {
339        *U = color->U;
340        *V = color->V;
341    }
342}
343
344void EmulatedFakeCameraDevice::drawStripes()
345{
346    /* Divide frame into 4 stripes. */
347    const int change_color_at = mFrameHeight / 4;
348    const int each_in_row = mUVInRow / mUVStep;
349    uint8_t* pY = mCurrentFrame;
350    for (int y = 0; y < mFrameHeight; y++, pY += mFrameWidth) {
351        /* Select the color. */
352        YUVPixel* color;
353        const int color_index = y / change_color_at;
354        if (color_index == 0) {
355            /* White stripe on top. */
356            color = &mWhiteYUV;
357        } else if (color_index == 1) {
358            /* Then the red stripe. */
359            color = &mRedYUV;
360        } else if (color_index == 2) {
361            /* Then the green stripe. */
362            color = &mGreenYUV;
363        } else {
364            /* And the blue stripe at the bottom. */
365            color = &mBlueYUV;
366        }
367
368        /* All Ys at the row are the same. */
369        memset(pY, changeExposure(color->Y), mFrameWidth);
370
371        /* Offset of the current row inside U/V panes. */
372        const int uv_off = (y / 2) * mUVInRow;
373        /* Fill U, and V panes. */
374        uint8_t* U = mFrameU + uv_off;
375        uint8_t* V = mFrameV + uv_off;
376        for (int k = 0; k < each_in_row; k++, U += mUVStep, V += mUVStep) {
377            *U = color->U;
378            *V = color->V;
379        }
380    }
381}
382
383int EmulatedFakeCameraDevice::rotateFrame()
384{
385    if ((systemTime(SYSTEM_TIME_MONOTONIC) - mLastRotatedAt) >= mRotateFreq) {
386        mLastRotatedAt = systemTime(SYSTEM_TIME_MONOTONIC);
387        mCurrentFrameType++;
388        if (mCurrentFrameType > 2) {
389            mCurrentFrameType = 0;
390        }
391        if (mCurrentFrameType == 2) {
392            LOGD("********** Rotated to the SOLID COLOR frame **********");
393            /* Solid color: lets rotate color too. */
394            if (mCurrentColor == &mWhiteYUV) {
395                LOGD("----- Painting a solid RED frame -----");
396                mCurrentColor = &mRedYUV;
397            } else if (mCurrentColor == &mRedYUV) {
398                LOGD("----- Painting a solid GREEN frame -----");
399                mCurrentColor = &mGreenYUV;
400            } else if (mCurrentColor == &mGreenYUV) {
401                LOGD("----- Painting a solid BLUE frame -----");
402                mCurrentColor = &mBlueYUV;
403            } else {
404                /* Back to white. */
405                LOGD("----- Painting a solid WHITE frame -----");
406                mCurrentColor = &mWhiteYUV;
407            }
408        } else if (mCurrentFrameType == 0) {
409            LOGD("********** Rotated to the CHECKERBOARD frame **********");
410        } else {
411            LOGD("********** Rotated to the STRIPED frame **********");
412        }
413    }
414
415    return mCurrentFrameType;
416}
417
418#endif  // EFCD_ROTATE_FRAME
419
420}; /* namespace android */
421