summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/lvpp/PreviewRenderer.cpp
blob: 36fc9693453d26615be21eea3de2292995c76ad2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#define LOG_NDEBUG 1
#define LOG_TAG "PreviewRenderer"
#include <utils/Log.h>

#include "PreviewRenderer.h"

#include <binder/MemoryHeapBase.h>
#include <binder/MemoryHeapPmem.h>
#include <media/stagefright/MediaDebug.h>
#include <surfaceflinger/Surface.h>

namespace android {

PreviewRenderer* PreviewRenderer::CreatePreviewRenderer (OMX_COLOR_FORMATTYPE colorFormat,
        const sp<Surface> &surface,
        size_t displayWidth, size_t displayHeight,
        size_t decodedWidth, size_t decodedHeight,
        int32_t rotationDegrees) {

        PreviewRenderer* returnCtx =
            new PreviewRenderer(colorFormat,
            surface,
            displayWidth, displayHeight,
            decodedWidth, decodedHeight,
            rotationDegrees);

        int result = 0;

        int halFormat;
        switch (returnCtx->mColorFormat) {
            case OMX_COLOR_FormatYUV420Planar:
            {
                halFormat = HAL_PIXEL_FORMAT_YV12;
                returnCtx->mYUVMode = None;
                break;
            }
            default:
                halFormat = HAL_PIXEL_FORMAT_RGB_565;

                returnCtx->mConverter = new ColorConverter(
                        returnCtx->mColorFormat, OMX_COLOR_Format16bitRGB565);
                CHECK(returnCtx->mConverter->isValid());
                break;
        }

        CHECK(returnCtx->mSurface.get() != NULL);
        CHECK(returnCtx->mDecodedWidth > 0);
        CHECK(returnCtx->mDecodedHeight > 0);
        CHECK(returnCtx->mConverter == NULL || returnCtx->mConverter->isValid());

        result = native_window_set_usage(
                    returnCtx->mSurface.get(),
                    GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN
                    | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP);

        if ( result == 0 ) {
            result = native_window_set_buffer_count(returnCtx->mSurface.get(), 3);

            if (result == 0) {
                result = native_window_set_buffers_geometry(
                    returnCtx->mSurface.get(), returnCtx->mDecodedWidth, returnCtx->mDecodedHeight,
                    halFormat);
                if ( result == 0) {
                    uint32_t transform;
                    switch (rotationDegrees) {
                        case 0: transform = 0; break;
                        case 90: transform = HAL_TRANSFORM_ROT_90; break;
                        case 180: transform = HAL_TRANSFORM_ROT_180; break;
                        case 270: transform = HAL_TRANSFORM_ROT_270; break;
                        default: transform = 0; break;
                    }
                    if (transform) {
                        result = native_window_set_buffers_transform(
                                    returnCtx->mSurface.get(), transform);

                    }
                }
            }
        }

        if ( result != 0 )
        {
            /* free the ctx */
            returnCtx->~PreviewRenderer();
            return NULL;
        }

        return returnCtx;
}

PreviewRenderer::PreviewRenderer(
        OMX_COLOR_FORMATTYPE colorFormat,
        const sp<Surface> &surface,
        size_t displayWidth, size_t displayHeight,
        size_t decodedWidth, size_t decodedHeight,
        int32_t rotationDegrees)
    : mColorFormat(colorFormat),
      mConverter(NULL),
      mYUVMode(None),
      mSurface(surface),
      mDisplayWidth(displayWidth),
      mDisplayHeight(displayHeight),
      mDecodedWidth(decodedWidth),
      mDecodedHeight(decodedHeight) {

    LOGV("input format = %d", mColorFormat);
    LOGV("display = %d x %d, decoded = %d x %d",
            mDisplayWidth, mDisplayHeight, mDecodedWidth, mDecodedHeight);

    mDecodedWidth = mDisplayWidth;
    mDecodedHeight = mDisplayHeight;
}

PreviewRenderer::~PreviewRenderer() {
    delete mConverter;
    mConverter = NULL;
}


//
// Provides a buffer and associated stride
// This buffer is allocated by the SurfaceFlinger
//
// For optimal display performances, you should :
// 1) call getBufferYV12()
// 2) fill the buffer with your data
// 3) call renderYV12() to take these changes into account
//
// For each call to getBufferYV12(), you must also call renderYV12()
// Expected format in the buffer is YV12 formats (similar to YUV420 planar fromat)
// for more details on this YV12 cf hardware/libhardware/include/hardware/hardware.h
//
void PreviewRenderer::getBufferYV12(uint8_t **data, size_t *stride) {
    int err = OK;
    LOGV("getBuffer START");

    if ((err = mSurface->ANativeWindow::dequeueBuffer(mSurface.get(), &mBuf)) != 0) {
        LOGW("Surface::dequeueBuffer returned error %d", err);
        return;
    }

    CHECK_EQ(0, mSurface->ANativeWindow::lockBuffer(mSurface.get(), mBuf));

    GraphicBufferMapper &mapper = GraphicBufferMapper::get();

    Rect bounds(mDecodedWidth, mDecodedHeight);

    void *dst;
    CHECK_EQ(0, mapper.lock(
                mBuf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst));
    LOGV("Buffer locked");

    *data   = (uint8_t*)dst;
    *stride = mBuf->stride;

    LOGV("getBuffer END %p %d", dst, mBuf->stride);
}


//
// Display the content of the buffer provided by last call to getBufferYV12()
//
// See getBufferYV12() for details.
//
void PreviewRenderer::renderYV12() {
    LOGV("renderYV12() START");
    int err = OK;

    GraphicBufferMapper &mapper = GraphicBufferMapper::get();

    if (mBuf!= NULL) {
        CHECK_EQ(0, mapper.unlock(mBuf->handle));

        if ((err = mSurface->ANativeWindow::queueBuffer(mSurface.get(), mBuf)) != 0) {
            LOGW("Surface::queueBuffer returned error %d", err);
        }
    }
    mBuf = NULL;
    LOGV("renderYV12() END");
}



//
// Display the given data buffer
// platformPrivate is not used (kept for backwrad compatibility)
// Please rather use getbuffer() and the other render()functions (with no params)
// for optimal display
//
void PreviewRenderer::render(
        const void *data, size_t size, void *platformPrivate) {
    ANativeWindowBuffer *buf;
    int err;

    if ((err = mSurface->ANativeWindow::dequeueBuffer(mSurface.get(), &buf)) != 0) {
        LOGW("Surface::dequeueBuffer returned error %d", err);
        return;
    }

    CHECK_EQ(0, mSurface->ANativeWindow::lockBuffer(mSurface.get(), buf));

    GraphicBufferMapper &mapper = GraphicBufferMapper::get();

    Rect bounds(mDecodedWidth, mDecodedHeight);

    void *dst;
    CHECK_EQ(0, mapper.lock(
                buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst));
    LOGV("Buffer locked");

    if (mConverter) {
        LOGV("Convert to RGB565");
        mConverter->convert(data,
                mDecodedWidth, mDecodedHeight,
                0,0,mDecodedWidth, mDecodedHeight,
                dst, mDecodedWidth, mDecodedHeight,
                0,0,mDecodedWidth, mDecodedHeight);
    } else if (mYUVMode == None) {
        // Input and output are both YUV420sp, but the alignment requirements
        // are different.
        LOGV("mYUVMode == None %d x %d", mDecodedWidth, mDecodedHeight);
        size_t srcYStride = mDecodedWidth;
        const uint8_t *srcY = (const uint8_t *)data;
        uint8_t *dstY = (uint8_t *)dst;
        LOGV("srcY =       %p   dstY =       %p", srcY, dstY);
        LOGV("srcYStride = %d   dstYstride = %d", srcYStride, buf->stride);
        for (size_t i = 0; i < mDecodedHeight; ++i) {
            memcpy(dstY, srcY, mDecodedWidth);
            srcY += srcYStride;
            dstY += buf->stride;
        }

        size_t srcUVStride = (mDecodedWidth + 1) / 2;
        size_t dstUVStride = ALIGN(mDecodedWidth / 2, 32);
        LOGV("srcUVStride = %d   dstUVStride = %d", srcUVStride, dstUVStride);

        // Copy V
        // Source buffer is YUV, skip U
        const uint8_t *srcV = (const uint8_t *)data
                + mDecodedHeight * mDecodedWidth + (mDecodedHeight * mDecodedWidth)/4;
        // Destination buffer is YVU
        uint8_t *dstUV = (uint8_t *)dst
                + buf->stride*mDecodedHeight;
        LOGV("srcV =       %p   dstUV =       %p", srcV, dstUV);
        for (size_t i = 0; i < (mDecodedHeight+1)/2; ++i) {
            memcpy(dstUV, srcV, mDecodedWidth/2);
            srcV += srcUVStride;
            dstUV += dstUVStride;
        }


        // Copy V
        // Source buffer is YUV, go back to end of Y
        const uint8_t *srcU = (const uint8_t *)data
            + mDecodedHeight * mDecodedWidth ;
        // Destination buffer is YVU
        // Keep writing after V buffer has been filled, U follows immediately
        LOGV("srcU =       %p   dstUV =       %p", srcU, dstUV);
        for (size_t i = 0; i < (mDecodedHeight+1)/2; ++i) {
            memcpy(dstUV, srcU, mDecodedWidth/2);
            srcU += srcUVStride;
            dstUV += dstUVStride;
        }
    } else {
        memcpy(dst, data, size);
    }

    CHECK_EQ(0, mapper.unlock(buf->handle));

    if ((err = mSurface->ANativeWindow::queueBuffer(mSurface.get(), buf)) != 0) {
        LOGW("Surface::queueBuffer returned error %d", err);
    }
    buf = NULL;
}

}  // namespace android