Home | History | Annotate | Download | only in lvpp
      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 #define LOG_NDEBUG 1
     19 #define LOG_TAG "PreviewRenderer"
     20 #include <utils/Log.h>
     21 
     22 #include "PreviewRenderer.h"
     23 
     24 #include <media/stagefright/foundation/ADebug.h>
     25 #include <gui/Surface.h>
     26 
     27 namespace android {
     28 
     29 PreviewRenderer* PreviewRenderer::CreatePreviewRenderer (
     30         const sp<Surface> &surface, size_t width, size_t height) {
     31 
     32     PreviewRenderer* renderer = new PreviewRenderer(surface, width, height);
     33 
     34     if (renderer->init() != 0) {
     35         delete renderer;
     36         return NULL;
     37     }
     38 
     39     return renderer;
     40 }
     41 
     42 PreviewRenderer::PreviewRenderer(
     43         const sp<Surface> &surface,
     44         size_t width, size_t height)
     45     : mSurface(surface),
     46       mWidth(width),
     47       mHeight(height) {
     48 }
     49 
     50 int PreviewRenderer::init() {
     51     int err = 0;
     52     ANativeWindow* anw = mSurface.get();
     53 
     54     err = native_window_api_connect(anw, NATIVE_WINDOW_API_CPU);
     55     if (err) goto fail;
     56 
     57     err = native_window_set_usage(
     58             anw, GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN);
     59     if (err) goto fail;
     60 
     61     err = native_window_set_buffer_count(anw, 3);
     62     if (err) goto fail;
     63 
     64     err = native_window_set_scaling_mode(
     65             anw, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
     66     if (err) goto fail;
     67 
     68     err = native_window_set_buffers_geometry(
     69             anw, mWidth, mHeight, HAL_PIXEL_FORMAT_YV12);
     70     if (err) goto fail;
     71 
     72     err = native_window_set_buffers_transform(anw, 0);
     73     if (err) goto fail;
     74 
     75 fail:
     76     return err;
     77 }
     78 
     79 PreviewRenderer::~PreviewRenderer() {
     80     native_window_api_disconnect(mSurface.get(), NATIVE_WINDOW_API_CPU);
     81 }
     82 
     83 
     84 //
     85 // Provides a buffer and associated stride
     86 // This buffer is allocated by the SurfaceFlinger
     87 //
     88 // For optimal display performances, you should :
     89 // 1) call getBufferYV12()
     90 // 2) fill the buffer with your data
     91 // 3) call renderYV12() to take these changes into account
     92 //
     93 // For each call to getBufferYV12(), you must also call renderYV12()
     94 // Expected format in the buffer is YV12 formats (similar to YUV420 planar fromat)
     95 // for more details on this YV12 cf hardware/libhardware/include/hardware/hardware.h
     96 //
     97 void PreviewRenderer::getBufferYV12(uint8_t **data, size_t *stride) {
     98     int err = OK;
     99 
    100     if ((err = native_window_dequeue_buffer_and_wait(mSurface.get(),
    101             &mBuf)) != 0) {
    102         ALOGW("native_window_dequeue_buffer_and_wait returned error %d", err);
    103         return;
    104     }
    105 
    106     GraphicBufferMapper &mapper = GraphicBufferMapper::get();
    107 
    108     Rect bounds(mWidth, mHeight);
    109 
    110     void *dst;
    111     CHECK_EQ(0, mapper.lock(mBuf->handle,
    112             GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
    113             bounds, &dst));
    114 
    115     *data   = (uint8_t*)dst;
    116     *stride = mBuf->stride;
    117 }
    118 
    119 
    120 //
    121 // Display the content of the buffer provided by last call to getBufferYV12()
    122 //
    123 // See getBufferYV12() for details.
    124 //
    125 void PreviewRenderer::renderYV12() {
    126     int err = OK;
    127 
    128     GraphicBufferMapper &mapper = GraphicBufferMapper::get();
    129 
    130     if (mBuf!= NULL) {
    131         CHECK_EQ(0, mapper.unlock(mBuf->handle));
    132 
    133         if ((err = mSurface->ANativeWindow::queueBuffer(mSurface.get(), mBuf, -1)) != 0) {
    134             ALOGW("Surface::queueBuffer returned error %d", err);
    135         }
    136     }
    137     mBuf = NULL;
    138 }
    139 
    140 }  // namespace android
    141