• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
19 #define ATRACE_TAG  ATRACE_TAG_VIDEO
20 #include <utils/Log.h>
21 #include <utils/Trace.h>
22 
23 #include <libyuv.h>
24 
25 #include <list>
26 #include <mutex>
27 
28 #include <android/hardware_buffer.h>
29 #include <media/hardware/HardwareAPI.h>
30 #include <media/stagefright/foundation/ABuffer.h>
31 #include <media/stagefright/foundation/AMessage.h>
32 #include <media/stagefright/foundation/AUtils.h>
33 #include <media/stagefright/MediaCodecConstants.h>
34 
35 #include <C2Debug.h>
36 
37 #include "Codec2BufferUtils.h"
38 
39 namespace android {
40 
41 namespace {
42 
43 /**
44  * A flippable, optimizable memcpy. Constructs such as (from ? src : dst)
45  * do not work as the results are always const.
46  */
47 template<bool ToA, size_t S>
48 struct MemCopier {
49     template<typename A, typename B>
copyandroid::__anonacf7e43c0111::MemCopier50     inline static void copy(A *a, const B *b, size_t size) {
51         __builtin_memcpy(a, b, size);
52     }
53 };
54 
55 template<size_t S>
56 struct MemCopier<false, S> {
57     template<typename A, typename B>
copyandroid::__anonacf7e43c0111::MemCopier58     inline static void copy(const A *a, B *b, size_t size) {
59         MemCopier<true, S>::copy(b, a, size);
60     }
61 };
62 
63 /**
64  * Copies between a MediaImage and a graphic view.
65  *
66  * \param ToMediaImage whether to copy to (or from) the MediaImage
67  * \param view graphic view (could be ConstGraphicView or GraphicView depending on direction)
68  * \param img MediaImage data
69  * \param imgBase base of MediaImage (could be const uint8_t* or uint8_t* depending on direction)
70  */
71 template<bool ToMediaImage, typename View, typename ImagePixel>
_ImageCopy(View & view,const MediaImage2 * img,ImagePixel * imgBase)72 static status_t _ImageCopy(View &view, const MediaImage2 *img, ImagePixel *imgBase) {
73     // TODO: more efficient copying --- e.g. copy interleaved planes together, etc.
74     const C2PlanarLayout &layout = view.layout();
75     const size_t bpp = divUp(img->mBitDepthAllocated, 8u);
76 
77     for (uint32_t i = 0; i < layout.numPlanes; ++i) {
78         typename std::conditional<ToMediaImage, uint8_t, const uint8_t>::type *imgRow =
79             imgBase + img->mPlane[i].mOffset;
80         typename std::conditional<ToMediaImage, const uint8_t, uint8_t>::type *viewRow =
81             viewRow = view.data()[i];
82         const C2PlaneInfo &plane = layout.planes[i];
83         if (plane.colSampling != img->mPlane[i].mHorizSubsampling
84                 || plane.rowSampling != img->mPlane[i].mVertSubsampling
85                 || plane.allocatedDepth != img->mBitDepthAllocated
86                 || plane.allocatedDepth < plane.bitDepth
87                 // MediaImage only supports MSB values
88                 || plane.rightShift != plane.allocatedDepth - plane.bitDepth
89                 || (bpp > 1 && plane.endianness != plane.NATIVE)) {
90             return BAD_VALUE;
91         }
92 
93         uint32_t planeW = img->mWidth / plane.colSampling;
94         uint32_t planeH = img->mHeight / plane.rowSampling;
95 
96         bool canCopyByRow = (plane.colInc == bpp) && (img->mPlane[i].mColInc == bpp);
97         bool canCopyByPlane = canCopyByRow && (plane.rowInc == img->mPlane[i].mRowInc);
98         if (canCopyByPlane) {
99             MemCopier<ToMediaImage, 0>::copy(imgRow, viewRow, plane.rowInc * planeH);
100         } else if (canCopyByRow) {
101             for (uint32_t row = 0; row < planeH; ++row) {
102                 MemCopier<ToMediaImage, 0>::copy(
103                         imgRow, viewRow, std::min(plane.rowInc, img->mPlane[i].mRowInc));
104                 imgRow += img->mPlane[i].mRowInc;
105                 viewRow += plane.rowInc;
106             }
107         } else {
108             for (uint32_t row = 0; row < planeH; ++row) {
109                 decltype(imgRow) imgPtr = imgRow;
110                 decltype(viewRow) viewPtr = viewRow;
111                 for (uint32_t col = 0; col < planeW; ++col) {
112                     MemCopier<ToMediaImage, 0>::copy(imgPtr, viewPtr, bpp);
113                     imgPtr += img->mPlane[i].mColInc;
114                     viewPtr += plane.colInc;
115                 }
116                 imgRow += img->mPlane[i].mRowInc;
117                 viewRow += plane.rowInc;
118             }
119         }
120     }
121     return OK;
122 }
123 
124 }  // namespace
125 
ImageCopy(uint8_t * imgBase,const MediaImage2 * img,const C2GraphicView & view)126 status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view) {
127     if (img == nullptr
128         || imgBase == nullptr
129         || view.crop().width != img->mWidth
130         || view.crop().height != img->mHeight) {
131         return BAD_VALUE;
132     }
133     const uint8_t* src_y = view.data()[0];
134     const uint8_t* src_u = view.data()[1];
135     const uint8_t* src_v = view.data()[2];
136     int32_t src_stride_y = view.layout().planes[0].rowInc;
137     int32_t src_stride_u = view.layout().planes[1].rowInc;
138     int32_t src_stride_v = view.layout().planes[2].rowInc;
139     uint8_t* dst_y = imgBase + img->mPlane[0].mOffset;
140     uint8_t* dst_u = imgBase + img->mPlane[1].mOffset;
141     uint8_t* dst_v = imgBase + img->mPlane[2].mOffset;
142     int32_t dst_stride_y = img->mPlane[0].mRowInc;
143     int32_t dst_stride_u = img->mPlane[1].mRowInc;
144     int32_t dst_stride_v = img->mPlane[2].mRowInc;
145     int width = view.crop().width;
146     int height = view.crop().height;
147 
148     if (IsNV12(view)) {
149         if (IsNV12(img)) {
150             ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV12");
151             libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
152             libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2);
153             return OK;
154         } else if (IsNV21(img)) {
155             ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV21");
156             if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_u, src_stride_u,
157                                     dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
158                 return OK;
159             }
160         } else if (IsI420(img)) {
161             ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->I420");
162             if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y,
163                                     dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
164                 return OK;
165             }
166         }
167     } else if (IsNV21(view)) {
168         if (IsNV12(img)) {
169             ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV12");
170             if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_v, src_stride_v,
171                                     dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
172                 return OK;
173             }
174         } else if (IsNV21(img)) {
175             ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV21");
176             libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
177             libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height / 2);
178             return OK;
179         } else if (IsI420(img)) {
180             ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->I420");
181             if (!libyuv::NV21ToI420(src_y, src_stride_y, src_v, src_stride_v, dst_y, dst_stride_y,
182                                     dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
183                 return OK;
184             }
185         }
186     } else if (IsI420(view)) {
187         if (IsNV12(img)) {
188             ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV12");
189             if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
190                                     dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
191                 return OK;
192             }
193         } else if (IsNV21(img)) {
194             ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV21");
195             if (!libyuv::I420ToNV21(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
196                                     dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
197                 return OK;
198             }
199         } else if (IsI420(img)) {
200             ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->I420");
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 / 2, height / 2);
203             libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2);
204             return OK;
205         }
206     }
207     ScopedTrace trace(ATRACE_TAG, "ImageCopy: generic");
208     return _ImageCopy<true>(view, img, imgBase);
209 }
210 
ImageCopy(C2GraphicView & view,const uint8_t * imgBase,const MediaImage2 * img)211 status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img) {
212     if (img == nullptr
213         || imgBase == nullptr
214         || view.crop().width != img->mWidth
215         || view.crop().height != img->mHeight) {
216         return BAD_VALUE;
217     }
218     const uint8_t* src_y = imgBase + img->mPlane[0].mOffset;
219     const uint8_t* src_u = imgBase + img->mPlane[1].mOffset;
220     const uint8_t* src_v = imgBase + img->mPlane[2].mOffset;
221     int32_t src_stride_y = img->mPlane[0].mRowInc;
222     int32_t src_stride_u = img->mPlane[1].mRowInc;
223     int32_t src_stride_v = img->mPlane[2].mRowInc;
224     uint8_t* dst_y = view.data()[0];
225     uint8_t* dst_u = view.data()[1];
226     uint8_t* dst_v = view.data()[2];
227     int32_t dst_stride_y = view.layout().planes[0].rowInc;
228     int32_t dst_stride_u = view.layout().planes[1].rowInc;
229     int32_t dst_stride_v = view.layout().planes[2].rowInc;
230     int width = view.crop().width;
231     int height = view.crop().height;
232     if (IsNV12(img)) {
233         if (IsNV12(view)) {
234             ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV12");
235             libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
236             libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2);
237             return OK;
238         } else if (IsNV21(view)) {
239             ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV21");
240             if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_u, src_stride_u,
241                                     dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
242                 return OK;
243             }
244         } else if (IsI420(view)) {
245             ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->I420");
246             if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y,
247                                     dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
248                 return OK;
249             }
250         }
251     } else if (IsNV21(img)) {
252         if (IsNV12(view)) {
253             ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV12");
254             if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_v, src_stride_v,
255                                     dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
256                 return OK;
257             }
258         } else if (IsNV21(view)) {
259             ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV21");
260             libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
261             libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height / 2);
262             return OK;
263         } else if (IsI420(view)) {
264             ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->I420");
265             if (!libyuv::NV21ToI420(src_y, src_stride_y, src_v, src_stride_v, dst_y, dst_stride_y,
266                                     dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
267                 return OK;
268             }
269         }
270     } else if (IsI420(img)) {
271         if (IsNV12(view)) {
272             ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV12");
273             if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
274                                     dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
275                 return OK;
276             }
277         } else if (IsNV21(view)) {
278             ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV21");
279             if (!libyuv::I420ToNV21(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
280                                     dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
281                 return OK;
282             }
283         } else if (IsI420(view)) {
284             ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->I420");
285             libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
286             libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width / 2, height / 2);
287             libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2);
288             return OK;
289         }
290     }
291     ScopedTrace trace(ATRACE_TAG, "ImageCopy: generic");
292     return _ImageCopy<false>(view, img, imgBase);
293 }
294 
IsYUV420(const C2GraphicView & view)295 bool IsYUV420(const C2GraphicView &view) {
296     const C2PlanarLayout &layout = view.layout();
297     return (layout.numPlanes == 3
298             && layout.type == C2PlanarLayout::TYPE_YUV
299             && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y
300             && layout.planes[layout.PLANE_Y].allocatedDepth == 8
301             && layout.planes[layout.PLANE_Y].bitDepth == 8
302             && layout.planes[layout.PLANE_Y].rightShift == 0
303             && layout.planes[layout.PLANE_Y].colSampling == 1
304             && layout.planes[layout.PLANE_Y].rowSampling == 1
305             && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB
306             && layout.planes[layout.PLANE_U].allocatedDepth == 8
307             && layout.planes[layout.PLANE_U].bitDepth == 8
308             && layout.planes[layout.PLANE_U].rightShift == 0
309             && layout.planes[layout.PLANE_U].colSampling == 2
310             && layout.planes[layout.PLANE_U].rowSampling == 2
311             && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR
312             && layout.planes[layout.PLANE_V].allocatedDepth == 8
313             && layout.planes[layout.PLANE_V].bitDepth == 8
314             && layout.planes[layout.PLANE_V].rightShift == 0
315             && layout.planes[layout.PLANE_V].colSampling == 2
316             && layout.planes[layout.PLANE_V].rowSampling == 2);
317 }
318 
IsYUV420_10bit(const C2GraphicView & view)319 bool IsYUV420_10bit(const C2GraphicView &view) {
320     const C2PlanarLayout &layout = view.layout();
321     return (layout.numPlanes == 3
322             && layout.type == C2PlanarLayout::TYPE_YUV
323             && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y
324             && layout.planes[layout.PLANE_Y].allocatedDepth == 16
325             && layout.planes[layout.PLANE_Y].bitDepth == 10
326             && layout.planes[layout.PLANE_Y].colSampling == 1
327             && layout.planes[layout.PLANE_Y].rowSampling == 1
328             && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB
329             && layout.planes[layout.PLANE_U].allocatedDepth == 16
330             && layout.planes[layout.PLANE_U].bitDepth == 10
331             && layout.planes[layout.PLANE_U].colSampling == 2
332             && layout.planes[layout.PLANE_U].rowSampling == 2
333             && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR
334             && layout.planes[layout.PLANE_V].allocatedDepth == 16
335             && layout.planes[layout.PLANE_V].bitDepth == 10
336             && layout.planes[layout.PLANE_V].colSampling == 2
337             && layout.planes[layout.PLANE_V].rowSampling == 2);
338 }
339 
IsYUV422_10bit(const C2GraphicView & view)340 bool IsYUV422_10bit(const C2GraphicView &view) {
341     const C2PlanarLayout &layout = view.layout();
342     return (layout.numPlanes == 3
343             && layout.type == C2PlanarLayout::TYPE_YUV
344             && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y
345             && layout.planes[layout.PLANE_Y].allocatedDepth == 16
346             && layout.planes[layout.PLANE_Y].bitDepth == 10
347             && layout.planes[layout.PLANE_Y].colSampling == 1
348             && layout.planes[layout.PLANE_Y].rowSampling == 1
349             && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB
350             && layout.planes[layout.PLANE_U].allocatedDepth == 16
351             && layout.planes[layout.PLANE_U].bitDepth == 10
352             && layout.planes[layout.PLANE_U].colSampling == 2
353             && layout.planes[layout.PLANE_U].rowSampling == 1
354             && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR
355             && layout.planes[layout.PLANE_V].allocatedDepth == 16
356             && layout.planes[layout.PLANE_V].bitDepth == 10
357             && layout.planes[layout.PLANE_V].colSampling == 2
358             && layout.planes[layout.PLANE_V].rowSampling == 1);
359 }
360 
361 
IsNV12(const C2GraphicView & view)362 bool IsNV12(const C2GraphicView &view) {
363     if (!IsYUV420(view)) {
364         return false;
365     }
366     const C2PlanarLayout &layout = view.layout();
367     return (layout.rootPlanes == 2
368             && layout.planes[layout.PLANE_U].colInc == 2
369             && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U
370             && layout.planes[layout.PLANE_U].offset == 0
371             && layout.planes[layout.PLANE_V].colInc == 2
372             && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U
373             && layout.planes[layout.PLANE_V].offset == 1);
374 }
375 
IsP010(const C2GraphicView & view)376 bool IsP010(const C2GraphicView &view) {
377     if (!IsYUV420_10bit(view)) {
378         return false;
379     }
380     const C2PlanarLayout &layout = view.layout();
381     return (layout.rootPlanes == 2
382             && layout.planes[layout.PLANE_U].colInc == 4
383             && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U
384             && layout.planes[layout.PLANE_U].offset == 0
385             && layout.planes[layout.PLANE_V].colInc == 4
386             && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U
387             && layout.planes[layout.PLANE_V].offset == 2
388             && layout.planes[layout.PLANE_Y].rightShift == 6
389             && layout.planes[layout.PLANE_U].rightShift == 6
390             && layout.planes[layout.PLANE_V].rightShift == 6);
391 }
392 
IsP210(const C2GraphicView & view)393 bool IsP210(const C2GraphicView &view) {
394     if (!IsYUV422_10bit(view)) {
395         return false;
396     }
397     const C2PlanarLayout &layout = view.layout();
398     return (layout.rootPlanes == 2
399             && layout.planes[layout.PLANE_U].colInc == 4
400             && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U
401             && layout.planes[layout.PLANE_U].offset == 0
402             && layout.planes[layout.PLANE_V].colInc == 4
403             && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U
404             && layout.planes[layout.PLANE_V].offset == 2
405             && layout.planes[layout.PLANE_Y].rightShift == 6
406             && layout.planes[layout.PLANE_U].rightShift == 6
407             && layout.planes[layout.PLANE_V].rightShift == 6);
408 }
409 
IsNV21(const C2GraphicView & view)410 bool IsNV21(const C2GraphicView &view) {
411     if (!IsYUV420(view)) {
412         return false;
413     }
414     const C2PlanarLayout &layout = view.layout();
415     return (layout.rootPlanes == 2
416             && layout.planes[layout.PLANE_U].colInc == 2
417             && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_V
418             && layout.planes[layout.PLANE_U].offset == 1
419             && layout.planes[layout.PLANE_V].colInc == 2
420             && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V
421             && layout.planes[layout.PLANE_V].offset == 0);
422 }
423 
IsI420(const C2GraphicView & view)424 bool IsI420(const C2GraphicView &view) {
425     if (!IsYUV420(view)) {
426         return false;
427     }
428     const C2PlanarLayout &layout = view.layout();
429     return (layout.rootPlanes == 3
430             && layout.planes[layout.PLANE_U].colInc == 1
431             && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U
432             && layout.planes[layout.PLANE_U].offset == 0
433             && layout.planes[layout.PLANE_V].colInc == 1
434             && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V
435             && layout.planes[layout.PLANE_V].offset == 0);
436 }
437 
IsYUV420(const MediaImage2 * img)438 bool IsYUV420(const MediaImage2 *img) {
439     return (img->mType == MediaImage2::MEDIA_IMAGE_TYPE_YUV
440             && img->mNumPlanes == 3
441             && img->mBitDepth == 8
442             && img->mBitDepthAllocated == 8
443             && img->mPlane[0].mHorizSubsampling == 1
444             && img->mPlane[0].mVertSubsampling == 1
445             && img->mPlane[1].mHorizSubsampling == 2
446             && img->mPlane[1].mVertSubsampling == 2
447             && img->mPlane[2].mHorizSubsampling == 2
448             && img->mPlane[2].mVertSubsampling == 2);
449 }
450 
IsNV12(const MediaImage2 * img)451 bool IsNV12(const MediaImage2 *img) {
452     if (!IsYUV420(img)) {
453         return false;
454     }
455     return (img->mPlane[1].mColInc == 2
456             && img->mPlane[2].mColInc == 2
457             && (img->mPlane[2].mOffset == img->mPlane[1].mOffset + 1));
458 }
459 
IsNV21(const MediaImage2 * img)460 bool IsNV21(const MediaImage2 *img) {
461     if (!IsYUV420(img)) {
462         return false;
463     }
464     return (img->mPlane[1].mColInc == 2
465             && img->mPlane[2].mColInc == 2
466             && (img->mPlane[1].mOffset == img->mPlane[2].mOffset + 1));
467 }
468 
IsI420(const MediaImage2 * img)469 bool IsI420(const MediaImage2 *img) {
470     if (!IsYUV420(img)) {
471         return false;
472     }
473     return (img->mPlane[1].mColInc == 1
474             && img->mPlane[2].mColInc == 1
475             && img->mPlane[2].mOffset > img->mPlane[1].mOffset);
476 }
477 
GetYuv420FlexibleLayout()478 FlexLayout GetYuv420FlexibleLayout() {
479     static FlexLayout sLayout = []{
480         AHardwareBuffer_Desc desc = {
481             16,  // width
482             16,  // height
483             1,   // layers
484             AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420,
485             AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
486             0,   // stride
487             0,   // rfu0
488             0,   // rfu1
489         };
490         AHardwareBuffer *buffer = nullptr;
491         int ret = AHardwareBuffer_allocate(&desc, &buffer);
492         if (ret != 0) {
493             return FLEX_LAYOUT_UNKNOWN;
494         }
495         class AutoCloser {
496         public:
497             AutoCloser(AHardwareBuffer *buffer) : mBuffer(buffer), mLocked(false) {}
498             ~AutoCloser() {
499                 if (mLocked) {
500                     AHardwareBuffer_unlock(mBuffer, nullptr);
501                 }
502                 AHardwareBuffer_release(mBuffer);
503             }
504 
505             void setLocked() { mLocked = true; }
506 
507         private:
508             AHardwareBuffer *mBuffer;
509             bool mLocked;
510         } autoCloser(buffer);
511         AHardwareBuffer_Planes planes;
512         ret = AHardwareBuffer_lockPlanes(
513                 buffer,
514                 AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
515                 -1,       // fence
516                 nullptr,  // rect
517                 &planes);
518         if (ret != 0) {
519             AHardwareBuffer_release(buffer);
520             return FLEX_LAYOUT_UNKNOWN;
521         }
522         autoCloser.setLocked();
523         if (planes.planeCount != 3) {
524             return FLEX_LAYOUT_UNKNOWN;
525         }
526         if (planes.planes[0].pixelStride != 1) {
527             return FLEX_LAYOUT_UNKNOWN;
528         }
529         if (planes.planes[1].pixelStride == 1 && planes.planes[2].pixelStride == 1) {
530             return FLEX_LAYOUT_PLANAR;
531         }
532         if (planes.planes[1].pixelStride == 2 && planes.planes[2].pixelStride == 2) {
533             ssize_t uvDist =
534                 static_cast<uint8_t *>(planes.planes[2].data) -
535                 static_cast<uint8_t *>(planes.planes[1].data);
536             if (uvDist == 1) {
537                 return FLEX_LAYOUT_SEMIPLANAR_UV;
538             } else if (uvDist == -1) {
539                 return FLEX_LAYOUT_SEMIPLANAR_VU;
540             }
541             return FLEX_LAYOUT_UNKNOWN;
542         }
543         return FLEX_LAYOUT_UNKNOWN;
544     }();
545     return sLayout;
546 }
547 
CreateYUV420PlanarMediaImage2(uint32_t width,uint32_t height,uint32_t stride,uint32_t vstride)548 MediaImage2 CreateYUV420PlanarMediaImage2(
549         uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
550     return MediaImage2 {
551         .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
552         .mNumPlanes = 3,
553         .mWidth = width,
554         .mHeight = height,
555         .mBitDepth = 8,
556         .mBitDepthAllocated = 8,
557         .mPlane = {
558             {
559                 .mOffset = 0,
560                 .mColInc = 1,
561                 .mRowInc = (int32_t)stride,
562                 .mHorizSubsampling = 1,
563                 .mVertSubsampling = 1,
564             },
565             {
566                 .mOffset = stride * vstride,
567                 .mColInc = 1,
568                 .mRowInc = (int32_t)stride / 2,
569                 .mHorizSubsampling = 2,
570                 .mVertSubsampling = 2,
571             },
572             {
573                 .mOffset = stride * vstride * 5 / 4,
574                 .mColInc = 1,
575                 .mRowInc = (int32_t)stride / 2,
576                 .mHorizSubsampling = 2,
577                 .mVertSubsampling = 2,
578             }
579         },
580     };
581 }
582 
CreateYUV420SemiPlanarMediaImage2(uint32_t width,uint32_t height,uint32_t stride,uint32_t vstride)583 MediaImage2 CreateYUV420SemiPlanarMediaImage2(
584         uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
585     return MediaImage2 {
586         .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
587         .mNumPlanes = 3,
588         .mWidth = width,
589         .mHeight = height,
590         .mBitDepth = 8,
591         .mBitDepthAllocated = 8,
592         .mPlane = {
593             {
594                 .mOffset = 0,
595                 .mColInc = 1,
596                 .mRowInc = (int32_t)stride,
597                 .mHorizSubsampling = 1,
598                 .mVertSubsampling = 1,
599             },
600             {
601                 .mOffset = stride * vstride,
602                 .mColInc = 2,
603                 .mRowInc = (int32_t)stride,
604                 .mHorizSubsampling = 2,
605                 .mVertSubsampling = 2,
606             },
607             {
608                 .mOffset = stride * vstride + 1,
609                 .mColInc = 2,
610                 .mRowInc = (int32_t)stride,
611                 .mHorizSubsampling = 2,
612                 .mVertSubsampling = 2,
613             }
614         },
615     };
616 }
617 
618 // Matrix coefficient to convert RGB to Planar YUV data.
619 // Each sub-array represents the 3X3 coeff used with R, G and B
620 static const int16_t bt601Matrix[2][3][3] = {
621     { { 77, 150, 29 }, { -43, -85, 128 }, { 128, -107, -21 } }, /* RANGE_FULL */
622     { { 66, 129, 25 }, { -38, -74, 112 }, { 112, -94, -18 } },  /* RANGE_LIMITED */
623 };
624 
625 static const int16_t bt709Matrix[2][3][3] = {
626     // TRICKY: 18 is adjusted to 19 so that sum of row 1 is 256
627     { { 54, 183, 19 }, { -29, -99, 128 }, { 128, -116, -12 } }, /* RANGE_FULL */
628     // TRICKY: -87 is adjusted to -86 so that sum of row 2 is 0
629     { { 47, 157, 16 }, { -26, -86, 112 }, { 112, -102, -10 } }, /* RANGE_LIMITED */
630 };
631 
ConvertRGBToPlanarYUV(uint8_t * dstY,size_t dstStride,size_t dstVStride,size_t bufferSize,const C2GraphicView & src,C2Color::matrix_t colorMatrix,C2Color::range_t colorRange)632 status_t ConvertRGBToPlanarYUV(
633         uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize,
634         const C2GraphicView &src, C2Color::matrix_t colorMatrix, C2Color::range_t colorRange) {
635     CHECK(dstY != nullptr);
636 
637     if (dstStride * dstVStride * 3 / 2 > bufferSize) {
638         ALOGD("conversion buffer is too small for converting from RGB to YUV");
639         return NO_MEMORY;
640     }
641 
642     uint8_t *dstU = dstY + dstStride * dstVStride;
643     uint8_t *dstV = dstU + (dstStride >> 1) * (dstVStride >> 1);
644 
645     const C2PlanarLayout &layout = src.layout();
646     const uint8_t *pRed   = src.data()[C2PlanarLayout::PLANE_R];
647     const uint8_t *pGreen = src.data()[C2PlanarLayout::PLANE_G];
648     const uint8_t *pBlue  = src.data()[C2PlanarLayout::PLANE_B];
649 
650     // set default range as limited
651     if (colorRange != C2Color::RANGE_FULL && colorRange != C2Color::RANGE_LIMITED) {
652         colorRange = C2Color::RANGE_LIMITED;
653     }
654     const int16_t (*weights)[3] =
655         (colorMatrix == C2Color::MATRIX_BT709) ?
656             bt709Matrix[colorRange - 1] : bt601Matrix[colorRange - 1];
657     uint8_t zeroLvl =  colorRange == C2Color::RANGE_FULL ? 0 : 16;
658     uint8_t maxLvlLuma =  colorRange == C2Color::RANGE_FULL ? 255 : 235;
659     uint8_t maxLvlChroma =  colorRange == C2Color::RANGE_FULL ? 255 : 240;
660 
661 #define CLIP3(min,v,max) (((v) < (min)) ? (min) : (((max) > (v)) ? (v) : (max)))
662     for (size_t y = 0; y < src.crop().height; ++y) {
663         for (size_t x = 0; x < src.crop().width; ++x) {
664             uint8_t r = *pRed;
665             uint8_t g = *pGreen;
666             uint8_t b = *pBlue;
667 
668             unsigned luma = ((r * weights[0][0] + g * weights[0][1] + b * weights[0][2]) >> 8) +
669                              zeroLvl;
670 
671             dstY[x] = CLIP3(zeroLvl, luma, maxLvlLuma);
672 
673             if ((x & 1) == 0 && (y & 1) == 0) {
674                 unsigned U = ((r * weights[1][0] + g * weights[1][1] + b * weights[1][2]) >> 8) +
675                               128;
676 
677                 unsigned V = ((r * weights[2][0] + g * weights[2][1] + b * weights[2][2]) >> 8) +
678                               128;
679 
680                 dstU[x >> 1] = CLIP3(zeroLvl, U, maxLvlChroma);
681                 dstV[x >> 1] = CLIP3(zeroLvl, V, maxLvlChroma);
682             }
683             pRed   += layout.planes[C2PlanarLayout::PLANE_R].colInc;
684             pGreen += layout.planes[C2PlanarLayout::PLANE_G].colInc;
685             pBlue  += layout.planes[C2PlanarLayout::PLANE_B].colInc;
686         }
687 
688         if ((y & 1) == 0) {
689             dstU += dstStride >> 1;
690             dstV += dstStride >> 1;
691         }
692 
693         pRed   -= layout.planes[C2PlanarLayout::PLANE_R].colInc * src.width();
694         pGreen -= layout.planes[C2PlanarLayout::PLANE_G].colInc * src.width();
695         pBlue  -= layout.planes[C2PlanarLayout::PLANE_B].colInc * src.width();
696         pRed   += layout.planes[C2PlanarLayout::PLANE_R].rowInc;
697         pGreen += layout.planes[C2PlanarLayout::PLANE_G].rowInc;
698         pBlue  += layout.planes[C2PlanarLayout::PLANE_B].rowInc;
699 
700         dstY += dstStride;
701     }
702     return OK;
703 }
704 
705 namespace {
706 
707 /**
708  * A block of raw allocated memory.
709  */
710 struct MemoryBlockPoolBlock {
MemoryBlockPoolBlockandroid::__anonacf7e43c0311::MemoryBlockPoolBlock711     MemoryBlockPoolBlock(size_t size)
712         : mData(new uint8_t[size]), mSize(mData ? size : 0) { }
713 
~MemoryBlockPoolBlockandroid::__anonacf7e43c0311::MemoryBlockPoolBlock714     ~MemoryBlockPoolBlock() {
715         delete[] mData;
716     }
717 
dataandroid::__anonacf7e43c0311::MemoryBlockPoolBlock718     const uint8_t *data() const {
719         return mData;
720     }
721 
sizeandroid::__anonacf7e43c0311::MemoryBlockPoolBlock722     size_t size() const {
723         return mSize;
724     }
725 
726     C2_DO_NOT_COPY(MemoryBlockPoolBlock);
727 
728 private:
729     uint8_t *mData;
730     size_t mSize;
731 };
732 
733 /**
734  * A simple raw memory block pool implementation.
735  */
736 struct MemoryBlockPoolImpl {
releaseandroid::__anonacf7e43c0311::MemoryBlockPoolImpl737     void release(std::list<MemoryBlockPoolBlock>::const_iterator block) {
738         std::lock_guard<std::mutex> lock(mMutex);
739         // return block to free blocks if it is the current size; otherwise, discard
740         if (block->size() == mCurrentSize) {
741             mFreeBlocks.splice(mFreeBlocks.begin(), mBlocksInUse, block);
742         } else {
743             mBlocksInUse.erase(block);
744         }
745     }
746 
fetchandroid::__anonacf7e43c0311::MemoryBlockPoolImpl747     std::list<MemoryBlockPoolBlock>::const_iterator fetch(size_t size) {
748         std::lock_guard<std::mutex> lock(mMutex);
749         mFreeBlocks.remove_if([size](const MemoryBlockPoolBlock &block) -> bool {
750             return block.size() != size;
751         });
752         mCurrentSize = size;
753         if (mFreeBlocks.empty()) {
754             mBlocksInUse.emplace_front(size);
755         } else {
756             mBlocksInUse.splice(mBlocksInUse.begin(), mFreeBlocks, mFreeBlocks.begin());
757         }
758         return mBlocksInUse.begin();
759     }
760 
761     MemoryBlockPoolImpl() = default;
762 
763     C2_DO_NOT_COPY(MemoryBlockPoolImpl);
764 
765 private:
766     std::mutex mMutex;
767     std::list<MemoryBlockPoolBlock> mFreeBlocks;
768     std::list<MemoryBlockPoolBlock> mBlocksInUse;
769     size_t mCurrentSize;
770 };
771 
772 } // namespace
773 
774 struct MemoryBlockPool::Impl : MemoryBlockPoolImpl {
775 };
776 
777 struct MemoryBlock::Impl {
Implandroid::MemoryBlock::Impl778     Impl(std::list<MemoryBlockPoolBlock>::const_iterator block,
779          std::shared_ptr<MemoryBlockPoolImpl> pool)
780         : mBlock(block), mPool(pool) {
781     }
782 
~Implandroid::MemoryBlock::Impl783     ~Impl() {
784         mPool->release(mBlock);
785     }
786 
dataandroid::MemoryBlock::Impl787     const uint8_t *data() const {
788         return mBlock->data();
789     }
790 
sizeandroid::MemoryBlock::Impl791     size_t size() const {
792         return mBlock->size();
793     }
794 
795 private:
796     std::list<MemoryBlockPoolBlock>::const_iterator mBlock;
797     std::shared_ptr<MemoryBlockPoolImpl> mPool;
798 };
799 
fetch(size_t size)800 MemoryBlock MemoryBlockPool::fetch(size_t size) {
801     std::list<MemoryBlockPoolBlock>::const_iterator poolBlock = mImpl->fetch(size);
802     return MemoryBlock(std::make_shared<MemoryBlock::Impl>(
803             poolBlock, std::static_pointer_cast<MemoryBlockPoolImpl>(mImpl)));
804 }
805 
MemoryBlockPool()806 MemoryBlockPool::MemoryBlockPool()
807     : mImpl(std::make_shared<MemoryBlockPool::Impl>()) {
808 }
809 
MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)810 MemoryBlock::MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)
811     : mImpl(impl) {
812 }
813 
814 MemoryBlock::MemoryBlock() = default;
815 
816 MemoryBlock::~MemoryBlock() = default;
817 
data() const818 const uint8_t* MemoryBlock::data() const {
819     return mImpl ? mImpl->data() : nullptr;
820 }
821 
size() const822 size_t MemoryBlock::size() const {
823     return mImpl ? mImpl->size() : 0;
824 }
825 
Allocate(size_t size)826 MemoryBlock MemoryBlock::Allocate(size_t size) {
827     return MemoryBlockPool().fetch(size);
828 }
829 
GraphicView2MediaImageConverter(const C2GraphicView & view,const sp<AMessage> & format,bool copy)830 GraphicView2MediaImageConverter::GraphicView2MediaImageConverter(
831         const C2GraphicView &view, const sp<AMessage> &format, bool copy)
832     : mInitCheck(NO_INIT),
833         mView(view),
834         mWidth(view.width()),
835         mHeight(view.height()),
836         mAllocatedDepth(0),
837         mBackBufferSize(0),
838         mMediaImage(new ABuffer(sizeof(MediaImage2))) {
839     ATRACE_CALL();
840     if (!format->findInt32(KEY_COLOR_FORMAT, &mClientColorFormat)) {
841         mClientColorFormat = COLOR_FormatYUV420Flexible;
842     }
843     if (!format->findInt32("android._color-format", &mComponentColorFormat)) {
844         mComponentColorFormat = COLOR_FormatYUV420Flexible;
845     }
846     if (view.error() != C2_OK) {
847         ALOGD("Converter: view.error() = %d", view.error());
848         mInitCheck = BAD_VALUE;
849         return;
850     }
851     MediaImage2 *mediaImage = (MediaImage2 *)mMediaImage->base();
852     const C2PlanarLayout &layout = view.layout();
853     if (layout.numPlanes == 0) {
854         ALOGD("Converter: 0 planes");
855         mInitCheck = BAD_VALUE;
856         return;
857     }
858     memset(mediaImage, 0, sizeof(*mediaImage));
859     mAllocatedDepth = layout.planes[0].allocatedDepth;
860     uint32_t bitDepth = layout.planes[0].bitDepth;
861 
862     // align width and height to support subsampling cleanly
863     uint32_t stride = align(view.crop().width, 2) * divUp(layout.planes[0].allocatedDepth, 8u);
864     uint32_t vStride = align(view.crop().height, 2);
865 
866     bool tryWrapping = !copy;
867 
868     switch (layout.type) {
869         case C2PlanarLayout::TYPE_YUV: {
870             mediaImage->mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV;
871             if (layout.numPlanes != 3) {
872                 ALOGD("Converter: %d planes for YUV layout", layout.numPlanes);
873                 mInitCheck = BAD_VALUE;
874                 return;
875             }
876             std::optional<int> clientBitDepth = {};
877             switch (mClientColorFormat) {
878                 case COLOR_FormatYUVP010:
879                     clientBitDepth = 10;
880                     break;
881                 case COLOR_FormatYUV411PackedPlanar:
882                 case COLOR_FormatYUV411Planar:
883                 case COLOR_FormatYUV420Flexible:
884                 case COLOR_FormatYUV420PackedPlanar:
885                 case COLOR_FormatYUV420PackedSemiPlanar:
886                 case COLOR_FormatYUV420Planar:
887                 case COLOR_FormatYUV420SemiPlanar:
888                 case COLOR_FormatYUV422Flexible:
889                 case COLOR_FormatYUV422PackedPlanar:
890                 case COLOR_FormatYUV422PackedSemiPlanar:
891                 case COLOR_FormatYUV422Planar:
892                 case COLOR_FormatYUV422SemiPlanar:
893                 case COLOR_FormatYUV444Flexible:
894                 case COLOR_FormatYUV444Interleaved:
895                     clientBitDepth = 8;
896                     break;
897                 default:
898                     // no-op; used with optional
899                     break;
900 
901             }
902             // conversion fails if client bit-depth and the component bit-depth differs
903             if ((clientBitDepth) && (bitDepth != clientBitDepth.value())) {
904                 ALOGD("Bit depth of client: %d and component: %d differs",
905                     *clientBitDepth, bitDepth);
906                 mInitCheck = BAD_VALUE;
907                 return;
908             }
909             C2PlaneInfo yPlane = layout.planes[C2PlanarLayout::PLANE_Y];
910             C2PlaneInfo uPlane = layout.planes[C2PlanarLayout::PLANE_U];
911             C2PlaneInfo vPlane = layout.planes[C2PlanarLayout::PLANE_V];
912             if (yPlane.channel != C2PlaneInfo::CHANNEL_Y
913                     || uPlane.channel != C2PlaneInfo::CHANNEL_CB
914                     || vPlane.channel != C2PlaneInfo::CHANNEL_CR) {
915                 ALOGD("Converter: not YUV layout");
916                 mInitCheck = BAD_VALUE;
917                 return;
918             }
919             bool yuv420888 = yPlane.rowSampling == 1 && yPlane.colSampling == 1
920                     && uPlane.rowSampling == 2 && uPlane.colSampling == 2
921                     && vPlane.rowSampling == 2 && vPlane.colSampling == 2;
922             if (yuv420888) {
923                 for (uint32_t i = 0; i < 3; ++i) {
924                     const C2PlaneInfo &plane = layout.planes[i];
925                     if (plane.allocatedDepth != 8 || plane.bitDepth != 8) {
926                         yuv420888 = false;
927                         break;
928                     }
929                 }
930                 yuv420888 = yuv420888 && yPlane.colInc == 1 && uPlane.rowInc == vPlane.rowInc;
931             }
932             int32_t copyFormat = mClientColorFormat;
933             if (yuv420888 && mClientColorFormat == COLOR_FormatYUV420Flexible) {
934                 if (uPlane.colInc == 2 && vPlane.colInc == 2
935                         && yPlane.rowInc == uPlane.rowInc) {
936                     copyFormat = COLOR_FormatYUV420PackedSemiPlanar;
937                 } else if (uPlane.colInc == 1 && vPlane.colInc == 1
938                         && yPlane.rowInc == uPlane.rowInc * 2) {
939                     copyFormat = COLOR_FormatYUV420PackedPlanar;
940                 }
941             }
942             ALOGV("client_fmt=0x%x y:{colInc=%d rowInc=%d} u:{colInc=%d rowInc=%d} "
943                     "v:{colInc=%d rowInc=%d}",
944                     mClientColorFormat,
945                     yPlane.colInc, yPlane.rowInc,
946                     uPlane.colInc, uPlane.rowInc,
947                     vPlane.colInc, vPlane.rowInc);
948             switch (copyFormat) {
949                 case COLOR_FormatYUV420Flexible:
950                 case COLOR_FormatYUV420Planar:
951                 case COLOR_FormatYUV420PackedPlanar:
952                     mediaImage->mPlane[mediaImage->Y].mOffset = 0;
953                     mediaImage->mPlane[mediaImage->Y].mColInc = 1;
954                     mediaImage->mPlane[mediaImage->Y].mRowInc = stride;
955                     mediaImage->mPlane[mediaImage->Y].mHorizSubsampling = 1;
956                     mediaImage->mPlane[mediaImage->Y].mVertSubsampling = 1;
957 
958                     mediaImage->mPlane[mediaImage->U].mOffset = stride * vStride;
959                     mediaImage->mPlane[mediaImage->U].mColInc = 1;
960                     mediaImage->mPlane[mediaImage->U].mRowInc = stride / 2;
961                     mediaImage->mPlane[mediaImage->U].mHorizSubsampling = 2;
962                     mediaImage->mPlane[mediaImage->U].mVertSubsampling = 2;
963 
964                     mediaImage->mPlane[mediaImage->V].mOffset = stride * vStride * 5 / 4;
965                     mediaImage->mPlane[mediaImage->V].mColInc = 1;
966                     mediaImage->mPlane[mediaImage->V].mRowInc = stride / 2;
967                     mediaImage->mPlane[mediaImage->V].mHorizSubsampling = 2;
968                     mediaImage->mPlane[mediaImage->V].mVertSubsampling = 2;
969 
970                     if (tryWrapping && mClientColorFormat != COLOR_FormatYUV420Flexible) {
971                         tryWrapping = yuv420888 && uPlane.colInc == 1 && vPlane.colInc == 1
972                                 && yPlane.rowInc == uPlane.rowInc * 2
973                                 && view.data()[0] < view.data()[1]
974                                 && view.data()[1] < view.data()[2];
975                     }
976                     break;
977 
978                 case COLOR_FormatYUV420SemiPlanar:
979                 case COLOR_FormatYUV420PackedSemiPlanar:
980                     mediaImage->mPlane[mediaImage->Y].mOffset = 0;
981                     mediaImage->mPlane[mediaImage->Y].mColInc = 1;
982                     mediaImage->mPlane[mediaImage->Y].mRowInc = stride;
983                     mediaImage->mPlane[mediaImage->Y].mHorizSubsampling = 1;
984                     mediaImage->mPlane[mediaImage->Y].mVertSubsampling = 1;
985 
986                     mediaImage->mPlane[mediaImage->U].mOffset = stride * vStride;
987                     mediaImage->mPlane[mediaImage->U].mColInc = 2;
988                     mediaImage->mPlane[mediaImage->U].mRowInc = stride;
989                     mediaImage->mPlane[mediaImage->U].mHorizSubsampling = 2;
990                     mediaImage->mPlane[mediaImage->U].mVertSubsampling = 2;
991 
992                     mediaImage->mPlane[mediaImage->V].mOffset = stride * vStride + 1;
993                     mediaImage->mPlane[mediaImage->V].mColInc = 2;
994                     mediaImage->mPlane[mediaImage->V].mRowInc = stride;
995                     mediaImage->mPlane[mediaImage->V].mHorizSubsampling = 2;
996                     mediaImage->mPlane[mediaImage->V].mVertSubsampling = 2;
997 
998                     if (tryWrapping && mClientColorFormat != COLOR_FormatYUV420Flexible) {
999                         tryWrapping = yuv420888 && uPlane.colInc == 2 && vPlane.colInc == 2
1000                                 && yPlane.rowInc == uPlane.rowInc
1001                                 && view.data()[0] < view.data()[1]
1002                                 && view.data()[1] < view.data()[2];
1003                     }
1004                     break;
1005 
1006                 case COLOR_FormatYUVP010:
1007                     // stride is in bytes
1008                     mediaImage->mPlane[mediaImage->Y].mOffset = 0;
1009                     mediaImage->mPlane[mediaImage->Y].mColInc = 2;
1010                     mediaImage->mPlane[mediaImage->Y].mRowInc = stride;
1011                     mediaImage->mPlane[mediaImage->Y].mHorizSubsampling = 1;
1012                     mediaImage->mPlane[mediaImage->Y].mVertSubsampling = 1;
1013 
1014                     mediaImage->mPlane[mediaImage->U].mOffset = stride * vStride;
1015                     mediaImage->mPlane[mediaImage->U].mColInc = 4;
1016                     mediaImage->mPlane[mediaImage->U].mRowInc = stride;
1017                     mediaImage->mPlane[mediaImage->U].mHorizSubsampling = 2;
1018                     mediaImage->mPlane[mediaImage->U].mVertSubsampling = 2;
1019 
1020                     mediaImage->mPlane[mediaImage->V].mOffset = stride * vStride + 2;
1021                     mediaImage->mPlane[mediaImage->V].mColInc = 4;
1022                     mediaImage->mPlane[mediaImage->V].mRowInc = stride;
1023                     mediaImage->mPlane[mediaImage->V].mHorizSubsampling = 2;
1024                     mediaImage->mPlane[mediaImage->V].mVertSubsampling = 2;
1025                     if (tryWrapping) {
1026                         tryWrapping = yPlane.allocatedDepth == 16
1027                                 && uPlane.allocatedDepth == 16
1028                                 && vPlane.allocatedDepth == 16
1029                                 && yPlane.bitDepth == 10
1030                                 && uPlane.bitDepth == 10
1031                                 && vPlane.bitDepth == 10
1032                                 && yPlane.rightShift == 6
1033                                 && uPlane.rightShift == 6
1034                                 && vPlane.rightShift == 6
1035                                 && yPlane.rowSampling == 1 && yPlane.colSampling == 1
1036                                 && uPlane.rowSampling == 2 && uPlane.colSampling == 2
1037                                 && vPlane.rowSampling == 2 && vPlane.colSampling == 2
1038                                 && yPlane.colInc == 2
1039                                 && uPlane.colInc == 4
1040                                 && vPlane.colInc == 4
1041                                 && yPlane.rowInc == uPlane.rowInc
1042                                 && yPlane.rowInc == vPlane.rowInc;
1043                     }
1044                     break;
1045 
1046                 default: {
1047                     // default to fully planar format --- this will be overridden if wrapping
1048                     // TODO: keep interleaved format
1049                     int32_t colInc = divUp(mAllocatedDepth, 8u);
1050                     int32_t rowInc = stride * colInc / yPlane.colSampling;
1051                     mediaImage->mPlane[mediaImage->Y].mOffset = 0;
1052                     mediaImage->mPlane[mediaImage->Y].mColInc = colInc;
1053                     mediaImage->mPlane[mediaImage->Y].mRowInc = rowInc;
1054                     mediaImage->mPlane[mediaImage->Y].mHorizSubsampling = yPlane.colSampling;
1055                     mediaImage->mPlane[mediaImage->Y].mVertSubsampling = yPlane.rowSampling;
1056                     int32_t offset = rowInc * vStride / yPlane.rowSampling;
1057 
1058                     rowInc = stride * colInc / uPlane.colSampling;
1059                     mediaImage->mPlane[mediaImage->U].mOffset = offset;
1060                     mediaImage->mPlane[mediaImage->U].mColInc = colInc;
1061                     mediaImage->mPlane[mediaImage->U].mRowInc = rowInc;
1062                     mediaImage->mPlane[mediaImage->U].mHorizSubsampling = uPlane.colSampling;
1063                     mediaImage->mPlane[mediaImage->U].mVertSubsampling = uPlane.rowSampling;
1064                     offset += rowInc * vStride / uPlane.rowSampling;
1065 
1066                     rowInc = stride * colInc / vPlane.colSampling;
1067                     mediaImage->mPlane[mediaImage->V].mOffset = offset;
1068                     mediaImage->mPlane[mediaImage->V].mColInc = colInc;
1069                     mediaImage->mPlane[mediaImage->V].mRowInc = rowInc;
1070                     mediaImage->mPlane[mediaImage->V].mHorizSubsampling = vPlane.colSampling;
1071                     mediaImage->mPlane[mediaImage->V].mVertSubsampling = vPlane.rowSampling;
1072                     break;
1073                 }
1074             }
1075             break;
1076         }
1077 
1078         case C2PlanarLayout::TYPE_YUVA:
1079             ALOGD("Converter: unrecognized color format "
1080                     "(client %d component %d) for YUVA layout",
1081                     mClientColorFormat, mComponentColorFormat);
1082             mInitCheck = NO_INIT;
1083             return;
1084         case C2PlanarLayout::TYPE_RGB:
1085             mediaImage->mType = MediaImage2::MEDIA_IMAGE_TYPE_RGB;
1086             // TODO: support MediaImage layout
1087             switch (mClientColorFormat) {
1088                 case COLOR_FormatSurface:
1089                 case COLOR_FormatRGBFlexible:
1090                 case COLOR_Format24bitBGR888:
1091                 case COLOR_Format24bitRGB888:
1092                     ALOGD("Converter: accept color format "
1093                             "(client %d component %d) for RGB layout",
1094                             mClientColorFormat, mComponentColorFormat);
1095                     break;
1096                 default:
1097                     ALOGD("Converter: unrecognized color format "
1098                             "(client %d component %d) for RGB layout",
1099                             mClientColorFormat, mComponentColorFormat);
1100                     mInitCheck = BAD_VALUE;
1101                     return;
1102             }
1103             if (layout.numPlanes != 3) {
1104                 ALOGD("Converter: %d planes for RGB layout", layout.numPlanes);
1105                 mInitCheck = BAD_VALUE;
1106                 return;
1107             }
1108             break;
1109         case C2PlanarLayout::TYPE_RGBA:
1110             mediaImage->mType = MediaImage2::MEDIA_IMAGE_TYPE_RGBA;
1111             // TODO: support MediaImage layout
1112             switch (mClientColorFormat) {
1113                 case COLOR_FormatSurface:
1114                 case COLOR_FormatRGBAFlexible:
1115                 case COLOR_Format32bitABGR8888:
1116                 case COLOR_Format32bitARGB8888:
1117                 case COLOR_Format32bitBGRA8888:
1118                     ALOGD("Converter: accept color format "
1119                             "(client %d component %d) for RGBA layout",
1120                             mClientColorFormat, mComponentColorFormat);
1121                     break;
1122                 default:
1123                     ALOGD("Converter: unrecognized color format "
1124                             "(client %d component %d) for RGBA layout",
1125                             mClientColorFormat, mComponentColorFormat);
1126                     mInitCheck = BAD_VALUE;
1127                     return;
1128             }
1129             if (layout.numPlanes != 4) {
1130                 ALOGD("Converter: %d planes for RGBA layout", layout.numPlanes);
1131                 mInitCheck = BAD_VALUE;
1132                 return;
1133             }
1134             break;
1135         default:
1136             mediaImage->mType = MediaImage2::MEDIA_IMAGE_TYPE_UNKNOWN;
1137             if (layout.numPlanes == 1) {
1138                 const C2PlaneInfo &plane = layout.planes[0];
1139                 if (plane.colInc < 0 || plane.rowInc < 0) {
1140                     // Copy-only if we have negative colInc/rowInc
1141                     tryWrapping = false;
1142                 }
1143                 mediaImage->mPlane[0].mOffset = 0;
1144                 mediaImage->mPlane[0].mColInc = std::abs(plane.colInc);
1145                 mediaImage->mPlane[0].mRowInc = std::abs(plane.rowInc);
1146                 mediaImage->mPlane[0].mHorizSubsampling = plane.colSampling;
1147                 mediaImage->mPlane[0].mVertSubsampling = plane.rowSampling;
1148             } else {
1149                 ALOGD("Converter: unrecognized layout: color format (client %d component %d)",
1150                         mClientColorFormat, mComponentColorFormat);
1151                 mInitCheck = NO_INIT;
1152                 return;
1153             }
1154             break;
1155     }
1156     if (tryWrapping) {
1157         // try to map directly. check if the planes are near one another
1158         const uint8_t *minPtr = mView.data()[0];
1159         const uint8_t *maxPtr = mView.data()[0];
1160         int32_t planeSize = 0;
1161         for (uint32_t i = 0; i < layout.numPlanes; ++i) {
1162             const C2PlaneInfo &plane = layout.planes[i];
1163             int64_t planeStride = std::abs(plane.rowInc / plane.colInc);
1164             ssize_t minOffset = plane.minOffset(
1165                     mWidth / plane.colSampling, mHeight / plane.rowSampling);
1166             ssize_t maxOffset = plane.maxOffset(
1167                     mWidth / plane.colSampling, mHeight / plane.rowSampling);
1168             if (minPtr > mView.data()[i] + minOffset) {
1169                 minPtr = mView.data()[i] + minOffset;
1170             }
1171             if (maxPtr < mView.data()[i] + maxOffset) {
1172                 maxPtr = mView.data()[i] + maxOffset;
1173             }
1174             planeSize += planeStride * divUp(mAllocatedDepth, 8u)
1175                     * align(mHeight, 64) / plane.rowSampling;
1176         }
1177 
1178         if (minPtr == mView.data()[0] && (maxPtr - minPtr) <= planeSize) {
1179             // FIXME: this is risky as reading/writing data out of bound results
1180             //        in an undefined behavior, but gralloc does assume a
1181             //        contiguous mapping
1182             for (uint32_t i = 0; i < layout.numPlanes; ++i) {
1183                 const C2PlaneInfo &plane = layout.planes[i];
1184                 mediaImage->mPlane[i].mOffset = mView.data()[i] - minPtr;
1185                 mediaImage->mPlane[i].mColInc = plane.colInc;
1186                 mediaImage->mPlane[i].mRowInc = plane.rowInc;
1187                 mediaImage->mPlane[i].mHorizSubsampling = plane.colSampling;
1188                 mediaImage->mPlane[i].mVertSubsampling = plane.rowSampling;
1189             }
1190             mWrapped = new ABuffer(const_cast<uint8_t *>(minPtr), maxPtr - minPtr);
1191             ALOGV("Converter: wrapped (capacity=%zu)", mWrapped->capacity());
1192         }
1193     }
1194     mediaImage->mNumPlanes = layout.numPlanes;
1195     mediaImage->mWidth = view.crop().width;
1196     mediaImage->mHeight = view.crop().height;
1197     mediaImage->mBitDepth = bitDepth;
1198     mediaImage->mBitDepthAllocated = mAllocatedDepth;
1199 
1200     uint32_t bufferSize = 0;
1201     for (uint32_t i = 0; i < layout.numPlanes; ++i) {
1202         const C2PlaneInfo &plane = layout.planes[i];
1203         if (plane.allocatedDepth < plane.bitDepth
1204                 || plane.rightShift != plane.allocatedDepth - plane.bitDepth) {
1205             ALOGD("rightShift value of %u unsupported", plane.rightShift);
1206             mInitCheck = BAD_VALUE;
1207             return;
1208         }
1209         if (plane.allocatedDepth > 8 && plane.endianness != C2PlaneInfo::NATIVE) {
1210             ALOGD("endianness value of %u unsupported", plane.endianness);
1211             mInitCheck = BAD_VALUE;
1212             return;
1213         }
1214         if (plane.allocatedDepth != mAllocatedDepth || plane.bitDepth != bitDepth) {
1215             ALOGD("different allocatedDepth/bitDepth per plane unsupported");
1216             mInitCheck = BAD_VALUE;
1217             return;
1218         }
1219         // stride is in bytes
1220         bufferSize += stride * vStride / plane.rowSampling / plane.colSampling;
1221     }
1222 
1223     mBackBufferSize = bufferSize;
1224     mInitCheck = OK;
1225 }
1226 
initCheck() const1227 status_t GraphicView2MediaImageConverter::initCheck() const { return mInitCheck; }
1228 
backBufferSize() const1229 uint32_t GraphicView2MediaImageConverter::backBufferSize() const { return mBackBufferSize; }
1230 
wrap() const1231 sp<ABuffer> GraphicView2MediaImageConverter::wrap() const {
1232     if (mBackBuffer == nullptr) {
1233         return mWrapped;
1234     }
1235     return nullptr;
1236 }
1237 
setBackBuffer(const sp<ABuffer> & backBuffer)1238 bool GraphicView2MediaImageConverter::setBackBuffer(const sp<ABuffer> &backBuffer) {
1239     if (backBuffer == nullptr) {
1240         return false;
1241     }
1242     if (backBuffer->capacity() < mBackBufferSize) {
1243         return false;
1244     }
1245     backBuffer->setRange(0, mBackBufferSize);
1246     mBackBuffer = backBuffer;
1247     return true;
1248 }
1249 
copyToMediaImage()1250 status_t GraphicView2MediaImageConverter::copyToMediaImage() {
1251     ATRACE_CALL();
1252     if (mInitCheck != OK) {
1253         return mInitCheck;
1254     }
1255     return ImageCopy(mBackBuffer->base(), getMediaImage(), mView);
1256 }
1257 
imageData() const1258 const sp<ABuffer> &GraphicView2MediaImageConverter::imageData() const { return mMediaImage; }
1259 
getMediaImage()1260 MediaImage2 *GraphicView2MediaImageConverter::getMediaImage() {
1261     return (MediaImage2 *)mMediaImage->base();
1262 }
1263 
1264 }  // namespace android
1265