gpu_surface_gl.cc 4.0 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/glue/trace_event.h"
8 9
#include "lib/ftl/arraysize.h"
#include "lib/ftl/logging.h"
10
#include "third_party/skia/include/core/SkSurface.h"
11 12
#include "third_party/skia/include/gpu/gl/GrGLInterface.h"

13 14
namespace shell {

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

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

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

GPUSurfaceGL::~GPUSurfaceGL() = default;

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

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

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

  // Create the native interface.
44 45 46
  auto backend_context =
      reinterpret_cast<GrBackendContext>(GrGLCreateNativeInterface());

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

50
  if (context_ == nullptr) {
51
    FTL_LOG(INFO) << "Failed to setup Skia Gr context.";
52
    return false;
53 54
  }

55
  context_->setResourceCacheLimits(kGrCacheMaxCount, kGrCacheMaxByteSize);
56

57 58
  delegate_->GLContextClearCurrent();

59 60 61
  return true;
}

62
bool GPUSurfaceGL::IsValid() {
63 64 65
  return context_ != nullptr;
}

66 67 68 69 70
std::unique_ptr<SurfaceFrame> GPUSurfaceGL::AcquireFrame(const SkISize& size) {
  if (delegate_ == nullptr) {
    return nullptr;
  }

71 72 73 74
  if (!delegate_->GLContextMakeCurrent()) {
    return nullptr;
  }

75
  sk_sp<SkSurface> surface = AcquireSurface(size);
76 77 78 79 80

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

81 82
  auto weak_this = weak_factory_.GetWeakPtr();

83 84
  SurfaceFrame::SubmitCallback submit_callback = [weak_this](
      const SurfaceFrame& surface_frame, SkCanvas* canvas) {
85 86
    return weak_this ? weak_this->PresentSurface(canvas) : false;
  };
87

88
  return std::make_unique<SurfaceFrame>(surface, submit_callback);
89 90 91 92 93 94 95
}

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

96 97 98 99
  {
    TRACE_EVENT0("flutter", "SkCanvas::Flush");
    canvas->flush();
  }
100 101 102 103

  delegate_->GLContextPresent();

  return true;
104 105
}

106
bool GPUSurfaceGL::SelectPixelConfig(GrPixelConfig* config) {
107 108 109
  static const GrPixelConfig kConfigOptions[] = {
      kSkia8888_GrPixelConfig, kRGBA_4444_GrPixelConfig,
  };
110

111 112 113 114 115
  for (size_t i = 0; i < arraysize(kConfigOptions); i++) {
    if (context_->caps()->isConfigRenderable(kConfigOptions[i], false)) {
      *config = kConfigOptions[i];
      return true;
    }
116 117 118 119 120
  }

  return false;
}

121 122
sk_sp<SkSurface> GPUSurfaceGL::CreateSurface(const SkISize& size) {
  if (delegate_ == nullptr || context_ == nullptr) {
123 124 125 126
    return nullptr;
  }

  GrBackendRenderTargetDesc desc;
127

128 129 130 131 132 133 134 135
  if (!SelectPixelConfig(&desc.fConfig)) {
    return nullptr;
  }

  desc.fWidth = size.width();
  desc.fHeight = size.height();
  desc.fStencilBits = 8;
  desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
136
  desc.fRenderTargetHandle = delegate_->GLContextFBO();
137

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

141
sk_sp<SkSurface> GPUSurfaceGL::AcquireSurface(const SkISize& size) {
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  // 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_;
}

158
GrContext* GPUSurfaceGL::GetContext() {
159 160
  return context_.get();
}
161 162

}  // namespace shell