blob: 5dc61aa1d7fd29b98d5e1452bddca710ecb6d482 [file] [log] [blame]
[email protected]1920930592012-01-11 14:54:481// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c797cd42011-03-15 02:18:362// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]116302fc2012-05-05 21:45:415#include "ui/compositor/compositor.h"
[email protected]ed8de92d2011-09-14 04:16:486
[email protected]ab6627372012-01-29 21:22:137#include "base/command_line.h"
[email protected]894e8fc2012-02-24 13:29:508#include "base/threading/thread_restrictions.h"
[email protected]ab6627372012-01-29 21:22:139#include "third_party/WebKit/Source/WebKit/chromium/public/WebCompositor.h"
10#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatPoint.h"
11#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
12#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h"
[email protected]46a519fba2012-04-23 00:40:3513#include "third_party/skia/include/core/SkBitmap.h"
14#include "third_party/skia/include/images/SkImageEncoder.h"
[email protected]116302fc2012-05-05 21:45:4115#include "ui/compositor/compositor_observer.h"
16#include "ui/compositor/compositor_switches.h"
[email protected]cd9a61c72012-05-08 19:16:5917#include "ui/compositor/dip_util.h"
[email protected]116302fc2012-05-05 21:45:4118#include "ui/compositor/layer.h"
19#include "ui/compositor/test_web_graphics_context_3d.h"
[email protected]c9e2cbbb2012-05-12 21:17:2720#include "ui/gl/gl_context.h"
21#include "ui/gl/gl_implementation.h"
22#include "ui/gl/gl_surface.h"
[email protected]ab6627372012-01-29 21:22:1323#include "webkit/glue/webthread_impl.h"
24#include "webkit/gpu/webgraphicscontext3d_in_process_impl.h"
25
26namespace {
27
28const double kDefaultRefreshRate = 60.0;
29const double kTestRefreshRate = 100.0;
30
31webkit_glue::WebThreadImpl* g_compositor_thread = NULL;
32
33bool test_compositor_enabled = false;
34
[email protected]894e8fc2012-02-24 13:29:5035ui::ContextFactory* g_context_factory = NULL;
36
[email protected]ab6627372012-01-29 21:22:1337} // anonymous namespace
[email protected]c797cd42011-03-15 02:18:3638
39namespace ui {
40
[email protected]894e8fc2012-02-24 13:29:5041// static
42ContextFactory* ContextFactory::GetInstance() {
43 // We leak the shared resources so that we don't race with
44 // the tear down of the gl_bindings.
45 if (!g_context_factory) {
[email protected]46a519fba2012-04-23 00:40:3546 DVLOG(1) << "Using DefaultSharedResource";
[email protected]894e8fc2012-02-24 13:29:5047 scoped_ptr<DefaultContextFactory> instance(
48 new DefaultContextFactory());
49 if (instance->Initialize())
50 g_context_factory = instance.release();
51 }
52 return g_context_factory;
[email protected]ca806632012-02-16 02:15:5953}
54
[email protected]58b4b6d2012-02-16 01:40:2455// static
[email protected]894e8fc2012-02-24 13:29:5056void ContextFactory::SetInstance(ContextFactory* instance) {
57 g_context_factory = instance;
[email protected]ca806632012-02-16 02:15:5958}
59
[email protected]894e8fc2012-02-24 13:29:5060DefaultContextFactory::DefaultContextFactory() {
61}
[email protected]ca806632012-02-16 02:15:5962
[email protected]894e8fc2012-02-24 13:29:5063DefaultContextFactory::~DefaultContextFactory() {
64}
[email protected]ab6627372012-01-29 21:22:1365
[email protected]894e8fc2012-02-24 13:29:5066bool DefaultContextFactory::Initialize() {
67 // The following line of code exists soley to disable IO restrictions
68 // on this thread long enough to perform the GL bindings.
69 // TODO(wjmaclean) Remove this when GL initialisation cleaned up.
70 base::ThreadRestrictions::ScopedAllowIO allow_io;
71 if (!gfx::GLSurface::InitializeOneOff() ||
72 gfx::GetGLImplementation() == gfx::kGLImplementationNone) {
73 LOG(ERROR) << "Could not load the GL bindings";
[email protected]ab6627372012-01-29 21:22:1374 return false;
75 }
[email protected]ab6627372012-01-29 21:22:1376 return true;
77}
78
[email protected]894e8fc2012-02-24 13:29:5079WebKit::WebGraphicsContext3D* DefaultContextFactory::CreateContext(
80 Compositor* compositor) {
[email protected]84dba8ca2012-04-10 03:27:3981 return CreateContextCommon(compositor, false);
82}
83
84WebKit::WebGraphicsContext3D* DefaultContextFactory::CreateOffscreenContext(
85 Compositor* compositor) {
86 return CreateContextCommon(compositor, true);
87}
88
89void DefaultContextFactory::RemoveCompositor(Compositor* compositor) {
90}
91
92WebKit::WebGraphicsContext3D* DefaultContextFactory::CreateContextCommon(
93 Compositor* compositor,
94 bool offscreen) {
[email protected]894e8fc2012-02-24 13:29:5095 WebKit::WebGraphicsContext3D::Attributes attrs;
96 attrs.shareResources = true;
97 WebKit::WebGraphicsContext3D* context =
[email protected]84dba8ca2012-04-10 03:27:3998 offscreen ?
99 webkit::gpu::WebGraphicsContext3DInProcessImpl::CreateForWebView(
100 attrs, false) :
[email protected]894e8fc2012-02-24 13:29:50101 webkit::gpu::WebGraphicsContext3DInProcessImpl::CreateForWindow(
102 attrs, compositor->widget(), share_group_.get());
103 CommandLine* command_line = CommandLine::ForCurrentProcess();
104 if (!command_line->HasSwitch(switches::kDisableUIVsync)) {
105 context->makeContextCurrent();
106 gfx::GLContext* gl_context = gfx::GLContext::GetCurrent();
107 gl_context->SetSwapInterval(1);
108 gl_context->ReleaseCurrent(NULL);
109 }
110 return context;
[email protected]ab6627372012-01-29 21:22:13111}
112
[email protected]7359d132012-02-10 00:31:43113Texture::Texture(bool flipped, const gfx::Size& size)
[email protected]ab6627372012-01-29 21:22:13114 : texture_id_(0),
[email protected]7359d132012-02-10 00:31:43115 flipped_(flipped),
116 size_(size) {
[email protected]ab6627372012-01-29 21:22:13117}
118
119Texture::~Texture() {
120}
121
122Compositor::Compositor(CompositorDelegate* delegate,
[email protected]cd9a61c72012-05-08 19:16:59123 gfx::AcceleratedWidget widget)
[email protected]82a01ac2011-09-08 16:00:18124 : delegate_(delegate),
[email protected]ab6627372012-01-29 21:22:13125 root_layer_(NULL),
126 widget_(widget),
[email protected]894e8fc2012-02-24 13:29:50127 root_web_layer_(WebKit::WebLayer::create()),
[email protected]cd9a61c72012-05-08 19:16:59128 swap_posted_(false),
129 device_scale_factor_(0.0f) {
[email protected]ab6627372012-01-29 21:22:13130 WebKit::WebLayerTreeView::Settings settings;
131 CommandLine* command_line = CommandLine::ForCurrentProcess();
132 settings.showFPSCounter =
133 command_line->HasSwitch(switches::kUIShowFPSCounter);
134 settings.showPlatformLayerTree =
135 command_line->HasSwitch(switches::kUIShowLayerTree);
136 settings.refreshRate = test_compositor_enabled ?
137 kTestRefreshRate : kDefaultRefreshRate;
138 settings.partialSwapEnabled =
139 command_line->HasSwitch(switches::kUIEnablePartialSwap);
[email protected]e570b93c2012-02-15 16:50:48140 settings.perTilePainting =
141 command_line->HasSwitch(switches::kUIEnablePerTilePainting);
[email protected]ab6627372012-01-29 21:22:13142
[email protected]ba7aeb82012-02-24 23:36:13143 host_.initialize(this, root_web_layer_, settings);
[email protected]ab6627372012-01-29 21:22:13144 root_web_layer_.setAnchorPoint(WebKit::WebFloatPoint(0.f, 0.f));
[email protected]82a01ac2011-09-08 16:00:18145}
146
147Compositor::~Compositor() {
[email protected]e332a1b2012-05-10 20:32:27148 // Don't call |CompositorDelegate::ScheduleDraw| from this point.
149 delegate_ = NULL;
[email protected]ab6627372012-01-29 21:22:13150 // There's a cycle between |root_web_layer_| and |host_|, which results in
151 // leaking and/or crashing. Explicitly set the root layer to NULL so the cycle
152 // is broken.
153 host_.setRootLayer(NULL);
[email protected]7ab3f272011-11-16 00:51:56154 if (root_layer_)
155 root_layer_->SetCompositor(NULL);
[email protected]894e8fc2012-02-24 13:29:50156 if (!test_compositor_enabled)
157 ContextFactory::GetInstance()->RemoveCompositor(this);
[email protected]82a01ac2011-09-08 16:00:18158}
159
[email protected]ab6627372012-01-29 21:22:13160void Compositor::Initialize(bool use_thread) {
161 if (use_thread)
162 g_compositor_thread = new webkit_glue::WebThreadImpl("Browser Compositor");
163 WebKit::WebCompositor::initialize(g_compositor_thread);
164}
165
166void Compositor::Terminate() {
167 WebKit::WebCompositor::shutdown();
168 if (g_compositor_thread) {
169 delete g_compositor_thread;
170 g_compositor_thread = NULL;
171 }
172}
173
[email protected]332749032011-10-22 00:32:46174void Compositor::ScheduleDraw() {
[email protected]f78649ea2012-02-23 18:39:04175 if (g_compositor_thread) {
176 // TODO(nduca): Temporary while compositor calls
177 // compositeImmediately() directly.
178 layout();
[email protected]ab6627372012-01-29 21:22:13179 host_.composite();
[email protected]e332a1b2012-05-10 20:32:27180 } else if (delegate_) {
[email protected]ab6627372012-01-29 21:22:13181 delegate_->ScheduleDraw();
[email protected]f78649ea2012-02-23 18:39:04182 }
[email protected]332749032011-10-22 00:32:46183}
184
[email protected]993d6b322011-09-27 19:14:38185void Compositor::SetRootLayer(Layer* root_layer) {
[email protected]12233c362011-11-21 16:09:25186 if (root_layer_ == root_layer)
187 return;
[email protected]7ab3f272011-11-16 00:51:56188 if (root_layer_)
189 root_layer_->SetCompositor(NULL);
[email protected]993d6b322011-09-27 19:14:38190 root_layer_ = root_layer;
[email protected]7ab3f272011-11-16 00:51:56191 if (root_layer_ && !root_layer_->GetCompositor())
[email protected]993d6b322011-09-27 19:14:38192 root_layer_->SetCompositor(this);
[email protected]ab6627372012-01-29 21:22:13193 root_web_layer_.removeAllChildren();
194 if (root_layer_)
195 root_web_layer_.addChild(root_layer_->web_layer());
[email protected]993d6b322011-09-27 19:14:38196}
197
[email protected]ed8de92d2011-09-14 04:16:48198void Compositor::Draw(bool force_clear) {
199 if (!root_layer_)
200 return;
201
[email protected]f78649ea2012-02-23 18:39:04202 // TODO(nduca): Temporary while compositor calls
203 // compositeImmediately() directly.
204 layout();
[email protected]ab6627372012-01-29 21:22:13205 host_.composite();
[email protected]894e8fc2012-02-24 13:29:50206 if (!g_compositor_thread && !swap_posted_)
[email protected]1920930592012-01-11 14:54:48207 NotifyEnd();
[email protected]ab6627372012-01-29 21:22:13208}
209
[email protected]7df588fbd2012-02-10 14:15:56210void Compositor::ScheduleFullDraw() {
211 host_.setNeedsRedraw();
212}
213
[email protected]cd9a61c72012-05-08 19:16:59214bool Compositor::ReadPixels(SkBitmap* bitmap,
215 const gfx::Rect& bounds_in_pixel) {
216 if (bounds_in_pixel.right() > size().width() ||
217 bounds_in_pixel.bottom() > size().height())
[email protected]ab6627372012-01-29 21:22:13218 return false;
219 // Convert to OpenGL coordinates.
[email protected]cd9a61c72012-05-08 19:16:59220 gfx::Point new_origin(
221 bounds_in_pixel.x(),
222 size().height() - bounds_in_pixel.height() - bounds_in_pixel.y());
[email protected]ab6627372012-01-29 21:22:13223
224 bitmap->setConfig(SkBitmap::kARGB_8888_Config,
[email protected]cd9a61c72012-05-08 19:16:59225 bounds_in_pixel.width(), bounds_in_pixel.height());
[email protected]ab6627372012-01-29 21:22:13226 bitmap->allocPixels();
227 SkAutoLockPixels lock_image(*bitmap);
228 unsigned char* pixels = static_cast<unsigned char*>(bitmap->getPixels());
[email protected]cd9a61c72012-05-08 19:16:59229 if (host_.compositeAndReadback(
230 pixels, gfx::Rect(new_origin, bounds_in_pixel.size()))) {
231 SwizzleRGBAToBGRAAndFlip(pixels, bounds_in_pixel.size());
[email protected]ab6627372012-01-29 21:22:13232 return true;
[email protected]1920930592012-01-11 14:54:48233 }
[email protected]ab6627372012-01-29 21:22:13234 return false;
235}
236
[email protected]cd9a61c72012-05-08 19:16:59237void Compositor::SetScaleAndSize(float scale, const gfx::Size& size_in_pixel) {
238 DCHECK(scale > 0);
239 if (size_in_pixel.IsEmpty() || scale <= 0)
[email protected]ab6627372012-01-29 21:22:13240 return;
[email protected]cd9a61c72012-05-08 19:16:59241 size_ = size_in_pixel;
242 host_.setViewportSize(size_in_pixel);
243 root_web_layer_.setBounds(size_in_pixel);
244
245 if (device_scale_factor_ != scale && IsDIPEnabled()) {
246 device_scale_factor_ = scale;
247 if (root_layer_)
248 root_layer_->OnDeviceScaleFactorChanged(scale);
249 }
[email protected]ed8de92d2011-09-14 04:16:48250}
251
252void Compositor::AddObserver(CompositorObserver* observer) {
253 observer_list_.AddObserver(observer);
254}
255
256void Compositor::RemoveObserver(CompositorObserver* observer) {
257 observer_list_.RemoveObserver(observer);
258}
259
[email protected]3ce2feb2011-09-19 18:44:23260bool Compositor::HasObserver(CompositorObserver* observer) {
261 return observer_list_.HasObserver(observer);
262}
263
[email protected]894e8fc2012-02-24 13:29:50264void Compositor::OnSwapBuffersPosted() {
265 swap_posted_ = true;
266}
267
268void Compositor::OnSwapBuffersComplete() {
269 DCHECK(swap_posted_);
270 swap_posted_ = false;
271 NotifyEnd();
272}
273
274void Compositor::OnSwapBuffersAborted() {
275 if (swap_posted_) {
276 swap_posted_ = false;
277 NotifyEnd();
278 }
279}
[email protected]ab6627372012-01-29 21:22:13280
281void Compositor::updateAnimations(double frameBeginTime) {
282}
283
284void Compositor::layout() {
[email protected]f78649ea2012-02-23 18:39:04285 if (root_layer_)
[email protected]cedc3952012-03-06 06:15:55286 root_layer_->SendDamagedRects();
[email protected]ab6627372012-01-29 21:22:13287}
288
289void Compositor::applyScrollAndScale(const WebKit::WebSize& scrollDelta,
290 float scaleFactor) {
291}
292
293WebKit::WebGraphicsContext3D* Compositor::createContext3D() {
[email protected]ca806632012-02-16 02:15:59294 if (test_compositor_enabled) {
[email protected]894e8fc2012-02-24 13:29:50295 ui::TestWebGraphicsContext3D* test_context =
296 new ui::TestWebGraphicsContext3D();
297 test_context->Initialize();
298 return test_context;
[email protected]ca806632012-02-16 02:15:59299 } else {
[email protected]894e8fc2012-02-24 13:29:50300 return ContextFactory::GetInstance()->CreateContext(this);
[email protected]ca806632012-02-16 02:15:59301 }
[email protected]ab6627372012-01-29 21:22:13302}
303
[email protected]ba7aeb82012-02-24 23:36:13304void Compositor::didRebindGraphicsContext(bool success) {
[email protected]ab6627372012-01-29 21:22:13305}
306
[email protected]ba7aeb82012-02-24 23:36:13307void Compositor::didCommitAndDrawFrame() {
[email protected]a8f677c2012-03-23 01:36:06308 FOR_EACH_OBSERVER(CompositorObserver,
309 observer_list_,
310 OnCompositingStarted(this));
[email protected]ba7aeb82012-02-24 23:36:13311}
312
313void Compositor::didCompleteSwapBuffers() {
314 NotifyEnd();
[email protected]ab6627372012-01-29 21:22:13315}
316
317void Compositor::scheduleComposite() {
[email protected]332749032011-10-22 00:32:46318 ScheduleDraw();
319}
320
[email protected]6f83e4f2011-11-15 21:25:33321void Compositor::SwizzleRGBAToBGRAAndFlip(unsigned char* pixels,
322 const gfx::Size& image_size) {
323 // Swizzle from RGBA to BGRA
324 size_t bitmap_size = 4 * image_size.width() * image_size.height();
325 for(size_t i = 0; i < bitmap_size; i += 4)
326 std::swap(pixels[i], pixels[i + 2]);
327
328 // Vertical flip to transform from GL co-ords
329 size_t row_size = 4 * image_size.width();
330 scoped_array<unsigned char> tmp_row(new unsigned char[row_size]);
331 for(int row = 0; row < image_size.height() / 2; row++) {
332 memcpy(tmp_row.get(),
333 &pixels[row * row_size],
334 row_size);
335 memcpy(&pixels[row * row_size],
336 &pixels[bitmap_size - (row + 1) * row_size],
337 row_size);
338 memcpy(&pixels[bitmap_size - (row + 1) * row_size],
339 tmp_row.get(),
340 row_size);
341 }
342}
343
[email protected]a8f21152011-09-08 15:30:02344void Compositor::NotifyEnd() {
[email protected]a8f21152011-09-08 15:30:02345 FOR_EACH_OBSERVER(CompositorObserver,
346 observer_list_,
[email protected]3ce2feb2011-09-19 18:44:23347 OnCompositingEnded(this));
[email protected]a8f21152011-09-08 15:30:02348}
349
[email protected]ab6627372012-01-29 21:22:13350COMPOSITOR_EXPORT void SetupTestCompositor() {
351 if (!CommandLine::ForCurrentProcess()->HasSwitch(
352 switches::kDisableTestCompositor)) {
353 test_compositor_enabled = true;
354 }
355}
356
357COMPOSITOR_EXPORT void DisableTestCompositor() {
358 test_compositor_enabled = false;
[email protected]1920930592012-01-11 14:54:48359}
360
[email protected]894e8fc2012-02-24 13:29:50361COMPOSITOR_EXPORT bool IsTestCompositorEnabled() {
362 return test_compositor_enabled;
363}
364
[email protected]c797cd42011-03-15 02:18:36365} // namespace ui