Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set sw=2 ts=8 et ft=cpp : */ |
michael@0 | 3 | /* Copyright 2012 Mozilla Foundation and Mozilla contributors |
michael@0 | 4 | * |
michael@0 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 6 | * you may not use this file except in compliance with the License. |
michael@0 | 7 | * You may obtain a copy of the License at |
michael@0 | 8 | * |
michael@0 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 10 | * |
michael@0 | 11 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 14 | * See the License for the specific language governing permissions and |
michael@0 | 15 | * limitations under the License. |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | #include <fcntl.h> |
michael@0 | 19 | #include <linux/fb.h> |
michael@0 | 20 | #include <linux/kd.h> |
michael@0 | 21 | #include <stdio.h> |
michael@0 | 22 | #include <stdlib.h> |
michael@0 | 23 | #include <sys/ioctl.h> |
michael@0 | 24 | #include <sys/mman.h> |
michael@0 | 25 | #include <sys/types.h> |
michael@0 | 26 | #include <unistd.h> |
michael@0 | 27 | |
michael@0 | 28 | #include <vector> |
michael@0 | 29 | |
michael@0 | 30 | #include "android/log.h" |
michael@0 | 31 | |
michael@0 | 32 | #include "Framebuffer.h" |
michael@0 | 33 | #include "gfxContext.h" |
michael@0 | 34 | #include "gfxImageSurface.h" |
michael@0 | 35 | #include "gfxUtils.h" |
michael@0 | 36 | #include "mozilla/FileUtils.h" |
michael@0 | 37 | #include "nsTArray.h" |
michael@0 | 38 | #include "nsRegion.h" |
michael@0 | 39 | |
michael@0 | 40 | #define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gonk" , ## args) |
michael@0 | 41 | |
michael@0 | 42 | using namespace std; |
michael@0 | 43 | |
michael@0 | 44 | namespace mozilla { |
michael@0 | 45 | |
michael@0 | 46 | namespace Framebuffer { |
michael@0 | 47 | |
michael@0 | 48 | static int sFd = -1; |
michael@0 | 49 | static size_t sMappedSize; |
michael@0 | 50 | static struct fb_var_screeninfo sVi; |
michael@0 | 51 | static size_t sActiveBuffer; |
michael@0 | 52 | typedef vector<nsRefPtr<gfxImageSurface> > BufferVector; |
michael@0 | 53 | BufferVector* sBuffers; |
michael@0 | 54 | static gfxIntSize *sScreenSize = nullptr; |
michael@0 | 55 | |
michael@0 | 56 | BufferVector& Buffers() { return *sBuffers; } |
michael@0 | 57 | |
michael@0 | 58 | bool |
michael@0 | 59 | SetGraphicsMode() |
michael@0 | 60 | { |
michael@0 | 61 | ScopedClose fd(open("/dev/tty0", O_RDWR | O_SYNC)); |
michael@0 | 62 | if (0 > fd.get()) { |
michael@0 | 63 | // This is non-fatal; post-Cupcake kernels don't have tty0. |
michael@0 | 64 | LOG("No /dev/tty0?"); |
michael@0 | 65 | } else if (ioctl(fd.get(), KDSETMODE, (void*) KD_GRAPHICS)) { |
michael@0 | 66 | LOG("Error setting graphics mode on /dev/tty0"); |
michael@0 | 67 | return false; |
michael@0 | 68 | } |
michael@0 | 69 | return true; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | bool |
michael@0 | 73 | Open() |
michael@0 | 74 | { |
michael@0 | 75 | if (0 <= sFd) |
michael@0 | 76 | return true; |
michael@0 | 77 | |
michael@0 | 78 | if (!SetGraphicsMode()) |
michael@0 | 79 | return false; |
michael@0 | 80 | |
michael@0 | 81 | ScopedClose fd(open("/dev/graphics/fb0", O_RDWR)); |
michael@0 | 82 | if (0 > fd.get()) { |
michael@0 | 83 | LOG("Error opening framebuffer device"); |
michael@0 | 84 | return false; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | struct fb_fix_screeninfo fi; |
michael@0 | 88 | if (0 > ioctl(fd.get(), FBIOGET_FSCREENINFO, &fi)) { |
michael@0 | 89 | LOG("Error getting fixed screeninfo"); |
michael@0 | 90 | return false; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | if (0 > ioctl(fd.get(), FBIOGET_VSCREENINFO, &sVi)) { |
michael@0 | 94 | LOG("Error getting variable screeninfo"); |
michael@0 | 95 | return false; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | sMappedSize = fi.smem_len; |
michael@0 | 99 | void* mem = mmap(0, sMappedSize, PROT_READ | PROT_WRITE, MAP_SHARED, |
michael@0 | 100 | fd.rwget(), 0); |
michael@0 | 101 | if (MAP_FAILED == mem) { |
michael@0 | 102 | LOG("Error mmap'ing framebuffer"); |
michael@0 | 103 | return false; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | sFd = fd.get(); |
michael@0 | 107 | fd.forget(); |
michael@0 | 108 | |
michael@0 | 109 | // The android porting doc requires a /dev/graphics/fb0 device |
michael@0 | 110 | // that's double buffered with r5g6b5 format. Hence the |
michael@0 | 111 | // hard-coded numbers here. |
michael@0 | 112 | gfxImageFormat format = gfxImageFormat::RGB16_565; |
michael@0 | 113 | if (!sScreenSize) { |
michael@0 | 114 | sScreenSize = new gfxIntSize(sVi.xres, sVi.yres); |
michael@0 | 115 | } |
michael@0 | 116 | long stride = fi.line_length; |
michael@0 | 117 | size_t numFrameBytes = stride * sScreenSize->height; |
michael@0 | 118 | |
michael@0 | 119 | sBuffers = new BufferVector(2); |
michael@0 | 120 | unsigned char* data = static_cast<unsigned char*>(mem); |
michael@0 | 121 | for (size_t i = 0; i < 2; ++i, data += numFrameBytes) { |
michael@0 | 122 | memset(data, 0, numFrameBytes); |
michael@0 | 123 | Buffers()[i] = new gfxImageSurface(data, *sScreenSize, stride, format); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | // Clear the framebuffer to a known state. |
michael@0 | 127 | Present(nsIntRect()); |
michael@0 | 128 | |
michael@0 | 129 | return true; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | bool |
michael@0 | 133 | GetSize(nsIntSize *aScreenSize) { |
michael@0 | 134 | // If the framebuffer has been opened, we should always have the size. |
michael@0 | 135 | if (sScreenSize) { |
michael@0 | 136 | *aScreenSize = *sScreenSize; |
michael@0 | 137 | return true; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | ScopedClose fd(open("/dev/graphics/fb0", O_RDWR)); |
michael@0 | 141 | if (0 > fd.get()) { |
michael@0 | 142 | LOG("Error opening framebuffer device"); |
michael@0 | 143 | return false; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | if (0 > ioctl(fd.get(), FBIOGET_VSCREENINFO, &sVi)) { |
michael@0 | 147 | LOG("Error getting variable screeninfo"); |
michael@0 | 148 | return false; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | sScreenSize = new gfxIntSize(sVi.xres, sVi.yres); |
michael@0 | 152 | *aScreenSize = *sScreenSize; |
michael@0 | 153 | return true; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | void |
michael@0 | 157 | Close() |
michael@0 | 158 | { |
michael@0 | 159 | if (0 > sFd) |
michael@0 | 160 | return; |
michael@0 | 161 | |
michael@0 | 162 | munmap(Buffers()[0]->Data(), sMappedSize); |
michael@0 | 163 | delete sBuffers; |
michael@0 | 164 | sBuffers = nullptr; |
michael@0 | 165 | delete sScreenSize; |
michael@0 | 166 | sScreenSize = nullptr; |
michael@0 | 167 | |
michael@0 | 168 | close(sFd); |
michael@0 | 169 | sFd = -1; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | gfxASurface* |
michael@0 | 173 | BackBuffer() |
michael@0 | 174 | { |
michael@0 | 175 | return Buffers()[!sActiveBuffer]; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | static gfxASurface* |
michael@0 | 179 | FrontBuffer() |
michael@0 | 180 | { |
michael@0 | 181 | return Buffers()[sActiveBuffer]; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | void |
michael@0 | 185 | Present(const nsIntRegion& aUpdated) |
michael@0 | 186 | { |
michael@0 | 187 | sActiveBuffer = !sActiveBuffer; |
michael@0 | 188 | |
michael@0 | 189 | sVi.yres_virtual = sVi.yres * 2; |
michael@0 | 190 | sVi.yoffset = sActiveBuffer * sVi.yres; |
michael@0 | 191 | sVi.bits_per_pixel = 16; |
michael@0 | 192 | if (ioctl(sFd, FBIOPUT_VSCREENINFO, &sVi) < 0) { |
michael@0 | 193 | LOG("Error presenting front buffer"); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | nsRefPtr<gfxContext> ctx = new gfxContext(BackBuffer()); |
michael@0 | 197 | gfxUtils::PathFromRegion(ctx, aUpdated); |
michael@0 | 198 | ctx->Clip(); |
michael@0 | 199 | ctx->SetSource(FrontBuffer()); |
michael@0 | 200 | ctx->SetOperator(gfxContext::OPERATOR_SOURCE); |
michael@0 | 201 | ctx->Paint(1.0); |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | } // namespace Framebuffer |
michael@0 | 205 | |
michael@0 | 206 | } // namespace mozilla |