1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gpu/skia_runner/in_process_graphics_system.h"
9 #include "base/lazy_instance.h"
10 #include "base/memory/discardable_memory.h"
11 #include "base/memory/discardable_memory_allocator.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/trace_event/memory_allocator_dump.h"
14 #include "base/trace_event/memory_dump_manager.h"
15 #include "base/trace_event/process_memory_dump.h"
16 #include "gpu/command_buffer/client/gles2_implementation.h"
17 #include "gpu/command_buffer/client/gles2_lib.h"
18 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
19 #include "third_party/khronos/GLES2/gl2.h"
20 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
24 // TODO(hendrikw): Replace TestDiscardableMemoryAllocator and move to base?
25 class NonDiscardableMemory
: public base::DiscardableMemory
{
27 explicit NonDiscardableMemory(size_t size
)
28 : data_(new uint8_t[size
]), size_(size
) {}
29 bool Lock() override
{ return false; }
30 void Unlock() override
{}
31 void* data() const override
{ return data_
.get(); }
33 base::trace_event::MemoryAllocatorDump
* CreateMemoryAllocatorDump(
35 base::trace_event::ProcessMemoryDump
* pmd
) const override
{
36 base::trace_event::MemoryAllocatorDump
* dump
=
37 pmd
->CreateAllocatorDump(name
);
38 dump
->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize
,
39 base::trace_event::MemoryAllocatorDump::kUnitsBytes
, size_
);
41 // Memory is allocated from system allocator (malloc).
42 pmd
->AddSuballocation(dump
->guid(),
43 base::trace_event::MemoryDumpManager::GetInstance()
44 ->system_allocator_pool_name());
49 scoped_ptr
<uint8_t[]> data_
;
53 class NonDiscardableMemoryAllocator
: public base::DiscardableMemoryAllocator
{
55 // Overridden from DiscardableMemoryAllocator:
56 scoped_ptr
<base::DiscardableMemory
> AllocateLockedDiscardableMemory(
57 size_t size
) override
{
58 return make_scoped_ptr(new NonDiscardableMemory(size
));
62 // Singleton used to initialize and terminate the gles2 library.
63 class GLES2Initializer
{
65 GLES2Initializer() { gles2::Initialize(); }
66 ~GLES2Initializer() { gles2::Terminate(); }
69 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer
);
72 base::LazyInstance
<GLES2Initializer
> g_gles2_initializer
=
73 LAZY_INSTANCE_INITIALIZER
;
74 base::LazyInstance
<NonDiscardableMemoryAllocator
> g_discardable
;
76 } // namespace anonymous
78 namespace skia_runner
{
80 void BindContext3DGLContextCallback(const GrGLInterface
* gl_interface
) {
81 gles2::SetGLContext(reinterpret_cast<gpu::GLInProcessContext
*>(
82 gl_interface
->fCallbackData
)->GetImplementation());
85 InProcessGraphicsSystem::InProcessGraphicsSystem() {
86 base::DiscardableMemoryAllocator::SetInstance(&g_discardable
.Get());
87 g_gles2_initializer
.Get();
89 if (!gfx::GLSurface::InitializeOneOff()) {
90 LOG(ERROR
) << "Unable to initialize gfx::GLSurface.";
94 // Create the in process context.
95 in_process_context_
= CreateInProcessContext();
96 if (!in_process_context_
) {
97 LOG(ERROR
) << "Unable to create in process context.";
101 // Create and set up skia's GL bindings.
102 gles2::SetGLContext(in_process_context_
->GetImplementation());
103 GrGLInterface
* bindings
= skia_bindings::CreateCommandBufferSkiaGLBinding();
105 LOG(ERROR
) << "Unable to create skia command buffer bindings.";
109 bindings
->fCallback
= BindContext3DGLContextCallback
;
110 bindings
->fCallbackData
=
111 reinterpret_cast<GrGLInterfaceCallbackData
>(in_process_context_
.get());
113 // Create skia's graphics context.
114 gr_context_
= skia::AdoptRef(GrContext::Create(
115 kOpenGL_GrBackend
, reinterpret_cast<GrBackendContext
>(bindings
)));
118 LOG(ERROR
) << "Unable to create skia graphics context.";
122 // Set skia's graphics context cache size.
123 const int kMaxGaneshResourceCacheCount
= 2048;
124 const size_t kMaxGaneshResourceCacheBytes
= 96 * 1024 * 1024;
125 gr_context_
->setResourceCacheLimits(kMaxGaneshResourceCacheCount
,
126 kMaxGaneshResourceCacheBytes
);
129 InProcessGraphicsSystem::~InProcessGraphicsSystem() {
132 bool InProcessGraphicsSystem::IsSuccessfullyInitialized() const {
136 int InProcessGraphicsSystem::GetMaxTextureSize() const {
137 int max_texture_size
= 0;
138 in_process_context_
->GetImplementation()->GetIntegerv(GL_MAX_TEXTURE_SIZE
,
140 return max_texture_size
;
143 scoped_ptr
<gpu::GLInProcessContext
>
144 InProcessGraphicsSystem::CreateInProcessContext() const {
145 const bool is_offscreen
= true;
146 const bool share_resources
= true;
147 gpu::gles2::ContextCreationAttribHelper attribs
;
148 attribs
.alpha_size
= 8;
149 attribs
.blue_size
= 8;
150 attribs
.green_size
= 8;
151 attribs
.red_size
= 8;
152 attribs
.depth_size
= 24;
153 attribs
.stencil_size
= 8;
155 attribs
.sample_buffers
= 0;
156 attribs
.fail_if_major_perf_caveat
= false;
157 attribs
.bind_generates_resource
= false;
158 gfx::GpuPreference gpu_preference
= gfx::PreferDiscreteGpu
;
160 scoped_ptr
<gpu::GLInProcessContext
> context
=
161 make_scoped_ptr(gpu::GLInProcessContext::Create(
162 nullptr, nullptr, is_offscreen
, gfx::kNullAcceleratedWidget
,
163 gfx::Size(1, 1), nullptr, share_resources
, attribs
, gpu_preference
,
164 gpu::GLInProcessContextSharedMemoryLimits(), nullptr, nullptr));
167 return context
.Pass();
170 } // namespace skia_runner