rasterizer.cc 3.3 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 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 "sky/shell/gpu/rasterizer.h"

A
Adam Barth 已提交
7
#include "base/trace_event/trace_event.h"
8
#include "sky/compositor/layer.h"
9 10
#include "sky/shell/gpu/ganesh_context.h"
#include "sky/shell/gpu/ganesh_surface.h"
11
#include "sky/shell/gpu/picture_serializer.h"
12 13 14 15 16 17 18
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_share_group.h"
#include "ui/gl/gl_surface.h"

19 20 21
// Set this value to 1 to serialize the layer tree to disk.
#define SERIALIZE_LAYER_TREE 0

22 23
namespace sky {
namespace shell {
24 25 26 27 28 29 30 31 32 33 34 35 36 37
namespace {

#if SERIALIZE_LAYER_TREE

void SketchySerializeLayerTree(const char* path, LayerTree* layer_tree) {
  const auto& layers = static_cast<ContainerLayer*>(layer_tree->root_layer())->layers();
  if (layers.empty())
    return;
  SerializePicture(path, static_cast<PictureLayer*>(layers[0].get())->picture());
}

#endif

}  // namespace
38

A
Adam Barth 已提交
39 40
Rasterizer::Rasterizer()
    : share_group_(new gfx::GLShareGroup()), weak_factory_(this) {
41 42 43 44 45 46 47 48 49
}

Rasterizer::~Rasterizer() {
}

base::WeakPtr<Rasterizer> Rasterizer::GetWeakPtr() {
  return weak_factory_.GetWeakPtr();
}

50
void Rasterizer::OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) {
51 52
  surface_ = gfx::GLSurface::CreateViewGLSurface(widget,
                                                 gfx::SurfaceConfiguration());
53 54 55
  CHECK(surface_) << "GLSurface required.";
}

56
void Rasterizer::Draw(scoped_ptr<compositor::LayerTree> layer_tree) {
A
Adam Barth 已提交
57 58
  TRACE_EVENT0("sky", "Rasterizer::Draw");

A
Adam Barth 已提交
59 60 61
  if (!surface_)
    return;

62 63 64
  gfx::Size size(layer_tree->frame_size().width(),
                 layer_tree->frame_size().height());

A
Adam Barth 已提交
65 66 67
  if (surface_->GetSize() != size)
    surface_->Resize(size);

A
Adam Barth 已提交
68
  EnsureGLContext();
69
  CHECK(context_->MakeCurrent(surface_.get()));
70
  EnsureGaneshSurface(surface_->GetBackingFrameBufferObject(), size);
71
  SkCanvas* canvas = ganesh_surface_->canvas();
72

73
  canvas->clear(SK_ColorBLACK);
74
  layer_tree->root_layer()->Paint(ganesh_context_->gr(), canvas);
75
  canvas->flush();
A
Adam Barth 已提交
76
  surface_->SwapBuffers();
77

78 79 80
#if SERIALIZE_LAYER_TREE
  SketchySerializeLayerTree("/data/data/org.domokit.sky.shell/cache/layer0.skp", layer_tree.get());
#endif
A
Adam Barth 已提交
81 82
}

83
void Rasterizer::OnOutputSurfaceDestroyed() {
E
Eric Seidel 已提交
84 85 86 87 88 89 90 91 92
  if (context_) {
    CHECK(context_->MakeCurrent(surface_.get()));
    ganesh_surface_.reset();
    ganesh_context_.reset();
    context_ = nullptr;
  }
  CHECK(!ganesh_surface_);
  CHECK(!ganesh_context_);
  CHECK(!context_);
A
Adam Barth 已提交
93
  surface_ = nullptr;
94 95
}

A
Adam Barth 已提交
96 97 98
void Rasterizer::EnsureGLContext() {
  if (context_)
    return;
99 100
  context_ = gfx::GLContext::CreateGLContext(share_group_.get(), surface_.get(),
                                             gfx::PreferIntegratedGpu);
A
Adam Barth 已提交
101 102
  CHECK(context_) << "GLContext required.";
  CHECK(context_->MakeCurrent(surface_.get()));
103 104 105
  ganesh_context_.reset(new GaneshContext(context_.get()));
}

106 107
void Rasterizer::EnsureGaneshSurface(intptr_t window_fbo,
                                     const gfx::Size& size) {
108
  if (!ganesh_surface_ || ganesh_surface_->size() != size)
109 110
    ganesh_surface_.reset(
      new GaneshSurface(window_fbo, ganesh_context_.get(), size));
111 112 113 114
}

}  // namespace shell
}  // namespace sky