aboutsummaryrefslogtreecommitdiff
path: root/evs/app/RenderPixelCopy.cpp
blob: 0a586a4b42eb93771cdf729f48bf5fc3e1692cb9 (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
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * 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.
 */

#include "RenderPixelCopy.h"
#include "FormatConvert.h"

#include <log/log.h>


RenderPixelCopy::RenderPixelCopy(sp<IEvsEnumerator> enumerator,
                                   const ConfigManager::CameraInfo& cam) {
    mEnumerator = enumerator;
    mCameraInfo = cam;
}


bool RenderPixelCopy::activate() {
    // Set up the camera to feed this texture
    sp<IEvsCamera> pCamera = mEnumerator->openCamera(mCameraInfo.cameraId.c_str());
    if (pCamera.get() == nullptr) {
        ALOGE("Failed to allocate new EVS Camera interface");
        return false;
    }

    // Initialize the stream that will help us update this texture's contents
    sp<StreamHandler> pStreamHandler = new StreamHandler(pCamera);
    if (pStreamHandler.get() == nullptr) {
        ALOGE("failed to allocate FrameHandler");
        return false;
    }

    // Start the video stream
    if (!pStreamHandler->startStream()) {
        ALOGE("start stream failed");
        return false;
    }

    mStreamHandler = pStreamHandler;

    return true;
}


void RenderPixelCopy::deactivate() {
    mStreamHandler = nullptr;
}


bool RenderPixelCopy::drawFrame(const BufferDesc& tgtBuffer) {
    bool success = true;

    sp<android::GraphicBuffer> tgt = new android::GraphicBuffer(
            tgtBuffer.memHandle, android::GraphicBuffer::CLONE_HANDLE,
            tgtBuffer.width, tgtBuffer.height, tgtBuffer.format, 1, tgtBuffer.usage,
            tgtBuffer.stride);

    // Lock our target buffer for writing (should be RGBA8888 format)
    uint32_t* tgtPixels = nullptr;
    tgt->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&tgtPixels);

    if (tgtPixels) {
        if (tgtBuffer.format != HAL_PIXEL_FORMAT_RGBA_8888) {
            // We always expect 32 bit RGB for the display output for now.  Is there a need for 565?
            ALOGE("Diplay buffer is always expected to be 32bit RGBA");
            success = false;
        } else {
            // Make sure we have the latest frame data
            if (mStreamHandler->newFrameAvailable()) {
                const BufferDesc& srcBuffer = mStreamHandler->getNewFrame();

                // Lock our source buffer for reading (current expectation are for this to be NV21 format)
                sp<android::GraphicBuffer> src = new android::GraphicBuffer(
                        srcBuffer.memHandle, android::GraphicBuffer::CLONE_HANDLE,
                        srcBuffer.width, srcBuffer.height, srcBuffer.format, 1, srcBuffer.usage,
                        srcBuffer.stride);
                unsigned char* srcPixels = nullptr;
                src->lock(GRALLOC_USAGE_SW_READ_OFTEN, (void**)&srcPixels);
                if (!srcPixels) {
                    ALOGE("Failed to get pointer into src image data");
                }

                // Make sure we don't run off the end of either buffer
                const unsigned width     = std::min(tgtBuffer.width,
                                                    srcBuffer.width);
                const unsigned height    = std::min(tgtBuffer.height,
                                                    srcBuffer.height);

                if (srcBuffer.format == HAL_PIXEL_FORMAT_YCRCB_420_SP) {   // 420SP == NV21
                    copyNV21toRGB32(width, height,
                                    srcPixels,
                                    tgtPixels, tgtBuffer.stride);
                } else if (srcBuffer.format == HAL_PIXEL_FORMAT_YV12) { // YUV_420P == YV12
                    copyYV12toRGB32(width, height,
                                    srcPixels,
                                    tgtPixels, tgtBuffer.stride);
                } else if (srcBuffer.format == HAL_PIXEL_FORMAT_YCBCR_422_I) { // YUYV
                    copyYUYVtoRGB32(width, height,
                                    srcPixels, srcBuffer.stride,
                                    tgtPixels, tgtBuffer.stride);
                } else if (srcBuffer.format == tgtBuffer.format) {  // 32bit RGBA
                    copyMatchedInterleavedFormats(width, height,
                                                  srcPixels, srcBuffer.stride,
                                                  tgtPixels, tgtBuffer.stride,
                                                  tgtBuffer.pixelSize);
                }

                mStreamHandler->doneWithFrame(srcBuffer);
            }
        }
    } else {
        ALOGE("Failed to lock buffer contents for contents transfer");
        success = false;
    }

    if (tgtPixels) {
        tgt->unlock();
    }

    return success;
}