Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / ozone / platform / caca / caca_surface_factory.cc
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 "ui/ozone/platform/caca/caca_surface_factory.h"
6
7 #include "third_party/skia/include/core/SkBitmap.h"
8 #include "third_party/skia/include/core/SkCanvas.h"
9 #include "third_party/skia/include/core/SkSurface.h"
10 #include "ui/gfx/ozone/surface_ozone_canvas.h"
11 #include "ui/gfx/skia_util.h"
12 #include "ui/gfx/vsync_provider.h"
13 #include "ui/ozone/platform/caca/caca_connection.h"
14
15 namespace ui {
16
17 namespace {
18
19 const gfx::AcceleratedWidget kDefaultWidgetHandle = 1;
20
21 class CacaSurface : public gfx::SurfaceOzoneCanvas {
22  public:
23   CacaSurface(CacaConnection* connection);
24   virtual ~CacaSurface();
25
26   bool Initialize();
27
28   // gfx::SurfaceOzoneCanvas overrides:
29   virtual skia::RefPtr<SkCanvas> GetCanvas() OVERRIDE;
30   virtual void ResizeCanvas(const gfx::Size& viewport_size) OVERRIDE;
31   virtual void PresentCanvas(const gfx::Rect& damage) OVERRIDE;
32   virtual scoped_ptr<gfx::VSyncProvider> CreateVSyncProvider() OVERRIDE;
33
34  private:
35   CacaConnection* connection_;  // Not owned.
36
37   caca_dither_t* dither_;
38
39   skia::RefPtr<SkSurface> surface_;
40
41   DISALLOW_COPY_AND_ASSIGN(CacaSurface);
42 };
43
44 CacaSurface::CacaSurface(CacaConnection* connection)
45   : connection_(connection) {}
46
47 CacaSurface::~CacaSurface() {
48   caca_free_dither(dither_);
49 }
50
51 bool CacaSurface::Initialize() {
52   SkImageInfo info = SkImageInfo::Make(connection_->bitmap_size().width(),
53                                        connection_->bitmap_size().height(),
54                                        kPMColor_SkColorType,
55                                        kPremul_SkAlphaType);
56
57   surface_ = skia::AdoptRef(SkSurface::NewRaster(info));
58   if (!surface_) {
59     LOG(ERROR) << "Failed to create SkCanvas";
60     return false;
61   }
62
63   dither_ = caca_create_dither(
64       info.bytesPerPixel() * 8,
65       info.width(),
66       info.height(),
67       SkBitmap::ComputeRowBytes(SkBitmap::kARGB_8888_Config, info.width()),
68       0x00ff0000,
69       0x0000ff00,
70       0x000000ff,
71       0xff000000);
72
73   return true;
74 }
75
76 skia::RefPtr<SkCanvas> CacaSurface::GetCanvas() {
77   return skia::SharePtr<SkCanvas>(surface_->getCanvas());
78 }
79
80 void CacaSurface::ResizeCanvas(const gfx::Size& viewport_size) {
81   NOTIMPLEMENTED();
82 }
83
84 void CacaSurface::PresentCanvas(const gfx::Rect& damage) {
85   SkImageInfo info;
86   size_t row_bytes;
87   const void* pixels = surface_->peekPixels(&info, &row_bytes);
88
89   caca_canvas_t* canvas = caca_get_canvas(connection_->display());
90   caca_dither_bitmap(canvas, 0, 0,
91                      caca_get_canvas_width(canvas),
92                      caca_get_canvas_height(canvas),
93                      dither_,
94                      static_cast<const uint8_t*>(pixels));
95   caca_refresh_display(connection_->display());
96 }
97
98 scoped_ptr<gfx::VSyncProvider> CacaSurface::CreateVSyncProvider() {
99   return scoped_ptr<gfx::VSyncProvider>();
100 }
101
102 }  // namespace
103
104 CacaSurfaceFactory::CacaSurfaceFactory(CacaConnection* connection)
105     : state_(UNINITIALIZED),
106       connection_(connection) {
107 }
108
109 CacaSurfaceFactory::~CacaSurfaceFactory() {
110   if (state_ == INITIALIZED)
111     ShutdownHardware();
112 }
113
114 gfx::SurfaceFactoryOzone::HardwareState
115 CacaSurfaceFactory::InitializeHardware() {
116   connection_->Initialize();
117   state_ = INITIALIZED;
118   return state_;
119 }
120
121 void CacaSurfaceFactory::ShutdownHardware() {
122   CHECK_EQ(INITIALIZED, state_);
123   state_ = UNINITIALIZED;
124 }
125
126 gfx::AcceleratedWidget CacaSurfaceFactory::GetAcceleratedWidget() {
127   return kDefaultWidgetHandle;
128 }
129
130 bool CacaSurfaceFactory::LoadEGLGLES2Bindings(
131       AddGLLibraryCallback add_gl_library,
132       SetGLGetProcAddressProcCallback set_gl_get_proc_address) {
133   NOTREACHED();
134   return false;
135 }
136
137 scoped_ptr<gfx::SurfaceOzoneCanvas> CacaSurfaceFactory::CreateCanvasForWidget(
138     gfx::AcceleratedWidget widget) {
139   CHECK_EQ(INITIALIZED, state_);
140   CHECK_EQ(kDefaultWidgetHandle, widget);
141
142   scoped_ptr<CacaSurface> canvas(new CacaSurface(connection_));
143   CHECK(canvas->Initialize());
144   return canvas.PassAs<gfx::SurfaceOzoneCanvas>();
145 }
146
147 }  // namespace ui