1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2020 Google LLC
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "bench/Benchmark.h"
9cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h"
10cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h"
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ciclass CreateBackendTextureBench : public Benchmark {
13cb93a386Sopenharmony_ciprivate:
14cb93a386Sopenharmony_ci    SkString fName;
15cb93a386Sopenharmony_ci    SkTArray<GrBackendTexture> fBackendTextures;
16cb93a386Sopenharmony_ci    GrMipmapped fMipmapped;
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_cipublic:
19cb93a386Sopenharmony_ci    CreateBackendTextureBench(GrMipmapped mipMapped) : fMipmapped(mipMapped) {
20cb93a386Sopenharmony_ci        fName.printf("create_backend_texture%s", mipMapped == GrMipmapped::kYes ? "_mipped" : "");
21cb93a386Sopenharmony_ci    }
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_ciprivate:
24cb93a386Sopenharmony_ci    bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; }
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_ci    const char* onGetName() override { return fName.c_str(); }
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_ci    void onDraw(int loops, SkCanvas* canvas) override {
29cb93a386Sopenharmony_ci        auto context = canvas->recordingContext()->asDirectContext();
30cb93a386Sopenharmony_ci
31cb93a386Sopenharmony_ci        fBackendTextures.reserve_back(loops);
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ci        static const int kSize = 16;
34cb93a386Sopenharmony_ci        for (int i = 0; i < loops; ++i) {
35cb93a386Sopenharmony_ci            fBackendTextures.push_back(context->createBackendTexture(
36cb93a386Sopenharmony_ci                    kSize, kSize, kRGBA_8888_SkColorType, SkColors::kRed, fMipmapped,
37cb93a386Sopenharmony_ci                    GrRenderable::kNo, GrProtected::kNo));
38cb93a386Sopenharmony_ci        }
39cb93a386Sopenharmony_ci    }
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ci    void onPerCanvasPostDraw(SkCanvas* canvas) override {
42cb93a386Sopenharmony_ci        auto context = canvas->recordingContext()->asDirectContext();
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ci        context->flush();
45cb93a386Sopenharmony_ci        context->submit(true);
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_ci        for (int i = 0; i < fBackendTextures.count(); ++i) {
48cb93a386Sopenharmony_ci            if (fBackendTextures[i].isValid()) {
49cb93a386Sopenharmony_ci                context->deleteBackendTexture(fBackendTextures[i]);
50cb93a386Sopenharmony_ci            }
51cb93a386Sopenharmony_ci        }
52cb93a386Sopenharmony_ci        fBackendTextures.reset();
53cb93a386Sopenharmony_ci    }
54cb93a386Sopenharmony_ci};
55cb93a386Sopenharmony_ci
56cb93a386Sopenharmony_ciDEF_BENCH(return new CreateBackendTextureBench(GrMipmapped::kNo);)
57cb93a386Sopenharmony_ciDEF_BENCH(return new CreateBackendTextureBench(GrMipmapped::kYes);)
58