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.
5 #include "ui/gfx/ozone/impl/file_surface_factory.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"
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)),
32 class FileSurface : public SurfaceOzoneCanvas {
34 FileSurface(const base::FilePath& location) : location_(location) {}
35 virtual ~FileSurface() {}
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())));
43 virtual skia::RefPtr<SkCanvas> GetCanvas() OVERRIDE {
44 return skia::SharePtr(surface_->getCanvas());
46 virtual void PresentCanvas(const gfx::Rect& damage) OVERRIDE {
48 bitmap.setConfig(surface_->getCanvas()->imageInfo());
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);
57 virtual scoped_ptr<gfx::VSyncProvider> CreateVSyncProvider() OVERRIDE {
58 return scoped_ptr<gfx::VSyncProvider>();
62 base::FilePath location_;
63 skia::RefPtr<SkSurface> surface_;
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_));
76 FileSurfaceFactory::~FileSurfaceFactory() {}
78 SurfaceFactoryOzone::HardwareState
79 FileSurfaceFactory::InitializeHardware() {
83 void FileSurfaceFactory::ShutdownHardware() {
86 AcceleratedWidget FileSurfaceFactory::GetAcceleratedWidget() {
90 scoped_ptr<SurfaceOzoneCanvas> FileSurfaceFactory::CreateCanvasForWidget(
91 gfx::AcceleratedWidget w) {
92 return make_scoped_ptr<SurfaceOzoneCanvas>(new FileSurface(location_));
95 bool FileSurfaceFactory::LoadEGLGLES2Bindings(
96 AddGLLibraryCallback add_gl_library,
97 SetGLGetProcAddressProcCallback set_gl_get_proc_address) {