Home | History | Annotate | Download | only in test
      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.
      4 
      5 #include "ui/ozone/platform/test/file_surface_factory.h"
      6 
      7 #include "base/bind.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"
     18 
     19 namespace ui {
     20 
     21 namespace {
     22 
     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)),
     28                   png_data.size());
     29 }
     30 
     31 class FileSurface : public SurfaceOzoneCanvas {
     32  public:
     33   FileSurface(const base::FilePath& location) : location_(location) {}
     34   virtual ~FileSurface() {}
     35 
     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())));
     40   }
     41   virtual skia::RefPtr<SkCanvas> GetCanvas() OVERRIDE {
     42     return skia::SharePtr(surface_->getCanvas());
     43   }
     44   virtual void PresentCanvas(const gfx::Rect& damage) OVERRIDE {
     45     SkBitmap bitmap;
     46     bitmap.setInfo(surface_->getCanvas()->imageInfo());
     47 
     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);
     53     }
     54   }
     55   virtual scoped_ptr<gfx::VSyncProvider> CreateVSyncProvider() OVERRIDE {
     56     return scoped_ptr<gfx::VSyncProvider>();
     57   }
     58 
     59  private:
     60   base::FilePath location_;
     61   skia::RefPtr<SkSurface> surface_;
     62 };
     63 
     64 }  // namespace
     65 
     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_));
     71 }
     72 
     73 FileSurfaceFactory::~FileSurfaceFactory() {
     74 }
     75 
     76 SurfaceFactoryOzone::HardwareState FileSurfaceFactory::InitializeHardware() {
     77   return INITIALIZED;
     78 }
     79 
     80 void FileSurfaceFactory::ShutdownHardware() {
     81 }
     82 
     83 gfx::AcceleratedWidget FileSurfaceFactory::GetAcceleratedWidget() {
     84   return 1;
     85 }
     86 
     87 scoped_ptr<SurfaceOzoneCanvas> FileSurfaceFactory::CreateCanvasForWidget(
     88     gfx::AcceleratedWidget w) {
     89   return make_scoped_ptr<SurfaceOzoneCanvas>(new FileSurface(location_));
     90 }
     91 
     92 bool FileSurfaceFactory::LoadEGLGLES2Bindings(
     93     AddGLLibraryCallback add_gl_library,
     94     SetGLGetProcAddressProcCallback set_gl_get_proc_address) {
     95   return false;
     96 }
     97 
     98 }  // namespace ui
     99