File: single_pixel_buffer.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 (69 lines) | stat: -rw-r--r-- 2,401 bytes parent folder | download | duplicates (5)
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
// Copyright 2023 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/ozone/platform/wayland/host/single_pixel_buffer.h"

#include <single-pixel-buffer-v1-client-protocol.h>
#include <wayland-util.h>

#include "base/logging.h"
#include "ui/ozone/platform/wayland/common/wayland_object.h"
#include "ui/ozone/platform/wayland/common/wayland_util.h"
#include "ui/ozone/platform/wayland/host/wayland_connection.h"

namespace ui {

namespace {
constexpr uint32_t kVersion = 1;
}

// static
constexpr char SinglePixelBuffer::kInterfaceName[];

// static
void SinglePixelBuffer::Instantiate(WaylandConnection* connection,
                                    wl_registry* registry,
                                    uint32_t name,
                                    const std::string& interface,
                                    uint32_t version) {
  CHECK_EQ(interface, kInterfaceName) << "Expected \"" << kInterfaceName
                                      << "\" but got \"" << interface << "\"";

  if (connection->single_pixel_buffer_ ||
      !wl::CanBind(interface, version, kVersion, kVersion)) {
    return;
  }

  auto single_pixel_buffer =
      wl::Bind<wp_single_pixel_buffer_manager_v1>(registry, name, kVersion);
  if (!single_pixel_buffer) {
    LOG(ERROR) << "Failed to bind wp_single_pixel_buffer_manager";
    return;
  }
  connection->single_pixel_buffer_ = std::make_unique<SinglePixelBuffer>(
      single_pixel_buffer.release(), connection);
}

SinglePixelBuffer::SinglePixelBuffer(
    wp_single_pixel_buffer_manager_v1* single_pixel_buffer,
    WaylandConnection* connection)
    : single_pixel_buffer_(single_pixel_buffer) {}

SinglePixelBuffer::~SinglePixelBuffer() = default;

wl::Object<wl_buffer> SinglePixelBuffer::CreateSinglePixelBuffer(
    const SkColor4f& color) {
  // Single Pixel Buffer protocol uses premultiplied color.
  uint32_t red = UINT_MAX * (double)color.fR * (double)color.fA;
  uint32_t green = UINT_MAX * (double)color.fG * (double)color.fA;
  uint32_t blue = UINT_MAX * (double)color.fB * (double)color.fA;
  uint32_t alpha = UINT_MAX * (double)color.fA;

  auto buffer = wl::Object<wl_buffer>(
      wp_single_pixel_buffer_manager_v1_create_u32_rgba_buffer(
          single_pixel_buffer_.get(), red, green, blue, alpha));
  return buffer;
}

}  // namespace ui