Revert of Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320) (patchset...
[chromium-blink-merge.git] / ui / ozone / platform / caca / caca_window_manager.cc
blob3e11ad29bed543d5f9ee12520bf303fbe137785f
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"
16 namespace ui {
18 namespace {
20 class CacaSurface : public ui::SurfaceOzoneCanvas {
21 public:
22 CacaSurface(CacaWindow* window);
23 ~CacaSurface() override;
25 bool Initialize();
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;
33 private:
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());
51 return true;
54 skia::RefPtr<SkSurface> CacaSurface::GetSurface() {
55 return surface_;
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(),
65 kN32_SkColorType,
66 kPremul_SkAlphaType);
68 surface_ = skia::AdoptRef(SkSurface::NewRaster(info));
69 if (!surface_)
70 LOG(ERROR) << "Failed to create SkSurface";
72 dither_.reset(caca_create_dither(info.bytesPerPixel() * 8,
73 info.width(),
74 info.height(),
75 info.minRowBytes(),
76 0x00ff0000,
77 0x0000ff00,
78 0x000000ff,
79 0xff000000));
80 if (!dither_)
81 LOG(ERROR) << "failed to create dither";
84 void CacaSurface::PresentCanvas(const gfx::Rect& damage) {
85 TRACE_EVENT0("ozone", "CacaSurface::PresentCanvas");
87 SkImageInfo info;
88 size_t row_bytes;
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),
97 dither_.get(),
98 static_cast<const uint8_t*>(pixels));
99 caca_refresh_display(window_->display());
102 scoped_ptr<gfx::VSyncProvider> CacaSurface::CreateVSyncProvider() {
103 return nullptr;
106 } // namespace
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) {
126 return false;
129 scoped_ptr<ui::SurfaceOzoneCanvas> CacaWindowManager::CreateCanvasForWidget(
130 gfx::AcceleratedWidget widget) {
131 CacaWindow* window = windows_.Lookup(widget);
132 DCHECK(window);
134 scoped_ptr<CacaSurface> canvas(new CacaSurface(window));
135 bool initialized = canvas->Initialize();
136 DCHECK(initialized);
137 return canvas.Pass();
140 } // namespace ui