Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 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 | |
Andreas Huber | bd7b43b | 2009-10-13 10:22:55 -0700 | [diff] [blame] | 20 | #include "../include/SoftwareRenderer.h" |
| 21 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 22 | #include <binder/MemoryHeapBase.h> |
Andreas Huber | cfde87b | 2010-01-29 14:56:33 -0800 | [diff] [blame] | 23 | #include <binder/MemoryHeapPmem.h> |
Andreas Huber | b5ceb9e | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 24 | #include <media/stagefright/MediaDebug.h> |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame^] | 25 | #include <surfaceflinger/Surface.h> |
| 26 | #include <ui/android_native_buffer.h> |
| 27 | #include <ui/GraphicBufferMapper.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 31 | SoftwareRenderer::SoftwareRenderer( |
Andreas Huber | c966a3c | 2009-09-15 14:32:59 -0700 | [diff] [blame] | 32 | OMX_COLOR_FORMATTYPE colorFormat, |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame^] | 33 | const sp<Surface> &surface, |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 34 | size_t displayWidth, size_t displayHeight, |
| 35 | size_t decodedWidth, size_t decodedHeight) |
Andreas Huber | c966a3c | 2009-09-15 14:32:59 -0700 | [diff] [blame] | 36 | : mColorFormat(colorFormat), |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame^] | 37 | mConverter(NULL), |
| 38 | mYUVMode(None), |
| 39 | mSurface(surface), |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 40 | mDisplayWidth(displayWidth), |
| 41 | mDisplayHeight(displayHeight), |
| 42 | mDecodedWidth(decodedWidth), |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame^] | 43 | mDecodedHeight(decodedHeight) { |
| 44 | LOGI("input format = %d", mColorFormat); |
| 45 | LOGI("display = %d x %d, decoded = %d x %d", |
| 46 | mDisplayWidth, mDisplayHeight, mDecodedWidth, mDecodedHeight); |
| 47 | |
| 48 | int halFormat; |
| 49 | switch (mColorFormat) { |
| 50 | #if HAS_YCBCR420_SP_ADRENO |
| 51 | case OMX_COLOR_FormatYUV420Planar: |
| 52 | { |
| 53 | halFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO; |
| 54 | mYUVMode = YUV420ToYUV420sp; |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | case 0x7fa30c00: |
| 59 | { |
| 60 | halFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO; |
| 61 | mYUVMode = YUV420spToYUV420sp; |
| 62 | break; |
| 63 | } |
| 64 | #endif |
| 65 | |
| 66 | default: |
| 67 | halFormat = HAL_PIXEL_FORMAT_RGB_565; |
| 68 | |
| 69 | mConverter = new ColorConverter( |
| 70 | mColorFormat, OMX_COLOR_Format16bitRGB565); |
| 71 | CHECK(mConverter->isValid()); |
| 72 | break; |
Andreas Huber | cfde87b | 2010-01-29 14:56:33 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame^] | 75 | CHECK(mSurface.get() != NULL); |
Andreas Huber | b5ceb9e | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 76 | CHECK(mDecodedWidth > 0); |
| 77 | CHECK(mDecodedHeight > 0); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame^] | 78 | CHECK(mConverter == NULL || mConverter->isValid()); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 79 | |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame^] | 80 | CHECK_EQ(0, |
| 81 | native_window_set_usage( |
| 82 | mSurface.get(), |
| 83 | GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN |
| 84 | | GRALLOC_USAGE_HW_TEXTURE)); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 85 | |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame^] | 86 | CHECK_EQ(0, native_window_set_buffer_count(mSurface.get(), 2)); |
| 87 | |
| 88 | // Width must be multiple of 32??? |
| 89 | CHECK_EQ(0, native_window_set_buffers_geometry( |
| 90 | mSurface.get(), mDecodedWidth, mDecodedHeight, |
| 91 | halFormat)); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | SoftwareRenderer::~SoftwareRenderer() { |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame^] | 95 | delete mConverter; |
| 96 | mConverter = NULL; |
| 97 | } |
| 98 | |
| 99 | static inline size_t ALIGN(size_t x, size_t alignment) { |
| 100 | return (x + alignment - 1) & ~(alignment - 1); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void SoftwareRenderer::render( |
| 104 | const void *data, size_t size, void *platformPrivate) { |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame^] | 105 | android_native_buffer_t *buf; |
| 106 | CHECK_EQ(0, mSurface->dequeueBuffer(mSurface.get(), &buf)); |
| 107 | CHECK_EQ(0, mSurface->lockBuffer(mSurface.get(), buf)); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 108 | |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame^] | 109 | GraphicBufferMapper &mapper = GraphicBufferMapper::get(); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 110 | |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame^] | 111 | Rect bounds(mDecodedWidth, mDecodedHeight); |
| 112 | |
| 113 | void *dst; |
| 114 | CHECK_EQ(0, mapper.lock( |
| 115 | buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst)); |
| 116 | |
| 117 | if (mConverter) { |
| 118 | mConverter->convert( |
| 119 | mDecodedWidth, mDecodedHeight, |
| 120 | data, 0, dst, buf->stride * 2); |
| 121 | } else if (mYUVMode == YUV420spToYUV420sp) { |
| 122 | // Input and output are both YUV420sp, but the alignment requirements |
| 123 | // are different. |
| 124 | size_t srcYStride = mDecodedWidth; |
| 125 | const uint8_t *srcY = (const uint8_t *)data; |
| 126 | uint8_t *dstY = (uint8_t *)dst; |
| 127 | for (size_t i = 0; i < mDecodedHeight; ++i) { |
| 128 | memcpy(dstY, srcY, mDecodedWidth); |
| 129 | srcY += srcYStride; |
| 130 | dstY += buf->stride; |
| 131 | } |
| 132 | |
| 133 | size_t srcUVStride = (mDecodedWidth + 1) & ~1; |
| 134 | size_t dstUVStride = ALIGN(mDecodedWidth / 2, 32) * 2; |
| 135 | |
| 136 | const uint8_t *srcUV = (const uint8_t *)data |
| 137 | + mDecodedHeight * mDecodedWidth; |
| 138 | |
| 139 | size_t dstUVOffset = ALIGN(ALIGN(mDecodedHeight, 32) * buf->stride, 4096); |
| 140 | uint8_t *dstUV = (uint8_t *)dst + dstUVOffset; |
| 141 | |
| 142 | for (size_t i = 0; i < (mDecodedHeight + 1) / 2; ++i) { |
| 143 | memcpy(dstUV, srcUV, (mDecodedWidth + 1) & ~1); |
| 144 | srcUV += srcUVStride; |
| 145 | dstUV += dstUVStride; |
| 146 | } |
| 147 | } else if (mYUVMode == YUV420ToYUV420sp) { |
| 148 | // Input is YUV420 planar, output is YUV420sp, adhere to proper |
| 149 | // alignment requirements. |
| 150 | size_t srcYStride = mDecodedWidth; |
| 151 | const uint8_t *srcY = (const uint8_t *)data; |
| 152 | uint8_t *dstY = (uint8_t *)dst; |
| 153 | for (size_t i = 0; i < mDecodedHeight; ++i) { |
| 154 | memcpy(dstY, srcY, mDecodedWidth); |
| 155 | srcY += srcYStride; |
| 156 | dstY += buf->stride; |
| 157 | } |
| 158 | |
| 159 | size_t srcUVStride = (mDecodedWidth + 1) / 2; |
| 160 | size_t dstUVStride = ALIGN(mDecodedWidth / 2, 32) * 2; |
| 161 | |
| 162 | const uint8_t *srcU = (const uint8_t *)data |
| 163 | + mDecodedHeight * mDecodedWidth; |
| 164 | |
| 165 | const uint8_t *srcV = |
| 166 | srcU + ((mDecodedWidth + 1) / 2) * ((mDecodedHeight + 1) / 2); |
| 167 | |
| 168 | size_t dstUVOffset = ALIGN(ALIGN(mDecodedHeight, 32) * buf->stride, 4096); |
| 169 | uint8_t *dstUV = (uint8_t *)dst + dstUVOffset; |
| 170 | |
| 171 | for (size_t i = 0; i < (mDecodedHeight + 1) / 2; ++i) { |
| 172 | for (size_t j = 0; j < (mDecodedWidth + 1) / 2; ++j) { |
| 173 | dstUV[2 * j + 1] = srcU[j]; |
| 174 | dstUV[2 * j] = srcV[j]; |
| 175 | } |
| 176 | srcU += srcUVStride; |
| 177 | srcV += srcUVStride; |
| 178 | dstUV += dstUVStride; |
| 179 | } |
| 180 | } else { |
| 181 | memcpy(dst, data, size); |
| 182 | } |
| 183 | |
| 184 | CHECK_EQ(0, mapper.unlock(buf->handle)); |
| 185 | |
| 186 | CHECK_EQ(0, mSurface->queueBuffer(mSurface.get(), buf)); |
| 187 | buf = NULL; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 190 | } // namespace android |