gpu_surface_gl.cc 4.1 KB
Newer Older
1 2 3 4
// 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.

5
#include "gpu_surface_gl.h"
6

7
#include "flutter/flow/gl_connection.h"
8
#include "flutter/glue/trace_event.h"
9 10
#include "lib/ftl/arraysize.h"
#include "lib/ftl/logging.h"
11
#include "third_party/skia/include/core/SkSurface.h"
12 13 14
#include "third_party/skia/include/gpu/GrContext.h"
#include "third_party/skia/include/gpu/gl/GrGLInterface.h"

15 16
namespace shell {

17 18
// Default maximum number of budgeted resources in the cache.
static const int kGrCacheMaxCount = 8192;
19

20 21 22
// Default maximum number of bytes of GPU memory of budgeted resources in the
// cache.
static const size_t kGrCacheMaxByteSize = 512 * (1 << 20);
23

24 25 26 27 28 29 30 31 32 33
GPUSurfaceGL::GPUSurfaceGL(GPUSurfaceGLDelegate* delegate)
    : delegate_(delegate), weak_factory_(this) {}

GPUSurfaceGL::~GPUSurfaceGL() = default;

bool GPUSurfaceGL::Setup() {
  if (delegate_ == nullptr) {
    // Invalid delegate.
    return false;
  }
34 35 36 37 38 39

  if (context_ != nullptr) {
    // Already setup.
    return false;
  }

40 41 42 43 44 45
  if (!delegate_->GLContextMakeCurrent()) {
    // Could not make the context current to create the native interface.
    return false;
  }

  // Create the native interface.
46 47 48 49 50 51 52 53 54 55
  auto backend_context =
      reinterpret_cast<GrBackendContext>(GrGLCreateNativeInterface());

  context_ =
      sk_sp<GrContext>(GrContext::Create(kOpenGL_GrBackend, backend_context));

  if (context_ == nullptr) {
    flow::GLConnection connection;
    FTL_LOG(INFO) << "Failed to setup GL context. Aborting.";
    FTL_LOG(INFO) << connection.Description();
56
    return false;
57 58
  }

59
  context_->setResourceCacheLimits(kGrCacheMaxCount, kGrCacheMaxByteSize);
60

61 62
  delegate_->GLContextClearCurrent();

63 64 65
  return true;
}

66
bool GPUSurfaceGL::IsValid() {
67 68 69
  return context_ != nullptr;
}

70 71 72 73 74
std::unique_ptr<SurfaceFrame> GPUSurfaceGL::AcquireFrame(const SkISize& size) {
  if (delegate_ == nullptr) {
    return nullptr;
  }

75 76 77 78
  if (!delegate_->GLContextMakeCurrent()) {
    return nullptr;
  }

79
  sk_sp<SkSurface> surface = AcquireSurface(size);
80 81 82 83 84

  if (surface == nullptr) {
    return nullptr;
  }

85 86
  auto weak_this = weak_factory_.GetWeakPtr();

87 88
  SurfaceFrame::SubmitCallback submit_callback = [weak_this](
      const SurfaceFrame& surface_frame, SkCanvas* canvas) {
89 90
    return weak_this ? weak_this->PresentSurface(canvas) : false;
  };
91

92
  return std::make_unique<SurfaceFrame>(surface, submit_callback);
93 94 95 96 97 98 99
}

bool GPUSurfaceGL::PresentSurface(SkCanvas* canvas) {
  if (delegate_ == nullptr || canvas == nullptr) {
    return false;
  }

100 101 102 103
  {
    TRACE_EVENT0("flutter", "SkCanvas::Flush");
    canvas->flush();
  }
104 105 106 107

  delegate_->GLContextPresent();

  return true;
108 109
}

110
bool GPUSurfaceGL::SelectPixelConfig(GrPixelConfig* config) {
111 112 113 114 115 116 117 118 119 120 121 122 123 124
  static const GrPixelConfig kConfigOptions[] = {
      kSkia8888_GrPixelConfig, kRGBA_4444_GrPixelConfig,
  };

  for (size_t i = 0; i < arraysize(kConfigOptions); i++) {
    if (context_->caps()->isConfigRenderable(kConfigOptions[i], false)) {
      *config = kConfigOptions[i];
      return true;
    }
  }

  return false;
}

125 126
sk_sp<SkSurface> GPUSurfaceGL::CreateSurface(const SkISize& size) {
  if (delegate_ == nullptr || context_ == nullptr) {
127 128 129 130 131 132 133 134 135 136 137 138 139
    return nullptr;
  }

  GrBackendRenderTargetDesc desc;

  if (!SelectPixelConfig(&desc.fConfig)) {
    return nullptr;
  }

  desc.fWidth = size.width();
  desc.fHeight = size.height();
  desc.fStencilBits = 8;
  desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
140
  desc.fRenderTargetHandle = delegate_->GLContextFBO();
141 142 143 144

  return SkSurface::MakeFromBackendRenderTarget(context_.get(), desc, nullptr);
}

145
sk_sp<SkSurface> GPUSurfaceGL::AcquireSurface(const SkISize& size) {
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  // There is no cached surface.
  if (cached_surface_ == nullptr) {
    cached_surface_ = CreateSurface(size);
    return cached_surface_;
  }

  // There is a surface previously created of the same size.
  if (cached_surface_->width() == size.width() &&
      cached_surface_->height() == size.height()) {
    return cached_surface_;
  }

  cached_surface_ = CreateSurface(size);
  return cached_surface_;
}

162
GrContext* GPUSurfaceGL::GetContext() {
163 164
  return context_.get();
}
165 166

}  // namespace shell