1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci* Copyright 2014 Google Inc.
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 "include/core/SkSize.h"
9cb93a386Sopenharmony_ci#include "include/private/SkTDArray.h"
10cb93a386Sopenharmony_ci#include "include/utils/SkRandom.h"
11cb93a386Sopenharmony_ci#include "src/gpu/GrRectanizerPow2.h"
12cb93a386Sopenharmony_ci#include "src/gpu/GrRectanizerSkyline.h"
13cb93a386Sopenharmony_ci#include "tests/Test.h"
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_cistatic const int kWidth = 1024;
16cb93a386Sopenharmony_cistatic const int kHeight = 1024;
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ci// Basic test of a GrRectanizer-derived class' functionality
19cb93a386Sopenharmony_cistatic void test_rectanizer_basic(skiatest::Reporter* reporter, GrRectanizer* rectanizer) {
20cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, kWidth == rectanizer->width());
21cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, kHeight == rectanizer->height());
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_ci    SkIPoint16 loc;
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, rectanizer->addRect(50, 50, &loc));
26cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, rectanizer->percentFull() > 0.0f);
27cb93a386Sopenharmony_ci    rectanizer->reset();
28cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, rectanizer->percentFull() == 0.0f);
29cb93a386Sopenharmony_ci}
30cb93a386Sopenharmony_ci
31cb93a386Sopenharmony_cistatic void test_rectanizer_inserts(skiatest::Reporter*,
32cb93a386Sopenharmony_ci                                    GrRectanizer* rectanizer,
33cb93a386Sopenharmony_ci                                    const SkTDArray<SkISize>& rects) {
34cb93a386Sopenharmony_ci    int i;
35cb93a386Sopenharmony_ci    for (i = 0; i < rects.count(); ++i) {
36cb93a386Sopenharmony_ci        SkIPoint16 loc;
37cb93a386Sopenharmony_ci        if (!rectanizer->addRect(rects[i].fWidth, rects[i].fHeight, &loc)) {
38cb93a386Sopenharmony_ci            break;
39cb93a386Sopenharmony_ci        }
40cb93a386Sopenharmony_ci    }
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci    //SkDebugf("\n***%d %f\n", i, rectanizer->percentFull());
43cb93a386Sopenharmony_ci}
44cb93a386Sopenharmony_ci
45cb93a386Sopenharmony_cistatic void test_skyline(skiatest::Reporter* reporter, const SkTDArray<SkISize>& rects) {
46cb93a386Sopenharmony_ci    GrRectanizerSkyline skylineRectanizer(kWidth, kHeight);
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci    test_rectanizer_basic(reporter, &skylineRectanizer);
49cb93a386Sopenharmony_ci    test_rectanizer_inserts(reporter, &skylineRectanizer, rects);
50cb93a386Sopenharmony_ci}
51cb93a386Sopenharmony_ci
52cb93a386Sopenharmony_cistatic void test_pow2(skiatest::Reporter* reporter, const SkTDArray<SkISize>& rects) {
53cb93a386Sopenharmony_ci    GrRectanizerPow2 pow2Rectanizer(kWidth, kHeight);
54cb93a386Sopenharmony_ci
55cb93a386Sopenharmony_ci    test_rectanizer_basic(reporter, &pow2Rectanizer);
56cb93a386Sopenharmony_ci    test_rectanizer_inserts(reporter, &pow2Rectanizer, rects);
57cb93a386Sopenharmony_ci}
58cb93a386Sopenharmony_ci
59cb93a386Sopenharmony_ciDEF_GPUTEST(GpuRectanizer, reporter, factory) {
60cb93a386Sopenharmony_ci    SkTDArray<SkISize> rects;
61cb93a386Sopenharmony_ci    SkRandom rand;
62cb93a386Sopenharmony_ci
63cb93a386Sopenharmony_ci    for (int i = 0; i < 50; i++) {
64cb93a386Sopenharmony_ci        rects.push_back(SkISize::Make(rand.nextRangeU(1, kWidth / 2),
65cb93a386Sopenharmony_ci                                      rand.nextRangeU(1, kHeight / 2)));
66cb93a386Sopenharmony_ci    }
67cb93a386Sopenharmony_ci
68cb93a386Sopenharmony_ci    test_skyline(reporter, rects);
69cb93a386Sopenharmony_ci    test_pow2(reporter, rects);
70cb93a386Sopenharmony_ci}
71