// Copyright 2020 The MediaPipe Authors. // // 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. #ifndef MEDIAPIPE_FRAMEWORK_FORMATS_TENSOR_H_ #define MEDIAPIPE_FRAMEWORK_FORMATS_TENSOR_H_ #include <algorithm> #include <cstdint> #include <functional> #include <initializer_list> #include <memory> #include <numeric> #include <tuple> #include <type_traits> #include <utility> #include <vector> #include "absl/functional/any_invocable.h" #include "absl/status/status.h" #include "absl/synchronization/mutex.h" #include "mediapipe/framework/formats/tensor/internal.h" #include "mediapipe/framework/memory_manager.h" // Exports MEDIAPIPE_TENSOR_USE_AHWB macro. #include "mediapipe/framework/port.h" #ifdef MEDIAPIPE_TENSOR_USE_AHWB #include <EGL/egl.h> #include <EGL/eglext.h> #include <list> #include "absl/log/absl_check.h" #include "mediapipe/framework/formats/hardware_buffer.h" #include "mediapipe/framework/formats/hardware_buffer_pool.h" #include "mediapipe/framework/formats/tensor_ahwb_usage.h" #include "mediapipe/framework/formats/unique_fd.h" #endif // MEDIAPIPE_TENSOR_USE_AHWB #if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_30 #include "mediapipe/gpu/gl_base.h" #include "mediapipe/gpu/gl_context.h" #endif // MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_30 #if defined __has_builtin #if __has_builtin(__builtin_LINE) #define builtin_LINE … #endif #if __has_builtin(__builtin_FILE) #define builtin_FILE … #endif #endif #ifndef builtin_LINE #define builtin_LINE … #endif #ifndef builtin_FILE #define builtin_FILE … #endif #include "mediapipe/gpu/webgpu/webgpu_check.h" #if MEDIAPIPE_USE_WEBGPU #include <webgpu/webgpu_cpp.h> #include "mediapipe/gpu/webgpu/webgpu_service.h" #endif // MEDIAPIPE_USE_WEBGPU namespace mediapipe { // Tensor is a container of multi-dimensional data that supports sharing the // content across different backends and APIs, currently: CPU / Metal / OpenGL. // Texture2DView is limited to 4 dimensions. // The content is accessible through requesting device specific views. // Acquiring a view guarantees that the content is not changed by another thread // until the view is released. // // Tensor::MtlBufferView view = tensor.GetMtlBufferWriteView(mtl_device); // mtl_device is used to create MTLBuffer // id<MTLBuffer> buffer = view.buffer(); // For OpenGL the code below must be called by a thread with valid OpenGL ES // context bound: // GLuint buffer = view.buffer(); // Then the buffer can be bound to the GPU command buffer. // ...binding the buffer to the command buffer... // ...committing command buffer and releasing the view... // // The following request for the CPU view will be blocked until the GPU view is // released and the GPU task is finished. // // auto view = tensor.GetCpuReadView(); // float* pointer = view.buffer<float>(); // ...reading the cpu memory... struct MtlResources; class Tensor { … }; int BhwcBatchFromShape(const Tensor::Shape& shape); int BhwcHeightFromShape(const Tensor::Shape& shape); int BhwcWidthFromShape(const Tensor::Shape& shape); int BhwcDepthFromShape(const Tensor::Shape& shape); } // namespace mediapipe #endif // MEDIAPIPE_FRAMEWORK_FORMATS_TENSOR_H_