- add sources.
[platform/framework/web/crosswalk.git] / src / content / plugin / webplugin_accelerated_surface_proxy_mac.cc
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.
4
5 #import <OpenGL/OpenGL.h>
6
7 #include "content/plugin/webplugin_accelerated_surface_proxy_mac.h"
8
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "content/plugin/webplugin_proxy.h"
12 #include "content/public/common/content_switches.h"
13 #include "ui/gl/io_surface_support_mac.h"
14 #include "ui/surface/accelerated_surface_mac.h"
15 #include "ui/surface/transport_dib.h"
16
17 namespace content {
18
19 WebPluginAcceleratedSurfaceProxy* WebPluginAcceleratedSurfaceProxy::Create(
20     WebPluginProxy* plugin_proxy,
21     gfx::GpuPreference gpu_preference) {
22   // This code path shouldn't be taken if CA plugins are disabled
23   // because the CA drawing model shouldn't be advertised.
24   DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch(
25       switches::kDisableCoreAnimationPlugins));
26
27   // Require IOSurface support for drawing Core Animation plugins.
28   if (!IOSurfaceSupport::Initialize())
29     return NULL;
30
31   AcceleratedSurface* surface = new AcceleratedSurface;
32   // It's possible for OpenGL to fail to initialize (e.g., if an incompatible
33   // mode is forced via flags), so handle that gracefully.
34   if (!surface->Initialize(NULL, true, gpu_preference)) {
35     delete surface;
36     return NULL;
37   }
38
39   return new WebPluginAcceleratedSurfaceProxy(plugin_proxy, surface);
40 }
41
42 WebPluginAcceleratedSurfaceProxy::WebPluginAcceleratedSurfaceProxy(
43     WebPluginProxy* plugin_proxy,
44     AcceleratedSurface* surface)
45         : plugin_proxy_(plugin_proxy),
46           surface_(surface) {
47 }
48
49 WebPluginAcceleratedSurfaceProxy::~WebPluginAcceleratedSurfaceProxy() {
50   if (surface_) {
51     surface_->Destroy();
52     delete surface_;
53     surface_ = NULL;
54   }
55 }
56
57 void WebPluginAcceleratedSurfaceProxy::SetSize(const gfx::Size& size) {
58   if (!surface_)
59     return;
60
61   uint32 io_surface_id = surface_->SetSurfaceSize(size);
62   // If allocation fails for some reason, still inform the plugin proxy.
63   plugin_proxy_->AcceleratedPluginAllocatedIOSurface(
64       size.width(), size.height(), io_surface_id);
65 }
66
67 CGLContextObj WebPluginAcceleratedSurfaceProxy::context() {
68   return surface_ ? surface_->context() : NULL;
69 }
70
71 void WebPluginAcceleratedSurfaceProxy::StartDrawing() {
72   if (!surface_)
73     return;
74
75   surface_->MakeCurrent();
76   surface_->Clear(gfx::Rect(surface_->GetSize()));
77 }
78
79 void WebPluginAcceleratedSurfaceProxy::EndDrawing() {
80   if (!surface_)
81     return;
82
83   surface_->SwapBuffers();
84   plugin_proxy_->AcceleratedPluginSwappedIOSurface();
85 }
86
87 }  // namespace content