aboutsummaryrefslogtreecommitdiff
path: root/guest/hals/camera/cached_stream_buffer.h
blob: 19d738d2b0439baf6d4556e2a249b43afcd32654 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once
#include <android/hardware/camera/device/3.4/ICameraDeviceSession.h>
#include <android/hardware/graphics/mapper/2.0/IMapper.h>
#include <android/hardware/graphics/mapper/3.0/IMapper.h>
#include <android/hardware/graphics/mapper/4.0/IMapper.h>
#include "HandleImporter.h"

namespace android::hardware::camera::device::V3_4::implementation {

using ::android::hardware::camera::common::V1_0::helper::HandleImporter;
using ::android::hardware::camera::device::V3_2::StreamBuffer;
using ::android::hardware::graphics::mapper::V2_0::YCbCrLayout;

// Small wrapper for allocating/freeing native handles
class ReleaseFence {
 public:
  ReleaseFence(int fence_fd);
  ~ReleaseFence();

  native_handle_t* handle() const { return handle_; }

 private:
  native_handle_t* handle_;
};

// CachedStreamBuffer holds a buffer of camera3 stream.
class CachedStreamBuffer {
 public:
  CachedStreamBuffer();
  CachedStreamBuffer(const StreamBuffer& buffer);
  // Not copyable
  CachedStreamBuffer(const CachedStreamBuffer&) = delete;
  CachedStreamBuffer& operator=(const CachedStreamBuffer&) = delete;
  // ...but movable
  CachedStreamBuffer(CachedStreamBuffer&& from) noexcept;
  CachedStreamBuffer& operator=(CachedStreamBuffer&& from) noexcept;

  ~CachedStreamBuffer();

  bool valid() const { return buffer_ != nullptr; }
  uint64_t bufferId() const { return buffer_id_; }
  int32_t streamId() const { return stream_id_; }
  int acquireFence() const { return acquire_fence_; }

  void importFence(const native_handle_t* fence_handle);
  // Acquire methods wait first on acquire fence and then return pointers to
  // data. Data is nullptr if the wait timed out
  YCbCrLayout acquireAsYUV(int32_t width, int32_t height, int timeout_ms);
  void* acquireAsBlob(int32_t size, int timeout_ms);
  int release();

 private:
  buffer_handle_t buffer_;
  uint64_t buffer_id_;
  int32_t stream_id_;
  int acquire_fence_;
};

}  // namespace android::hardware::camera::device::V3_4::implementation