Debian Codegraph @

doc/spec/source/snippets/prg_hw.cpp

raw
1 /*############################################################################
2 # Copyright (C) 2020 Intel Corporation
3 #
4 # SPDX-License-Identifier: MIT
5 ############################################################################*/
6
7 #include <stdlib.h>
8 #include <stdio.h>
9
10 #include <vector>
11 #include <algorithm>
12
13 #include "mfxdefs.h"
14 #include "mfxvideo.h"
15 #include "mfxadapter.h"
16 #include "mfxdispatcher.h"
17
18 /* These macro required for code compilation. */
19
20 #define MSDK_CHECK_STATUS(X, MSG) {if ((X) < MFX_ERR_NONE) {printf("%s\n",MSG);}}
21
22 /* end of internal stuff */
23
24
25 static void* vaDisplay;
26
27
28 static int prg_hw1 () {
29 /*beg1*/
30 mfxU32 num_adapters_available;
31 mfxIMPL impl;
32
33 // Query number of graphics adapters available on system
34 mfxStatus sts = MFXQueryAdaptersNumber(&num_adapters_available);
35 MSDK_CHECK_STATUS(sts, "MFXQueryAdaptersNumber failed");
36
37 // Allocate memory for response
38 std::vector<mfxAdapterInfo> displays_data(num_adapters_available);
39 mfxAdaptersInfo adapters = { displays_data.data(), mfxU32(displays_data.size()), 0u, {0} };
40
41 // Query information about all adapters (mind that first parameter is NULL)
42 sts = MFXQueryAdapters(nullptr, &adapters);
43 MSDK_CHECK_STATUS(sts, "MFXQueryAdapters failed");
44
45 // Find dGfx adapter in list of adapters
46 auto idx_d = std::find_if(adapters.Adapters, adapters.Adapters + adapters.NumActual,
47 [](const mfxAdapterInfo info)
48 {
49 return info.Platform.MediaAdapterType == mfxMediaAdapterType::MFX_MEDIA_DISCRETE;
50 });
51
52 // No dGfx in list
53 if (idx_d == adapters.Adapters + adapters.NumActual)
54 {
55 printf("Warning: No dGfx detected on machine\n");
56 return -1;
57 }
58
59 mfxU32 idx = static_cast<mfxU32>(std::distance(adapters.Adapters, idx_d));
60
61 // Choose correct implementation for discrete adapter
62 switch (adapters.Adapters[idx].Number)
63 {
64 case 0:
65 impl = MFX_IMPL_HARDWARE;
66 break;
67 case 1:
68 impl = MFX_IMPL_HARDWARE2;
69 break;
70 case 2:
71 impl = MFX_IMPL_HARDWARE3;
72 break;
73 case 3:
74 impl = MFX_IMPL_HARDWARE4;
75 break;
76
77 default:
78 // Try searching on all display adapters
79 impl = MFX_IMPL_HARDWARE_ANY;
80 break;
81 }
82 printf("Choosen implementation: %d\n", impl);
83 // Initialize mfxSession in regular way with obtained implementation.
84 /*end1*/
85 return 0;
86 }
87
88 static int prg_hw2 () {
89 /*beg2*/
90 mfxU32 num_adapters_available;
91 mfxIMPL impl;
92 mfxVideoParam Encode_mfxVideoParam;
93
94 // Query number of graphics adapters available on system
95 mfxStatus sts = MFXQueryAdaptersNumber(&num_adapters_available);
96 MSDK_CHECK_STATUS(sts, "MFXQueryAdaptersNumber failed");
97
98 // Allocate memory for response
99 std::vector<mfxAdapterInfo> displays_data(num_adapters_available);
100 mfxAdaptersInfo adapters = { displays_data.data(), mfxU32(displays_data.size()), 0u, {0} };
101
102 // Fill description of Encode workload
103 mfxComponentInfo interface_request = { MFX_COMPONENT_ENCODE, Encode_mfxVideoParam, {0} };
104
105 // Query information about suitable adapters for Encode workload described by Encode_mfxVideoParam
106 sts = MFXQueryAdapters(&interface_request, &adapters);
107
108 if (sts == MFX_ERR_NOT_FOUND)
109 {
110 printf("Error: No adapters on machine capable to process desired workload\n");
111 return -1;
112 }
113
114 MSDK_CHECK_STATUS(sts, "MFXQueryAdapters failed");
115
116 // Choose correct implementation for discrete adapter. Mind usage of index 0, this is best suitable adapter from MSDK perspective
117 switch (adapters.Adapters[0].Number)
118 {
119 case 0:
120 impl = MFX_IMPL_HARDWARE;
121 break;
122 case 1:
123 impl = MFX_IMPL_HARDWARE2;
124 break;
125 case 2:
126 impl = MFX_IMPL_HARDWARE3;
127 break;
128 case 3:
129 impl = MFX_IMPL_HARDWARE4;
130 break;
131
132 default:
133 // Try searching on all display adapters
134 impl = MFX_IMPL_HARDWARE_ANY;
135 break;
136 }
137
138 printf("Choosen implementation: %d\n", impl);
139
140 // Initialize mfxSession in regular way with obtained implementation
141 /*end2*/
142 return 0;
143 }
144
145 static int prg_hw3 () {
146 /*beg3*/
147 mfxLoader loader = MFXLoad();
148 mfxConfig config1 = MFXCreateConfig(loader);
149 mfxConfig config2 = MFXCreateConfig(loader);
150 mfxSession session;
151
152 mfxVariant HandleType;
153 HandleType.Type = MFX_VARIANT_TYPE_U32;
154 HandleType.Data.U32 = MFX_HANDLE_VA_DISPLAY;
155 MFXSetConfigFilterProperty(config1, (mfxU8*)"mfxHandleType", HandleType);
156
157 mfxVariant DisplayHandle;
158 DisplayHandle.Type = MFX_VARIANT_TYPE_PTR;
159 HandleType.Data.Ptr = vaDisplay;
160 MFXSetConfigFilterProperty(config2, (mfxU8*)"mfxHDL", DisplayHandle);
161
162 MFXCreateSession(loader, 0, &session);
163
164 /*end3*/
165 return 0;
166 }
167