| 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/ozone/platform/test/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/SkCanvas.h" | 12 #include "third_party/skia/include/core/SkCanvas.h" |
| 13 #include "third_party/skia/include/core/SkSurface.h" | 13 #include "third_party/skia/include/core/SkSurface.h" |
| 14 #include "ui/gfx/codec/png_codec.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" | 15 #include "ui/gfx/skia_util.h" |
| 17 #include "ui/gfx/vsync_provider.h" | 16 #include "ui/gfx/vsync_provider.h" |
| 17 #include "ui/ozone/factories/surface_ozone_canvas.h" |
| 18 | 18 |
| 19 namespace gfx { | 19 namespace ui { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 void WriteDataToFile(const base::FilePath& location, | 23 void WriteDataToFile(const base::FilePath& location, const SkBitmap& bitmap) { |
| 24 const SkBitmap& bitmap) { | |
| 25 std::vector<unsigned char> png_data; | 24 std::vector<unsigned char> png_data; |
| 26 gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, true, &png_data); | 25 gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, true, &png_data); |
| 27 base::WriteFile(location, | 26 base::WriteFile(location, |
| 28 reinterpret_cast<const char*>(vector_as_array(&png_data)), | 27 reinterpret_cast<const char*>(vector_as_array(&png_data)), |
| 29 png_data.size()); | 28 png_data.size()); |
| 30 } | 29 } |
| 31 | 30 |
| 32 class FileSurface : public SurfaceOzoneCanvas { | 31 class FileSurface : public SurfaceOzoneCanvas { |
| 33 public: | 32 public: |
| 34 FileSurface(const base::FilePath& location) : location_(location) {} | 33 FileSurface(const base::FilePath& location) : location_(location) {} |
| 35 virtual ~FileSurface() {} | 34 virtual ~FileSurface() {} |
| 36 | 35 |
| 37 // SurfaceOzoneCanvas overrides: | 36 // SurfaceOzoneCanvas overrides: |
| 38 virtual void ResizeCanvas(const Size& viewport_size) OVERRIDE { | 37 virtual void ResizeCanvas(const gfx::Size& viewport_size) OVERRIDE { |
| 39 surface_ = skia::AdoptRef(SkSurface::NewRaster( | 38 surface_ = skia::AdoptRef(SkSurface::NewRaster(SkImageInfo::MakeN32Premul( |
| 40 SkImageInfo::MakeN32Premul(viewport_size.width(), | 39 viewport_size.width(), viewport_size.height()))); |
| 41 viewport_size.height()))); | |
| 42 } | 40 } |
| 43 virtual skia::RefPtr<SkCanvas> GetCanvas() OVERRIDE { | 41 virtual skia::RefPtr<SkCanvas> GetCanvas() OVERRIDE { |
| 44 return skia::SharePtr(surface_->getCanvas()); | 42 return skia::SharePtr(surface_->getCanvas()); |
| 45 } | 43 } |
| 46 virtual void PresentCanvas(const gfx::Rect& damage) OVERRIDE { | 44 virtual void PresentCanvas(const gfx::Rect& damage) OVERRIDE { |
| 47 SkBitmap bitmap; | 45 SkBitmap bitmap; |
| 48 bitmap.setConfig(surface_->getCanvas()->imageInfo()); | 46 bitmap.setConfig(surface_->getCanvas()->imageInfo()); |
| 49 | 47 |
| 50 // TODO(dnicoara) Use SkImage instead to potentially avoid a copy. | 48 // TODO(dnicoara) Use SkImage instead to potentially avoid a copy. |
| 51 // See crbug.com/361605 for details. | 49 // See crbug.com/361605 for details. |
| 52 if (surface_->getCanvas()->readPixels(&bitmap, 0, 0)) { | 50 if (surface_->getCanvas()->readPixels(&bitmap, 0, 0)) { |
| 53 base::WorkerPool::PostTask( | 51 base::WorkerPool::PostTask( |
| 54 FROM_HERE, base::Bind(&WriteDataToFile, location_, bitmap), true); | 52 FROM_HERE, base::Bind(&WriteDataToFile, location_, bitmap), true); |
| 55 } | 53 } |
| 56 } | 54 } |
| 57 virtual scoped_ptr<gfx::VSyncProvider> CreateVSyncProvider() OVERRIDE { | 55 virtual scoped_ptr<gfx::VSyncProvider> CreateVSyncProvider() OVERRIDE { |
| 58 return scoped_ptr<gfx::VSyncProvider>(); | 56 return scoped_ptr<gfx::VSyncProvider>(); |
| 59 } | 57 } |
| 60 | 58 |
| 61 private: | 59 private: |
| 62 base::FilePath location_; | 60 base::FilePath location_; |
| 63 skia::RefPtr<SkSurface> surface_; | 61 skia::RefPtr<SkSurface> surface_; |
| 64 }; | 62 }; |
| 65 | 63 |
| 66 } // namespace | 64 } // namespace |
| 67 | 65 |
| 68 FileSurfaceFactory::FileSurfaceFactory( | 66 FileSurfaceFactory::FileSurfaceFactory(const base::FilePath& dump_location) |
| 69 const base::FilePath& dump_location) | |
| 70 : location_(dump_location) { | 67 : location_(dump_location) { |
| 71 CHECK(!base::DirectoryExists(location_)) | 68 CHECK(!base::DirectoryExists(location_)) << "Location cannot be a directory (" |
| 72 << "Location cannot be a directory (" << location_.value() << ")"; | 69 << location_.value() << ")"; |
| 73 CHECK(!base::PathExists(location_) || base::PathIsWritable(location_)); | 70 CHECK(!base::PathExists(location_) || base::PathIsWritable(location_)); |
| 74 } | 71 } |
| 75 | 72 |
| 76 FileSurfaceFactory::~FileSurfaceFactory() {} | 73 FileSurfaceFactory::~FileSurfaceFactory() { |
| 74 } |
| 77 | 75 |
| 78 SurfaceFactoryOzone::HardwareState | 76 SurfaceFactoryOzone::HardwareState FileSurfaceFactory::InitializeHardware() { |
| 79 FileSurfaceFactory::InitializeHardware() { | |
| 80 return INITIALIZED; | 77 return INITIALIZED; |
| 81 } | 78 } |
| 82 | 79 |
| 83 void FileSurfaceFactory::ShutdownHardware() { | 80 void FileSurfaceFactory::ShutdownHardware() { |
| 84 } | 81 } |
| 85 | 82 |
| 86 AcceleratedWidget FileSurfaceFactory::GetAcceleratedWidget() { | 83 gfx::AcceleratedWidget FileSurfaceFactory::GetAcceleratedWidget() { |
| 87 return 1; | 84 return 1; |
| 88 } | 85 } |
| 89 | 86 |
| 90 scoped_ptr<SurfaceOzoneCanvas> FileSurfaceFactory::CreateCanvasForWidget( | 87 scoped_ptr<SurfaceOzoneCanvas> FileSurfaceFactory::CreateCanvasForWidget( |
| 91 gfx::AcceleratedWidget w) { | 88 gfx::AcceleratedWidget w) { |
| 92 return make_scoped_ptr<SurfaceOzoneCanvas>(new FileSurface(location_)); | 89 return make_scoped_ptr<SurfaceOzoneCanvas>(new FileSurface(location_)); |
| 93 } | 90 } |
| 94 | 91 |
| 95 bool FileSurfaceFactory::LoadEGLGLES2Bindings( | 92 bool FileSurfaceFactory::LoadEGLGLES2Bindings( |
| 96 AddGLLibraryCallback add_gl_library, | 93 AddGLLibraryCallback add_gl_library, |
| 97 SetGLGetProcAddressProcCallback set_gl_get_proc_address) { | 94 SetGLGetProcAddressProcCallback set_gl_get_proc_address) { |
| 98 return false; | 95 return false; |
| 99 } | 96 } |
| 100 | 97 |
| 101 } // namespace gfx | 98 } // namespace ui |
| OLD | NEW |