SoftwareRenderer.cpp revision 3778838eee9470825bea7e5bad376f1dc9a94392
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#ifndef THIS_IS_CRESPO
63        case OMX_COLOR_FormatYUV420Planar:
64        {
65            halFormat = HAL_PIXEL_FORMAT_YV12;
66            bufWidth = (mWidth + 1) & ~1;
67            bufHeight = (mHeight + 1) & ~1;
68            break;
69        }
70#endif
71
72        default:
73            halFormat = HAL_PIXEL_FORMAT_RGB_565;
74            bufWidth = mWidth;
75            bufHeight = mHeight;
76
77            mConverter = new ColorConverter(
78                    mColorFormat, OMX_COLOR_Format16bitRGB565);
79            CHECK(mConverter->isValid());
80            break;
81    }
82
83    CHECK(mNativeWindow != NULL);
84    CHECK(mWidth > 0);
85    CHECK(mHeight > 0);
86    CHECK(mConverter == NULL || mConverter->isValid());
87
88    CHECK_EQ(0,
89            native_window_set_usage(
90            mNativeWindow.get(),
91            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN
92            | GRALLOC_USAGE_HW_TEXTURE
93#ifndef THIS_IS_CRESPO
94            | GRALLOC_USAGE_EXTERNAL_DISP
95#endif
96            ));
97
98    // Width must be multiple of 32???
99    CHECK_EQ(0, native_window_set_buffers_geometry(
100                mNativeWindow.get(),
101                bufWidth,
102                bufHeight,
103                halFormat));
104
105    uint32_t transform;
106    switch (rotationDegrees) {
107        case 0: transform = 0; break;
108        case 90: transform = HAL_TRANSFORM_ROT_90; break;
109        case 180: transform = HAL_TRANSFORM_ROT_180; break;
110        case 270: transform = HAL_TRANSFORM_ROT_270; break;
111        default: transform = 0; break;
112    }
113
114    if (transform) {
115        CHECK_EQ(0, native_window_set_buffers_transform(
116                    mNativeWindow.get(), transform));
117    }
118
119    android_native_rect_t crop;
120    crop.left = mCropLeft;
121    crop.top = mCropTop;
122    crop.right = mCropRight + 1;
123    crop.bottom = mCropBottom + 1;
124
125    CHECK_EQ(0, native_window_set_crop(mNativeWindow.get(), &crop));
126}
127
128SoftwareRenderer::~SoftwareRenderer() {
129    delete mConverter;
130    mConverter = NULL;
131}
132
133static int ALIGN(int x, int y) {
134    // y must be a power of 2.
135    return (x + y - 1) & ~(y - 1);
136}
137
138void SoftwareRenderer::render(
139        const void *data, size_t size, void *platformPrivate) {
140    ANativeWindowBuffer *buf;
141    int err;
142    if ((err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf)) != 0) {
143        LOGW("Surface::dequeueBuffer returned error %d", err);
144        return;
145    }
146
147    CHECK_EQ(0, mNativeWindow->lockBuffer(mNativeWindow.get(), buf));
148
149    GraphicBufferMapper &mapper = GraphicBufferMapper::get();
150
151    Rect bounds(mWidth, mHeight);
152
153    void *dst;
154    CHECK_EQ(0, mapper.lock(
155                buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst));
156
157    if (mConverter) {
158        mConverter->convert(
159                data,
160                mWidth, mHeight,
161                0, 0, mWidth - 1, mHeight - 1,
162                dst,
163                buf->stride, buf->height,
164                0, 0, mWidth - 1, mHeight - 1);
165    } else {
166        CHECK_EQ(mColorFormat, OMX_COLOR_FormatYUV420Planar);
167
168        const uint8_t *src_y = (const uint8_t *)data;
169        const uint8_t *src_u = (const uint8_t *)data + mWidth * mHeight;
170        const uint8_t *src_v = src_u + (mWidth / 2 * mHeight / 2);
171
172        uint8_t *dst_y = (uint8_t *)dst;
173        size_t dst_y_size = buf->stride * buf->height;
174        size_t dst_c_stride = ALIGN(buf->stride / 2, 16);
175        size_t dst_c_size = dst_c_stride * buf->height / 2;
176        uint8_t *dst_v = dst_y + dst_y_size;
177        uint8_t *dst_u = dst_v + dst_c_size;
178
179        for (int y = 0; y < mHeight; ++y) {
180            memcpy(dst_y, src_y, mWidth);
181
182            src_y += mWidth;
183            dst_y += buf->stride;
184        }
185
186        for (int y = 0; y < (mHeight + 1) / 2; ++y) {
187            memcpy(dst_u, src_u, (mWidth + 1) / 2);
188            memcpy(dst_v, src_v, (mWidth + 1) / 2);
189
190            src_u += mWidth / 2;
191            src_v += mWidth / 2;
192            dst_u += dst_c_stride;
193            dst_v += dst_c_stride;
194        }
195    }
196
197    CHECK_EQ(0, mapper.unlock(buf->handle));
198
199    if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf)) != 0) {
200        LOGW("Surface::queueBuffer returned error %d", err);
201    }
202    buf = NULL;
203}
204
205}  // namespace android
206