SoftwareRenderer.cpp revision eac728dcf35de0f74f46c9b65bde43ab6361c176
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 <cutils/properties.h> // for property_get 23#include <media/stagefright/foundation/ADebug.h> 24#include <media/stagefright/foundation/AMessage.h> 25#include <system/window.h> 26#include <ui/GraphicBufferMapper.h> 27#include <gui/IGraphicBufferProducer.h> 28 29namespace android { 30 31static bool runningInEmulator() { 32 char prop[PROPERTY_VALUE_MAX]; 33 return (property_get("ro.kernel.qemu", prop, NULL) > 0); 34} 35 36static int ALIGN(int x, int y) { 37 // y must be a power of 2. 38 return (x + y - 1) & ~(y - 1); 39} 40 41SoftwareRenderer::SoftwareRenderer(const sp<ANativeWindow> &nativeWindow) 42 : mColorFormat(OMX_COLOR_FormatUnused), 43 mConverter(NULL), 44 mYUVMode(None), 45 mNativeWindow(nativeWindow), 46 mWidth(0), 47 mHeight(0), 48 mCropLeft(0), 49 mCropTop(0), 50 mCropRight(0), 51 mCropBottom(0), 52 mCropWidth(0), 53 mCropHeight(0) { 54} 55 56SoftwareRenderer::~SoftwareRenderer() { 57 delete mConverter; 58 mConverter = NULL; 59} 60 61void SoftwareRenderer::resetFormatIfChanged(const sp<AMessage> &format) { 62 CHECK(format != NULL); 63 64 int32_t colorFormatNew; 65 CHECK(format->findInt32("color-format", &colorFormatNew)); 66 67 int32_t widthNew, heightNew; 68 CHECK(format->findInt32("stride", &widthNew)); 69 CHECK(format->findInt32("slice-height", &heightNew)); 70 71 int32_t cropLeftNew, cropTopNew, cropRightNew, cropBottomNew; 72 if (!format->findRect( 73 "crop", &cropLeftNew, &cropTopNew, &cropRightNew, &cropBottomNew)) { 74 cropLeftNew = cropTopNew = 0; 75 cropRightNew = widthNew - 1; 76 cropBottomNew = heightNew - 1; 77 } 78 79 if (static_cast<int32_t>(mColorFormat) == colorFormatNew && 80 mWidth == widthNew && 81 mHeight == heightNew && 82 mCropLeft == cropLeftNew && 83 mCropTop == cropTopNew && 84 mCropRight == cropRightNew && 85 mCropBottom == cropBottomNew) { 86 // Nothing changed, no need to reset renderer. 87 return; 88 } 89 90 mColorFormat = static_cast<OMX_COLOR_FORMATTYPE>(colorFormatNew); 91 mWidth = widthNew; 92 mHeight = heightNew; 93 mCropLeft = cropLeftNew; 94 mCropTop = cropTopNew; 95 mCropRight = cropRightNew; 96 mCropBottom = cropBottomNew; 97 98 mCropWidth = mCropRight - mCropLeft + 1; 99 mCropHeight = mCropBottom - mCropTop + 1; 100 101 // by default convert everything to RGB565 102 int halFormat = HAL_PIXEL_FORMAT_RGB_565; 103 size_t bufWidth = mCropWidth; 104 size_t bufHeight = mCropHeight; 105 106 // hardware has YUV12 and RGBA8888 support, so convert known formats 107 if (!runningInEmulator()) { 108 switch (mColorFormat) { 109 case OMX_COLOR_FormatYUV420Planar: 110 case OMX_TI_COLOR_FormatYUV420PackedSemiPlanar: 111 { 112 halFormat = HAL_PIXEL_FORMAT_YV12; 113 bufWidth = (mCropWidth + 1) & ~1; 114 bufHeight = (mCropHeight + 1) & ~1; 115 break; 116 } 117 case OMX_COLOR_Format24bitRGB888: 118 { 119 halFormat = HAL_PIXEL_FORMAT_RGB_888; 120 bufWidth = (mCropWidth + 1) & ~1; 121 bufHeight = (mCropHeight + 1) & ~1; 122 break; 123 } 124 case OMX_COLOR_Format32bitARGB8888: 125 case OMX_COLOR_Format32BitRGBA8888: 126 { 127 halFormat = HAL_PIXEL_FORMAT_RGBA_8888; 128 bufWidth = (mCropWidth + 1) & ~1; 129 bufHeight = (mCropHeight + 1) & ~1; 130 break; 131 } 132 default: 133 { 134 break; 135 } 136 } 137 } 138 139 if (halFormat == HAL_PIXEL_FORMAT_RGB_565) { 140 mConverter = new ColorConverter( 141 mColorFormat, OMX_COLOR_Format16bitRGB565); 142 CHECK(mConverter->isValid()); 143 } 144 145 CHECK(mNativeWindow != NULL); 146 CHECK(mCropWidth > 0); 147 CHECK(mCropHeight > 0); 148 CHECK(mConverter == NULL || mConverter->isValid()); 149 150 CHECK_EQ(0, 151 native_window_set_usage( 152 mNativeWindow.get(), 153 GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN 154 | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP)); 155 156 CHECK_EQ(0, 157 native_window_set_scaling_mode( 158 mNativeWindow.get(), 159 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)); 160 161 // Width must be multiple of 32??? 162 CHECK_EQ(0, native_window_set_buffers_dimensions( 163 mNativeWindow.get(), 164 bufWidth, 165 bufHeight)); 166 CHECK_EQ(0, native_window_set_buffers_format( 167 mNativeWindow.get(), 168 halFormat)); 169 170 // NOTE: native window uses extended right-bottom coordinate 171 android_native_rect_t crop; 172 crop.left = mCropLeft; 173 crop.top = mCropTop; 174 crop.right = mCropRight + 1; 175 crop.bottom = mCropBottom + 1; 176 ALOGV("setting crop: [%d, %d, %d, %d] for size [%zu, %zu]", 177 crop.left, crop.top, crop.right, crop.bottom, bufWidth, bufHeight); 178 179 CHECK_EQ(0, native_window_set_crop(mNativeWindow.get(), &crop)); 180 181 int32_t rotationDegrees; 182 if (!format->findInt32("rotation-degrees", &rotationDegrees)) { 183 rotationDegrees = 0; 184 } 185 uint32_t transform; 186 switch (rotationDegrees) { 187 case 0: transform = 0; break; 188 case 90: transform = HAL_TRANSFORM_ROT_90; break; 189 case 180: transform = HAL_TRANSFORM_ROT_180; break; 190 case 270: transform = HAL_TRANSFORM_ROT_270; break; 191 default: transform = 0; break; 192 } 193 194 CHECK_EQ(0, native_window_set_buffers_transform( 195 mNativeWindow.get(), transform)); 196} 197 198void SoftwareRenderer::render( 199 const void *data, size_t /*size*/, int64_t timestampNs, 200 void* /*platformPrivate*/, const sp<AMessage>& format) { 201 resetFormatIfChanged(format); 202 203 ANativeWindowBuffer *buf; 204 int err; 205 if ((err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(), 206 &buf)) != 0) { 207 ALOGW("Surface::dequeueBuffer returned error %d", err); 208 return; 209 } 210 211 GraphicBufferMapper &mapper = GraphicBufferMapper::get(); 212 213 Rect bounds(mCropWidth, mCropHeight); 214 215 void *dst; 216 CHECK_EQ(0, mapper.lock( 217 buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst)); 218 219 // TODO move the other conversions also into ColorConverter, and 220 // fix cropping issues (when mCropLeft/Top != 0 or mWidth != mCropWidth) 221 if (mConverter) { 222 mConverter->convert( 223 data, 224 mWidth, mHeight, 225 mCropLeft, mCropTop, mCropRight, mCropBottom, 226 dst, 227 buf->stride, buf->height, 228 0, 0, mCropWidth - 1, mCropHeight - 1); 229 } else if (mColorFormat == OMX_COLOR_FormatYUV420Planar) { 230 // YV12 really 231 const uint8_t *src_y = (const uint8_t *)data; 232 const uint8_t *src_u = 233 (const uint8_t *)data + mWidth * mHeight; 234 const uint8_t *src_v = src_u + (mWidth / 2 * mHeight / 2); 235 236 uint8_t *dst_y = (uint8_t *)dst; 237 size_t dst_y_size = buf->stride * buf->height; 238 size_t dst_c_stride = ALIGN(buf->stride / 2, 16); 239 size_t dst_c_size = dst_c_stride * buf->height / 2; 240 uint8_t *dst_v = dst_y + dst_y_size; 241 uint8_t *dst_u = dst_v + dst_c_size; 242 243 for (int y = 0; y < mCropHeight; ++y) { 244 memcpy(dst_y, src_y, mCropWidth); 245 246 src_y += mWidth; 247 dst_y += buf->stride; 248 } 249 250 for (int y = 0; y < (mCropHeight + 1) / 2; ++y) { 251 memcpy(dst_u, src_u, (mCropWidth + 1) / 2); 252 memcpy(dst_v, src_v, (mCropWidth + 1) / 2); 253 254 src_u += mWidth / 2; 255 src_v += mWidth / 2; 256 dst_u += dst_c_stride; 257 dst_v += dst_c_stride; 258 } 259 } else if (mColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar) { 260 const uint8_t *src_y = (const uint8_t *)data; 261 const uint8_t *src_uv = (const uint8_t *)data 262 + mWidth * (mHeight - mCropTop / 2); 263 264 uint8_t *dst_y = (uint8_t *)dst; 265 266 size_t dst_y_size = buf->stride * buf->height; 267 size_t dst_c_stride = ALIGN(buf->stride / 2, 16); 268 size_t dst_c_size = dst_c_stride * buf->height / 2; 269 uint8_t *dst_v = dst_y + dst_y_size; 270 uint8_t *dst_u = dst_v + dst_c_size; 271 272 for (int y = 0; y < mCropHeight; ++y) { 273 memcpy(dst_y, src_y, mCropWidth); 274 275 src_y += mWidth; 276 dst_y += buf->stride; 277 } 278 279 for (int y = 0; y < (mCropHeight + 1) / 2; ++y) { 280 size_t tmp = (mCropWidth + 1) / 2; 281 for (size_t x = 0; x < tmp; ++x) { 282 dst_u[x] = src_uv[2 * x]; 283 dst_v[x] = src_uv[2 * x + 1]; 284 } 285 286 src_uv += mWidth; 287 dst_u += dst_c_stride; 288 dst_v += dst_c_stride; 289 } 290 } else if (mColorFormat == OMX_COLOR_Format24bitRGB888) { 291 uint8_t* srcPtr = (uint8_t*)data; 292 uint8_t* dstPtr = (uint8_t*)dst; 293 294 for (size_t y = 0; y < (size_t)mCropHeight; ++y) { 295 memcpy(dstPtr, srcPtr, mCropWidth * 3); 296 srcPtr += mWidth * 3; 297 dstPtr += buf->stride * 3; 298 } 299 } else if (mColorFormat == OMX_COLOR_Format32bitARGB8888) { 300 uint8_t *srcPtr, *dstPtr; 301 302 for (size_t y = 0; y < (size_t)mCropHeight; ++y) { 303 srcPtr = (uint8_t*)data + mWidth * 4 * y; 304 dstPtr = (uint8_t*)dst + buf->stride * 4 * y; 305 for (size_t x = 0; x < (size_t)mCropWidth; ++x) { 306 uint8_t a = *srcPtr++; 307 for (size_t i = 0; i < 3; ++i) { // copy RGB 308 *dstPtr++ = *srcPtr++; 309 } 310 *dstPtr++ = a; // alpha last (ARGB to RGBA) 311 } 312 } 313 } else if (mColorFormat == OMX_COLOR_Format32BitRGBA8888) { 314 uint8_t* srcPtr = (uint8_t*)data; 315 uint8_t* dstPtr = (uint8_t*)dst; 316 317 for (size_t y = 0; y < (size_t)mCropHeight; ++y) { 318 memcpy(dstPtr, srcPtr, mCropWidth * 4); 319 srcPtr += mWidth * 4; 320 dstPtr += buf->stride * 4; 321 } 322 } else { 323 LOG_ALWAYS_FATAL("bad color format %#x", mColorFormat); 324 } 325 326 CHECK_EQ(0, mapper.unlock(buf->handle)); 327 328 if ((err = native_window_set_buffers_timestamp(mNativeWindow.get(), 329 timestampNs)) != 0) { 330 ALOGW("Surface::set_buffers_timestamp returned error %d", err); 331 } 332 333 if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf, 334 -1)) != 0) { 335 ALOGW("Surface::queueBuffer returned error %d", err); 336 } 337 buf = NULL; 338} 339 340} // namespace android 341