Commit 05683872 authored by halcanary's avatar halcanary Committed by Commit bot

Implement SkEncodeImage()

This change replaces the implementation in SkImageEncoder_none.cpp with
an implementation that allows the layer above to define an image
encoding function.

Background:  Skia's (experimental) XPS backend needs a PNG encoder.
Skia for Chromium does not compile its PNG encoder, but there is one
available in Chrome's gfx component.  To make layering work right,
I allow the layer above skia (gfx) to define an encoding function
that can be installed into skia.

TODO: initialize the function pointer in printing process.

Also: Revert "SK_SUPPORT_LEGACY_IMAGE_ENCODER_CLASS"
This reverts commit 1430e85b.

BUG=chromium:616763

Review-Url: https://codereview.chromium.org/2527833002
Cr-Commit-Position: refs/heads/master@{#441395}
parent 835d5509
...@@ -207,6 +207,8 @@ component("skia") { ...@@ -207,6 +207,8 @@ component("skia") {
"ext/opacity_filter_canvas.cc", "ext/opacity_filter_canvas.cc",
"ext/platform_device.cc", "ext/platform_device.cc",
"ext/recursive_gaussian_convolution.cc", "ext/recursive_gaussian_convolution.cc",
"ext/skia_encode_image.cc",
"ext/skia_encode_image.h",
"ext/skia_histogram.cc", "ext/skia_histogram.cc",
"ext/skia_memory_dump_provider.cc", "ext/skia_memory_dump_provider.cc",
"ext/skia_trace_memory_dump_impl.cc", "ext/skia_trace_memory_dump_impl.cc",
...@@ -247,7 +249,6 @@ component("skia") { ...@@ -247,7 +249,6 @@ component("skia") {
"//third_party/skia/src/ports/SkFontMgr_android_parser.cpp", "//third_party/skia/src/ports/SkFontMgr_android_parser.cpp",
"//third_party/skia/src/ports/SkFontMgr_win_dw.cpp", "//third_party/skia/src/ports/SkFontMgr_win_dw.cpp",
"//third_party/skia/src/ports/SkGlobalInitialization_default.cpp", "//third_party/skia/src/ports/SkGlobalInitialization_default.cpp",
"//third_party/skia/src/ports/SkImageEncoder_none.cpp",
"//third_party/skia/src/ports/SkImageGenerator_none.cpp", "//third_party/skia/src/ports/SkImageGenerator_none.cpp",
"//third_party/skia/src/ports/SkOSFile_posix.cpp", "//third_party/skia/src/ports/SkOSFile_posix.cpp",
"//third_party/skia/src/ports/SkOSFile_stdio.cpp", "//third_party/skia/src/ports/SkOSFile_stdio.cpp",
......
...@@ -220,10 +220,6 @@ SK_API void SkDebugf_FileLine(const char* file, int line, bool fatal, ...@@ -220,10 +220,6 @@ SK_API void SkDebugf_FileLine(const char* file, int line, bool fatal,
# define SK_DISABLE_COLOR_XFORM_PIPELINE # define SK_DISABLE_COLOR_XFORM_PIPELINE
#endif #endif
#ifndef SK_SUPPORT_LEGACY_IMAGE_ENCODER_CLASS
# define SK_SUPPORT_LEGACY_IMAGE_ENCODER_CLASS
#endif
#ifndef SK_SUPPORT_LEGACY_BITMAP_SETPIXELREF #ifndef SK_SUPPORT_LEGACY_BITMAP_SETPIXELREF
# define SK_SUPPORT_LEGACY_BITMAP_SETPIXELREF # define SK_SUPPORT_LEGACY_BITMAP_SETPIXELREF
#endif #endif
......
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "skia/ext/skia_encode_image.h"
#include "third_party/skia/include/core/SkImageEncoder.h"
static skia::ImageEncoderFunction g_image_encoder = nullptr;
void skia::SetImageEncoder(skia::ImageEncoderFunction image_encoder) {
g_image_encoder = image_encoder;
}
bool SkEncodeImage(SkWStream* stream,
const SkPixmap& pixmap,
SkEncodedImageFormat format,
int quality) {
skia::ImageEncoderFunction encoder = g_image_encoder;
return encoder && encoder(stream, pixmap, format, quality);
}
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SKIA_EXT_SKIA_ENCODE_IMAGE_H
#define SKIA_EXT_SKIA_ENCODE_IMAGE_H
#include "third_party/skia/include/core/SkEncodedImageFormat.h"
#include "third_party/skia/include/core/SkTypes.h"
class SkPixmap;
class SkWStream;
namespace skia {
using ImageEncoderFunction = bool (*)(SkWStream*,
const SkPixmap&,
SkEncodedImageFormat,
int quality);
SK_API void SetImageEncoder(ImageEncoderFunction);
} // namespace skia
#endif // SKIA_EXT_SKIA_ENCODE_IMAGE_H
...@@ -57,6 +57,8 @@ component("gfx") { ...@@ -57,6 +57,8 @@ component("gfx") {
"codec/jpeg_codec.h", "codec/jpeg_codec.h",
"codec/png_codec.cc", "codec/png_codec.cc",
"codec/png_codec.h", "codec/png_codec.h",
"codec/skia_image_encoder_adapter.cc",
"codec/skia_image_encoder_adapter.h",
"color_analysis.cc", "color_analysis.cc",
"color_analysis.h", "color_analysis.h",
"color_palette.h", "color_palette.h",
......
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/codec/skia_image_encoder_adapter.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkStream.h"
#include "ui/gfx/codec/jpeg_codec.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/geometry/size.h"
bool gfx::EncodeSkiaImage(SkWStream* dst,
const SkPixmap& pixmap,
SkEncodedImageFormat format,
int quality) {
if (kN32_SkColorType != pixmap.colorType() ||
(kPremul_SkAlphaType != pixmap.alphaType() &&
kOpaque_SkAlphaType != pixmap.alphaType())) {
return false;
}
std::vector<unsigned char> buffer;
switch (format) {
case SkEncodedImageFormat::kPNG:
return gfx::PNGCodec::Encode(
reinterpret_cast<const unsigned char*>(pixmap.addr()),
gfx::PNGCodec::FORMAT_SkBitmap,
gfx::Size(pixmap.width(), pixmap.height()),
static_cast<int>(pixmap.rowBytes()), false,
std::vector<gfx::PNGCodec::Comment>(), &buffer) &&
dst->write(buffer.data(), buffer.size());
case SkEncodedImageFormat::kJPEG:
return gfx::JPEGCodec::Encode(
reinterpret_cast<const unsigned char*>(pixmap.addr()),
gfx::JPEGCodec::FORMAT_SkBitmap, pixmap.width(),
pixmap.height(), static_cast<int>(pixmap.rowBytes()), quality,
&buffer) &&
dst->write(buffer.data(), buffer.size());
default:
return false;
}
}
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_GFX_CODEC_SKIA_IMAGE_ENCODER_ADAPTER_H
#define UI_GFX_CODEC_SKIA_IMAGE_ENCODER_ADAPTER_H
#include "third_party/skia/include/core/SkEncodedImageFormat.h"
#include "ui/gfx/gfx_export.h"
class SkWStream;
class SkPixmap;
namespace gfx {
// Matches signature of Skia's SkEncodeImage, but makes use of Chromium's
// encoders.
GFX_EXPORT bool EncodeSkiaImage(SkWStream* dst,
const SkPixmap& pixmap,
SkEncodedImageFormat format,
int quality);
} // namespace gfx
#endif // UI_GFX_CODEC_SKIA_IMAGE_ENCODER_ADAPTER_H
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment