blob: a54af83d4d45f24c8c61517791839158402a03dd [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
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"
19#include <utils/Log.h>
20
Pawin Vongmasa1f213362019-01-24 06:59:16 -080021#include <libyuv.h>
22
Pawin Vongmasa36653902018-11-15 00:10:25 -080023#include <list>
24#include <mutex>
25
26#include <media/hardware/HardwareAPI.h>
27#include <media/stagefright/foundation/AUtils.h>
28
29#include <C2Debug.h>
30
31#include "Codec2BufferUtils.h"
32
33namespace android {
34
35namespace {
36
37/**
38 * A flippable, optimizable memcpy. Constructs such as (from ? src : dst) do not work as the results are
39 * always const.
40 */
41template<bool ToA, size_t S>
42struct MemCopier {
43 template<typename A, typename B>
44 inline static void copy(A *a, const B *b, size_t size) {
45 __builtin_memcpy(a, b, size);
46 }
47};
48
49template<size_t S>
50struct MemCopier<false, S> {
51 template<typename A, typename B>
52 inline static void copy(const A *a, B *b, size_t size) {
53 MemCopier<true, S>::copy(b, a, size);
54 }
55};
56
57/**
58 * Copies between a MediaImage and a graphic view.
59 *
60 * \param ToMediaImage whether to copy to (or from) the MediaImage
61 * \param view graphic view (could be ConstGraphicView or GraphicView depending on direction)
62 * \param img MediaImage data
63 * \param imgBase base of MediaImage (could be const uint8_t* or uint8_t* depending on direction)
64 */
65template<bool ToMediaImage, typename View, typename ImagePixel>
66static status_t _ImageCopy(View &view, const MediaImage2 *img, ImagePixel *imgBase) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -080067 // TODO: more efficient copying --- e.g. copy interleaved planes together, etc.
Pawin Vongmasa36653902018-11-15 00:10:25 -080068 const C2PlanarLayout &layout = view.layout();
69 const size_t bpp = divUp(img->mBitDepthAllocated, 8u);
Pawin Vongmasa1f213362019-01-24 06:59:16 -080070
Pawin Vongmasa36653902018-11-15 00:10:25 -080071 for (uint32_t i = 0; i < layout.numPlanes; ++i) {
72 typename std::conditional<ToMediaImage, uint8_t, const uint8_t>::type *imgRow =
73 imgBase + img->mPlane[i].mOffset;
74 typename std::conditional<ToMediaImage, const uint8_t, uint8_t>::type *viewRow =
75 viewRow = view.data()[i];
76 const C2PlaneInfo &plane = layout.planes[i];
77 if (plane.colSampling != img->mPlane[i].mHorizSubsampling
78 || plane.rowSampling != img->mPlane[i].mVertSubsampling
79 || plane.allocatedDepth != img->mBitDepthAllocated
80 || plane.allocatedDepth < plane.bitDepth
81 // MediaImage only supports MSB values
82 || plane.rightShift != plane.allocatedDepth - plane.bitDepth
83 || (bpp > 1 && plane.endianness != plane.NATIVE)) {
84 return BAD_VALUE;
85 }
86
87 uint32_t planeW = img->mWidth / plane.colSampling;
88 uint32_t planeH = img->mHeight / plane.rowSampling;
Pawin Vongmasa8be93112018-12-11 14:01:42 -080089
90 bool canCopyByRow = (plane.colInc == 1) && (img->mPlane[i].mColInc == 1);
91 bool canCopyByPlane = canCopyByRow && (plane.rowInc == img->mPlane[i].mRowInc);
92 if (canCopyByPlane) {
93 MemCopier<ToMediaImage, 0>::copy(imgRow, viewRow, plane.rowInc * planeH);
94 } else if (canCopyByRow) {
95 for (uint32_t row = 0; row < planeH; ++row) {
96 MemCopier<ToMediaImage, 0>::copy(
97 imgRow, viewRow, std::min(plane.rowInc, img->mPlane[i].mRowInc));
98 imgRow += img->mPlane[i].mRowInc;
99 viewRow += plane.rowInc;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800100 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800101 } else {
102 for (uint32_t row = 0; row < planeH; ++row) {
103 decltype(imgRow) imgPtr = imgRow;
104 decltype(viewRow) viewPtr = viewRow;
105 for (uint32_t col = 0; col < planeW; ++col) {
106 MemCopier<ToMediaImage, 0>::copy(imgPtr, viewPtr, bpp);
107 imgPtr += img->mPlane[i].mColInc;
108 viewPtr += plane.colInc;
109 }
110 imgRow += img->mPlane[i].mRowInc;
111 viewRow += plane.rowInc;
112 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800113 }
114 }
115 return OK;
116}
117
118} // namespace
119
120status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view) {
Harish Mahendrakarf7c49e22019-05-24 14:19:16 -0700121 if (view.crop().width != img->mWidth || view.crop().height != img->mHeight) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800122 return BAD_VALUE;
123 }
Wonsik Kimf37f2422021-03-04 15:38:59 -0800124 const uint8_t* src_y = view.data()[0];
125 const uint8_t* src_u = view.data()[1];
126 const uint8_t* src_v = view.data()[2];
127 int32_t src_stride_y = view.layout().planes[0].rowInc;
128 int32_t src_stride_u = view.layout().planes[1].rowInc;
129 int32_t src_stride_v = view.layout().planes[2].rowInc;
130 uint8_t* dst_y = imgBase + img->mPlane[0].mOffset;
131 uint8_t* dst_u = imgBase + img->mPlane[1].mOffset;
132 uint8_t* dst_v = imgBase + img->mPlane[2].mOffset;
133 int32_t dst_stride_y = img->mPlane[0].mRowInc;
134 int32_t dst_stride_u = img->mPlane[1].mRowInc;
135 int32_t dst_stride_v = img->mPlane[2].mRowInc;
136 int width = view.crop().width;
137 int height = view.crop().height;
138
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800139 if ((IsNV12(view) && IsI420(img)) || (IsI420(view) && IsNV12(img))) {
140 // Take shortcuts to use libyuv functions between NV12 and I420 conversion.
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800141 if (IsNV12(view) && IsI420(img)) {
142 if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y,
Wonsik Kimf37f2422021-03-04 15:38:59 -0800143 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800144 return OK;
145 }
146 } else {
147 if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
Wonsik Kimf37f2422021-03-04 15:38:59 -0800148 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800149 return OK;
150 }
151 }
152 }
Wonsik Kimf37f2422021-03-04 15:38:59 -0800153 if (IsNV12(view) && IsNV12(img)) {
154 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
155 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2);
156 return OK;
157 }
158 if (IsI420(view) && IsI420(img)) {
159 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
160 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width / 2, height / 2);
161 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2);
162 return OK;
163 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800164 return _ImageCopy<true>(view, img, imgBase);
165}
166
167status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img) {
Harish Mahendrakarf7c49e22019-05-24 14:19:16 -0700168 if (view.crop().width != img->mWidth || view.crop().height != img->mHeight) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800169 return BAD_VALUE;
170 }
Wonsik Kimf37f2422021-03-04 15:38:59 -0800171 const uint8_t* src_y = imgBase + img->mPlane[0].mOffset;
172 const uint8_t* src_u = imgBase + img->mPlane[1].mOffset;
173 const uint8_t* src_v = imgBase + img->mPlane[2].mOffset;
174 int32_t src_stride_y = img->mPlane[0].mRowInc;
175 int32_t src_stride_u = img->mPlane[1].mRowInc;
176 int32_t src_stride_v = img->mPlane[2].mRowInc;
177 uint8_t* dst_y = view.data()[0];
178 uint8_t* dst_u = view.data()[1];
179 uint8_t* dst_v = view.data()[2];
180 int32_t dst_stride_y = view.layout().planes[0].rowInc;
181 int32_t dst_stride_u = view.layout().planes[1].rowInc;
182 int32_t dst_stride_v = view.layout().planes[2].rowInc;
183 int width = view.crop().width;
184 int height = view.crop().height;
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800185 if ((IsNV12(img) && IsI420(view)) || (IsI420(img) && IsNV12(view))) {
186 // Take shortcuts to use libyuv functions between NV12 and I420 conversion.
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800187 if (IsNV12(img) && IsI420(view)) {
188 if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y,
Wonsik Kimf37f2422021-03-04 15:38:59 -0800189 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800190 return OK;
191 }
192 } else {
193 if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
Wonsik Kimf37f2422021-03-04 15:38:59 -0800194 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800195 return OK;
196 }
197 }
198 }
Wonsik Kimf37f2422021-03-04 15:38:59 -0800199 if (IsNV12(img) && IsNV12(view)) {
200 // For NV12, copy Y and UV plane
201 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
202 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2);
203 return OK;
204 }
205 if (IsI420(img) && IsI420(view)) {
206 // For I420, copy Y, U and V plane.
207 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
208 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width / 2, height / 2);
209 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2);
210 return OK;
211 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800212 return _ImageCopy<false>(view, img, imgBase);
213}
214
215bool IsYUV420(const C2GraphicView &view) {
216 const C2PlanarLayout &layout = view.layout();
217 return (layout.numPlanes == 3
218 && layout.type == C2PlanarLayout::TYPE_YUV
219 && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y
220 && layout.planes[layout.PLANE_Y].allocatedDepth == 8
221 && layout.planes[layout.PLANE_Y].bitDepth == 8
222 && layout.planes[layout.PLANE_Y].rightShift == 0
223 && layout.planes[layout.PLANE_Y].colSampling == 1
224 && layout.planes[layout.PLANE_Y].rowSampling == 1
225 && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB
226 && layout.planes[layout.PLANE_U].allocatedDepth == 8
227 && layout.planes[layout.PLANE_U].bitDepth == 8
228 && layout.planes[layout.PLANE_U].rightShift == 0
229 && layout.planes[layout.PLANE_U].colSampling == 2
230 && layout.planes[layout.PLANE_U].rowSampling == 2
231 && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR
232 && layout.planes[layout.PLANE_V].allocatedDepth == 8
233 && layout.planes[layout.PLANE_V].bitDepth == 8
234 && layout.planes[layout.PLANE_V].rightShift == 0
235 && layout.planes[layout.PLANE_V].colSampling == 2
236 && layout.planes[layout.PLANE_V].rowSampling == 2);
237}
238
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800239bool IsNV12(const C2GraphicView &view) {
240 if (!IsYUV420(view)) {
241 return false;
242 }
243 const C2PlanarLayout &layout = view.layout();
244 return (layout.rootPlanes == 2
245 && layout.planes[layout.PLANE_U].colInc == 2
246 && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U
247 && layout.planes[layout.PLANE_U].offset == 0
248 && layout.planes[layout.PLANE_V].colInc == 2
249 && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U
250 && layout.planes[layout.PLANE_V].offset == 1);
251}
252
253bool IsI420(const C2GraphicView &view) {
254 if (!IsYUV420(view)) {
255 return false;
256 }
257 const C2PlanarLayout &layout = view.layout();
258 return (layout.rootPlanes == 3
259 && layout.planes[layout.PLANE_U].colInc == 1
260 && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U
261 && layout.planes[layout.PLANE_U].offset == 0
262 && layout.planes[layout.PLANE_V].colInc == 1
263 && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V
264 && layout.planes[layout.PLANE_V].offset == 0);
265}
266
267bool IsYUV420(const MediaImage2 *img) {
268 return (img->mType == MediaImage2::MEDIA_IMAGE_TYPE_YUV
269 && img->mNumPlanes == 3
270 && img->mBitDepth == 8
271 && img->mBitDepthAllocated == 8
272 && img->mPlane[0].mHorizSubsampling == 1
273 && img->mPlane[0].mVertSubsampling == 1
274 && img->mPlane[1].mHorizSubsampling == 2
275 && img->mPlane[1].mVertSubsampling == 2
276 && img->mPlane[2].mHorizSubsampling == 2
277 && img->mPlane[2].mVertSubsampling == 2);
278}
279
280bool IsNV12(const MediaImage2 *img) {
281 if (!IsYUV420(img)) {
282 return false;
283 }
284 return (img->mPlane[1].mColInc == 2
285 && img->mPlane[2].mColInc == 2
286 && (img->mPlane[2].mOffset - img->mPlane[1].mOffset == 1));
287}
288
289bool IsI420(const MediaImage2 *img) {
290 if (!IsYUV420(img)) {
291 return false;
292 }
293 return (img->mPlane[1].mColInc == 1
294 && img->mPlane[2].mColInc == 1
295 && img->mPlane[2].mOffset > img->mPlane[1].mOffset);
296}
297
Pawin Vongmasa36653902018-11-15 00:10:25 -0800298MediaImage2 CreateYUV420PlanarMediaImage2(
299 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
300 return MediaImage2 {
301 .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
302 .mNumPlanes = 3,
303 .mWidth = width,
304 .mHeight = height,
305 .mBitDepth = 8,
306 .mBitDepthAllocated = 8,
307 .mPlane = {
308 {
309 .mOffset = 0,
310 .mColInc = 1,
311 .mRowInc = (int32_t)stride,
312 .mHorizSubsampling = 1,
313 .mVertSubsampling = 1,
314 },
315 {
316 .mOffset = stride * vstride,
317 .mColInc = 1,
318 .mRowInc = (int32_t)stride / 2,
319 .mHorizSubsampling = 2,
320 .mVertSubsampling = 2,
321 },
322 {
323 .mOffset = stride * vstride * 5 / 4,
324 .mColInc = 1,
325 .mRowInc = (int32_t)stride / 2,
326 .mHorizSubsampling = 2,
327 .mVertSubsampling = 2,
328 }
329 },
330 };
331}
332
333MediaImage2 CreateYUV420SemiPlanarMediaImage2(
334 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
335 return MediaImage2 {
336 .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
337 .mNumPlanes = 3,
338 .mWidth = width,
339 .mHeight = height,
340 .mBitDepth = 8,
341 .mBitDepthAllocated = 8,
342 .mPlane = {
343 {
344 .mOffset = 0,
345 .mColInc = 1,
346 .mRowInc = (int32_t)stride,
347 .mHorizSubsampling = 1,
348 .mVertSubsampling = 1,
349 },
350 {
351 .mOffset = stride * vstride,
352 .mColInc = 2,
353 .mRowInc = (int32_t)stride,
354 .mHorizSubsampling = 2,
355 .mVertSubsampling = 2,
356 },
357 {
358 .mOffset = stride * vstride + 1,
359 .mColInc = 2,
360 .mRowInc = (int32_t)stride,
361 .mHorizSubsampling = 2,
362 .mVertSubsampling = 2,
363 }
364 },
365 };
366}
367
368status_t ConvertRGBToPlanarYUV(
369 uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize,
370 const C2GraphicView &src) {
371 CHECK(dstY != nullptr);
372 CHECK((src.width() & 1) == 0);
373 CHECK((src.height() & 1) == 0);
374
375 if (dstStride * dstVStride * 3 / 2 > bufferSize) {
376 ALOGD("conversion buffer is too small for converting from RGB to YUV");
377 return NO_MEMORY;
378 }
379
380 uint8_t *dstU = dstY + dstStride * dstVStride;
381 uint8_t *dstV = dstU + (dstStride >> 1) * (dstVStride >> 1);
382
383 const C2PlanarLayout &layout = src.layout();
384 const uint8_t *pRed = src.data()[C2PlanarLayout::PLANE_R];
385 const uint8_t *pGreen = src.data()[C2PlanarLayout::PLANE_G];
386 const uint8_t *pBlue = src.data()[C2PlanarLayout::PLANE_B];
387
388#define CLIP3(x,y,z) (((z) < (x)) ? (x) : (((z) > (y)) ? (y) : (z)))
389 for (size_t y = 0; y < src.height(); ++y) {
390 for (size_t x = 0; x < src.width(); ++x) {
391 uint8_t red = *pRed;
392 uint8_t green = *pGreen;
393 uint8_t blue = *pBlue;
394
395 // using ITU-R BT.601 conversion matrix
396 unsigned luma =
397 CLIP3(0, (((red * 66 + green * 129 + blue * 25) >> 8) + 16), 255);
398
399 dstY[x] = luma;
400
401 if ((x & 1) == 0 && (y & 1) == 0) {
402 unsigned U =
403 CLIP3(0, (((-red * 38 - green * 74 + blue * 112) >> 8) + 128), 255);
404
405 unsigned V =
406 CLIP3(0, (((red * 112 - green * 94 - blue * 18) >> 8) + 128), 255);
407
408 dstU[x >> 1] = U;
409 dstV[x >> 1] = V;
410 }
411 pRed += layout.planes[C2PlanarLayout::PLANE_R].colInc;
412 pGreen += layout.planes[C2PlanarLayout::PLANE_G].colInc;
413 pBlue += layout.planes[C2PlanarLayout::PLANE_B].colInc;
414 }
415
416 if ((y & 1) == 0) {
417 dstU += dstStride >> 1;
418 dstV += dstStride >> 1;
419 }
420
421 pRed -= layout.planes[C2PlanarLayout::PLANE_R].colInc * src.width();
422 pGreen -= layout.planes[C2PlanarLayout::PLANE_G].colInc * src.width();
423 pBlue -= layout.planes[C2PlanarLayout::PLANE_B].colInc * src.width();
424 pRed += layout.planes[C2PlanarLayout::PLANE_R].rowInc;
425 pGreen += layout.planes[C2PlanarLayout::PLANE_G].rowInc;
426 pBlue += layout.planes[C2PlanarLayout::PLANE_B].rowInc;
427
428 dstY += dstStride;
429 }
430 return OK;
431}
432
433namespace {
434
435/**
436 * A block of raw allocated memory.
437 */
438struct MemoryBlockPoolBlock {
439 MemoryBlockPoolBlock(size_t size)
440 : mData(new uint8_t[size]), mSize(mData ? size : 0) { }
441
442 ~MemoryBlockPoolBlock() {
443 delete[] mData;
444 }
445
446 const uint8_t *data() const {
447 return mData;
448 }
449
450 size_t size() const {
451 return mSize;
452 }
453
454 C2_DO_NOT_COPY(MemoryBlockPoolBlock);
455
456private:
457 uint8_t *mData;
458 size_t mSize;
459};
460
461/**
462 * A simple raw memory block pool implementation.
463 */
464struct MemoryBlockPoolImpl {
465 void release(std::list<MemoryBlockPoolBlock>::const_iterator block) {
466 std::lock_guard<std::mutex> lock(mMutex);
467 // return block to free blocks if it is the current size; otherwise, discard
468 if (block->size() == mCurrentSize) {
469 mFreeBlocks.splice(mFreeBlocks.begin(), mBlocksInUse, block);
470 } else {
471 mBlocksInUse.erase(block);
472 }
473 }
474
475 std::list<MemoryBlockPoolBlock>::const_iterator fetch(size_t size) {
476 std::lock_guard<std::mutex> lock(mMutex);
477 mFreeBlocks.remove_if([size](const MemoryBlockPoolBlock &block) -> bool {
478 return block.size() != size;
479 });
480 mCurrentSize = size;
481 if (mFreeBlocks.empty()) {
482 mBlocksInUse.emplace_front(size);
483 } else {
484 mBlocksInUse.splice(mBlocksInUse.begin(), mFreeBlocks, mFreeBlocks.begin());
485 }
486 return mBlocksInUse.begin();
487 }
488
489 MemoryBlockPoolImpl() = default;
490
491 C2_DO_NOT_COPY(MemoryBlockPoolImpl);
492
493private:
494 std::mutex mMutex;
495 std::list<MemoryBlockPoolBlock> mFreeBlocks;
496 std::list<MemoryBlockPoolBlock> mBlocksInUse;
497 size_t mCurrentSize;
498};
499
500} // namespace
501
502struct MemoryBlockPool::Impl : MemoryBlockPoolImpl {
503};
504
505struct MemoryBlock::Impl {
506 Impl(std::list<MemoryBlockPoolBlock>::const_iterator block,
507 std::shared_ptr<MemoryBlockPoolImpl> pool)
508 : mBlock(block), mPool(pool) {
509 }
510
511 ~Impl() {
512 mPool->release(mBlock);
513 }
514
515 const uint8_t *data() const {
516 return mBlock->data();
517 }
518
519 size_t size() const {
520 return mBlock->size();
521 }
522
523private:
524 std::list<MemoryBlockPoolBlock>::const_iterator mBlock;
525 std::shared_ptr<MemoryBlockPoolImpl> mPool;
526};
527
528MemoryBlock MemoryBlockPool::fetch(size_t size) {
529 std::list<MemoryBlockPoolBlock>::const_iterator poolBlock = mImpl->fetch(size);
530 return MemoryBlock(std::make_shared<MemoryBlock::Impl>(
531 poolBlock, std::static_pointer_cast<MemoryBlockPoolImpl>(mImpl)));
532}
533
534MemoryBlockPool::MemoryBlockPool()
535 : mImpl(std::make_shared<MemoryBlockPool::Impl>()) {
536}
537
538MemoryBlock::MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)
539 : mImpl(impl) {
540}
541
542MemoryBlock::MemoryBlock() = default;
543
544MemoryBlock::~MemoryBlock() = default;
545
546const uint8_t* MemoryBlock::data() const {
547 return mImpl ? mImpl->data() : nullptr;
548}
549
550size_t MemoryBlock::size() const {
551 return mImpl ? mImpl->size() : 0;
552}
553
554MemoryBlock MemoryBlock::Allocate(size_t size) {
555 return MemoryBlockPool().fetch(size);
556}
557
558} // namespace android