SoftwareRenderer.cpp revision 35395ea6ad11824a4a89cc1ab9ee84f936188296
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    int halFormat;
102    size_t bufWidth, bufHeight;
103
104    switch ((int)mColorFormat) {
105        case OMX_COLOR_FormatYUV420Planar:
106        case OMX_TI_COLOR_FormatYUV420PackedSemiPlanar:
107        case OMX_INTEL_COLOR_FormatYUV420PackedSemiPlanar:
108        case OMX_COLOR_FormatYUV420SemiPlanar:
109        {
110            if (!runningInEmulator()) {
111                halFormat = HAL_PIXEL_FORMAT_YV12;
112                bufWidth = (mCropWidth + 1) & ~1;
113                bufHeight = (mCropHeight + 1) & ~1;
114                break;
115            }
116
117            // fall through.
118        }
119
120        default:
121            halFormat = HAL_PIXEL_FORMAT_RGB_565;
122            bufWidth = mCropWidth;
123            bufHeight = mCropHeight;
124
125            mConverter = new ColorConverter(
126                    mColorFormat, OMX_COLOR_Format16bitRGB565);
127            CHECK(mConverter->isValid());
128            break;
129    }
130
131    CHECK(mNativeWindow != NULL);
132    CHECK(mCropWidth > 0);
133    CHECK(mCropHeight > 0);
134    CHECK(mConverter == NULL || mConverter->isValid());
135
136    CHECK_EQ(0,
137            native_window_set_usage(
138            mNativeWindow.get(),
139            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN
140            | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP));
141
142    CHECK_EQ(0,
143            native_window_set_scaling_mode(
144            mNativeWindow.get(),
145            NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW));
146
147    // Width must be multiple of 32???
148    CHECK_EQ(0, native_window_set_buffers_dimensions(
149                mNativeWindow.get(),
150                bufWidth,
151                bufHeight));
152    CHECK_EQ(0, native_window_set_buffers_format(
153                mNativeWindow.get(),
154                halFormat));
155
156    // NOTE: native window uses extended right-bottom coordinate
157    android_native_rect_t crop;
158    crop.left = mCropLeft;
159    crop.top = mCropTop;
160    crop.right = mCropRight + 1;
161    crop.bottom = mCropBottom + 1;
162    ALOGV("setting crop: [%d, %d, %d, %d] for size [%zu, %zu]",
163          crop.left, crop.top, crop.right, crop.bottom, bufWidth, bufHeight);
164
165    CHECK_EQ(0, native_window_set_crop(mNativeWindow.get(), &crop));
166
167    int32_t rotationDegrees;
168    if (!format->findInt32("rotation-degrees", &rotationDegrees)) {
169        rotationDegrees = 0;
170    }
171    uint32_t transform;
172    switch (rotationDegrees) {
173        case 0: transform = 0; break;
174        case 90: transform = HAL_TRANSFORM_ROT_90; break;
175        case 180: transform = HAL_TRANSFORM_ROT_180; break;
176        case 270: transform = HAL_TRANSFORM_ROT_270; break;
177        default: transform = 0; break;
178    }
179
180    CHECK_EQ(0, native_window_set_buffers_transform(
181                mNativeWindow.get(), transform));
182}
183
184void SoftwareRenderer::render(
185        const void *data, size_t /*size*/, int64_t timestampNs,
186        void* /*platformPrivate*/, const sp<AMessage>& format) {
187    resetFormatIfChanged(format);
188
189    ANativeWindowBuffer *buf;
190    int err;
191    if ((err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(),
192            &buf)) != 0) {
193        ALOGW("Surface::dequeueBuffer returned error %d", err);
194        return;
195    }
196
197    GraphicBufferMapper &mapper = GraphicBufferMapper::get();
198
199    Rect bounds(mCropWidth, mCropHeight);
200
201    void *dst;
202    CHECK_EQ(0, mapper.lock(
203                buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst));
204
205    if (mConverter) {
206        mConverter->convert(
207                data,
208                mWidth, mHeight,
209                mCropLeft, mCropTop, mCropRight, mCropBottom,
210                dst,
211                buf->stride, buf->height,
212                0, 0, mCropWidth - 1, mCropHeight - 1);
213    } else if (mColorFormat == OMX_COLOR_FormatYUV420Planar) {
214        const uint8_t *src_y = (const uint8_t *)data;
215        const uint8_t *src_u = (const uint8_t *)data + mWidth * mHeight;
216        const uint8_t *src_v = src_u + (mWidth / 2 * mHeight / 2);
217
218        uint8_t *dst_y = (uint8_t *)dst;
219        size_t dst_y_size = buf->stride * buf->height;
220        size_t dst_c_stride = ALIGN(buf->stride / 2, 16);
221        size_t dst_c_size = dst_c_stride * buf->height / 2;
222        uint8_t *dst_v = dst_y + dst_y_size;
223        uint8_t *dst_u = dst_v + dst_c_size;
224
225        for (int y = 0; y < mCropHeight; ++y) {
226            memcpy(dst_y, src_y, mCropWidth);
227
228            src_y += mWidth;
229            dst_y += buf->stride;
230        }
231
232        for (int y = 0; y < (mCropHeight + 1) / 2; ++y) {
233            memcpy(dst_u, src_u, (mCropWidth + 1) / 2);
234            memcpy(dst_v, src_v, (mCropWidth + 1) / 2);
235
236            src_u += mWidth / 2;
237            src_v += mWidth / 2;
238            dst_u += dst_c_stride;
239            dst_v += dst_c_stride;
240        }
241    } else if (mColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar
242            || mColorFormat == OMX_INTEL_COLOR_FormatYUV420PackedSemiPlanar
243            || mColorFormat == OMX_COLOR_FormatYUV420SemiPlanar) {
244        const uint8_t *src_y =
245            (const uint8_t *)data;
246
247        const uint8_t *src_uv =
248            (const uint8_t *)data + mWidth * (mHeight - mCropTop / 2);
249
250        uint8_t *dst_y = (uint8_t *)dst;
251
252        size_t dst_y_size = buf->stride * buf->height;
253        size_t dst_c_stride = ALIGN(buf->stride / 2, 16);
254        size_t dst_c_size = dst_c_stride * buf->height / 2;
255        uint8_t *dst_v = dst_y + dst_y_size;
256        uint8_t *dst_u = dst_v + dst_c_size;
257
258        for (int y = 0; y < mCropHeight; ++y) {
259            memcpy(dst_y, src_y, mCropWidth);
260
261            src_y += mWidth;
262            dst_y += buf->stride;
263        }
264
265        for (int y = 0; y < (mCropHeight + 1) / 2; ++y) {
266            size_t tmp = (mCropWidth + 1) / 2;
267            for (size_t x = 0; x < tmp; ++x) {
268                dst_u[x] = src_uv[2 * x];
269                dst_v[x] = src_uv[2 * x + 1];
270            }
271
272            src_uv += mWidth;
273            dst_u += dst_c_stride;
274            dst_v += dst_c_stride;
275        }
276    } else {
277        LOG_ALWAYS_FATAL("bad color format %#x", mColorFormat);
278    }
279
280    CHECK_EQ(0, mapper.unlock(buf->handle));
281
282    if ((err = native_window_set_buffers_timestamp(mNativeWindow.get(),
283            timestampNs)) != 0) {
284        ALOGW("Surface::set_buffers_timestamp returned error %d", err);
285    }
286
287    if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf,
288            -1)) != 0) {
289        ALOGW("Surface::queueBuffer returned error %d", err);
290    }
291    buf = NULL;
292}
293
294}  // namespace android
295