aboutsummaryrefslogtreecommitdiff
path: root/guest/hals/camera/cached_stream_buffer.cpp
blob: ff692c7dc5f6864dc5feba44de9c99b52faf9a64 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * 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.
 */
#define LOG_TAG "CachedStreamBuffer"
#include "cached_stream_buffer.h"
#include <hardware/gralloc.h>
#include <log/log.h>
#include <sync/sync.h>

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

namespace {
HandleImporter g_importer;
}

ReleaseFence::ReleaseFence(int fence_fd) : handle_(nullptr) {
  if (fence_fd >= 0) {
    handle_ = native_handle_create(/*numFds*/ 1, /*numInts*/ 0);
    handle_->data[0] = fence_fd;
  }
}

ReleaseFence::~ReleaseFence() {
  if (handle_ != nullptr) {
    native_handle_close(handle_);
    native_handle_delete(handle_);
  }
}

CachedStreamBuffer::CachedStreamBuffer()
    : buffer_(nullptr), buffer_id_(0), stream_id_(0), acquire_fence_(-1) {}

CachedStreamBuffer::CachedStreamBuffer(const StreamBuffer& buffer)
    : buffer_(buffer.buffer.getNativeHandle()),
      buffer_id_(buffer.bufferId),
      stream_id_(buffer.streamId),
      acquire_fence_(-1) {
  g_importer.importBuffer(buffer_);
  g_importer.importFence(buffer.acquireFence, acquire_fence_);
}

CachedStreamBuffer::CachedStreamBuffer(CachedStreamBuffer&& from) noexcept {
  buffer_ = from.buffer_;
  buffer_id_ = from.buffer_id_;
  stream_id_ = from.stream_id_;
  acquire_fence_ = from.acquire_fence_;
  from.acquire_fence_ = -1;
  from.buffer_ = nullptr;
}

CachedStreamBuffer& CachedStreamBuffer::operator=(
    CachedStreamBuffer&& from) noexcept {
  if (this != &from) {
    buffer_ = from.buffer_;
    buffer_id_ = from.buffer_id_;
    stream_id_ = from.stream_id_;
    acquire_fence_ = from.acquire_fence_;
    from.acquire_fence_ = -1;
    from.buffer_ = nullptr;
  }
  return *this;
}

CachedStreamBuffer::~CachedStreamBuffer() {
  if (buffer_ != nullptr) {
    g_importer.freeBuffer(buffer_);
  }
  g_importer.closeFence(acquire_fence_);
}

void CachedStreamBuffer::importFence(const native_handle_t* fence_handle) {
  g_importer.closeFence(acquire_fence_);
  g_importer.importFence(fence_handle, acquire_fence_);
}

YCbCrLayout CachedStreamBuffer::acquireAsYUV(int32_t width, int32_t height,
                                             int timeout_ms) {
  if (acquire_fence_ >= 0) {
    if (sync_wait(acquire_fence_, timeout_ms)) {
      ALOGW("%s: timeout while waiting acquire fence", __FUNCTION__);
      return {};
    } else {
      ::close(acquire_fence_);
      acquire_fence_ = -1;
    }
  }
  IMapper::Rect region{0, 0, width, height};
  return g_importer.lockYCbCr(buffer_, GRALLOC_USAGE_SW_WRITE_OFTEN, region);
}

void* CachedStreamBuffer::acquireAsBlob(int32_t size, int timeout_ms) {
  if (acquire_fence_ >= 0) {
    if (sync_wait(acquire_fence_, timeout_ms)) {
      return nullptr;
    } else {
      ::close(acquire_fence_);
      acquire_fence_ = -1;
    }
  }
  return g_importer.lock(buffer_, GRALLOC_USAGE_SW_WRITE_OFTEN, size);
}

int CachedStreamBuffer::release() { return g_importer.unlock(buffer_); }

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