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