2016-08-26 17:13:39 +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/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColorSpace.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkImageGenerator.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkSize.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2020-07-10 18:33:22 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2016-08-26 17:13:39 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const SkISize kSize = SkISize::Make(100, 100);
|
|
|
|
const SkIRect kSubset = SkIRect::MakeLTRB(25, 25, 75, 75);
|
|
|
|
const SkRect kDest = SkRect::MakeXYWH(10, 10, 100, 100);
|
|
|
|
|
|
|
|
sk_sp<SkImage> make_mask(const sk_sp<SkSurface>& surface) {
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::draw_checkerboard(surface->getCanvas(), 0x80808080, 0x00000000, 5);
|
2016-08-26 17:13:39 +00:00
|
|
|
return surface->makeImageSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
class MaskGenerator final : public SkImageGenerator {
|
|
|
|
public:
|
|
|
|
MaskGenerator(const SkImageInfo& info) : INHERITED(info) {}
|
|
|
|
|
2017-05-12 15:41:27 +00:00
|
|
|
bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options&)
|
|
|
|
override {
|
2017-03-18 15:36:52 +00:00
|
|
|
SkImageInfo surfaceInfo = info;
|
|
|
|
if (kAlpha_8_SkColorType == info.colorType()) {
|
|
|
|
surfaceInfo = surfaceInfo.makeColorSpace(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
make_mask(SkSurface::MakeRasterDirect(surfaceInfo, pixels, rowBytes));
|
2016-08-26 17:13:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = SkImageGenerator;
|
2016-08-26 17:13:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using MakerT = sk_sp<SkImage>(*)(SkCanvas*, const SkImageInfo&);
|
|
|
|
const MakerT makers[] = {
|
|
|
|
// SkImage_Raster
|
|
|
|
[](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> {
|
|
|
|
return make_mask(SkSurface::MakeRaster(info));
|
|
|
|
},
|
|
|
|
|
|
|
|
// SkImage_Gpu
|
|
|
|
[](SkCanvas* c, const SkImageInfo& info) -> sk_sp<SkImage> {
|
|
|
|
sk_sp<SkSurface> surface;
|
2020-07-07 14:20:27 +00:00
|
|
|
surface = SkSurface::MakeRenderTarget(c->recordingContext(), SkBudgeted::kNo, info);
|
2016-08-26 17:13:39 +00:00
|
|
|
return make_mask(surface ? surface : SkSurface::MakeRaster(info));
|
|
|
|
},
|
|
|
|
|
2017-04-20 17:43:23 +00:00
|
|
|
// SkImage_Lazy
|
2016-08-26 17:13:39 +00:00
|
|
|
[](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> {
|
2019-12-11 15:45:01 +00:00
|
|
|
return SkImage::MakeFromGenerator(std::make_unique<MaskGenerator>(info));
|
2016-08-26 17:13:39 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace
|
2016-08-26 17:13:39 +00:00
|
|
|
|
|
|
|
// Checks whether subset SkImages preserve the original color type (A8 in this case).
|
|
|
|
DEF_SIMPLE_GM(imagemasksubset, canvas, 480, 480) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0xff00ff00);
|
|
|
|
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeA8(kSize.width(), kSize.height());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(makers); ++i) {
|
|
|
|
sk_sp<SkImage> image = makers[i](canvas, info);
|
|
|
|
if (image) {
|
|
|
|
canvas->drawImageRect(image, SkRect::Make(kSubset), kDest, &paint);
|
2020-07-10 18:33:22 +00:00
|
|
|
auto direct = GrAsDirectContext(canvas->recordingContext());
|
|
|
|
sk_sp<SkImage> subset = image->makeSubset(kSubset, direct);
|
2016-08-26 17:13:39 +00:00
|
|
|
canvas->drawImageRect(subset, kDest.makeOffset(kSize.width() * 1.5f, 0), &paint);
|
|
|
|
}
|
|
|
|
canvas->translate(0, kSize.height() * 1.5f);
|
|
|
|
}
|
|
|
|
}
|