OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/ozone/impl/file_surface_factory.h" | 5 #include "ui/gfx/ozone/impl/file_surface_factory.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "base/threading/worker_pool.h" | 11 #include "base/threading/worker_pool.h" |
12 #include "third_party/skia/include/core/SkBitmapDevice.h" | 12 #include "third_party/skia/include/core/SkBitmapDevice.h" |
13 #include "third_party/skia/include/core/SkDevice.h" | 13 #include "third_party/skia/include/core/SkDevice.h" |
14 #include "ui/gfx/codec/png_codec.h" | 14 #include "ui/gfx/codec/png_codec.h" |
| 15 #include "ui/gfx/ozone/surface_ozone_base.h" |
| 16 #include "ui/gfx/skia_util.h" |
15 #include "ui/gfx/vsync_provider.h" | 17 #include "ui/gfx/vsync_provider.h" |
16 | 18 |
| 19 namespace gfx { |
| 20 |
17 namespace { | 21 namespace { |
18 | 22 |
19 void WriteDataToFile(const base::FilePath& location, | 23 void WriteDataToFile(const base::FilePath& location, |
20 const SkBitmap& bitmap) { | 24 const SkBitmap& bitmap) { |
21 std::vector<unsigned char> png_data; | 25 std::vector<unsigned char> png_data; |
22 gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, true, &png_data); | 26 gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, true, &png_data); |
23 base::WriteFile(location, | 27 base::WriteFile(location, |
24 reinterpret_cast<const char*>(vector_as_array(&png_data)), | 28 reinterpret_cast<const char*>(vector_as_array(&png_data)), |
25 png_data.size()); | 29 png_data.size()); |
26 } | 30 } |
27 | 31 |
28 } | 32 class FileSurface : public SurfaceOzoneBase { |
| 33 public: |
| 34 FileSurface(const base::FilePath& location) : location_(location) {} |
| 35 virtual ~FileSurface() {} |
29 | 36 |
30 namespace gfx { | 37 bool InitializeCanvas() OVERRIDE { return true; } |
| 38 |
| 39 bool ResizeCanvas(const Size& viewport_size) OVERRIDE { |
| 40 SkImageInfo info = SkImageInfo::MakeN32Premul(viewport_size.width(), |
| 41 viewport_size.height()); |
| 42 device_ = skia::AdoptRef(SkBitmapDevice::Create(info)); |
| 43 canvas_ = skia::AdoptRef(new SkCanvas(device_.get())); |
| 44 return true; |
| 45 } |
| 46 |
| 47 skia::RefPtr<SkCanvas> GetCanvas() OVERRIDE { return canvas_; } |
| 48 |
| 49 bool PresentCanvas() OVERRIDE { |
| 50 SkBitmap bitmap; |
| 51 bitmap.setConfig( |
| 52 SkBitmap::kARGB_8888_Config, device_->width(), device_->height()); |
| 53 |
| 54 if (canvas_->readPixels(&bitmap, 0, 0)) { |
| 55 base::WorkerPool::PostTask( |
| 56 FROM_HERE, base::Bind(&WriteDataToFile, location_, bitmap), true); |
| 57 } |
| 58 return true; |
| 59 } |
| 60 |
| 61 private: |
| 62 base::FilePath location_; |
| 63 skia::RefPtr<SkBitmapDevice> device_; |
| 64 skia::RefPtr<SkCanvas> canvas_; |
| 65 }; |
| 66 |
| 67 } // namespace |
31 | 68 |
32 FileSurfaceFactory::FileSurfaceFactory( | 69 FileSurfaceFactory::FileSurfaceFactory( |
33 const base::FilePath& dump_location) | 70 const base::FilePath& dump_location) |
34 : location_(dump_location) { | 71 : location_(dump_location) { |
35 CHECK(!base::DirectoryExists(location_)) | 72 CHECK(!base::DirectoryExists(location_)) |
36 << "Location cannot be a directory (" << location_.value() << ")"; | 73 << "Location cannot be a directory (" << location_.value() << ")"; |
37 CHECK(!base::PathExists(location_) || base::PathIsWritable(location_)); | 74 CHECK(!base::PathExists(location_) || base::PathIsWritable(location_)); |
38 } | 75 } |
39 | 76 |
40 FileSurfaceFactory::~FileSurfaceFactory() {} | 77 FileSurfaceFactory::~FileSurfaceFactory() {} |
41 | 78 |
42 SurfaceFactoryOzone::HardwareState | 79 SurfaceFactoryOzone::HardwareState |
43 FileSurfaceFactory::InitializeHardware() { | 80 FileSurfaceFactory::InitializeHardware() { |
44 return INITIALIZED; | 81 return INITIALIZED; |
45 } | 82 } |
46 | 83 |
47 void FileSurfaceFactory::ShutdownHardware() { | 84 void FileSurfaceFactory::ShutdownHardware() { |
48 } | 85 } |
49 | 86 |
50 AcceleratedWidget FileSurfaceFactory::GetAcceleratedWidget() { | 87 AcceleratedWidget FileSurfaceFactory::GetAcceleratedWidget() { |
51 return 1; | 88 return 1; |
52 } | 89 } |
53 | 90 |
54 AcceleratedWidget FileSurfaceFactory::RealizeAcceleratedWidget( | 91 scoped_ptr<SurfaceOzone> FileSurfaceFactory::CreateSurfaceForWidget( |
55 AcceleratedWidget widget) { | 92 AcceleratedWidget widget) { |
56 return 1; | 93 return make_scoped_ptr<SurfaceOzone>(new FileSurface(location_)); |
57 } | 94 } |
58 | 95 |
59 bool FileSurfaceFactory::LoadEGLGLES2Bindings( | 96 bool FileSurfaceFactory::LoadEGLGLES2Bindings( |
60 AddGLLibraryCallback add_gl_library, | 97 AddGLLibraryCallback add_gl_library, |
61 SetGLGetProcAddressProcCallback set_gl_get_proc_address) { | 98 SetGLGetProcAddressProcCallback set_gl_get_proc_address) { |
62 return false; | 99 return false; |
63 } | 100 } |
64 | 101 |
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 | 102 } // namespace gfx |
OLD | NEW |