MNN/source/backend/opencl/core/ImageBufferConvertor.hpp

106 lines
4.3 KiB
C++
Raw Normal View History

2019-04-17 10:49:11 +08:00
//
// ImageBufferConvertor.hpp
// MNN
//
// Created by MNN on 2019/01/31.
// Copyright © 2018, Alibaba Group Holding Limited
//
#ifndef ImageBufferConvertor_hpp
#define ImageBufferConvertor_hpp
2019-12-27 22:16:57 +08:00
#include "core/Macro.h"
#include <MNN/Tensor.hpp>
#include "backend/opencl/core/OpenCLRunningUtils.hpp"
2019-04-17 10:49:11 +08:00
namespace MNN {
namespace OpenCL {
/**
* @brief convert nchw buffer to image.
* @param input input tensor.
* @param output output tensor.
* @param bufferToImageKernel opencl kernel reference.
* @param runtime opencl runtime instance pointer.
* @param needWait whether need wait opencl complete before return or not, default false.
* @return true if success, false otherwise.
*/
bool convertNCHWBufferToImage(const Tensor *input, Tensor *output, cl::Kernel &bufferToImageKernel,
OpenCLRuntime *runtime, bool needWait = false);
/**
* @brief convert nhwc buffer to image.
* @param input input tensor.
* @param output output tensor.
* @param bufferToImageKernel opencl kernel reference.
* @param runtime opencl runtime instance pointer.
* @param needWait whether need wait opencl complete before return or not, default false.
* @return true if success, false otherwise.
*/
bool convertNHWCBufferToImage(const Tensor *input, Tensor *output, cl::Kernel &bufferToImageKernel,
OpenCLRuntime *runtime, bool needWait = false);
/**
* @brief convert image to nchw buffer.
* @param input input tensor.
* @param output output tensor.
* @param bufferToImageKernel opencl kernel reference.
* @param runtime opencl runtime instance pointer.
* @param needWait whether need wait opencl complete before return or not, default false.
* @return true if success, false otherwise.
*/
bool convertImageToNCHWBuffer(const Tensor *input, Tensor *output, cl::Kernel &imageToBufferKernel,
OpenCLRuntime *runtime, bool needWait = false);
/**
* @brief convert nc/4hwc%4 buffer to image.
* @param input input tensor.
* @param output output tensor.
* @param bufferToImageKernel opencl kernel reference.
* @param runtime opencl runtime instance pointer.
* @param needWait whether need wait opencl complete before return or not, default false.
* @return true if success, false otherwise.
*/
bool convertNC4HW4BufferToImage(const Tensor *input, Tensor *output, cl::Kernel &bufferToImageKernel,
OpenCLRuntime *runtime, bool needWait = false);
/**
* @brief convert image to nc/4hwc%4 buffer.
* @param input input tensor.
* @param output output tensor.
* @param bufferToImageKernel opencl kernel reference.
* @param runtime opencl runtime instance pointer.
* @param needWait whether need wait opencl complete before return or not, default false.
* @return true if success, false otherwise.
*/
bool convertImageToNC4HW4Buffer(const Tensor *input, Tensor *output, cl::Kernel &imageToBufferKernel,
OpenCLRuntime *runtime, bool needWait = false);
/**
* @brief convert image to nhwc buffer.
* @param input input tensor.
* @param output output tensor.
* @param bufferToImageKernel opencl kernel reference.
* @param runtime opencl runtime instance pointer.
* @param needWait whether need wait opencl complete before return or not, default false.
* @return true if success, false otherwise.
*/
bool convertImageToNHWCBuffer(const Tensor *input, Tensor *output, cl::Kernel &imageToBufferKernel,
OpenCLRuntime *runtime, bool needWait = false);
class ImageBufferConvertor {
public:
explicit ImageBufferConvertor(OpenCLRuntime *opencl_runtime) : mOpenCLRuntime(opencl_runtime) {
}
bool convertImageToBuffer(const Tensor *input, const OpenCLBufferFormat type, Tensor *output,
bool needWait = false);
bool convertBufferToImage(const Tensor *input, const OpenCLBufferFormat type, Tensor *output,
2020-11-05 16:41:56 +08:00
bool needWait = false, const std::string &buildOption = "");
2019-04-17 10:49:11 +08:00
private:
OpenCLRuntime *mOpenCLRuntime;
cl::Kernel mImageToBufferKernel;
std::string mImageToBufferKernelName;
cl::Kernel mBufferToImageKernel;
std::string mBufferToImageKernelName;
};
} // namespace OpenCL
} // namespace MNN
#endif /* ImageBufferConvertor_hpp */