File: android_surface_control_compat_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (252 lines) | stat: -rw-r--r-- 8,666 bytes parent folder | download | duplicates (4)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/android/android_surface_control_compat.h"

#include <android/data_space.h>

#include "base/android/build_info.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "skia/ext/skcolorspace_trfn.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/color_space.h"

namespace gfx {
namespace {

class SurfaceControlTransactionTest : public testing::Test {
 public:
  SurfaceControlTransactionTest() {
    gfx::SurfaceControl::SetStubImplementationForTesting();
  }

 protected:
  struct CallbackContext {
    CallbackContext(bool* called, bool* destroyed)
        : called(called), destroyed(destroyed) {}
    ~CallbackContext() { *destroyed = true; }
    raw_ptr<bool> called;
    raw_ptr<bool> destroyed;
  };

  SurfaceControl::Transaction::OnCompleteCb CreateOnCompleteCb(
      bool* called,
      bool* destroyed) {
    return base::BindOnce(
        [](std::unique_ptr<CallbackContext> context,
           SurfaceControl::TransactionStats stats) {
          DCHECK(!*context->called);
          *context->called = true;
        },
        std::make_unique<CallbackContext>(called, destroyed));
  }

  SurfaceControl::Transaction::OnCommitCb CreateOnCommitCb(bool* called,
                                                           bool* destroyed) {
    return base::BindOnce(
        [](std::unique_ptr<CallbackContext> context) {
          DCHECK(!*context->called);
          *context->called = true;
        },
        std::make_unique<CallbackContext>(called, destroyed));
  }

  void RunRemainingTasks() {
    base::RunLoop runloop;
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, runloop.QuitClosure());
    runloop.Run();
  }

  base::test::SingleThreadTaskEnvironment task_environment;
};

TEST_F(SurfaceControlTransactionTest, CallbackCalledAfterApply) {
  bool on_complete_called = false;
  bool on_commit_called = false;
  bool on_commit_destroyed = false;
  bool on_complete_destroyed = false;

  gfx::SurfaceControl::Transaction transaction;
  transaction.SetOnCompleteCb(
      CreateOnCompleteCb(&on_complete_called, &on_complete_destroyed),
      base::SingleThreadTaskRunner::GetCurrentDefault());
  transaction.SetOnCommitCb(
      CreateOnCommitCb(&on_commit_called, &on_commit_destroyed),
      base::SingleThreadTaskRunner::GetCurrentDefault());

  // Nothing should have been called yet.
  EXPECT_FALSE(on_complete_called);
  EXPECT_FALSE(on_commit_called);

  transaction.Apply();
  RunRemainingTasks();

  // After apply callbacks should be called.
  EXPECT_TRUE(on_complete_called);
  EXPECT_TRUE(on_commit_called);

  // As this is Once callback naturally it's context should have been destroyed.
  EXPECT_TRUE(on_complete_destroyed);
  EXPECT_TRUE(on_commit_destroyed);
}

TEST_F(SurfaceControlTransactionTest, CallbackDestroyedWithoutApply) {
  bool on_complete_called = false;
  bool on_commit_called = false;
  bool on_commit_destroyed = false;
  bool on_complete_destroyed = false;

  {
    SurfaceControl::Transaction transaction;
    transaction.SetOnCompleteCb(
        CreateOnCompleteCb(&on_complete_called, &on_complete_destroyed),
        base::SingleThreadTaskRunner::GetCurrentDefault());
    transaction.SetOnCommitCb(
        CreateOnCommitCb(&on_commit_called, &on_commit_destroyed),
        base::SingleThreadTaskRunner::GetCurrentDefault());

    // Nothing should have been called yet.
    EXPECT_FALSE(on_complete_called);
    EXPECT_FALSE(on_commit_called);
  }

  RunRemainingTasks();

  // Apply wasn't called, but transaction left the scope, so the callback
  // contexts should have been destroyed.
  EXPECT_TRUE(on_complete_destroyed);
  EXPECT_TRUE(on_commit_destroyed);
}

TEST_F(SurfaceControlTransactionTest, CallbackSetupAfterGetTransaction) {
  bool on_complete_called = false;
  bool on_commit_called = false;
  bool on_commit_destroyed = false;
  bool on_complete_destroyed = false;

  gfx::SurfaceControl::Transaction transaction;
  transaction.SetOnCompleteCb(
      CreateOnCompleteCb(&on_complete_called, &on_complete_destroyed),
      base::SingleThreadTaskRunner::GetCurrentDefault());
  transaction.SetOnCommitCb(
      CreateOnCommitCb(&on_commit_called, &on_commit_destroyed),
      base::SingleThreadTaskRunner::GetCurrentDefault());

  // Nothing should have been called yet.
  EXPECT_FALSE(on_complete_called);
  EXPECT_FALSE(on_commit_called);

  auto* asurfacetransaction = transaction.GetTransaction();

  // Should be no task to run, but calling this to make sure nothing is
  // scheduled that can call callbacks.
  RunRemainingTasks();

  // And not yet.
  EXPECT_FALSE(on_complete_called);
  EXPECT_FALSE(on_commit_called);

  // This is usually called by framework.
  SurfaceControl::ApplyTransaction(asurfacetransaction);
  RunRemainingTasks();

  // After apply callbacks should be called.
  EXPECT_TRUE(on_complete_called);
  EXPECT_TRUE(on_commit_called);

  // As this is Once callback naturally it's context should have been destroyed.
  EXPECT_TRUE(on_complete_destroyed);
  EXPECT_TRUE(on_commit_destroyed);
}

TEST(SurfaceControl, ColorSpaceToADataSpace) {
  // Invalid color spaces are mapped to sRGB.
  {
    ADataSpace dataspace = ADATASPACE_UNKNOWN;
    float extended_range_brightness_ratio = 0.f;
    EXPECT_TRUE(SurfaceControl::ColorSpaceToADataSpace(
        gfx::ColorSpace(), 1.f, dataspace, extended_range_brightness_ratio));
    EXPECT_EQ(dataspace, ADATASPACE_SRGB);
    EXPECT_EQ(extended_range_brightness_ratio, 1.f);
  }

  // sRGB.
  {
    ADataSpace dataspace = ADATASPACE_UNKNOWN;
    float extended_range_brightness_ratio = 0.f;
    EXPECT_TRUE(SurfaceControl::ColorSpaceToADataSpace(
        gfx::ColorSpace::CreateSRGB(), 1.f, dataspace,
        extended_range_brightness_ratio));
    EXPECT_EQ(dataspace, ADATASPACE_SRGB);
    EXPECT_EQ(extended_range_brightness_ratio, 1.f);
  }

  // Display P3.
  {
    ADataSpace dataspace = ADATASPACE_UNKNOWN;
    float extended_range_brightness_ratio = 0.f;
    EXPECT_TRUE(SurfaceControl::ColorSpaceToADataSpace(
        gfx::ColorSpace::CreateDisplayP3D65(), 1.f, dataspace,
        extended_range_brightness_ratio));
    EXPECT_EQ(dataspace, ADATASPACE_DISPLAY_P3);
    EXPECT_EQ(extended_range_brightness_ratio, 1.f);
  }

  // Before S, only sRGB and P3 are supported.
  if (base::android::BuildInfo::GetInstance()->sdk_int() <
      base::android::SDK_VERSION_S) {
    return;
  }

  // Rec2020 with an sRGB transfer function.
  {
    gfx::ColorSpace rec2020(gfx::ColorSpace::PrimaryID::BT2020,
                            gfx::ColorSpace::TransferID::SRGB);
    ADataSpace dataspace = ADATASPACE_UNKNOWN;
    float extended_range_brightness_ratio = 0.f;
    EXPECT_TRUE(SurfaceControl::ColorSpaceToADataSpace(
        rec2020, 1.f, dataspace, extended_range_brightness_ratio));
    EXPECT_EQ(dataspace, ADATASPACE_STANDARD_BT2020 | ADATASPACE_TRANSFER_SRGB |
                             ADATASPACE_RANGE_FULL);
    EXPECT_EQ(extended_range_brightness_ratio, 1.f);
  }

  // sRGB, but it will come out as extended because there is a >1 desired
  // brightness ratio.
  {
    ADataSpace dataspace = ADATASPACE_UNKNOWN;
    float extended_range_brightness_ratio = 0.f;
    EXPECT_TRUE(SurfaceControl::ColorSpaceToADataSpace(
        gfx::ColorSpace::CreateSRGB(), 4.f, dataspace,
        extended_range_brightness_ratio));
    EXPECT_EQ(dataspace, ADATASPACE_STANDARD_BT709 | ADATASPACE_TRANSFER_SRGB |
                             ADATASPACE_RANGE_EXTENDED);
    EXPECT_EQ(extended_range_brightness_ratio, 1.f);
  }

  // P3, extended by 2x.
  {
    skcms_TransferFunction trfn_srgb_scaled =
        skia::ScaleTransferFunction(SkNamedTransferFn::kSRGB, 2.f);
    gfx::ColorSpace p3_scaled(
        gfx::ColorSpace::PrimaryID::P3, gfx::ColorSpace::TransferID::CUSTOM_HDR,
        gfx::ColorSpace::MatrixID::RGB, gfx::ColorSpace::RangeID::FULL, nullptr,
        &trfn_srgb_scaled);
    ADataSpace dataspace = ADATASPACE_UNKNOWN;
    float extended_range_brightness_ratio = 0.f;
    EXPECT_TRUE(SurfaceControl::ColorSpaceToADataSpace(
        p3_scaled, 1.f, dataspace, extended_range_brightness_ratio));
    EXPECT_EQ(dataspace, ADATASPACE_STANDARD_DCI_P3 | ADATASPACE_TRANSFER_SRGB |
                             ADATASPACE_RANGE_EXTENDED);
    EXPECT_NEAR(extended_range_brightness_ratio, 2.f, 0.0001f);
  }
}

}  // namespace
}  // namespace gfx