michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set sw=2 ts=8 et ft=cpp : */ michael@0: /* Copyright 2012 Mozilla Foundation and Mozilla contributors michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: michael@0: #include "android/log.h" michael@0: michael@0: #include "Framebuffer.h" michael@0: #include "gfxContext.h" michael@0: #include "gfxImageSurface.h" michael@0: #include "gfxUtils.h" michael@0: #include "mozilla/FileUtils.h" michael@0: #include "nsTArray.h" michael@0: #include "nsRegion.h" michael@0: michael@0: #define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gonk" , ## args) michael@0: michael@0: using namespace std; michael@0: michael@0: namespace mozilla { michael@0: michael@0: namespace Framebuffer { michael@0: michael@0: static int sFd = -1; michael@0: static size_t sMappedSize; michael@0: static struct fb_var_screeninfo sVi; michael@0: static size_t sActiveBuffer; michael@0: typedef vector > BufferVector; michael@0: BufferVector* sBuffers; michael@0: static gfxIntSize *sScreenSize = nullptr; michael@0: michael@0: BufferVector& Buffers() { return *sBuffers; } michael@0: michael@0: bool michael@0: SetGraphicsMode() michael@0: { michael@0: ScopedClose fd(open("/dev/tty0", O_RDWR | O_SYNC)); michael@0: if (0 > fd.get()) { michael@0: // This is non-fatal; post-Cupcake kernels don't have tty0. michael@0: LOG("No /dev/tty0?"); michael@0: } else if (ioctl(fd.get(), KDSETMODE, (void*) KD_GRAPHICS)) { michael@0: LOG("Error setting graphics mode on /dev/tty0"); michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: Open() michael@0: { michael@0: if (0 <= sFd) michael@0: return true; michael@0: michael@0: if (!SetGraphicsMode()) michael@0: return false; michael@0: michael@0: ScopedClose fd(open("/dev/graphics/fb0", O_RDWR)); michael@0: if (0 > fd.get()) { michael@0: LOG("Error opening framebuffer device"); michael@0: return false; michael@0: } michael@0: michael@0: struct fb_fix_screeninfo fi; michael@0: if (0 > ioctl(fd.get(), FBIOGET_FSCREENINFO, &fi)) { michael@0: LOG("Error getting fixed screeninfo"); michael@0: return false; michael@0: } michael@0: michael@0: if (0 > ioctl(fd.get(), FBIOGET_VSCREENINFO, &sVi)) { michael@0: LOG("Error getting variable screeninfo"); michael@0: return false; michael@0: } michael@0: michael@0: sMappedSize = fi.smem_len; michael@0: void* mem = mmap(0, sMappedSize, PROT_READ | PROT_WRITE, MAP_SHARED, michael@0: fd.rwget(), 0); michael@0: if (MAP_FAILED == mem) { michael@0: LOG("Error mmap'ing framebuffer"); michael@0: return false; michael@0: } michael@0: michael@0: sFd = fd.get(); michael@0: fd.forget(); michael@0: michael@0: // The android porting doc requires a /dev/graphics/fb0 device michael@0: // that's double buffered with r5g6b5 format. Hence the michael@0: // hard-coded numbers here. michael@0: gfxImageFormat format = gfxImageFormat::RGB16_565; michael@0: if (!sScreenSize) { michael@0: sScreenSize = new gfxIntSize(sVi.xres, sVi.yres); michael@0: } michael@0: long stride = fi.line_length; michael@0: size_t numFrameBytes = stride * sScreenSize->height; michael@0: michael@0: sBuffers = new BufferVector(2); michael@0: unsigned char* data = static_cast(mem); michael@0: for (size_t i = 0; i < 2; ++i, data += numFrameBytes) { michael@0: memset(data, 0, numFrameBytes); michael@0: Buffers()[i] = new gfxImageSurface(data, *sScreenSize, stride, format); michael@0: } michael@0: michael@0: // Clear the framebuffer to a known state. michael@0: Present(nsIntRect()); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: GetSize(nsIntSize *aScreenSize) { michael@0: // If the framebuffer has been opened, we should always have the size. michael@0: if (sScreenSize) { michael@0: *aScreenSize = *sScreenSize; michael@0: return true; michael@0: } michael@0: michael@0: ScopedClose fd(open("/dev/graphics/fb0", O_RDWR)); michael@0: if (0 > fd.get()) { michael@0: LOG("Error opening framebuffer device"); michael@0: return false; michael@0: } michael@0: michael@0: if (0 > ioctl(fd.get(), FBIOGET_VSCREENINFO, &sVi)) { michael@0: LOG("Error getting variable screeninfo"); michael@0: return false; michael@0: } michael@0: michael@0: sScreenSize = new gfxIntSize(sVi.xres, sVi.yres); michael@0: *aScreenSize = *sScreenSize; michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: Close() michael@0: { michael@0: if (0 > sFd) michael@0: return; michael@0: michael@0: munmap(Buffers()[0]->Data(), sMappedSize); michael@0: delete sBuffers; michael@0: sBuffers = nullptr; michael@0: delete sScreenSize; michael@0: sScreenSize = nullptr; michael@0: michael@0: close(sFd); michael@0: sFd = -1; michael@0: } michael@0: michael@0: gfxASurface* michael@0: BackBuffer() michael@0: { michael@0: return Buffers()[!sActiveBuffer]; michael@0: } michael@0: michael@0: static gfxASurface* michael@0: FrontBuffer() michael@0: { michael@0: return Buffers()[sActiveBuffer]; michael@0: } michael@0: michael@0: void michael@0: Present(const nsIntRegion& aUpdated) michael@0: { michael@0: sActiveBuffer = !sActiveBuffer; michael@0: michael@0: sVi.yres_virtual = sVi.yres * 2; michael@0: sVi.yoffset = sActiveBuffer * sVi.yres; michael@0: sVi.bits_per_pixel = 16; michael@0: if (ioctl(sFd, FBIOPUT_VSCREENINFO, &sVi) < 0) { michael@0: LOG("Error presenting front buffer"); michael@0: } michael@0: michael@0: nsRefPtr ctx = new gfxContext(BackBuffer()); michael@0: gfxUtils::PathFromRegion(ctx, aUpdated); michael@0: ctx->Clip(); michael@0: ctx->SetSource(FrontBuffer()); michael@0: ctx->SetOperator(gfxContext::OPERATOR_SOURCE); michael@0: ctx->Paint(1.0); michael@0: } michael@0: michael@0: } // namespace Framebuffer michael@0: michael@0: } // namespace mozilla