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