aboutsummaryrefslogtreecommitdiff
path: root/samples/tuningfork/tftestapp/app/src/main/cpp/tftestapp.cpp
blob: e160f1209155de2dbad28cdc3ea98317c452f762 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "tuningfork/protobuf_util.h"
#include "tuningfork/tuningfork_extra.h"
#include "swappy/swappy.h"
#include "full/tuningfork.pb.h"
#include "full/tuningfork_clearcut_log.pb.h"
#include "full/dev_tuningfork.pb.h"
#include <sstream>
#include <jni.h>
#include <android/native_window_jni.h>

#define LOG_TAG "tftestapp"
#include "Log.h"
#include "Renderer.h"

using ::com::google::tuningfork::FidelityParams;
using ::com::google::tuningfork::Settings;
using ::com::google::tuningfork::Annotation;
using ::logs::proto::tuningfork::TuningForkLogEvent;
using ::logs::proto::tuningfork::TuningForkHistogram;

namespace proto_tf = com::google::tuningfork;
namespace tf = tuningfork;
using namespace samples;

namespace {

constexpr TFInstrumentKey TFTICK_CHOREOGRAPHER = 4;

struct HistogramSettings {
    float start, end;
    int nBuckets;
};
Settings TestSettings(Settings::AggregationStrategy::Submission method, int n_ticks, int n_keys,
                      std::vector<int> annotation_size,
                      const std::vector<HistogramSettings>& hists = {}) {
    // Make sure we set all required fields
    Settings s;
    s.mutable_aggregation_strategy()->set_method(method);
    s.mutable_aggregation_strategy()->set_intervalms_or_count(n_ticks);
    s.mutable_aggregation_strategy()->set_max_instrumentation_keys(n_keys);
    for(int i=0;i<annotation_size.size();++i)
        s.mutable_aggregation_strategy()->add_annotation_enum_size(annotation_size[i]);
    int i=0;
    for(auto& h: hists) {
        auto sh = s.add_histograms();
        sh->set_bucket_min(h.start);
        sh->set_bucket_max(h.end);
        sh->set_n_buckets(h.nBuckets);
        sh->set_instrument_key(i++);
    }
    return s;
}

std::string ReplaceReturns(const std::string& s) {
    std::string r = s;
    for (int i=0; i<r.length(); ++i) {
        if (r[i]=='\n') r[i] = ',';
        if (r[i]=='\r') r[i] = ' ';
    }
    return r;
}

std::string PrettyPrintTuningForkLogEvent(const TuningForkLogEvent& evt) {
    std::stringstream eventStr;
    eventStr << "TuningForkLogEvent {\n";
    if (evt.has_fidelityparams()) {
        FidelityParams p;
        p.ParseFromArray(evt.fidelityparams().c_str(), evt.fidelityparams().length());
        eventStr << "  fidelityparams : " << ReplaceReturns(p.DebugString()) << "\n";
    }
    for (int i=0; i<evt.histograms_size(); ++i) {
        auto &h = evt.histograms(i);
        Annotation ann;
        ann.ParseFromArray(h.annotation().c_str(), h.annotation().length());
        bool first = true;
        eventStr << "  histogram {\n";
        eventStr << "    instrument_id : " << h.instrument_id() << "\n";
        eventStr << "    annotation : " << ReplaceReturns(ann.DebugString()) << "\n    counts : ";
        eventStr << "[";
        for (int j=0; j<h.counts_size(); ++j) {
            if (first) {
                first = false;
            } else {
                eventStr << ",";
            }
            eventStr << h.counts(j);
        }
        eventStr << "]\n  }\n";
    }
    if (evt.has_experiment_id()) {
        eventStr << "  experiment_id : " << evt.experiment_id() << "\n";
    }
    if (evt.has_session_id()) {
        eventStr << "  session_id : " << evt.session_id() << "\n";
    }
    if (evt.has_device_info()) {
        eventStr << "  device_info : {" << ReplaceReturns(evt.device_info().DebugString()) << "}\n";
    }
    if (evt.has_apk_package_name()) {
        eventStr << "  apk_package_name : " << evt.apk_package_name() << "\n";
    }
    if (evt.has_apk_version_code()) {
        eventStr << "  apk_version_code : " << evt.apk_version_code() << "\n";
    }
    eventStr << "}";
    return eventStr.str();
}
void SplitAndLog(const std::string& s) {
    std::istringstream str(s);
    std::string line;
    std::stringstream to_log;
    const int nlines_per_log = 16;
    int l = nlines_per_log;
    while (std::getline(str, line, '\n')) {
        to_log << line << '\n';
        if(--l<=0) {
            ALOGI("%s", to_log.str().c_str());
            l = nlines_per_log;
            to_log.str("");
        }
    }
    if (to_log.str().length()>0)
        ALOGI("%s", to_log.str().c_str());
}

void UploadCallback(const CProtobufSerialization *tuningfork_log_event) {
    if(tuningfork_log_event) {
        TuningForkLogEvent evt;
        evt.ParseFromArray(tuningfork_log_event->bytes, tuningfork_log_event->size);
        auto pp = PrettyPrintTuningForkLogEvent(evt);
        SplitAndLog(pp);
    }
}

static int sLevel = proto_tf::LEVEL_1;
extern "C"
void SetAnnotations() {
    if(proto_tf::Level_IsValid(sLevel)) {
        Annotation a;
        a.set_level((proto_tf::Level)sLevel);
        auto next_level = sLevel + 1;
        a.set_next_level((proto_tf::Level)(next_level>proto_tf::Level_MAX?1:next_level));
        auto ser = tf::CProtobufSerialization_Alloc(a);
        TuningFork_setCurrentAnnotation(&ser);
        tf::CProtobufSerialization_Free(&ser);
    }
}

void SetFidelityParams(const CProtobufSerialization* params) {
    FidelityParams p;
    // Set default values
    p.set_num_spheres(20);
    p.set_tesselation_percent(50);
    std::vector<uint8_t> params_ser(params->bytes, params->bytes + params->size);
    tf::Deserialize(params_ser, p);
    std::string s = p.DebugString();
    ALOGI("Using FidelityParams: %s", ReplaceReturns(s).c_str());
    int nSpheres = p.num_spheres();
    int tesselation = p.tesselation_percent();
    Renderer::getInstance()->setQuality(nSpheres, tesselation);
}

} // anonymous namespace

extern "C" {

JNIEXPORT void JNICALL
Java_com_google_tuningfork_TFTestActivity_initTuningFork(JNIEnv *env, jobject activity) {
    Swappy_init(env, activity);
    if (Swappy_isEnabled()) {
        int defaultFPIndex = 3; // i.e. dev_tuningfork_fidelityparams_3.bin
        int initialTimeoutMs = 1000;
        int ultimateTimeoutMs = 100000;
        TFErrorCode c = TuningFork_initFromAssetsWithSwappy(env, activity, "libnative-lib.so",
                                                    SetAnnotations, defaultFPIndex,
                                                    SetFidelityParams,
                                                    initialTimeoutMs, ultimateTimeoutMs);
        if (c==TFERROR_OK) {
            TuningFork_setUploadCallback(UploadCallback);
            SetAnnotations();
        } else {
            ALOGW("Error initializing TuningFork: %d", c);
        }
    } else {
        ALOGW("Couldn't enable Swappy. Tuning Fork is not enabled either");
    }
}

JNIEXPORT void JNICALL
Java_com_google_tuningfork_TFTestActivity_onChoreographer(JNIEnv */*env*/, jclass clz, jlong /*frameTimeNanos*/) {
    TuningFork_frameTick(TFTICK_CHOREOGRAPHER);
    // After 600 ticks, switch to the next level
    static int tick_count = 0;
    ++tick_count;
    if(tick_count>=600) {
        ++sLevel;
        if(sLevel>proto_tf::Level_MAX) sLevel = proto_tf::LEVEL_1;
        SetAnnotations();
        tick_count = 0;
    }
}
JNIEXPORT void JNICALL
Java_com_google_tuningfork_TFTestActivity_resize(JNIEnv *env, jclass /*clz*/, jobject surface, jint width, jint height) {
    ANativeWindow *window = ANativeWindow_fromSurface(env, surface);
    Renderer::getInstance()->setWindow(window,
                                       static_cast<int32_t>(width),
                                       static_cast<int32_t>(height));
}
JNIEXPORT void JNICALL
Java_com_google_tuningfork_TFTestActivity_clearSurface(JNIEnv */*env*/, jclass /*clz*/ ) {
    Renderer::getInstance()->setWindow(nullptr, 0, 0);
}
JNIEXPORT void JNICALL
Java_com_google_tuningfork_TFTestActivity_start(JNIEnv */*env*/, jclass /*clz*/ ) {
    Renderer::getInstance()->start();
}
JNIEXPORT void JNICALL
Java_com_google_tuningfork_TFTestActivity_stop(JNIEnv */*env*/, jclass /*clz*/ ) {
    Renderer::getInstance()->stop();
}

}