1 /* Copyright 2017 The Chromium OS Authors. All rights reserved. 2 * Use of this source code is governed by a BSD-style license that can be 3 * found in the LICENSE file. 4 */ 5 6 #ifndef HAL_USB_CACHED_FRAME_H_ 7 #define HAL_USB_CACHED_FRAME_H_ 8 9 #include <memory> 10 11 #include <camera/CameraMetadata.h> 12 #include "arc/image_processor.h" 13 14 namespace arc { 15 16 // CachedFrame contains a source FrameBuffer and a cached, converted 17 // FrameBuffer. The incoming frames would be converted to YU12, the default 18 // format of libyuv, to allow convenient processing. 19 class CachedFrame { 20 public: 21 CachedFrame(); 22 ~CachedFrame(); 23 24 // SetSource() doesn't take ownership of |frame|. The caller can only release 25 // |frame| after calling UnsetSource(). SetSource() immediately converts 26 // incoming frame into YU12. Return non-zero values if it encounters errors. 27 // If |rotate_degree| is 90 or 270, |frame| will be cropped, rotated by the 28 // specified amount and scaled. 29 // If |rotate_degree| is -1, |frame| will not be cropped, rotated, and scaled. 30 // This function will return an error if |rotate_degree| is not -1, 90, or 31 // 270. 32 int SetSource(const FrameBuffer* frame, int rotate_degree); 33 void UnsetSource(); 34 35 uint8_t* GetSourceBuffer() const; 36 size_t GetSourceDataSize() const; 37 uint32_t GetSourceFourCC() const; 38 uint8_t* GetCachedBuffer() const; 39 uint32_t GetCachedFourCC() const; 40 41 uint32_t GetWidth() const; 42 uint32_t GetHeight() const; 43 44 // Calculate the output buffer size when converting to the specified pixel 45 // format. |fourcc| is defined as V4L2_PIX_FMT_* in linux/videodev2.h. Return 46 // 0 on error. 47 size_t GetConvertedSize(int fourcc) const; 48 49 // Caller should fill everything except |data_size| and |fd| of |out_frame|. 50 // The function will do format conversion and scale to fit |out_frame| 51 // requirement. 52 // If |video_hack| is true, it outputs YU12 when |hal_pixel_format| is YV12 53 // (swapping U/V planes). Caller should fill |fourcc|, |data|, and 54 // Return non-zero error code on failure; return 0 on success. 55 int Convert(const android::CameraMetadata& metadata, FrameBuffer* out_frame, 56 bool video_hack = false); 57 58 private: 59 int ConvertToYU12(); 60 // When we have a landscape mounted camera and the current camera activity is 61 // portrait, the frames shown in the activity would be stretched. Therefore, 62 // we want to simulate a native portrait camera. That's why we want to crop, 63 // rotate |rotate_degree| clockwise and scale the frame. HAL would not change 64 // CameraInfo.orientation. Instead, framework would fake the 65 // CameraInfo.orientation. Framework would then tell HAL how much the frame 66 // needs to rotate clockwise by |rotate_degree|. 67 int CropRotateScale(int rotate_degree); 68 69 const FrameBuffer* source_frame_; 70 // const V4L2FrameBuffer* source_frame_; 71 72 // Temporary buffer for cropped and rotated results. 73 std::unique_ptr<uint8_t[]> cropped_buffer_; 74 size_t cropped_buffer_capacity_; 75 76 // Cache YU12 decoded results. 77 std::unique_ptr<AllocatedFrameBuffer> yu12_frame_; 78 79 // Temporary buffer for scaled results. 80 std::unique_ptr<AllocatedFrameBuffer> scaled_frame_; 81 }; 82 83 } // namespace arc 84 85 #endif // HAL_USB_CACHED_FRAME_H_ 86