Revert of default SkPixelSerializer (patchset #2 id:20001 of https://codereview.chromium.org/1507123002/ )

Reason for revert:
I was overconfident.

Original issue's description:
> default SkPixelSerializer
>
> Add SkImageEncoder::EncodeData(const SkPixmap&, ...) function.
>
> Add SkImageEncoder::CreatePixelSerializer() to return a
> PixelSerializer that calls into SkImageEncoder::EncodeData.
>
> SkImage::encode() make use of SkImageEncoder::CreatePixelSerializer.
>
> Committed: https://skia.googlesource.com/skia/+/b0bd1516bff3f5afcbfd615e805867531657811b
>
> Committed: https://skia.googlesource.com/skia/+/808ce2886d732b1055f89c8fb0f1b11b47fcb0ce

TBR=reed@google.com,scroggo@google.com
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true

Review URL: https://codereview.chromium.org/1511183002
This commit is contained in:
halcanary 2015-12-09 03:56:02 -08:00 committed by Commit bot
parent ef6a1ca531
commit a2e056773d
7 changed files with 43 additions and 70 deletions

View File

@ -281,9 +281,8 @@ void SkDebuggerGUI::saveToFile(const SkString& filename) {
SkFILEWStream file(filename.c_str()); SkFILEWStream file(filename.c_str());
SkAutoTUnref<SkPicture> copy(fDebugger.copyPicture()); SkAutoTUnref<SkPicture> copy(fDebugger.copyPicture());
SkAutoTUnref<SkPixelSerializer> serializer( sk_tool_utils::PngPixelSerializer serializer;
SkImageEncoder::CreatePixelSerializer()); copy->serialize(&file, &serializer);
copy->serialize(&file, serializer);
} }
void SkDebuggerGUI::loadFile(QListWidgetItem *item) { void SkDebuggerGUI::loadFile(QListWidgetItem *item) {

View File

@ -12,8 +12,6 @@
#include "SkTRegistry.h" #include "SkTRegistry.h"
class SkBitmap; class SkBitmap;
class SkPixelSerializer;
class SkPixmap;
class SkData; class SkData;
class SkWStream; class SkWStream;
@ -66,17 +64,11 @@ public:
Type, int quality); Type, int quality);
static SkData* EncodeData(const SkBitmap&, Type, int quality); static SkData* EncodeData(const SkBitmap&, Type, int quality);
static SkData* EncodeData(const SkPixmap&, Type, int quality);
static bool EncodeFile(const char file[], const SkBitmap&, Type, static bool EncodeFile(const char file[], const SkBitmap&, Type,
int quality); int quality);
static bool EncodeStream(SkWStream*, const SkBitmap&, Type, static bool EncodeStream(SkWStream*, const SkBitmap&, Type,
int quality); int quality);
/** Uses SkImageEncoder to serialize images that are not already
encoded as SkImageEncoder::kPNG_Type images. */
static SkPixelSerializer* CreatePixelSerializer();
protected: protected:
/** /**
* Encode bitmap 'bm' in the desired format, writing results to * Encode bitmap 'bm' in the desired format, writing results to

View File

@ -9,7 +9,6 @@
#include "SkBitmapCache.h" #include "SkBitmapCache.h"
#include "SkCanvas.h" #include "SkCanvas.h"
#include "SkData.h" #include "SkData.h"
#include "SkImageEncoder.h"
#include "SkImageGenerator.h" #include "SkImageGenerator.h"
#include "SkImagePriv.h" #include "SkImagePriv.h"
#include "SkImageShader.h" #include "SkImageShader.h"
@ -179,14 +178,32 @@ SkData* SkImage::encode(SkImageEncoder::Type type, int quality) const {
return nullptr; return nullptr;
} }
SkData* SkImage::encode(SkPixelSerializer* serializer) const { namespace {
SkAutoTUnref<SkPixelSerializer> defaultSerializer;
SkPixelSerializer* effectiveSerializer = serializer; class DefaultSerializer : public SkPixelSerializer {
if (!effectiveSerializer) { protected:
defaultSerializer.reset(SkImageEncoder::CreatePixelSerializer()); bool onUseEncodedData(const void *data, size_t len) override {
SkASSERT(defaultSerializer.get()); return true;
effectiveSerializer = defaultSerializer.get();
} }
SkData* onEncode(const SkPixmap& pixmap) override {
SkBitmap bm;
if (!bm.installPixels(pixmap.info(),
const_cast<void*>(pixmap.addr()),
pixmap.rowBytes(),
pixmap.ctable(),
nullptr, nullptr)) {
return nullptr;
}
return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
}
};
} // anonymous namespace
SkData* SkImage::encode(SkPixelSerializer* serializer) const {
DefaultSerializer defaultSerializer;
SkPixelSerializer* effectiveSerializer = serializer ? serializer : &defaultSerializer;
SkAutoTUnref<SkData> encoded(this->refEncoded()); SkAutoTUnref<SkData> encoded(this->refEncoded());
if (encoded && effectiveSerializer->useEncodedData(encoded->data(), encoded->size())) { if (encoded && effectiveSerializer->useEncodedData(encoded->data(), encoded->size())) {
return encoded.detach(); return encoded.detach();

View File

@ -7,8 +7,6 @@
#include "SkImageEncoder.h" #include "SkImageEncoder.h"
#include "SkBitmap.h" #include "SkBitmap.h"
#include "SkPixelSerializer.h"
#include "SkPixmap.h"
#include "SkStream.h" #include "SkStream.h"
#include "SkTemplates.h" #include "SkTemplates.h"
@ -59,33 +57,6 @@ SkData* SkImageEncoder::EncodeData(const SkImageInfo& info, const void* pixels,
if (!bm.installPixels(info, const_cast<void*>(pixels), rowBytes)) { if (!bm.installPixels(info, const_cast<void*>(pixels), rowBytes)) {
return nullptr; return nullptr;
} }
bm.setImmutable(); SkAutoTDelete<SkImageEncoder> enc(SkImageEncoder::Create(t));
return SkImageEncoder::EncodeData(bm, t, quality); return enc.get() ? enc.get()->encodeData(bm, quality) : nullptr;
}
SkData* SkImageEncoder::EncodeData(const SkPixmap& pixmap,
Type t, int quality) {
SkBitmap bm;
if (!bm.installPixels(pixmap.info(),
const_cast<void*>(pixmap.addr()),
pixmap.rowBytes(),
pixmap.ctable(), nullptr, nullptr)) {
return nullptr;
}
bm.setImmutable();
return SkImageEncoder::EncodeData(bm, t, quality);
}
namespace {
class ImageEncoderPixelSerializer final : public SkPixelSerializer {
protected:
bool onUseEncodedData(const void*, size_t) override { return true; }
SkData* onEncode(const SkPixmap& pmap) override {
return SkImageEncoder::EncodeData(pmap, SkImageEncoder::kPNG_Type, 100);
}
};
} // namespace
SkPixelSerializer* SkImageEncoder::CreatePixelSerializer() {
return new ImageEncoderPixelSerializer;
} }

View File

@ -108,10 +108,6 @@ SkData* SkImageEncoder::EncodeData(const SkImageInfo&, const void* pixels, size_
return nullptr; return nullptr;
} }
static SkData* SkImageEncoder::EncodeData(const SkPixmap&, Type, int) {
return nullptr;
}
bool SkImageEncoder::encodeStream(SkWStream*, const SkBitmap&, int) { bool SkImageEncoder::encodeStream(SkWStream*, const SkBitmap&, int) {
return false; return false;
} }
@ -123,17 +119,4 @@ SkData* SkImageEncoder::encodeData(const SkBitmap&, int) {
bool SkImageEncoder::encodeFile(const char file[], const SkBitmap& bm, int quality) { bool SkImageEncoder::encodeFile(const char file[], const SkBitmap& bm, int quality) {
return false; return false;
} }
namespace {
class ImageEncoderPixelSerializer final : public SkPixelSerializer {
protected:
bool onUseEncodedData(const void*, size_t) override { return true; }
SkData* onEncode(const SkPixmap& pmap) override { return nullptr; }
};
} // namespace
SkPixelSerializer* SkImageEncoder::CreatePixelSerializer() {
return new ImageEncoderPixelSerializer;
}
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////

View File

@ -851,9 +851,8 @@ static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
SkAutoTUnref<SkPicture> picture(recorder.endRecording()); SkAutoTUnref<SkPicture> picture(recorder.endRecording());
SkDynamicMemoryWStream wStream; SkDynamicMemoryWStream wStream;
SkAutoTUnref<SkPixelSerializer> serializer( sk_tool_utils::PngPixelSerializer serializer;
SkImageEncoder::CreatePixelSerializer()); picture->serialize(&wStream, &serializer);
picture->serialize(&wStream, serializer);
return wStream.copyToData(); return wStream.copyToData();
} }

View File

@ -114,6 +114,18 @@ namespace sk_tool_utils {
SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
int textSize, const char* str); int textSize, const char* str);
// Encodes to PNG, unless there is already encoded data, in which case that gets
// used.
class PngPixelSerializer : public SkPixelSerializer {
public:
bool onUseEncodedData(const void*, size_t) override { return true; }
SkData* onEncodePixels(const SkImageInfo& info, const void* pixels,
size_t rowBytes) override {
return SkImageEncoder::EncodeData(info, pixels, rowBytes,
SkImageEncoder::kPNG_Type, 100);
}
};
// A helper for inserting a drawtext call into a SkTextBlobBuilder // A helper for inserting a drawtext call into a SkTextBlobBuilder
void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint, void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint,
SkScalar x, SkScalar y); SkScalar x, SkScalar y);