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