SoftwareRenderer.cpp revision 7ea50388bd51cf2a45355139499314bed63e70ed
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        const uint8_t *src_y = (const uint8_t *)data;
231        const uint8_t *src_u =
232                (const uint8_t *)data + mWidth * mHeight;
233        const uint8_t *src_v = src_u + (mWidth / 2 * mHeight / 2);
234
235        uint8_t *dst_y = (uint8_t *)dst;
236        size_t dst_y_size = buf->stride * buf->height;
237        size_t dst_c_stride = ALIGN(buf->stride / 2, 16);
238        size_t dst_c_size = dst_c_stride * buf->height / 2;
239        uint8_t *dst_v = dst_y + dst_y_size;
240        uint8_t *dst_u = dst_v + dst_c_size;
241
242        for (int y = 0; y < mCropHeight; ++y) {
243            memcpy(dst_y, src_y, mCropWidth);
244
245            src_y += mWidth;
246            dst_y += buf->stride;
247        }
248
249        for (int y = 0; y < (mCropHeight + 1) / 2; ++y) {
250            memcpy(dst_u, src_u, (mCropWidth + 1) / 2);
251            memcpy(dst_v, src_v, (mCropWidth + 1) / 2);
252
253            src_u += mWidth / 2;
254            src_v += mWidth / 2;
255            dst_u += dst_c_stride;
256            dst_v += dst_c_stride;
257        }
258    } else if (mColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar) {
259        const uint8_t *src_y = (const uint8_t *)data;
260        const uint8_t *src_uv = (const uint8_t *)data
261                + mWidth * (mHeight - mCropTop / 2);
262
263        uint8_t *dst_y = (uint8_t *)dst;
264
265        size_t dst_y_size = buf->stride * buf->height;
266        size_t dst_c_stride = ALIGN(buf->stride / 2, 16);
267        size_t dst_c_size = dst_c_stride * buf->height / 2;
268        uint8_t *dst_v = dst_y + dst_y_size;
269        uint8_t *dst_u = dst_v + dst_c_size;
270
271        for (int y = 0; y < mCropHeight; ++y) {
272            memcpy(dst_y, src_y, mCropWidth);
273
274            src_y += mWidth;
275            dst_y += buf->stride;
276        }
277
278        for (int y = 0; y < (mCropHeight + 1) / 2; ++y) {
279            size_t tmp = (mCropWidth + 1) / 2;
280            for (size_t x = 0; x < tmp; ++x) {
281                dst_u[x] = src_uv[2 * x];
282                dst_v[x] = src_uv[2 * x + 1];
283            }
284
285            src_uv += mWidth;
286            dst_u += dst_c_stride;
287            dst_v += dst_c_stride;
288        }
289    } else if (mColorFormat == OMX_COLOR_Format24bitRGB888) {
290        uint8_t* srcPtr = (uint8_t*)data;
291        uint8_t* dstPtr = (uint8_t*)dst;
292
293        for (size_t y = 0; y < (size_t)mCropHeight; ++y) {
294            memcpy(dstPtr, srcPtr, mCropWidth * 3);
295            srcPtr += mWidth * 3;
296            dstPtr += buf->stride * 3;
297        }
298    } else if (mColorFormat == OMX_COLOR_Format32bitARGB8888) {
299        uint8_t *srcPtr, *dstPtr;
300
301        for (size_t y = 0; y < (size_t)mCropHeight; ++y) {
302            srcPtr = (uint8_t*)data + mWidth * 4 * y;
303            dstPtr = (uint8_t*)dst + buf->stride * 4 * y;
304            for (size_t x = 0; x < (size_t)mCropWidth; ++x) {
305                uint8_t a = *srcPtr++;
306                for (size_t i = 0; i < 3; ++i) {   // copy RGB
307                    *dstPtr++ = *srcPtr++;
308                }
309                *dstPtr++ = a;  // alpha last (ARGB to RGBA)
310            }
311        }
312    } else if (mColorFormat == OMX_COLOR_Format32BitRGBA8888) {
313        uint8_t* srcPtr = (uint8_t*)data;
314        uint8_t* dstPtr = (uint8_t*)dst;
315
316        for (size_t y = 0; y < (size_t)mCropHeight; ++y) {
317            memcpy(dstPtr, srcPtr, mCropWidth * 4);
318            srcPtr += mWidth * 4;
319            dstPtr += buf->stride * 4;
320        }
321    } else {
322        LOG_ALWAYS_FATAL("bad color format %#x", mColorFormat);
323    }
324
325    CHECK_EQ(0, mapper.unlock(buf->handle));
326
327    if ((err = native_window_set_buffers_timestamp(mNativeWindow.get(),
328            timestampNs)) != 0) {
329        ALOGW("Surface::set_buffers_timestamp returned error %d", err);
330    }
331
332    if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf,
333            -1)) != 0) {
334        ALOGW("Surface::queueBuffer returned error %d", err);
335    }
336    buf = NULL;
337}
338
339}  // namespace android
340