Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018, 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_NDEBUG 0 |
| 18 | #define LOG_TAG "Codec2BufferUtils" |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 19 | #define ATRACE_TAG ATRACE_TAG_VIDEO |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 20 | #include <utils/Log.h> |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 21 | #include <utils/Trace.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 22 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 23 | #include <libyuv.h> |
| 24 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 25 | #include <list> |
| 26 | #include <mutex> |
| 27 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 28 | #include <android/hardware_buffer.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 29 | #include <media/hardware/HardwareAPI.h> |
| 30 | #include <media/stagefright/foundation/AUtils.h> |
| 31 | |
| 32 | #include <C2Debug.h> |
| 33 | |
| 34 | #include "Codec2BufferUtils.h" |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | namespace { |
| 39 | |
| 40 | /** |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 41 | * A flippable, optimizable memcpy. Constructs such as (from ? src : dst) |
| 42 | * do not work as the results are always const. |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 43 | */ |
| 44 | template<bool ToA, size_t S> |
| 45 | struct MemCopier { |
| 46 | template<typename A, typename B> |
| 47 | inline static void copy(A *a, const B *b, size_t size) { |
| 48 | __builtin_memcpy(a, b, size); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | template<size_t S> |
| 53 | struct MemCopier<false, S> { |
| 54 | template<typename A, typename B> |
| 55 | inline static void copy(const A *a, B *b, size_t size) { |
| 56 | MemCopier<true, S>::copy(b, a, size); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * Copies between a MediaImage and a graphic view. |
| 62 | * |
| 63 | * \param ToMediaImage whether to copy to (or from) the MediaImage |
| 64 | * \param view graphic view (could be ConstGraphicView or GraphicView depending on direction) |
| 65 | * \param img MediaImage data |
| 66 | * \param imgBase base of MediaImage (could be const uint8_t* or uint8_t* depending on direction) |
| 67 | */ |
| 68 | template<bool ToMediaImage, typename View, typename ImagePixel> |
| 69 | static status_t _ImageCopy(View &view, const MediaImage2 *img, ImagePixel *imgBase) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 70 | // TODO: more efficient copying --- e.g. copy interleaved planes together, etc. |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 71 | const C2PlanarLayout &layout = view.layout(); |
| 72 | const size_t bpp = divUp(img->mBitDepthAllocated, 8u); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 73 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 74 | for (uint32_t i = 0; i < layout.numPlanes; ++i) { |
| 75 | typename std::conditional<ToMediaImage, uint8_t, const uint8_t>::type *imgRow = |
| 76 | imgBase + img->mPlane[i].mOffset; |
| 77 | typename std::conditional<ToMediaImage, const uint8_t, uint8_t>::type *viewRow = |
| 78 | viewRow = view.data()[i]; |
| 79 | const C2PlaneInfo &plane = layout.planes[i]; |
| 80 | if (plane.colSampling != img->mPlane[i].mHorizSubsampling |
| 81 | || plane.rowSampling != img->mPlane[i].mVertSubsampling |
| 82 | || plane.allocatedDepth != img->mBitDepthAllocated |
| 83 | || plane.allocatedDepth < plane.bitDepth |
| 84 | // MediaImage only supports MSB values |
| 85 | || plane.rightShift != plane.allocatedDepth - plane.bitDepth |
| 86 | || (bpp > 1 && plane.endianness != plane.NATIVE)) { |
| 87 | return BAD_VALUE; |
| 88 | } |
| 89 | |
| 90 | uint32_t planeW = img->mWidth / plane.colSampling; |
| 91 | uint32_t planeH = img->mHeight / plane.rowSampling; |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 92 | |
Praveen Chavan | bc2b713 | 2022-01-19 23:02:56 -0800 | [diff] [blame] | 93 | bool canCopyByRow = (plane.colInc == bpp) && (img->mPlane[i].mColInc == bpp); |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 94 | bool canCopyByPlane = canCopyByRow && (plane.rowInc == img->mPlane[i].mRowInc); |
| 95 | if (canCopyByPlane) { |
| 96 | MemCopier<ToMediaImage, 0>::copy(imgRow, viewRow, plane.rowInc * planeH); |
| 97 | } else if (canCopyByRow) { |
| 98 | for (uint32_t row = 0; row < planeH; ++row) { |
| 99 | MemCopier<ToMediaImage, 0>::copy( |
| 100 | imgRow, viewRow, std::min(plane.rowInc, img->mPlane[i].mRowInc)); |
| 101 | imgRow += img->mPlane[i].mRowInc; |
| 102 | viewRow += plane.rowInc; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 103 | } |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 104 | } else { |
| 105 | for (uint32_t row = 0; row < planeH; ++row) { |
| 106 | decltype(imgRow) imgPtr = imgRow; |
| 107 | decltype(viewRow) viewPtr = viewRow; |
| 108 | for (uint32_t col = 0; col < planeW; ++col) { |
| 109 | MemCopier<ToMediaImage, 0>::copy(imgPtr, viewPtr, bpp); |
| 110 | imgPtr += img->mPlane[i].mColInc; |
| 111 | viewPtr += plane.colInc; |
| 112 | } |
| 113 | imgRow += img->mPlane[i].mRowInc; |
| 114 | viewRow += plane.rowInc; |
| 115 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | return OK; |
| 119 | } |
| 120 | |
| 121 | } // namespace |
| 122 | |
| 123 | status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view) { |
Harish Mahendrakar | f7c49e2 | 2019-05-24 14:19:16 -0700 | [diff] [blame] | 124 | if (view.crop().width != img->mWidth || view.crop().height != img->mHeight) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 125 | return BAD_VALUE; |
| 126 | } |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 127 | const uint8_t* src_y = view.data()[0]; |
| 128 | const uint8_t* src_u = view.data()[1]; |
| 129 | const uint8_t* src_v = view.data()[2]; |
| 130 | int32_t src_stride_y = view.layout().planes[0].rowInc; |
| 131 | int32_t src_stride_u = view.layout().planes[1].rowInc; |
| 132 | int32_t src_stride_v = view.layout().planes[2].rowInc; |
| 133 | uint8_t* dst_y = imgBase + img->mPlane[0].mOffset; |
| 134 | uint8_t* dst_u = imgBase + img->mPlane[1].mOffset; |
| 135 | uint8_t* dst_v = imgBase + img->mPlane[2].mOffset; |
| 136 | int32_t dst_stride_y = img->mPlane[0].mRowInc; |
| 137 | int32_t dst_stride_u = img->mPlane[1].mRowInc; |
| 138 | int32_t dst_stride_v = img->mPlane[2].mRowInc; |
| 139 | int width = view.crop().width; |
| 140 | int height = view.crop().height; |
| 141 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 142 | if (IsNV12(view)) { |
| 143 | if (IsNV12(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 144 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV12"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 145 | libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 146 | libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2); |
| 147 | return OK; |
| 148 | } else if (IsNV21(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 149 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 150 | if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_u, src_stride_u, |
| 151 | dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) { |
| 152 | return OK; |
| 153 | } |
| 154 | } else if (IsI420(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 155 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->I420"); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 156 | if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y, |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 157 | dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 158 | return OK; |
| 159 | } |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 160 | } |
| 161 | } else if (IsNV21(view)) { |
| 162 | if (IsNV12(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 163 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV12"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 164 | if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_v, src_stride_v, |
| 165 | dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) { |
| 166 | return OK; |
| 167 | } |
| 168 | } else if (IsNV21(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 169 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 170 | libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 171 | libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height / 2); |
| 172 | return OK; |
| 173 | } else if (IsI420(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 174 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->I420"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 175 | if (!libyuv::NV21ToI420(src_y, src_stride_y, src_v, src_stride_v, dst_y, dst_stride_y, |
| 176 | dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) { |
| 177 | return OK; |
| 178 | } |
| 179 | } |
| 180 | } else if (IsI420(view)) { |
| 181 | if (IsNV12(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 182 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV12"); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 183 | if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v, |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 184 | dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 185 | return OK; |
| 186 | } |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 187 | } else if (IsNV21(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 188 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 189 | if (!libyuv::I420ToNV21(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v, |
| 190 | dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) { |
| 191 | return OK; |
| 192 | } |
| 193 | } else if (IsI420(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 194 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->I420"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 195 | libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 196 | libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width / 2, height / 2); |
| 197 | libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2); |
| 198 | return OK; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 201 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: generic"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 202 | return _ImageCopy<true>(view, img, imgBase); |
| 203 | } |
| 204 | |
| 205 | status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img) { |
Harish Mahendrakar | f7c49e2 | 2019-05-24 14:19:16 -0700 | [diff] [blame] | 206 | if (view.crop().width != img->mWidth || view.crop().height != img->mHeight) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 207 | return BAD_VALUE; |
| 208 | } |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 209 | const uint8_t* src_y = imgBase + img->mPlane[0].mOffset; |
| 210 | const uint8_t* src_u = imgBase + img->mPlane[1].mOffset; |
| 211 | const uint8_t* src_v = imgBase + img->mPlane[2].mOffset; |
| 212 | int32_t src_stride_y = img->mPlane[0].mRowInc; |
| 213 | int32_t src_stride_u = img->mPlane[1].mRowInc; |
| 214 | int32_t src_stride_v = img->mPlane[2].mRowInc; |
| 215 | uint8_t* dst_y = view.data()[0]; |
| 216 | uint8_t* dst_u = view.data()[1]; |
| 217 | uint8_t* dst_v = view.data()[2]; |
| 218 | int32_t dst_stride_y = view.layout().planes[0].rowInc; |
| 219 | int32_t dst_stride_u = view.layout().planes[1].rowInc; |
| 220 | int32_t dst_stride_v = view.layout().planes[2].rowInc; |
| 221 | int width = view.crop().width; |
| 222 | int height = view.crop().height; |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 223 | if (IsNV12(img)) { |
| 224 | if (IsNV12(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 225 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV12"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 226 | libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 227 | libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2); |
| 228 | return OK; |
| 229 | } else if (IsNV21(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 230 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 231 | if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_u, src_stride_u, |
| 232 | dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) { |
| 233 | return OK; |
| 234 | } |
| 235 | } else if (IsI420(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 236 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->I420"); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 237 | if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y, |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 238 | dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 239 | return OK; |
| 240 | } |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 241 | } |
| 242 | } else if (IsNV21(img)) { |
| 243 | if (IsNV12(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 244 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV12"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 245 | if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_v, src_stride_v, |
| 246 | dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) { |
| 247 | return OK; |
| 248 | } |
| 249 | } else if (IsNV21(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 250 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 251 | libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 252 | libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height / 2); |
| 253 | return OK; |
| 254 | } else if (IsI420(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 255 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->I420"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 256 | if (!libyuv::NV21ToI420(src_y, src_stride_y, src_v, src_stride_v, dst_y, dst_stride_y, |
| 257 | dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) { |
| 258 | return OK; |
| 259 | } |
| 260 | } |
| 261 | } else if (IsI420(img)) { |
| 262 | if (IsNV12(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 263 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV12"); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 264 | if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v, |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 265 | dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 266 | return OK; |
| 267 | } |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 268 | } else if (IsNV21(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 269 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 270 | if (!libyuv::I420ToNV21(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v, |
| 271 | dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) { |
| 272 | return OK; |
| 273 | } |
| 274 | } else if (IsI420(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 275 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->I420"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 276 | libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 277 | libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width / 2, height / 2); |
| 278 | libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2); |
| 279 | return OK; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 280 | } |
| 281 | } |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 282 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: generic"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 283 | return _ImageCopy<false>(view, img, imgBase); |
| 284 | } |
| 285 | |
| 286 | bool IsYUV420(const C2GraphicView &view) { |
| 287 | const C2PlanarLayout &layout = view.layout(); |
| 288 | return (layout.numPlanes == 3 |
| 289 | && layout.type == C2PlanarLayout::TYPE_YUV |
| 290 | && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y |
| 291 | && layout.planes[layout.PLANE_Y].allocatedDepth == 8 |
| 292 | && layout.planes[layout.PLANE_Y].bitDepth == 8 |
| 293 | && layout.planes[layout.PLANE_Y].rightShift == 0 |
| 294 | && layout.planes[layout.PLANE_Y].colSampling == 1 |
| 295 | && layout.planes[layout.PLANE_Y].rowSampling == 1 |
| 296 | && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB |
| 297 | && layout.planes[layout.PLANE_U].allocatedDepth == 8 |
| 298 | && layout.planes[layout.PLANE_U].bitDepth == 8 |
| 299 | && layout.planes[layout.PLANE_U].rightShift == 0 |
| 300 | && layout.planes[layout.PLANE_U].colSampling == 2 |
| 301 | && layout.planes[layout.PLANE_U].rowSampling == 2 |
| 302 | && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR |
| 303 | && layout.planes[layout.PLANE_V].allocatedDepth == 8 |
| 304 | && layout.planes[layout.PLANE_V].bitDepth == 8 |
| 305 | && layout.planes[layout.PLANE_V].rightShift == 0 |
| 306 | && layout.planes[layout.PLANE_V].colSampling == 2 |
| 307 | && layout.planes[layout.PLANE_V].rowSampling == 2); |
| 308 | } |
| 309 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 310 | bool IsNV12(const C2GraphicView &view) { |
| 311 | if (!IsYUV420(view)) { |
| 312 | return false; |
| 313 | } |
| 314 | const C2PlanarLayout &layout = view.layout(); |
| 315 | return (layout.rootPlanes == 2 |
| 316 | && layout.planes[layout.PLANE_U].colInc == 2 |
| 317 | && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U |
| 318 | && layout.planes[layout.PLANE_U].offset == 0 |
| 319 | && layout.planes[layout.PLANE_V].colInc == 2 |
| 320 | && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U |
| 321 | && layout.planes[layout.PLANE_V].offset == 1); |
| 322 | } |
| 323 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 324 | bool IsNV21(const C2GraphicView &view) { |
| 325 | if (!IsYUV420(view)) { |
| 326 | return false; |
| 327 | } |
| 328 | const C2PlanarLayout &layout = view.layout(); |
| 329 | return (layout.rootPlanes == 2 |
| 330 | && layout.planes[layout.PLANE_U].colInc == 2 |
| 331 | && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_V |
| 332 | && layout.planes[layout.PLANE_U].offset == 1 |
| 333 | && layout.planes[layout.PLANE_V].colInc == 2 |
| 334 | && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V |
| 335 | && layout.planes[layout.PLANE_V].offset == 0); |
| 336 | } |
| 337 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 338 | bool IsI420(const C2GraphicView &view) { |
| 339 | if (!IsYUV420(view)) { |
| 340 | return false; |
| 341 | } |
| 342 | const C2PlanarLayout &layout = view.layout(); |
| 343 | return (layout.rootPlanes == 3 |
| 344 | && layout.planes[layout.PLANE_U].colInc == 1 |
| 345 | && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U |
| 346 | && layout.planes[layout.PLANE_U].offset == 0 |
| 347 | && layout.planes[layout.PLANE_V].colInc == 1 |
| 348 | && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V |
| 349 | && layout.planes[layout.PLANE_V].offset == 0); |
| 350 | } |
| 351 | |
| 352 | bool IsYUV420(const MediaImage2 *img) { |
| 353 | return (img->mType == MediaImage2::MEDIA_IMAGE_TYPE_YUV |
| 354 | && img->mNumPlanes == 3 |
| 355 | && img->mBitDepth == 8 |
| 356 | && img->mBitDepthAllocated == 8 |
| 357 | && img->mPlane[0].mHorizSubsampling == 1 |
| 358 | && img->mPlane[0].mVertSubsampling == 1 |
| 359 | && img->mPlane[1].mHorizSubsampling == 2 |
| 360 | && img->mPlane[1].mVertSubsampling == 2 |
| 361 | && img->mPlane[2].mHorizSubsampling == 2 |
| 362 | && img->mPlane[2].mVertSubsampling == 2); |
| 363 | } |
| 364 | |
| 365 | bool IsNV12(const MediaImage2 *img) { |
| 366 | if (!IsYUV420(img)) { |
| 367 | return false; |
| 368 | } |
| 369 | return (img->mPlane[1].mColInc == 2 |
| 370 | && img->mPlane[2].mColInc == 2 |
Harish Mahendrakar | cd59f03 | 2021-04-16 18:55:41 -0700 | [diff] [blame] | 371 | && (img->mPlane[2].mOffset == img->mPlane[1].mOffset + 1)); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 374 | bool IsNV21(const MediaImage2 *img) { |
| 375 | if (!IsYUV420(img)) { |
| 376 | return false; |
| 377 | } |
| 378 | return (img->mPlane[1].mColInc == 2 |
| 379 | && img->mPlane[2].mColInc == 2 |
Harish Mahendrakar | cd59f03 | 2021-04-16 18:55:41 -0700 | [diff] [blame] | 380 | && (img->mPlane[1].mOffset == img->mPlane[2].mOffset + 1)); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 383 | bool IsI420(const MediaImage2 *img) { |
| 384 | if (!IsYUV420(img)) { |
| 385 | return false; |
| 386 | } |
| 387 | return (img->mPlane[1].mColInc == 1 |
| 388 | && img->mPlane[2].mColInc == 1 |
| 389 | && img->mPlane[2].mOffset > img->mPlane[1].mOffset); |
| 390 | } |
| 391 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 392 | FlexLayout GetYuv420FlexibleLayout() { |
| 393 | static FlexLayout sLayout = []{ |
| 394 | AHardwareBuffer_Desc desc = { |
| 395 | 16, // width |
| 396 | 16, // height |
| 397 | 1, // layers |
| 398 | AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420, |
| 399 | AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, |
| 400 | 0, // stride |
| 401 | 0, // rfu0 |
| 402 | 0, // rfu1 |
| 403 | }; |
| 404 | AHardwareBuffer *buffer = nullptr; |
| 405 | int ret = AHardwareBuffer_allocate(&desc, &buffer); |
| 406 | if (ret != 0) { |
| 407 | return FLEX_LAYOUT_UNKNOWN; |
| 408 | } |
| 409 | class AutoCloser { |
| 410 | public: |
| 411 | AutoCloser(AHardwareBuffer *buffer) : mBuffer(buffer), mLocked(false) {} |
| 412 | ~AutoCloser() { |
| 413 | if (mLocked) { |
| 414 | AHardwareBuffer_unlock(mBuffer, nullptr); |
| 415 | } |
| 416 | AHardwareBuffer_release(mBuffer); |
| 417 | } |
| 418 | |
| 419 | void setLocked() { mLocked = true; } |
| 420 | |
| 421 | private: |
| 422 | AHardwareBuffer *mBuffer; |
| 423 | bool mLocked; |
| 424 | } autoCloser(buffer); |
| 425 | AHardwareBuffer_Planes planes; |
| 426 | ret = AHardwareBuffer_lockPlanes( |
| 427 | buffer, |
| 428 | AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, |
| 429 | -1, // fence |
| 430 | nullptr, // rect |
| 431 | &planes); |
| 432 | if (ret != 0) { |
| 433 | AHardwareBuffer_release(buffer); |
| 434 | return FLEX_LAYOUT_UNKNOWN; |
| 435 | } |
| 436 | autoCloser.setLocked(); |
| 437 | if (planes.planeCount != 3) { |
| 438 | return FLEX_LAYOUT_UNKNOWN; |
| 439 | } |
| 440 | if (planes.planes[0].pixelStride != 1) { |
| 441 | return FLEX_LAYOUT_UNKNOWN; |
| 442 | } |
| 443 | if (planes.planes[1].pixelStride == 1 && planes.planes[2].pixelStride == 1) { |
| 444 | return FLEX_LAYOUT_PLANAR; |
| 445 | } |
| 446 | if (planes.planes[1].pixelStride == 2 && planes.planes[2].pixelStride == 2) { |
| 447 | ssize_t uvDist = |
| 448 | static_cast<uint8_t *>(planes.planes[2].data) - |
| 449 | static_cast<uint8_t *>(planes.planes[1].data); |
| 450 | if (uvDist == 1) { |
| 451 | return FLEX_LAYOUT_SEMIPLANAR_UV; |
| 452 | } else if (uvDist == -1) { |
| 453 | return FLEX_LAYOUT_SEMIPLANAR_VU; |
| 454 | } |
| 455 | return FLEX_LAYOUT_UNKNOWN; |
| 456 | } |
| 457 | return FLEX_LAYOUT_UNKNOWN; |
| 458 | }(); |
| 459 | return sLayout; |
| 460 | } |
| 461 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 462 | MediaImage2 CreateYUV420PlanarMediaImage2( |
| 463 | uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) { |
| 464 | return MediaImage2 { |
| 465 | .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV, |
| 466 | .mNumPlanes = 3, |
| 467 | .mWidth = width, |
| 468 | .mHeight = height, |
| 469 | .mBitDepth = 8, |
| 470 | .mBitDepthAllocated = 8, |
| 471 | .mPlane = { |
| 472 | { |
| 473 | .mOffset = 0, |
| 474 | .mColInc = 1, |
| 475 | .mRowInc = (int32_t)stride, |
| 476 | .mHorizSubsampling = 1, |
| 477 | .mVertSubsampling = 1, |
| 478 | }, |
| 479 | { |
| 480 | .mOffset = stride * vstride, |
| 481 | .mColInc = 1, |
| 482 | .mRowInc = (int32_t)stride / 2, |
| 483 | .mHorizSubsampling = 2, |
| 484 | .mVertSubsampling = 2, |
| 485 | }, |
| 486 | { |
| 487 | .mOffset = stride * vstride * 5 / 4, |
| 488 | .mColInc = 1, |
| 489 | .mRowInc = (int32_t)stride / 2, |
| 490 | .mHorizSubsampling = 2, |
| 491 | .mVertSubsampling = 2, |
| 492 | } |
| 493 | }, |
| 494 | }; |
| 495 | } |
| 496 | |
| 497 | MediaImage2 CreateYUV420SemiPlanarMediaImage2( |
| 498 | uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) { |
| 499 | return MediaImage2 { |
| 500 | .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV, |
| 501 | .mNumPlanes = 3, |
| 502 | .mWidth = width, |
| 503 | .mHeight = height, |
| 504 | .mBitDepth = 8, |
| 505 | .mBitDepthAllocated = 8, |
| 506 | .mPlane = { |
| 507 | { |
| 508 | .mOffset = 0, |
| 509 | .mColInc = 1, |
| 510 | .mRowInc = (int32_t)stride, |
| 511 | .mHorizSubsampling = 1, |
| 512 | .mVertSubsampling = 1, |
| 513 | }, |
| 514 | { |
| 515 | .mOffset = stride * vstride, |
| 516 | .mColInc = 2, |
| 517 | .mRowInc = (int32_t)stride, |
| 518 | .mHorizSubsampling = 2, |
| 519 | .mVertSubsampling = 2, |
| 520 | }, |
| 521 | { |
| 522 | .mOffset = stride * vstride + 1, |
| 523 | .mColInc = 2, |
| 524 | .mRowInc = (int32_t)stride, |
| 525 | .mHorizSubsampling = 2, |
| 526 | .mVertSubsampling = 2, |
| 527 | } |
| 528 | }, |
| 529 | }; |
| 530 | } |
| 531 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 532 | // Matrix coefficient to convert RGB to Planar YUV data. |
| 533 | // Each sub-array represents the 3X3 coeff used with R, G and B |
| 534 | static const int16_t bt601Matrix[2][3][3] = { |
| 535 | { { 76, 150, 29 }, { -43, -85, 128 }, { 128, -107, -21 } }, /* RANGE_FULL */ |
| 536 | { { 66, 129, 25 }, { -38, -74, 112 }, { 112, -94, -18 } }, /* RANGE_LIMITED */ |
| 537 | }; |
| 538 | |
| 539 | static const int16_t bt709Matrix[2][3][3] = { |
| 540 | { { 54, 183, 18 }, { -29, -99, 128 }, { 128, -116, -12 } }, /* RANGE_FULL */ |
| 541 | { { 47, 157, 16 }, { -26, -86, 112 }, { 112, -102, -10 } }, /* RANGE_LIMITED */ |
| 542 | }; |
| 543 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 544 | status_t ConvertRGBToPlanarYUV( |
| 545 | uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize, |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 546 | const C2GraphicView &src, C2Color::matrix_t colorMatrix, C2Color::range_t colorRange) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 547 | CHECK(dstY != nullptr); |
| 548 | CHECK((src.width() & 1) == 0); |
| 549 | CHECK((src.height() & 1) == 0); |
| 550 | |
| 551 | if (dstStride * dstVStride * 3 / 2 > bufferSize) { |
| 552 | ALOGD("conversion buffer is too small for converting from RGB to YUV"); |
| 553 | return NO_MEMORY; |
| 554 | } |
| 555 | |
| 556 | uint8_t *dstU = dstY + dstStride * dstVStride; |
| 557 | uint8_t *dstV = dstU + (dstStride >> 1) * (dstVStride >> 1); |
| 558 | |
| 559 | const C2PlanarLayout &layout = src.layout(); |
| 560 | const uint8_t *pRed = src.data()[C2PlanarLayout::PLANE_R]; |
| 561 | const uint8_t *pGreen = src.data()[C2PlanarLayout::PLANE_G]; |
| 562 | const uint8_t *pBlue = src.data()[C2PlanarLayout::PLANE_B]; |
| 563 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 564 | // set default range as limited |
| 565 | if (colorRange != C2Color::RANGE_FULL && colorRange != C2Color::RANGE_LIMITED) { |
| 566 | colorRange = C2Color::RANGE_LIMITED; |
| 567 | } |
| 568 | const int16_t (*weights)[3] = |
| 569 | (colorMatrix == C2Color::MATRIX_BT709) ? |
| 570 | bt709Matrix[colorRange - 1] : bt601Matrix[colorRange - 1]; |
| 571 | uint8_t zeroLvl = colorRange == C2Color::RANGE_FULL ? 0 : 16; |
| 572 | uint8_t maxLvlLuma = colorRange == C2Color::RANGE_FULL ? 255 : 235; |
| 573 | uint8_t maxLvlChroma = colorRange == C2Color::RANGE_FULL ? 255 : 240; |
| 574 | |
| 575 | #define CLIP3(min,v,max) (((v) < (min)) ? (min) : (((max) > (v)) ? (v) : (max))) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 576 | for (size_t y = 0; y < src.height(); ++y) { |
| 577 | for (size_t x = 0; x < src.width(); ++x) { |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 578 | uint8_t r = *pRed; |
| 579 | uint8_t g = *pGreen; |
| 580 | uint8_t b = *pBlue; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 581 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 582 | unsigned luma = ((r * weights[0][0] + g * weights[0][1] + b * weights[0][2]) >> 8) + |
| 583 | zeroLvl; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 584 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 585 | dstY[x] = CLIP3(zeroLvl, luma, maxLvlLuma); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 586 | |
| 587 | if ((x & 1) == 0 && (y & 1) == 0) { |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 588 | unsigned U = ((r * weights[1][0] + g * weights[1][1] + b * weights[1][2]) >> 8) + |
| 589 | 128; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 590 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 591 | unsigned V = ((r * weights[2][0] + g * weights[2][1] + b * weights[2][2]) >> 8) + |
| 592 | 128; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 593 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 594 | dstU[x >> 1] = CLIP3(zeroLvl, U, maxLvlChroma); |
| 595 | dstV[x >> 1] = CLIP3(zeroLvl, V, maxLvlChroma); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 596 | } |
| 597 | pRed += layout.planes[C2PlanarLayout::PLANE_R].colInc; |
| 598 | pGreen += layout.planes[C2PlanarLayout::PLANE_G].colInc; |
| 599 | pBlue += layout.planes[C2PlanarLayout::PLANE_B].colInc; |
| 600 | } |
| 601 | |
| 602 | if ((y & 1) == 0) { |
| 603 | dstU += dstStride >> 1; |
| 604 | dstV += dstStride >> 1; |
| 605 | } |
| 606 | |
| 607 | pRed -= layout.planes[C2PlanarLayout::PLANE_R].colInc * src.width(); |
| 608 | pGreen -= layout.planes[C2PlanarLayout::PLANE_G].colInc * src.width(); |
| 609 | pBlue -= layout.planes[C2PlanarLayout::PLANE_B].colInc * src.width(); |
| 610 | pRed += layout.planes[C2PlanarLayout::PLANE_R].rowInc; |
| 611 | pGreen += layout.planes[C2PlanarLayout::PLANE_G].rowInc; |
| 612 | pBlue += layout.planes[C2PlanarLayout::PLANE_B].rowInc; |
| 613 | |
| 614 | dstY += dstStride; |
| 615 | } |
| 616 | return OK; |
| 617 | } |
| 618 | |
| 619 | namespace { |
| 620 | |
| 621 | /** |
| 622 | * A block of raw allocated memory. |
| 623 | */ |
| 624 | struct MemoryBlockPoolBlock { |
| 625 | MemoryBlockPoolBlock(size_t size) |
| 626 | : mData(new uint8_t[size]), mSize(mData ? size : 0) { } |
| 627 | |
| 628 | ~MemoryBlockPoolBlock() { |
| 629 | delete[] mData; |
| 630 | } |
| 631 | |
| 632 | const uint8_t *data() const { |
| 633 | return mData; |
| 634 | } |
| 635 | |
| 636 | size_t size() const { |
| 637 | return mSize; |
| 638 | } |
| 639 | |
| 640 | C2_DO_NOT_COPY(MemoryBlockPoolBlock); |
| 641 | |
| 642 | private: |
| 643 | uint8_t *mData; |
| 644 | size_t mSize; |
| 645 | }; |
| 646 | |
| 647 | /** |
| 648 | * A simple raw memory block pool implementation. |
| 649 | */ |
| 650 | struct MemoryBlockPoolImpl { |
| 651 | void release(std::list<MemoryBlockPoolBlock>::const_iterator block) { |
| 652 | std::lock_guard<std::mutex> lock(mMutex); |
| 653 | // return block to free blocks if it is the current size; otherwise, discard |
| 654 | if (block->size() == mCurrentSize) { |
| 655 | mFreeBlocks.splice(mFreeBlocks.begin(), mBlocksInUse, block); |
| 656 | } else { |
| 657 | mBlocksInUse.erase(block); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | std::list<MemoryBlockPoolBlock>::const_iterator fetch(size_t size) { |
| 662 | std::lock_guard<std::mutex> lock(mMutex); |
| 663 | mFreeBlocks.remove_if([size](const MemoryBlockPoolBlock &block) -> bool { |
| 664 | return block.size() != size; |
| 665 | }); |
| 666 | mCurrentSize = size; |
| 667 | if (mFreeBlocks.empty()) { |
| 668 | mBlocksInUse.emplace_front(size); |
| 669 | } else { |
| 670 | mBlocksInUse.splice(mBlocksInUse.begin(), mFreeBlocks, mFreeBlocks.begin()); |
| 671 | } |
| 672 | return mBlocksInUse.begin(); |
| 673 | } |
| 674 | |
| 675 | MemoryBlockPoolImpl() = default; |
| 676 | |
| 677 | C2_DO_NOT_COPY(MemoryBlockPoolImpl); |
| 678 | |
| 679 | private: |
| 680 | std::mutex mMutex; |
| 681 | std::list<MemoryBlockPoolBlock> mFreeBlocks; |
| 682 | std::list<MemoryBlockPoolBlock> mBlocksInUse; |
| 683 | size_t mCurrentSize; |
| 684 | }; |
| 685 | |
| 686 | } // namespace |
| 687 | |
| 688 | struct MemoryBlockPool::Impl : MemoryBlockPoolImpl { |
| 689 | }; |
| 690 | |
| 691 | struct MemoryBlock::Impl { |
| 692 | Impl(std::list<MemoryBlockPoolBlock>::const_iterator block, |
| 693 | std::shared_ptr<MemoryBlockPoolImpl> pool) |
| 694 | : mBlock(block), mPool(pool) { |
| 695 | } |
| 696 | |
| 697 | ~Impl() { |
| 698 | mPool->release(mBlock); |
| 699 | } |
| 700 | |
| 701 | const uint8_t *data() const { |
| 702 | return mBlock->data(); |
| 703 | } |
| 704 | |
| 705 | size_t size() const { |
| 706 | return mBlock->size(); |
| 707 | } |
| 708 | |
| 709 | private: |
| 710 | std::list<MemoryBlockPoolBlock>::const_iterator mBlock; |
| 711 | std::shared_ptr<MemoryBlockPoolImpl> mPool; |
| 712 | }; |
| 713 | |
| 714 | MemoryBlock MemoryBlockPool::fetch(size_t size) { |
| 715 | std::list<MemoryBlockPoolBlock>::const_iterator poolBlock = mImpl->fetch(size); |
| 716 | return MemoryBlock(std::make_shared<MemoryBlock::Impl>( |
| 717 | poolBlock, std::static_pointer_cast<MemoryBlockPoolImpl>(mImpl))); |
| 718 | } |
| 719 | |
| 720 | MemoryBlockPool::MemoryBlockPool() |
| 721 | : mImpl(std::make_shared<MemoryBlockPool::Impl>()) { |
| 722 | } |
| 723 | |
| 724 | MemoryBlock::MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl) |
| 725 | : mImpl(impl) { |
| 726 | } |
| 727 | |
| 728 | MemoryBlock::MemoryBlock() = default; |
| 729 | |
| 730 | MemoryBlock::~MemoryBlock() = default; |
| 731 | |
| 732 | const uint8_t* MemoryBlock::data() const { |
| 733 | return mImpl ? mImpl->data() : nullptr; |
| 734 | } |
| 735 | |
| 736 | size_t MemoryBlock::size() const { |
| 737 | return mImpl ? mImpl->size() : 0; |
| 738 | } |
| 739 | |
| 740 | MemoryBlock MemoryBlock::Allocate(size_t size) { |
| 741 | return MemoryBlockPool().fetch(size); |
| 742 | } |
| 743 | |
| 744 | } // namespace android |