Upstream version 7.36.149.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/SkCanvas.h"
13 #include "third_party/skia/include/core/SkSurface.h"
14 #include "ui/gfx/codec/png_codec.h"
15 #include "ui/gfx/ozone/surface_ozone_canvas.h"
16 #include "ui/gfx/skia_util.h"
17 #include "ui/gfx/vsync_provider.h"
18
19 namespace gfx {
20
21 namespace {
22
23 void WriteDataToFile(const base::FilePath& location,
24                      const SkBitmap& bitmap) {
25   std::vector<unsigned char> png_data;
26   gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, true, &png_data);
27   base::WriteFile(location,
28                   reinterpret_cast<const char*>(vector_as_array(&png_data)),
29                   png_data.size());
30 }
31
32 class FileSurface : public SurfaceOzoneCanvas {
33  public:
34   FileSurface(const base::FilePath& location) : location_(location) {}
35   virtual ~FileSurface() {}
36
37   // SurfaceOzoneCanvas overrides:
38   virtual void ResizeCanvas(const Size& viewport_size) OVERRIDE {
39     surface_ = skia::AdoptRef(SkSurface::NewRaster(
40         SkImageInfo::MakeN32Premul(viewport_size.width(),
41                                    viewport_size.height())));
42   }
43   virtual skia::RefPtr<SkCanvas> GetCanvas() OVERRIDE {
44     return skia::SharePtr(surface_->getCanvas());
45   }
46   virtual void PresentCanvas(const gfx::Rect& damage) OVERRIDE {
47     SkBitmap bitmap;
48     bitmap.setConfig(surface_->getCanvas()->imageInfo());
49
50     // TODO(dnicoara) Use SkImage instead to potentially avoid a copy.
51     // See crbug.com/361605 for details.
52     if (surface_->getCanvas()->readPixels(&bitmap, 0, 0)) {
53       base::WorkerPool::PostTask(
54           FROM_HERE, base::Bind(&WriteDataToFile, location_, bitmap), true);
55     }
56   }
57   virtual scoped_ptr<gfx::VSyncProvider> CreateVSyncProvider() OVERRIDE {
58     return scoped_ptr<gfx::VSyncProvider>();
59   }
60
61  private:
62   base::FilePath location_;
63   skia::RefPtr<SkSurface> surface_;
64 };
65
66 }  // namespace
67
68 FileSurfaceFactory::FileSurfaceFactory(
69     const base::FilePath& dump_location)
70     : location_(dump_location) {
71   CHECK(!base::DirectoryExists(location_))
72       << "Location cannot be a directory (" << location_.value() << ")";
73   CHECK(!base::PathExists(location_) || base::PathIsWritable(location_));
74 }
75
76 FileSurfaceFactory::~FileSurfaceFactory() {}
77
78 SurfaceFactoryOzone::HardwareState
79 FileSurfaceFactory::InitializeHardware() {
80   return INITIALIZED;
81 }
82
83 void FileSurfaceFactory::ShutdownHardware() {
84 }
85
86 AcceleratedWidget FileSurfaceFactory::GetAcceleratedWidget() {
87   return 1;
88 }
89
90 scoped_ptr<SurfaceOzoneCanvas> FileSurfaceFactory::CreateCanvasForWidget(
91       gfx::AcceleratedWidget w) {
92   return make_scoped_ptr<SurfaceOzoneCanvas>(new FileSurface(location_));
93 }
94
95 bool FileSurfaceFactory::LoadEGLGLES2Bindings(
96       AddGLLibraryCallback add_gl_library,
97       SetGLGetProcAddressProcCallback set_gl_get_proc_address) {
98   return false;
99 }
100
101 }  // namespace gfx