scene_update_context.cc 11.6 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/flow/scene_update_context.h"

7
#include "flutter/common/threads.h"
8
#include "flutter/flow/export_node.h"
9
#include "flutter/flow/layers/layer.h"
10
#include "flutter/flow/matrix_decomposition.h"
11 12 13 14
#include "flutter/glue/trace_event.h"

namespace flow {

15
SceneUpdateContext::SceneUpdateContext(scenic_lib::Session* session,
16
                                       SurfaceProducer* surface_producer)
17 18 19
    : session_(session), surface_producer_(surface_producer) {
  FTL_DCHECK(surface_producer_ != nullptr);
}
20

21 22 23 24 25 26 27 28
SceneUpdateContext::~SceneUpdateContext() {
  ASSERT_IS_GPU_THREAD;

  // Release Mozart session resources for all ExportNodes.
  for (auto export_node : export_nodes_) {
    export_node->Dispose(false);
  }
};
29

30 31 32
void SceneUpdateContext::AddChildScene(ExportNode* export_node,
                                       SkPoint offset,
                                       bool hit_testable) {
33
  ASSERT_IS_GPU_THREAD;
34
  FTL_DCHECK(top_entity_);
35

36
  export_node->Bind(*this, top_entity_->entity_node(), offset, hit_testable);
37 38
}

39 40 41 42 43 44 45 46 47 48 49 50
void SceneUpdateContext::AddExportNode(ExportNode* export_node) {
  ASSERT_IS_GPU_THREAD;

  export_nodes_.insert(export_node);  // Might already have been added.
}

void SceneUpdateContext::RemoveExportNode(ExportNode* export_node) {
  ASSERT_IS_GPU_THREAD;

  export_nodes_.erase(export_node);
}

51
void SceneUpdateContext::CreateFrame(scenic_lib::EntityNode& entity_node,
52 53 54 55 56 57
                                     const SkRRect& rrect,
                                     SkColor color,
                                     const SkRect& paint_bounds,
                                     std::vector<Layer*> paint_layers) {
  // Frames always clip their children.
  entity_node.SetClip(0u, true /* clip to self */);
58

59 60 61
  // We don't need a shape if the frame is zero size.
  if (rrect.isEmpty())
    return;
62

63 64 65 66
  // Add a part which represents the frame's geometry for clipping purposes
  // and possibly for its texture.
  // TODO(MZ-137): Need to be able to express the radii as vectors.
  SkRect shape_bounds = rrect.getBounds();
67
  scenic_lib::RoundedRectangle shape(
68 69 70 71 72 73 74 75
      session_,                                      // session
      rrect.width(),                                 // width
      rrect.height(),                                // height
      rrect.radii(SkRRect::kUpperLeft_Corner).x(),   // top_left_radius
      rrect.radii(SkRRect::kUpperRight_Corner).x(),  // top_right_radius
      rrect.radii(SkRRect::kLowerRight_Corner).x(),  // bottom_right_radius
      rrect.radii(SkRRect::kLowerLeft_Corner).x()    // bottom_left_radius
      );
76
  scenic_lib::ShapeNode shape_node(session_);
77 78 79 80 81
  shape_node.SetShape(shape);
  shape_node.SetTranslation(shape_bounds.width() * 0.5f + shape_bounds.left(),
                            shape_bounds.height() * 0.5f + shape_bounds.top(),
                            0.f);
  entity_node.AddPart(shape_node);
82

83 84 85
  // Check whether the painted layers will be visible.
  if (paint_bounds.isEmpty() || !paint_bounds.intersects(shape_bounds))
    paint_layers.clear();
86

87 88 89 90 91 92
  // Check whether a solid color will suffice.
  if (paint_layers.empty()) {
    SetShapeColor(shape_node, color);
    return;
  }

93 94 95 96
  // Apply current metrics and transformation scale factors.
  const float scale_x = metrics_->scale_x * top_scale_x_;
  const float scale_y = metrics_->scale_y * top_scale_y_;

97 98 99 100 101 102 103
  // If the painted area only covers a portion of the frame then we can
  // reduce the texture size by drawing just that smaller area.
  SkRect inner_bounds = shape_bounds;
  inner_bounds.intersect(paint_bounds);
  if (inner_bounds != shape_bounds && rrect.contains(inner_bounds)) {
    SetShapeColor(shape_node, color);

104
    scenic_lib::Rectangle inner_shape(session_, inner_bounds.width(),
105
                                          inner_bounds.height());
106
    scenic_lib::ShapeNode inner_node(session_);
107 108 109 110 111 112 113 114
    inner_node.SetShape(inner_shape);
    inner_node.SetTranslation(inner_bounds.width() * 0.5f + inner_bounds.left(),
                              inner_bounds.height() * 0.5f + inner_bounds.top(),
                              0.f);
    entity_node.AddPart(inner_node);
    SetShapeTextureOrColor(inner_node, color, scale_x, scale_y, inner_bounds,
                           std::move(paint_layers));
    return;
115 116
  }

117 118 119
  // Apply a texture to the whole shape.
  SetShapeTextureOrColor(shape_node, color, scale_x, scale_y, shape_bounds,
                         std::move(paint_layers));
120 121
}

122
void SceneUpdateContext::SetShapeTextureOrColor(
123
    scenic_lib::ShapeNode& shape_node,
124 125 126 127 128
    SkColor color,
    SkScalar scale_x,
    SkScalar scale_y,
    const SkRect& paint_bounds,
    std::vector<Layer*> paint_layers) {
129
  scenic_lib::Image* image = GenerateImageIfNeeded(
130 131
      color, scale_x, scale_y, paint_bounds, std::move(paint_layers));
  if (image != nullptr) {
132
    scenic_lib::Material material(session_);
133 134 135 136 137 138
    material.SetTexture(*image);
    shape_node.SetMaterial(material);
    return;
  }

  SetShapeColor(shape_node, color);
139 140
}

141
void SceneUpdateContext::SetShapeColor(scenic_lib::ShapeNode& shape_node,
142 143 144 145
                                       SkColor color) {
  if (SkColorGetA(color) == 0)
    return;

146
  scenic_lib::Material material(session_);
147 148 149
  material.SetColor(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color),
                    SkColorGetA(color));
  shape_node.SetMaterial(material);
150 151
}

152
scenic_lib::Image* SceneUpdateContext::GenerateImageIfNeeded(
153 154 155 156 157 158 159 160 161 162 163 164 165 166
    SkColor color,
    SkScalar scale_x,
    SkScalar scale_y,
    const SkRect& paint_bounds,
    std::vector<Layer*> paint_layers) {
  // Bail if there's nothing to paint.
  if (paint_layers.empty())
    return nullptr;

  // Bail if the physical bounds are empty after rounding.
  SkISize physical_size = SkISize::Make(paint_bounds.width() * scale_x,
                                        paint_bounds.height() * scale_y);
  if (physical_size.isEmpty())
    return nullptr;
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
  // Acquire a surface from the surface producer and register the paint tasks.
  auto surface = surface_producer_->ProduceSurface(physical_size);

  if (!surface) {
    FTL_LOG(ERROR) << "Could not acquire a surface from the surface producer "
                      "of size: "
                   << physical_size.width() << "x" << physical_size.height();
    return nullptr;
  }

  auto image = surface->GetImage();

  // Enqueue the paint task.
  paint_tasks_.push_back({.surface = std::move(surface),
                          .left = paint_bounds.left(),
                          .top = paint_bounds.top(),
                          .scale_x = scale_x,
                          .scale_y = scale_y,
                          .background_color = color,
                          .layers = std::move(paint_layers)});
  return image;
}

std::vector<std::unique_ptr<flow::SceneUpdateContext::SurfaceProducerSurface>>
SceneUpdateContext::ExecutePaintTasks(CompositorContext::ScopedFrame& frame) {
  TRACE_EVENT0("flutter", "SceneUpdateContext::ExecutePaintTasks");
  std::vector<std::unique_ptr<SurfaceProducerSurface>> surfaces_to_submit;
195 196
  for (auto& task : paint_tasks_) {
    FTL_DCHECK(task.surface);
197
    SkCanvas* canvas = task.surface->GetSkiaSurface()->getCanvas();
198
    Layer::PaintContext context = {*canvas, frame.context().frame_time(),
A
Adam Barth 已提交
199
                                   frame.context().engine_time(),
200 201 202 203 204
                                   frame.context().memory_usage(), false};
    canvas->restoreToCount(1);
    canvas->save();
    canvas->clear(task.background_color);
    canvas->scale(task.scale_x, task.scale_y);
205
    canvas->translate(-task.left, -task.top);
206
    for (Layer* layer : task.layers) {
207
      layer->Paint(context);
208 209
    }
    surfaces_to_submit.emplace_back(std::move(task.surface));
210 211
  }
  paint_tasks_.clear();
212
  return surfaces_to_submit;
213 214
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
SceneUpdateContext::Entity::Entity(SceneUpdateContext& context)
    : context_(context),
      previous_entity_(context.top_entity_),
      entity_node_(context.session()) {
  if (previous_entity_)
    previous_entity_->entity_node_.AddChild(entity_node_);
  context.top_entity_ = this;
}

SceneUpdateContext::Entity::~Entity() {
  FTL_DCHECK(context_.top_entity_ == this);
  context_.top_entity_ = previous_entity_;
}

SceneUpdateContext::Clip::Clip(SceneUpdateContext& context,
230
                               scenic_lib::Shape& shape,
231 232
                               const SkRect& shape_bounds)
    : Entity(context) {
233
  scenic_lib::ShapeNode shape_node(context.session());
234 235 236 237 238 239 240 241 242 243
  shape_node.SetShape(shape);
  shape_node.SetTranslation(shape_bounds.width() * 0.5f + shape_bounds.left(),
                            shape_bounds.height() * 0.5f + shape_bounds.top(),
                            0.f);

  entity_node().AddPart(shape_node);
  entity_node().SetClip(0u, true /* clip to self */);
}

SceneUpdateContext::Clip::~Clip() = default;
244

245 246
SceneUpdateContext::Transform::Transform(SceneUpdateContext& context,
                                         const SkMatrix& transform)
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
    : Entity(context),
      previous_scale_x_(context.top_scale_x_),
      previous_scale_y_(context.top_scale_y_) {
  if (!transform.isIdentity()) {
    // TODO(MZ-192): The perspective and shear components in the matrix
    // are not handled correctly.
    MatrixDecomposition decomposition(transform);
    if (decomposition.IsValid()) {
      entity_node().SetTranslation(decomposition.translation().x(),  //
                                   decomposition.translation().y(),  //
                                   decomposition.translation().z()   //
                                   );

      entity_node().SetScale(decomposition.scale().x(),  //
                             decomposition.scale().y(),  //
                             decomposition.scale().z()   //
                             );
      context.top_scale_x_ *= decomposition.scale().x();
      context.top_scale_y_ *= decomposition.scale().y();

      entity_node().SetRotation(decomposition.rotation().fData[0],  //
                                decomposition.rotation().fData[1],  //
                                decomposition.rotation().fData[2],  //
                                decomposition.rotation().fData[3]   //
                                );
    }
  }
}

SceneUpdateContext::Transform::Transform(SceneUpdateContext& context,
                                         float scale_x,
                                         float scale_y,
                                         float scale_z)
    : Entity(context),
      previous_scale_x_(context.top_scale_x_),
      previous_scale_y_(context.top_scale_y_) {
  if (scale_x != 1.f || scale_y != 1.f || scale_z != 1.f) {
    entity_node().SetScale(scale_x, scale_y, scale_z);
    context.top_scale_x_ *= scale_x;
    context.top_scale_y_ *= scale_y;
287 288 289
  }
}

290 291 292 293
SceneUpdateContext::Transform::~Transform() {
  context().top_scale_x_ = previous_scale_x_;
  context().top_scale_y_ = previous_scale_y_;
}
294 295 296 297

SceneUpdateContext::Frame::Frame(SceneUpdateContext& context,
                                 const SkRRect& rrect,
                                 SkColor color,
298
                                 float elevation)
299 300 301 302
    : Entity(context),
      rrect_(rrect),
      color_(color),
      paint_bounds_(SkRect::MakeEmpty()) {
303 304
  if (elevation != 0.0)
    entity_node().SetTranslation(0.f, 0.f, elevation);
305 306 307
}

SceneUpdateContext::Frame::~Frame() {
308 309
  context().CreateFrame(entity_node(), rrect_, color_, paint_bounds_,
                        std::move(paint_layers_));
310 311 312 313 314 315 316 317 318
}

void SceneUpdateContext::Frame::AddPaintedLayer(Layer* layer) {
  FTL_DCHECK(layer->needs_painting());
  paint_layers_.push_back(layer);
  paint_bounds_.join(layer->paint_bounds());
}

}  // namespace flow