1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file implements a standalone executable demo of using GLES2 over
// command buffers. This code is temporary as the setup that happens
// here is in a state of flux. Eventually there will be several versions of
// setup, one for trusted code, one for pepper, one for NaCl, and maybe others.
#include <windows.h>
#include <windowsx.h>
#include <shellapi.h>
#include <stdlib.h>
#include <stdio.h>
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/shared_memory.h"
#include "gpu/command_buffer/service/gpu_processor.h"
#include "gpu/command_buffer/service/command_buffer_service.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "gpu/command_buffer/client/gles2_lib.h"
#include "gpu/command_buffer/client/gles2_demo_c.h"
#include "gpu/command_buffer/client/gles2_demo_cc.h"
#include "ui/gfx/gl/gl_context.h"
using base::SharedMemory;
using gpu::Buffer;
using gpu::GPUProcessor;
using gpu::CommandBufferService;
using gpu::gles2::GLES2CmdHelper;
using gpu::gles2::GLES2Implementation;
#if defined(OS_WIN)
HINSTANCE g_instance;
#endif
class GLES2Demo {
public:
GLES2Demo();
bool GLES2Demo::Setup(void* hwnd, int32 size);
private:
DISALLOW_COPY_AND_ASSIGN(GLES2Demo);
};
GLES2Demo::GLES2Demo() {
}
bool GLES2Demo::Setup(void* hwnd, int32 size) {
scoped_ptr<CommandBufferService> command_buffer(new CommandBufferService);
if (!command_buffer->Initialize(size))
return NULL;
GPUProcessor* gpu_processor = new GPUProcessor(command_buffer.get(), NULL);
if (!gpu_processor->Initialize(reinterpret_cast<HWND>(hwnd),
gfx::Size(),
gpu::gles2::DisallowedExtensions(),
NULL,
std::vector<int32>(),
NULL,
0)) {
return NULL;
}
command_buffer->SetPutOffsetChangeCallback(
NewCallback(gpu_processor, &GPUProcessor::ProcessCommands));
GLES2CmdHelper* helper = new GLES2CmdHelper(command_buffer.get());
if (!helper->Initialize(size)) {
// TODO(gman): cleanup.
return false;
}
size_t transfer_buffer_size = 512 * 1024;
int32 transfer_buffer_id =
command_buffer->CreateTransferBuffer(transfer_buffer_size, -1);
Buffer transfer_buffer =
command_buffer->GetTransferBuffer(transfer_buffer_id);
if (!transfer_buffer.ptr)
return false;
gles2::Initialize();
gles2::SetGLContext(new GLES2Implementation(helper,
transfer_buffer.size,
transfer_buffer.ptr,
transfer_buffer_id,
false));
GLFromCPPInit();
return command_buffer.release() != NULL;
}
#if defined(OS_WIN)
LRESULT CALLBACK WindowProc(
HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) {
switch (msg) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT: {
GLFromCPPDraw();
GLFromCDraw();
// TODO(gman): Not sure how SwapBuffer should be exposed.
gles2::GetGLContext()->SwapBuffers();
break;
}
default:
return ::DefWindowProc(hwnd, msg, w_param, l_param);
}
return 0;
}
void ProcessMessages(void* in_hwnd) {
HWND hwnd = reinterpret_cast<HWND>(in_hwnd);
MSG msg;
bool done = false;
while (!done) {
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
done = true;
}
// dispatch the message
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (!done) {
InvalidateRect(hwnd, NULL, TRUE);
}
}
}
#endif
void* SetupWindow() {
#if defined(OS_WIN)
WNDCLASSEX wc = {0};
wc.lpszClassName = L"MY_WINDOWS_CLASS";
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = ::WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = g_instance;
wc.hIcon = ::LoadIcon(g_instance, IDI_APPLICATION);
wc.hIconSm = NULL;
wc.hCursor = ::LoadCursor(g_instance, IDC_ARROW);
wc.hbrBackground = static_cast<HBRUSH>(::GetStockObject(BLACK_BRUSH));
wc.lpszMenuName = NULL;
if (!::RegisterClassEx(&wc))
return false;
// Leaving this window onscreen leads to a redraw error which makes it
// a hassle to debug tests in an IDE, so we place the window somewhere that
// won't happen.
HWND hwnd = ::CreateWindowExW(
NULL,
wc.lpszClassName,
L"",
WS_OVERLAPPEDWINDOW,
10,
0,
512,
512,
0,
0,
g_instance,
0);
if (hwnd == NULL) {
return false;
}
::ShowWindow(hwnd, SW_SHOWNORMAL);
return hwnd;
#else
#error Need code.
#endif
}
#if defined(OS_WIN)
int WINAPI WinMain(HINSTANCE instance,
HINSTANCE prev_instance,
LPSTR command_line,
int command_show) {
g_instance = instance;
int argc = 1;
const char* const argv[] = {
"gles2_demo.exe"
};
#else
int main(int argc, char** argv) {
#endif
CommandLine::Init(argc, argv);
const int32 kCommandBufferSize = 1024 * 1024;
base::AtExitManager at_exit_manager;
MessageLoopForUI message_loop;
gfx::GLContext::InitializeOneOff();
GLES2Demo* demo = new GLES2Demo();
void* hwnd = SetupWindow();
if (!hwnd) {
::fprintf(stdout, "Could not setup window.\n");
return EXIT_FAILURE;
}
demo->Setup(hwnd, kCommandBufferSize);
ProcessMessages(hwnd);
return EXIT_SUCCESS;
}
|