diff --git a/core/jni/android/graphics/Bitmap.cpp b/core/jni/android/graphics/Bitmap.cpp index b96e8bd90ebbe..b1b2959ee9457 100755 --- a/core/jni/android/graphics/Bitmap.cpp +++ b/core/jni/android/graphics/Bitmap.cpp @@ -573,6 +573,14 @@ static jobject Bitmap_copy(JNIEnv* env, jobject, jlong srcHandle, jint dstConfigHandle, jboolean isMutable) { SkBitmap src; reinterpret_cast(srcHandle)->getSkBitmap(&src); + if (dstConfigHandle == GraphicsJNI::hardwareLegacyBitmapConfig()) { + sk_sp bitmap(Bitmap::allocateHardwareBitmap(src)); + if (!bitmap.get()) { + return NULL; + } + return createBitmap(env, bitmap.release(), kBitmapCreateFlag_None); + } + SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle); SkBitmap result; HeapAllocator allocator; diff --git a/graphics/java/android/graphics/Bitmap.java b/graphics/java/android/graphics/Bitmap.java index cd75fe95121ae..289ec7a4c3567 100644 --- a/graphics/java/android/graphics/Bitmap.java +++ b/graphics/java/android/graphics/Bitmap.java @@ -583,9 +583,13 @@ public final class Bitmap implements Parcelable { * @param isMutable True if the resulting bitmap should be mutable (i.e. * its pixels can be modified) * @return the new bitmap, or null if the copy could not be made. + * @throws IllegalArgumentException if config is {@link Config#HARDWARE} and isMutable is true */ public Bitmap copy(Config config, boolean isMutable) { checkRecycled("Can't copy a recycled bitmap"); + if (config == Config.HARDWARE && isMutable) { + throw new IllegalArgumentException("Hardware bitmaps are always immutable"); + } Bitmap b = nativeCopy(mNativePtr, config.nativeInt, isMutable); if (b != null) { b.setPremultiplied(mRequestPremultiplied); diff --git a/libs/hwui/hwui/Bitmap.cpp b/libs/hwui/hwui/Bitmap.cpp index 2177af1773e5e..39a8499c284d1 100644 --- a/libs/hwui/hwui/Bitmap.cpp +++ b/libs/hwui/hwui/Bitmap.cpp @@ -98,8 +98,6 @@ static sk_sp allocateHeapBitmap(size_t size, const SkImageInfo& info, si // TODO: handle SRGB sanely static PixelFormat internalFormatToPixelFormat(GLint internalFormat) { switch (internalFormat) { - case GL_ALPHA: - return PIXEL_FORMAT_TRANSPARENT; case GL_LUMINANCE: return PIXEL_FORMAT_RGBA_8888; case GL_SRGB8_ALPHA8: @@ -215,8 +213,8 @@ sk_sp Bitmap::allocateHardwareBitmap(uirenderer::renderthread::RenderThr } const SkImageInfo& info = skBitmap.info(); - if (info.colorType() == kUnknown_SkColorType) { - ALOGW("unable to create hardware bitmap of configuration"); + if (info.colorType() == kUnknown_SkColorType || info.colorType() == kAlpha_8_SkColorType) { + ALOGW("unable to create hardware bitmap of colortype: %d", info.colorType()); return nullptr; } @@ -249,7 +247,7 @@ sk_sp Bitmap::allocateHardwareBitmap(uirenderer::renderthread::RenderThr if (!uploadBitmapToGraphicBuffer(caches, bitmap, *buffer, format, type)) { return nullptr; } - return sk_sp(new Bitmap(buffer.get(), info)); + return sk_sp(new Bitmap(buffer.get(), bitmap.info())); } sk_sp Bitmap::allocateHardwareBitmap(SkBitmap& bitmap) { @@ -310,7 +308,8 @@ sk_sp Bitmap::createFrom(sp graphicBuffer) { return nullptr; } SkImageInfo info = SkImageInfo::Make(graphicBuffer->getWidth(), graphicBuffer->getHeight(), - kRGBA_8888_SkColorType, kPremul_SkAlphaType); + kRGBA_8888_SkColorType, kPremul_SkAlphaType, + SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named)); return sk_sp(new Bitmap(graphicBuffer.get(), info)); }