File: skia_output_device_x11.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 (147 lines) | stat: -rw-r--r-- 5,708 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
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
// 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_x11.h"

#include <utility>
#include <vector>

#include "base/logging.h"
#include "base/memory/ref_counted_memory.h"
#include "gpu/command_buffer/service/graphite_shared_context.h"
#include "third_party/libyuv/include/libyuv/planar_functions.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/gpu/graphite/Context.h"
#include "ui/base/x/x11_util.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/x/xproto.h"

namespace viz {

namespace {

x11::GraphicsContext CreateGC(x11::Connection* connection, x11::Window window) {
  auto gc = connection->GenerateId<x11::GraphicsContext>();
  connection->CreateGC({gc, window});
  return gc;
}

struct ReadPixelsContext {
  std::unique_ptr<const SkImage::AsyncReadResult> async_result;
  bool finished = false;
};

void OnReadPixelsDone(
    void* raw_ctx,
    std::unique_ptr<const SkImage::AsyncReadResult> async_result) {
  ReadPixelsContext* context = reinterpret_cast<ReadPixelsContext*>(raw_ctx);
  context->async_result = std::move(async_result);
  context->finished = true;
}

}  // namespace

// static
std::unique_ptr<SkiaOutputDeviceX11> SkiaOutputDeviceX11::Create(
    scoped_refptr<gpu::SharedContextState> context_state,
    gfx::AcceleratedWidget widget,
    gpu::MemoryTracker* memory_tracker,
    DidSwapBufferCompleteCallback did_swap_buffer_complete_callback) {
  auto window = static_cast<x11::Window>(widget);
  auto attributes =
      x11::Connection::Get()->GetWindowAttributes({window}).Sync();
  if (!attributes) {
    DLOG(ERROR) << "Failed to get attributes for window "
                << static_cast<uint32_t>(window);
    return {};
  }
  return std::make_unique<SkiaOutputDeviceX11>(
      base::PassKey<SkiaOutputDeviceX11>(), std::move(context_state), window,
      attributes->visual, memory_tracker, did_swap_buffer_complete_callback);
}

SkiaOutputDeviceX11::SkiaOutputDeviceX11(
    base::PassKey<SkiaOutputDeviceX11> pass_key,
    scoped_refptr<gpu::SharedContextState> context_state,
    x11::Window window,
    x11::VisualId visual,
    gpu::MemoryTracker* memory_tracker,
    DidSwapBufferCompleteCallback did_swap_buffer_complete_callback)
    : SkiaOutputDeviceOffscreen(context_state,
                                gfx::SurfaceOrigin::kTopLeft,
                                true /* has_alpha */,
                                memory_tracker,
                                did_swap_buffer_complete_callback),
      connection_(x11::Connection::Get()),
      window_(window),
      visual_(visual),
      gc_(CreateGC(connection_, window_)) {
  // |capabilities_| should be set by SkiaOutputDeviceOffscreen.
  DCHECK_EQ(capabilities_.output_surface_origin, gfx::SurfaceOrigin::kTopLeft);
  DCHECK(capabilities_.supports_post_sub_buffer);
}

SkiaOutputDeviceX11::~SkiaOutputDeviceX11() {
  connection_->FreeGC({gc_});
}

bool SkiaOutputDeviceX11::Reshape(const ReshapeParams& params) {
  if (!SkiaOutputDeviceOffscreen::Reshape(params)) {
    return false;
  }
  auto image_info =
      SkImageInfo::MakeN32(params.image_info.width(),
                           params.image_info.height(), kOpaque_SkAlphaType);
  pixels_ = base::MakeRefCounted<base::RefCountedBytes>(
      image_info.computeMinByteSize());
  return true;
}

void SkiaOutputDeviceX11::Present(const std::optional<gfx::Rect>& update_rect,
                                  BufferPresentedCallback feedback,
                                  OutputSurfaceFrame frame) {
  gfx::Rect rect = update_rect.value_or(
      gfx::Rect(0, 0, sk_surface_->width(), sk_surface_->height()));
  StartSwapBuffers(std::move(feedback));
  if (!rect.IsEmpty()) {
    auto dst_info =
        SkImageInfo::MakeN32(rect.width(), rect.height(), kOpaque_SkAlphaType);
    DCHECK_GE(pixels_->size(), dst_info.computeMinByteSize());
    SkPixmap dst_pixmap(dst_info, pixels_->data(), dst_info.minRowBytes());

    if (context_state_->gr_context()) {
      bool result = sk_surface_->readPixels(dst_pixmap, rect.x(), rect.y());
      LOG_IF(FATAL, !result)
          << "Failed to read pixels from offscreen SkSurface.";
    } else {
      auto* graphite_shared_context = context_state_->graphite_shared_context();
      CHECK(graphite_shared_context);
      ReadPixelsContext context;
      graphite_shared_context->asyncRescaleAndReadPixels(
          sk_surface_.get(), dst_info, gfx::RectToSkIRect(rect),
          SkImage::RescaleGamma::kSrc, SkImage::RescaleMode::kRepeatedLinear,
          base::BindOnce(&OnReadPixelsDone), &context);
      graphite_shared_context->submit(skgpu::graphite::SyncToCpu::kYes);
      LOG_IF(FATAL, !context.async_result)
          << "Failed to read pixels from offscreen SkSurface.";
      libyuv::CopyPlane(
          static_cast<const uint8_t*>(context.async_result->data(0)),
          context.async_result->rowBytes(0),
          static_cast<uint8_t*>(dst_pixmap.writable_addr()),
          dst_pixmap.rowBytes(), dst_info.minRowBytes(), dst_info.height());
    }

    // TODO(penghuang): Switch to XShmPutImage.
    ui::DrawPixmap(x11::Connection::Get(), visual_, window_, gc_, dst_pixmap,
                   0 /* src_x */, 0 /* src_y */, rect.x() /* dst_x */,
                   rect.y() /* dst_y */, rect.width(), rect.height());

    connection_->Flush();
  }
  FinishSwapBuffers(gfx::SwapCompletionResult(gfx::SwapResult::SWAP_ACK),
                    gfx::Size(sk_surface_->width(), sk_surface_->height()),
                    std::move(frame));
}

}  // namespace viz