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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/viz/service/display_embedder/skia_output_device_gl.h"
#include <tuple>
#include <utility>
#include "base/debug/alias.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "base/notreached.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "gpu/command_buffer/service/feature_info.h"
#include "gpu/command_buffer/service/shared_context_state.h"
#include "gpu/command_buffer/service/texture_manager.h"
#include "skia/ext/legacy_display_globals.h"
#include "third_party/skia/include/core/SkColorType.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/core/SkSurfaceProps.h"
#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
#include "third_party/skia/include/gpu/ganesh/gl/GrGLBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/gl/GrGLTypes.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_features.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_surface.h"
namespace viz {
namespace {
base::TimeTicks g_last_reshape_failure = base::TimeTicks();
NOINLINE void CheckForLoopFailures() {
const auto threshold = base::Seconds(1);
auto now = base::TimeTicks::Now();
if (!g_last_reshape_failure.is_null() &&
now - g_last_reshape_failure < threshold) {
NOTREACHED();
}
g_last_reshape_failure = now;
}
} // namespace
class SkiaOutputDeviceGL::MultiSurfaceSwapBuffersTracker {
public:
// Returns true if multiple surface swaps detected.
bool TrackSwapBuffers() {
static int next_global_swap_generation = 0;
static int num_swaps_in_current_swap_generation = 0;
static int last_multi_window_swap_generation = 0;
// This code is a simple way of enforcing that we only vsync if one surface
// is swapping per frame. This provides single window cases a stable refresh
// while allowing multi-window cases to not slow down due to multiple syncs
// on a single thread. A better way to fix this problem would be to have
// each surface present on its own thread.
// If next global swap generation equals to our surface's next swap
// generation means we start new swap generation and this is first surface
// to swap.
if (next_global_swap_generation == next_surface_swap_generation_) {
// Start new generation.
next_global_swap_generation++;
// Store number of swaps in the previous generation.
if (num_swaps_in_current_swap_generation > 1) {
last_multi_window_swap_generation = next_global_swap_generation;
}
num_swaps_in_current_swap_generation = 0;
}
next_surface_swap_generation_ = next_global_swap_generation;
num_swaps_in_current_swap_generation++;
// Number of swap generations before vsync is re-enabled after we've stopped
// doing multiple swaps per frame.
constexpr int kMultiWindowSwapEnableVSyncDelay = 60;
return (num_swaps_in_current_swap_generation > 1) ||
(next_global_swap_generation - last_multi_window_swap_generation <
kMultiWindowSwapEnableVSyncDelay);
}
private:
int next_surface_swap_generation_ = 0;
};
SkiaOutputDeviceGL::SkiaOutputDeviceGL(
gpu::SharedContextState* context_state,
scoped_refptr<gl::GLSurface> gl_surface,
scoped_refptr<gpu::gles2::FeatureInfo> feature_info,
scoped_refptr<gpu::MemoryTracker> memory_tracker,
DidSwapBufferCompleteCallback did_swap_buffer_complete_callback)
: SkiaOutputDevice(context_state->gr_context(),
context_state->graphite_shared_context(),
std::move(memory_tracker),
std::move(did_swap_buffer_complete_callback)),
context_state_(context_state),
gl_surface_(std::move(gl_surface)),
supports_async_swap_(gl_surface_->SupportsAsyncSwap()) {
capabilities_.uses_default_gl_framebuffer = true;
capabilities_.output_surface_origin = gl_surface_->GetOrigin();
capabilities_.supports_post_sub_buffer = gl_surface_->SupportsPostSubBuffer();
if (feature_info->workarounds()
.disable_post_sub_buffers_for_onscreen_surfaces) {
capabilities_.supports_post_sub_buffer = false;
}
capabilities_.pending_swap_params.max_pending_swaps =
gl_surface_->GetBufferCount() - 1;
#if BUILDFLAG(IS_ANDROID)
// TODO(weiliangc): This capability is used to check whether we should do
// overlay. Since currently none of the other overlay system is implemented,
// only update this for Android.
// This output device is never offscreen.
capabilities_.supports_surfaceless = gl_surface_->IsSurfaceless();
#endif
#if BUILDFLAG(IS_CHROMEOS)
// If Chrome OS is run on Linux for development purposes, we need to
// advertise a hardware orientation mode since Ash manages a separate device
// rotation independent of the host's native windowing system.
capabilities_.orientation_mode = OutputSurface::OrientationMode::kHardware;
#endif // IS_CHROMEOS
DCHECK(context_state_);
DCHECK(gl_surface_);
DCHECK(context_state_->gr_context());
DCHECK(context_state_->context());
GrDirectContext* gr_context = context_state_->gr_context();
// Get alpha bits from the default frame buffer.
int alpha_bits = 0;
glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
gr_context->resetContext(kRenderTarget_GrGLBackendState);
glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
CHECK_GL_ERROR();
auto color_type = kRGBA_8888_SkColorType;
if (alpha_bits == 0) {
color_type = gl_surface_->GetFormat().IsRGB565() ? kRGB_565_SkColorType
: kRGB_888x_SkColorType;
// Skia disables RGBx on some GPUs, fallback to RGBA if it's the
// case. This doesn't change framebuffer itself, as we already allocated it,
// but will change any temporary buffer Skia needs to allocate.
if (!context_state_->gr_context()
->defaultBackendFormat(color_type, GrRenderable::kYes)
.isValid()) {
color_type = kRGBA_8888_SkColorType;
}
}
// SRGB
capabilities_.sk_color_type_map[SinglePlaneFormat::kRGBA_8888] = color_type;
capabilities_.sk_color_type_map[SinglePlaneFormat::kRGBX_8888] = color_type;
capabilities_.sk_color_type_map[SinglePlaneFormat::kBGRA_8888] = color_type;
capabilities_.sk_color_type_map[SinglePlaneFormat::kBGRX_8888] = color_type;
// HDR10
capabilities_.sk_color_type_map[SinglePlaneFormat::kRGBA_1010102] =
kRGBA_1010102_SkColorType;
// scRGB linear
capabilities_.sk_color_type_map[SinglePlaneFormat::kRGBA_F16] =
kRGBA_F16_SkColorType;
if (features::UseGpuVsync()) {
// Historically we never disabled vsync on Android and it's very rare
// use-case to have multiple active windows there. On other platforms we
// disable GLSurface's VSync if we're swapping multiple surfaces per frame
// to prevent SwapBuffers from blocking and slowing down other windows.
#if !BUILDFLAG(IS_ANDROID)
multisurface_swapbuffers_tracker_ =
std::make_unique<MultiSurfaceSwapBuffersTracker>();
#endif
} else {
gl_surface_->SetVSyncEnabled(false);
}
}
SkiaOutputDeviceGL::~SkiaOutputDeviceGL() {
// gl_surface_ will be destructed soon.
memory_type_tracker_->TrackMemFree(backbuffer_estimated_size_);
}
bool SkiaOutputDeviceGL::Reshape(const ReshapeParams& params) {
#if !BUILDFLAG(IS_CHROMEOS)
DCHECK_EQ(params.transform, gfx::OVERLAY_TRANSFORM_NONE);
#endif // !BUILDFLAG(IS_CHROMEOS)
const gfx::Size size = params.GfxSize();
const SkColorType color_type = params.image_info.colorType();
const bool has_alpha = !params.image_info.isOpaque();
if (!gl_surface_->Resize(size, params.device_scale_factor, params.color_space,
has_alpha)) {
CheckForLoopFailures();
// To prevent tail call, so we can see the stack.
base::debug::Alias(nullptr);
return false;
}
SkSurfaceProps surface_props;
GrGLFramebufferInfo framebuffer_info = {0};
DCHECK_EQ(gl_surface_->GetBackingFramebufferObject(), 0u);
auto* gr_context = context_state_->gr_context();
GrBackendFormat backend_format =
gr_context->defaultBackendFormat(color_type, GrRenderable::kYes);
DCHECK(backend_format.isValid()) << "color_type: " << color_type;
framebuffer_info.fFormat = GrBackendFormats::AsGLFormatEnum(backend_format);
auto render_target = GrBackendRenderTargets::MakeGL(
size.width(), size.height(), params.sample_count,
/*stencilBits=*/0, framebuffer_info);
auto origin = (gl_surface_->GetOrigin() == gfx::SurfaceOrigin::kTopLeft)
? kTopLeft_GrSurfaceOrigin
: kBottomLeft_GrSurfaceOrigin;
sk_surface_ = SkSurfaces::WrapBackendRenderTarget(
gr_context, render_target, origin, color_type,
params.image_info.refColorSpace(), &surface_props);
if (!sk_surface_) {
LOG(ERROR) << "Couldn't create surface:" << "\n abandoned()="
<< gr_context->abandoned() << "\n color_type=" << color_type
<< "\n framebuffer_info.fFBOID=" << framebuffer_info.fFBOID
<< "\n framebuffer_info.fFormat=" << framebuffer_info.fFormat
<< "\n color_space=" << params.color_space.ToString()
<< "\n size=" << size.ToString();
CheckForLoopFailures();
// To prevent tail call, so we can see the stack.
base::debug::Alias(nullptr);
}
memory_type_tracker_->TrackMemFree(backbuffer_estimated_size_);
GLenum format = gpu::gles2::TextureManager::ExtractFormatFromStorageFormat(
framebuffer_info.fFormat);
GLenum type = gpu::gles2::TextureManager::ExtractTypeFromStorageFormat(
framebuffer_info.fFormat);
uint32_t estimated_size;
gpu::gles2::GLES2Util::ComputeImageDataSizes(
size.width(), size.height(), 1 /* depth */, format, type,
4 /* alignment */, &estimated_size, nullptr, nullptr);
backbuffer_estimated_size_ = estimated_size * gl_surface_->GetBufferCount();
memory_type_tracker_->TrackMemAlloc(backbuffer_estimated_size_);
return !!sk_surface_;
}
void SkiaOutputDeviceGL::Present(const std::optional<gfx::Rect>& update_rect,
BufferPresentedCallback feedback,
OutputSurfaceFrame frame) {
if (multisurface_swapbuffers_tracker_) {
const bool multiple_surface_swaps =
multisurface_swapbuffers_tracker_->TrackSwapBuffers();
gl_surface_->SetVSyncEnabled(!multiple_surface_swaps);
}
StartSwapBuffers({});
gfx::Size surface_size =
gfx::Size(sk_surface_->width(), sk_surface_->height());
auto data = frame.data;
if (supports_async_swap_) {
auto callback = base::BindOnce(
&SkiaOutputDeviceGL::DoFinishSwapBuffersAsync,
weak_ptr_factory_.GetWeakPtr(), surface_size, std::move(frame));
if (update_rect) {
gl_surface_->PostSubBufferAsync(
update_rect->x(), update_rect->y(), update_rect->width(),
update_rect->height(), std::move(callback), std::move(feedback),
std::move(data));
} else {
gl_surface_->SwapBuffersAsync(std::move(callback), std::move(feedback),
std::move(data));
}
} else {
gfx::SwapResult result;
if (update_rect) {
result = gl_surface_->PostSubBuffer(
update_rect->x(), update_rect->y(), update_rect->width(),
update_rect->height(), std::move(feedback), std::move(data));
} else {
result = gl_surface_->SwapBuffers(std::move(feedback), std::move(data));
}
DoFinishSwapBuffers(surface_size, std::move(frame),
gfx::SwapCompletionResult(result));
}
}
void SkiaOutputDeviceGL::DoFinishSwapBuffersAsync(
const gfx::Size& size,
OutputSurfaceFrame frame,
gfx::SwapCompletionResult result) {
DCHECK(result.release_fence.is_null());
FinishSwapBuffers(std::move(result), size, std::move(frame));
}
void SkiaOutputDeviceGL::DoFinishSwapBuffers(const gfx::Size& size,
OutputSurfaceFrame frame,
gfx::SwapCompletionResult result) {
DCHECK(result.release_fence.is_null());
FinishSwapBuffers(std::move(result), size, std::move(frame));
}
SkSurface* SkiaOutputDeviceGL::BeginPaint(
std::vector<GrBackendSemaphore>* end_semaphores) {
DCHECK(sk_surface_);
return sk_surface_.get();
}
void SkiaOutputDeviceGL::EndPaint() {}
} // namespace viz
|