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/ozone/platform/test/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/skia_util.h"
16 #include "ui/gfx/vsync_provider.h"
17 #include "ui/ozone/public/surface_ozone_canvas.h"
23 void WriteDataToFile(const base::FilePath
& location
, const SkBitmap
& bitmap
) {
24 std::vector
<unsigned char> png_data
;
25 gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap
, true, &png_data
);
26 base::WriteFile(location
,
27 reinterpret_cast<const char*>(vector_as_array(&png_data
)),
31 class FileSurface
: public SurfaceOzoneCanvas
{
33 FileSurface(const base::FilePath
& location
) : location_(location
) {}
34 virtual ~FileSurface() {}
36 // SurfaceOzoneCanvas overrides:
37 virtual void ResizeCanvas(const gfx::Size
& viewport_size
) OVERRIDE
{
38 surface_
= skia::AdoptRef(SkSurface::NewRaster(SkImageInfo::MakeN32Premul(
39 viewport_size
.width(), viewport_size
.height())));
41 virtual skia::RefPtr
<SkCanvas
> GetCanvas() OVERRIDE
{
42 return skia::SharePtr(surface_
->getCanvas());
44 virtual void PresentCanvas(const gfx::Rect
& damage
) OVERRIDE
{
46 bitmap
.setInfo(surface_
->getCanvas()->imageInfo());
48 // TODO(dnicoara) Use SkImage instead to potentially avoid a copy.
49 // See crbug.com/361605 for details.
50 if (surface_
->getCanvas()->readPixels(&bitmap
, 0, 0)) {
51 base::WorkerPool::PostTask(
52 FROM_HERE
, base::Bind(&WriteDataToFile
, location_
, bitmap
), true);
55 virtual scoped_ptr
<gfx::VSyncProvider
> CreateVSyncProvider() OVERRIDE
{
56 return scoped_ptr
<gfx::VSyncProvider
>();
60 base::FilePath location_
;
61 skia::RefPtr
<SkSurface
> surface_
;
66 FileSurfaceFactory::FileSurfaceFactory(const base::FilePath
& dump_location
)
67 : location_(dump_location
) {
68 CHECK(!base::DirectoryExists(location_
)) << "Location cannot be a directory ("
69 << location_
.value() << ")";
70 CHECK(!base::PathExists(location_
) || base::PathIsWritable(location_
));
73 FileSurfaceFactory::~FileSurfaceFactory() {
76 SurfaceFactoryOzone::HardwareState
FileSurfaceFactory::InitializeHardware() {
80 void FileSurfaceFactory::ShutdownHardware() {
83 gfx::AcceleratedWidget
FileSurfaceFactory::GetAcceleratedWidget() {
87 scoped_ptr
<SurfaceOzoneCanvas
> FileSurfaceFactory::CreateCanvasForWidget(
88 gfx::AcceleratedWidget w
) {
89 return make_scoped_ptr
<SurfaceOzoneCanvas
>(new FileSurface(location_
));
92 bool FileSurfaceFactory::LoadEGLGLES2Bindings(
93 AddGLLibraryCallback add_gl_library
,
94 SetGLGetProcAddressProcCallback set_gl_get_proc_address
) {