blob: 2a6de2e9640f0cfa0db41d2c69f74367c1c05e69 [file] [log] [blame]
Kuang-che Wudb9c20b2014-04-17 16:14:18 +08001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4#include <stdlib.h>
5#include <stdio.h>
6#include <alloca.h>
7
8#include <X11/Xlib.h>
9#include <va/va.h>
10#include <va/va_x11.h>
11
12#include "label_detect.h"
13
14/* Returns true if given VA profile |va_profile| has |entrypoint| and the entry
15 * point supports given raw |format|.
16 */
17static bool has_vaapi_entrypoint(VADisplay va_display, VAProfile va_profile,
18 VAEntrypoint entrypoint, unsigned int format) {
19 VAStatus va_res;
20 VAConfigAttrib attrib = {VAConfigAttribRTFormat, 0};
21 va_res = vaGetConfigAttributes(va_display, va_profile, entrypoint,
22 &attrib, 1);
23 if (va_res != VA_STATUS_SUCCESS) {
24 TRACE("vaGetConfigAttributes failed (%d)\n", va_res);
25 return false;
26 }
27
28 return attrib.value & format;
29}
30
31/* Returns true if the current platform supports at least one of the
32 * |required_profiles| and |entrypoint| for that profile supports given raw
33 * |format|.
34 */
35static bool match_vaapi_capabilities(VADisplay va_display,
36 VAProfile* required_profiles,
37 VAEntrypoint entrypoint, unsigned int format) {
38 int i;
39 bool found;
40 int num_supported_profiles;
41 VAStatus va_res;
42 VAProfile* profiles;
43 int max_profiles = vaMaxNumProfiles(va_display);
44 if (max_profiles < 0) {
45 TRACE("vaMaxNumProfiles returns negative number\n");
46 return false;
47 }
48
49 profiles = (VAProfile*) alloca(sizeof(VAProfile) * max_profiles);
50 if (!profiles) {
51 TRACE("alloca failed\n");
52 return false;
53 }
54 va_res = vaQueryConfigProfiles(va_display, profiles, &num_supported_profiles);
55 if (va_res != VA_STATUS_SUCCESS) {
56 TRACE("vaQueryConfigProfiles failed (%d)\n", va_res);
57 return false;
58 }
59 for (i = 0; i < num_supported_profiles; i++) {
60 int j;
61 VAProfile profile = profiles[i];
62 TRACE("supported profile: %d\n", profile);
63 for (j = 0; required_profiles[j] != VAProfileNone; j++) {
64 if (required_profiles[j] == profile &&
65 has_vaapi_entrypoint(va_display, profile, entrypoint, format)) {
66 found = true;
67 /* continue the loop in order to output all supported profiles */
68 }
69 }
70 }
71 return found;
72}
73
74/* Returns true if libva supports any given profiles. And that profile has said
75 * entrypoint with format.
76 */
77bool is_vaapi_support_formats(VAProfile* profiles, VAEntrypoint entrypoint,
78 unsigned int format) {
79 bool found = false;
80 Display* display;
81 VAStatus va_res;
82 VADisplay va_display;
83 int major_version, minor_version;
84
85 setenv("XAUTHORITY", "/home/chronos/.Xauthority", 0 /* no overwrite */);
86
87 display = XOpenDisplay(":0.0");
88 if (!display) {
89 TRACE("Failed connecting to the X Server\n");
90 return false;
91 }
92
93 va_display = vaGetDisplay(display);
94 if (!vaDisplayIsValid(va_display)) {
95 TRACE("vaGetDisplay returns invalid display\n");
96 return false;
97 }
98
99 va_res = vaInitialize(va_display, &major_version, &minor_version);
100 if (va_res != VA_STATUS_SUCCESS) {
101 TRACE("vaInitialize failed\n");
102 return false;
103 }
104
105 if (match_vaapi_capabilities(va_display, profiles, entrypoint, format))
106 found = true;
107
108 vaTerminate(va_display);
109 XCloseDisplay(display);
110
111 return found;
112}