SoftwareRenderer.cpp revision 3cf613507f1e2f7bd932d921a6e222e426fd3be4
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/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    mMemoryHeap = new MemoryHeapBase("/dev/pmem_adsp", 2 * mFrameSize);
44    if (mMemoryHeap->heapID() < 0) {
45        LOGI("Creating physical memory heap failed, reverting to regular heap.");
46        mMemoryHeap = new MemoryHeapBase(2 * mFrameSize);
47    } else {
48        mMemoryHeap = new MemoryHeapPmem(mMemoryHeap);
49    }
50
51    CHECK(mISurface.get() != NULL);
52    CHECK(mDecodedWidth > 0);
53    CHECK(mDecodedHeight > 0);
54    CHECK(mMemoryHeap->heapID() >= 0);
55    CHECK(mConverter.isValid());
56
57    ISurface::BufferHeap bufferHeap(
58            mDisplayWidth, mDisplayHeight,
59            mDecodedWidth, mDecodedHeight,
60            PIXEL_FORMAT_RGB_565,
61            mMemoryHeap);
62
63    status_t err = mISurface->registerBuffers(bufferHeap);
64    CHECK_EQ(err, OK);
65}
66
67SoftwareRenderer::~SoftwareRenderer() {
68    mISurface->unregisterBuffers();
69}
70
71void SoftwareRenderer::render(
72        const void *data, size_t size, void *platformPrivate) {
73    size_t offset = mIndex * mFrameSize;
74    void *dst = (uint8_t *)mMemoryHeap->getBase() + offset;
75
76    mConverter.convert(
77            mDecodedWidth, mDecodedHeight,
78            data, 0, dst, 2 * mDecodedWidth);
79
80    mISurface->postBuffer(offset);
81    mIndex = 1 - mIndex;
82}
83
84}  // namespace android
85