SoftwareRenderer.cpp revision 89e7fff6a5d7410815f42b4a55958a59d4463180
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 <ui/ISurface.h>
26
27namespace android {
28
29SoftwareRenderer::SoftwareRenderer(
30        OMX_COLOR_FORMATTYPE colorFormat,
31        const sp<ISurface> &surface,
32        size_t displayWidth, size_t displayHeight,
33        size_t decodedWidth, size_t decodedHeight)
34    : mColorFormat(colorFormat),
35      mConverter(colorFormat, OMX_COLOR_Format16bitRGB565),
36      mISurface(surface),
37      mDisplayWidth(displayWidth),
38      mDisplayHeight(displayHeight),
39      mDecodedWidth(decodedWidth),
40      mDecodedHeight(decodedHeight),
41      mFrameSize(mDecodedWidth * mDecodedHeight * 2),  // RGB565
42      mIndex(0) {
43    // TODO: How do I allocate physical memory on Droid?
44    mMemoryHeap = new MemoryHeapBase("/dev/pmem_adsp", 2 * mFrameSize);
45    if (mMemoryHeap->heapID() < 0) {
46        LOGI("Creating physical memory heap failed, reverting to regular heap.");
47        mMemoryHeap = new MemoryHeapBase(2 * mFrameSize);
48    } else {
49        mMemoryHeap = new MemoryHeapPmem(mMemoryHeap);
50    }
51
52    CHECK(mISurface.get() != NULL);
53    CHECK(mDecodedWidth > 0);
54    CHECK(mDecodedHeight > 0);
55    CHECK(mMemoryHeap->heapID() >= 0);
56    CHECK(mConverter.isValid());
57
58    ISurface::BufferHeap bufferHeap(
59            mDisplayWidth, mDisplayHeight,
60            mDecodedWidth, mDecodedHeight,
61            PIXEL_FORMAT_RGB_565,
62            mMemoryHeap);
63
64    status_t err = mISurface->registerBuffers(bufferHeap);
65    CHECK_EQ(err, OK);
66}
67
68SoftwareRenderer::~SoftwareRenderer() {
69    mISurface->unregisterBuffers();
70}
71
72void SoftwareRenderer::render(
73        const void *data, size_t size, void *platformPrivate) {
74    size_t offset = mIndex * mFrameSize;
75    void *dst = (uint8_t *)mMemoryHeap->getBase() + offset;
76
77    mConverter.convert(
78            mDecodedWidth, mDecodedHeight,
79            data, 0, dst, 2 * mDecodedWidth);
80
81    mISurface->postBuffer(offset);
82    mIndex = 1 - mIndex;
83}
84
85}  // namespace android
86