2015-01-26 08:14:26 +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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkImageFilter.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/SkScalar.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkTableColorFilter.h"
|
2020-07-10 15:49:06 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2015-01-26 08:14:26 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr int kTestRectSize = 50;
|
|
|
|
constexpr int kDetectorGreenValue = 50;
|
2015-01-26 08:14:26 +00:00
|
|
|
|
|
|
|
// Below are few functions to install "detector" color filters. The filter is there to assert that
|
|
|
|
// the color value it sees is the expected. It will trigger only with kDetectorGreenValue, and
|
|
|
|
// turn that value into full green. The idea is that if an optimization incorrectly changes
|
|
|
|
// kDetectorGreenValue and then the incorrect value is observable by some part of the drawing
|
|
|
|
// pipeline, that pixel will remain empty.
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
static sk_sp<SkColorFilter> make_detector_color_filter() {
|
2015-01-26 08:14:26 +00:00
|
|
|
uint8_t tableA[256] = { 0, };
|
|
|
|
uint8_t tableR[256] = { 0, };
|
|
|
|
uint8_t tableG[256] = { 0, };
|
|
|
|
uint8_t tableB[256] = { 0, };
|
|
|
|
tableA[255] = 255;
|
|
|
|
tableG[kDetectorGreenValue] = 255;
|
2016-03-22 17:17:23 +00:00
|
|
|
return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
|
2015-01-26 08:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This detector detects that color filter phase of the pixel pipeline receives the correct value.
|
|
|
|
static void install_detector_color_filter(SkPaint* drawPaint) {
|
2016-03-22 17:17:23 +00:00
|
|
|
drawPaint->setColorFilter(make_detector_color_filter());
|
2015-01-26 08:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This detector detects that image filter phase of the pixel pipeline receives the correct value.
|
|
|
|
static void install_detector_image_filter(SkPaint* drawPaint) {
|
2019-08-02 19:21:23 +00:00
|
|
|
drawPaint->setImageFilter(SkImageFilters::ColorFilter(
|
|
|
|
make_detector_color_filter(), drawPaint->refImageFilter()));
|
2015-01-26 08:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void no_detector_install(SkPaint*) {
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void(*InstallDetectorFunc)(SkPaint*);
|
|
|
|
|
|
|
|
|
|
|
|
// Draws an pattern that can be optimized by alpha folding outer savelayer alpha value to
|
|
|
|
// inner draw. Since we know that folding will happen to the inner draw, install a detector
|
|
|
|
// to make sure that optimization does not change anything observable.
|
|
|
|
static void draw_save_layer_draw_rect_restore_sequence(SkCanvas* canvas, SkColor shapeColor,
|
|
|
|
InstallDetectorFunc installDetector) {
|
|
|
|
SkRect targetRect(SkRect::MakeWH(SkIntToScalar(kTestRectSize), SkIntToScalar(kTestRectSize)));
|
|
|
|
SkPaint layerPaint;
|
|
|
|
layerPaint.setColor(SkColorSetARGB(128, 0, 0, 0));
|
|
|
|
canvas->saveLayer(&targetRect, &layerPaint);
|
|
|
|
SkPaint drawPaint;
|
|
|
|
drawPaint.setColor(shapeColor);
|
|
|
|
installDetector(&drawPaint);
|
|
|
|
canvas->drawRect(targetRect, drawPaint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draws an pattern that can be optimized by alpha folding outer savelayer alpha value to
|
|
|
|
// inner draw. A variant where the draw is not uniform color.
|
|
|
|
static void draw_save_layer_draw_bitmap_restore_sequence(SkCanvas* canvas, SkColor shapeColor,
|
|
|
|
InstallDetectorFunc installDetector) {
|
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.allocN32Pixels(kTestRectSize, kTestRectSize);
|
|
|
|
bitmap.eraseColor(shapeColor);
|
|
|
|
{
|
|
|
|
// Make the bitmap non-uniform color, so that it can not be optimized as uniform drawRect.
|
2021-08-13 02:33:57 +00:00
|
|
|
SkCanvas bitmapCanvas(bitmap);
|
2015-01-26 08:14:26 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setColor(SK_ColorWHITE);
|
|
|
|
SkASSERT(shapeColor != SK_ColorWHITE);
|
2021-08-13 02:33:57 +00:00
|
|
|
bitmapCanvas.drawRect(SkRect::MakeWH(SkIntToScalar(7), SkIntToScalar(7)), p);
|
2015-01-26 08:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkRect targetRect(SkRect::MakeWH(SkIntToScalar(kTestRectSize), SkIntToScalar(kTestRectSize)));
|
|
|
|
SkPaint layerPaint;
|
|
|
|
layerPaint.setColor(SkColorSetARGB(129, 0, 0, 0));
|
|
|
|
canvas->saveLayer(&targetRect, &layerPaint);
|
|
|
|
SkPaint drawPaint;
|
|
|
|
installDetector(&drawPaint);
|
2021-01-25 22:49:32 +00:00
|
|
|
canvas->drawImage(bitmap.asImage(), 0, 0, SkSamplingOptions(), &drawPaint);
|
2015-01-26 08:14:26 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draws an pattern that can be optimized by alpha folding outer savelayer alpha value to
|
|
|
|
// inner savelayer. We know that alpha folding happens to inner savelayer, so add detector there.
|
|
|
|
static void draw_svg_opacity_and_filter_layer_sequence(SkCanvas* canvas, SkColor shapeColor,
|
|
|
|
InstallDetectorFunc installDetector) {
|
|
|
|
|
|
|
|
SkRect targetRect(SkRect::MakeWH(SkIntToScalar(kTestRectSize), SkIntToScalar(kTestRectSize)));
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> shape;
|
2015-01-26 08:14:26 +00:00
|
|
|
{
|
|
|
|
SkPictureRecorder recorder;
|
2021-08-13 02:33:57 +00:00
|
|
|
SkCanvas* recordCanvas = recorder.beginRecording(SkIntToScalar(kTestRectSize + 2),
|
|
|
|
SkIntToScalar(kTestRectSize + 2));
|
2015-01-26 08:14:26 +00:00
|
|
|
SkPaint shapePaint;
|
|
|
|
shapePaint.setColor(shapeColor);
|
2021-08-13 02:33:57 +00:00
|
|
|
recordCanvas->drawRect(targetRect, shapePaint);
|
2016-03-18 14:25:55 +00:00
|
|
|
shape = recorder.finishRecordingAsPicture();
|
2015-01-26 08:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint layerPaint;
|
|
|
|
layerPaint.setColor(SkColorSetARGB(130, 0, 0, 0));
|
|
|
|
canvas->saveLayer(&targetRect, &layerPaint);
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(targetRect);
|
|
|
|
SkPaint drawPaint;
|
2019-08-02 19:21:23 +00:00
|
|
|
drawPaint.setImageFilter(SkImageFilters::Picture(shape));
|
2015-01-26 08:14:26 +00:00
|
|
|
installDetector(&drawPaint);
|
|
|
|
canvas->saveLayer(&targetRect, &drawPaint);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->restore();
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draws two columns of rectangles. The test is correct when:
|
|
|
|
// - Left and right columns always identical
|
|
|
|
// - First 3 rows are green, with a white dent in the middle row
|
|
|
|
// - Next 6 rows are green, with a grey dent in the middle row
|
|
|
|
// (the grey dent is from the color filter removing everything but the "good" green, see below)
|
|
|
|
// - Last 6 rows are grey
|
|
|
|
DEF_SIMPLE_GM(recordopts, canvas, (kTestRectSize+1)*2, (kTestRectSize+1)*15) {
|
2020-07-10 15:49:06 +00:00
|
|
|
auto direct = GrAsDirectContext(canvas->recordingContext());
|
2015-01-26 08:14:26 +00:00
|
|
|
canvas->clear(SK_ColorTRANSPARENT);
|
|
|
|
|
|
|
|
typedef void (*TestVariantSequence)(SkCanvas*, SkColor, InstallDetectorFunc);
|
|
|
|
TestVariantSequence funcs[] = {
|
|
|
|
draw_save_layer_draw_rect_restore_sequence,
|
|
|
|
draw_save_layer_draw_bitmap_restore_sequence,
|
|
|
|
draw_svg_opacity_and_filter_layer_sequence,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Draw layer-related sequences that can be optimized by folding the opacity layer alpha to
|
|
|
|
// the inner draw operation. This tries to trigger the optimization, and relies on gm diffs
|
|
|
|
// to keep the color value correct over time.
|
|
|
|
|
|
|
|
// Draws two green rects side by side: one is without the optimization, the other is with
|
|
|
|
// the optimization applied.
|
|
|
|
|
|
|
|
SkColor shapeColor = SkColorSetARGB(255, 0, 255, 0);
|
2022-06-21 13:49:17 +00:00
|
|
|
for (size_t k = 0; k < std::size(funcs); ++k) {
|
2015-01-26 08:14:26 +00:00
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
TestVariantSequence drawTestSequence = funcs[k];
|
|
|
|
drawTestSequence(canvas, shapeColor, no_detector_install);
|
2020-07-10 15:49:06 +00:00
|
|
|
if (direct) {
|
|
|
|
direct->flushAndSubmit();
|
2019-03-04 16:00:10 +00:00
|
|
|
}
|
2015-01-26 08:14:26 +00:00
|
|
|
canvas->translate(SkIntToScalar(kTestRectSize) + SkIntToScalar(1), SkIntToScalar(0));
|
|
|
|
{
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
drawTestSequence(recorder.beginRecording(SkIntToScalar(kTestRectSize),
|
|
|
|
SkIntToScalar(kTestRectSize)),
|
|
|
|
shapeColor, no_detector_install);
|
2016-03-18 14:25:55 +00:00
|
|
|
recorder.finishRecordingAsPicture()->playback(canvas);
|
2020-07-10 15:49:06 +00:00
|
|
|
if (direct) {
|
|
|
|
direct->flushAndSubmit();
|
2019-03-04 16:00:10 +00:00
|
|
|
}
|
2015-01-26 08:14:26 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(SkIntToScalar(0), SkIntToScalar(kTestRectSize) + SkIntToScalar(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the same layer related sequences, but manipulate the sequences so that the result is
|
|
|
|
// incorrect if the alpha is folded or folded incorrectly. These test the observable state
|
|
|
|
// throughout the pixel pipeline, and thus may turn off the optimizations (this is why we
|
|
|
|
// trigger the optimizations above).
|
|
|
|
|
|
|
|
// Draws two green rects side by side: one is without the optimization, the other is with
|
|
|
|
// the possibility that optimization is applied.
|
|
|
|
// At the end, draws the same patterns in translucent black. This tests that the detectors
|
|
|
|
// work, eg. that if the value the detector sees is wrong, the resulting image shows this.
|
|
|
|
SkColor shapeColors[] = {
|
|
|
|
SkColorSetARGB(255, 0, kDetectorGreenValue, 0),
|
2015-01-26 13:39:26 +00:00
|
|
|
SkColorSetARGB(255, 0, (kDetectorGreenValue + 1), 0) // This tests that detectors work.
|
2015-01-26 08:14:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
InstallDetectorFunc detectorInstallFuncs[] = {
|
|
|
|
install_detector_image_filter,
|
|
|
|
install_detector_color_filter
|
|
|
|
};
|
|
|
|
|
2022-06-21 13:49:17 +00:00
|
|
|
for (size_t i = 0; i < std::size(shapeColors); ++i) {
|
2015-01-26 08:14:26 +00:00
|
|
|
shapeColor = shapeColors[i];
|
2022-06-21 13:49:17 +00:00
|
|
|
for (size_t j = 0; j < std::size(detectorInstallFuncs); ++j) {
|
2015-01-26 08:14:26 +00:00
|
|
|
InstallDetectorFunc detectorInstallFunc = detectorInstallFuncs[j];
|
2022-06-21 13:49:17 +00:00
|
|
|
for (size_t k = 0; k < std::size(funcs); ++k) {
|
2015-01-26 08:14:26 +00:00
|
|
|
TestVariantSequence drawTestSequence = funcs[k];
|
|
|
|
canvas->save();
|
|
|
|
drawTestSequence(canvas, shapeColor, detectorInstallFunc);
|
2020-07-10 15:49:06 +00:00
|
|
|
if (direct) {
|
|
|
|
direct->flushAndSubmit();
|
2019-03-04 16:00:10 +00:00
|
|
|
}
|
2020-07-10 15:49:06 +00:00
|
|
|
canvas->translate(SkIntToScalar(kTestRectSize + 1), SkIntToScalar(0));
|
2015-01-26 08:14:26 +00:00
|
|
|
{
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
drawTestSequence(recorder.beginRecording(SkIntToScalar(kTestRectSize),
|
|
|
|
SkIntToScalar(kTestRectSize)),
|
|
|
|
shapeColor, detectorInstallFunc);
|
2016-03-18 14:25:55 +00:00
|
|
|
recorder.finishRecordingAsPicture()->playback(canvas);
|
2020-07-10 15:49:06 +00:00
|
|
|
if (direct) {
|
|
|
|
direct->flushAndSubmit();
|
2019-03-04 16:00:10 +00:00
|
|
|
}
|
2015-01-26 08:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canvas->restore();
|
2020-07-10 15:49:06 +00:00
|
|
|
canvas->translate(SkIntToScalar(0), SkIntToScalar(kTestRectSize + 1));
|
2015-01-26 08:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|