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