Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ui / ozone / platform / test / test_window_manager.cc
1 // Copyright 2014 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/ozone/platform/test/test_window_manager.h"
6
7 #include "base/bind.h"
8 #include "base/files/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/skia_util.h"
16 #include "ui/gfx/vsync_provider.h"
17 #include "ui/ozone/public/surface_ozone_canvas.h"
18
19 namespace ui {
20
21 namespace {
22
23 void WriteDataToFile(const base::FilePath& location, const SkBitmap& bitmap) {
24   DCHECK(!location.empty());
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 gfx::Size& viewport_size) OVERRIDE {
39     surface_ = skia::AdoptRef(SkSurface::NewRaster(SkImageInfo::MakeN32Premul(
40         viewport_size.width(), viewport_size.height())));
41   }
42   virtual skia::RefPtr<SkCanvas> GetCanvas() OVERRIDE {
43     return skia::SharePtr(surface_->getCanvas());
44   }
45   virtual void PresentCanvas(const gfx::Rect& damage) OVERRIDE {
46     if (location_.empty())
47       return;
48     SkBitmap bitmap;
49     bitmap.setInfo(surface_->getCanvas()->imageInfo());
50
51     // TODO(dnicoara) Use SkImage instead to potentially avoid a copy.
52     // See crbug.com/361605 for details.
53     if (surface_->getCanvas()->readPixels(&bitmap, 0, 0)) {
54       base::WorkerPool::PostTask(
55           FROM_HERE, base::Bind(&WriteDataToFile, location_, bitmap), true);
56     }
57   }
58   virtual scoped_ptr<gfx::VSyncProvider> CreateVSyncProvider() OVERRIDE {
59     return scoped_ptr<gfx::VSyncProvider>();
60   }
61
62  private:
63   base::FilePath location_;
64   skia::RefPtr<SkSurface> surface_;
65 };
66
67 }  // namespace
68
69 TestWindowManager::TestWindowManager(const base::FilePath& dump_location)
70     : location_(dump_location) {
71 }
72
73 TestWindowManager::~TestWindowManager() {
74 }
75
76 void TestWindowManager::Initialize() {
77   if (location_.empty())
78     return;
79   if (!DirectoryExists(location_) && !base::CreateDirectory(location_) &&
80       location_ != base::FilePath("/dev/null"))
81     PLOG(FATAL) << "unable to create output directory";
82   if (!base::PathIsWritable(location_))
83     PLOG(FATAL) << "unable to write to output location";
84 }
85
86 int32_t TestWindowManager::AddWindow(TestWindow* window) {
87   return windows_.Add(window);
88 }
89
90 void TestWindowManager::RemoveWindow(int32_t window_id, TestWindow* window) {
91   DCHECK_EQ(window, windows_.Lookup(window_id));
92   windows_.Remove(window_id);
93 }
94
95 base::FilePath TestWindowManager::base_path() const {
96   return location_;
97 }
98
99 scoped_ptr<SurfaceOzoneCanvas> TestWindowManager::CreateCanvasForWidget(
100     gfx::AcceleratedWidget widget) {
101   TestWindow* window = windows_.Lookup(widget);
102   DCHECK(window);
103   return make_scoped_ptr<SurfaceOzoneCanvas>(new FileSurface(window->path()));
104 }
105
106 bool TestWindowManager::LoadEGLGLES2Bindings(
107     AddGLLibraryCallback add_gl_library,
108     SetGLGetProcAddressProcCallback set_gl_get_proc_address) {
109   return false;
110 }
111
112 }  // namespace ui