File: native_pixmap_gl_binding_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (136 lines) | stat: -rw-r--r-- 4,544 bytes parent folder | download | duplicates (9)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>
#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/strings/stringize_macros.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/buffer_format_util.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/client_native_pixmap.h"
#include "ui/gfx/color_space.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_display.h"
#include "ui/gl/gl_surface.h"
#include "ui/gl/init/gl_factory.h"
#include "ui/gl/test/gl_test_support.h"
#include "ui/ozone/public/client_native_pixmap_factory_ozone.h"
#include "ui/ozone/public/ozone_platform.h"
#include "ui/ozone/public/surface_factory_ozone.h"

// TODO(crbug.com/40630408): Fix memory leaks in tests and re-enable on LSAN.
#ifdef LEAK_SANITIZER
#define MAYBE_Create DISABLED_Create
#else
#define MAYBE_Create Create
#endif

namespace gl {
namespace {

constexpr gfx::BufferUsage kUsage = gfx::BufferUsage::SCANOUT;
constexpr gfx::BufferFormat kFormat = gfx::BufferFormat::BGRA_8888;

bool SkipTest() {
  ui::OzonePlatform::InitParams params;
  params.single_process = true;
  ui::OzonePlatform::InitializeForGPU(params);

  ui::GLOzone* gl_ozone = ui::OzonePlatform::GetInstance()
                              ->GetSurfaceFactoryOzone()
                              ->GetCurrentGLOzone();
  if (!gl_ozone || !gl_ozone->CanImportNativePixmap(kFormat)) {
    LOG(WARNING) << "Skip test, ozone implementation can't import native "
                 << "pixmaps";
    return true;
  }
  return false;
}

class NativePixmapGLBindingTest : public testing::Test {
 public:
  NativePixmapGLBindingTest() {
    client_native_pixmap_factory_ = ui::CreateClientNativePixmapFactoryOzone();
  }

 protected:
  // Overridden from testing::Test:
  void SetUp() override {
    if (SkipTest()) {
      GTEST_SKIP();
    }
    display_ = GLTestSupport::InitializeGL(std::nullopt);
    surface_ = gl::init::CreateOffscreenGLSurface(display_, gfx::Size());
    context_ =
        gl::init::CreateGLContext(nullptr, surface_.get(), GLContextAttribs());
    context_->MakeCurrent(surface_.get());
  }
  void TearDown() override {
    if (texture_id_) {
      glDeleteTextures(1, &texture_id_);
    }
    if (context_) {
      context_->ReleaseCurrent(surface_.get());
      context_ = nullptr;
    }
    surface_ = nullptr;
    GLTestSupport::CleanupGL(display_);
  }

 protected:
  std::unique_ptr<ui::NativePixmapGLBinding> CreateSolidColorImage(
      const gfx::Size& size) {
    ui::SurfaceFactoryOzone* surface_factory =
        ui::OzonePlatform::GetInstance()->GetSurfaceFactoryOzone();
    scoped_refptr<gfx::NativePixmap> pixmap =
        surface_factory->CreateNativePixmap(gfx::kNullAcceleratedWidget,
                                            nullptr, size, kFormat, kUsage);
    DCHECK(pixmap) << "Offending format: "
                   << gfx::BufferFormatToString(kFormat);

    // Create a dummy texture ID to bind - these tests don't actually care about
    // binding.
    if (!texture_id_) {
      glGenTextures(1, &texture_id_);
    }

    ui::GLOzone* gl_ozone = ui::OzonePlatform::GetInstance()
                                ->GetSurfaceFactoryOzone()
                                ->GetCurrentGLOzone();
    EXPECT_TRUE(gl_ozone->CanImportNativePixmap(kFormat));

    auto binding = gl_ozone->ImportNativePixmap(
        std::move(pixmap), kFormat, gfx::BufferPlane::DEFAULT, size,
        gfx::ColorSpace(), GL_TEXTURE_EXTERNAL_OES, texture_id_);
    EXPECT_TRUE(binding);
    return binding;
  }

  scoped_refptr<GLSurface> surface_;
  scoped_refptr<GLContext> context_;
  GLuint texture_id_ = 0;
  std::unique_ptr<gfx::ClientNativePixmapFactory> client_native_pixmap_factory_;
  raw_ptr<GLDisplay> display_ = nullptr;
};

TEST_F(NativePixmapGLBindingTest, MAYBE_Create) {
  // NOTE: On some drm devices (mediatek) the minimum width/height to add an fb
  // for a bo must be 64, and YVU_420 in i915 requires at least 128 length.
  const gfx::Size small_image_size(128, 128);
  const gfx::Size large_image_size(512, 512);

  // Create a small solid color green image of preferred format.
  auto small_image = CreateSolidColorImage(small_image_size);
  ASSERT_TRUE(small_image);

  // Create a large solid color green image of preferred format.
  auto large_image = CreateSolidColorImage(large_image_size);
  ASSERT_TRUE(large_image);
}

}  // namespace
}  // namespace gl