Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gl / gl_context_android.cc
blobebf1dda6b13a8486e7f5c60acb74140da8a7e7ba
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_osmesa.h"
13 #include "ui/gl/gl_context_stub.h"
14 #include "ui/gl/gl_implementation.h"
15 #include "ui/gl/gl_surface.h"
17 namespace gfx {
19 namespace {
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 {
25 public:
26 GLNonOwnedContext(GLShareGroup* share_group);
28 // Implement GLContext.
29 bool Initialize(GLSurface* compatible_surface,
30 GpuPreference gpu_preference) override;
31 bool MakeCurrent(GLSurface* surface) override;
32 void ReleaseCurrent(GLSurface* surface) override {}
33 bool IsCurrent(GLSurface* surface) override { return true; }
34 void* GetHandle() override { return nullptr; }
35 void OnSetSwapInterval(int interval) override {}
36 std::string GetExtensions() override;
38 protected:
39 ~GLNonOwnedContext() override {}
41 private:
42 EGLDisplay display_;
44 DISALLOW_COPY_AND_ASSIGN(GLNonOwnedContext);
47 GLNonOwnedContext::GLNonOwnedContext(GLShareGroup* share_group)
48 : GLContextReal(share_group), display_(nullptr) {}
50 bool GLNonOwnedContext::Initialize(GLSurface* compatible_surface,
51 GpuPreference gpu_preference) {
52 display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
53 return true;
56 bool GLNonOwnedContext::MakeCurrent(GLSurface* surface) {
57 SetCurrent(surface);
58 SetRealGLApi();
59 return true;
62 std::string GLNonOwnedContext::GetExtensions() {
63 const char* extensions = eglQueryString(display_, EGL_EXTENSIONS);
64 if (!extensions)
65 return GLContext::GetExtensions();
67 return GLContext::GetExtensions() + " " + extensions;
70 } // anonymous namespace
72 // static
73 scoped_refptr<GLContext> GLContext::CreateGLContext(
74 GLShareGroup* share_group,
75 GLSurface* compatible_surface,
76 GpuPreference gpu_preference) {
77 scoped_refptr<GLContext> context;
78 switch (GetGLImplementation()) {
79 case kGLImplementationMockGL:
80 return scoped_refptr<GLContext>(new GLContextStub());
81 case kGLImplementationOSMesaGL:
82 context = new GLContextOSMesa(share_group);
83 break;
84 default:
85 if (compatible_surface->GetHandle())
86 context = new GLContextEGL(share_group);
87 else
88 context = new GLNonOwnedContext(share_group);
89 break;
92 if (!context->Initialize(compatible_surface, gpu_preference))
93 return nullptr;
95 return context;
98 bool GLContextEGL::GetTotalGpuMemory(size_t* bytes) {
99 DCHECK(bytes);
100 *bytes = 0;
102 // We can't query available GPU memory from the system on Android.
103 // Physical memory is also mis-reported sometimes (eg. Nexus 10 reports
104 // 1262MB when it actually has 2GB, while Razr M has 1GB but only reports
105 // 128MB java heap size). First we estimate physical memory using both.
106 size_t dalvik_mb = base::SysInfo::DalvikHeapSizeMB();
107 size_t physical_mb = base::SysInfo::AmountOfPhysicalMemoryMB();
108 size_t physical_memory_mb = 0;
109 if (dalvik_mb >= 256)
110 physical_memory_mb = dalvik_mb * 4;
111 else
112 physical_memory_mb = std::max(dalvik_mb * 4,
113 (physical_mb * 4) / 3);
115 // Now we take a default of 1/8th of memory on high-memory devices,
116 // and gradually scale that back for low-memory devices (to be nicer
117 // to other apps so they don't get killed). Examples:
118 // Nexus 4/10(2GB) 256MB (normally 128MB)
119 // Droid Razr M(1GB) 114MB (normally 57MB)
120 // Galaxy Nexus(1GB) 100MB (normally 50MB)
121 // Xoom(1GB) 100MB (normally 50MB)
122 // Nexus S(low-end) 8MB (normally 8MB)
123 // Note that the compositor now uses only some of this memory for
124 // pre-painting and uses the rest only for 'emergencies'.
125 static size_t limit_bytes = 0;
126 if (limit_bytes == 0) {
127 // NOTE: Non-low-end devices use only 50% of these limits,
128 // except during 'emergencies' where 100% can be used.
129 if (!base::SysInfo::IsLowEndDevice()) {
130 if (physical_memory_mb >= 1536)
131 limit_bytes = physical_memory_mb / 8; // >192MB
132 else if (physical_memory_mb >= 1152)
133 limit_bytes = physical_memory_mb / 8; // >144MB
134 else if (physical_memory_mb >= 768)
135 limit_bytes = physical_memory_mb / 10; // >76MB
136 else
137 limit_bytes = physical_memory_mb / 12; // <64MB
138 } else {
139 // Low-end devices have 512MB or less memory by definition
140 // so we hard code the limit rather than relying on the heuristics
141 // above. Low-end devices use 4444 textures so we can use a lower limit.
142 limit_bytes = 8;
144 limit_bytes = limit_bytes * 1024 * 1024;
146 *bytes = limit_bytes;
147 return true;