Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2017 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 #include "RenderPixelCopy.h"
     18 #include "FormatConvert.h"
     19 
     20 #include <log/log.h>
     21 
     22 
     23 RenderPixelCopy::RenderPixelCopy(sp<IEvsEnumerator> enumerator,
     24                                    const ConfigManager::CameraInfo& cam) {
     25     mEnumerator = enumerator;
     26     mCameraInfo = cam;
     27 }
     28 
     29 
     30 bool RenderPixelCopy::activate() {
     31     // Set up the camera to feed this texture
     32     sp<IEvsCamera> pCamera = mEnumerator->openCamera(mCameraInfo.cameraId.c_str());
     33     if (pCamera.get() == nullptr) {
     34         ALOGE("Failed to allocate new EVS Camera interface");
     35         return false;
     36     }
     37 
     38     // Initialize the stream that will help us update this texture's contents
     39     sp<StreamHandler> pStreamHandler = new StreamHandler(pCamera);
     40     if (pStreamHandler.get() == nullptr) {
     41         ALOGE("failed to allocate FrameHandler");
     42         return false;
     43     }
     44 
     45     // Start the video stream
     46     if (!pStreamHandler->startStream()) {
     47         ALOGE("start stream failed");
     48         return false;
     49     }
     50 
     51     mStreamHandler = pStreamHandler;
     52 
     53     return true;
     54 }
     55 
     56 
     57 void RenderPixelCopy::deactivate() {
     58     mStreamHandler = nullptr;
     59 }
     60 
     61 
     62 bool RenderPixelCopy::drawFrame(const BufferDesc& tgtBuffer) {
     63     bool success = true;
     64 
     65     sp<android::GraphicBuffer> tgt = new android::GraphicBuffer(
     66             tgtBuffer.memHandle, android::GraphicBuffer::CLONE_HANDLE,
     67             tgtBuffer.width, tgtBuffer.height, tgtBuffer.format, 1, tgtBuffer.usage,
     68             tgtBuffer.stride);
     69 
     70     // Lock our target buffer for writing (should be RGBA8888 format)
     71     uint32_t* tgtPixels = nullptr;
     72     tgt->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&tgtPixels);
     73 
     74     if (tgtPixels) {
     75         if (tgtBuffer.format != HAL_PIXEL_FORMAT_RGBA_8888) {
     76             // We always expect 32 bit RGB for the display output for now.  Is there a need for 565?
     77             ALOGE("Diplay buffer is always expected to be 32bit RGBA");
     78             success = false;
     79         } else {
     80             // Make sure we have the latest frame data
     81             if (mStreamHandler->newFrameAvailable()) {
     82                 const BufferDesc& srcBuffer = mStreamHandler->getNewFrame();
     83 
     84                 // Lock our source buffer for reading (current expectation are for this to be NV21 format)
     85                 sp<android::GraphicBuffer> src = new android::GraphicBuffer(
     86                         srcBuffer.memHandle, android::GraphicBuffer::CLONE_HANDLE,
     87                         srcBuffer.width, srcBuffer.height, srcBuffer.format, 1, srcBuffer.usage,
     88                         srcBuffer.stride);
     89                 unsigned char* srcPixels = nullptr;
     90                 src->lock(GRALLOC_USAGE_SW_READ_OFTEN, (void**)&srcPixels);
     91                 if (!srcPixels) {
     92                     ALOGE("Failed to get pointer into src image data");
     93                 }
     94 
     95                 // Make sure we don't run off the end of either buffer
     96                 const unsigned width     = std::min(tgtBuffer.width,
     97                                                     srcBuffer.width);
     98                 const unsigned height    = std::min(tgtBuffer.height,
     99                                                     srcBuffer.height);
    100 
    101                 if (srcBuffer.format == HAL_PIXEL_FORMAT_YCRCB_420_SP) {   // 420SP == NV21
    102                     copyNV21toRGB32(width, height,
    103                                     srcPixels,
    104                                     tgtPixels, tgtBuffer.stride);
    105                 } else if (srcBuffer.format == HAL_PIXEL_FORMAT_YV12) { // YUV_420P == YV12
    106                     copyYV12toRGB32(width, height,
    107                                     srcPixels,
    108                                     tgtPixels, tgtBuffer.stride);
    109                 } else if (srcBuffer.format == HAL_PIXEL_FORMAT_YCBCR_422_I) { // YUYV
    110                     copyYUYVtoRGB32(width, height,
    111                                     srcPixels, srcBuffer.stride,
    112                                     tgtPixels, tgtBuffer.stride);
    113                 } else if (srcBuffer.format == tgtBuffer.format) {  // 32bit RGBA
    114                     copyMatchedInterleavedFormats(width, height,
    115                                                   srcPixels, srcBuffer.stride,
    116                                                   tgtPixels, tgtBuffer.stride,
    117                                                   tgtBuffer.pixelSize);
    118                 }
    119 
    120                 mStreamHandler->doneWithFrame(srcBuffer);
    121             }
    122         }
    123     } else {
    124         ALOGE("Failed to lock buffer contents for contents transfer");
    125         success = false;
    126     }
    127 
    128     if (tgtPixels) {
    129         tgt->unlock();
    130     }
    131 
    132     return success;
    133 }
    134