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/caca/caca_surface_factory.h"
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/skia_util.h"
11 #include "ui/gfx/vsync_provider.h"
12 #include "ui/ozone/platform/caca/caca_connection.h"
13 #include "ui/ozone/public/surface_ozone_canvas.h"
19 const gfx::AcceleratedWidget kDefaultWidgetHandle
= 1;
21 class CacaSurface
: public ui::SurfaceOzoneCanvas
{
23 CacaSurface(CacaConnection
* connection
);
24 virtual ~CacaSurface();
28 // ui::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
;
35 CacaConnection
* connection_
; // Not owned.
37 caca_dither_t
* dither_
;
39 skia::RefPtr
<SkSurface
> surface_
;
41 DISALLOW_COPY_AND_ASSIGN(CacaSurface
);
44 CacaSurface::CacaSurface(CacaConnection
* connection
)
45 : connection_(connection
) {}
47 CacaSurface::~CacaSurface() {
48 caca_free_dither(dither_
);
51 bool CacaSurface::Initialize() {
52 SkImageInfo info
= SkImageInfo::Make(connection_
->bitmap_size().width(),
53 connection_
->bitmap_size().height(),
57 surface_
= skia::AdoptRef(SkSurface::NewRaster(info
));
59 LOG(ERROR
) << "Failed to create SkCanvas";
63 dither_
= caca_create_dither(
64 info
.bytesPerPixel() * 8,
76 skia::RefPtr
<SkCanvas
> CacaSurface::GetCanvas() {
77 return skia::SharePtr
<SkCanvas
>(surface_
->getCanvas());
80 void CacaSurface::ResizeCanvas(const gfx::Size
& viewport_size
) {
84 void CacaSurface::PresentCanvas(const gfx::Rect
& damage
) {
87 const void* pixels
= surface_
->peekPixels(&info
, &row_bytes
);
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
),
94 static_cast<const uint8_t*>(pixels
));
95 caca_refresh_display(connection_
->display());
98 scoped_ptr
<gfx::VSyncProvider
> CacaSurface::CreateVSyncProvider() {
99 return scoped_ptr
<gfx::VSyncProvider
>();
104 CacaSurfaceFactory::CacaSurfaceFactory(CacaConnection
* connection
)
105 : state_(UNINITIALIZED
),
106 connection_(connection
) {
109 CacaSurfaceFactory::~CacaSurfaceFactory() {
110 if (state_
== INITIALIZED
)
114 ui::SurfaceFactoryOzone::HardwareState
115 CacaSurfaceFactory::InitializeHardware() {
116 connection_
->Initialize();
117 state_
= INITIALIZED
;
121 void CacaSurfaceFactory::ShutdownHardware() {
122 CHECK_EQ(INITIALIZED
, state_
);
123 state_
= UNINITIALIZED
;
126 gfx::AcceleratedWidget
CacaSurfaceFactory::GetAcceleratedWidget() {
127 return kDefaultWidgetHandle
;
130 bool CacaSurfaceFactory::LoadEGLGLES2Bindings(
131 AddGLLibraryCallback add_gl_library
,
132 SetGLGetProcAddressProcCallback set_gl_get_proc_address
) {
137 scoped_ptr
<ui::SurfaceOzoneCanvas
> CacaSurfaceFactory::CreateCanvasForWidget(
138 gfx::AcceleratedWidget widget
) {
139 CHECK_EQ(INITIALIZED
, state_
);
140 CHECK_EQ(kDefaultWidgetHandle
, widget
);
142 scoped_ptr
<CacaSurface
> canvas(new CacaSurface(connection_
));
143 CHECK(canvas
->Initialize());
144 return canvas
.PassAs
<ui::SurfaceOzoneCanvas
>();