Android第一次--by{流芒}-{土豆}
最近因为要了解一点手机开发的东东,于是接触了一下Android。没有用过相关gphone,看过相关的手机,没想到手机的发展超过我的想象。只怪我太落后于时代了。大学时代所谓的3G已经这么普及了。
Android是基于linux的一个手机操作系统,在linux操作系统的基础上提供了不少的核心的中间件。对于开发者而言,Android提供了一个基于google java library的java开发接口。呵呵,如果使用纯c来开发也不是不可。Android上的java虚拟机也照样可以按照本地码来的形式来加载这些纯c的代码。看来Android的一个核心是一个java的虚拟机Dalvik。
Dalvik–看看wiki上怎么解释的。Dalvik虚拟机的官网。
Dalvik is the virtual machine on Android mobile devices.
It runs applications which have been converted into a compact Dalvik Executable (.dex) format suitable for systems that are constrained in terms of memory andprocessor speed.
Dalvik was written by Dan Bornstein, who named it after the fishing village of Dalvík in Eyjafjörður, Iceland, where some of his ancestors lived.
Dalvik不同于平时常用的机遇stack的java虚拟机,是一个基于register的虚拟机。(呵呵,有兴趣的话,可以搜搜基于stack的虚拟机和基于register的虚拟机的区别)
Android上使用的java库不是传统的j2se或者j2me上的awt或者swing,而是采用了开源的Apache Harmony 上的一个子集。
Apache Harmony官网

apache harmony架构图
关于harmony的更多参考ibm的developer works有更多详细的说明:http://www.ibm.com/developerworks/cn/java/j-harmony/
Android开发使用的模拟器是 QEMU
QEMU是一套由Fabrice Bellard]所編寫的模擬處理器的自由軟體。它與Bochs,PearPC近似,但其具有某些後兩者所不具備的特性,如高速度及跨平台的特性。經由kqemu這個开源的加速器,QEMU能模擬至接近真實電腦的速度。QEMU有兩種主要運作模式:
- System mode模擬模式,亦即是系統模式。QEMU能模擬整個電腦系統,包括中央處理器及其他週邊設備。它使得為系統源代碼進行測試及除錯工作變得容易。其亦能用來在一部主機上虛擬數部不同虛擬電腦。
使用kqemu可使QEMU能模擬至接近實機速度,但其在虛擬的操作系統是Microsoft Windows 98或以下的情況下是無用的。
Android使用的图形引擎Skia Graphics Engine 官网
Skia 图形渲染引擎最初由 Skia 公司开发,该公司于 2005 年被 Google 收购。
Skia 与 Openwave’s (现在叫 Purple Labs)V7 vector graphics engine 非常 类似,它们都来自于 Mike Reed 的公司。
如果你正在做移动设备上的矢量图形或者图像渲染相关的工作,你应该 checkout skia 的源代码仔细研究下。
Skia is quite elegant in its simplicity and power, a great combination.
“By adding Skia engine to Chrome, Google can ensure good graphics performance on devices that don’t have a graphics processing unit.”
skia 有大概 80,000 行代码,基于 C++ 开发,主要特点包括:
- Optimised software-based rasteriser (module sgl/);
- Optional GL-based acceleration of certain graphics operations including shader support and textures (module gl/);
- Animation capabilities (module animator/);
- Some built-in SVG support (module (svg/)
- Built-in image decoders: PNG, JPEG, GIF, BMP, WBMP, ICO (modules images/);
- Text capabilities (no built-in support for complex scripts);
- Some awareness of higher-level UI toolkit constructs (platform windows, platform events): Mac, Unix (sic. X11, incomplete), Windows, wxwidgets
- Performace features
- Copy-on-write for images and certain other data types;
- Extensive use of the stack, both internally and for API consumers to avoid needless allocations and memory fragmentation;
- Thread-safety to enable parallelisation.
The library is portable and has (optional) platform-specific backends:
- Fonts: Android / Ascender, FreeType, Windows (GDI);
- Threading: pthread, Windows;
- XML: expat, tinyxml;
- Android shared memory (ashmem) for inter-process image data references;
Google Chrome 选择 Skia 的理由6
为什么不用 OpenGL 或者 DirectX 来加速渲染?
- 数据从 video card 读出,然后在另一个进程中再拷贝回 video card,这种 情况下不值得用硬件加速渲染;
- 相对而言,图形绘制只占很少时间,大部分时间是计算页面元素的位置、风 格、输出文本,即使用了 3D 加速也不会有明显改善;
为什么不用别的图形库?
- Windows GDI:Microsoft Windows 的底层图形 API,相对而言只具备基 本的绘制功能,像 <canvas>, SVG 需要单独实现。
- GDI+:Windows上更高级的 API,GDI+ 使用的是设备独立的 metrics,这会 使 Chrome中的 text and letter spacing 看以来与别的 Windows 应用不同 (which measure and draw text tailored to the screen device)。而且, 微软当时也推荐开发人员使用新的图形 API,GDI+ 的技术支持和维护可能有 限。
- Cairo:一个开源 2D 图形库,已经在 firefox 3 中使用。
最终选择 Skia 是因为:
- skia is cross-platform;
- there was already a high-quality WebKit port using it created for Android‘s browser;
- we had in-house expertise.
http://blog.chromium.org/2008/10/graphics-in-google-chrome.html
源码下载
http://src.chromium.org/viewvc/chrome/trunk/src/skia/
Hello World4
In this simple example we draw a few rectangles to a memory-based image buffer. This also demonstrates how one might integrate with the platform graphics system to get something on screen, though in this case we’re using Cairo to save the resulting image to disk:
#include "SkBitmap.h"
#include "SkDevice.h"
#include "SkPaint.h"
#include "SkRect.h"
#include <cairo.h>
int main()
{
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
bitmap.allocPixels();
SkDevice device(bitmap);
SkCanvas canvas(&device);
SkPaint paint;
SkRect r;
paint.setARGB(255, 255, 255, 255);
r.set(10, 10, 20, 20);
canvas.drawRect(r, paint);
paint.setARGB(255, 255, 0, 0);
r.offset(5, 5);
canvas.drawRect(r, paint);
paint.setARGB(255, 0, 0, 255);
r.offset(5, 5);
canvas.drawRect(r, paint);
{
SkAutoLockPixels image_lock(bitmap);
cairo_surface_t* surface = cairo_image_surface_create_for_data(
(unsigned char*)bitmap.getPixels(), CAIRO_FORMAT_ARGB32,
bitmap.width(), bitmap.height(), bitmap.rowBytes());
cairo_surface_write_to_png(surface, "snapshot.png");
cairo_surface_destroy(surface);
}
return 0;
}
You can build this example for yourself linking statically to the libskia.a object file generated during the Chrome build process on Linux.
Standalone skia2
网上有人尝试了 stand-alone 版的 skia,具体内容参考:
http://vuhung16.blogspot.com/2008/10/standalone-skia.html
Open Skia3
Google Code 上发现一个 openskia 项目,从作者看跟上面的 standalone skia 是一个人。
http://code.google.com/p/openskia/
openskia is a graphics engine based Skia Graphics Engine that was developed by Skia inc., a company Google acquired in 2005.
This vector graphics engine software makes highend visual effects possible on feature phone. It is tiny in size and is capable of delivering very high quality. Skia’s engine is the graphics core of both Google Android and Google Chrome.
Reference
- Skia Source Code Released
- Standalone skia
- openskia
- Skia graphics library in Chrome: First impressions
- Chrome’s secret: The Skia engine
- Graphics in Google Chrome
目前我了解的大概就是这些了。后续会在整理一些Android方面相关的东东。开始一步一步的学习一下。
Tags: Android, Apache Harmony, Dalvik, Dalvik Executable, Dan Bornstein, linux