Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ui / gl / gl_context_android.cc
blob846433c3f1439f37bec3c272641bc1a9675919a4
1 // Copyright (c) 2012 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 "ui/gl/gl_context.h"
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/sys_info.h"
10 #include "ui/gl/gl_bindings.h"
11 #include "ui/gl/gl_context_egl.h"
12 #include "ui/gl/gl_context_stub.h"
13 #include "ui/gl/gl_implementation.h"
14 #include "ui/gl/gl_surface.h"
16 namespace gfx {
18 namespace {
20 // Used to render into an already current context+surface,
21 // that we do not have ownership of (draw callback).
22 class GLNonOwnedContext : public GLContext {
23 public:
24 GLNonOwnedContext();
26 // Implement GLContext.
27 virtual bool Initialize(GLSurface* compatible_surface,
28 GpuPreference gpu_preference) OVERRIDE {
29 return true;
31 virtual void Destroy() OVERRIDE {}
32 virtual bool MakeCurrent(GLSurface* surface) OVERRIDE;
33 virtual void ReleaseCurrent(GLSurface* surface) OVERRIDE {}
34 virtual bool IsCurrent(GLSurface* surface) OVERRIDE { return true; }
35 virtual void* GetHandle() OVERRIDE { return NULL; }
36 virtual void SetSwapInterval(int interval) OVERRIDE {}
37 virtual std::string GetExtensions() OVERRIDE;
39 protected:
40 virtual ~GLNonOwnedContext() {}
42 private:
43 DISALLOW_COPY_AND_ASSIGN(GLNonOwnedContext);
46 GLNonOwnedContext::GLNonOwnedContext() : GLContext(NULL) {}
48 bool GLNonOwnedContext::MakeCurrent(GLSurface* surface) {
49 SetCurrent(this, surface);
50 SetRealGLApi();
51 return true;
54 std::string GLNonOwnedContext::GetExtensions() {
55 return GLContext::GetExtensions();
58 } // anonymous namespace
60 // static
61 scoped_refptr<GLContext> GLContext::CreateGLContext(
62 GLShareGroup* share_group,
63 GLSurface* compatible_surface,
64 GpuPreference gpu_preference) {
65 if (GetGLImplementation() == kGLImplementationMockGL)
66 return scoped_refptr<GLContext>(new GLContextStub());
68 scoped_refptr<GLContext> context;
69 if (compatible_surface->GetHandle())
70 context = new GLContextEGL(share_group);
71 else
72 context = new GLNonOwnedContext();
73 if (!context->Initialize(compatible_surface, gpu_preference))
74 return NULL;
75 return context;
78 bool GLContextEGL::GetTotalGpuMemory(size_t* bytes) {
79 DCHECK(bytes);
80 *bytes = 0;
81 // We can't query available GPU memory from the system on Android,
82 // but the dalvik heap size give us a good estimate of available
83 // GPU memory on a wide range of devices.
85 // The heap size tends to be about 1/4 of total ram on higher end
86 // devices, so we use 1/2 of that by default. For example both the
87 // Nexus 4/10 have 2GB of ram and 512MB Dalvik heap size. For lower
88 // end devices, 1/2 of the heap size can be too high, but this
89 // correlates well with having a small heap-growth-limit. So for
90 // devices with less ram, we factor in the growth limit.
92 // This is the result of the calculation below:
93 // Droid DNA 1080P 128MB
94 // Nexus S 56MB
95 // Galaxy Nexus 112MB
96 // Nexus 4/10 256MB
97 // Xoom 88MB
98 size_t dalvik_limit = 0;
99 if (!dalvik_limit) {
100 size_t heap_size = static_cast<size_t>(base::SysInfo::DalvikHeapSizeMB());
101 size_t heap_growth = static_cast<size_t>(
102 base::SysInfo::DalvikHeapGrowthLimitMB());
103 size_t limit = 0;
104 if (heap_size >= 350)
105 limit = heap_size / 2;
106 else
107 limit = (heap_size + (heap_growth * 2)) / 4;
108 dalvik_limit = limit * 1024 * 1024;
110 *bytes = dalvik_limit;
111 return true;