1 | // Copyright (c) 2011 The Native Client 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. |
---|
4 | |
---|
5 | #if defined HAVE_CONFIG_H |
---|
6 | # include "config.h" |
---|
7 | #endif |
---|
8 | |
---|
9 | #include <pthread.h> |
---|
10 | #include <ppapi/cpp/completion_callback.h> |
---|
11 | #include <ppapi/gles2/gl2ext_ppapi.h> |
---|
12 | |
---|
13 | #include "core.h" |
---|
14 | |
---|
15 | #include "platform/nacl/opengl_context.h" |
---|
16 | |
---|
17 | namespace { |
---|
18 | // This is called by the brower when the 3D context has been flushed to the |
---|
19 | // browser window. |
---|
20 | void FlushCallback(void* data, int32_t result) { |
---|
21 | static_cast<lol::OpenGLContext*>(data)->set_flush_pending(false); |
---|
22 | } |
---|
23 | } // namespace |
---|
24 | |
---|
25 | namespace lol { |
---|
26 | |
---|
27 | OpenGLContext::OpenGLContext(pp::Instance* instance) |
---|
28 | : pp::Graphics3DClient(instance), |
---|
29 | flush_pending_(false) { |
---|
30 | pp::Module* module = pp::Module::Get(); |
---|
31 | assert(module); |
---|
32 | gles2_interface_ = static_cast<const struct PPB_OpenGLES2*>( |
---|
33 | module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)); |
---|
34 | assert(gles2_interface_); |
---|
35 | } |
---|
36 | |
---|
37 | OpenGLContext::~OpenGLContext() { |
---|
38 | glSetCurrentContextPPAPI(0); |
---|
39 | } |
---|
40 | |
---|
41 | bool OpenGLContext::MakeContextCurrent(pp::Instance* instance) { |
---|
42 | if (instance == NULL) { |
---|
43 | glSetCurrentContextPPAPI(0); |
---|
44 | return false; |
---|
45 | } |
---|
46 | // Lazily create the Pepper context. |
---|
47 | if (context_.is_null()) { |
---|
48 | int32_t attribs[] = { |
---|
49 | PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8, |
---|
50 | PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24, |
---|
51 | PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8, |
---|
52 | PP_GRAPHICS3DATTRIB_SAMPLES, 0, |
---|
53 | PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0, |
---|
54 | PP_GRAPHICS3DATTRIB_WIDTH, size_.width(), |
---|
55 | PP_GRAPHICS3DATTRIB_HEIGHT, size_.height(), |
---|
56 | PP_GRAPHICS3DATTRIB_NONE |
---|
57 | }; |
---|
58 | context_ = pp::Graphics3D(instance, pp::Graphics3D(), attribs); |
---|
59 | if (context_.is_null()) { |
---|
60 | glSetCurrentContextPPAPI(0); |
---|
61 | return false; |
---|
62 | } |
---|
63 | instance->BindGraphics(context_); |
---|
64 | } |
---|
65 | glSetCurrentContextPPAPI(context_.pp_resource()); |
---|
66 | return true; |
---|
67 | } |
---|
68 | |
---|
69 | void OpenGLContext::InvalidateContext(pp::Instance* instance) { |
---|
70 | glSetCurrentContextPPAPI(0); |
---|
71 | } |
---|
72 | |
---|
73 | void OpenGLContext::ResizeContext(const pp::Size& size) { |
---|
74 | size_ = size; |
---|
75 | if (!context_.is_null()) { |
---|
76 | context_.ResizeBuffers(size.width(), size.height()); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | void OpenGLContext::FlushContext() { |
---|
82 | if (flush_pending()) { |
---|
83 | // A flush is pending so do nothing; just drop this flush on the floor. |
---|
84 | return; |
---|
85 | } |
---|
86 | set_flush_pending(true); |
---|
87 | context_.SwapBuffers(pp::CompletionCallback(&FlushCallback, this)); |
---|
88 | } |
---|
89 | } // namespace lol |
---|
90 | |
---|