SoftwareRenderer.cpp revision 733b7729ea462fae9c6899456444e28fef1c757c
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 <media/stagefright/MediaDebug.h>
24#include <ui/ISurface.h>
25
26namespace android {
27
28SoftwareRenderer::SoftwareRenderer(
29        OMX_COLOR_FORMATTYPE colorFormat,
30        const sp<ISurface> &surface,
31        size_t displayWidth, size_t displayHeight,
32        size_t decodedWidth, size_t decodedHeight)
33    : mColorFormat(colorFormat),
34      mConverter(colorFormat, OMX_COLOR_Format16bitRGB565),
35      mISurface(surface),
36      mDisplayWidth(displayWidth),
37      mDisplayHeight(displayHeight),
38      mDecodedWidth(decodedWidth),
39      mDecodedHeight(decodedHeight),
40      mFrameSize(mDecodedWidth * mDecodedHeight * 2),  // RGB565
41      mMemoryHeap(new MemoryHeapBase(2 * mFrameSize)),
42      mIndex(0) {
43    CHECK(mISurface.get() != NULL);
44    CHECK(mDecodedWidth > 0);
45    CHECK(mDecodedHeight > 0);
46    CHECK(mMemoryHeap->heapID() >= 0);
47    CHECK(mConverter.isValid());
48
49    ISurface::BufferHeap bufferHeap(
50            mDisplayWidth, mDisplayHeight,
51            mDecodedWidth, mDecodedHeight,
52            PIXEL_FORMAT_RGB_565,
53            mMemoryHeap);
54
55    status_t err = mISurface->registerBuffers(bufferHeap);
56    CHECK_EQ(err, OK);
57}
58
59SoftwareRenderer::~SoftwareRenderer() {
60    mISurface->unregisterBuffers();
61}
62
63void SoftwareRenderer::render(
64        const void *data, size_t size, void *platformPrivate) {
65    size_t offset = mIndex * mFrameSize;
66    void *dst = (uint8_t *)mMemoryHeap->getBase() + offset;
67
68    mConverter.convert(
69            mDecodedWidth, mDecodedHeight,
70            data, 0, dst, 2 * mDecodedWidth);
71
72    mISurface->postBuffer(offset);
73    mIndex = 1 - mIndex;
74}
75
76}  // namespace android
77