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