1/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 *
7 */
8
9#include "VisualSKPBench.h"
10
11#if SK_SUPPORT_GPU
12#include "GrContext.h"
13#endif
14
15VisualSKPBench::VisualSKPBench(const char* name, const SkPicture* pic)
16    : fPic(SkRef(pic))
17    , fCullRect(fPic->cullRect().roundOut())
18    , fName(name) {
19    fUniqueName.printf("%s", name);
20}
21
22const char* VisualSKPBench::onGetName() {
23    return fName.c_str();
24}
25
26const char* VisualSKPBench::onGetUniqueName() {
27    return fUniqueName.c_str();
28}
29
30bool VisualSKPBench::isSuitableFor(Backend backend) {
31    return backend != kNonRendering_Backend;
32}
33
34SkIPoint VisualSKPBench::onGetSize() {
35    return SkIPoint::Make(fCullRect.width(), fCullRect.height());
36}
37
38void VisualSKPBench::onDraw(int loops, SkCanvas* canvas) {
39    bool isOffset = SkToBool(fCullRect.left() | fCullRect.top());
40    if (isOffset) {
41        canvas->save();
42        canvas->translate(SkIntToScalar(-fCullRect.left()), SkIntToScalar(-fCullRect.top()));
43    }
44
45    for (int i = 0; i < loops; i++) {
46        canvas->drawPicture(fPic);
47#if SK_SUPPORT_GPU
48        // Ensure the GrContext doesn't batch across draw loops.
49        if (GrContext* context = canvas->getGrContext()) {
50            context->flush();
51        }
52#endif
53    }
54
55    if (isOffset) {
56        canvas->restore();
57    }
58}
59