SoftwareRenderer.cpp revision 3e135cf939b4baf9902fe24fb0e50528e62c10bd
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    CHECK_EQ(0, mSurface->dequeueBuffer(mSurface.get(), &buf));
110    CHECK_EQ(0, mSurface->lockBuffer(mSurface.get(), buf));
111
112    GraphicBufferMapper &mapper = GraphicBufferMapper::get();
113
114    Rect bounds(mDecodedWidth, mDecodedHeight);
115
116    void *dst;
117    CHECK_EQ(0, mapper.lock(
118                buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst));
119
120    if (mConverter) {
121        mConverter->convert(
122                mDecodedWidth, mDecodedHeight,
123                data, 0, dst, buf->stride * 2);
124    } else if (mYUVMode == YUV420spToYUV420sp) {
125        // Input and output are both YUV420sp, but the alignment requirements
126        // are different.
127        size_t srcYStride = mDecodedWidth;
128        const uint8_t *srcY = (const uint8_t *)data;
129        uint8_t *dstY = (uint8_t *)dst;
130        for (size_t i = 0; i < mDecodedHeight; ++i) {
131            memcpy(dstY, srcY, mDecodedWidth);
132            srcY += srcYStride;
133            dstY += buf->stride;
134        }
135
136        size_t srcUVStride = (mDecodedWidth + 1) & ~1;
137        size_t dstUVStride = ALIGN(mDecodedWidth / 2, 32) * 2;
138
139        const uint8_t *srcUV = (const uint8_t *)data
140            + mDecodedHeight * mDecodedWidth;
141
142        size_t dstUVOffset = ALIGN(ALIGN(mDecodedHeight, 32) * buf->stride, 4096);
143        uint8_t *dstUV = (uint8_t *)dst + dstUVOffset;
144
145        for (size_t i = 0; i < (mDecodedHeight + 1) / 2; ++i) {
146            memcpy(dstUV, srcUV, (mDecodedWidth + 1) & ~1);
147            srcUV += srcUVStride;
148            dstUV += dstUVStride;
149        }
150    } else if (mYUVMode == YUV420ToYUV420sp) {
151        // Input is YUV420 planar, output is YUV420sp, adhere to proper
152        // alignment requirements.
153        size_t srcYStride = mDecodedWidth;
154        const uint8_t *srcY = (const uint8_t *)data;
155        uint8_t *dstY = (uint8_t *)dst;
156        for (size_t i = 0; i < mDecodedHeight; ++i) {
157            memcpy(dstY, srcY, mDecodedWidth);
158            srcY += srcYStride;
159            dstY += buf->stride;
160        }
161
162        size_t srcUVStride = (mDecodedWidth + 1) / 2;
163        size_t dstUVStride = ALIGN(mDecodedWidth / 2, 32) * 2;
164
165        const uint8_t *srcU = (const uint8_t *)data
166            + mDecodedHeight * mDecodedWidth;
167
168        const uint8_t *srcV =
169            srcU + ((mDecodedWidth + 1) / 2) * ((mDecodedHeight + 1) / 2);
170
171        size_t dstUVOffset = ALIGN(ALIGN(mDecodedHeight, 32) * buf->stride, 4096);
172        uint8_t *dstUV = (uint8_t *)dst + dstUVOffset;
173
174        for (size_t i = 0; i < (mDecodedHeight + 1) / 2; ++i) {
175            for (size_t j = 0; j < (mDecodedWidth + 1) / 2; ++j) {
176                dstUV[2 * j + 1] = srcU[j];
177                dstUV[2 * j] = srcV[j];
178            }
179            srcU += srcUVStride;
180            srcV += srcUVStride;
181            dstUV += dstUVStride;
182        }
183    } else {
184        memcpy(dst, data, size);
185    }
186
187    CHECK_EQ(0, mapper.unlock(buf->handle));
188
189    CHECK_EQ(0, mSurface->queueBuffer(mSurface.get(), buf));
190    buf = NULL;
191}
192
193}  // namespace android
194