SoftwareRenderer.cpp revision 5daeb129a2c2ba3d14ccd94af283b5f561c783ea
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/MediaDebug.h>
25#include <surfaceflinger/Surface.h>
26#include <ui/android_native_buffer.h>
27#include <ui/GraphicBufferMapper.h>
28
29namespace android {
30
31SoftwareRenderer::SoftwareRenderer(
32        OMX_COLOR_FORMATTYPE colorFormat,
33        const sp<Surface> &surface,
34        size_t displayWidth, size_t displayHeight,
35        size_t decodedWidth, size_t decodedHeight)
36    : mColorFormat(colorFormat),
37      mConverter(NULL),
38      mYUVMode(None),
39      mSurface(surface),
40      mDisplayWidth(displayWidth),
41      mDisplayHeight(displayHeight),
42      mDecodedWidth(decodedWidth),
43      mDecodedHeight(decodedHeight) {
44    LOGI("input format = %d", mColorFormat);
45    LOGI("display = %d x %d, decoded = %d x %d",
46            mDisplayWidth, mDisplayHeight, mDecodedWidth, mDecodedHeight);
47
48    int halFormat;
49    switch (mColorFormat) {
50#if HAS_YCBCR420_SP_ADRENO
51        case OMX_COLOR_FormatYUV420Planar:
52        {
53            halFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO;
54            mYUVMode = YUV420ToYUV420sp;
55            break;
56        }
57
58        case 0x7fa30c00:
59        {
60            halFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO;
61            mYUVMode = YUV420spToYUV420sp;
62            break;
63        }
64#endif
65
66        default:
67            halFormat = HAL_PIXEL_FORMAT_RGB_565;
68
69            mConverter = new ColorConverter(
70                    mColorFormat, OMX_COLOR_Format16bitRGB565);
71            CHECK(mConverter->isValid());
72            break;
73    }
74
75    CHECK(mSurface.get() != NULL);
76    CHECK(mDecodedWidth > 0);
77    CHECK(mDecodedHeight > 0);
78    CHECK(mConverter == NULL || mConverter->isValid());
79
80    CHECK_EQ(0,
81            native_window_set_usage(
82            mSurface.get(),
83            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN
84            | GRALLOC_USAGE_HW_TEXTURE));
85
86    CHECK_EQ(0, native_window_set_buffer_count(mSurface.get(), 2));
87
88    // Width must be multiple of 32???
89    CHECK_EQ(0, native_window_set_buffers_geometry(
90                mSurface.get(), mDecodedWidth, mDecodedHeight,
91                halFormat));
92}
93
94SoftwareRenderer::~SoftwareRenderer() {
95    delete mConverter;
96    mConverter = NULL;
97}
98
99static inline size_t ALIGN(size_t x, size_t alignment) {
100    return (x + alignment - 1) & ~(alignment - 1);
101}
102
103void SoftwareRenderer::render(
104        const void *data, size_t size, void *platformPrivate) {
105    android_native_buffer_t *buf;
106    CHECK_EQ(0, mSurface->dequeueBuffer(mSurface.get(), &buf));
107    CHECK_EQ(0, mSurface->lockBuffer(mSurface.get(), buf));
108
109    GraphicBufferMapper &mapper = GraphicBufferMapper::get();
110
111    Rect bounds(mDecodedWidth, mDecodedHeight);
112
113    void *dst;
114    CHECK_EQ(0, mapper.lock(
115                buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst));
116
117    if (mConverter) {
118        mConverter->convert(
119                mDecodedWidth, mDecodedHeight,
120                data, 0, dst, buf->stride * 2);
121    } else if (mYUVMode == YUV420spToYUV420sp) {
122        // Input and output are both YUV420sp, but the alignment requirements
123        // are different.
124        size_t srcYStride = mDecodedWidth;
125        const uint8_t *srcY = (const uint8_t *)data;
126        uint8_t *dstY = (uint8_t *)dst;
127        for (size_t i = 0; i < mDecodedHeight; ++i) {
128            memcpy(dstY, srcY, mDecodedWidth);
129            srcY += srcYStride;
130            dstY += buf->stride;
131        }
132
133        size_t srcUVStride = (mDecodedWidth + 1) & ~1;
134        size_t dstUVStride = ALIGN(mDecodedWidth / 2, 32) * 2;
135
136        const uint8_t *srcUV = (const uint8_t *)data
137            + mDecodedHeight * mDecodedWidth;
138
139        size_t dstUVOffset = ALIGN(ALIGN(mDecodedHeight, 32) * buf->stride, 4096);
140        uint8_t *dstUV = (uint8_t *)dst + dstUVOffset;
141
142        for (size_t i = 0; i < (mDecodedHeight + 1) / 2; ++i) {
143            memcpy(dstUV, srcUV, (mDecodedWidth + 1) & ~1);
144            srcUV += srcUVStride;
145            dstUV += dstUVStride;
146        }
147    } else if (mYUVMode == YUV420ToYUV420sp) {
148        // Input is YUV420 planar, output is YUV420sp, adhere to proper
149        // alignment requirements.
150        size_t srcYStride = mDecodedWidth;
151        const uint8_t *srcY = (const uint8_t *)data;
152        uint8_t *dstY = (uint8_t *)dst;
153        for (size_t i = 0; i < mDecodedHeight; ++i) {
154            memcpy(dstY, srcY, mDecodedWidth);
155            srcY += srcYStride;
156            dstY += buf->stride;
157        }
158
159        size_t srcUVStride = (mDecodedWidth + 1) / 2;
160        size_t dstUVStride = ALIGN(mDecodedWidth / 2, 32) * 2;
161
162        const uint8_t *srcU = (const uint8_t *)data
163            + mDecodedHeight * mDecodedWidth;
164
165        const uint8_t *srcV =
166            srcU + ((mDecodedWidth + 1) / 2) * ((mDecodedHeight + 1) / 2);
167
168        size_t dstUVOffset = ALIGN(ALIGN(mDecodedHeight, 32) * buf->stride, 4096);
169        uint8_t *dstUV = (uint8_t *)dst + dstUVOffset;
170
171        for (size_t i = 0; i < (mDecodedHeight + 1) / 2; ++i) {
172            for (size_t j = 0; j < (mDecodedWidth + 1) / 2; ++j) {
173                dstUV[2 * j + 1] = srcU[j];
174                dstUV[2 * j] = srcV[j];
175            }
176            srcU += srcUVStride;
177            srcV += srcUVStride;
178            dstUV += dstUVStride;
179        }
180    } else {
181        memcpy(dst, data, size);
182    }
183
184    CHECK_EQ(0, mapper.unlock(buf->handle));
185
186    CHECK_EQ(0, mSurface->queueBuffer(mSurface.get(), buf));
187    buf = NULL;
188}
189
190}  // namespace android
191