From fb4ae3c06ca6598ecf31d9e39276f87091e5037d Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 12 Jan 2017 13:02:19 +0100 Subject: cuda: use libavutil functions for copying hw surfaces to memory mp_image_hw_download() is a libavutil wrapper added in the previous commit. We drop our own code completely, as everything is provided by libavutil and our helper wrapper. This breaks the screenshot code, so that has to be adjusted as well. --- player/screenshot.c | 16 +++++++---- video/out/opengl/hwdec_cuda.c | 67 ------------------------------------------- 2 files changed, 10 insertions(+), 73 deletions(-) diff --git a/player/screenshot.c b/player/screenshot.c index 13532ec1a3..4c043ab986 100644 --- a/player/screenshot.c +++ b/player/screenshot.c @@ -31,6 +31,7 @@ #include "common/msg.h" #include "options/path.h" #include "video/mp_image.h" +#include "video/mp_image_pool.h" #include "video/decode/dec_video.h" #include "video/out/vo.h" #include "video/image_writer.h" @@ -346,12 +347,15 @@ static struct mp_image *screenshot_get(struct MPContext *mpctx, int mode) } } - if (image && mpctx->vo_chain && mpctx->vo_chain->hwdec_devs) { - struct mp_hwdec_ctx *ctx = - hwdec_devices_get_first(mpctx->vo_chain->hwdec_devs); - struct mp_image *nimage = NULL; - if (ctx && ctx->download_image && (image->fmt.flags & MP_IMGFLAG_HWACCEL)) - nimage = ctx->download_image(ctx, image, NULL); + bool hwimage = image && (image->fmt.flags & MP_IMGFLAG_HWACCEL); + if (hwimage) { + struct mp_image *nimage = mp_image_hw_download(image, NULL); + if (!nimage && mpctx->vo_chain && mpctx->vo_chain->hwdec_devs) { + struct mp_hwdec_ctx *ctx = + hwdec_devices_get_first(mpctx->vo_chain->hwdec_devs); + if (ctx && ctx->download_image && hwimage) + nimage = ctx->download_image(ctx, image, NULL); + } if (nimage) { talloc_free(image); image = nimage; diff --git a/video/out/opengl/hwdec_cuda.c b/video/out/opengl/hwdec_cuda.c index 4edad96c7a..d7821f1a9f 100644 --- a/video/out/opengl/hwdec_cuda.c +++ b/video/out/opengl/hwdec_cuda.c @@ -66,72 +66,6 @@ static int check_cu(struct gl_hwdec *hw, CUresult err, const char *func) #define CHECK_CU(x) check_cu(hw, (x), #x) -static struct mp_image *cuda_download_image(struct mp_hwdec_ctx *ctx, - struct mp_image *hw_image, - struct mp_image_pool *swpool) -{ - CUcontext cuda_ctx = ctx->ctx; - CUcontext dummy; - CUresult err, eerr; - - if (hw_image->imgfmt != IMGFMT_CUDA) - return NULL; - - int sample_width; - switch (hw_image->params.hw_subfmt) { - case IMGFMT_NV12: - sample_width = 1; - break; - case IMGFMT_P010: - case IMGFMT_P016: - sample_width = 2; - break; - default: - return NULL; - } - - struct mp_image *out = mp_image_pool_get(swpool, - hw_image->params.hw_subfmt, - hw_image->w, hw_image->h); - if (!out) - return NULL; - - err = cuCtxPushCurrent(cuda_ctx); - if (err != CUDA_SUCCESS) - goto error; - - mp_image_set_size(out, hw_image->w, hw_image->h); - mp_image_copy_attributes(out, hw_image); - - for (int n = 0; n < 2; n++) { - CUDA_MEMCPY2D cpy = { - .srcMemoryType = CU_MEMORYTYPE_DEVICE, - .dstMemoryType = CU_MEMORYTYPE_HOST, - .srcDevice = (CUdeviceptr)hw_image->planes[n], - .dstHost = out->planes[n], - .srcPitch = hw_image->stride[n], - .dstPitch = out->stride[n], - .WidthInBytes = mp_image_plane_w(out, n) * - (n + 1) * sample_width, - .Height = mp_image_plane_h(out, n), - }; - - err = cuMemcpy2D(&cpy); - if (err != CUDA_SUCCESS) { - goto error; - } - } - - error: - eerr = cuCtxPopCurrent(&dummy); - if (eerr != CUDA_SUCCESS || err != CUDA_SUCCESS) { - talloc_free(out); - return NULL; - } - - return out; -} - static int cuda_create(struct gl_hwdec *hw) { CUdevice device; @@ -172,7 +106,6 @@ static int cuda_create(struct gl_hwdec *hw) p->hwctx = (struct mp_hwdec_ctx) { .type = HWDEC_CUDA, .ctx = cuda_ctx, - .download_image = cuda_download_image, }; p->hwctx.driver_name = hw->driver->name; hwdec_devices_add(hw->devs, &p->hwctx); -- cgit v1.2.3