SoftwareRenderer.cpp revision 103487c0cb5c06f47225484486a98bc84745a059
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    size_t bufWidth, bufHeight;
60
61    switch (mColorFormat) {
62        case OMX_COLOR_FormatYUV420Planar:
63        {
64            halFormat = HAL_PIXEL_FORMAT_YV12;
65            bufWidth = (mWidth + 1) & ~1;
66            bufHeight = (mHeight + 1) & ~1;
67            break;
68        }
69
70        default:
71            halFormat = HAL_PIXEL_FORMAT_RGB_565;
72            bufWidth = mWidth;
73            bufHeight = mHeight;
74
75            mConverter = new ColorConverter(
76                    mColorFormat, OMX_COLOR_Format16bitRGB565);
77            CHECK(mConverter->isValid());
78            break;
79    }
80
81    CHECK(mNativeWindow != NULL);
82    CHECK(mWidth > 0);
83    CHECK(mHeight > 0);
84    CHECK(mConverter == NULL || mConverter->isValid());
85
86    CHECK_EQ(0,
87            native_window_set_usage(
88            mNativeWindow.get(),
89            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN
90            | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP));
91
92    // Width must be multiple of 32???
93    CHECK_EQ(0, native_window_set_buffers_geometry(
94                mNativeWindow.get(),
95                bufWidth,
96                bufHeight,
97                halFormat));
98
99    uint32_t transform;
100    switch (rotationDegrees) {
101        case 0: transform = 0; break;
102        case 90: transform = HAL_TRANSFORM_ROT_90; break;
103        case 180: transform = HAL_TRANSFORM_ROT_180; break;
104        case 270: transform = HAL_TRANSFORM_ROT_270; break;
105        default: transform = 0; break;
106    }
107
108    if (transform) {
109        CHECK_EQ(0, native_window_set_buffers_transform(
110                    mNativeWindow.get(), transform));
111    }
112
113    android_native_rect_t crop;
114    crop.left = mCropLeft;
115    crop.top = mCropTop;
116    crop.right = mCropRight + 1;
117    crop.bottom = mCropBottom + 1;
118
119    CHECK_EQ(0, native_window_set_crop(mNativeWindow.get(), &crop));
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        LOGW("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(mWidth, mHeight);
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                0, 0, mWidth - 1, mHeight - 1,
156                dst,
157                buf->stride, buf->height,
158                0, 0, mWidth - 1, mHeight - 1);
159    } else {
160        CHECK_EQ(mColorFormat, OMX_COLOR_FormatYUV420Planar);
161
162        const uint8_t *src_y = (const uint8_t *)data;
163        const uint8_t *src_u = (const uint8_t *)data + mWidth * mHeight;
164        const uint8_t *src_v = src_u + (mWidth / 2 * mHeight / 2);
165
166        uint8_t *dst_y = (uint8_t *)dst;
167        size_t dst_y_size = buf->stride * buf->height;
168        size_t dst_c_stride = ALIGN(buf->stride / 2, 16);
169        size_t dst_c_size = dst_c_stride * buf->height / 2;
170        uint8_t *dst_v = dst_y + dst_y_size;
171        uint8_t *dst_u = dst_v + dst_c_size;
172
173        for (int y = 0; y < mHeight; ++y) {
174            memcpy(dst_y, src_y, mWidth);
175
176            src_y += mWidth;
177            dst_y += buf->stride;
178        }
179
180        for (int y = 0; y < (mHeight + 1) / 2; ++y) {
181            memcpy(dst_u, src_u, (mWidth + 1) / 2);
182            memcpy(dst_v, src_v, (mWidth + 1) / 2);
183
184            src_u += mWidth / 2;
185            src_v += mWidth / 2;
186            dst_u += dst_c_stride;
187            dst_v += dst_c_stride;
188        }
189    }
190
191    CHECK_EQ(0, mapper.unlock(buf->handle));
192
193    if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf)) != 0) {
194        LOGW("Surface::queueBuffer returned error %d", err);
195    }
196    buf = NULL;
197}
198
199}  // namespace android
200