SoftwareRenderer.cpp revision 5e97c8861ef81d07cf1e304c1c1bed09b84513d4
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/foundation/ADebug.h>
25#include <media/stagefright/MetaData.h>
26#include <surfaceflinger/Surface.h>
27#include <ui/android_native_buffer.h>
28#include <ui/GraphicBufferMapper.h>
29#include <gui/ISurfaceTexture.h>
30
31namespace android {
32
33SoftwareRenderer::SoftwareRenderer(
34        const sp<ANativeWindow> &nativeWindow, const sp<MetaData> &meta)
35    : mConverter(NULL),
36      mYUVMode(None),
37      mNativeWindow(nativeWindow) {
38    int32_t tmp;
39    CHECK(meta->findInt32(kKeyColorFormat, &tmp));
40    mColorFormat = (OMX_COLOR_FORMATTYPE)tmp;
41
42    CHECK(meta->findInt32(kKeyWidth, &mWidth));
43    CHECK(meta->findInt32(kKeyHeight, &mHeight));
44
45    if (!meta->findRect(
46                kKeyCropRect,
47                &mCropLeft, &mCropTop, &mCropRight, &mCropBottom)) {
48        mCropLeft = mCropTop = 0;
49        mCropRight = mWidth - 1;
50        mCropBottom = mHeight - 1;
51    }
52
53    int32_t rotationDegrees;
54    if (!meta->findInt32(kKeyRotation, &rotationDegrees)) {
55        rotationDegrees = 0;
56    }
57
58    int halFormat;
59    size_t bufWidth, bufHeight;
60
61    switch (mColorFormat) {
62        case OMX_COLOR_FormatYUV420Planar:
63        {
64            halFormat = HAL_PIXEL_FORMAT_YV12;
65            bufWidth = (mWidth + 1) & ~1;
66            bufHeight = (mHeight + 1) & ~1;
67            break;
68        }
69
70        default:
71            halFormat = HAL_PIXEL_FORMAT_RGB_565;
72            bufWidth = mWidth;
73            bufHeight = mHeight;
74
75            mConverter = new ColorConverter(
76                    mColorFormat, OMX_COLOR_Format16bitRGB565);
77            CHECK(mConverter->isValid());
78            break;
79    }
80
81    CHECK(mNativeWindow != NULL);
82    CHECK(mWidth > 0);
83    CHECK(mHeight > 0);
84    CHECK(mConverter == NULL || mConverter->isValid());
85
86    CHECK_EQ(0,
87            native_window_set_usage(
88            mNativeWindow.get(),
89            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN
90            | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP));
91
92    CHECK_EQ(0, native_window_set_buffer_count(mNativeWindow.get(), 2));
93
94    // Width must be multiple of 32???
95    CHECK_EQ(0, native_window_set_buffers_geometry(
96                mNativeWindow.get(),
97                bufWidth,
98                bufHeight,
99                halFormat));
100
101    uint32_t transform;
102    switch (rotationDegrees) {
103        case 0: transform = 0; break;
104        case 90: transform = HAL_TRANSFORM_ROT_90; break;
105        case 180: transform = HAL_TRANSFORM_ROT_180; break;
106        case 270: transform = HAL_TRANSFORM_ROT_270; break;
107        default: transform = 0; break;
108    }
109
110    if (transform) {
111        CHECK_EQ(0, native_window_set_buffers_transform(
112                    mNativeWindow.get(), transform));
113    }
114
115    android_native_rect_t crop;
116    crop.left = mCropLeft;
117    crop.top = mCropTop;
118    crop.right = mCropRight + 1;
119    crop.bottom = mCropBottom + 1;
120
121    CHECK_EQ(0, native_window_set_crop(mNativeWindow.get(), &crop));
122}
123
124SoftwareRenderer::~SoftwareRenderer() {
125    delete mConverter;
126    mConverter = NULL;
127}
128
129static int ALIGN(int x, int y) {
130    // y must be a power of 2.
131    return (x + y - 1) & ~(y - 1);
132}
133
134void SoftwareRenderer::render(
135        const void *data, size_t size, void *platformPrivate) {
136    ANativeWindowBuffer *buf;
137    int err;
138    if ((err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf)) != 0) {
139        LOGW("Surface::dequeueBuffer returned error %d", err);
140        return;
141    }
142
143    CHECK_EQ(0, mNativeWindow->lockBuffer(mNativeWindow.get(), buf));
144
145    GraphicBufferMapper &mapper = GraphicBufferMapper::get();
146
147    Rect bounds(mWidth, mHeight);
148
149    void *dst;
150    CHECK_EQ(0, mapper.lock(
151                buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst));
152
153    if (mConverter) {
154        mConverter->convert(
155                data,
156                mWidth, mHeight,
157                0, 0, mWidth - 1, mHeight - 1,
158                dst,
159                buf->stride, buf->height,
160                0, 0, mWidth - 1, mHeight - 1);
161    } else {
162        CHECK_EQ(mColorFormat, OMX_COLOR_FormatYUV420Planar);
163
164        const uint8_t *src_y = (const uint8_t *)data;
165        const uint8_t *src_u = (const uint8_t *)data + mWidth * mHeight;
166        const uint8_t *src_v = src_u + (mWidth / 2 * mHeight / 2);
167
168        uint8_t *dst_y = (uint8_t *)dst;
169        size_t dst_y_size = buf->stride * buf->height;
170        size_t dst_c_stride = ALIGN(buf->stride / 2, 16);
171        size_t dst_c_size = dst_c_stride * buf->height / 2;
172        uint8_t *dst_v = dst_y + dst_y_size;
173        uint8_t *dst_u = dst_v + dst_c_size;
174
175        for (int y = 0; y < mHeight; ++y) {
176            memcpy(dst_y, src_y, mWidth);
177
178            src_y += mWidth;
179            dst_y += buf->stride;
180        }
181
182        for (int y = 0; y < (mHeight + 1) / 2; ++y) {
183            memcpy(dst_u, src_u, (mWidth + 1) / 2);
184            memcpy(dst_v, src_v, (mWidth + 1) / 2);
185
186            src_u += mWidth / 2;
187            src_v += mWidth / 2;
188            dst_u += dst_c_stride;
189            dst_v += dst_c_stride;
190        }
191    }
192
193    CHECK_EQ(0, mapper.unlock(buf->handle));
194
195    if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf)) != 0) {
196        LOGW("Surface::queueBuffer returned error %d", err);
197    }
198    buf = NULL;
199}
200
201}  // namespace android
202