Home | History | Annotate | Download | only in tuningfork
      1 /*
      2  * Copyright 2019 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #pragma once
     18 
     19 #include "tuningfork.h"
     20 
     21 #ifdef __cplusplus
     22 extern "C" {
     23 #endif
     24 
     25 typedef void (*VoidCallback)();
     26 typedef void (*ProtoCallback)(const CProtobufSerialization*);
     27 
     28 // Load settings from assets/tuningfork/tuningfork_settings.bin.
     29 // Returns true and fills 'settings' if the file could be loaded.
     30 // Ownership of settings is passed to the caller: call
     31 //  CProtobufSerialization_Free to deallocate any memory.
     32 // Returns false if the file was not found or there was an error.
     33 bool TuningFork_findSettingsInAPK(JNIEnv* env, jobject activity,
     34                                   CProtobufSerialization* settings);
     35 
     36 // Load fidelity params from assets/tuningfork/dev_tuningfork_fidelityparams_#.bin.
     37 // Call once with fps=NULL to get the number of files in fp_count.
     38 // The call a second time with a pre-allocated array of size fp_count in fps.
     39 // Ownership of serializations is passed to the caller: call
     40 //  CProtobufSerialization_Free to deallocate any memory.
     41 void TuningFork_findFidelityParamsInAPK(JNIEnv* env, jobject activity,
     42                                         CProtobufSerialization* fps,
     43                                         int* fp_count);
     44 
     45 // Initialize tuning fork and automatically inject tracers into Swappy.
     46 // If Swappy is not available or could not be initialized, the function returns
     47 //  false.
     48 // When using Swappy, there will be 2 default tick points added and the
     49 //  histogram settings need to be coordinated with your swap rate.
     50 // If you know the shared library in which Swappy is, pass it in libraryName.
     51 // If libraryName is NULL or TuningFork cannot find Swappy in the library, the
     52 //  function  will look in the current module and then try in order:
     53 //  [libgamesdk.so, libswappy.so, libunity.so]
     54 // frame_callback is called once per frame: you can set any Annotations
     55 //  during this callback if you wish.
     56 bool TuningFork_initWithSwappy(const CProtobufSerialization* settings,
     57                                JNIEnv* env, jobject activity,
     58                                const char* libraryName,
     59                                VoidCallback frame_callback);
     60 
     61 // Set a callback to be called on a separate thread every time TuningFork
     62 //  performs an upload.
     63 void TuningFork_setUploadCallback(ProtoCallback cbk);
     64 
     65 // This function calls initWithSwappy and also performs the following:
     66 // 1) Settings and default fidelity params are retrieved from the APK.
     67 // 2) A download thread is activated to retrieve fideloty params and retries are
     68 //    performed until a download is successful or a timeout occurs.
     69 // 3) Downloaded params are stored locally and used in preference of default
     70 //    params when the app is started in future.
     71 // fpDefaultFileNum is the index of the dev_tuningfork_fidelityparams_#.bin file you
     72 //  wish to use when there is no download connection and no saved params. It has a
     73 //  special meaning if it is negative: in this case, saved params are reset to
     74 //  dev_tuningfork_fidelity_params_(-$fpDefaultFileNum).bin
     75 // fidelity_params_callback is called with any downloaded params or with default /
     76 //  saved params.
     77 // initialTimeoutMs is the time to wait for an initial download. The fidelity_params_callback
     78 //  will be called after this time with the default / saved params if no params
     79 //  could be downloaded..
     80 // ultimateTimeoutMs is the time after which to stop retrying the download.
     81 // The following error codes may be returned:
     82 enum TFErrorCode {
     83     TFERROR_OK = 0, // No error
     84     TFERROR_NO_SETTINGS = 1, // No tuningfork_settings.bin found in assets/tuningfork.
     85     TFERROR_NO_SWAPPY = 2, // Not able to find Swappy.
     86     TFERROR_INVALID_DEFAULT_FIDELITY_PARAMS = 3, // fpDefaultFileNum is out of range.
     87     TFERROR_NO_FIDELITY_PARAMS = 4 // No dev_tuningfork_fidelityparams_#.bin found
     88                                  //  in assets/tuningfork.
     89 };
     90 TFErrorCode TuningFork_initFromAssetsWithSwappy(JNIEnv* env, jobject activity,
     91                              const char* libraryName,
     92                              VoidCallback frame_callback,
     93                              int fpDefaultFileNum,
     94                              ProtoCallback fidelity_params_callback,
     95                              int initialTimeoutMs, int ultimateTimeoutMs);
     96 
     97 #ifdef __cplusplus
     98 }
     99 #endif
    100