Exclude Pixel-specific code from compilation when not on Android.

PiperOrigin-RevId: 313288675
Change-Id: I5883edf3fc9f90eb6d7bdeac02bb32bd2949ff7b
This commit is contained in:
Robert David 2020-05-26 16:23:56 -07:00 committed by TensorFlower Gardener
parent 6a0bdbdb7c
commit 3f423f882b

View File

@ -26,21 +26,27 @@ namespace tflite {
namespace gpu { namespace gpu {
namespace cl { namespace cl {
#ifdef __ANDROID__
#define LoadFunction(function) \ #define LoadFunction(function) \
if (is_pixel) { \ if (is_pixel) { \
function = reinterpret_cast<PFN_##function>(loadOpenCLPointer(#function)); \ function = reinterpret_cast<PFN_##function>(loadOpenCLPointer(#function)); \
} else { \ } else { \
function = reinterpret_cast<PFN_##function>(dlsym(libopencl, #function)); \ function = reinterpret_cast<PFN_##function>(dlsym(libopencl, #function)); \
} }
#else
#define LoadFunction(function) \
function = reinterpret_cast<PFN_##function>(dlsym(libopencl, #function));
#endif
absl::Status LoadOpenCL() { absl::Status LoadOpenCL() {
void* libopencl = dlopen("libOpenCL.so", RTLD_NOW | RTLD_LOCAL); void* libopencl = dlopen("libOpenCL.so", RTLD_NOW | RTLD_LOCAL);
if (libopencl) { if (libopencl) {
LoadOpenCLFunctions(libopencl, false); LoadOpenCLFunctions(libopencl, false);
return absl::OkStatus(); return absl::OkStatus();
} else { }
// record error // record error
std::string error(dlerror()); std::string error(dlerror());
#ifdef __ANDROID__
// Pixel phone? // Pixel phone?
libopencl = dlopen("libOpenCL-pixel.so", RTLD_NOW | RTLD_LOCAL); libopencl = dlopen("libOpenCL-pixel.so", RTLD_NOW | RTLD_LOCAL);
if (libopencl) { if (libopencl) {
@ -50,20 +56,21 @@ absl::Status LoadOpenCL() {
enableOpenCL(); enableOpenCL();
LoadOpenCLFunctions(libopencl, true); LoadOpenCLFunctions(libopencl, true);
return absl::OkStatus(); return absl::OkStatus();
} else { }
#endif
return absl::UnknownError( return absl::UnknownError(
absl::StrCat("Can not open OpenCL library on this device - ", error)); absl::StrCat("Can not open OpenCL library on this device - ", error));
} }
}
}
void LoadOpenCLFunctions(void* libopencl, bool is_pixel) { void LoadOpenCLFunctions(void* libopencl, bool is_pixel) {
#ifdef __ANDROID__
typedef void* (*loadOpenCLPointer_t)(const char* name); typedef void* (*loadOpenCLPointer_t)(const char* name);
loadOpenCLPointer_t loadOpenCLPointer; loadOpenCLPointer_t loadOpenCLPointer;
if (is_pixel) { if (is_pixel) {
loadOpenCLPointer = reinterpret_cast<loadOpenCLPointer_t>( loadOpenCLPointer = reinterpret_cast<loadOpenCLPointer_t>(
dlsym(libopencl, "loadOpenCLPointer")); dlsym(libopencl, "loadOpenCLPointer"));
} }
#endif
LoadFunction(clGetPlatformIDs); LoadFunction(clGetPlatformIDs);
LoadFunction(clGetPlatformInfo); LoadFunction(clGetPlatformInfo);