Update changelog.
[deb_x265.git] / source / PPA / ppa.cpp
CommitLineData
72b9787e
JB
1/*****************************************************************************
2 * Copyright (C) 2013 x265 project
3 *
4 * Authors: Steve Borho <steve@borho.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
19 *
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
23
24#if defined(ENABLE_PPA)
25
26#include "ppa.h"
27#include <stdlib.h>
28
29#define PPA_REGISTER_CPU_EVENT2GROUP(x, y) # x, # y,
30#define PPA_REGISTER_CPU_EVENT(x) PPA_REGISTER_CPU_EVENT2GROUP(x, NoGroup)
31const char *PPACpuAndGroup[] =
32{
33#include "ppaCPUEvents.h"
34 ""
35};
36#undef PPA_REGISTER_CPU_EVENT
37#undef PPA_REGISTER_CPU_EVENT2GROUP
38
39extern "C" {
40typedef ppa::Base *(FUNC_PPALibInit)(const char **, int);
41typedef void (FUNC_PPALibRelease)(ppa::Base* &);
42}
43
44static FUNC_PPALibRelease *_pfuncPpaRelease;
45ppa::Base *ppabase;
46
47static void _ppaReleaseAtExit()
48{
49 _pfuncPpaRelease(ppabase);
50}
51
52#ifdef _WIN32
53#include <windows.h>
54
55#if defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
56# ifdef UNICODE
57# define PPA_DLL_NAME L"ppa64.dll"
58# else
59# define PPA_DLL_NAME "ppa64.dll"
60# endif
61#else
62# ifdef UNICODE
63# define PPA_DLL_NAME L"ppa.dll"
64# else
65# define PPA_DLL_NAME "ppa.dll"
66# endif
67#endif // if defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
68
69void initializePPA(void)
70{
71 if (ppabase)
72 return;
73
74 HMODULE _ppaLibHandle = LoadLibrary(PPA_DLL_NAME);
75 if (!_ppaLibHandle)
76 return;
77
78 FUNC_PPALibInit *_pfuncPpaInit = (FUNC_PPALibInit*)GetProcAddress(_ppaLibHandle, "InitPpaUtil");
79 _pfuncPpaRelease = (FUNC_PPALibRelease*)GetProcAddress(_ppaLibHandle, "DeletePpa");
80
81 if (!_pfuncPpaInit || !_pfuncPpaRelease)
82 {
83 FreeLibrary(_ppaLibHandle);
84 return;
85 }
86
87 ppabase = _pfuncPpaInit(PPACpuAndGroup, PPACpuGroupNums);
88 if (!ppabase)
89 {
90 FreeLibrary(_ppaLibHandle);
91 return;
92 }
93
94 atexit(_ppaReleaseAtExit);
95}
96
97#else /* linux & unix & cygwin */
98#include <dlfcn.h>
99#include <stdio.h>
100
101#if defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
102# define PPA_LIB_NAME "libppa64.so"
103#else
104# define PPA_LIB_NAME "libppa.so"
105#endif
106
107void initializePPA(void)
108{
109 if (ppabase)
110 {
111 printf("PPA: already initialized\n");
112 return;
113 }
114
115 void *_ppaDllHandle = dlopen(PPA_LIB_NAME, RTLD_LAZY);
116 if (!_ppaDllHandle)
117 {
118 printf("PPA: Unable to load %s\n", PPA_LIB_NAME);
119 return;
120 }
121
122 FUNC_PPALibInit *_pfuncPpaInit = (FUNC_PPALibInit*)dlsym(_ppaDllHandle, "InitPpaUtil");
123 _pfuncPpaRelease = (FUNC_PPALibRelease*)dlsym(_ppaDllHandle, "DeletePpa");
124
125 if (!_pfuncPpaInit || !_pfuncPpaRelease)
126 {
127 printf("PPA: Function bindings failed\n");
128 dlclose(_ppaDllHandle);
129 return;
130 }
131
132 ppabase = _pfuncPpaInit(PPACpuAndGroup, PPACpuGroupNums);
133 if (!ppabase)
134 {
135 printf("PPA: Init failed\n");
136 dlclose(_ppaDllHandle);
137 return;
138 }
139
140 atexit(_ppaReleaseAtExit);
141}
142
143#endif /* !_WIN32 */
144
145#endif /* defined(ENABLE_PPA) */