skia2/include/images/SkDecodingImageGenerator.h
scroggo a1193e4b0e Make SkStream *not* ref counted.
SkStream is a stateful object, so it does not make sense for it to have
multiple owners. Make SkStream inherit directly from SkNoncopyable.

Update methods which previously called SkStream::ref() (e.g.
SkImageDecoder::buildTileIndex() and SkFrontBufferedStream::Create(),
which required the existing owners to call SkStream::unref()) to take
ownership of their SkStream parameters and delete when done (including
on failure).

Switch all SkAutoTUnref<SkStream>s to SkAutoTDelete<SkStream>s. In some
cases this means heap allocating streams that were previously stack
allocated.

Respect ownership rules of SkTypeface::CreateFromStream() and
SkImageDecoder::buildTileIndex().

Update the comments for exceptional methods which do not affect the
ownership of their SkStream parameters (e.g.
SkPicture::CreateFromStream() and SkTypeface::Deserialize()) to be
explicit about ownership.

Remove test_stream_life, which tested that buildTileIndex() behaved
correctly when SkStream was a ref counted object. The test does not
make sense now that it is not.

In SkPDFStream, remove the SkMemoryStream member. Instead of using it,
create a new SkMemoryStream to pass to fDataStream (which is now an
SkAutoTDelete).

Make other pdf rasterizers behave like SkPDFDocumentToBitmap.

SkPDFDocumentToBitmap delete the SkStream, so do the same in the
following pdf rasterizers:

SkPopplerRasterizePDF
SkNativeRasterizePDF
SkNoRasterizePDF

Requires a change to Android, which currently treats SkStreams as ref
counted objects.

Review URL: https://codereview.chromium.org/849103004
2015-01-21 12:09:53 -08:00

120 lines
4.5 KiB
C++

/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkDecodingImageGenerator_DEFINED
#define SkDecodingImageGenerator_DEFINED
#include "SkBitmap.h"
#include "SkImageGenerator.h"
class SkData;
class SkStreamRewindable;
/**
* An implementation of SkImageGenerator that calls into
* SkImageDecoder.
*/
namespace SkDecodingImageGenerator {
/**
* These options will be passed on to the image decoder. The
* defaults are sensible.
*
* @param fSampleSize If set to > 1, tells the decoder to return a
* smaller than original bitmap, sampling 1 pixel for
* every size pixels. e.g. if sample size is set to 3,
* then the returned bitmap will be 1/3 as wide and high,
* and will contain 1/9 as many pixels as the original.
* Note: this is a hint, and the codec may choose to
* ignore this, or only approximate the sample size.
*
* @param fDitherImage Set to true if the the decoder should try to
* dither the resulting image when decoding to a smaller
* color-space. The default is true.
*
* @param fRequestedColorType If not given, then use whichever
* config the decoder wants. Else try to use this color
* type. If the decoder won't support this color type,
* SkDecodingImageGenerator::Create will return
* NULL. kIndex_8_SkColorType is not supported.
*
* @param fRequireUnpremul If true, the decoder will attempt to
* decode without premultiplying the alpha. If it cannot,
* the pixels will be set to NULL.
*/
struct Options {
Options()
: fSampleSize(1)
, fDitherImage(true)
, fUseRequestedColorType(false)
, fRequestedColorType()
, fRequireUnpremul(false) { }
Options(int sampleSize, bool dither)
: fSampleSize(sampleSize)
, fDitherImage(dither)
, fUseRequestedColorType(false)
, fRequestedColorType()
, fRequireUnpremul(false) { }
Options(int sampleSize, bool dither, SkColorType colorType)
: fSampleSize(sampleSize)
, fDitherImage(dither)
, fUseRequestedColorType(true)
, fRequestedColorType(colorType)
, fRequireUnpremul(false) { }
Options(int sampleSize, bool dither, SkColorType colorType,
bool requireUnpremul)
: fSampleSize(sampleSize)
, fDitherImage(dither)
, fUseRequestedColorType(true)
, fRequestedColorType(colorType)
, fRequireUnpremul(requireUnpremul) { }
const int fSampleSize;
const bool fDitherImage;
const bool fUseRequestedColorType;
const SkColorType fRequestedColorType;
const bool fRequireUnpremul;
};
/**
* These two functions return a SkImageGenerator that calls into
* SkImageDecoder. They return NULL on failure.
*
* The SkData version of this function is preferred. If the stream
* has an underlying SkData (such as a SkMemoryStream) pass that in.
*
* This object, if non-NULL, takes ownership of stream and deletes stream
* upon deletion. If NULL is returned, stream is deleted immediately.
*
* @param Options (see above)
*
* @return NULL on failure, a new SkImageGenerator on success.
*/
SkImageGenerator* Create(SkStreamRewindable* stream,
const Options& opt);
/**
* @param data Contains the encoded image data that will be used by
* the SkDecodingImageGenerator. Will be ref()ed by the
* SkImageGenerator constructor and and unref()ed on deletion.
*/
SkImageGenerator* Create(SkData* data, const Options& opt);
};
// // Example of most basic use case:
//
// bool install_data(SkData* data, SkBitmap* dst) {
// return SkInstallDiscardablePixelRef(
// SkDecodingImageGenerator::Create(
// data, SkDecodingImageGenerator::Options()), dst, NULL);
// }
// bool install_stream(SkStreamRewindable* stream, SkBitmap* dst) {
// return SkInstallDiscardablePixelRef(
// SkDecodingImageGenerator::Create(
// stream, SkDecodingImageGenerator::Options()), dst, NULL);
// }
#endif // SkDecodingImageGenerator_DEFINED