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