OSDN Git Service

readme: Add mention to waffle kanban
[android-x86/hardware-intel-common-libva.git] / va / va.c
diff --git a/va/va.c b/va/va.c
index 3ef01fa..5cf7220 100644 (file)
--- a/va/va.c
+++ b/va/va.c
@@ -74,8 +74,10 @@ int va_parseConfig(char *env, char *env_value)
            continue;
 
         if (strcmp(token, env) == 0) {
-            if (env_value)
+            if (env_value) {
                 strncpy(env_value,value, 1024);
+                env_value[1023] = '\0';
+            }
 
             fclose(fp);
 
@@ -88,8 +90,10 @@ int va_parseConfig(char *env, char *env_value)
     /* no setting in config file, use env setting */
     value = getenv(env);
     if (value) {
-        if (env_value)
+        if (env_value) {
             strncpy(env_value, value, 1024);
+            env_value[1023] = '\0';
+        }
         return 0;
     }
     
@@ -102,12 +106,62 @@ int vaDisplayIsValid(VADisplay dpy)
     return pDisplayContext && (pDisplayContext->vadpy_magic == VA_DISPLAY_MAGIC) && pDisplayContext->vaIsValid(pDisplayContext);
 }
 
+static void default_log_error(const char *buffer)
+{
+# ifdef ANDROID_ALOG
+    ALOGE("%s", buffer);
+# elif ANDROID_LOG
+    LOGE("%s", buffer);
+# else
+    fprintf(stderr, "libva error: %s", buffer);
+# endif
+}
+
+static void default_log_info(const char *buffer)
+{
+# ifdef ANDROID_ALOG
+    ALOGI("%s", buffer);
+# elif ANDROID_LOG
+    LOGI("%s", buffer);
+# else
+    fprintf(stderr, "libva info: %s", buffer);
+# endif
+}
+
+static vaMessageCallback va_log_error = default_log_error;
+static vaMessageCallback va_log_info = default_log_info;
+
+/**
+ * Set the callback for error messages, or NULL for no logging.
+ * Returns the previous one, or NULL if it was disabled.
+ */
+vaMessageCallback vaSetErrorCallback(vaMessageCallback callback)
+{
+    vaMessageCallback old_callback = va_log_error;
+    va_log_error = callback;
+    return old_callback;
+}
+
+/**
+ * Set the callback for info messages, or NULL for no logging.
+ * Returns the previous one, or NULL if it was disabled.
+ */
+vaMessageCallback vaSetInfoCallback(vaMessageCallback callback)
+{
+    vaMessageCallback old_callback = va_log_info;
+    va_log_info = callback;
+    return old_callback;
+}
+
 void va_errorMessage(const char *msg, ...)
 {
     char buf[512], *dynbuf;
     va_list args;
     int n, len;
 
+    if (va_log_error == NULL)
+        return;
+
     va_start(args, msg);
     len = vsnprintf(buf, sizeof(buf), msg, args);
     va_end(args);
@@ -133,6 +187,9 @@ void va_infoMessage(const char *msg, ...)
     va_list args;
     int n, len;
 
+    if (va_log_info == NULL)
+        return;
+
     va_start(args, msg);
     len = vsnprintf(buf, sizeof(buf), msg, args);
     va_end(args);
@@ -246,6 +303,10 @@ static VAStatus va_openDriver(VADisplay dpy, char *driver_name)
                 int minor;
             } compatible_versions[] = {
                 { VA_MAJOR_VERSION, VA_MINOR_VERSION },
+                { 0, 38 },
+                { 0, 37 },
+                { 0, 36 },
+                { 0, 35 },
                 { 0, 34 },
                 { 0, 33 },
                 { 0, 32 },
@@ -438,7 +499,68 @@ const char *vaErrorStr(VAStatus error_status)
     }
     return "unknown libva error / description missing";
 }
-      
+
+const static char *prefer_driver_list[4] = {
+    "i965",
+    "hybrid",
+    "pvr",
+    "iHD",
+};
+
+VAStatus vaSetDriverName(
+    VADisplay dpy,
+    char *driver_name
+)
+{
+    VADriverContextP ctx;
+    VAStatus vaStatus = VA_STATUS_SUCCESS;
+    char *override_driver_name = NULL;
+    int i, found;
+    ctx = CTX(dpy);
+
+    if (geteuid() != getuid()) {
+        vaStatus = VA_STATUS_ERROR_OPERATION_FAILED;
+        va_errorMessage("no permission to vaSetDriverName\n");
+        return vaStatus;
+    }
+
+    if (strlen(driver_name) == 0 || strlen(driver_name) >=256) {
+        vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
+        va_errorMessage("vaSetDriverName returns %s\n",
+                         vaErrorStr(vaStatus));
+        return vaStatus;
+    }
+
+    found = 0;
+    for (i = 0; i < sizeof(prefer_driver_list) / sizeof(char *); i++) {
+        if (strlen(prefer_driver_list[i]) != strlen(driver_name))
+            continue;
+        if (!strncmp(prefer_driver_list[i], driver_name, strlen(driver_name))) {
+            found = 1;
+            break;
+        }
+    }
+
+    if (!found) {
+        vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
+        va_errorMessage("vaSetDriverName returns %s. Incorrect parameter\n",
+                         vaErrorStr(vaStatus));
+        return vaStatus;
+    }
+
+    override_driver_name = strdup(driver_name);
+
+    if (!override_driver_name) {
+        vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
+        va_errorMessage("vaSetDriverName returns %s. Out of Memory\n",
+                         vaErrorStr(vaStatus));
+        return vaStatus;
+    }
+
+    ctx->override_driver_name = override_driver_name;
+    return VA_STATUS_SUCCESS;
+}
+
 VAStatus vaInitialize (
     VADisplay dpy,
     int *major_version,         /* out */
@@ -448,9 +570,12 @@ VAStatus vaInitialize (
     const char *driver_name_env = NULL;
     char *driver_name = NULL;
     VAStatus vaStatus;
+    VADriverContextP ctx;
 
     CHECK_DISPLAY(dpy);
 
+    ctx = CTX(dpy);
+
     va_TraceInit(dpy);
 
     va_FoolInit(dpy);
@@ -458,11 +583,26 @@ VAStatus vaInitialize (
     va_infoMessage("VA-API version %s\n", VA_VERSION_S);
 
     vaStatus = va_getDriverName(dpy, &driver_name);
-    va_infoMessage("va_getDriverName() returns %d\n", vaStatus);
 
-    driver_name_env = getenv("LIBVA_DRIVER_NAME");
-    if ((VA_STATUS_SUCCESS == vaStatus) &&
-        driver_name_env && (geteuid() == getuid())) {
+    if (!ctx->override_driver_name) {
+        va_infoMessage("va_getDriverName() returns %d\n", vaStatus);
+
+        driver_name_env = getenv("LIBVA_DRIVER_NAME");
+    } else if (vaStatus == VA_STATUS_SUCCESS) {
+        if (driver_name)
+            free(driver_name);
+
+        driver_name = strdup(ctx->override_driver_name);
+        if (!driver_name) {
+            vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
+            va_errorMessage("vaInitialize() failed with %s, out of memory\n",
+                        vaErrorStr(vaStatus));
+            return vaStatus;
+        }
+        va_infoMessage("User requested driver '%s'\n", driver_name);
+    }
+
+    if (driver_name_env && (geteuid() == getuid())) {
         /* Don't allow setuid apps to use LIBVA_DRIVER_NAME */
         if (driver_name) /* memory is allocated in va_getDriverName */
             free(driver_name);
@@ -515,6 +655,11 @@ VAStatus vaTerminate (
   free(old_ctx->vtable_vpp);
   old_ctx->vtable_vpp = NULL;
 
+  if (old_ctx->override_driver_name) {
+      free(old_ctx->override_driver_name);
+      old_ctx->override_driver_name = NULL;
+  }
+
   VA_TRACE_LOG(va_TraceTerminate, dpy);
 
   va_TraceEnd(dpy);
@@ -633,7 +778,6 @@ VAStatus vaCreateConfig (
 {
   VADriverContextP ctx;
   VAStatus vaStatus = VA_STATUS_SUCCESS;
-  int ret = 0;
   
   CHECK_DISPLAY(dpy);
   ctx = CTX(dpy);
@@ -641,7 +785,7 @@ VAStatus vaCreateConfig (
   vaStatus = ctx->vtable->vaCreateConfig ( ctx, profile, entrypoint, attrib_list, num_attribs, config_id );
 
   /* record the current entrypoint for further trace/fool determination */
-  VA_TRACE_LOG(va_TraceCreateConfig, dpy, profile, entrypoint, attrib_list, num_attribs, config_id);
+  VA_TRACE_ALL(va_TraceCreateConfig, dpy, profile, entrypoint, attrib_list, num_attribs, config_id);
   VA_FOOL_FUNC(va_FoolCreateConfig, dpy, profile, entrypoint, attrib_list, num_attribs, config_id);
   
   return vaStatus;
@@ -653,10 +797,16 @@ VAStatus vaDestroyConfig (
 )
 {
   VADriverContextP ctx;
+  VAStatus vaStatus = VA_STATUS_SUCCESS;
+
   CHECK_DISPLAY(dpy);
   ctx = CTX(dpy);
 
-  return ctx->vtable->vaDestroyConfig ( ctx, config_id );
+  vaStatus = ctx->vtable->vaDestroyConfig ( ctx, config_id );
+
+  VA_TRACE_ALL(va_TraceDestroyConfig, dpy, config_id);
+
+  return vaStatus;
 }
 
 VAStatus vaQueryConfigAttributes (
@@ -917,7 +1067,7 @@ VAStatus vaCreateContext (
                                       flag, render_targets, num_render_targets, context );
 
   /* keep current encode/decode resoluton */
-  VA_TRACE_LOG(va_TraceCreateContext, dpy, config_id, picture_width, picture_height, flag, render_targets, num_render_targets, context);
+  VA_TRACE_ALL(va_TraceCreateContext, dpy, config_id, picture_width, picture_height, flag, render_targets, num_render_targets, context);
 
   return vaStatus;
 }
@@ -928,10 +1078,16 @@ VAStatus vaDestroyContext (
 )
 {
   VADriverContextP ctx;
+  VAStatus vaStatus;
+
   CHECK_DISPLAY(dpy);
   ctx = CTX(dpy);
 
-  return ctx->vtable->vaDestroyContext( ctx, context );
+  vaStatus = ctx->vtable->vaDestroyContext( ctx, context );
+
+  VA_TRACE_ALL(va_TraceDestroyContext, dpy, context);
+
+  return vaStatus;
 }
 
 VAStatus vaCreateBuffer (
@@ -946,14 +1102,11 @@ VAStatus vaCreateBuffer (
 {
   VADriverContextP ctx;
   VAStatus vaStatus;
-  int ret = 0;
   
   CHECK_DISPLAY(dpy);
   ctx = CTX(dpy);
 
   VA_FOOL_FUNC(va_FoolCreateBuffer, dpy, context, type, size, num_elements, data, buf_id);
-  if (ret)
-      return VA_STATUS_SUCCESS;
 
   vaStatus = ctx->vtable->vaCreateBuffer( ctx, context, type, size, num_elements, data, buf_id);
 
@@ -972,8 +1125,8 @@ VAStatus vaBufferSetNumElements (
   VADriverContextP ctx;
   CHECK_DISPLAY(dpy);
   ctx = CTX(dpy);
-  
-  VA_FOOL_RETURN();
+
+  VA_FOOL_FUNC(va_FoolCheckContinuity, dpy);
   
   return ctx->vtable->vaBufferSetNumElements( ctx, buf_id, num_elements );
 }
@@ -987,18 +1140,15 @@ VAStatus vaMapBuffer (
 {
   VADriverContextP ctx;
   VAStatus va_status;
-  int ret = 0;
   
   CHECK_DISPLAY(dpy);
   ctx = CTX(dpy);
-
+  
   VA_FOOL_FUNC(va_FoolMapBuffer, dpy, buf_id, pbuf);
-  if (ret)
-      return VA_STATUS_SUCCESS;
   
   va_status = ctx->vtable->vaMapBuffer( ctx, buf_id, pbuf );
 
-  VA_TRACE_LOG(va_TraceMapBuffer, dpy, buf_id, pbuf);
+  VA_TRACE_ALL(va_TraceMapBuffer, dpy, buf_id, pbuf);
   
   return va_status;
 }
@@ -1011,11 +1161,8 @@ VAStatus vaUnmapBuffer (
   VADriverContextP ctx;
   CHECK_DISPLAY(dpy);
   ctx = CTX(dpy);
-  int ret = 0;
 
-  VA_FOOL_FUNC(va_FoolUnmapBuffer, dpy, buf_id);
-  if (ret)
-      return VA_STATUS_SUCCESS;
+  VA_FOOL_FUNC(va_FoolCheckContinuity, dpy);
 
   return ctx->vtable->vaUnmapBuffer( ctx, buf_id );
 }
@@ -1029,7 +1176,7 @@ VAStatus vaDestroyBuffer (
   CHECK_DISPLAY(dpy);
   ctx = CTX(dpy);
 
-  VA_FOOL_RETURN();
+  VA_FOOL_FUNC(va_FoolCheckContinuity, dpy);
 
   VA_TRACE_LOG(va_TraceDestroyBuffer,
                dpy, buffer_id);
@@ -1047,14 +1194,11 @@ VAStatus vaBufferInfo (
 )
 {
   VADriverContextP ctx;
-  int ret = 0;
   
   CHECK_DISPLAY(dpy);
   ctx = CTX(dpy);
 
   VA_FOOL_FUNC(va_FoolBufferInfo, dpy, buf_id, type, size, num_elements);
-  if (ret)
-      return VA_STATUS_SUCCESS;
   
   return ctx->vtable->vaBufferInfo( ctx, buf_id, type, size, num_elements );
 }
@@ -1099,8 +1243,8 @@ VAStatus vaBeginPicture (
   CHECK_DISPLAY(dpy);
   ctx = CTX(dpy);
 
-  VA_TRACE_LOG(va_TraceBeginPicture, dpy, context, render_target);
-  VA_FOOL_RETURN();
+  VA_TRACE_ALL(va_TraceBeginPicture, dpy, context, render_target);
+  VA_FOOL_FUNC(va_FoolCheckContinuity, dpy);
   
   va_status = ctx->vtable->vaBeginPicture( ctx, context, render_target );
   
@@ -1120,7 +1264,7 @@ VAStatus vaRenderPicture (
   ctx = CTX(dpy);
 
   VA_TRACE_LOG(va_TraceRenderPicture, dpy, context, buffers, num_buffers);
-  VA_FOOL_RETURN();
+  VA_FOOL_FUNC(va_FoolCheckContinuity, dpy);
 
   return ctx->vtable->vaRenderPicture( ctx, context, buffers, num_buffers );
 }
@@ -1136,11 +1280,12 @@ VAStatus vaEndPicture (
   CHECK_DISPLAY(dpy);
   ctx = CTX(dpy);
 
-  if (fool_codec == 0)
-      va_status = ctx->vtable->vaEndPicture( ctx, context );
+  VA_FOOL_FUNC(va_FoolCheckContinuity, dpy);
+
+  va_status = ctx->vtable->vaEndPicture( ctx, context );
 
   /* dump surface content */
-  VA_TRACE_SURFACE(va_TraceEndPicture, dpy, context, 1);
+  VA_TRACE_ALL(va_TraceEndPicture, dpy, context, 1);
 
   return va_status;
 }