2013-08-08 21:13:38 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2015-12-25 20:56:03 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkBitmap.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColorPriv.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColorSpace.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.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/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2013-08-08 21:13:38 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-08-08 21:13:38 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This tests drawing device-covering rects with solid colors and bitmap shaders over a
|
|
|
|
* checkerboard background using different xfermodes.
|
|
|
|
*/
|
|
|
|
class Xfermodes3GM : public GM {
|
|
|
|
public:
|
2019-03-20 16:12:10 +00:00
|
|
|
Xfermodes3GM() { this->setBGColor(ToolUtils::color_to_565(0xFF70D0E0)); }
|
2013-08-08 21:13:38 +00:00
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-08-08 21:13:38 +00:00
|
|
|
return SkString("xfermodes3");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(630, 1215);
|
2013-08-08 21:13:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-08-08 21:13:38 +00:00
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface());
|
2013-08-08 21:13:38 +00:00
|
|
|
SkPaint labelP;
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkColor kSolidColors[] = {
|
2013-08-08 21:13:38 +00:00
|
|
|
SK_ColorTRANSPARENT,
|
|
|
|
SK_ColorBLUE,
|
|
|
|
0x80808000
|
|
|
|
};
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkColor kBmpAlphas[] = {
|
2013-08-08 21:13:38 +00:00
|
|
|
0xff,
|
|
|
|
0x80,
|
|
|
|
};
|
|
|
|
|
2018-04-25 18:38:24 +00:00
|
|
|
auto tempSurface(this->makeTempSurface(canvas, kSize, kSize));
|
2013-08-08 21:13:38 +00:00
|
|
|
|
|
|
|
int test = 0;
|
|
|
|
int x = 0, y = 0;
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr struct { SkPaint::Style fStyle; SkScalar fWidth; } kStrokes[] = {
|
2013-08-14 19:20:45 +00:00
|
|
|
{SkPaint::kFill_Style, 0},
|
|
|
|
{SkPaint::kStroke_Style, SkIntToScalar(kSize) / 2},
|
|
|
|
};
|
|
|
|
for (size_t s = 0; s < SK_ARRAY_COUNT(kStrokes); ++s) {
|
2016-11-15 21:44:34 +00:00
|
|
|
for (size_t m = 0; m <= (size_t)SkBlendMode::kLastMode; ++m) {
|
2016-10-06 00:33:02 +00:00
|
|
|
SkBlendMode mode = static_cast<SkBlendMode>(m);
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(SkBlendMode_Name(mode),
|
2019-01-08 14:38:02 +00:00
|
|
|
SkIntToScalar(x),
|
|
|
|
SkIntToScalar(y + kSize + 3) + font.getSize(),
|
|
|
|
font, labelP);
|
2013-08-14 19:20:45 +00:00
|
|
|
for (size_t c = 0; c < SK_ARRAY_COUNT(kSolidColors); ++c) {
|
|
|
|
SkPaint modePaint;
|
2016-10-06 00:33:02 +00:00
|
|
|
modePaint.setBlendMode(mode);
|
2013-08-14 19:20:45 +00:00
|
|
|
modePaint.setColor(kSolidColors[c]);
|
|
|
|
modePaint.setStyle(kStrokes[s].fStyle);
|
|
|
|
modePaint.setStrokeWidth(kStrokes[s].fWidth);
|
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get());
|
2013-08-14 19:20:45 +00:00
|
|
|
|
|
|
|
++test;
|
|
|
|
x += kSize + 10;
|
|
|
|
if (!(test % kTestsPerRow)) {
|
|
|
|
x = 0;
|
|
|
|
y += kSize + 30;
|
|
|
|
}
|
2013-08-08 21:13:38 +00:00
|
|
|
}
|
2013-08-14 19:20:45 +00:00
|
|
|
for (size_t a = 0; a < SK_ARRAY_COUNT(kBmpAlphas); ++a) {
|
|
|
|
SkPaint modePaint;
|
2016-10-06 00:33:02 +00:00
|
|
|
modePaint.setBlendMode(mode);
|
2013-08-14 19:20:45 +00:00
|
|
|
modePaint.setAlpha(kBmpAlphas[a]);
|
|
|
|
modePaint.setShader(fBmpShader);
|
|
|
|
modePaint.setStyle(kStrokes[s].fStyle);
|
|
|
|
modePaint.setStrokeWidth(kStrokes[s].fWidth);
|
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get());
|
2013-08-14 19:20:45 +00:00
|
|
|
|
|
|
|
++test;
|
|
|
|
x += kSize + 10;
|
|
|
|
if (!(test % kTestsPerRow)) {
|
|
|
|
x = 0;
|
|
|
|
y += kSize + 30;
|
|
|
|
}
|
2013-08-08 21:13:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* GrContext has optimizations around full rendertarget draws that can be replaced with clears.
|
|
|
|
* We are trying to test those. We could use saveLayer() to create small SkGpuDevices but
|
|
|
|
* saveLayer() uses the texture cache. This means that the actual render target may be larger
|
|
|
|
* than the layer. Because the clip will contain the layer's bounds, no draws will be full-RT.
|
2018-04-25 18:38:24 +00:00
|
|
|
* So explicitly create a temporary canvas with dimensions exactly the layer size.
|
2013-08-08 21:13:38 +00:00
|
|
|
*/
|
2018-04-25 18:38:24 +00:00
|
|
|
sk_sp<SkSurface> makeTempSurface(SkCanvas* baseCanvas, int w, int h) {
|
2015-01-16 15:32:33 +00:00
|
|
|
SkImageInfo baseInfo = baseCanvas->imageInfo();
|
|
|
|
SkImageInfo info = SkImageInfo::Make(w, h, baseInfo.colorType(), baseInfo.alphaType(),
|
2017-01-12 15:13:40 +00:00
|
|
|
baseInfo.refColorSpace());
|
2018-04-25 18:38:24 +00:00
|
|
|
return baseCanvas->makeSurface(info);
|
2013-08-08 21:13:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawMode(SkCanvas* canvas,
|
|
|
|
int x, int y, int w, int h,
|
2015-12-25 20:56:03 +00:00
|
|
|
const SkPaint& modePaint, SkSurface* surface) {
|
2013-08-08 21:13:38 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h));
|
|
|
|
|
|
|
|
SkCanvas* modeCanvas;
|
2015-12-25 20:56:03 +00:00
|
|
|
if (nullptr == surface) {
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&r, nullptr);
|
2018-04-25 18:38:24 +00:00
|
|
|
canvas->clipRect(r);
|
2013-08-08 21:13:38 +00:00
|
|
|
modeCanvas = canvas;
|
|
|
|
} else {
|
2015-12-25 20:56:03 +00:00
|
|
|
modeCanvas = surface->getCanvas();
|
2013-08-08 21:13:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint bgPaint;
|
|
|
|
bgPaint.setAntiAlias(false);
|
|
|
|
bgPaint.setShader(fBGShader);
|
|
|
|
modeCanvas->drawRect(r, bgPaint);
|
|
|
|
modeCanvas->drawRect(r, modePaint);
|
2015-08-27 14:41:13 +00:00
|
|
|
modeCanvas = nullptr;
|
2013-08-08 21:13:38 +00:00
|
|
|
|
2015-12-25 20:56:03 +00:00
|
|
|
if (nullptr == surface) {
|
2013-08-08 21:13:38 +00:00
|
|
|
canvas->restore();
|
|
|
|
} else {
|
2021-01-06 13:43:51 +00:00
|
|
|
surface->draw(canvas, 0, 0);
|
2013-08-08 21:13:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
|
|
|
|
SkPaint borderPaint;
|
|
|
|
borderPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawRect(r, borderPaint);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2016-09-01 18:24:54 +00:00
|
|
|
const uint32_t kCheckData[] = {
|
2015-07-21 14:42:45 +00:00
|
|
|
SkPackARGB32(0xFF, 0x42, 0x41, 0x42),
|
|
|
|
SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
|
|
|
|
SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
|
|
|
|
SkPackARGB32(0xFF, 0x42, 0x41, 0x42)
|
2013-08-08 21:13:38 +00:00
|
|
|
};
|
|
|
|
SkBitmap bg;
|
2014-01-25 16:46:20 +00:00
|
|
|
bg.allocN32Pixels(2, 2, true);
|
2013-08-08 21:13:38 +00:00
|
|
|
memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
|
|
|
|
|
|
|
|
SkMatrix lm;
|
|
|
|
lm.setScale(SkIntToScalar(kCheckSize), SkIntToScalar(kCheckSize));
|
2020-12-12 16:18:31 +00:00
|
|
|
fBGShader = bg.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
|
|
|
|
SkSamplingOptions(), lm);
|
2013-08-08 21:13:38 +00:00
|
|
|
|
|
|
|
SkPaint bmpPaint;
|
2016-03-13 21:13:58 +00:00
|
|
|
const SkPoint kCenter = { SkIntToScalar(kSize) / 2, SkIntToScalar(kSize) / 2 };
|
|
|
|
const SkColor kColors[] = {
|
|
|
|
SK_ColorTRANSPARENT, 0x80800000, 0xF020F060, SK_ColorWHITE
|
|
|
|
};
|
|
|
|
bmpPaint.setShader(SkGradientShader::MakeRadial(kCenter, 3 * SkIntToScalar(kSize) / 4,
|
|
|
|
kColors, nullptr, SK_ARRAY_COUNT(kColors),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kRepeat));
|
2013-08-08 21:13:38 +00:00
|
|
|
|
|
|
|
SkBitmap bmp;
|
2014-01-25 16:46:20 +00:00
|
|
|
bmp.allocN32Pixels(kSize, kSize);
|
2013-08-08 21:13:38 +00:00
|
|
|
SkCanvas bmpCanvas(bmp);
|
|
|
|
|
|
|
|
bmpCanvas.clear(SK_ColorTRANSPARENT);
|
|
|
|
SkRect rect = { SkIntToScalar(kSize) / 8, SkIntToScalar(kSize) / 8,
|
|
|
|
7 * SkIntToScalar(kSize) / 8, 7 * SkIntToScalar(kSize) / 8};
|
|
|
|
bmpCanvas.drawRect(rect, bmpPaint);
|
|
|
|
|
2020-12-12 16:18:31 +00:00
|
|
|
fBmpShader = bmp.makeShader(SkSamplingOptions());
|
2013-08-08 21:13:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kCheckSize = 8,
|
|
|
|
kSize = 30,
|
|
|
|
kTestsPerRow = 15,
|
|
|
|
};
|
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
sk_sp<SkShader> fBGShader;
|
|
|
|
sk_sp<SkShader> fBmpShader;
|
2013-08-08 21:13:38 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2013-08-08 21:13:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM(return new Xfermodes3GM;)
|
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|