Imported Upstream version 1.4+222+hg5f9f7194267b
[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
b53f7c52
JB
44using namespace ppa;
45
72b9787e 46static FUNC_PPALibRelease *_pfuncPpaRelease;
b53f7c52 47ppa::Base *ppa::ppabase;
72b9787e
JB
48
49static void _ppaReleaseAtExit()
50{
51 _pfuncPpaRelease(ppabase);
52}
53
54#ifdef _WIN32
55#include <windows.h>
56
57#if defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
58# ifdef UNICODE
59# define PPA_DLL_NAME L"ppa64.dll"
60# else
61# define PPA_DLL_NAME "ppa64.dll"
62# endif
63#else
64# ifdef UNICODE
65# define PPA_DLL_NAME L"ppa.dll"
66# else
67# define PPA_DLL_NAME "ppa.dll"
68# endif
69#endif // if defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
70
71void initializePPA(void)
72{
73 if (ppabase)
74 return;
75
76 HMODULE _ppaLibHandle = LoadLibrary(PPA_DLL_NAME);
77 if (!_ppaLibHandle)
78 return;
79
80 FUNC_PPALibInit *_pfuncPpaInit = (FUNC_PPALibInit*)GetProcAddress(_ppaLibHandle, "InitPpaUtil");
81 _pfuncPpaRelease = (FUNC_PPALibRelease*)GetProcAddress(_ppaLibHandle, "DeletePpa");
82
83 if (!_pfuncPpaInit || !_pfuncPpaRelease)
84 {
85 FreeLibrary(_ppaLibHandle);
86 return;
87 }
88
89 ppabase = _pfuncPpaInit(PPACpuAndGroup, PPACpuGroupNums);
90 if (!ppabase)
91 {
92 FreeLibrary(_ppaLibHandle);
93 return;
94 }
95
96 atexit(_ppaReleaseAtExit);
97}
98
99#else /* linux & unix & cygwin */
100#include <dlfcn.h>
101#include <stdio.h>
102
103#if defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
104# define PPA_LIB_NAME "libppa64.so"
105#else
106# define PPA_LIB_NAME "libppa.so"
107#endif
108
109void initializePPA(void)
110{
111 if (ppabase)
112 {
113 printf("PPA: already initialized\n");
114 return;
115 }
116
117 void *_ppaDllHandle = dlopen(PPA_LIB_NAME, RTLD_LAZY);
118 if (!_ppaDllHandle)
119 {
120 printf("PPA: Unable to load %s\n", PPA_LIB_NAME);
121 return;
122 }
123
124 FUNC_PPALibInit *_pfuncPpaInit = (FUNC_PPALibInit*)dlsym(_ppaDllHandle, "InitPpaUtil");
125 _pfuncPpaRelease = (FUNC_PPALibRelease*)dlsym(_ppaDllHandle, "DeletePpa");
126
127 if (!_pfuncPpaInit || !_pfuncPpaRelease)
128 {
129 printf("PPA: Function bindings failed\n");
130 dlclose(_ppaDllHandle);
131 return;
132 }
133
134 ppabase = _pfuncPpaInit(PPACpuAndGroup, PPACpuGroupNums);
135 if (!ppabase)
136 {
137 printf("PPA: Init failed\n");
138 dlclose(_ppaDllHandle);
139 return;
140 }
141
142 atexit(_ppaReleaseAtExit);
143}
144
145#endif /* !_WIN32 */
146
147#endif /* defined(ENABLE_PPA) */