Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / gpu / skia_runner / in_process_graphics_system.cc
blobe5294b1777fb78580d12411640352d5016988893
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"
7 #include <iostream>
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 "gpu/command_buffer/client/gles2_implementation.h"
14 #include "gpu/command_buffer/client/gles2_lib.h"
15 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
16 #include "third_party/khronos/GLES2/gl2.h"
17 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
19 namespace {
21 // TODO(hendrikw): Replace TestDiscardableMemoryAllocator and move to base?
22 class NonDiscardableMemory : public base::DiscardableMemory {
23 public:
24 explicit NonDiscardableMemory(size_t size) : data_(new uint8_t[size]) {}
25 bool Lock() override { return false; }
26 void Unlock() override {}
27 void* data() const override { return data_.get(); }
29 private:
30 scoped_ptr<uint8_t[]> data_;
33 class NonDiscardableMemoryAllocator : public base::DiscardableMemoryAllocator {
34 public:
35 // Overridden from DiscardableMemoryAllocator:
36 scoped_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory(
37 size_t size) override {
38 return make_scoped_ptr(new NonDiscardableMemory(size));
42 // Singleton used to initialize and terminate the gles2 library.
43 class GLES2Initializer {
44 public:
45 GLES2Initializer() { gles2::Initialize(); }
46 ~GLES2Initializer() { gles2::Terminate(); }
48 private:
49 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer);
52 base::LazyInstance<GLES2Initializer> g_gles2_initializer =
53 LAZY_INSTANCE_INITIALIZER;
54 base::LazyInstance<NonDiscardableMemoryAllocator> g_discardable;
56 } // namespace anonymous
58 namespace skia_runner {
60 void BindContext3DGLContextCallback(const GrGLInterface* gl_interface) {
61 gles2::SetGLContext(reinterpret_cast<gpu::GLInProcessContext*>(
62 gl_interface->fCallbackData)->GetImplementation());
65 InProcessGraphicsSystem::InProcessGraphicsSystem() {
66 base::DiscardableMemoryAllocator::SetInstance(&g_discardable.Get());
67 g_gles2_initializer.Get();
69 if (!gfx::GLSurface::InitializeOneOff()) {
70 LOG(ERROR) << "Unable to initialize gfx::GLSurface.";
71 return;
74 // Create the in process context.
75 in_process_context_ = CreateInProcessContext();
76 if (!in_process_context_) {
77 LOG(ERROR) << "Unable to create in process context.";
78 return;
81 // Create and set up skia's GL bindings.
82 gles2::SetGLContext(in_process_context_->GetImplementation());
83 GrGLInterface* bindings = skia_bindings::CreateCommandBufferSkiaGLBinding();
84 if (!bindings) {
85 LOG(ERROR) << "Unable to create skia command buffer bindings.";
86 return;
89 bindings->fCallback = BindContext3DGLContextCallback;
90 bindings->fCallbackData =
91 reinterpret_cast<GrGLInterfaceCallbackData>(in_process_context_.get());
93 // Create skia's graphics context.
94 gr_context_ = skia::AdoptRef(GrContext::Create(
95 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(bindings)));
97 if (!gr_context_) {
98 LOG(ERROR) << "Unable to create skia graphics context.";
99 return;
102 // Set skia's graphics context cache size.
103 const int kMaxGaneshResourceCacheCount = 2048;
104 const size_t kMaxGaneshResourceCacheBytes = 96 * 1024 * 1024;
105 gr_context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount,
106 kMaxGaneshResourceCacheBytes);
109 InProcessGraphicsSystem::~InProcessGraphicsSystem() {
112 bool InProcessGraphicsSystem::IsSuccessfullyInitialized() const {
113 return gr_context_;
116 int InProcessGraphicsSystem::GetMaxTextureSize() const {
117 int max_texture_size = 0;
118 in_process_context_->GetImplementation()->GetIntegerv(GL_MAX_TEXTURE_SIZE,
119 &max_texture_size);
120 return max_texture_size;
123 scoped_ptr<gpu::GLInProcessContext>
124 InProcessGraphicsSystem::CreateInProcessContext() const {
125 const bool is_offscreen = true;
126 const bool share_resources = true;
127 gpu::gles2::ContextCreationAttribHelper attribs;
128 attribs.alpha_size = 8;
129 attribs.blue_size = 8;
130 attribs.green_size = 8;
131 attribs.red_size = 8;
132 attribs.depth_size = 24;
133 attribs.stencil_size = 8;
134 attribs.samples = 0;
135 attribs.sample_buffers = 0;
136 attribs.fail_if_major_perf_caveat = false;
137 attribs.bind_generates_resource = false;
138 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
140 scoped_ptr<gpu::GLInProcessContext> context =
141 make_scoped_ptr(gpu::GLInProcessContext::Create(
142 nullptr, nullptr, is_offscreen, gfx::kNullAcceleratedWidget,
143 gfx::Size(1, 1), nullptr, share_resources, attribs, gpu_preference,
144 gpu::GLInProcessContextSharedMemoryLimits(), nullptr, nullptr));
146 DCHECK(context);
147 return context.Pass();
150 } // namespace skia_runner