io_manager.cc 2.7 KB
Newer Older
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
// Copyright 2017 The Flutter 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/shell/common/io_manager.h"

#include "flutter/fml/message_loop.h"
#include "third_party/skia/include/gpu/gl/GrGLInterface.h"

namespace shell {

sk_sp<GrContext> IOManager::CreateCompatibleResourceLoadingContext(
    GrBackend backend) {
  if (backend != GrBackend::kOpenGL_GrBackend) {
    return nullptr;
  }

  GrContextOptions options = {};

  // There is currently a bug with doing GPU YUV to RGB conversions on the IO
  // thread. The necessary work isn't being flushed or synchronized with the
  // other threads correctly, so the textures end up blank.  For now, suppress
  // that feature, which will cause texture uploads to do CPU YUV conversion.
  options.fDisableGpuYUVConversion = true;

26 27 28 29
  // To get video playback on the widest range of devices, we limit Skia to
  // ES2 shading language when the ES3 external image extension is missing.
  options.fPreferExternalImagesOverES3 = true;

30 31 32 33 34 35 36 37 38 39 40
  if (auto context = GrContext::MakeGL(GrGLMakeNativeInterface(), options)) {
    // Do not cache textures created by the image decoder.  These textures
    // should be deleted when they are no longer referenced by an SkImage.
    context->setResourceCacheLimits(0, 0);
    return context;
  }

  return nullptr;
}

IOManager::IOManager(sk_sp<GrContext> resource_context,
41
                     fml::RefPtr<fml::TaskRunner> unref_queue_task_runner)
42 43 44 45 46
    : resource_context_(std::move(resource_context)),
      resource_context_weak_factory_(
          resource_context_ ? std::make_unique<fml::WeakPtrFactory<GrContext>>(
                                  resource_context_.get())
                            : nullptr),
47
      unref_queue_(fml::MakeRefCounted<flow::SkiaUnrefQueue>(
48
          std::move(unref_queue_task_runner),
49
          fml::TimeDelta::FromMilliseconds(250))),
50 51
      weak_factory_(this) {
  if (!resource_context_) {
52
    FML_DLOG(WARNING) << "The IO manager was initialized without a resource "
53 54 55 56 57 58 59 60 61 62 63 64
                         "context. Async texture uploads will be disabled. "
                         "Expect performance degradation.";
  }
}

IOManager::~IOManager() {
  // Last chance to drain the IO queue as the platform side reference to the
  // underlying OpenGL context may be going away.
  unref_queue_->Drain();
}

fml::WeakPtr<GrContext> IOManager::GetResourceContext() const {
65 66 67
  return resource_context_weak_factory_
             ? resource_context_weak_factory_->GetWeakPtr()
             : fml::WeakPtr<GrContext>();
68 69
}

70
fml::RefPtr<flow::SkiaUnrefQueue> IOManager::GetSkiaUnrefQueue() const {
71 72 73 74
  return unref_queue_;
}

}  // namespace shell