SoftwareRenderer.cpp revision 0caaa95e11b700440f64e110b11f77a93acfa569
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        sp<MemoryHeapPmem> pmemHeap = new MemoryHeapPmem(mMemoryHeap);
49        pmemHeap->slap();
50        mMemoryHeap = pmemHeap;
51    }
52
53    CHECK(mISurface.get() != NULL);
54    CHECK(mDecodedWidth > 0);
55    CHECK(mDecodedHeight > 0);
56    CHECK(mMemoryHeap->heapID() >= 0);
57    CHECK(mConverter.isValid());
58
59    ISurface::BufferHeap bufferHeap(
60            mDisplayWidth, mDisplayHeight,
61            mDecodedWidth, mDecodedHeight,
62            PIXEL_FORMAT_RGB_565,
63            mMemoryHeap);
64
65    status_t err = mISurface->registerBuffers(bufferHeap);
66    CHECK_EQ(err, OK);
67}
68
69SoftwareRenderer::~SoftwareRenderer() {
70    mISurface->unregisterBuffers();
71}
72
73void SoftwareRenderer::render(
74        const void *data, size_t size, void *platformPrivate) {
75    size_t offset = mIndex * mFrameSize;
76    void *dst = (uint8_t *)mMemoryHeap->getBase() + offset;
77
78    mConverter.convert(
79            mDecodedWidth, mDecodedHeight,
80            data, 0, dst, 2 * mDecodedWidth);
81
82    mISurface->postBuffer(offset);
83    mIndex = 1 - mIndex;
84}
85
86}  // namespace android
87