| OLD | NEW |
| (Empty) |
| 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_window_manager.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/macros.h" | |
| 10 #include "base/trace_event/trace_event.h" | |
| 11 #include "third_party/skia/include/core/SkCanvas.h" | |
| 12 #include "third_party/skia/include/core/SkRefCnt.h" | |
| 13 #include "third_party/skia/include/core/SkSurface.h" | |
| 14 #include "ui/gfx/skia_util.h" | |
| 15 #include "ui/gfx/vsync_provider.h" | |
| 16 #include "ui/ozone/platform/caca/caca_window.h" | |
| 17 #include "ui/ozone/platform/caca/scoped_caca_types.h" | |
| 18 #include "ui/ozone/public/surface_ozone_canvas.h" | |
| 19 | |
| 20 namespace ui { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 class CacaSurface : public ui::SurfaceOzoneCanvas { | |
| 25 public: | |
| 26 explicit CacaSurface(CacaWindow* window); | |
| 27 ~CacaSurface() override; | |
| 28 | |
| 29 bool Initialize(); | |
| 30 | |
| 31 // ui::SurfaceOzoneCanvas overrides: | |
| 32 sk_sp<SkSurface> GetSurface() override; | |
| 33 void ResizeCanvas(const gfx::Size& viewport_size) override; | |
| 34 void PresentCanvas(const gfx::Rect& damage) override; | |
| 35 std::unique_ptr<gfx::VSyncProvider> CreateVSyncProvider() override; | |
| 36 | |
| 37 private: | |
| 38 CacaWindow* window_; // Not owned. | |
| 39 | |
| 40 ScopedCacaDither dither_; | |
| 41 | |
| 42 sk_sp<SkSurface> surface_; | |
| 43 | |
| 44 DISALLOW_COPY_AND_ASSIGN(CacaSurface); | |
| 45 }; | |
| 46 | |
| 47 CacaSurface::CacaSurface(CacaWindow* window) : window_(window) { | |
| 48 } | |
| 49 | |
| 50 CacaSurface::~CacaSurface() { | |
| 51 } | |
| 52 | |
| 53 bool CacaSurface::Initialize() { | |
| 54 ResizeCanvas(window_->bitmap_size()); | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 sk_sp<SkSurface> CacaSurface::GetSurface() { | |
| 59 return surface_; | |
| 60 } | |
| 61 | |
| 62 void CacaSurface::ResizeCanvas(const gfx::Size& viewport_size) { | |
| 63 TRACE_EVENT0("ozone", "CacaSurface::ResizeCanvas"); | |
| 64 | |
| 65 VLOG(2) << "creating libcaca surface with from window " << (void*)window_; | |
| 66 | |
| 67 SkImageInfo info = SkImageInfo::Make(window_->bitmap_size().width(), | |
| 68 window_->bitmap_size().height(), | |
| 69 kN32_SkColorType, | |
| 70 kPremul_SkAlphaType); | |
| 71 | |
| 72 surface_ = SkSurface::MakeRaster(info); | |
| 73 if (!surface_) | |
| 74 LOG(ERROR) << "Failed to create SkSurface"; | |
| 75 | |
| 76 dither_.reset(caca_create_dither(info.bytesPerPixel() * 8, | |
| 77 info.width(), | |
| 78 info.height(), | |
| 79 info.minRowBytes(), | |
| 80 0x00ff0000, | |
| 81 0x0000ff00, | |
| 82 0x000000ff, | |
| 83 0xff000000)); | |
| 84 if (!dither_) | |
| 85 LOG(ERROR) << "failed to create dither"; | |
| 86 } | |
| 87 | |
| 88 void CacaSurface::PresentCanvas(const gfx::Rect& damage) { | |
| 89 TRACE_EVENT0("ozone", "CacaSurface::PresentCanvas"); | |
| 90 | |
| 91 SkPixmap pixmap; | |
| 92 surface_->peekPixels(&pixmap); | |
| 93 | |
| 94 caca_canvas_t* canvas = caca_get_canvas(window_->display()); | |
| 95 caca_dither_bitmap(canvas, 0, 0, caca_get_canvas_width(canvas), | |
| 96 caca_get_canvas_height(canvas), dither_.get(), | |
| 97 static_cast<const uint8_t*>(pixmap.addr())); | |
| 98 caca_refresh_display(window_->display()); | |
| 99 } | |
| 100 | |
| 101 std::unique_ptr<gfx::VSyncProvider> CacaSurface::CreateVSyncProvider() { | |
| 102 return nullptr; | |
| 103 } | |
| 104 | |
| 105 } // namespace | |
| 106 | |
| 107 CacaWindowManager::CacaWindowManager() { | |
| 108 } | |
| 109 | |
| 110 int32_t CacaWindowManager::AddWindow(CacaWindow* window) { | |
| 111 return windows_.Add(window); | |
| 112 } | |
| 113 | |
| 114 void CacaWindowManager::RemoveWindow(int window_id, CacaWindow* window) { | |
| 115 DCHECK_EQ(window, windows_.Lookup(window_id)); | |
| 116 windows_.Remove(window_id); | |
| 117 } | |
| 118 | |
| 119 CacaWindowManager::~CacaWindowManager() { | |
| 120 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 121 } | |
| 122 | |
| 123 std::unique_ptr<ui::SurfaceOzoneCanvas> | |
| 124 CacaWindowManager::CreateCanvasForWidget(gfx::AcceleratedWidget widget) { | |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 126 CacaWindow* window = windows_.Lookup(widget); | |
| 127 DCHECK(window); | |
| 128 | |
| 129 std::unique_ptr<CacaSurface> canvas(new CacaSurface(window)); | |
| 130 bool initialized = canvas->Initialize(); | |
| 131 DCHECK(initialized); | |
| 132 return std::move(canvas); | |
| 133 } | |
| 134 | |
| 135 } // namespace ui | |
| OLD | NEW |