SkStream is no longer a ref counted object.
With https://codereview.chromium.org/849103004/, SkStream is no longer
ref counted. Change callers that currently unref() SkStreams to one of
either:
- delete the stream
- pass ownership of the stream
screencap.cpp:
Call EncodeData directly, bypassing SkDynamicMemoryWStream and SkBitmap.
Utils.cpp:
Write directly to an SkData, and then use that to construct a new
SkStream.
Cherry-pick of a change that originally landed in master-skia and is
dependent on a skia merge (ag/655422).
Change-Id: Idc99ad7d5a70c893dc012d59915216f301ab3c9d
diff --git a/cmds/screencap/screencap.cpp b/cmds/screencap/screencap.cpp
index b0aee7b..dbc35af 100644
--- a/cmds/screencap/screencap.cpp
+++ b/cmds/screencap/screencap.cpp
@@ -36,9 +36,7 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <SkImageEncoder.h>
-#include <SkBitmap.h>
#include <SkData.h>
-#include <SkStream.h>
#pragma GCC diagnostic pop
using namespace android;
@@ -198,14 +196,11 @@
if (png) {
const SkImageInfo info = SkImageInfo::Make(w, h, flinger2skia(f),
kPremul_SkAlphaType);
- SkBitmap b;
- b.installPixels(info, const_cast<void*>(base), s*bytesPerPixel(f));
- SkDynamicMemoryWStream stream;
- SkImageEncoder::EncodeStream(&stream, b,
- SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality);
- SkData* streamData = stream.copyToData();
- write(fd, streamData->data(), streamData->size());
- streamData->unref();
+ SkAutoTUnref<SkData> data(SkImageEncoder::EncodeData(info, base, s*bytesPerPixel(f),
+ SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality));
+ if (data.get()) {
+ write(fd, data->data(), data->size());
+ }
if (fn != NULL) {
notifyMediaScanner(fn);
}
diff --git a/core/jni/android/graphics/BitmapFactory.cpp b/core/jni/android/graphics/BitmapFactory.cpp
index 6a50b52..47090fb 100644
--- a/core/jni/android/graphics/BitmapFactory.cpp
+++ b/core/jni/android/graphics/BitmapFactory.cpp
@@ -461,11 +461,11 @@
jobject padding, jobject options) {
jobject bitmap = NULL;
- SkAutoTUnref<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage));
+ SkAutoTDelete<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage));
if (stream.get()) {
- SkAutoTUnref<SkStreamRewindable> bufferedStream(
- SkFrontBufferedStream::Create(stream, BYTES_TO_BUFFER));
+ SkAutoTDelete<SkStreamRewindable> bufferedStream(
+ SkFrontBufferedStream::Create(stream.detach(), BYTES_TO_BUFFER));
SkASSERT(bufferedStream.get() != NULL);
bitmap = doDecode(env, bufferedStream, padding, options);
}
@@ -504,13 +504,13 @@
return nullObjectReturn("Could not open file");
}
- SkAutoTUnref<SkFILEStream> fileStream(new SkFILEStream(file,
- SkFILEStream::kCallerPasses_Ownership));
+ SkAutoTDelete<SkFILEStream> fileStream(new SkFILEStream(file,
+ SkFILEStream::kCallerPasses_Ownership));
// Use a buffered stream. Although an SkFILEStream can be rewound, this
// ensures that SkImageDecoder::Factory never rewinds beyond the
// current position of the file descriptor.
- SkAutoTUnref<SkStreamRewindable> stream(SkFrontBufferedStream::Create(fileStream,
+ SkAutoTDelete<SkStreamRewindable> stream(SkFrontBufferedStream::Create(fileStream.detach(),
BYTES_TO_BUFFER));
return doDecode(env, stream, padding, bitmapFactoryOptions);
@@ -522,7 +522,7 @@
Asset* asset = reinterpret_cast<Asset*>(native_asset);
// since we know we'll be done with the asset when we return, we can
// just use a simple wrapper
- SkAutoTUnref<SkStreamRewindable> stream(new AssetStreamAdaptor(asset));
+ SkAutoTDelete<SkStreamRewindable> stream(new AssetStreamAdaptor(asset));
return doDecode(env, stream, padding, options);
}
@@ -530,8 +530,7 @@
jint offset, jint length, jobject options) {
AutoJavaByteArray ar(env, byteArray);
- SkMemoryStream* stream = new SkMemoryStream(ar.ptr() + offset, length, false);
- SkAutoUnref aur(stream);
+ SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(ar.ptr() + offset, length, false));
return doDecode(env, stream, NULL, options);
}
diff --git a/core/jni/android/graphics/BitmapRegionDecoder.cpp b/core/jni/android/graphics/BitmapRegionDecoder.cpp
index 04afe3e..3525d07 100644
--- a/core/jni/android/graphics/BitmapRegionDecoder.cpp
+++ b/core/jni/android/graphics/BitmapRegionDecoder.cpp
@@ -69,10 +69,13 @@
int fHeight;
};
+// Takes ownership of the SkStreamRewindable. For consistency, deletes stream even
+// when returning null.
static jobject createBitmapRegionDecoder(JNIEnv* env, SkStreamRewindable* stream) {
SkImageDecoder* decoder = SkImageDecoder::Factory(stream);
int width, height;
if (NULL == decoder) {
+ SkDELETE(stream);
doThrowIOE(env, "Image format not supported");
return nullObjectReturn("SkImageDecoder::Factory returned null");
}
@@ -81,6 +84,7 @@
decoder->setAllocator(javaAllocator);
javaAllocator->unref();
+ // This call passes ownership of stream to the decoder, or deletes on failure.
if (!decoder->buildTileIndex(stream, &width, &height)) {
char msg[100];
snprintf(msg, sizeof(msg), "Image failed to decode using %s decoder",
@@ -103,8 +107,8 @@
AutoJavaByteArray ar(env, byteArray);
SkMemoryStream* stream = new SkMemoryStream(ar.ptr() + offset, length, true);
+ // the decoder owns the stream.
jobject brd = createBitmapRegionDecoder(env, stream);
- SkSafeUnref(stream); // the decoder now holds a reference
return brd;
}
@@ -123,8 +127,8 @@
SkAutoTUnref<SkData> data(SkData::NewFromFD(descriptor));
SkMemoryStream* stream = new SkMemoryStream(data);
+ // the decoder owns the stream.
jobject brd = createBitmapRegionDecoder(env, stream);
- SkSafeUnref(stream); // the decoder now holds a reference
return brd;
}
@@ -137,8 +141,8 @@
SkStreamRewindable* stream = CopyJavaInputStream(env, is, storage);
if (stream) {
+ // the decoder owns the stream.
brd = createBitmapRegionDecoder(env, stream);
- stream->unref(); // the decoder now holds a reference
}
return brd;
}
@@ -147,13 +151,13 @@
jlong native_asset, // Asset
jboolean isShareable) {
Asset* asset = reinterpret_cast<Asset*>(native_asset);
- SkAutoTUnref<SkMemoryStream> stream(CopyAssetToStream(asset));
- if (NULL == stream.get()) {
+ SkMemoryStream* stream = CopyAssetToStream(asset);
+ if (NULL == stream) {
return NULL;
}
- jobject brd = createBitmapRegionDecoder(env, stream.get());
- // The decoder now holds a reference to stream.
+ // the decoder owns the stream.
+ jobject brd = createBitmapRegionDecoder(env, stream);
return brd;
}
diff --git a/core/jni/android/graphics/CreateJavaOutputStreamAdaptor.cpp b/core/jni/android/graphics/CreateJavaOutputStreamAdaptor.cpp
index 7937941..26523f8 100644
--- a/core/jni/android/graphics/CreateJavaOutputStreamAdaptor.cpp
+++ b/core/jni/android/graphics/CreateJavaOutputStreamAdaptor.cpp
@@ -164,7 +164,7 @@
SkStreamRewindable* CopyJavaInputStream(JNIEnv* env, jobject stream,
jbyteArray storage) {
- SkAutoTUnref<SkStream> adaptor(CreateJavaInputStreamAdaptor(env, stream, storage));
+ SkAutoTDelete<SkStream> adaptor(CreateJavaInputStreamAdaptor(env, stream, storage));
if (NULL == adaptor.get()) {
return NULL;
}
diff --git a/core/jni/android/graphics/FontFamily.cpp b/core/jni/android/graphics/FontFamily.cpp
index 3e6327b..d28669a 100644
--- a/core/jni/android/graphics/FontFamily.cpp
+++ b/core/jni/android/graphics/FontFamily.cpp
@@ -111,7 +111,8 @@
}
SkAutoTUnref<SkData> data(SkData::NewWithProc(buf, asset->getLength(), releaseAsset, asset));
- SkAutoTUnref<SkMemoryStream> stream(new SkMemoryStream(data));
+ SkMemoryStream* stream = new SkMemoryStream(data);
+ // CreateFromStream takes ownership of stream.
SkTypeface* face = SkTypeface::CreateFromStream(stream);
if (face == NULL) {
ALOGE("addFontFromAsset failed to create font %s", str.c_str());
diff --git a/core/jni/android/graphics/Movie.cpp b/core/jni/android/graphics/Movie.cpp
index 134551d..5c7acf8 100644
--- a/core/jni/android/graphics/Movie.cpp
+++ b/core/jni/android/graphics/Movie.cpp
@@ -84,7 +84,7 @@
static jobject movie_decodeAsset(JNIEnv* env, jobject clazz, jlong native_asset) {
android::Asset* asset = reinterpret_cast<android::Asset*>(native_asset);
if (asset == NULL) return NULL;
- SkAutoTUnref<SkStreamRewindable> stream(new android::AssetStreamAdaptor(asset));
+ SkAutoTDelete<SkStreamRewindable> stream(new android::AssetStreamAdaptor(asset));
SkMovie* moov = SkMovie::DecodeStream(stream.get());
return create_jmovie(env, moov);
}
@@ -104,11 +104,11 @@
// trying to determine the stream's format. The only decoder for movies is GIF, which
// will only read 6.
// FIXME: Get this number from SkImageDecoder
- SkAutoTUnref<SkStreamRewindable> bufferedStream(SkFrontBufferedStream::Create(strm, 6));
+ // bufferedStream takes ownership of strm
+ SkAutoTDelete<SkStreamRewindable> bufferedStream(SkFrontBufferedStream::Create(strm, 6));
SkASSERT(bufferedStream.get() != NULL);
SkMovie* moov = SkMovie::DecodeStream(bufferedStream);
- strm->unref();
return create_jmovie(env, moov);
}
diff --git a/core/jni/android/graphics/Typeface.cpp b/core/jni/android/graphics/Typeface.cpp
index 808ae2c..b092e44 100644
--- a/core/jni/android/graphics/Typeface.cpp
+++ b/core/jni/android/graphics/Typeface.cpp
@@ -19,7 +19,6 @@
#include "GraphicsJNI.h"
#include <ScopedPrimitiveArray.h>
-#include "SkStream.h"
#include "SkTypeface.h"
#include "TypefaceImpl.h"
#include <android_runtime/android_util_AssetManager.h>
diff --git a/core/jni/android/graphics/TypefaceImpl.cpp b/core/jni/android/graphics/TypefaceImpl.cpp
index 7afbeb2..da56290 100644
--- a/core/jni/android/graphics/TypefaceImpl.cpp
+++ b/core/jni/android/graphics/TypefaceImpl.cpp
@@ -24,7 +24,6 @@
#include "jni.h" // for jlong, remove when being passed proper type
-#include "SkStream.h"
#include "SkTypeface.h"
#include <vector>
diff --git a/core/jni/android/graphics/Utils.cpp b/core/jni/android/graphics/Utils.cpp
index 3c471bd..4f9ce8b 100644
--- a/core/jni/android/graphics/Utils.cpp
+++ b/core/jni/android/graphics/Utils.cpp
@@ -16,6 +16,7 @@
#include "Utils.h"
#include "SkUtils.h"
+#include "SkData.h"
using namespace android;
@@ -87,27 +88,26 @@
return NULL;
}
- off64_t size = asset->seek(0, SEEK_SET);
- if ((off64_t)-1 == size) {
+ const off64_t seekReturnVal = asset->seek(0, SEEK_SET);
+ if ((off64_t)-1 == seekReturnVal) {
SkDebugf("---- copyAsset: asset rewind failed\n");
return NULL;
}
- size = asset->getLength();
+ const off64_t size = asset->getLength();
if (size <= 0) {
SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
return NULL;
}
- SkMemoryStream* stream = new SkMemoryStream(size);
- void* data = const_cast<void*>(stream->getMemoryBase());
- off64_t len = asset->read(data, size);
+ SkAutoTUnref<SkData> data(SkData::NewUninitialized(size));
+ const off64_t len = asset->read(data->writable_data(), size);
if (len != size) {
SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
- delete stream;
- stream = NULL;
+ return NULL;
}
- return stream;
+
+ return new SkMemoryStream(data);
}
jobject android::nullObjectReturn(const char msg[]) {