Merge "New update of preloaded classes for Froyo."
diff --git a/include/binder/Parcel.h b/include/binder/Parcel.h
index ba6c711..66c34b2 100644
--- a/include/binder/Parcel.h
+++ b/include/binder/Parcel.h
@@ -30,6 +30,7 @@
 class ProcessState;
 class String8;
 class TextOutput;
+class Flattenable;
 
 struct flat_binder_object;  // defined in support_p/binder_module.h
 
@@ -81,6 +82,7 @@
     status_t            writeString16(const char16_t* str, size_t len);
     status_t            writeStrongBinder(const sp<IBinder>& val);
     status_t            writeWeakBinder(const wp<IBinder>& val);
+    status_t            write(const Flattenable& val);
 
     // Place a native_handle into the parcel (the native_handle's file-
     // descriptors are dup'ed, so it is safe to delete the native_handle
@@ -119,7 +121,7 @@
     const char16_t*     readString16Inplace(size_t* outLen) const;
     sp<IBinder>         readStrongBinder() const;
     wp<IBinder>         readWeakBinder() const;
-
+    status_t            read(Flattenable& val) const;
     
     // Retrieve native_handle from the parcel. This returns a copy of the
     // parcel's native_handle (the caller takes ownership). The caller
diff --git a/include/ui/GraphicBuffer.h b/include/ui/GraphicBuffer.h
index b9c491b..e72b6b3 100644
--- a/include/ui/GraphicBuffer.h
+++ b/include/ui/GraphicBuffer.h
@@ -23,6 +23,7 @@
 #include <ui/android_native_buffer.h>
 #include <ui/PixelFormat.h>
 #include <ui/Rect.h>
+#include <utils/Flattenable.h>
 #include <pixelflinger/pixelflinger.h>
 
 struct android_native_buffer_t;
@@ -30,7 +31,6 @@
 namespace android {
 
 class GraphicBufferMapper;
-class Parcel;
 
 // ===========================================================================
 // GraphicBuffer
@@ -40,7 +40,7 @@
     : public EGLNativeBase<
         android_native_buffer_t, 
         GraphicBuffer, 
-        LightRefBase<GraphicBuffer> >
+        LightRefBase<GraphicBuffer> >, public Flattenable
 {
 public:
 
@@ -97,7 +97,6 @@
     uint32_t getVerticalStride() const;
 
 protected:
-    GraphicBuffer(const Parcel& reply);
     virtual ~GraphicBuffer();
 
     enum {
@@ -122,8 +121,16 @@
     status_t initSize(uint32_t w, uint32_t h, PixelFormat format, 
             uint32_t usage);
 
-    static status_t writeToParcel(Parcel* reply, 
-            android_native_buffer_t const* buffer);
+    void free_handle();
+
+    // Flattenable interface
+    size_t getFlattenedSize() const;
+    size_t getFdCount() const;
+    status_t flatten(void* buffer, size_t size,
+            int fds[], size_t count) const;
+    status_t unflatten(void const* buffer, size_t size,
+            int fds[], size_t count);
+
 
     GraphicBufferMapper& mBufferMapper;
     ssize_t mInitCheck;
diff --git a/include/utils/Flattenable.h b/include/utils/Flattenable.h
new file mode 100644
index 0000000..852be3b
--- /dev/null
+++ b/include/utils/Flattenable.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_UTILS_FLATTENABLE_H
+#define ANDROID_UTILS_FLATTENABLE_H
+
+
+#include <stdint.h>
+#include <sys/types.h>
+#include <utils/Errors.h>
+
+namespace android {
+
+class Flattenable
+{
+public:
+    // size in bytes of the flattened object
+    virtual size_t getFlattenedSize() const = 0;
+
+    // number of file descriptors to flatten
+    virtual size_t getFdCount() const = 0;
+
+    // flattens the object into buffer.
+    // size should be at least of getFlattenedSize()
+    // file descriptors are written in the fds[] array but ownership is
+    // not transfered (ie: they must be dupped by the caller of
+    // flatten() if needed).
+    virtual status_t flatten(void* buffer, size_t size,
+            int fds[], size_t count) const = 0;
+
+    // unflattens the object from buffer.
+    // size should be equal to the value of getFlattenedSize() when the
+    // object was flattened.
+    // unflattened file descriptors are found in the fds[] array and
+    // don't need to be dupped(). ie: the caller of unflatten doesn't
+    // keep ownership. If a fd is not retained by unflatten() it must be
+    // explicitly closed.
+    virtual status_t unflatten(void const* buffer, size_t size,
+            int fds[], size_t count) = 0;
+
+protected:
+    virtual ~Flattenable() = 0;
+
+};
+
+}; // namespace android
+
+
+#endif /* ANDROID_UTILS_FLATTENABLE_H */
diff --git a/include/utils/ResourceTypes.h b/include/utils/ResourceTypes.h
index 13ea27e..cd657e8 100644
--- a/include/utils/ResourceTypes.h
+++ b/include/utils/ResourceTypes.h
@@ -447,6 +447,8 @@
     }
     const char16_t* stringAt(size_t idx, size_t* outLen) const;
 
+    const char* string8At(size_t idx, size_t* outLen) const;
+
     const ResStringPool_span* styleAt(const ResStringPool_ref& ref) const;
     const ResStringPool_span* styleAt(size_t idx) const;
 
diff --git a/libs/audioflinger/AudioPolicyManagerBase.cpp b/libs/audioflinger/AudioPolicyManagerBase.cpp
index 096aa73..42b6508 100644
--- a/libs/audioflinger/AudioPolicyManagerBase.cpp
+++ b/libs/audioflinger/AudioPolicyManagerBase.cpp
@@ -295,13 +295,31 @@
     if (oldState == AudioSystem::MODE_IN_CALL && newDevice == 0) {
         newDevice = hwOutputDesc->device();
     }
+
+    // when changing from ring tone to in call mode, mute the ringing tone
+    // immediately and delay the route change to avoid sending the ring tone
+    // tail into the earpiece or headset.
+    int delayMs = 0;
+    if (state == AudioSystem::MODE_IN_CALL && oldState == AudioSystem::MODE_RINGTONE) {
+        // delay the device change command by twice the output latency to have some margin
+        // and be sure that audio buffers not yet affected by the mute are out when
+        // we actually apply the route change
+        delayMs = hwOutputDesc->mLatency*2;
+        setStreamMute(AudioSystem::RING, true, mHardwareOutput);
+    }
+
     // change routing is necessary
-    setOutputDevice(mHardwareOutput, newDevice, force);
+    setOutputDevice(mHardwareOutput, newDevice, force, delayMs);
 
     // if entering in call state, handle special case of active streams
     // pertaining to sonification strategy see handleIncallSonification()
     if (state == AudioSystem::MODE_IN_CALL) {
         LOGV("setPhoneState() in call state management: new state is %d", state);
+        // unmute the ringing tone after a sufficient delay if it was muted before
+        // setting output device above
+        if (oldState == AudioSystem::MODE_RINGTONE) {
+            setStreamMute(AudioSystem::RING, false, mHardwareOutput, MUTE_TIME_MS);
+        }
         for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
             handleIncallSonification(stream, true, true);
         }
@@ -1207,10 +1225,10 @@
         return INVALID_OPERATION;
     }
 
-    // mute media during 2 seconds to avoid outputing sound on hardware output while music stream
+    // mute media strategy to avoid outputting sound on hardware output while music stream
     // is switched from A2DP output and before music is paused by music application
     setStrategyMute(STRATEGY_MEDIA, true, mHardwareOutput);
-    setStrategyMute(STRATEGY_MEDIA, false, mHardwareOutput, 2000);
+    setStrategyMute(STRATEGY_MEDIA, false, mHardwareOutput, MUTE_TIME_MS);
 
     if (!a2dpUsedForSonification()) {
         // unmute music on A2DP output if a notification or ringtone is playing
diff --git a/libs/binder/Parcel.cpp b/libs/binder/Parcel.cpp
index e397bce..00d2210 100644
--- a/libs/binder/Parcel.cpp
+++ b/libs/binder/Parcel.cpp
@@ -28,6 +28,7 @@
 #include <utils/String16.h>
 #include <utils/TextOutput.h>
 #include <utils/misc.h>
+#include <utils/Flattenable.h>
 
 #include <private/binder/binder_module.h>
 
@@ -675,6 +676,42 @@
     return writeObject(obj, true);
 }
 
+status_t Parcel::write(const Flattenable& val)
+{
+    status_t err;
+
+    // size if needed
+    size_t len = val.getFlattenedSize();
+    size_t fd_count = val.getFdCount();
+
+    err = this->writeInt32(len);
+    if (err) return err;
+
+    err = this->writeInt32(fd_count);
+    if (err) return err;
+
+    // payload
+    void* buf = this->writeInplace(PAD_SIZE(len));
+    if (buf == NULL)
+        return BAD_VALUE;
+
+    int* fds = NULL;
+    if (fd_count) {
+        fds = new int[fd_count];
+    }
+
+    err = val.flatten(buf, len, fds, fd_count);
+    for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
+        err = this->writeDupFileDescriptor( fds[i] );
+    }
+
+    if (fd_count) {
+        delete [] fds;
+    }
+
+    return err;
+}
+
 status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
 {
     const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
@@ -713,7 +750,6 @@
     goto restart_write;
 }
 
-
 void Parcel::remove(size_t start, size_t amt)
 {
     LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
@@ -940,6 +976,38 @@
     return BAD_TYPE;
 }
 
+status_t Parcel::read(Flattenable& val) const
+{
+    // size
+    const size_t len = this->readInt32();
+    const size_t fd_count = this->readInt32();
+
+    // payload
+    void const* buf = this->readInplace(PAD_SIZE(len));
+    if (buf == NULL)
+        return BAD_VALUE;
+
+    int* fds = NULL;
+    if (fd_count) {
+        fds = new int[fd_count];
+    }
+
+    status_t err = NO_ERROR;
+    for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
+        fds[i] = dup(this->readFileDescriptor());
+        if (fds[i] < 0) err = BAD_VALUE;
+    }
+
+    if (err == NO_ERROR) {
+        err = val.unflatten(buf, len, fds, fd_count);
+    }
+
+    if (fd_count) {
+        delete [] fds;
+    }
+
+    return err;
+}
 const flat_binder_object* Parcel::readObject(bool nullMetaData) const
 {
     const size_t DPOS = mDataPos;
diff --git a/libs/surfaceflinger/Android.mk b/libs/surfaceflinger/Android.mk
index 395a937..86eb78d 100644
--- a/libs/surfaceflinger/Android.mk
+++ b/libs/surfaceflinger/Android.mk
@@ -35,7 +35,6 @@
 	libpixelflinger \
 	libhardware \
 	libutils \
-	libskia \
 	libEGL \
 	libGLESv1_CM \
 	libbinder \
diff --git a/libs/surfaceflinger/LayerBase.h b/libs/surfaceflinger/LayerBase.h
index d1bbd04..6aacd82 100644
--- a/libs/surfaceflinger/LayerBase.h
+++ b/libs/surfaceflinger/LayerBase.h
@@ -22,6 +22,7 @@
 
 #include <EGL/egl.h>
 #include <EGL/eglext.h>
+#include <GLES/gl.h>
 
 #include <utils/RefBase.h>
 
diff --git a/libs/surfaceflinger/SurfaceFlinger.cpp b/libs/surfaceflinger/SurfaceFlinger.cpp
index 2d6152e..b408779 100644
--- a/libs/surfaceflinger/SurfaceFlinger.cpp
+++ b/libs/surfaceflinger/SurfaceFlinger.cpp
@@ -1496,8 +1496,8 @@
                     layer->needsBlending(), layer->needsDithering(),
                     layer->contentDirty,
                     s.alpha, s.flags,
-                    s.transform[0], s.transform[1],
-                    s.transform[2], s.transform[3]);
+                    s.transform[0][0], s.transform[0][1],
+                    s.transform[1][0], s.transform[1][1]);
             result.append(buffer);
             buffer[0] = 0;
             /*** LayerBaseClient ***/
@@ -1833,27 +1833,25 @@
 
 status_t GraphicPlane::orientationToTransfrom(
         int orientation, int w, int h, Transform* tr)
-{    
-    float a, b, c, d, x, y;
+{
+    uint32_t flags = 0;
     switch (orientation) {
     case ISurfaceComposer::eOrientationDefault:
-        // make sure the default orientation is optimal
-        tr->reset();
-        return NO_ERROR;
+        flags = Transform::ROT_0;
+        break;
     case ISurfaceComposer::eOrientation90:
-        a=0; b=-1; c=1; d=0; x=w; y=0;
+        flags = Transform::ROT_90;
         break;
     case ISurfaceComposer::eOrientation180:
-        a=-1; b=0; c=0; d=-1; x=w; y=h;
+        flags = Transform::ROT_180;
         break;
     case ISurfaceComposer::eOrientation270:
-        a=0; b=1; c=-1; d=0; x=0; y=h;
+        flags = Transform::ROT_270;
         break;
     default:
         return BAD_VALUE;
     }
-    tr->set(a, b, c, d);
-    tr->set(x, y);
+    tr->set(flags, w, h);
     return NO_ERROR;
 }
 
@@ -1869,24 +1867,13 @@
     mHeight = int(h);
 
     Transform orientationTransform;
-    if (UNLIKELY(orientation == 42)) {
-        float a, b, c, d, x, y;
-        const float r = (3.14159265f / 180.0f) * 42.0f;
-        const float si = sinf(r);
-        const float co = cosf(r);
-        a=co; b=-si; c=si; d=co;
-        x = si*(h*0.5f) + (1-co)*(w*0.5f);
-        y =-si*(w*0.5f) + (1-co)*(h*0.5f);
-        orientationTransform.set(a, b, c, d);
-        orientationTransform.set(x, y);
-    } else {
-        GraphicPlane::orientationToTransfrom(orientation, w, h,
-                &orientationTransform);
-        if (orientation & ISurfaceComposer::eOrientationSwapMask) {
-            mWidth = int(h);
-            mHeight = int(w);
-        }
+    GraphicPlane::orientationToTransfrom(orientation, w, h,
+            &orientationTransform);
+    if (orientation & ISurfaceComposer::eOrientationSwapMask) {
+        mWidth = int(h);
+        mHeight = int(w);
     }
+
     mOrientation = orientation;
     mGlobalTransform = mDisplayTransform * orientationTransform;
     return NO_ERROR;
diff --git a/libs/surfaceflinger/Transform.cpp b/libs/surfaceflinger/Transform.cpp
index ab6f7ba..b2d5856 100644
--- a/libs/surfaceflinger/Transform.cpp
+++ b/libs/surfaceflinger/Transform.cpp
@@ -14,180 +14,257 @@
  * limitations under the License.
  */
 
+#include <math.h>
+
+#include <cutils/compiler.h>
+#include <utils/String8.h>
 #include <ui/Region.h>
 
-#include <private/pixelflinger/ggl_fixed.h>
-
 #include "Transform.h"
 
 // ---------------------------------------------------------------------------
 
-#define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
-#define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))
-
-// ---------------------------------------------------------------------------
-
 namespace android {
 
 // ---------------------------------------------------------------------------
 
-Transform::Transform()
-    : mType(0)
-{
-    mTransform.reset();
+template <typename T> inline T min(T a, T b) {
+    return a<b ? a : b;
+}
+template <typename T> inline T min(T a, T b, T c) {
+    return min(a, min(b, c));
+}
+template <typename T> inline T min(T a, T b, T c, T d) {
+    return min(a, b, min(c, d));
+}
+
+template <typename T> inline T max(T a, T b) {
+    return a>b ? a : b;
+}
+template <typename T> inline T max(T a, T b, T c) {
+    return max(a, max(b, c));
+}
+template <typename T> inline T max(T a, T b, T c, T d) {
+    return max(a, b, max(c, d));
+}
+
+// ---------------------------------------------------------------------------
+
+Transform::Transform() {
+    reset();
 }
 
 Transform::Transform(const Transform&  other)
-    : mTransform(other.mTransform), mType(other.mType)
-{
+    : mMatrix(other.mMatrix), mType(other.mType) {
 }
 
-Transform::Transform(int32_t flags) {
-    mTransform.reset();
-    int sx = (flags & FLIP_H) ? -1 : 1;
-    int sy = (flags & FLIP_V) ? -1 : 1;
-    if (flags & ROT_90) {
-        this->set(0, -sy, sx, 0);
-    } else {
-        this->set(sx, 0, 0, sy);
-    }
+Transform::Transform(uint32_t orientation) {
+    set(orientation, 0, 0);
 }
 
 Transform::~Transform() {
 }
 
+
+bool Transform::absIsOne(float f) {
+    return fabs(f) == 1.0f;
+}
+
+bool Transform::isZero(float f) {
+    return fabs(f) == 0.0f;
+}
+
+bool Transform::absEqual(float a, float b) {
+    return fabs(a) == fabs(b);
+}
+
 Transform Transform::operator * (const Transform& rhs) const
 {
-    if (LIKELY(mType == 0))
+    if (CC_LIKELY(mType == IDENTITY))
         return rhs;
 
     Transform r(*this);
-    r.mTransform.preConcat(rhs.mTransform);
+    if (rhs.mType == IDENTITY)
+        return r;
+
+    // TODO: we could use mType to optimize the matrix multiply
+    const mat33& A(mMatrix);
+    const mat33& B(rhs.mMatrix);
+          mat33& D(r.mMatrix);
+    for (int i=0 ; i<3 ; i++) {
+        const float v0 = A[0][i];
+        const float v1 = A[1][i];
+        const float v2 = A[2][i];
+        D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
+        D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
+        D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
+    }
     r.mType |= rhs.mType;
+
+    // TODO: we could recompute this value from r and rhs
+    r.mType &= 0xFF;
+    r.mType |= UNKNOWN_TYPE;
     return r;
 }
 
-float Transform::operator [] (int i) const
-{
-    float r = 0;
-    switch(i) {
-        case 0: r = SkScalarToFloat( mTransform[SkMatrix::kMScaleX] );  break;
-        case 1: r = SkScalarToFloat( mTransform[SkMatrix::kMSkewX] );   break;
-        case 2: r = SkScalarToFloat( mTransform[SkMatrix::kMSkewY] );   break;
-        case 3: r = SkScalarToFloat( mTransform[SkMatrix::kMScaleY] );  break;
-    }
-    return r;
-}
-
-uint8_t Transform::type() const
-{
-    if (UNLIKELY(mType & 0x80000000)) {
-        mType = mTransform.getType();
-    }
-    return uint8_t(mType & 0xFF);
+float const* Transform::operator [] (int i) const {
+    return mMatrix[i].v;
 }
 
 bool Transform::transformed() const {
-    return type() > SkMatrix::kTranslate_Mask;
+    return type() > TRANSLATE;
 }
 
 int Transform::tx() const {
-    return SkScalarRound( mTransform[SkMatrix::kMTransX] );
+    return floorf(mMatrix[2][0] + 0.5f);
 }
 
 int Transform::ty() const {
-    return SkScalarRound( mTransform[SkMatrix::kMTransY] );
+    return floorf(mMatrix[2][1] + 0.5f);
 }
 
 void Transform::reset() {
-    mTransform.reset();
-    mType = 0;
-}
-
-void Transform::set( float xx, float xy,
-                     float yx, float yy)
-{
-    mTransform.set(SkMatrix::kMScaleX, SkFloatToScalar(xx));
-    mTransform.set(SkMatrix::kMSkewX, SkFloatToScalar(xy));
-    mTransform.set(SkMatrix::kMSkewY, SkFloatToScalar(yx));
-    mTransform.set(SkMatrix::kMScaleY, SkFloatToScalar(yy));
-    mType |= 0x80000000;
-}
-
-void Transform::set(float radian, float x, float y)
-{
-    float r00 = cosf(radian);    float r01 = -sinf(radian);
-    float r10 = sinf(radian);    float r11 =  cosf(radian);
-    mTransform.set(SkMatrix::kMScaleX, SkFloatToScalar(r00));
-    mTransform.set(SkMatrix::kMSkewX, SkFloatToScalar(r01));
-    mTransform.set(SkMatrix::kMSkewY, SkFloatToScalar(r10));
-    mTransform.set(SkMatrix::kMScaleY, SkFloatToScalar(r11));
-    mTransform.set(SkMatrix::kMTransX, SkIntToScalar(x - r00*x - r01*y));
-    mTransform.set(SkMatrix::kMTransY, SkIntToScalar(y - r10*x - r11*y));
-    mType |= 0x80000000 | SkMatrix::kTranslate_Mask;
-}
-
-void Transform::scale(float s, float x, float y)
-{
-    mTransform.postScale(s, s, x, y); 
-    mType |= 0x80000000;
-}
-
-void Transform::set(int tx, int ty)
-{
-    if (tx | ty) {
-        mTransform.set(SkMatrix::kMTransX, SkIntToScalar(tx));
-        mTransform.set(SkMatrix::kMTransY, SkIntToScalar(ty));
-        mType |= SkMatrix::kTranslate_Mask;
-    } else {
-        mTransform.set(SkMatrix::kMTransX, 0);
-        mTransform.set(SkMatrix::kMTransY, 0);
-        mType &= ~SkMatrix::kTranslate_Mask;
+    mType = IDENTITY;
+    for(int i=0 ; i<3 ; i++) {
+        vec3& v(mMatrix[i]);
+        for (int j=0 ; j<3 ; j++)
+            v[j] = ((i==j) ? 1.0f : 0.0f);
     }
 }
 
-void Transform::transform(GLfixed* point, int x, int y) const
+void Transform::set(float tx, float ty)
 {
-    SkPoint s;
-    mTransform.mapXY(SkIntToScalar(x), SkIntToScalar(y), &s);
-    point[0] = SkScalarToFixed(s.fX);
-    point[1] = SkScalarToFixed(s.fY);
+    mMatrix[2][0] = tx;
+    mMatrix[2][1] = ty;
+    mMatrix[2][2] = 1.0f;
+
+    if (isZero(tx) && isZero(ty)) {
+        mType &= ~TRANSLATE;
+    } else {
+        mType |= TRANSLATE;
+    }
+}
+
+void Transform::set(float a, float b, float c, float d)
+{
+    mat33& M(mMatrix);
+    M[0][0] = a;    M[1][0] = b;
+    M[0][1] = c;    M[1][1] = d;
+    M[0][2] = 0;    M[1][2] = 0;
+    mType = UNKNOWN_TYPE;
+}
+
+void Transform::set(uint32_t flags, float w, float h)
+{
+    mType = flags << 8;
+    float sx = (flags & FLIP_H) ? -1 : 1;
+    float sy = (flags & FLIP_V) ? -1 : 1;
+    float a=0, b=0, c=0, d=0, x=0, y=0;
+    int xmask = 0;
+
+    // computation of x,y
+    // x y
+    // 0 0  0
+    // w 0  ROT90
+    // w h  FLIPH|FLIPV
+    // 0 h  FLIPH|FLIPV|ROT90
+
+    if (flags & ROT_90) {
+        mType |= ROTATE;
+        b = -sy;
+        c = sx;
+        xmask = 1;
+    } else {
+        a = sx;
+        d = sy;
+    }
+
+    if (flags & FLIP_H) {
+        mType ^= SCALE;
+        xmask ^= 1;
+    }
+
+    if (flags & FLIP_V) {
+        mType ^= SCALE;
+        y = h;
+    }
+
+    if ((flags & ROT_180) == ROT_180) {
+        mType |= ROTATE;
+    }
+
+    if (xmask) {
+        x = w;
+    }
+
+    if (!isZero(x) || !isZero(y)) {
+        mType |= TRANSLATE;
+    }
+
+    mat33& M(mMatrix);
+    M[0][0] = a;    M[1][0] = b;    M[2][0] = x;
+    M[0][1] = c;    M[1][1] = d;    M[2][1] = y;
+    M[0][2] = 0;    M[1][2] = 0;    M[2][2] = 1;
+}
+
+Transform::vec2 Transform::transform(const vec2& v) const {
+    vec2 r;
+    const mat33& M(mMatrix);
+    r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
+    r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
+    return r;
+}
+
+Transform::vec3 Transform::transform(const vec3& v) const {
+    vec3 r;
+    const mat33& M(mMatrix);
+    r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
+    r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
+    r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
+    return r;
+}
+
+void Transform::transform(fixed1616* point, int x, int y) const
+{
+    const float toFixed = 65536.0f;
+    const mat33& M(mMatrix);
+    vec2 v(x, y);
+    v = transform(v);
+    point[0] = v[0] * toFixed;
+    point[1] = v[1] * toFixed;
 }
 
 Rect Transform::makeBounds(int w, int h) const
 {
-    Rect r;
-    SkRect d, s;
-    s.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
-    mTransform.mapRect(&d, s);
-    r.left   = SkScalarRound( d.fLeft );
-    r.top    = SkScalarRound( d.fTop );
-    r.right  = SkScalarRound( d.fRight );
-    r.bottom = SkScalarRound( d.fBottom );
-    return r;
+    return transform( Rect(w, h) );
 }
 
 Rect Transform::transform(const Rect& bounds) const
 {
     Rect r;
-    SkRect d, s;
-    s.set(  SkIntToScalar( bounds.left ),
-            SkIntToScalar( bounds.top ),
-            SkIntToScalar( bounds.right ),
-            SkIntToScalar( bounds.bottom ));
-    mTransform.mapRect(&d, s);
-    r.left   = SkScalarRound( d.fLeft );
-    r.top    = SkScalarRound( d.fTop );
-    r.right  = SkScalarRound( d.fRight );
-    r.bottom = SkScalarRound( d.fBottom );
+    vec2 lt( bounds.left,  bounds.top    );
+    vec2 rt( bounds.right, bounds.top    );
+    vec2 lb( bounds.left,  bounds.bottom );
+    vec2 rb( bounds.right, bounds.bottom );
+
+    lt = transform(lt);
+    rt = transform(rt);
+    lb = transform(lb);
+    rb = transform(rb);
+
+    r.left   = floorf(min(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
+    r.top    = floorf(min(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
+    r.right  = floorf(max(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
+    r.bottom = floorf(max(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
+
     return r;
 }
 
 Region Transform::transform(const Region& reg) const
 {
     Region out;
-    if (UNLIKELY(transformed())) {
-        if (LIKELY(preserveRects())) {
+    if (CC_UNLIKELY(transformed())) {
+        if (CC_LIKELY(preserveRects())) {
             Region::const_iterator it = reg.begin();
             Region::const_iterator const end = reg.end();
             while (it != end) {
@@ -202,31 +279,108 @@
     return out;
 }
 
-int32_t Transform::getOrientation() const
+uint32_t Transform::type() const
 {
-    uint32_t flags = 0;
-    if (UNLIKELY(transformed())) {
-        SkScalar a = mTransform[SkMatrix::kMScaleX];
-        SkScalar b = mTransform[SkMatrix::kMSkewX];
-        SkScalar c = mTransform[SkMatrix::kMSkewY];
-        SkScalar d = mTransform[SkMatrix::kMScaleY];
-        if (b==0 && c==0 && a && d) {
-            if (a<0)    flags |= FLIP_H;
-            if (d<0)    flags |= FLIP_V;
-        } else if (b && c && a==0 && d==0) {
-            flags |= ROT_90;
-            if (b>0)    flags |= FLIP_H;
-            if (c<0)    flags |= FLIP_V;
+    if (mType & UNKNOWN_TYPE) {
+        // recompute what this transform is
+
+        const mat33& M(mMatrix);
+        const float a = M[0][0];
+        const float b = M[1][0];
+        const float c = M[0][1];
+        const float d = M[1][1];
+        const float x = M[2][0];
+        const float y = M[2][1];
+
+        bool scale = false;
+        uint32_t flags = ROT_0;
+        if (isZero(b) && isZero(c)) {
+            if (absEqual(a, d)) {
+                if (a<0)    flags |= FLIP_H;
+                if (d<0)    flags |= FLIP_V;
+                if (!absIsOne(a) || !absIsOne(d)) {
+                    scale = true;
+                }
+            } else {
+                flags = ROT_INVALID;
+            }
+        } else if (isZero(a) && isZero(d)) {
+            if (absEqual(b, c)) {
+                flags |= ROT_90;
+                if (b>0)    flags |= FLIP_H;
+                if (c<0)    flags |= FLIP_V;
+                if (!absIsOne(b) || !absIsOne(c)) {
+                    scale = true;
+                }
+            } else {
+                flags = ROT_INVALID;
+            }
         } else {
-            flags = 0x80000000;
+            flags = ROT_INVALID;
         }
+
+        mType = flags << 8;
+        if (flags & ROT_INVALID) {
+            mType |= UNKNOWN;
+        } else {
+            if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
+                mType |= ROTATE;
+            if (flags & FLIP_H)
+                mType ^= SCALE;
+            if (flags & FLIP_V)
+                mType ^= SCALE;
+            if (scale)
+                mType |= SCALE;
+        }
+
+        if (!isZero(x) || !isZero(y))
+            mType |= TRANSLATE;
     }
-    return flags;
+    return mType;
+}
+
+uint32_t Transform::getType() const {
+    return type() & 0xFF;
+}
+
+uint32_t Transform::getOrientation() const
+{
+    return (type() >> 8) & 0xFF;
 }
 
 bool Transform::preserveRects() const
 {
-    return mTransform.rectStaysRect();
+    return (type() & ROT_INVALID) ? false : true;
+}
+
+void Transform::dump(const char* name) const
+{
+    type(); // updates the type
+
+    String8 flags, type;
+    const mat33& m(mMatrix);
+    uint32_t orient = mType >> 8;
+
+    if (orient&ROT_INVALID)
+        flags.append("ROT_INVALID ");
+    if (orient&ROT_90)
+        flags.append("ROT_90 ");
+    if (orient&FLIP_V)
+        flags.append("FLIP_V ");
+    if (orient&FLIP_H)
+        flags.append("FLIP_H ");
+
+    if (mType&SCALE)
+        type.append("SCALE ");
+    if (mType&ROTATE)
+        type.append("ROTATE ");
+    if (mType&TRANSLATE)
+        type.append("TRANSLATE ");
+
+    LOGD("%s (%s, %s)", name, flags.string(), type.string());
+    LOGD("%.2f  %.2f  %.2f", m[0][0], m[1][0], m[2][0]);
+    LOGD("%.2f  %.2f  %.2f", m[0][1], m[1][1], m[2][1]);
+    LOGD("%.2f  %.2f  %.2f", m[0][2], m[1][2], m[2][2]);
 }
 
 // ---------------------------------------------------------------------------
diff --git a/libs/surfaceflinger/Transform.h b/libs/surfaceflinger/Transform.h
index ddab404..51d3e3f 100644
--- a/libs/surfaceflinger/Transform.h
+++ b/libs/surfaceflinger/Transform.h
@@ -23,10 +23,6 @@
 #include <ui/Point.h>
 #include <ui/Rect.h>
 
-#include <GLES/gl.h>
-
-#include <core/SkMatrix.h>
-
 namespace android {
 
 class Region;
@@ -38,9 +34,12 @@
 public:
                     Transform();
                     Transform(const Transform&  other);
-                    Transform(int32_t flags);
+           explicit Transform(uint32_t orientation);
                     ~Transform();
 
+            typedef int32_t fixed1616;
+
+            // FIXME: must match OVERLAY_TRANSFORM_*, pull from hardware.h
             enum orientation_flags {
                 ROT_0   = 0x00000000,
                 FLIP_H  = 0x00000001,
@@ -48,48 +47,79 @@
                 ROT_90  = 0x00000004,
                 ROT_180 = FLIP_H|FLIP_V,
                 ROT_270 = ROT_180|ROT_90,
-                ROT_INVALID = 0x80000000
+                ROT_INVALID = 0x80
             };
 
             enum type_mask {
                 IDENTITY            = 0,
                 TRANSLATE           = 0x1,
-                SCALE               = 0x2,
-                AFFINE              = 0x4,
-                PERSPECTIVE         = 0x8
+                ROTATE              = 0x2,
+                SCALE               = 0x4,
+                UNKNOWN             = 0x8
             };
 
-            bool    transformed() const;
-            int32_t getOrientation() const;
-            bool    preserveRects() const;
-            
+            // query the transform
+            bool        transformed() const;
+            bool        preserveRects() const;
+            uint32_t    getType() const;
+            uint32_t    getOrientation() const;
+
+            float const* operator [] (int i) const;  // returns column i
             int     tx() const;
             int     ty() const;
-        
+
+            // modify the transform
             void    reset();
-            void    set(float xx, float xy, float yx, float yy);
-            void    set(int tx, int ty);
-            void    set(float radian, float x, float y);
-            void    scale(float s, float x, float y);
-            
+            void    set(float tx, float ty);
+            void    set(float a, float b, float c, float d);
+            void    set(uint32_t flags, float w, float h);
+
+            // transform data
             Rect    makeBounds(int w, int h) const;
-            void    transform(GLfixed* point, int x, int y) const;
+            void    transform(fixed1616* point, int x, int y) const;
             Region  transform(const Region& reg) const;
-            Rect    transform(const Rect& bounds) const;
-
             Transform operator * (const Transform& rhs) const;
-            float operator [] (int i) const;
-
-    inline uint32_t getType() const { return type(); }
-            
-    inline Transform(bool) : mType(0xFF) { };
 
 private:
-    uint8_t     type() const;
+    struct vec3 {
+        float v[3];
+        inline vec3() { }
+        inline vec3(float a, float b, float c) {
+            v[0] = a; v[1] = b; v[2] = c;
+        }
+        inline float operator [] (int i) const { return v[i]; }
+        inline float& operator [] (int i) { return v[i]; }
+    };
+    struct vec2 {
+        float v[2];
+        inline vec2() { }
+        inline vec2(float a, float b) {
+            v[0] = a; v[1] = b;
+        }
+        inline float operator [] (int i) const { return v[i]; }
+        inline float& operator [] (int i) { return v[i]; }
+    };
+    struct mat33 {
+        vec3 v[3];
+        inline const vec3& operator [] (int i) const { return v[i]; }
+        inline vec3& operator [] (int i) { return v[i]; }
+    };
 
-private:
-            SkMatrix    mTransform;
-    mutable uint32_t    mType;      
+    enum { UNKNOWN_TYPE = 0x80000000 };
+
+    // assumes the last row is < 0 , 0 , 1 >
+    vec2 transform(const vec2& v) const;
+    vec3 transform(const vec3& v) const;
+    Rect transform(const Rect& bounds) const;
+    uint32_t type() const;
+    static bool absIsOne(float f);
+    static bool absEqual(float a, float b);
+    static bool isZero(float f);
+
+    void dump(const char* name) const;
+
+    mat33               mMatrix;
+    mutable uint32_t    mType;
 };
 
 // ---------------------------------------------------------------------------
diff --git a/libs/surfaceflinger_client/ISurface.cpp b/libs/surfaceflinger_client/ISurface.cpp
index 9125146..bb86199 100644
--- a/libs/surfaceflinger_client/ISurface.cpp
+++ b/libs/surfaceflinger_client/ISurface.cpp
@@ -78,7 +78,8 @@
         data.writeInt32(bufferIdx);
         data.writeInt32(usage);
         remote()->transact(REQUEST_BUFFER, data, &reply);
-        sp<GraphicBuffer> buffer = new GraphicBuffer(reply);
+        sp<GraphicBuffer> buffer = new GraphicBuffer();
+        reply.read(*buffer);
         return buffer;
     }
 
@@ -141,7 +142,9 @@
             int bufferIdx = data.readInt32();
             int usage = data.readInt32();
             sp<GraphicBuffer> buffer(requestBuffer(bufferIdx, usage));
-            return GraphicBuffer::writeToParcel(reply, buffer.get());
+            if (buffer == NULL)
+                return BAD_VALUE;
+            return reply->write(*buffer);
         }
         case REGISTER_BUFFERS: {
             CHECK_INTERFACE(ISurface, data, reply);
diff --git a/libs/ui/GraphicBuffer.cpp b/libs/ui/GraphicBuffer.cpp
index 6a5c8a9..ba1fd9c 100644
--- a/libs/ui/GraphicBuffer.cpp
+++ b/libs/ui/GraphicBuffer.cpp
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "GraphicBuffer"
+
 #include <stdlib.h>
 #include <stdint.h>
 #include <sys/types.h>
 
-#include <binder/Parcel.h>
-
 #include <utils/Errors.h>
 #include <utils/Log.h>
 
@@ -77,34 +77,21 @@
     handle = inHandle;
 }
 
-GraphicBuffer::GraphicBuffer(const Parcel& data) 
-    : BASE(), mOwner(ownHandle), mBufferMapper(GraphicBufferMapper::get()),
-      mInitCheck(NO_ERROR),  mVStride(0), mIndex(-1)
-{
-    // we own the handle in this case
-    width  = data.readInt32();
-    if (width < 0) {
-        width = height = stride = format = usage = 0;
-        handle = 0;
-    } else {
-        height = data.readInt32();
-        stride = data.readInt32();
-        format = data.readInt32();
-        usage  = data.readInt32();
-        handle = data.readNativeHandle();
-    }
-}
-
 GraphicBuffer::~GraphicBuffer()
 {
     if (handle) {
-        if (mOwner == ownHandle) {
-            native_handle_close(handle);
-            native_handle_delete(const_cast<native_handle*>(handle));
-        } else if (mOwner == ownData) {
-            GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
-            allocator.free(handle);
-        }
+        free_handle();
+    }
+}
+
+void GraphicBuffer::free_handle()
+{
+    if (mOwner == ownHandle) {
+        native_handle_close(handle);
+        native_handle_delete(const_cast<native_handle*>(handle));
+    } else if (mOwner == ownData) {
+        GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
+        allocator.free(handle);
     }
 }
 
@@ -192,29 +179,83 @@
     return res;
 }
 
+size_t GraphicBuffer::getFlattenedSize() const {
+    return (8 + (handle ? handle->numInts : 0))*sizeof(int);
+}
 
-status_t GraphicBuffer::writeToParcel(Parcel* reply, 
-        android_native_buffer_t const* buffer)
+size_t GraphicBuffer::getFdCount() const {
+    return handle ? handle->numFds : 0;
+}
+
+status_t GraphicBuffer::flatten(void* buffer, size_t size,
+        int fds[], size_t count) const
 {
-    if (buffer == NULL)
-        return BAD_VALUE;
+    size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
+    if (size < sizeNeeded) return NO_MEMORY;
 
-    if (buffer->width < 0 || buffer->height < 0)
-        return BAD_VALUE;
+    size_t fdCountNeeded = GraphicBuffer::getFdCount();
+    if (count < fdCountNeeded) return NO_MEMORY;
 
-    status_t err = NO_ERROR;
-    if (buffer->handle == NULL) {
-        // this buffer doesn't have a handle
-        reply->writeInt32(NO_MEMORY);
-    } else {
-        reply->writeInt32(buffer->width);
-        reply->writeInt32(buffer->height);
-        reply->writeInt32(buffer->stride);
-        reply->writeInt32(buffer->format);
-        reply->writeInt32(buffer->usage);
-        err = reply->writeNativeHandle(buffer->handle);
+    int* buf = static_cast<int*>(buffer);
+    buf[0] = 'GBFR';
+    buf[1] = width;
+    buf[2] = height;
+    buf[3] = stride;
+    buf[4] = format;
+    buf[5] = usage;
+    buf[6] = 0;
+    buf[7] = 0;
+
+    if (handle) {
+        buf[6] = handle->numFds;
+        buf[7] = handle->numInts;
+        native_handle_t const* const h = handle;
+        memcpy(fds,     h->data,             h->numFds*sizeof(int));
+        memcpy(&buf[8], h->data + h->numFds, h->numInts*sizeof(int));
     }
-    return err;
+
+    return NO_ERROR;
+}
+
+status_t GraphicBuffer::unflatten(void const* buffer, size_t size,
+        int fds[], size_t count)
+{
+    if (size < 8*sizeof(int)) return NO_MEMORY;
+
+    int const* buf = static_cast<int const*>(buffer);
+    if (buf[0] != 'GBFR') return BAD_TYPE;
+
+    const size_t numFds  = buf[6];
+    const size_t numInts = buf[7];
+
+    const size_t sizeNeeded = (8 + numInts) * sizeof(int);
+    if (size < sizeNeeded) return NO_MEMORY;
+
+    size_t fdCountNeeded = 0;
+    if (count < fdCountNeeded) return NO_MEMORY;
+
+    if (handle) {
+        // free previous handle if any
+        free_handle();
+    }
+
+    if (numFds || numInts) {
+        width  = buf[1];
+        height = buf[2];
+        stride = buf[3];
+        format = buf[4];
+        usage  = buf[5];
+        native_handle* h = native_handle_create(numFds, numInts);
+        memcpy(h->data,          fds,     numFds*sizeof(int));
+        memcpy(h->data + numFds, &buf[8], numInts*sizeof(int));
+        handle = h;
+    } else {
+        width = height = stride = format = usage = 0;
+        handle = NULL;
+    }
+
+    mOwner = ownHandle;
+    return NO_ERROR;
 }
 
 
diff --git a/libs/utils/Android.mk b/libs/utils/Android.mk
index d2cfd3b..d0eedb4 100644
--- a/libs/utils/Android.mk
+++ b/libs/utils/Android.mk
@@ -25,6 +25,7 @@
 	CallStack.cpp \
 	Debug.cpp \
 	FileMap.cpp \
+	Flattenable.cpp \
 	RefBase.cpp \
 	ResourceTypes.cpp \
 	SharedBuffer.cpp \
diff --git a/libs/utils/Flattenable.cpp b/libs/utils/Flattenable.cpp
new file mode 100644
index 0000000..1f2ffaa
--- /dev/null
+++ b/libs/utils/Flattenable.cpp
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2006 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <utils/Flattenable.h>
+
+namespace android {
+
+Flattenable::~Flattenable() {
+}
+
+}; // namespace android
diff --git a/libs/utils/ResourceTypes.cpp b/libs/utils/ResourceTypes.cpp
index e8bd5cf..38600b9 100644
--- a/libs/utils/ResourceTypes.cpp
+++ b/libs/utils/ResourceTypes.cpp
@@ -497,6 +497,34 @@
     return NULL;
 }
 
+const char* ResStringPool::string8At(size_t idx, size_t* outLen) const
+{
+    if (mError == NO_ERROR && idx < mHeader->stringCount) {
+        const bool isUTF8 = (mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0;
+        const uint32_t off = mEntries[idx]/(isUTF8?sizeof(char):sizeof(char16_t));
+        if (off < (mStringPoolSize-1)) {
+            if (isUTF8) {
+                const uint8_t* strings = (uint8_t*)mStrings;
+                const uint8_t* str = strings+off;
+                DECODE_LENGTH(str, sizeof(uint8_t), *outLen)
+                size_t encLen;
+                DECODE_LENGTH(str, sizeof(uint8_t), encLen)
+                if ((uint32_t)(str+encLen-strings) < mStringPoolSize) {
+                    return (const char*)str;
+                } else {
+                    LOGW("Bad string block: string #%d extends to %d, past end at %d\n",
+                            (int)idx, (int)(str+encLen-strings), (int)mStringPoolSize);
+                }
+            }
+        } else {
+            LOGW("Bad string block: string #%d entry is at %d, past end at %d\n",
+                    (int)idx, (int)(off*sizeof(uint16_t)),
+                    (int)(mStringPoolSize*sizeof(uint16_t)));
+        }
+    }
+    return NULL;
+}
+
 const ResStringPool_span* ResStringPool::styleAt(const ResStringPool_ref& ref) const
 {
     return styleAt(ref.index);
@@ -4018,14 +4046,19 @@
         printf("(attribute) 0x%08x\n", value.data);
     } else if (value.dataType == Res_value::TYPE_STRING) {
         size_t len;
-        const char16_t* str = pkg->header->values.stringAt(
+        const char* str8 = pkg->header->values.string8At(
                 value.data, &len);
-        if (str == NULL) {
-            printf("(string) null\n");
+        if (str8 != NULL) {
+            printf("(string8) \"%s\"\n", str8);
         } else {
-            printf("(string%d) \"%s\"\n",
-                    pkg->header->values.isUTF8()?8:16,
-                    String8(str, len).string());
+            const char16_t* str16 = pkg->header->values.stringAt(
+                    value.data, &len);
+            if (str16 != NULL) {
+                printf("(string16) \"%s\"\n",
+                    String8(str16, len).string());
+            } else {
+                printf("(string) null\n");
+            }
         } 
     } else if (value.dataType == Res_value::TYPE_FLOAT) {
         printf("(float) %g\n", *(const float*)&value.data);