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
|
// 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/gfx/x/visual_manager.h"
#include <bitset>
#include "base/strings/string_number_conversions.h"
#include "ui/gfx/x/atom_cache.h"
#include "ui/gfx/x/event.h"
#include "ui/gfx/x/visual_picker_glx.h"
#include "ui/gfx/x/xfixes.h"
namespace x11 {
VisualManager::VisualManager(Connection* connection) : connection_(connection) {
auto atom_name =
"_NET_WM_CM_S" + base::NumberToString(connection_->DefaultScreenId());
compositor_atom_ = GetAtom(atom_name.c_str());
auto& xfixes = connection_->xfixes();
if (xfixes.present()) {
auto mask = x11::XFixes::SelectionEventMask::SetSelectionOwner |
x11::XFixes::SelectionEventMask::SelectionWindowDestroy |
x11::XFixes::SelectionEventMask::SelectionClientClose;
xfixes.SelectSelectionInput(connection_->default_root(), compositor_atom_,
mask);
connection_->AddEventObserver(this);
}
if (auto response = connection_->GetSelectionOwner(compositor_atom_).Sync()) {
compositor_owner_ = response->owner;
}
for (const auto& depth : connection_->default_screen().allowed_depths) {
for (const auto& visual : depth.visuals) {
visuals_[visual.visual_id] =
std::make_unique<XVisualData>(connection_, depth.depth, &visual);
}
}
ColorMap colormap;
PickBestVisuals(connection, opaque_visual_id_, transparent_visual_id_);
// Choose the opaque visual.
if (opaque_visual_id_ == VisualId{}) {
opaque_visual_id_ = connection->default_screen().root_visual;
}
// opaque_visual_id_ may be unset in headless environments
if (opaque_visual_id_ != VisualId{}) {
DCHECK(visuals_.find(opaque_visual_id_) != visuals_.end());
ChooseVisualForWindow(false, nullptr, nullptr, &colormap, nullptr);
}
// Choose the transparent visual.
if (transparent_visual_id_ == VisualId{}) {
for (const auto& pair : visuals_) {
// Why support only 8888 ARGB? Because it's all that GTK supports. In
// gdkvisual-x11.cc, they look for this specific visual and use it for
// all their alpha channel using needs.
const auto& data = *pair.second;
if (data.depth == 32 && data.info->red_mask == 0xff0000 &&
data.info->green_mask == 0x00ff00 &&
data.info->blue_mask == 0x0000ff) {
transparent_visual_id_ = pair.first;
break;
}
}
}
if (transparent_visual_id_ != VisualId{}) {
DCHECK(visuals_.find(transparent_visual_id_) != visuals_.end());
ChooseVisualForWindow(true, nullptr, nullptr, &colormap, nullptr);
}
}
VisualManager::~VisualManager() {
auto& xfixes = connection_->xfixes();
if (xfixes.present()) {
xfixes.SelectSelectionInput(connection_->default_root(), compositor_atom_);
connection_->RemoveEventObserver(this);
}
}
void VisualManager::ChooseVisualForWindow(bool want_argb_visual,
VisualId* visual_id,
uint8_t* depth,
ColorMap* colormap,
bool* visual_has_alpha) {
bool use_argb = want_argb_visual && ArgbVisualAvailable();
VisualId visual = use_argb ? transparent_visual_id_ : opaque_visual_id_;
if (visual_id) {
*visual_id = visual;
}
bool success = GetVisualInfo(visual, depth, colormap, visual_has_alpha);
DCHECK(success);
}
bool VisualManager::GetVisualInfo(VisualId visual_id,
uint8_t* depth,
ColorMap* colormap,
bool* visual_has_alpha) {
DCHECK_NE(visual_id, VisualId{});
auto it = visuals_.find(visual_id);
if (it == visuals_.end()) {
return false;
}
XVisualData& data = *it->second;
const VisualType& info = *data.info;
if (depth) {
*depth = data.depth;
}
if (colormap) {
bool is_default_visual =
visual_id == connection_->default_root_visual().visual_id;
*colormap = is_default_visual ? ColorMap{} : data.GetColormap(connection_);
}
if (visual_has_alpha) {
auto popcount = [](auto x) {
return std::bitset<8 * sizeof(decltype(x))>(x).count();
};
*visual_has_alpha = popcount(info.red_mask) + popcount(info.green_mask) +
popcount(info.blue_mask) <
static_cast<std::size_t>(data.depth);
}
return true;
}
bool VisualManager::ArgbVisualAvailable() const {
return compositor_owner_ != x11::Window::None &&
transparent_visual_id_ != VisualId{};
}
VisualManager::XVisualData::XVisualData(Connection* connection,
uint8_t depth,
const VisualType* info)
: depth(depth), info(info) {}
// Do not free the colormap as this would uninstall the colormap even for
// non-Chromium clients.
VisualManager::XVisualData::~XVisualData() = default;
ColorMap VisualManager::XVisualData::GetColormap(Connection* connection) {
if (colormap_ == ColorMap{}) {
colormap_ = connection->GenerateId<ColorMap>();
connection->CreateColormap({ColormapAlloc::None, colormap_,
connection->default_root(), info->visual_id});
// In single-process mode, VisualManager may be used on multiple threads,
// so we need to flush colormap creation early so that other threads are
// able to use it.
connection->Flush();
}
return colormap_;
}
void VisualManager::OnEvent(const x11::Event& event) {
if (auto* selection_notify = event.As<x11::XFixes::SelectionNotifyEvent>()) {
if (selection_notify->selection == compositor_atom_) {
compositor_owner_ = selection_notify->owner;
}
}
}
} // namespace x11
|