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/android/sys_utils.h"
8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/sys_info.h"
11 #include "ui/gl/gl_bindings.h"
12 #include "ui/gl/gl_context_egl.h"
13 #include "ui/gl/gl_context_stub.h"
14 #include "ui/gl/gl_implementation.h"
15 #include "ui/gl/gl_surface.h"
21 // Used to render into an already current context+surface,
22 // that we do not have ownership of (draw callback).
23 // TODO(boliu): Make this inherit from GLContextEGL.
24 class GLNonOwnedContext : public GLContextReal {
26 GLNonOwnedContext(GLShareGroup* share_group);
28 // Implement GLContext.
29 virtual bool Initialize(GLSurface* compatible_surface,
30 GpuPreference gpu_preference) OVERRIDE;
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;
40 virtual ~GLNonOwnedContext() {}
43 DISALLOW_COPY_AND_ASSIGN(GLNonOwnedContext);
48 GLNonOwnedContext::GLNonOwnedContext(GLShareGroup* share_group)
49 : GLContextReal(share_group), display_(NULL) {}
51 bool GLNonOwnedContext::Initialize(GLSurface* compatible_surface,
52 GpuPreference gpu_preference) {
53 display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
57 bool GLNonOwnedContext::MakeCurrent(GLSurface* surface) {
63 std::string GLNonOwnedContext::GetExtensions() {
64 const char* extensions = eglQueryString(display_, EGL_EXTENSIONS);
66 return GLContext::GetExtensions();
68 return GLContext::GetExtensions() + " " + extensions;
71 } // anonymous namespace
74 scoped_refptr<GLContext> GLContext::CreateGLContext(
75 GLShareGroup* share_group,
76 GLSurface* compatible_surface,
77 GpuPreference gpu_preference) {
78 if (GetGLImplementation() == kGLImplementationMockGL)
79 return scoped_refptr<GLContext>(new GLContextStub());
81 scoped_refptr<GLContext> context;
82 if (compatible_surface->GetHandle())
83 context = new GLContextEGL(share_group);
85 context = new GLNonOwnedContext(share_group);
86 if (!context->Initialize(compatible_surface, gpu_preference))
91 bool GLContextEGL::GetTotalGpuMemory(size_t* bytes) {
95 // We can't query available GPU memory from the system on Android.
96 // Physical memory is also mis-reported sometimes (eg. Nexus 10 reports
97 // 1262MB when it actually has 2GB, while Razr M has 1GB but only reports
98 // 128MB java heap size). First we estimate physical memory using both.
99 size_t dalvik_mb = base::SysInfo::DalvikHeapSizeMB();
100 size_t physical_mb = base::SysInfo::AmountOfPhysicalMemoryMB();
101 size_t physical_memory_mb = 0;
102 if (dalvik_mb >= 256)
103 physical_memory_mb = dalvik_mb * 4;
105 physical_memory_mb = std::max(dalvik_mb * 4,
106 (physical_mb * 4) / 3);
108 // Now we take a default of 1/8th of memory on high-memory devices,
109 // and gradually scale that back for low-memory devices (to be nicer
110 // to other apps so they don't get killed). Examples:
111 // Nexus 4/10(2GB) 256MB
112 // Droid Razr M(1GB) 91MB
113 // Galaxy Nexus(1GB) 85MB
115 // Nexus S(low-end) 8MB
116 static size_t limit_bytes = 0;
117 if (limit_bytes == 0) {
118 if (!base::android::SysUtils::IsLowEndDevice()) {
119 if (physical_memory_mb >= 1536)
120 limit_bytes = physical_memory_mb / 8;
121 else if (physical_memory_mb >= 1152)
122 limit_bytes = physical_memory_mb / 10;
123 else if (physical_memory_mb >= 768)
124 limit_bytes = physical_memory_mb / 12;
126 limit_bytes = physical_memory_mb / 16;
128 // Low-end devices have 512MB or less memory by definition
129 // so we hard code the limit rather than relying on the heuristics
130 // above. Low-end devices use 4444 textures so we can use a lower limit.
133 limit_bytes = limit_bytes * 1024 * 1024;
135 *bytes = limit_bytes;