Home | History | Annotate | Download | only in aura
      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.
      4 
      5 #include "mojo/aura/context_factory_mojo.h"
      6 
      7 #include "base/bind.h"
      8 #include "cc/output/output_surface.h"
      9 #include "cc/output/software_output_device.h"
     10 #include "cc/resources/shared_bitmap_manager.h"
     11 #include "mojo/aura/window_tree_host_mojo.h"
     12 #include "skia/ext/platform_canvas.h"
     13 #include "ui/compositor/reflector.h"
     14 
     15 namespace mojo {
     16 namespace {
     17 
     18 void FreeSharedBitmap(cc::SharedBitmap* shared_bitmap) {
     19   delete shared_bitmap->memory();
     20 }
     21 
     22 void IgnoreSharedBitmap(cc::SharedBitmap* shared_bitmap) {}
     23 
     24 class SoftwareOutputDeviceViewManager : public cc::SoftwareOutputDevice {
     25  public:
     26   explicit SoftwareOutputDeviceViewManager(ui::Compositor* compositor)
     27       : compositor_(compositor) {
     28   }
     29   virtual ~SoftwareOutputDeviceViewManager() {}
     30 
     31   // cc::SoftwareOutputDevice:
     32   virtual void EndPaint(cc::SoftwareFrameData* frame_data) OVERRIDE {
     33     WindowTreeHostMojo* window_tree_host =
     34         WindowTreeHostMojo::ForCompositor(compositor_);
     35     DCHECK(window_tree_host);
     36     window_tree_host->SetContents(
     37         skia::GetTopDevice(*canvas_)->accessBitmap(true));
     38 
     39     SoftwareOutputDevice::EndPaint(frame_data);
     40   }
     41 
     42  private:
     43   ui::Compositor* compositor_;
     44 
     45   DISALLOW_COPY_AND_ASSIGN(SoftwareOutputDeviceViewManager);
     46 };
     47 
     48 // TODO(sky): this is a copy from cc/test. Copy to a common place.
     49 class TestSharedBitmapManager : public cc::SharedBitmapManager {
     50  public:
     51   TestSharedBitmapManager() {}
     52   virtual ~TestSharedBitmapManager() {}
     53 
     54   virtual scoped_ptr<cc::SharedBitmap> AllocateSharedBitmap(
     55       const gfx::Size& size) OVERRIDE {
     56     base::AutoLock lock(lock_);
     57     scoped_ptr<base::SharedMemory> memory(new base::SharedMemory);
     58     memory->CreateAndMapAnonymous(size.GetArea() * 4);
     59     cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
     60     bitmap_map_[id] = memory.get();
     61     return scoped_ptr<cc::SharedBitmap>(
     62         new cc::SharedBitmap(memory.release(), id,
     63                              base::Bind(&FreeSharedBitmap)));
     64   }
     65 
     66   virtual scoped_ptr<cc::SharedBitmap> GetSharedBitmapFromId(
     67       const gfx::Size&,
     68       const cc::SharedBitmapId& id) OVERRIDE {
     69     base::AutoLock lock(lock_);
     70     if (bitmap_map_.find(id) == bitmap_map_.end())
     71       return scoped_ptr<cc::SharedBitmap>();
     72     return scoped_ptr<cc::SharedBitmap>(
     73         new cc::SharedBitmap(bitmap_map_[id], id,
     74                              base::Bind(&IgnoreSharedBitmap)));
     75   }
     76 
     77   virtual scoped_ptr<cc::SharedBitmap> GetBitmapForSharedMemory(
     78       base::SharedMemory* memory) OVERRIDE {
     79     base::AutoLock lock(lock_);
     80     cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
     81     bitmap_map_[id] = memory;
     82     return scoped_ptr<cc::SharedBitmap>(
     83         new cc::SharedBitmap(memory, id, base::Bind(&IgnoreSharedBitmap)));
     84   }
     85 
     86  private:
     87   base::Lock lock_;
     88   std::map<cc::SharedBitmapId, base::SharedMemory*> bitmap_map_;
     89 
     90   DISALLOW_COPY_AND_ASSIGN(TestSharedBitmapManager);
     91 };
     92 
     93 }  // namespace
     94 
     95 ContextFactoryMojo::ContextFactoryMojo()
     96     : shared_bitmap_manager_(new TestSharedBitmapManager()) {
     97 }
     98 
     99 ContextFactoryMojo::~ContextFactoryMojo() {}
    100 
    101 scoped_ptr<cc::OutputSurface> ContextFactoryMojo::CreateOutputSurface(
    102     ui::Compositor* compositor,
    103     bool software_fallback) {
    104   scoped_ptr<cc::SoftwareOutputDevice> output_device(
    105       new SoftwareOutputDeviceViewManager(compositor));
    106   return make_scoped_ptr(new cc::OutputSurface(output_device.Pass()));
    107 }
    108 
    109 scoped_refptr<ui::Reflector> ContextFactoryMojo::CreateReflector(
    110     ui::Compositor* mirroed_compositor,
    111     ui::Layer* mirroring_layer) {
    112   return new ui::Reflector();
    113 }
    114 
    115 void ContextFactoryMojo::RemoveReflector(
    116     scoped_refptr<ui::Reflector> reflector) {
    117 }
    118 
    119 scoped_refptr<cc::ContextProvider>
    120 ContextFactoryMojo::SharedMainThreadContextProvider() {
    121   return scoped_refptr<cc::ContextProvider>(NULL);
    122 }
    123 
    124 void ContextFactoryMojo::RemoveCompositor(ui::Compositor* compositor) {}
    125 
    126 bool ContextFactoryMojo::DoesCreateTestContexts() { return false; }
    127 
    128 cc::SharedBitmapManager* ContextFactoryMojo::GetSharedBitmapManager() {
    129   return shared_bitmap_manager_.get();
    130 }
    131 
    132 base::MessageLoopProxy* ContextFactoryMojo::GetCompositorMessageLoop() {
    133   return NULL;
    134 }
    135 
    136 }  // namespace mojo
    137