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