SoftwareRenderer.cpp revision 77f70f7de1977d7b0a9f9c37a25e34db16feb7b4
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 <media/stagefright/foundation/ADebug.h> 23#include <media/stagefright/MetaData.h> 24#include <system/window.h> 25#include <ui/GraphicBufferMapper.h> 26#include <gui/ISurfaceTexture.h> 27 28namespace android { 29 30SoftwareRenderer::SoftwareRenderer( 31 const sp<ANativeWindow> &nativeWindow, const sp<MetaData> &meta) 32 : mConverter(NULL), 33 mYUVMode(None), 34 mNativeWindow(nativeWindow) { 35 int32_t tmp; 36 CHECK(meta->findInt32(kKeyColorFormat, &tmp)); 37 mColorFormat = (OMX_COLOR_FORMATTYPE)tmp; 38 39 CHECK(meta->findInt32(kKeyWidth, &mWidth)); 40 CHECK(meta->findInt32(kKeyHeight, &mHeight)); 41 42 if (!meta->findRect( 43 kKeyCropRect, 44 &mCropLeft, &mCropTop, &mCropRight, &mCropBottom)) { 45 mCropLeft = mCropTop = 0; 46 mCropRight = mWidth - 1; 47 mCropBottom = mHeight - 1; 48 } 49 50 mCropWidth = mCropRight - mCropLeft + 1; 51 mCropHeight = mCropBottom - mCropTop + 1; 52 53 int32_t rotationDegrees; 54 if (!meta->findInt32(kKeyRotation, &rotationDegrees)) { 55 rotationDegrees = 0; 56 } 57 58 int halFormat; 59 size_t bufWidth, bufHeight; 60 61 switch (mColorFormat) { 62 case OMX_COLOR_FormatYUV420Planar: 63 case OMX_TI_COLOR_FormatYUV420PackedSemiPlanar: 64 { 65 halFormat = HAL_PIXEL_FORMAT_YV12; 66 bufWidth = (mCropWidth + 1) & ~1; 67 bufHeight = (mCropHeight + 1) & ~1; 68 break; 69 } 70 71 default: 72 halFormat = HAL_PIXEL_FORMAT_RGB_565; 73 bufWidth = mCropWidth; 74 bufHeight = mCropHeight; 75 76 mConverter = new ColorConverter( 77 mColorFormat, OMX_COLOR_Format16bitRGB565); 78 CHECK(mConverter->isValid()); 79 break; 80 } 81 82 CHECK(mNativeWindow != NULL); 83 CHECK(mCropWidth > 0); 84 CHECK(mCropHeight > 0); 85 CHECK(mConverter == NULL || mConverter->isValid()); 86 87 CHECK_EQ(0, 88 native_window_set_usage( 89 mNativeWindow.get(), 90 GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN 91 | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP)); 92 93 CHECK_EQ(0, 94 native_window_set_scaling_mode( 95 mNativeWindow.get(), 96 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)); 97 98 // Width must be multiple of 32??? 99 CHECK_EQ(0, native_window_set_buffers_geometry( 100 mNativeWindow.get(), 101 bufWidth, 102 bufHeight, 103 halFormat)); 104 105 uint32_t transform; 106 switch (rotationDegrees) { 107 case 0: transform = 0; break; 108 case 90: transform = HAL_TRANSFORM_ROT_90; break; 109 case 180: transform = HAL_TRANSFORM_ROT_180; break; 110 case 270: transform = HAL_TRANSFORM_ROT_270; break; 111 default: transform = 0; break; 112 } 113 114 if (transform) { 115 CHECK_EQ(0, native_window_set_buffers_transform( 116 mNativeWindow.get(), transform)); 117 } 118} 119 120SoftwareRenderer::~SoftwareRenderer() { 121 delete mConverter; 122 mConverter = NULL; 123} 124 125static int ALIGN(int x, int y) { 126 // y must be a power of 2. 127 return (x + y - 1) & ~(y - 1); 128} 129 130void SoftwareRenderer::render( 131 const void *data, size_t size, void *platformPrivate) { 132 ANativeWindowBuffer *buf; 133 int err; 134 if ((err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf)) != 0) { 135 ALOGW("Surface::dequeueBuffer returned error %d", err); 136 return; 137 } 138 139 CHECK_EQ(0, mNativeWindow->lockBuffer(mNativeWindow.get(), buf)); 140 141 GraphicBufferMapper &mapper = GraphicBufferMapper::get(); 142 143 Rect bounds(mCropWidth, mCropHeight); 144 145 void *dst; 146 CHECK_EQ(0, mapper.lock( 147 buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst)); 148 149 if (mConverter) { 150 mConverter->convert( 151 data, 152 mWidth, mHeight, 153 mCropLeft, mCropTop, mCropRight, mCropBottom, 154 dst, 155 buf->stride, buf->height, 156 0, 0, mCropWidth - 1, mCropHeight - 1); 157 } else if (mColorFormat == OMX_COLOR_FormatYUV420Planar) { 158 const uint8_t *src_y = (const uint8_t *)data; 159 const uint8_t *src_u = (const uint8_t *)data + mWidth * mHeight; 160 const uint8_t *src_v = src_u + (mWidth / 2 * mHeight / 2); 161 162 uint8_t *dst_y = (uint8_t *)dst; 163 size_t dst_y_size = buf->stride * buf->height; 164 size_t dst_c_stride = ALIGN(buf->stride / 2, 16); 165 size_t dst_c_size = dst_c_stride * buf->height / 2; 166 uint8_t *dst_v = dst_y + dst_y_size; 167 uint8_t *dst_u = dst_v + dst_c_size; 168 169 for (int y = 0; y < mCropHeight; ++y) { 170 memcpy(dst_y, src_y, mCropWidth); 171 172 src_y += mWidth; 173 dst_y += buf->stride; 174 } 175 176 for (int y = 0; y < (mCropHeight + 1) / 2; ++y) { 177 memcpy(dst_u, src_u, (mCropWidth + 1) / 2); 178 memcpy(dst_v, src_v, (mCropWidth + 1) / 2); 179 180 src_u += mWidth / 2; 181 src_v += mWidth / 2; 182 dst_u += dst_c_stride; 183 dst_v += dst_c_stride; 184 } 185 } else { 186 CHECK_EQ(mColorFormat, OMX_TI_COLOR_FormatYUV420PackedSemiPlanar); 187 188 const uint8_t *src_y = 189 (const uint8_t *)data; 190 191 const uint8_t *src_uv = 192 (const uint8_t *)data + mWidth * (mHeight - mCropTop / 2); 193 194 uint8_t *dst_y = (uint8_t *)dst; 195 196 size_t dst_y_size = buf->stride * buf->height; 197 size_t dst_c_stride = ALIGN(buf->stride / 2, 16); 198 size_t dst_c_size = dst_c_stride * buf->height / 2; 199 uint8_t *dst_v = dst_y + dst_y_size; 200 uint8_t *dst_u = dst_v + dst_c_size; 201 202 for (int y = 0; y < mCropHeight; ++y) { 203 memcpy(dst_y, src_y, mCropWidth); 204 205 src_y += mWidth; 206 dst_y += buf->stride; 207 } 208 209 for (int y = 0; y < (mCropHeight + 1) / 2; ++y) { 210 size_t tmp = (mCropWidth + 1) / 2; 211 for (size_t x = 0; x < tmp; ++x) { 212 dst_u[x] = src_uv[2 * x]; 213 dst_v[x] = src_uv[2 * x + 1]; 214 } 215 216 src_uv += mWidth; 217 dst_u += dst_c_stride; 218 dst_v += dst_c_stride; 219 } 220 } 221 222 CHECK_EQ(0, mapper.unlock(buf->handle)); 223 224 if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf)) != 0) { 225 ALOGW("Surface::queueBuffer returned error %d", err); 226 } 227 buf = NULL; 228} 229 230} // namespace android 231