SoftwareRenderer.cpp revision 4f4bedbfcbd443c9d0041a1e353d558fe6900757
1/* 2 * Copyright (C) 2009 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#define LOG_TAG "SoftwareRenderer" 18#include <utils/Log.h> 19 20#include "../include/SoftwareRenderer.h" 21 22#include <binder/MemoryHeapBase.h> 23#include <binder/MemoryHeapPmem.h> 24#include <media/stagefright/foundation/ADebug.h> 25#include <media/stagefright/MetaData.h> 26#include <surfaceflinger/Surface.h> 27#include <ui/android_native_buffer.h> 28#include <ui/GraphicBufferMapper.h> 29#include <gui/ISurfaceTexture.h> 30 31namespace android { 32 33SoftwareRenderer::SoftwareRenderer( 34 const sp<ANativeWindow> &nativeWindow, const sp<MetaData> &meta) 35 : mConverter(NULL), 36 mYUVMode(None), 37 mNativeWindow(nativeWindow) { 38 int32_t tmp; 39 CHECK(meta->findInt32(kKeyColorFormat, &tmp)); 40 mColorFormat = (OMX_COLOR_FORMATTYPE)tmp; 41 42 CHECK(meta->findInt32(kKeyWidth, &mWidth)); 43 CHECK(meta->findInt32(kKeyHeight, &mHeight)); 44 45 if (!meta->findRect( 46 kKeyCropRect, 47 &mCropLeft, &mCropTop, &mCropRight, &mCropBottom)) { 48 mCropLeft = mCropTop = 0; 49 mCropRight = mWidth - 1; 50 mCropBottom = mHeight - 1; 51 } 52 53 int32_t rotationDegrees; 54 if (!meta->findInt32(kKeyRotation, &rotationDegrees)) { 55 rotationDegrees = 0; 56 } 57 58 int halFormat; 59 switch (mColorFormat) { 60 default: 61 halFormat = HAL_PIXEL_FORMAT_RGB_565; 62 63 mConverter = new ColorConverter( 64 mColorFormat, OMX_COLOR_Format16bitRGB565); 65 CHECK(mConverter->isValid()); 66 break; 67 } 68 69 CHECK(mNativeWindow != NULL); 70 CHECK(mWidth > 0); 71 CHECK(mHeight > 0); 72 CHECK(mConverter == NULL || mConverter->isValid()); 73 74 CHECK_EQ(0, 75 native_window_set_usage( 76 mNativeWindow.get(), 77 GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN 78 | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP)); 79 80 // Width must be multiple of 32??? 81 CHECK_EQ(0, native_window_set_buffers_geometry( 82 mNativeWindow.get(), 83 mCropRight - mCropLeft + 1, 84 mCropBottom - mCropTop + 1, 85 halFormat)); 86 87 uint32_t transform; 88 switch (rotationDegrees) { 89 case 0: transform = 0; break; 90 case 90: transform = HAL_TRANSFORM_ROT_90; break; 91 case 180: transform = HAL_TRANSFORM_ROT_180; break; 92 case 270: transform = HAL_TRANSFORM_ROT_270; break; 93 default: transform = 0; break; 94 } 95 96 if (transform) { 97 CHECK_EQ(0, native_window_set_buffers_transform( 98 mNativeWindow.get(), transform)); 99 } 100} 101 102SoftwareRenderer::~SoftwareRenderer() { 103 delete mConverter; 104 mConverter = NULL; 105} 106 107void SoftwareRenderer::render( 108 const void *data, size_t size, void *platformPrivate) { 109 ANativeWindowBuffer *buf; 110 int err; 111 if ((err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf)) != 0) { 112 LOGW("Surface::dequeueBuffer returned error %d", err); 113 return; 114 } 115 116 CHECK_EQ(0, mNativeWindow->lockBuffer(mNativeWindow.get(), buf)); 117 118 GraphicBufferMapper &mapper = GraphicBufferMapper::get(); 119 120 Rect bounds(mWidth, mHeight); 121 122 void *dst; 123 CHECK_EQ(0, mapper.lock( 124 buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst)); 125 126 if (mConverter) { 127 mConverter->convert( 128 data, 129 mWidth, mHeight, 130 mCropLeft, mCropTop, mCropRight, mCropBottom, 131 dst, 132 buf->stride, buf->height, 133 0, 0, 134 mCropRight - mCropLeft, 135 mCropBottom - mCropTop); 136 } else { 137 TRESPASS(); 138 } 139 140 CHECK_EQ(0, mapper.unlock(buf->handle)); 141 142 if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf)) != 0) { 143 LOGW("Surface::queueBuffer returned error %d", err); 144 } 145 buf = NULL; 146} 147 148} // namespace android 149