- add sources.
[platform/framework/web/crosswalk.git] / src / ui / gfx / ozone / impl / file_surface_factory_ozone.cc
1 // Copyright (c) 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 "ui/gfx/ozone/impl/file_surface_factory_ozone.h"
6
7 #include "base/bind.h"
8 #include "base/file_util.h"
9 #include "base/location.h"
10 #include "base/stl_util.h"
11 #include "base/threading/worker_pool.h"
12 #include "third_party/skia/include/core/SkBitmapDevice.h"
13 #include "third_party/skia/include/core/SkDevice.h"
14 #include "ui/gfx/codec/png_codec.h"
15
16 namespace {
17
18 void WriteDataToFile(const base::FilePath& location,
19                      const SkBitmap& bitmap) {
20   std::vector<unsigned char> png_data;
21   gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, true, &png_data);
22   file_util::WriteFile(location,
23                        (char*)vector_as_array(&png_data),
24                        png_data.size());
25 }
26
27 }
28
29 namespace gfx {
30
31 FileSurfaceFactoryOzone::FileSurfaceFactoryOzone(
32     const base::FilePath& dump_location)
33     : location_(dump_location) {
34   CHECK(!base::DirectoryExists(location_))
35       << "Location cannot be a directory (" << location_.value() << ")";
36   CHECK(!base::PathExists(location_) || base::PathIsWritable(location_));
37 }
38
39 FileSurfaceFactoryOzone::~FileSurfaceFactoryOzone() {}
40
41 SurfaceFactoryOzone::HardwareState
42 FileSurfaceFactoryOzone::InitializeHardware() {
43   return INITIALIZED;
44 }
45
46 void FileSurfaceFactoryOzone::ShutdownHardware() {
47 }
48
49 AcceleratedWidget FileSurfaceFactoryOzone::GetAcceleratedWidget() {
50   return 1;
51 }
52
53 AcceleratedWidget FileSurfaceFactoryOzone::RealizeAcceleratedWidget(
54     AcceleratedWidget widget) {
55   return 1;
56 }
57
58 bool FileSurfaceFactoryOzone::LoadEGLGLES2Bindings(
59       AddGLLibraryCallback add_gl_library,
60       SetGLGetProcAddressProcCallback set_gl_get_proc_address) {
61   return false;
62 }
63
64 bool FileSurfaceFactoryOzone::AttemptToResizeAcceleratedWidget(
65     AcceleratedWidget widget,
66     const Rect& bounds) {
67   device_ = skia::AdoptRef(new SkBitmapDevice(SkBitmap::kARGB_8888_Config,
68                                               bounds.width(),
69                                               bounds.height()));
70   canvas_ = skia::AdoptRef(new SkCanvas(device_.get()));
71   return true;
72 }
73
74 bool FileSurfaceFactoryOzone::SchedulePageFlip(AcceleratedWidget widget) {
75   SkBitmap bitmap;
76   bitmap.setConfig(SkBitmap::kARGB_8888_Config,
77                    device_->width(),
78                    device_->height());
79
80   if (canvas_->readPixels(&bitmap, 0, 0)) {
81     base::WorkerPool::PostTask(FROM_HERE,
82         base::Bind(&WriteDataToFile, location_, bitmap),
83         true);
84   }
85   return true;
86 }
87
88 SkCanvas* FileSurfaceFactoryOzone::GetCanvasForWidget(AcceleratedWidget w) {
89   return canvas_.get();
90 }
91
92 VSyncProvider* FileSurfaceFactoryOzone::GetVSyncProvider(AcceleratedWidget w) {
93   return NULL;
94 }
95
96 }  // namespace gfx