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"
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
{
26 // Implement GLContext.
27 virtual bool Initialize(GLSurface
* compatible_surface
,
28 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
);
46 GLNonOwnedContext::GLNonOwnedContext() : GLContext(NULL
) {}
48 bool GLNonOwnedContext::MakeCurrent(GLSurface
* surface
) {
49 SetCurrent(this, surface
);
54 std::string
GLNonOwnedContext::GetExtensions() {
55 return GLContext::GetExtensions();
58 } // anonymous namespace
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
);
72 context
= new GLNonOwnedContext();
73 if (!context
->Initialize(compatible_surface
, gpu_preference
))
78 bool GLContextEGL::GetTotalGpuMemory(size_t* bytes
) {
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
98 size_t dalvik_limit
= 0;
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());
104 if (heap_size
>= 350)
105 limit
= heap_size
/ 2;
107 limit
= (heap_size
+ (heap_growth
* 2)) / 4;
108 dalvik_limit
= limit
* 1024 * 1024;
110 *bytes
= dalvik_limit
;