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
|
// Copyright 2021 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/common/transition_utils.h"
#include <memory>
#include <sstream>
#include <string>
#include <unordered_set>
#include <vector>
#include "components/viz/common/quads/compositor_render_pass.h"
#include "components/viz/common/quads/compositor_render_pass_draw_quad.h"
#include "components/viz/common/quads/render_pass_io.h"
#include "components/viz/common/quads/shared_element_draw_quad.h"
#include "components/viz/common/quads/solid_color_draw_quad.h"
#include "third_party/skia/include/core/SkColor.h"
namespace viz {
namespace {
constexpr int kMaxListToProcess = 32;
constexpr int kMaxQuadsPerFrame = 8;
struct StackFrame {
StackFrame(int list_index,
SharedQuadStateList::ConstIterator sqs_iter,
QuadList::ConstIterator quad_iter,
int indent)
: list_index(list_index),
sqs_iter(sqs_iter),
quad_iter(quad_iter),
indent(indent) {}
int list_index;
SharedQuadStateList::ConstIterator sqs_iter;
QuadList::ConstIterator quad_iter;
int indent;
bool first_pass_visit = true;
};
std::string SkColorToRGBAString(SkColor color) {
std::ostringstream str;
str << "rgba(" << SkColorGetR(color) << ", " << SkColorGetG(color) << ", "
<< SkColorGetB(color) << ", " << SkColorGetA(color) << ")";
return str.str();
}
std::string SkColorToRGBAString(SkColor4f color) {
return SkColorToRGBAString(color.toSkColor());
}
std::unordered_set<uint64_t> ProcessStack(
std::ostringstream& str,
std::vector<StackFrame>& stack,
const CompositorRenderPassList& list) {
auto write_indent = [&str](int indent) {
for (int i = 0; i < indent; ++i)
str << " ";
};
auto write_render_pass = [&str](const CompositorRenderPass* pass) {
str << "(" << pass << ") render pass id=" << pass->id.GetUnsafeValue()
<< " output_rect=" << pass->output_rect.ToString();
if (pass->view_transition_element_resource_id.IsValid())
str << " " << pass->view_transition_element_resource_id.ToString();
str << "\n";
};
auto write_sqs = [&str](const SharedQuadState* sqs) {
str << "(" << sqs << ") switched to sqs with opacity=" << sqs->opacity
<< ", blend_mode=" << BlendModeToString(sqs->blend_mode)
<< " quad_layer_rect=" << sqs->quad_layer_rect.ToString() << "\n";
};
auto write_shared_element_quad = [&str](const SharedElementDrawQuad* quad) {
str << "(" << quad << ") SharedElementDrawQuad "
<< quad->element_resource_id.ToString() << "\n";
};
auto write_solid_color_quad = [&str](const SolidColorDrawQuad* quad) {
str << "(" << quad
<< ") SolidColorDrawQuad color=" << SkColorToRGBAString(quad->color)
<< "\n";
};
auto write_compositor_render_pass_quad =
[&str](const CompositorRenderPassDrawQuad* quad) {
str << "(" << quad << ") CompositorRenderPassDrawQuad\n";
};
std::unordered_set<uint64_t> seen_render_pass_ids;
int quads_per_frame_logged = 0;
while (!stack.empty()) {
auto& frame = stack.back();
auto& pass = list[frame.list_index];
if (frame.first_pass_visit) {
frame.first_pass_visit = false;
write_indent(frame.indent);
write_render_pass(pass.get());
seen_render_pass_ids.insert(pass->id.GetUnsafeValue());
if (const auto* sqs = *frame.sqs_iter) {
frame.indent += 2;
write_indent(frame.indent);
write_sqs(sqs);
} else {
stack.pop_back();
continue;
}
if (!*frame.quad_iter) {
stack.pop_back();
continue;
}
frame.indent += 2;
} else {
if (++frame.quad_iter == pass->quad_list.end()) {
quads_per_frame_logged = 0;
stack.pop_back();
continue;
}
if (++quads_per_frame_logged > kMaxQuadsPerFrame) {
write_indent(frame.indent);
str << "(more quads - orphaned list may not be correct)\n";
quads_per_frame_logged = 0;
stack.pop_back();
continue;
}
frame.indent -= 2;
while ((*frame.quad_iter)->shared_quad_state != *frame.sqs_iter) {
++frame.sqs_iter;
DCHECK(frame.sqs_iter != pass->shared_quad_state_list.end());
write_indent(frame.indent);
write_sqs(*frame.sqs_iter);
}
frame.indent += 2;
}
write_indent(frame.indent);
switch ((*frame.quad_iter)->material) {
case DrawQuad::Material::kCompositorRenderPass: {
auto* quad =
CompositorRenderPassDrawQuad::MaterialCast((*frame.quad_iter));
write_compositor_render_pass_quad(quad);
bool found = false;
for (auto i = frame.list_index - 1; i >= 0; --i) {
if (list[i]->id == quad->render_pass_id) {
found = true;
stack.emplace_back(i, list[i]->shared_quad_state_list.begin(),
list[i]->quad_list.begin(), frame.indent + 2);
break;
}
}
CHECK(found) << "Couldn't find referenced render pass id";
break;
}
case DrawQuad::Material::kSharedElement: {
auto* quad = SharedElementDrawQuad::MaterialCast((*frame.quad_iter));
write_shared_element_quad(quad);
break;
}
case DrawQuad::Material::kSolidColor: {
auto* quad = SolidColorDrawQuad::MaterialCast((*frame.quad_iter));
write_solid_color_quad(quad);
break;
}
default:
str << "DrawQuad, material: "
<< DrawQuadMaterialToString((*frame.quad_iter)->material) << "\n";
break;
}
}
return seen_render_pass_ids;
}
} // namespace
std::string TransitionUtils::RenderPassListToString(
const CompositorRenderPassList& list) {
std::ostringstream str;
if (list.size() > kMaxListToProcess || list.empty()) {
str << "RenderPassList too large or too small (" << list.size()
<< "), max supported list length " << kMaxListToProcess;
return str.str();
}
std::vector<StackFrame> stack;
stack.emplace_back(list.size() - 1,
list.back()->shared_quad_state_list.begin(),
list.back()->quad_list.begin(), 0);
str << "render pass ids in order:\n";
for (const auto& pass : list)
str << " " << pass->id.GetUnsafeValue();
str << "\n";
str << "rooted render pass tree:\n";
std::unordered_set<uint64_t> seen_render_pass_ids =
ProcessStack(str, stack, list);
if (list.size() != seen_render_pass_ids.size())
str << "orphaned render pass tree(s):\n";
while (true) {
int i;
for (i = list.size() - 1; i >= 0; --i) {
if (seen_render_pass_ids.count(list[i]->id.GetUnsafeValue()) == 0)
break;
}
if (i < 0)
break;
DCHECK(stack.empty());
stack.emplace_back(i, list[i]->shared_quad_state_list.begin(),
list[i]->quad_list.begin(), 0);
auto new_seen_pass_ids = ProcessStack(str, stack, list);
seen_render_pass_ids.insert(new_seen_pass_ids.begin(),
new_seen_pass_ids.end());
}
return str.str();
}
// static
std::unique_ptr<CompositorRenderPass>
TransitionUtils::CopyPassWithQuadFiltering(
const CompositorRenderPass& source_pass,
FilterCallback filter_callback) {
// This code is similar to CompositorRenderPass::DeepCopy, but does special
// logic when copying compositor render pass draw quads.
auto copy_pass = CompositorRenderPass::Create(
source_pass.shared_quad_state_list.size(), source_pass.quad_list.size());
copy_pass->SetAll(
source_pass.id, source_pass.output_rect, source_pass.damage_rect,
source_pass.transform_to_root_target, source_pass.filters,
source_pass.backdrop_filters, source_pass.backdrop_filter_bounds,
source_pass.subtree_capture_id, source_pass.subtree_size,
source_pass.view_transition_element_resource_id,
source_pass.has_transparent_background, source_pass.cache_render_pass,
source_pass.has_damage_from_contributing_content,
source_pass.generate_mipmap, source_pass.has_per_quad_damage);
if (source_pass.shared_quad_state_list.empty())
return copy_pass;
SharedQuadStateList::ConstIterator sqs_iter =
source_pass.shared_quad_state_list.begin();
SharedQuadState* copy_shared_quad_state =
copy_pass->CreateAndAppendSharedQuadState();
*copy_shared_quad_state = **sqs_iter;
for (auto* quad : source_pass.quad_list) {
while (quad->shared_quad_state != *sqs_iter) {
++sqs_iter;
DCHECK(sqs_iter != source_pass.shared_quad_state_list.end());
copy_shared_quad_state = copy_pass->CreateAndAppendSharedQuadState();
*copy_shared_quad_state = **sqs_iter;
}
DCHECK(quad->shared_quad_state == *sqs_iter);
if (filter_callback.Run(*quad, *copy_pass.get()))
continue;
if (quad->material == DrawQuad::Material::kCompositorRenderPass) {
const auto* pass_quad = CompositorRenderPassDrawQuad::MaterialCast(quad);
copy_pass->CopyFromAndAppendRenderPassDrawQuad(pass_quad,
pass_quad->render_pass_id);
} else {
copy_pass->CopyFromAndAppendDrawQuad(quad);
}
}
return copy_pass;
}
} // namespace viz
|