Merge "Remove "StringTest.java"."
diff --git a/common/java/com/android/common/HttpDateTime.java b/common/java/com/android/common/HttpDateTime.java
new file mode 100644
index 0000000..f4052cc
--- /dev/null
+++ b/common/java/com/android/common/HttpDateTime.java
@@ -0,0 +1,224 @@
+/*
+ * Copyright (C) 2007 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.
+ */
+
+package com.android.common;
+
+import android.text.format.Time;
+
+import java.util.Calendar;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+
+/** {@hide} */
+public final class HttpDateTime {
+
+    /*
+     * Regular expression for parsing HTTP-date.
+     *
+     * Wdy, DD Mon YYYY HH:MM:SS GMT
+     * RFC 822, updated by RFC 1123
+     *
+     * Weekday, DD-Mon-YY HH:MM:SS GMT
+     * RFC 850, obsoleted by RFC 1036
+     *
+     * Wdy Mon DD HH:MM:SS YYYY
+     * ANSI C's asctime() format
+     *
+     * with following variations
+     *
+     * Wdy, DD-Mon-YYYY HH:MM:SS GMT
+     * Wdy, (SP)D Mon YYYY HH:MM:SS GMT
+     * Wdy,DD Mon YYYY HH:MM:SS GMT
+     * Wdy, DD-Mon-YY HH:MM:SS GMT
+     * Wdy, DD Mon YYYY HH:MM:SS -HHMM
+     * Wdy, DD Mon YYYY HH:MM:SS
+     * Wdy Mon (SP)D HH:MM:SS YYYY
+     * Wdy Mon DD HH:MM:SS YYYY GMT
+     * 
+     * HH can be H if the first digit is zero.
+     * 
+     * Mon can be the full name of the month.
+     */
+    private static final String HTTP_DATE_RFC_REGEXP =
+            "([0-9]{1,2})[- ]([A-Za-z]{3,9})[- ]([0-9]{2,4})[ ]"
+            + "([0-9]{1,2}:[0-9][0-9]:[0-9][0-9])";
+
+    private static final String HTTP_DATE_ANSIC_REGEXP =
+            "[ ]([A-Za-z]{3,9})[ ]+([0-9]{1,2})[ ]"
+            + "([0-9]{1,2}:[0-9][0-9]:[0-9][0-9])[ ]([0-9]{2,4})";
+
+    /**
+     * The compiled version of the HTTP-date regular expressions.
+     */
+    private static final Pattern HTTP_DATE_RFC_PATTERN =
+            Pattern.compile(HTTP_DATE_RFC_REGEXP);
+    private static final Pattern HTTP_DATE_ANSIC_PATTERN =
+            Pattern.compile(HTTP_DATE_ANSIC_REGEXP);
+
+    private static class TimeOfDay {
+        TimeOfDay(int h, int m, int s) {
+            this.hour = h;
+            this.minute = m;
+            this.second = s;
+        }
+        
+        int hour;
+        int minute;
+        int second;
+    }
+
+    public static Long parse(String timeString)
+            throws IllegalArgumentException {
+
+        int date = 1;
+        int month = Calendar.JANUARY;
+        int year = 1970;
+        TimeOfDay timeOfDay;
+
+        Matcher rfcMatcher = HTTP_DATE_RFC_PATTERN.matcher(timeString);
+        if (rfcMatcher.find()) {
+            date = getDate(rfcMatcher.group(1));
+            month = getMonth(rfcMatcher.group(2));
+            year = getYear(rfcMatcher.group(3));
+            timeOfDay = getTime(rfcMatcher.group(4));
+        } else {
+            Matcher ansicMatcher = HTTP_DATE_ANSIC_PATTERN.matcher(timeString);
+            if (ansicMatcher.find()) {
+                month = getMonth(ansicMatcher.group(1));
+                date = getDate(ansicMatcher.group(2));
+                timeOfDay = getTime(ansicMatcher.group(3));
+                year = getYear(ansicMatcher.group(4));
+            } else {
+                throw new IllegalArgumentException();
+            }
+        }
+
+        // FIXME: Y2038 BUG!
+        if (year >= 2038) {
+            year = 2038;
+            month = Calendar.JANUARY;
+            date = 1;
+        }
+
+        Time time = new Time(Time.TIMEZONE_UTC);
+        time.set(timeOfDay.second, timeOfDay.minute, timeOfDay.hour, date,
+                month, year);
+        return time.toMillis(false /* use isDst */);
+    }
+
+    private static int getDate(String dateString) {
+        if (dateString.length() == 2) {
+            return (dateString.charAt(0) - '0') * 10
+                    + (dateString.charAt(1) - '0');
+        } else {
+            return (dateString.charAt(0) - '0');
+        }
+    }
+
+    /*
+     * jan = 9 + 0 + 13 = 22
+     * feb = 5 + 4 + 1 = 10
+     * mar = 12 + 0 + 17 = 29
+     * apr = 0 + 15 + 17 = 32
+     * may = 12 + 0 + 24 = 36
+     * jun = 9 + 20 + 13 = 42
+     * jul = 9 + 20 + 11 = 40
+     * aug = 0 + 20 + 6 = 26
+     * sep = 18 + 4 + 15 = 37
+     * oct = 14 + 2 + 19 = 35
+     * nov = 13 + 14 + 21 = 48
+     * dec = 3 + 4 + 2 = 9
+     */
+    private static int getMonth(String monthString) {
+        int hash = Character.toLowerCase(monthString.charAt(0)) +
+                Character.toLowerCase(monthString.charAt(1)) +
+                Character.toLowerCase(monthString.charAt(2)) - 3 * 'a';
+        switch (hash) {
+            case 22:
+                return Calendar.JANUARY;
+            case 10:
+                return Calendar.FEBRUARY;
+            case 29:
+                return Calendar.MARCH;
+            case 32:
+                return Calendar.APRIL;
+            case 36:
+                return Calendar.MAY;
+            case 42:
+                return Calendar.JUNE;
+            case 40:
+                return Calendar.JULY;
+            case 26:
+                return Calendar.AUGUST;
+            case 37:
+                return Calendar.SEPTEMBER;
+            case 35:
+                return Calendar.OCTOBER;
+            case 48:
+                return Calendar.NOVEMBER;
+            case 9:
+                return Calendar.DECEMBER;
+            default:
+                throw new IllegalArgumentException();
+        }
+    }
+
+    private static int getYear(String yearString) {
+        if (yearString.length() == 2) {
+            int year = (yearString.charAt(0) - '0') * 10
+                    + (yearString.charAt(1) - '0');
+            if (year >= 70) {
+                return year + 1900;
+            } else {
+                return year + 2000;
+            }
+        } else if (yearString.length() == 3) {
+            // According to RFC 2822, three digit years should be added to 1900.
+            int year = (yearString.charAt(0) - '0') * 100
+                    + (yearString.charAt(1) - '0') * 10
+                    + (yearString.charAt(2) - '0');
+            return year + 1900;
+        } else if (yearString.length() == 4) {
+             return (yearString.charAt(0) - '0') * 1000
+                    + (yearString.charAt(1) - '0') * 100
+                    + (yearString.charAt(2) - '0') * 10
+                    + (yearString.charAt(3) - '0');
+        } else {
+             return 1970;
+        }
+    }
+
+    private static TimeOfDay getTime(String timeString) {
+        // HH might be H
+        int i = 0;
+        int hour = timeString.charAt(i++) - '0';
+        if (timeString.charAt(i) != ':')
+            hour = hour * 10 + (timeString.charAt(i++) - '0');
+        // Skip ':'
+        i++;
+        
+        int minute = (timeString.charAt(i++) - '0') * 10
+                    + (timeString.charAt(i++) - '0');
+        // Skip ':'
+        i++;
+        
+        int second = (timeString.charAt(i++) - '0') * 10
+                  + (timeString.charAt(i++) - '0');
+
+        return new TimeOfDay(hour, minute, second);        
+    }
+}
diff --git a/include/private/opengles/gl_context.h b/include/private/opengles/gl_context.h
index 26cde38..e0deba9 100644
--- a/include/private/opengles/gl_context.h
+++ b/include/private/opengles/gl_context.h
@@ -36,7 +36,12 @@
 
 namespace android {
 
-const unsigned int OGLES_NUM_COMPRESSED_TEXTURE_FORMATS = 10;
+
+const unsigned int OGLES_NUM_COMPRESSED_TEXTURE_FORMATS = 10
+#ifdef GL_OES_compressed_ETC1_RGB8_texture
+        + 1
+#endif
+        ;
 
 class EGLTextureObject;
 class EGLSurfaceManager;
diff --git a/mms-common/java/com/android/common/CharacterSets.java b/mms-common/java/com/android/mmscommon/CharacterSets.java
similarity index 100%
rename from mms-common/java/com/android/common/CharacterSets.java
rename to mms-common/java/com/android/mmscommon/CharacterSets.java
diff --git a/mms-common/java/com/android/common/ContentType.java b/mms-common/java/com/android/mmscommon/ContentType.java
similarity index 100%
rename from mms-common/java/com/android/common/ContentType.java
rename to mms-common/java/com/android/mmscommon/ContentType.java
diff --git a/mms-common/java/com/android/common/EncodedStringValue.java b/mms-common/java/com/android/mmscommon/EncodedStringValue.java
similarity index 100%
rename from mms-common/java/com/android/common/EncodedStringValue.java
rename to mms-common/java/com/android/mmscommon/EncodedStringValue.java
diff --git a/mms-common/java/com/android/common/InvalidHeaderValueException.java b/mms-common/java/com/android/mmscommon/InvalidHeaderValueException.java
similarity index 100%
rename from mms-common/java/com/android/common/InvalidHeaderValueException.java
rename to mms-common/java/com/android/mmscommon/InvalidHeaderValueException.java
diff --git a/mms-common/java/com/android/common/MmsException.java b/mms-common/java/com/android/mmscommon/MmsException.java
similarity index 100%
rename from mms-common/java/com/android/common/MmsException.java
rename to mms-common/java/com/android/mmscommon/MmsException.java
diff --git a/mms-common/java/com/android/common/PduHeaders.java b/mms-common/java/com/android/mmscommon/PduHeaders.java
similarity index 100%
rename from mms-common/java/com/android/common/PduHeaders.java
rename to mms-common/java/com/android/mmscommon/PduHeaders.java
diff --git a/mms-common/java/com/android/common/mms/ContentType.java b/mms-common/java/com/android/mmscommon/mms/ContentType.java
similarity index 99%
rename from mms-common/java/com/android/common/mms/ContentType.java
rename to mms-common/java/com/android/mmscommon/mms/ContentType.java
index 0fdb46c..f21eba8 100644
--- a/mms-common/java/com/android/common/mms/ContentType.java
+++ b/mms-common/java/com/android/mmscommon/mms/ContentType.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package com.android.mms.mms;
+package com.android.mmscommon.mms;
 
 import java.util.ArrayList;
 
diff --git a/mms-common/java/com/android/common/mms/pdu/AcknowledgeInd.java b/mms-common/java/com/android/mmscommon/mms/pdu/AcknowledgeInd.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/AcknowledgeInd.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/AcknowledgeInd.java
diff --git a/mms-common/java/com/android/common/mms/pdu/Base64.java b/mms-common/java/com/android/mmscommon/mms/pdu/Base64.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/Base64.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/Base64.java
diff --git a/mms-common/java/com/android/common/mms/pdu/DeliveryInd.java b/mms-common/java/com/android/mmscommon/mms/pdu/DeliveryInd.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/DeliveryInd.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/DeliveryInd.java
diff --git a/mms-common/java/com/android/common/mms/pdu/GenericPdu.java b/mms-common/java/com/android/mmscommon/mms/pdu/GenericPdu.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/GenericPdu.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/GenericPdu.java
diff --git a/mms-common/java/com/android/common/mms/pdu/MultimediaMessagePdu.java b/mms-common/java/com/android/mmscommon/mms/pdu/MultimediaMessagePdu.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/MultimediaMessagePdu.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/MultimediaMessagePdu.java
diff --git a/mms-common/java/com/android/common/mms/pdu/NotificationInd.java b/mms-common/java/com/android/mmscommon/mms/pdu/NotificationInd.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/NotificationInd.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/NotificationInd.java
diff --git a/mms-common/java/com/android/common/mms/pdu/NotifyRespInd.java b/mms-common/java/com/android/mmscommon/mms/pdu/NotifyRespInd.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/NotifyRespInd.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/NotifyRespInd.java
diff --git a/mms-common/java/com/android/common/mms/pdu/PduBody.java b/mms-common/java/com/android/mmscommon/mms/pdu/PduBody.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/PduBody.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/PduBody.java
diff --git a/mms-common/java/com/android/common/mms/pdu/PduComposer.java b/mms-common/java/com/android/mmscommon/mms/pdu/PduComposer.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/PduComposer.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/PduComposer.java
diff --git a/mms-common/java/com/android/common/mms/pdu/PduContentTypes.java b/mms-common/java/com/android/mmscommon/mms/pdu/PduContentTypes.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/PduContentTypes.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/PduContentTypes.java
diff --git a/mms-common/java/com/android/common/mms/pdu/PduParser.java b/mms-common/java/com/android/mmscommon/mms/pdu/PduParser.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/PduParser.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/PduParser.java
diff --git a/mms-common/java/com/android/common/mms/pdu/PduPart.java b/mms-common/java/com/android/mmscommon/mms/pdu/PduPart.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/PduPart.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/PduPart.java
diff --git a/mms-common/java/com/android/common/mms/pdu/PduPersister.java b/mms-common/java/com/android/mmscommon/mms/pdu/PduPersister.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/PduPersister.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/PduPersister.java
diff --git a/mms-common/java/com/android/common/mms/pdu/QuotedPrintable.java b/mms-common/java/com/android/mmscommon/mms/pdu/QuotedPrintable.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/QuotedPrintable.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/QuotedPrintable.java
diff --git a/mms-common/java/com/android/common/mms/pdu/ReadOrigInd.java b/mms-common/java/com/android/mmscommon/mms/pdu/ReadOrigInd.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/ReadOrigInd.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/ReadOrigInd.java
diff --git a/mms-common/java/com/android/common/mms/pdu/ReadRecInd.java b/mms-common/java/com/android/mmscommon/mms/pdu/ReadRecInd.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/ReadRecInd.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/ReadRecInd.java
diff --git a/mms-common/java/com/android/common/mms/pdu/RetrieveConf.java b/mms-common/java/com/android/mmscommon/mms/pdu/RetrieveConf.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/RetrieveConf.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/RetrieveConf.java
diff --git a/mms-common/java/com/android/common/mms/pdu/SendConf.java b/mms-common/java/com/android/mmscommon/mms/pdu/SendConf.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/SendConf.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/SendConf.java
diff --git a/mms-common/java/com/android/common/mms/pdu/SendReq.java b/mms-common/java/com/android/mmscommon/mms/pdu/SendReq.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/pdu/SendReq.java
rename to mms-common/java/com/android/mmscommon/mms/pdu/SendReq.java
diff --git a/mms-common/java/com/android/common/mms/util/AbstractCache.java b/mms-common/java/com/android/mmscommon/mms/util/AbstractCache.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/util/AbstractCache.java
rename to mms-common/java/com/android/mmscommon/mms/util/AbstractCache.java
diff --git a/mms-common/java/com/android/common/mms/util/PduCache.java b/mms-common/java/com/android/mmscommon/mms/util/PduCache.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/util/PduCache.java
rename to mms-common/java/com/android/mmscommon/mms/util/PduCache.java
diff --git a/mms-common/java/com/android/common/mms/util/PduCacheEntry.java b/mms-common/java/com/android/mmscommon/mms/util/PduCacheEntry.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/util/PduCacheEntry.java
rename to mms-common/java/com/android/mmscommon/mms/util/PduCacheEntry.java
diff --git a/mms-common/java/com/android/common/mms/telephony/TelephonyProvider.java b/mms-common/java/com/android/mmscommon/telephony/TelephonyProvider.java
similarity index 100%
rename from mms-common/java/com/android/common/mms/telephony/TelephonyProvider.java
rename to mms-common/java/com/android/mmscommon/telephony/TelephonyProvider.java
diff --git a/opengl/libagl/Android.mk b/opengl/libagl/Android.mk
index c2e9f31..6cb146c 100644
--- a/opengl/libagl/Android.mk
+++ b/opengl/libagl/Android.mk
@@ -29,7 +29,7 @@
 LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES -DEGL_EGLEXT_PROTOTYPES
 LOCAL_CFLAGS += -fvisibility=hidden
 
-LOCAL_SHARED_LIBRARIES := libcutils libhardware libutils libpixelflinger
+LOCAL_SHARED_LIBRARIES := libcutils libhardware libutils libpixelflinger libETC1
 LOCAL_LDLIBS := -lpthread -ldl
 
 ifeq ($(TARGET_ARCH),arm)
diff --git a/opengl/libagl/state.cpp b/opengl/libagl/state.cpp
index 0f1f27d..1224a96 100644
--- a/opengl/libagl/state.cpp
+++ b/opengl/libagl/state.cpp
@@ -51,6 +51,9 @@
     //        "GL_OES_point_size_array "              // TODO
     //        "GL_OES_point_sprite "                  // TODO
     "GL_OES_EGL_image "                     // OK
+#ifdef GL_OES_compressed_ETC1_RGB8_texture
+    "GL_OES_compressed_ETC1_RGB8_texture "  // OK
+#endif
     "GL_ARB_texture_compression "           // OK
     "GL_ARB_texture_non_power_of_two "      // OK
     "GL_ANDROID_user_clip_plane "           // OK
@@ -386,6 +389,7 @@
 
 void glGetIntegerv(GLenum pname, GLint *params)
 {
+    int i;
     ogles_context_t* c = ogles_context_t::get();
     switch (pname) {
     case GL_ALIASED_POINT_SIZE_RANGE:
@@ -431,6 +435,10 @@
         params[ 7] = GL_PALETTE8_R5_G6_B5_OES;
         params[ 8] = GL_PALETTE8_RGBA4_OES;
         params[ 9] = GL_PALETTE8_RGB5_A1_OES;
+        i = 10;
+#ifdef GL_OES_compressed_ETC1_RGB8_texture
+        params[i++] = GL_ETC1_RGB8_OES;
+#endif
         break;
     case GL_DEPTH_BITS:
         params[0] = c->rasterizer.state.buffers.depth.format ? 0 : 16;
diff --git a/opengl/libagl/texture.cpp b/opengl/libagl/texture.cpp
index 2875c13..a1a776f 100644
--- a/opengl/libagl/texture.cpp
+++ b/opengl/libagl/texture.cpp
@@ -24,6 +24,7 @@
 #include "TextureObjectManager.h"
 
 #include <private/ui/android_natives_priv.h>
+#include <ETC1/etc1.h>
 
 #ifdef LIBAGL_USE_GRALLOC_COPYBITS
 #include "copybit.h"
@@ -1081,11 +1082,6 @@
         ogles_error(c, GL_INVALID_ENUM);
         return;
     }
-    if ((internalformat < GL_PALETTE4_RGB8_OES ||
-         internalformat > GL_PALETTE8_RGB5_A1_OES)) {
-        ogles_error(c, GL_INVALID_ENUM);
-        return;
-    }
     if (width<0 || height<0 || border!=0) {
         ogles_error(c, GL_INVALID_VALUE);
         return;
@@ -1121,6 +1117,12 @@
         format      = GL_RGBA;
         type        = GL_UNSIGNED_SHORT_5_5_5_1;
         break;
+#ifdef GL_OES_compressed_ETC1_RGB8_texture
+    case GL_ETC1_RGB8_OES:
+        format      = GL_RGB;
+        type        = GL_UNSIGNED_BYTE;
+        break;
+#endif
     default:
         ogles_error(c, GL_INVALID_ENUM);
         return;
@@ -1133,6 +1135,30 @@
 
     int32_t size;
     GGLSurface* surface;
+
+#ifdef GL_OES_compressed_ETC1_RGB8_texture
+    if (internalformat == GL_ETC1_RGB8_OES) {
+        GLsizei compressedSize = etc1_get_encoded_data_size(width, height);
+        if (compressedSize > imageSize) {
+            ogles_error(c, GL_INVALID_VALUE);
+            return;
+        }
+        int error = createTextureSurface(c, &surface, &size,
+                level, format, type, width, height);
+        if (error) {
+            ogles_error(c, error);
+            return;
+        }
+        if (etc1_decode_image(
+                (const etc1_byte*)data,
+                (etc1_byte*)surface->data,
+                width, height, 3, surface->stride*3) != 0) {
+            ogles_error(c, GL_INVALID_OPERATION);
+        }
+        return;
+    }
+#endif
+
     // all mipmap levels are specified at once.
     const int numLevels = level<0 ? -level : 1;
 
@@ -1389,9 +1415,20 @@
     // (x,y) is the lower-left corner of colorBuffer
     y = cbSurface.height - (y + height);
 
+    /* The GLES spec says:
+     * If any of the pixels within the specified rectangle are outside
+     * the framebuffer associated with the current rendering context,
+     * then the values obtained for those pixels are undefined.
+     */
+    if (x+width > GLint(cbSurface.width))
+        width = cbSurface.width - x;
+
+    if (y+height > GLint(cbSurface.height))
+        height = cbSurface.height - y;
+
     int err = copyPixels(c,
             txSurface, 0, 0,
-            cbSurface, x, y, cbSurface.width, cbSurface.height);
+            cbSurface, x, y, width, height);
     if (err) {
         ogles_error(c, err);
     }
@@ -1439,6 +1476,17 @@
     const GGLSurface& cbSurface = c->rasterizer.state.buffers.color.s;
     y = cbSurface.height - (y + height);
 
+    /* The GLES spec says:
+     * If any of the pixels within the specified rectangle are outside
+     * the framebuffer associated with the current rendering context,
+     * then the values obtained for those pixels are undefined.
+     */
+    if (x+width > GLint(cbSurface.width))
+        width = cbSurface.width - x;
+
+    if (y+height > GLint(cbSurface.height))
+        height = cbSurface.height - y;
+
     int err = copyPixels(c,
             surface, xoffset, yoffset,
             cbSurface, x, y, width, height);