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.
5 #include "ui/ozone/platform/test/test_window_manager.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"
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
)),
32 class FileSurface
: public SurfaceOzoneCanvas
{
34 FileSurface(const base::FilePath
& location
) : location_(location
) {}
35 ~FileSurface() override
{}
37 // SurfaceOzoneCanvas overrides:
38 void ResizeCanvas(const gfx::Size
& viewport_size
) override
{
39 surface_
= skia::AdoptRef(SkSurface::NewRaster(SkImageInfo::MakeN32Premul(
40 viewport_size
.width(), viewport_size
.height())));
42 skia::RefPtr
<SkSurface
> GetSurface() override
{ return surface_
; }
43 void PresentCanvas(const gfx::Rect
& damage
) override
{
44 if (location_
.empty())
47 bitmap
.setInfo(surface_
->getCanvas()->imageInfo());
49 // TODO(dnicoara) Use SkImage instead to potentially avoid a copy.
50 // See crbug.com/361605 for details.
51 if (surface_
->getCanvas()->readPixels(&bitmap
, 0, 0)) {
52 base::WorkerPool::PostTask(
53 FROM_HERE
, base::Bind(&WriteDataToFile
, location_
, bitmap
), true);
56 scoped_ptr
<gfx::VSyncProvider
> CreateVSyncProvider() override
{
61 base::FilePath location_
;
62 skia::RefPtr
<SkSurface
> surface_
;
67 TestWindowManager::TestWindowManager(const base::FilePath
& dump_location
)
68 : location_(dump_location
) {
71 TestWindowManager::~TestWindowManager() {
74 void TestWindowManager::Initialize() {
75 if (location_
.empty())
77 if (!DirectoryExists(location_
) && !base::CreateDirectory(location_
) &&
78 location_
!= base::FilePath("/dev/null"))
79 PLOG(FATAL
) << "unable to create output directory";
80 if (!base::PathIsWritable(location_
))
81 PLOG(FATAL
) << "unable to write to output location";
84 int32_t TestWindowManager::AddWindow(TestWindow
* window
) {
85 return windows_
.Add(window
);
88 void TestWindowManager::RemoveWindow(int32_t window_id
, TestWindow
* window
) {
89 DCHECK_EQ(window
, windows_
.Lookup(window_id
));
90 windows_
.Remove(window_id
);
93 base::FilePath
TestWindowManager::base_path() const {
97 scoped_ptr
<SurfaceOzoneCanvas
> TestWindowManager::CreateCanvasForWidget(
98 gfx::AcceleratedWidget widget
) {
99 TestWindow
* window
= windows_
.Lookup(widget
);
101 return make_scoped_ptr
<SurfaceOzoneCanvas
>(new FileSurface(window
->path()));
104 bool TestWindowManager::LoadEGLGLES2Bindings(
105 AddGLLibraryCallback add_gl_library
,
106 SetGLGetProcAddressProcCallback set_gl_get_proc_address
) {