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