File: software_output_device_mac.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 (223 lines) | stat: -rw-r--r-- 7,839 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
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "components/viz/service/display_embedder/software_output_device_mac.h"

#include <memory>
#include <utility>

#include "base/apple/foundation_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "ui/gfx/ca_layer_params.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/mac/io_surface.h"

namespace viz {

SoftwareOutputDeviceMac::Buffer::Buffer() = default;
SoftwareOutputDeviceMac::Buffer::~Buffer() = default;

SoftwareOutputDeviceMac::SoftwareOutputDeviceMac(
    scoped_refptr<base::SequencedTaskRunner> task_runner)
    : SoftwareOutputDevice(std::move(task_runner)) {}

SoftwareOutputDeviceMac::~SoftwareOutputDeviceMac() = default;

void SoftwareOutputDeviceMac::Resize(const gfx::Size& pixel_size,
                                     float scale_factor) {
  if (pixel_size_ == pixel_size && scale_factor_ == scale_factor)
    return;

  pixel_size_ = pixel_size;
  scale_factor_ = scale_factor;

  DiscardBackbuffer();
}

void SoftwareOutputDeviceMac::UpdateAndCopyBufferDamage(
    Buffer* previous_paint_buffer,
    const SkRegion& new_damage_region) {
  TRACE_EVENT0("browser", "CopyPreviousBufferDamage");

  // Expand the |accumulated_damage| of all buffers to include this frame's
  // damage.
  for (auto& buffer : buffer_queue_)
    buffer->accumulated_damage.op(new_damage_region, SkRegion::kUnion_Op);

  // Compute the region to copy from |previous_paint_buffer| to
  // |current_paint_buffer_| by subtracting |new_damage_region| (which we will
  // be painting) from |current_paint_buffer_|'s |accumulated_damage|.
  SkRegion copy_region;
  current_paint_buffer_->accumulated_damage.swap(copy_region);
  bool copy_region_nonempty =
      copy_region.op(new_damage_region, SkRegion::kDifference_Op);
  last_copy_region_for_testing_ = copy_region;
  if (!copy_region_nonempty)
    return;

  // If we have anything to copy, we had better have a buffer to copy it from.
  if (!previous_paint_buffer) {
    DLOG(ERROR) << "No previous paint buffer to copy accumulated damage from.";
    last_copy_region_for_testing_.setEmpty();
    return;
  }

  // It is possible for |previous_paint_buffer| to equal
  // |current_paint_buffer_|, but if it does, we should not need to do a copy.
  CHECK_NE(previous_paint_buffer, current_paint_buffer_);

  IOSurfaceRef previous_io_surface = previous_paint_buffer->io_surface.get();

  {
    TRACE_EVENT0("browser", "IOSurfaceLock for software copy");
    kern_return_t io_result = IOSurfaceLock(
        previous_io_surface, kIOSurfaceLockReadOnly | kIOSurfaceLockAvoidSync,
        nullptr);
    if (io_result != KERN_SUCCESS) {
      DLOG(ERROR) << "Failed to lock previous IOSurface " << io_result;
      return;
    }
  }

  uint8_t* pixels =
      static_cast<uint8_t*>(IOSurfaceGetBaseAddress(previous_io_surface));
  size_t stride = IOSurfaceGetBytesPerRow(previous_io_surface);
  size_t bytes_per_element = 4;
  for (SkRegion::Iterator it(copy_region); !it.done(); it.next()) {
    const SkIRect& rect = it.rect();
    current_paint_canvas_->writePixels(
        SkImageInfo::MakeN32Premul(rect.width(), rect.height()),
        pixels + bytes_per_element * rect.x() + stride * rect.y(), stride,
        rect.x(), rect.y());
  }

  {
    TRACE_EVENT0("browser", "IOSurfaceUnlock");
    kern_return_t io_result = IOSurfaceUnlock(
        previous_io_surface, kIOSurfaceLockReadOnly | kIOSurfaceLockAvoidSync,
        nullptr);
    if (io_result != KERN_SUCCESS) {
      DLOG(ERROR) << "Failed to unlock previous IOSurface " << io_result;
    }
  }
}

SkCanvas* SoftwareOutputDeviceMac::BeginPaint(
    const gfx::Rect& new_damage_rect) {
  // Record the previous paint buffer.
  Buffer* previous_paint_buffer =
      buffer_queue_.empty() ? nullptr : buffer_queue_.back().get();

  // Find any buffer in the queue that is not in use by the window server, and
  // re-use it as the buffer for this paint. Note that this can be any buffer in
  // any position in the list.
  for (auto iter = buffer_queue_.begin(); iter != buffer_queue_.end(); ++iter) {
    Buffer* iter_buffer = iter->get();
    if (IOSurfaceIsInUse(iter_buffer->io_surface.get())) {
      continue;
    }
    current_paint_buffer_ = iter_buffer;
    buffer_queue_.splice(buffer_queue_.end(), buffer_queue_, iter);
    break;
  }

  // If we failed to find a suitable buffer, allocate a new one, and initialize
  // it with complete damage.
  if (!current_paint_buffer_) {
    std::unique_ptr<Buffer> new_buffer(new Buffer);
    new_buffer->io_surface =
        gfx::CreateIOSurface(pixel_size_, gfx::BufferFormat::BGRA_8888);
    if (!new_buffer->io_surface)
      return nullptr;
    // Set the initial damage to be the full buffer.
    new_buffer->accumulated_damage.setRect(
        gfx::RectToSkIRect(gfx::Rect(pixel_size_)));
    current_paint_buffer_ = new_buffer.get();
    buffer_queue_.push_back(std::move(new_buffer));
  }
  DCHECK(current_paint_buffer_);

  // Animating in steady-state should require no more than 4 buffers. If we have
  // more than that, then purge the older buffers (the window server will
  // continue to hold on to the IOSurfaces as long as is needed, so the
  // consequence will be extra allocate-free cycles).
  size_t kMaxBuffers = 4;
  while (buffer_queue_.size() > kMaxBuffers)
    buffer_queue_.pop_front();

  // Lock the |current_paint_buffer_|'s IOSurface and wrap it in
  // |current_paint_canvas_|.
  {
    TRACE_EVENT0("browser", "IOSurfaceLock for software paint");
    kern_return_t io_result =
        IOSurfaceLock(current_paint_buffer_->io_surface.get(),
                      kIOSurfaceLockAvoidSync, nullptr);
    if (io_result != KERN_SUCCESS) {
      DLOG(ERROR) << "Failed to lock IOSurface " << io_result;
      current_paint_buffer_ = nullptr;
      return nullptr;
    }
  }
  {
    SkPMColor* pixels = static_cast<SkPMColor*>(
        IOSurfaceGetBaseAddress(current_paint_buffer_->io_surface.get()));
    size_t stride =
        IOSurfaceGetBytesPerRow(current_paint_buffer_->io_surface.get());
    current_paint_canvas_ = SkCanvas::MakeRasterDirectN32(
        pixel_size_.width(), pixel_size_.height(), pixels, stride);
  }

  UpdateAndCopyBufferDamage(previous_paint_buffer,
                            SkRegion(gfx::RectToSkIRect(new_damage_rect)));

  return current_paint_canvas_.get();
}

void SoftwareOutputDeviceMac::EndPaint() {
  SoftwareOutputDevice::EndPaint();
  if (!current_paint_buffer_)
    return;

  {
    TRACE_EVENT0("browser", "IOSurfaceUnlock");
    kern_return_t io_result =
        IOSurfaceUnlock(current_paint_buffer_->io_surface.get(),
                        kIOSurfaceLockAvoidSync, nullptr);
    if (io_result != KERN_SUCCESS) {
      DLOG(ERROR) << "Failed to unlock IOSurface " << io_result;
    }
  }
  current_paint_canvas_.reset();

  if (client_) {
    gfx::CALayerParams ca_layer_params;
    ca_layer_params.is_empty = false;
    ca_layer_params.scale_factor = scale_factor_;
    ca_layer_params.pixel_size = pixel_size_;
    ca_layer_params.io_surface_mach_port.reset(
        IOSurfaceCreateMachPort(current_paint_buffer_->io_surface.get()));
    client_->SoftwareDeviceUpdatedCALayerParams(ca_layer_params);
  }

  current_paint_buffer_ = nullptr;
}

void SoftwareOutputDeviceMac::DiscardBackbuffer() {
  buffer_queue_.clear();
}

void SoftwareOutputDeviceMac::EnsureBackbuffer() {}

gfx::VSyncProvider* SoftwareOutputDeviceMac::GetVSyncProvider() {
  return nullptr;
}

}  // namespace viz