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