summaryrefslogtreecommitdiffstats
path: root/content/common/gpu/client/grcontext_for_webgraphicscontext3d.cc
blob: 5cd4944db68ed071ebbfd9efee8ca6da08f02562 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2013 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 "content/common/gpu/client/grcontext_for_webgraphicscontext3d.h"

#include <stddef.h>
#include <string.h>
#include <utility>

#include "base/lazy_instance.h"
#include "base/macros.h"
#include "base/trace_event/trace_event.h"
#include "gpu/blink/webgraphicscontext3d_impl.h"
#include "gpu/command_buffer/client/gles2_lib.h"
#include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
#include "third_party/skia/include/gpu/GrContext.h"

using gpu_blink::WebGraphicsContext3DImpl;

namespace content {

namespace {

// Singleton used to initialize and terminate the gles2 library.
class GLES2Initializer {
 public:
  GLES2Initializer() { gles2::Initialize(); }

  ~GLES2Initializer() { gles2::Terminate(); }

 private:
  DISALLOW_COPY_AND_ASSIGN(GLES2Initializer);
};

base::LazyInstance<GLES2Initializer> g_gles2_initializer =
    LAZY_INSTANCE_INITIALIZER;

void BindWebGraphicsContext3DGLContextCallback(const GrGLInterface* interface) {
  gles2::SetGLContext(static_cast<const GrGLInterfaceForWebGraphicsContext3D*>(
                          interface)->WebContext3D()->GetGLInterface());
}

}  // namespace anonymous

GrContextForWebGraphicsContext3D::GrContextForWebGraphicsContext3D(
    skia::RefPtr<GrGLInterfaceForWebGraphicsContext3D> gr_interface) {
  if (!gr_interface || !gr_interface->WebContext3D())
    return;

  // Ensure the gles2 library is initialized first in a thread safe way.
  g_gles2_initializer.Get();
  gles2::SetGLContext(gr_interface->WebContext3D()->GetGLInterface());

  skia_bindings::InitCommandBufferSkiaGLBinding(gr_interface.get());

  gr_interface->fCallback = BindWebGraphicsContext3DGLContextCallback;

  gr_context_ = skia::AdoptRef(GrContext::Create(
      kOpenGL_GrBackend,
      reinterpret_cast<GrBackendContext>(gr_interface.get())));
  if (gr_context_) {
    // The limit of the number of GPU resources we hold in the GrContext's
    // GPU cache.
    static const int kMaxGaneshResourceCacheCount = 2048;
    // The limit of the bytes allocated toward GPU resources in the GrContext's
    // GPU cache.
    static const size_t kMaxGaneshResourceCacheBytes = 96 * 1024 * 1024;

    gr_context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount,
                                        kMaxGaneshResourceCacheBytes);
  }
}

GrContextForWebGraphicsContext3D::~GrContextForWebGraphicsContext3D() {
}

void GrContextForWebGraphicsContext3D::OnLostContext() {
  if (gr_context_)
    gr_context_->abandonContext();
}

void GrContextForWebGraphicsContext3D::FreeGpuResources() {
  if (gr_context_) {
    TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", \
        TRACE_EVENT_SCOPE_THREAD);
    gr_context_->freeGpuResources();
  }
}

GrGLInterfaceForWebGraphicsContext3D::GrGLInterfaceForWebGraphicsContext3D(
    scoped_ptr<gpu_blink::WebGraphicsContext3DImpl> context3d)
    : context3d_(std::move(context3d)) {}

void GrGLInterfaceForWebGraphicsContext3D::BindToCurrentThread() {
  context_thread_checker_.DetachFromThread();
}

GrGLInterfaceForWebGraphicsContext3D::~GrGLInterfaceForWebGraphicsContext3D() {
  DCHECK(context_thread_checker_.CalledOnValidThread());
#if !defined(NDEBUG)
  // Set all the function pointers to zero, in order to crash if function
  // pointers are used after free.
  memset(&fFunctions, 0, sizeof(GrGLInterface::Functions));
#endif
}

}  // namespace content