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_window_manager.h"
7 #include "base/trace_event/trace_event.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_window.h"
13 #include "ui/ozone/platform/caca/scoped_caca_types.h"
14 #include "ui/ozone/public/surface_ozone_canvas.h"
20 class CacaSurface
: public ui::SurfaceOzoneCanvas
{
22 CacaSurface(CacaWindow
* window
);
23 ~CacaSurface() override
;
27 // ui::SurfaceOzoneCanvas overrides:
28 skia::RefPtr
<SkSurface
> GetSurface() override
;
29 void ResizeCanvas(const gfx::Size
& viewport_size
) override
;
30 void PresentCanvas(const gfx::Rect
& damage
) override
;
31 scoped_ptr
<gfx::VSyncProvider
> CreateVSyncProvider() override
;
34 CacaWindow
* window_
; // Not owned.
36 ScopedCacaDither dither_
;
38 skia::RefPtr
<SkSurface
> surface_
;
40 DISALLOW_COPY_AND_ASSIGN(CacaSurface
);
43 CacaSurface::CacaSurface(CacaWindow
* window
) : window_(window
) {
46 CacaSurface::~CacaSurface() {
49 bool CacaSurface::Initialize() {
50 ResizeCanvas(window_
->bitmap_size());
54 skia::RefPtr
<SkSurface
> CacaSurface::GetSurface() {
58 void CacaSurface::ResizeCanvas(const gfx::Size
& viewport_size
) {
59 TRACE_EVENT0("ozone", "CacaSurface::ResizeCanvas");
61 VLOG(2) << "creating libcaca surface with from window " << (void*)window_
;
63 SkImageInfo info
= SkImageInfo::Make(window_
->bitmap_size().width(),
64 window_
->bitmap_size().height(),
68 surface_
= skia::AdoptRef(SkSurface::NewRaster(info
));
70 LOG(ERROR
) << "Failed to create SkSurface";
72 dither_
.reset(caca_create_dither(info
.bytesPerPixel() * 8,
81 LOG(ERROR
) << "failed to create dither";
84 void CacaSurface::PresentCanvas(const gfx::Rect
& damage
) {
85 TRACE_EVENT0("ozone", "CacaSurface::PresentCanvas");
89 const void* pixels
= surface_
->peekPixels(&info
, &row_bytes
);
91 caca_canvas_t
* canvas
= caca_get_canvas(window_
->display());
92 caca_dither_bitmap(canvas
,
95 caca_get_canvas_width(canvas
),
96 caca_get_canvas_height(canvas
),
98 static_cast<const uint8_t*>(pixels
));
99 caca_refresh_display(window_
->display());
102 scoped_ptr
<gfx::VSyncProvider
> CacaSurface::CreateVSyncProvider() {
108 CacaWindowManager::CacaWindowManager() {
111 int32_t CacaWindowManager::AddWindow(CacaWindow
* window
) {
112 return windows_
.Add(window
);
115 void CacaWindowManager::RemoveWindow(int window_id
, CacaWindow
* window
) {
116 DCHECK_EQ(window
, windows_
.Lookup(window_id
));
117 windows_
.Remove(window_id
);
120 CacaWindowManager::~CacaWindowManager() {
123 bool CacaWindowManager::LoadEGLGLES2Bindings(
124 AddGLLibraryCallback add_gl_library
,
125 SetGLGetProcAddressProcCallback set_gl_get_proc_address
) {
129 scoped_ptr
<ui::SurfaceOzoneCanvas
> CacaWindowManager::CreateCanvasForWidget(
130 gfx::AcceleratedWidget widget
) {
131 CacaWindow
* window
= windows_
.Lookup(widget
);
134 scoped_ptr
<CacaSurface
> canvas(new CacaSurface(window
));
135 bool initialized
= canvas
->Initialize();
137 return canvas
.Pass();