Skip to content

Commit 77fa5d9

Browse files
jreckgitbuildkicker
authored andcommitted
Ensure munmap matches mmap
Bug: 31350622 Change-Id: I6d3f9faec32d54360caa6706d17405e20b50966c (cherry picked from commit aa394dd)
1 parent 26f6eb7 commit 77fa5d9

4 files changed

Lines changed: 11 additions & 10 deletions

File tree

core/jni/android/graphics/Bitmap.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ Bitmap::Bitmap(void* address, void* context, FreeFunc freeFunc,
150150
mPixelRef->unref();
151151
}
152152

153-
Bitmap::Bitmap(void* address, int fd,
153+
Bitmap::Bitmap(void* address, int fd, size_t mappedSize,
154154
const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
155155
: mPixelStorageType(PixelStorageType::Ashmem) {
156156
mPixelStorage.ashmem.address = address;
157157
mPixelStorage.ashmem.fd = fd;
158-
mPixelStorage.ashmem.size = ashmem_get_size_region(fd);
158+
mPixelStorage.ashmem.size = mappedSize;
159159
mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
160160
// Note: this will trigger a call to onStrongRefDestroyed(), but
161161
// we want the pixel ref to have a ref count of 0 at this point
@@ -1014,7 +1014,7 @@ static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
10141014

10151015
// Map the pixels in place and take ownership of the ashmem region.
10161016
nativeBitmap = GraphicsJNI::mapAshmemPixelRef(env, bitmap.get(),
1017-
ctable, dupFd, const_cast<void*>(blob.data()), !isMutable);
1017+
ctable, dupFd, const_cast<void*>(blob.data()), size, !isMutable);
10181018
SkSafeUnref(ctable);
10191019
if (!nativeBitmap) {
10201020
close(dupFd);

core/jni/android/graphics/Bitmap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class Bitmap {
5151
const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable);
5252
Bitmap(void* address, void* context, FreeFunc freeFunc,
5353
const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable);
54-
Bitmap(void* address, int fd, const SkImageInfo& info, size_t rowBytes,
55-
SkColorTable* ctable);
54+
Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info,
55+
size_t rowBytes, SkColorTable* ctable);
5656

5757
const SkImageInfo& info() const;
5858

core/jni/android/graphics/Graphics.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ android::Bitmap* GraphicsJNI::allocateAshmemPixelRef(JNIEnv* env, SkBitmap* bitm
613613
return nullptr;
614614
}
615615

616-
android::Bitmap* wrapper = new android::Bitmap(addr, fd, info, rowBytes, ctable);
616+
android::Bitmap* wrapper = new android::Bitmap(addr, fd, size, info, rowBytes, ctable);
617617
wrapper->getSkBitmap(bitmap);
618618
// since we're already allocated, we lockPixels right away
619619
// HeapAllocator behaves this way too
@@ -623,7 +623,7 @@ android::Bitmap* GraphicsJNI::allocateAshmemPixelRef(JNIEnv* env, SkBitmap* bitm
623623
}
624624

625625
android::Bitmap* GraphicsJNI::mapAshmemPixelRef(JNIEnv* env, SkBitmap* bitmap,
626-
SkColorTable* ctable, int fd, void* addr, bool readOnly) {
626+
SkColorTable* ctable, int fd, void* addr, size_t size, bool readOnly) {
627627
const SkImageInfo& info = bitmap->info();
628628
if (info.fColorType == kUnknown_SkColorType) {
629629
doThrowIAE(env, "unknown bitmap configuration");
@@ -633,7 +633,8 @@ android::Bitmap* GraphicsJNI::mapAshmemPixelRef(JNIEnv* env, SkBitmap* bitmap,
633633
if (!addr) {
634634
// Map existing ashmem region if not already mapped.
635635
int flags = readOnly ? (PROT_READ) : (PROT_READ | PROT_WRITE);
636-
addr = mmap(NULL, ashmem_get_size_region(fd), flags, MAP_SHARED, fd, 0);
636+
size = ashmem_get_size_region(fd);
637+
addr = mmap(NULL, size, flags, MAP_SHARED, fd, 0);
637638
if (addr == MAP_FAILED) {
638639
return nullptr;
639640
}
@@ -643,7 +644,7 @@ android::Bitmap* GraphicsJNI::mapAshmemPixelRef(JNIEnv* env, SkBitmap* bitmap,
643644
// attempting to compute our own.
644645
const size_t rowBytes = bitmap->rowBytes();
645646

646-
android::Bitmap* wrapper = new android::Bitmap(addr, fd, info, rowBytes, ctable);
647+
android::Bitmap* wrapper = new android::Bitmap(addr, fd, size, info, rowBytes, ctable);
647648
wrapper->getSkBitmap(bitmap);
648649
if (readOnly) {
649650
bitmap->pixelRef()->setImmutable();

core/jni/android/graphics/GraphicsJNI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class GraphicsJNI {
9999
SkColorTable* ctable);
100100

101101
static android::Bitmap* mapAshmemPixelRef(JNIEnv* env, SkBitmap* bitmap,
102-
SkColorTable* ctable, int fd, void* addr, bool readOnly);
102+
SkColorTable* ctable, int fd, void* addr, size_t size, bool readOnly);
103103

104104
/**
105105
* Given a bitmap we natively allocate a memory block to store the contents

0 commit comments

Comments
 (0)