1 /*
2 * Copyright (C) 2011 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
18 #define LOG_NDEBUG 1
19 #define LOG_TAG "PreviewRenderer"
20 #include <utils/Log.h>
21
22 #include "PreviewRenderer.h"
23
24 #include <media/stagefright/foundation/ADebug.h>
25 #include <gui/Surface.h>
26
27 namespace android {
28
CreatePreviewRenderer(const sp<Surface> & surface,size_t width,size_t height)29 PreviewRenderer* PreviewRenderer::CreatePreviewRenderer (
30 const sp<Surface> &surface, size_t width, size_t height) {
31
32 PreviewRenderer* renderer = new PreviewRenderer(surface, width, height);
33
34 if (renderer->init() != 0) {
35 delete renderer;
36 return NULL;
37 }
38
39 return renderer;
40 }
41
PreviewRenderer(const sp<Surface> & surface,size_t width,size_t height)42 PreviewRenderer::PreviewRenderer(
43 const sp<Surface> &surface,
44 size_t width, size_t height)
45 : mSurface(surface),
46 mWidth(width),
47 mHeight(height) {
48 }
49
init()50 int PreviewRenderer::init() {
51 int err = 0;
52 ANativeWindow* anw = mSurface.get();
53
54 err = native_window_api_connect(anw, NATIVE_WINDOW_API_CPU);
55 if (err) goto fail;
56
57 err = native_window_set_usage(
58 anw, GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN);
59 if (err) goto fail;
60
61 err = native_window_set_buffer_count(anw, 3);
62 if (err) goto fail;
63
64 err = native_window_set_scaling_mode(
65 anw, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
66 if (err) goto fail;
67
68 err = native_window_set_buffers_geometry(
69 anw, mWidth, mHeight, HAL_PIXEL_FORMAT_YV12);
70 if (err) goto fail;
71
72 err = native_window_set_buffers_transform(anw, 0);
73 if (err) goto fail;
74
75 fail:
76 return err;
77 }
78
~PreviewRenderer()79 PreviewRenderer::~PreviewRenderer() {
80 native_window_api_disconnect(mSurface.get(), NATIVE_WINDOW_API_CPU);
81 }
82
83
84 //
85 // Provides a buffer and associated stride
86 // This buffer is allocated by the SurfaceFlinger
87 //
88 // For optimal display performances, you should :
89 // 1) call getBufferYV12()
90 // 2) fill the buffer with your data
91 // 3) call renderYV12() to take these changes into account
92 //
93 // For each call to getBufferYV12(), you must also call renderYV12()
94 // Expected format in the buffer is YV12 formats (similar to YUV420 planar fromat)
95 // for more details on this YV12 cf hardware/libhardware/include/hardware/hardware.h
96 //
getBufferYV12(uint8_t ** data,size_t * stride)97 void PreviewRenderer::getBufferYV12(uint8_t **data, size_t *stride) {
98 int err = OK;
99
100 if ((err = mSurface->ANativeWindow::dequeueBuffer(mSurface.get(), &mBuf)) != 0) {
101 ALOGW("Surface::dequeueBuffer returned error %d", err);
102 return;
103 }
104
105 CHECK_EQ(0, mSurface->ANativeWindow::lockBuffer(mSurface.get(), mBuf));
106
107 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
108
109 Rect bounds(mWidth, mHeight);
110
111 void *dst;
112 CHECK_EQ(0, mapper.lock(mBuf->handle,
113 GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
114 bounds, &dst));
115
116 *data = (uint8_t*)dst;
117 *stride = mBuf->stride;
118 }
119
120
121 //
122 // Display the content of the buffer provided by last call to getBufferYV12()
123 //
124 // See getBufferYV12() for details.
125 //
renderYV12()126 void PreviewRenderer::renderYV12() {
127 int err = OK;
128
129 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
130
131 if (mBuf!= NULL) {
132 CHECK_EQ(0, mapper.unlock(mBuf->handle));
133
134 if ((err = mSurface->ANativeWindow::queueBuffer(mSurface.get(), mBuf)) != 0) {
135 ALOGW("Surface::queueBuffer returned error %d", err);
136 }
137 }
138 mBuf = NULL;
139 }
140
141 } // namespace android
142