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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
// Copyright (c) 2010 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.
#include "content/renderer/render_widget_fullscreen_pepper.h"
#include "chrome/common/render_messages.h"
#include "chrome/renderer/render_thread.h"
#include "content/renderer/ggl.h"
#include "content/renderer/gpu_channel_host.h"
#include "content/renderer/pepper_platform_context_3d_impl.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
#include "webkit/plugins/ppapi/plugin_delegate.h"
#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
using WebKit::WebCanvas;
using WebKit::WebCompositionUnderline;
using WebKit::WebCursorInfo;
using WebKit::WebInputEvent;
using WebKit::WebRect;
using WebKit::WebSize;
using WebKit::WebString;
using WebKit::WebTextDirection;
using WebKit::WebTextInputType;
using WebKit::WebVector;
using WebKit::WebWidget;
namespace {
// WebWidget that simply wraps the pepper plugin.
class PepperWidget : public WebWidget {
public:
PepperWidget(webkit::ppapi::PluginInstance* plugin,
RenderWidgetFullscreenPepper* widget)
: plugin_(plugin),
widget_(widget),
cursor_(WebCursorInfo::TypePointer) {
}
// WebWidget API
virtual void close() {
delete this;
}
virtual WebSize size() {
return size_;
}
virtual void resize(const WebSize& size) {
size_ = size;
WebRect plugin_rect(0, 0, size_.width, size_.height);
plugin_->ViewChanged(plugin_rect, plugin_rect);
widget_->Invalidate();
}
virtual void animate() {
}
virtual void layout() {
}
virtual void paint(WebCanvas* canvas, const WebRect& rect) {
WebRect plugin_rect(0, 0, size_.width, size_.height);
plugin_->Paint(canvas, plugin_rect, rect);
}
virtual void composite(bool finish) {
ggl::Context* context = widget_->context();
DCHECK(context);
gpu::gles2::GLES2Implementation* gl = ggl::GetImplementation(context);
unsigned int texture = plugin_->GetBackingTextureId();
gl->BindTexture(GL_TEXTURE_2D, texture);
gl->DrawArrays(GL_TRIANGLES, 0, 3);
ggl::SwapBuffers(context);
}
virtual void themeChanged() {
NOTIMPLEMENTED();
}
virtual bool handleInputEvent(const WebInputEvent& event) {
return plugin_->HandleInputEvent(event, &cursor_);
}
virtual void mouseCaptureLost() {
NOTIMPLEMENTED();
}
virtual void setFocus(bool focus) {
NOTIMPLEMENTED();
}
// TODO(piman): figure out IME and implement these if necessary.
virtual bool setComposition(
const WebString& text,
const WebVector<WebCompositionUnderline>& underlines,
int selectionStart,
int selectionEnd) {
return false;
}
virtual bool confirmComposition() {
return false;
}
virtual bool confirmComposition(const WebString& text) {
return false;
}
virtual WebTextInputType textInputType() {
return WebKit::WebTextInputTypeNone;
}
virtual WebRect caretOrSelectionBounds() {
return WebRect();
}
virtual void setTextDirection(WebTextDirection) {
}
virtual bool isAcceleratedCompositingActive() const {
return widget_->context() && (plugin_->GetBackingTextureId() != 0);
}
private:
scoped_refptr<webkit::ppapi::PluginInstance> plugin_;
RenderWidgetFullscreenPepper* widget_;
WebSize size_;
WebCursorInfo cursor_;
DISALLOW_COPY_AND_ASSIGN(PepperWidget);
};
} // anonymous namespace
// static
RenderWidgetFullscreenPepper* RenderWidgetFullscreenPepper::Create(
int32 opener_id, RenderThreadBase* render_thread,
webkit::ppapi::PluginInstance* plugin) {
DCHECK_NE(MSG_ROUTING_NONE, opener_id);
scoped_refptr<RenderWidgetFullscreenPepper> widget(
new RenderWidgetFullscreenPepper(render_thread, plugin));
widget->Init(opener_id);
return widget.release();
}
RenderWidgetFullscreenPepper::RenderWidgetFullscreenPepper(
RenderThreadBase* render_thread,
webkit::ppapi::PluginInstance* plugin)
: RenderWidgetFullscreen(render_thread),
plugin_(plugin),
context_(NULL),
buffer_(0),
program_(0) {
}
RenderWidgetFullscreenPepper::~RenderWidgetFullscreenPepper() {
DestroyContext();
}
void RenderWidgetFullscreenPepper::Invalidate() {
InvalidateRect(gfx::Rect(size_.width(), size_.height()));
}
void RenderWidgetFullscreenPepper::InvalidateRect(const WebKit::WebRect& rect) {
if (CheckCompositing()) {
scheduleComposite();
} else {
didInvalidateRect(rect);
}
}
void RenderWidgetFullscreenPepper::ScrollRect(
int dx, int dy, const WebKit::WebRect& rect) {
if (CheckCompositing()) {
scheduleComposite();
} else {
didScrollRect(dx, dy, rect);
}
}
void RenderWidgetFullscreenPepper::Destroy() {
// This function is called by the plugin instance as it's going away, so reset
// plugin_ to NULL to avoid calling into a dangling pointer e.g. on Close().
plugin_ = NULL;
Send(new ViewHostMsg_Close(routing_id_));
}
webkit::ppapi::PluginDelegate::PlatformContext3D*
RenderWidgetFullscreenPepper::CreateContext3D() {
if (!context_) {
CreateContext();
}
if (!context_)
return NULL;
return new PlatformContext3DImpl(context_);
}
void RenderWidgetFullscreenPepper::DidInitiatePaint() {
if (plugin_)
plugin_->ViewInitiatedPaint();
}
void RenderWidgetFullscreenPepper::DidFlushPaint() {
if (plugin_)
plugin_->ViewFlushedPaint();
}
void RenderWidgetFullscreenPepper::Close() {
// If the fullscreen window is closed (e.g. user pressed escape), reset to
// normal mode.
if (plugin_)
plugin_->SetFullscreen(false, false);
}
webkit::ppapi::PluginInstance*
RenderWidgetFullscreenPepper::GetBitmapForOptimizedPluginPaint(
const gfx::Rect& paint_bounds,
TransportDIB** dib,
gfx::Rect* location,
gfx::Rect* clip) {
if (plugin_ &&
plugin_->GetBitmapForOptimizedPluginPaint(paint_bounds, dib,
location, clip))
return plugin_;
return NULL;
}
void RenderWidgetFullscreenPepper::OnResize(const gfx::Size& size,
const gfx::Rect& resizer_rect) {
if (context_) {
gpu::gles2::GLES2Implementation* gl = ggl::GetImplementation(context_);
#if defined(OS_MACOSX)
ggl::ResizeOnscreenContext(context_, size);
#else
gl->ResizeCHROMIUM(size.width(), size.height());
#endif
gl->Viewport(0, 0, size.width(), size.height());
}
RenderWidget::OnResize(size, resizer_rect);
}
WebWidget* RenderWidgetFullscreenPepper::CreateWebWidget() {
return new PepperWidget(plugin_, this);
}
void RenderWidgetFullscreenPepper::CreateContext() {
DCHECK(!context_);
RenderThread* render_thread = RenderThread::current();
DCHECK(render_thread);
GpuChannelHost* host = render_thread->EstablishGpuChannelSync();
if (!host)
return;
const int32 attribs[] = {
ggl::GGL_ALPHA_SIZE, 8,
ggl::GGL_DEPTH_SIZE, 0,
ggl::GGL_STENCIL_SIZE, 0,
ggl::GGL_SAMPLES, 0,
ggl::GGL_SAMPLE_BUFFERS, 0,
ggl::GGL_NONE,
};
context_ = ggl::CreateViewContext(
host,
routing_id(),
"GL_OES_packed_depth_stencil GL_OES_depth24",
attribs);
if (!context_ || !InitContext()) {
DestroyContext();
return;
}
ggl::SetSwapBuffersCallback(
context_,
NewCallback(this, &RenderWidgetFullscreenPepper::DidFlushPaint));
}
void RenderWidgetFullscreenPepper::DestroyContext() {
if (context_) {
gpu::gles2::GLES2Implementation* gl = ggl::GetImplementation(context_);
if (program_) {
gl->DeleteProgram(program_);
program_ = 0;
}
if (buffer_) {
gl->DeleteBuffers(1, &buffer_);
buffer_ = 0;
}
ggl::DestroyContext(context_);
context_ = NULL;
}
}
namespace {
const char kVertexShader[] =
"attribute vec2 in_tex_coord;\n"
"varying vec2 tex_coord;\n"
"void main() {\n"
" gl_Position = vec4(in_tex_coord.x * 2. - 1.,\n"
" in_tex_coord.y * 2. - 1.,\n"
" 0.,\n"
" 1.);\n"
" tex_coord = vec2(in_tex_coord.x, in_tex_coord.y);\n"
"}\n";
const char kFragmentShader[] =
"precision mediump float;\n"
"varying vec2 tex_coord;\n"
"uniform sampler2D in_texture;\n"
"void main() {\n"
" gl_FragColor = texture2D(in_texture, tex_coord);\n"
"}\n";
GLuint CreateShaderFromSource(gpu::gles2::GLES2Implementation* gl,
GLenum type,
const char* source) {
GLuint shader = gl->CreateShader(type);
gl->ShaderSource(shader, 1, &source, NULL);
gl->CompileShader(shader);
int status;
gl->GetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (!status) {
int size = 0;
gl->GetShaderiv(shader, GL_INFO_LOG_LENGTH, &size);
scoped_array<char> log(new char[size]);
gl->GetShaderInfoLog(shader, size, NULL, log.get());
DLOG(ERROR) << "Compilation failed: " << log.get();
gl->DeleteShader(shader);
shader = 0;
}
return shader;
}
const float kTexCoords[] = {
0.f, 0.f,
0.f, 2.f,
2.f, 0.f,
};
} // anonymous namespace
bool RenderWidgetFullscreenPepper::InitContext() {
gpu::gles2::GLES2Implementation* gl = ggl::GetImplementation(context_);
program_ = gl->CreateProgram();
GLuint vertex_shader =
CreateShaderFromSource(gl, GL_VERTEX_SHADER, kVertexShader);
if (!vertex_shader)
return false;
gl->AttachShader(program_, vertex_shader);
gl->DeleteShader(vertex_shader);
GLuint fragment_shader =
CreateShaderFromSource(gl, GL_FRAGMENT_SHADER, kFragmentShader);
if (!fragment_shader)
return false;
gl->AttachShader(program_, fragment_shader);
gl->DeleteShader(fragment_shader);
gl->BindAttribLocation(program_, 0, "in_tex_coord");
gl->LinkProgram(program_);
int status;
gl->GetProgramiv(program_, GL_LINK_STATUS, &status);
if (!status) {
int size = 0;
gl->GetProgramiv(program_, GL_INFO_LOG_LENGTH, &size);
scoped_array<char> log(new char[size]);
gl->GetProgramInfoLog(program_, size, NULL, log.get());
DLOG(ERROR) << "Link failed: " << log.get();
return false;
}
gl->UseProgram(program_);
int texture_location = gl->GetUniformLocation(program_, "in_texture");
gl->Uniform1i(texture_location, 0);
gl->GenBuffers(1, &buffer_);
gl->BindBuffer(GL_ARRAY_BUFFER, buffer_);
gl->BufferData(GL_ARRAY_BUFFER,
sizeof(kTexCoords),
kTexCoords,
GL_STATIC_DRAW);
gl->VertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
gl->EnableVertexAttribArray(0);
return true;
}
bool RenderWidgetFullscreenPepper::CheckCompositing() {
bool compositing = webwidget_->isAcceleratedCompositingActive();
if (compositing != is_accelerated_compositing_active_) {
didActivateAcceleratedCompositing(compositing);
}
return compositing;
}
|