SoftwareRenderer.cpp revision 1173118eace0e9e347cb007f0da817cee87579ed
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    switch (mColorFormat) {
60        default:
61            halFormat = HAL_PIXEL_FORMAT_RGB_565;
62
63            mConverter = new ColorConverter(
64                    mColorFormat, OMX_COLOR_Format16bitRGB565);
65            CHECK(mConverter->isValid());
66            break;
67    }
68
69    CHECK(mNativeWindow != NULL);
70    CHECK(mWidth > 0);
71    CHECK(mHeight > 0);
72    CHECK(mConverter == NULL || mConverter->isValid());
73
74    CHECK_EQ(0,
75            native_window_set_usage(
76            mNativeWindow.get(),
77            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN
78            | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP));
79
80    CHECK_EQ(0, native_window_set_buffer_count(mNativeWindow.get(), 2));
81
82    // Width must be multiple of 32???
83    CHECK_EQ(0, native_window_set_buffers_geometry(
84                mNativeWindow.get(),
85                mCropRight - mCropLeft + 1,
86                mCropBottom - mCropTop + 1,
87                halFormat));
88
89    uint32_t transform;
90    switch (rotationDegrees) {
91        case 0: transform = 0; break;
92        case 90: transform = HAL_TRANSFORM_ROT_90; break;
93        case 180: transform = HAL_TRANSFORM_ROT_180; break;
94        case 270: transform = HAL_TRANSFORM_ROT_270; break;
95        default: transform = 0; break;
96    }
97
98    if (transform) {
99        CHECK_EQ(0, native_window_set_buffers_transform(
100                    mNativeWindow.get(), transform));
101    }
102}
103
104SoftwareRenderer::~SoftwareRenderer() {
105    delete mConverter;
106    mConverter = NULL;
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 = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf)) != 0) {
114        LOGW("Surface::dequeueBuffer returned error %d", err);
115        return;
116    }
117
118    CHECK_EQ(0, mNativeWindow->lockBuffer(mNativeWindow.get(), buf));
119
120    GraphicBufferMapper &mapper = GraphicBufferMapper::get();
121
122    Rect bounds(mWidth, mHeight);
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                data,
131                mWidth, mHeight,
132                mCropLeft, mCropTop, mCropRight, mCropBottom,
133                dst,
134                buf->stride, buf->height,
135                0, 0,
136                mCropRight - mCropLeft,
137                mCropBottom - mCropTop);
138    } else {
139        TRESPASS();
140    }
141
142    CHECK_EQ(0, mapper.unlock(buf->handle));
143
144    if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf)) != 0) {
145        LOGW("Surface::queueBuffer returned error %d", err);
146    }
147    buf = NULL;
148}
149
150}  // namespace android
151