2014-09-22 14:29:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2014-09-22 14:29:03 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "SkSurfaceProps.h"
|
2018-10-26 11:28:30 +00:00
|
|
|
#include "SkTextUtils.h"
|
2014-09-22 14:29:03 +00:00
|
|
|
|
|
|
|
#define W 200
|
|
|
|
#define H 100
|
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> make_shader() {
|
2014-09-22 14:29:03 +00:00
|
|
|
int a = 0x99;
|
|
|
|
int b = 0xBB;
|
|
|
|
SkPoint pts[] = { { 0, 0 }, { W, H } };
|
|
|
|
SkColor colors[] = { SkColorSetRGB(a, a, a), SkColorSetRGB(b, b, b) };
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
2014-09-22 14:29:03 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 16:30:44 +00:00
|
|
|
static sk_sp<SkSurface> make_surface(GrContext* ctx, const SkImageInfo& info, SkPixelGeometry geo) {
|
|
|
|
SkSurfaceProps props(0, geo);
|
2014-09-22 14:29:03 +00:00
|
|
|
if (ctx) {
|
2016-03-24 01:59:25 +00:00
|
|
|
return SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
|
2014-09-22 14:29:03 +00:00
|
|
|
} else {
|
2016-03-24 01:59:25 +00:00
|
|
|
return SkSurface::MakeRaster(info, &props);
|
2014-09-22 14:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_draw(SkCanvas* canvas, const char label[]) {
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setDither(true);
|
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(make_shader());
|
2014-09-22 14:29:03 +00:00
|
|
|
canvas->drawRect(SkRect::MakeWH(W, H), paint);
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setShader(nullptr);
|
2014-09-22 14:29:03 +00:00
|
|
|
|
|
|
|
paint.setColor(SK_ColorWHITE);
|
2019-01-03 20:45:53 +00:00
|
|
|
SkFont font(sk_tool_utils::create_portable_typeface(), 32);
|
|
|
|
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
|
|
|
SkTextUtils::DrawString(canvas, label, W / 2, H * 3 / 4, font, paint,
|
|
|
|
SkTextUtils::kCenter_Align);
|
2014-09-22 14:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class SurfacePropsGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
SurfacePropsGM() {}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-09-22 14:29:03 +00:00
|
|
|
return SkString("surfaceprops");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2016-08-18 16:30:44 +00:00
|
|
|
return SkISize::Make(W, H * 5);
|
2014-09-22 14:29:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-09-22 14:29:03 +00:00
|
|
|
GrContext* ctx = canvas->getGrContext();
|
|
|
|
|
|
|
|
// must be opaque to have a hope of testing LCD text
|
2016-08-30 14:07:59 +00:00
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
|
2014-09-22 14:29:03 +00:00
|
|
|
|
|
|
|
const struct {
|
|
|
|
SkPixelGeometry fGeo;
|
|
|
|
const char* fLabel;
|
2016-05-09 20:20:58 +00:00
|
|
|
} recs[] = {
|
2014-09-22 14:29:03 +00:00
|
|
|
{ kUnknown_SkPixelGeometry, "Unknown" },
|
|
|
|
{ kRGB_H_SkPixelGeometry, "RGB_H" },
|
|
|
|
{ kBGR_H_SkPixelGeometry, "BGR_H" },
|
|
|
|
{ kRGB_V_SkPixelGeometry, "RGB_V" },
|
|
|
|
{ kBGR_V_SkPixelGeometry, "BGR_V" },
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-09-22 14:29:03 +00:00
|
|
|
SkScalar x = 0;
|
2016-08-18 16:30:44 +00:00
|
|
|
SkScalar y = 0;
|
|
|
|
for (const auto& rec : recs) {
|
|
|
|
auto surface(make_surface(ctx, info, rec.fGeo));
|
|
|
|
if (!surface) {
|
|
|
|
SkDebugf("failed to create surface! label: %s", rec.fLabel);
|
|
|
|
continue;
|
2014-09-22 14:29:03 +00:00
|
|
|
}
|
2016-08-18 16:30:44 +00:00
|
|
|
test_draw(surface->getCanvas(), rec.fLabel);
|
|
|
|
surface->draw(canvas, x, y, nullptr);
|
|
|
|
y += H;
|
2014-09-22 14:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new SurfacePropsGM )
|
2014-11-21 16:46:37 +00:00
|
|
|
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
|
|
|
|
return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class NewSurfaceGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
NewSurfaceGM() {}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-11-21 16:46:37 +00:00
|
|
|
return SkString("surfacenew");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-11-21 16:46:37 +00:00
|
|
|
return SkISize::Make(300, 140);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drawInto(SkCanvas* canvas) {
|
|
|
|
canvas->drawColor(SK_ColorRED);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-11-21 16:46:37 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
|
|
|
|
|
2018-09-05 18:41:40 +00:00
|
|
|
auto surf(sk_tool_utils::makeSurface(canvas, info, nullptr));
|
2014-11-21 16:46:37 +00:00
|
|
|
drawInto(surf->getCanvas());
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(surf->makeImageSnapshot());
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->drawImage(image, 10, 10, nullptr);
|
2014-11-21 16:46:37 +00:00
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surf2(surf->makeSurface(info));
|
2014-11-21 16:46:37 +00:00
|
|
|
drawInto(surf2->getCanvas());
|
|
|
|
|
|
|
|
// Assert that the props were communicated transitively through the first image
|
|
|
|
SkASSERT(equal(surf->props(), surf2->props()));
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image2(surf2->makeImageSnapshot());
|
|
|
|
canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10, nullptr);
|
2014-11-21 16:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new NewSurfaceGM )
|
2017-03-06 21:37:07 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(copy_on_write_retain, canvas, 256, 256) {
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
|
2018-09-05 18:41:40 +00:00
|
|
|
sk_sp<SkSurface> surf = sk_tool_utils::makeSurface(canvas, info);
|
2017-03-06 21:37:07 +00:00
|
|
|
|
|
|
|
surf->getCanvas()->clear(SK_ColorRED);
|
|
|
|
// its important that image survives longer than the next draw, so the surface will see
|
|
|
|
// an outstanding image, and have to decide if it should retain or discard those pixels
|
|
|
|
sk_sp<SkImage> image = surf->makeImageSnapshot();
|
|
|
|
|
|
|
|
// normally a clear+opaque should trigger the discard optimization, but since we have a clip
|
|
|
|
// it should not (we need the previous red pixels).
|
|
|
|
surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
|
|
|
|
surf->getCanvas()->clear(SK_ColorBLUE);
|
|
|
|
|
|
|
|
// expect to see two rects: blue | red
|
|
|
|
canvas->drawImage(surf->makeImageSnapshot(), 0, 0, nullptr);
|
|
|
|
}
|
2017-03-07 14:37:29 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(copy_on_write_savelayer, canvas, 256, 256) {
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
|
2018-09-05 18:41:40 +00:00
|
|
|
sk_sp<SkSurface> surf = sk_tool_utils::makeSurface(canvas, info);
|
2017-03-07 14:37:29 +00:00
|
|
|
surf->getCanvas()->clear(SK_ColorRED);
|
|
|
|
// its important that image survives longer than the next draw, so the surface will see
|
|
|
|
// an outstanding image, and have to decide if it should retain or discard those pixels
|
|
|
|
sk_sp<SkImage> image = surf->makeImageSnapshot();
|
|
|
|
|
|
|
|
// now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should
|
|
|
|
// not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer
|
|
|
|
// with a non-opaque paint.
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAlpha(0x40);
|
|
|
|
surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint);
|
|
|
|
surf->getCanvas()->clear(SK_ColorBLUE);
|
|
|
|
surf->getCanvas()->restore();
|
|
|
|
|
|
|
|
// expect to see two rects: blue blended on red
|
|
|
|
canvas->drawImage(surf->makeImageSnapshot(), 0, 0, nullptr);
|
|
|
|
}
|
2018-11-21 14:12:09 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(surface_underdraw, canvas, 256, 256) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256, nullptr);
|
|
|
|
auto surf = sk_tool_utils::makeSurface(canvas, info);
|
|
|
|
|
|
|
|
const SkIRect subset = SkIRect::MakeLTRB(180, 0, 256, 256);
|
|
|
|
|
|
|
|
// noisy background
|
|
|
|
{
|
|
|
|
SkPoint pts[] = {{0, 0}, {40, 50}};
|
|
|
|
SkColor colors[] = {SK_ColorRED, SK_ColorBLUE};
|
|
|
|
auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kRepeat_TileMode);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(sh);
|
|
|
|
surf->getCanvas()->drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// save away the right-hand strip, then clear it
|
|
|
|
sk_sp<SkImage> saveImg = surf->makeImageSnapshot(subset);
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setBlendMode(SkBlendMode::kClear);
|
|
|
|
surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw the "foreground"
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
SkRect r = { 0, 10, 256, 35 };
|
|
|
|
while (r.fBottom < 256) {
|
|
|
|
surf->getCanvas()->drawRect(r, paint);
|
|
|
|
r.offset(0, r.height() * 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply the "fade"
|
|
|
|
{
|
|
|
|
SkPoint pts[] = {{SkIntToScalar(subset.left()), 0}, {SkIntToScalar(subset.right()), 0}};
|
|
|
|
SkColor colors[] = {0xFF000000, 0};
|
|
|
|
auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(sh);
|
|
|
|
paint.setBlendMode(SkBlendMode::kDstIn);
|
|
|
|
surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore the original strip, drawing it "under" the current foreground
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setBlendMode(SkBlendMode::kDstOver);
|
|
|
|
surf->getCanvas()->drawImage(saveImg,
|
|
|
|
SkIntToScalar(subset.left()), SkIntToScalar(subset.top()),
|
|
|
|
&paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// show it on screen
|
|
|
|
surf->draw(canvas, 0, 0, nullptr);
|
|
|
|
}
|