2016-12-22 15:52:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/codec/SkCodec.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColorSpace.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkData.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPicture.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPictureRecorder.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2020-07-16 14:58:58 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/third_party/skcms/skcms.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/Resources.h"
|
2016-12-22 15:52:25 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
2016-12-22 15:52:25 +00:00
|
|
|
static const int kWidth = 64;
|
|
|
|
static const int kHeight = 64;
|
|
|
|
|
2017-03-20 18:21:18 +00:00
|
|
|
static sk_sp<SkImage> make_raster_image(SkColorType colorType) {
|
2017-12-08 15:21:31 +00:00
|
|
|
std::unique_ptr<SkStream> stream(GetResourceAsStream("images/google_chrome.ico"));
|
2017-07-23 19:30:02 +00:00
|
|
|
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromStream(std::move(stream));
|
2018-11-26 20:34:12 +00:00
|
|
|
if (!codec) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-12-22 15:52:25 +00:00
|
|
|
|
|
|
|
SkImageInfo info = codec->getInfo().makeWH(kWidth, kHeight)
|
|
|
|
.makeColorType(colorType)
|
2018-08-27 19:16:02 +00:00
|
|
|
.makeAlphaType(kPremul_SkAlphaType);
|
2021-01-25 20:36:09 +00:00
|
|
|
return std::get<0>(codec->getImage(info));
|
2016-12-22 15:52:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 22:41:53 +00:00
|
|
|
static sk_sp<SkImage> make_codec_image() {
|
2017-12-08 15:21:31 +00:00
|
|
|
sk_sp<SkData> encoded = GetResourceAsData("images/randPixels.png");
|
2017-01-10 22:41:53 +00:00
|
|
|
return SkImage::MakeFromEncoded(encoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_contents(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(20);
|
|
|
|
paint.setColor(0xFF800000);
|
|
|
|
canvas->drawCircle(40, 40, 35, paint);
|
|
|
|
paint.setColor(0xFF008000);
|
|
|
|
canvas->drawCircle(50, 50, 35, paint);
|
|
|
|
paint.setColor(0xFF000080);
|
|
|
|
canvas->drawCircle(60, 60, 35, paint);
|
|
|
|
}
|
|
|
|
|
2017-02-14 18:50:43 +00:00
|
|
|
static sk_sp<SkImage> make_picture_image() {
|
2017-01-10 22:41:53 +00:00
|
|
|
SkPictureRecorder recorder;
|
|
|
|
draw_contents(recorder.beginRecording(SkRect::MakeIWH(kWidth, kHeight)));
|
|
|
|
return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
|
|
|
|
SkISize::Make(kWidth, kHeight), nullptr, nullptr,
|
|
|
|
SkImage::BitDepth::kU8,
|
2017-02-07 18:56:11 +00:00
|
|
|
SkColorSpace::MakeSRGB());
|
2017-01-10 22:41:53 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 20:19:51 +00:00
|
|
|
static sk_sp<SkColorSpace> make_parametric_transfer_fn(const SkColorSpacePrimaries& primaries) {
|
2019-01-04 22:03:00 +00:00
|
|
|
skcms_Matrix3x3 toXYZD50;
|
2016-12-22 15:52:25 +00:00
|
|
|
SkAssertResult(primaries.toXYZD50(&toXYZD50));
|
2019-01-04 22:03:00 +00:00
|
|
|
skcms_TransferFunction fn = { 1.8f, 1.f, 0.f, 0.f, 0.f, 0.f, 0.f };
|
2017-05-02 20:19:51 +00:00
|
|
|
return SkColorSpace::MakeRGB(fn, toXYZD50);
|
2016-12-22 15:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkColorSpace> make_wide_gamut() {
|
|
|
|
// ProPhoto
|
|
|
|
SkColorSpacePrimaries primaries;
|
|
|
|
primaries.fRX = 0.7347f;
|
|
|
|
primaries.fRY = 0.2653f;
|
|
|
|
primaries.fGX = 0.1596f;
|
|
|
|
primaries.fGY = 0.8404f;
|
|
|
|
primaries.fBX = 0.0366f;
|
|
|
|
primaries.fBY = 0.0001f;
|
|
|
|
primaries.fWX = 0.34567f;
|
|
|
|
primaries.fWY = 0.35850f;
|
2017-05-02 20:19:51 +00:00
|
|
|
return make_parametric_transfer_fn(primaries);
|
2016-12-22 15:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkColorSpace> make_small_gamut() {
|
|
|
|
SkColorSpacePrimaries primaries;
|
|
|
|
primaries.fRX = 0.50f;
|
|
|
|
primaries.fRY = 0.33f;
|
|
|
|
primaries.fGX = 0.30f;
|
|
|
|
primaries.fGY = 0.50f;
|
|
|
|
primaries.fBX = 0.25f;
|
|
|
|
primaries.fBY = 0.16f;
|
|
|
|
primaries.fWX = 0.3127f;
|
|
|
|
primaries.fWY = 0.3290f;
|
2017-05-02 20:19:51 +00:00
|
|
|
return make_parametric_transfer_fn(primaries);
|
2016-12-22 15:52:25 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 16:44:07 +00:00
|
|
|
static void draw_image(GrDirectContext* dContext, SkCanvas* canvas, SkImage* image,
|
|
|
|
SkColorType dstColorType, SkAlphaType dstAlphaType,
|
|
|
|
sk_sp<SkColorSpace> dstColorSpace, SkImage::CachingHint hint) {
|
2016-12-22 15:52:25 +00:00
|
|
|
size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(dstColorType);
|
|
|
|
sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
|
|
|
|
SkImageInfo dstInfo = SkImageInfo::Make(image->width(), image->height(), dstColorType,
|
|
|
|
dstAlphaType, dstColorSpace);
|
2020-08-27 16:44:07 +00:00
|
|
|
if (!image->readPixels(dContext, dstInfo, data->writable_data(), rowBytes, 0, 0, hint)) {
|
2017-03-20 20:54:28 +00:00
|
|
|
memset(data->writable_data(), 0, rowBytes * image->height());
|
|
|
|
}
|
2016-12-22 15:52:25 +00:00
|
|
|
|
|
|
|
// Now that we have called readPixels(), dump the raw pixels into an srgb image.
|
2018-08-27 19:16:02 +00:00
|
|
|
sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
|
2016-12-22 15:52:25 +00:00
|
|
|
sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorSpace(srgb), data, rowBytes);
|
2021-01-23 23:10:39 +00:00
|
|
|
canvas->drawImage(raw.get(), 0.0f, 0.0f);
|
2016-12-22 15:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ReadPixelsGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
ReadPixelsGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("readpixels");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
2017-03-20 18:21:18 +00:00
|
|
|
return SkISize::Make(6 * kWidth, 9 * kHeight);
|
2016-12-22 15:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
const SkAlphaType alphaTypes[] = {
|
|
|
|
kUnpremul_SkAlphaType,
|
|
|
|
kPremul_SkAlphaType,
|
|
|
|
};
|
|
|
|
const SkColorType colorTypes[] = {
|
|
|
|
kRGBA_8888_SkColorType,
|
|
|
|
kBGRA_8888_SkColorType,
|
|
|
|
kRGBA_F16_SkColorType,
|
|
|
|
};
|
|
|
|
const sk_sp<SkColorSpace> colorSpaces[] = {
|
|
|
|
make_wide_gamut(),
|
2017-02-07 18:56:11 +00:00
|
|
|
SkColorSpace::MakeSRGB(),
|
2016-12-22 15:52:25 +00:00
|
|
|
make_small_gamut(),
|
|
|
|
};
|
|
|
|
|
2020-07-31 00:24:57 +00:00
|
|
|
for (const sk_sp<SkColorSpace>& dstColorSpace : colorSpaces) {
|
2016-12-22 15:52:25 +00:00
|
|
|
for (SkColorType srcColorType : colorTypes) {
|
2017-03-20 18:21:18 +00:00
|
|
|
canvas->save();
|
|
|
|
sk_sp<SkImage> image = make_raster_image(srcColorType);
|
2018-11-26 20:34:12 +00:00
|
|
|
if (!image) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-27 16:44:07 +00:00
|
|
|
auto dContext = GrAsDirectContext(canvas->recordingContext());
|
|
|
|
if (dContext) {
|
|
|
|
image = image->makeTextureImage(dContext);
|
2017-03-20 20:54:28 +00:00
|
|
|
}
|
|
|
|
if (image) {
|
|
|
|
for (SkColorType dstColorType : colorTypes) {
|
|
|
|
for (SkAlphaType dstAlphaType : alphaTypes) {
|
2020-08-27 16:44:07 +00:00
|
|
|
draw_image(dContext, canvas, image.get(), dstColorType, dstAlphaType,
|
2017-03-20 20:54:28 +00:00
|
|
|
dstColorSpace, SkImage::kAllow_CachingHint);
|
|
|
|
canvas->translate((float)kWidth, 0.0f);
|
|
|
|
}
|
2016-12-22 15:52:25 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-20 18:21:18 +00:00
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0.0f, (float) kHeight);
|
2016-12-22 15:52:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2016-12-22 15:52:25 +00:00
|
|
|
};
|
|
|
|
DEF_GM( return new ReadPixelsGM; )
|
2017-01-10 22:41:53 +00:00
|
|
|
|
|
|
|
class ReadPixelsCodecGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
ReadPixelsCodecGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("readpixelscodec");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(3 * (kEncodedWidth + 1), 12 * (kEncodedHeight + 1));
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
2017-01-10 22:41:53 +00:00
|
|
|
if (!canvas->imageInfo().colorSpace()) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "This gm is only interesting in color correct modes.";
|
|
|
|
return DrawResult::kSkip;
|
2017-01-10 22:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const SkAlphaType alphaTypes[] = {
|
|
|
|
kUnpremul_SkAlphaType,
|
|
|
|
kPremul_SkAlphaType,
|
|
|
|
};
|
|
|
|
const SkColorType colorTypes[] = {
|
|
|
|
kRGBA_8888_SkColorType,
|
|
|
|
kBGRA_8888_SkColorType,
|
|
|
|
kRGBA_F16_SkColorType,
|
|
|
|
};
|
|
|
|
const sk_sp<SkColorSpace> colorSpaces[] = {
|
|
|
|
make_wide_gamut(),
|
2017-02-07 18:56:11 +00:00
|
|
|
SkColorSpace::MakeSRGB(),
|
2017-01-10 22:41:53 +00:00
|
|
|
make_small_gamut(),
|
|
|
|
};
|
|
|
|
const SkImage::CachingHint hints[] = {
|
|
|
|
SkImage::kAllow_CachingHint,
|
|
|
|
SkImage::kDisallow_CachingHint,
|
|
|
|
};
|
|
|
|
|
|
|
|
sk_sp<SkImage> image = make_codec_image();
|
2020-07-31 00:24:57 +00:00
|
|
|
for (const sk_sp<SkColorSpace>& dstColorSpace : colorSpaces) {
|
2017-01-10 22:41:53 +00:00
|
|
|
canvas->save();
|
|
|
|
for (SkColorType dstColorType : colorTypes) {
|
|
|
|
for (SkAlphaType dstAlphaType : alphaTypes) {
|
|
|
|
for (SkImage::CachingHint hint : hints) {
|
2020-08-27 16:44:07 +00:00
|
|
|
draw_image(nullptr, canvas, image.get(), dstColorType, dstAlphaType,
|
|
|
|
dstColorSpace, hint);
|
2017-01-10 22:41:53 +00:00
|
|
|
canvas->translate(0.0f, (float) kEncodedHeight + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate((float) kEncodedWidth + 1, 0.0f);
|
|
|
|
}
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2017-01-10 22:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const int kEncodedWidth = 8;
|
|
|
|
static const int kEncodedHeight = 8;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2017-01-10 22:41:53 +00:00
|
|
|
};
|
|
|
|
DEF_GM( return new ReadPixelsCodecGM; )
|
|
|
|
|
|
|
|
class ReadPixelsPictureGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
ReadPixelsPictureGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("readpixelspicture");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(3 * kWidth, 12 * kHeight);
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
2017-01-10 22:41:53 +00:00
|
|
|
if (!canvas->imageInfo().colorSpace()) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "This gm is only interesting in color correct modes.";
|
|
|
|
return DrawResult::kSkip;
|
2017-01-10 22:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const sk_sp<SkImage> images[] = {
|
2017-02-14 18:50:43 +00:00
|
|
|
make_picture_image(),
|
2017-01-10 22:41:53 +00:00
|
|
|
};
|
|
|
|
const SkAlphaType alphaTypes[] = {
|
|
|
|
kUnpremul_SkAlphaType,
|
|
|
|
kPremul_SkAlphaType,
|
|
|
|
};
|
|
|
|
const SkColorType colorTypes[] = {
|
|
|
|
kRGBA_8888_SkColorType,
|
|
|
|
kBGRA_8888_SkColorType,
|
|
|
|
kRGBA_F16_SkColorType,
|
|
|
|
};
|
|
|
|
const sk_sp<SkColorSpace> colorSpaces[] = {
|
|
|
|
make_wide_gamut(),
|
2017-02-07 18:56:11 +00:00
|
|
|
SkColorSpace::MakeSRGB(),
|
2017-01-10 22:41:53 +00:00
|
|
|
make_small_gamut(),
|
|
|
|
};
|
|
|
|
const SkImage::CachingHint hints[] = {
|
|
|
|
SkImage::kAllow_CachingHint,
|
|
|
|
SkImage::kDisallow_CachingHint,
|
|
|
|
};
|
|
|
|
|
2020-07-31 00:24:57 +00:00
|
|
|
for (const sk_sp<SkImage>& image : images) {
|
|
|
|
for (const sk_sp<SkColorSpace>& dstColorSpace : colorSpaces) {
|
2017-01-10 22:41:53 +00:00
|
|
|
canvas->save();
|
|
|
|
for (SkColorType dstColorType : colorTypes) {
|
|
|
|
for (SkAlphaType dstAlphaType : alphaTypes) {
|
|
|
|
for (SkImage::CachingHint hint : hints) {
|
2020-08-27 16:44:07 +00:00
|
|
|
draw_image(nullptr, canvas, image.get(), dstColorType, dstAlphaType,
|
2017-01-10 22:41:53 +00:00
|
|
|
dstColorSpace, hint);
|
|
|
|
canvas->translate(0.0f, (float) kHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate((float) kWidth, 0.0f);
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2017-01-10 22:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2017-01-10 22:41:53 +00:00
|
|
|
};
|
|
|
|
DEF_GM( return new ReadPixelsPictureGM; )
|