- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / aura / software_output_device_ozone.cc
1 // Copyright 2013 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 #include "content/browser/aura/software_output_device_ozone.h"
6 #include "third_party/skia/include/core/SkBitmapDevice.h"
7 #include "third_party/skia/include/core/SkDevice.h"
8 #include "ui/compositor/compositor.h"
9 #include "ui/gfx/ozone/surface_factory_ozone.h"
10 #include "ui/gfx/skia_util.h"
11
12 namespace content {
13
14 SoftwareOutputDeviceOzone::SoftwareOutputDeviceOzone(
15     ui::Compositor* compositor) : compositor_(compositor) {
16   if (gfx::SurfaceFactoryOzone::GetInstance()->InitializeHardware() !=
17       gfx::SurfaceFactoryOzone::INITIALIZED)
18     LOG(FATAL) << "Failed to initialize hardware in OZONE";
19 }
20
21 SoftwareOutputDeviceOzone::~SoftwareOutputDeviceOzone() {
22 }
23
24 void SoftwareOutputDeviceOzone::Resize(gfx::Size viewport_size) {
25   if (viewport_size_ == viewport_size)
26     return;
27
28   viewport_size_ = viewport_size;
29   gfx::Rect bounds(viewport_size_);
30
31   gfx::SurfaceFactoryOzone* factory = gfx::SurfaceFactoryOzone::GetInstance();
32   factory->AttemptToResizeAcceleratedWidget(compositor_->widget(),
33                                             bounds);
34   gfx::AcceleratedWidget realized_widget = factory->RealizeAcceleratedWidget(
35       compositor_->widget());
36
37   if (realized_widget == gfx::kNullAcceleratedWidget)
38     LOG(FATAL) << "Failed to get a realized AcceleratedWidget";
39
40   canvas_ = skia::SharePtr(factory->GetCanvasForWidget(realized_widget));
41   device_ = skia::SharePtr(canvas_->getDevice());
42 }
43
44 SkCanvas* SoftwareOutputDeviceOzone::BeginPaint(gfx::Rect damage_rect) {
45   DCHECK(gfx::Rect(viewport_size_).Contains(damage_rect));
46
47   canvas_->clipRect(gfx::RectToSkRect(damage_rect), SkRegion::kReplace_Op);
48   // Save the current state so we can restore once we're done drawing. This is
49   // saved after the clip since we want to keep the clip information after we're
50   // done drawing such that OZONE knows what was updated.
51   canvas_->save();
52
53   return SoftwareOutputDevice::BeginPaint(damage_rect);
54 }
55
56 void SoftwareOutputDeviceOzone::EndPaint(cc::SoftwareFrameData* frame_data) {
57   SoftwareOutputDevice::EndPaint(frame_data);
58
59   canvas_->restore();
60
61   if (damage_rect_.IsEmpty())
62     return;
63
64   bool scheduled = gfx::SurfaceFactoryOzone::GetInstance()->SchedulePageFlip(
65       compositor_->widget());
66   DCHECK(scheduled) << "Failed to schedule pageflip";
67 }
68
69 }  // namespace content