| 1 |
/* |
| 2 |
* Copyright 2014 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorFilter.h" |
| 13 |
#include "include/core/SkImageFilter.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkPicture.h" |
| 16 |
#include "include/core/SkPictureRecorder.h" |
| 17 |
#include "include/core/SkRect.h" |
| 18 |
#include "include/core/SkRefCnt.h" |
| 19 |
#include "include/core/SkScalar.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
#include "include/effects/SkImageFilters.h" |
| 22 |
|
| 23 |
#if defined(SK_GANESH) |
| 24 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
constexpr int kTestRectSize = 50; |
| 28 |
constexpr int kDetectorGreenValue = 50; |
| 29 |
|
| 30 |
// Below are few functions to install "detector" color filters. The filter is there to assert that |
| 31 |
// the color value it sees is the expected. It will trigger only with kDetectorGreenValue, and |
| 32 |
// turn that value into full green. The idea is that if an optimization incorrectly changes |
| 33 |
// kDetectorGreenValue and then the incorrect value is observable by some part of the drawing |
| 34 |
// pipeline, that pixel will remain empty. |
| 35 |
|
| 36 |
static sk_sp<SkColorFilter> make_detector_color_filter() { |
| 37 |
uint8_t tableA[256] = { 0, }; |
| 38 |
uint8_t tableR[256] = { 0, }; |
| 39 |
uint8_t tableG[256] = { 0, }; |
| 40 |
uint8_t tableB[256] = { 0, }; |
| 41 |
tableA[255] = 255; |
| 42 |
tableG[kDetectorGreenValue] = 255; |
| 43 |
return SkColorFilters::TableARGB(tableA, tableR, tableG, tableB); |
| 44 |
} |
| 45 |
|
| 46 |
// This detector detects that color filter phase of the pixel pipeline receives the correct value. |
| 47 |
static void install_detector_color_filter(SkPaint* drawPaint) { |
| 48 |
drawPaint->setColorFilter(make_detector_color_filter()); |
| 49 |
} |
| 50 |
|
| 51 |
// This detector detects that image filter phase of the pixel pipeline receives the correct value. |
| 52 |
static void install_detector_image_filter(SkPaint* drawPaint) { |
| 53 |
drawPaint->setImageFilter(SkImageFilters::ColorFilter( |
| 54 |
make_detector_color_filter(), drawPaint->refImageFilter())); |
| 55 |
} |
| 56 |
|
| 57 |
static void no_detector_install(SkPaint*) { |
| 58 |
} |
| 59 |
|
| 60 |
typedef void(*InstallDetectorFunc)(SkPaint*); |
| 61 |
|
| 62 |
|
| 63 |
// Draws an pattern that can be optimized by alpha folding outer savelayer alpha value to |
| 64 |
// inner draw. Since we know that folding will happen to the inner draw, install a detector |
| 65 |
// to make sure that optimization does not change anything observable. |
| 66 |
static void draw_save_layer_draw_rect_restore_sequence(SkCanvas* canvas, SkColor shapeColor, |
| 67 |
InstallDetectorFunc installDetector) { |
| 68 |
SkRect targetRect(SkRect::MakeWH(SkIntToScalar(kTestRectSize), SkIntToScalar(kTestRectSize))); |
| 69 |
SkPaint layerPaint; |
| 70 |
layerPaint.setColor(SkColorSetARGB(128, 0, 0, 0)); |
| 71 |
canvas->saveLayer(&targetRect, &layerPaint); |
| 72 |
SkPaint drawPaint; |
| 73 |
drawPaint.setColor(shapeColor); |
| 74 |
installDetector(&drawPaint); |
| 75 |
canvas->drawRect(targetRect, drawPaint); |
| 76 |
canvas->restore(); |
| 77 |
} |
| 78 |
|
| 79 |
// Draws an pattern that can be optimized by alpha folding outer savelayer alpha value to |
| 80 |
// inner draw. A variant where the draw is not uniform color. |
| 81 |
static void draw_save_layer_draw_bitmap_restore_sequence(SkCanvas* canvas, SkColor shapeColor, |
| 82 |
InstallDetectorFunc installDetector) { |
| 83 |
SkBitmap bitmap; |
| 84 |
bitmap.allocN32Pixels(kTestRectSize, kTestRectSize); |
| 85 |
bitmap.eraseColor(shapeColor); |
| 86 |
{ |
| 87 |
// Make the bitmap non-uniform color, so that it can not be optimized as uniform drawRect. |
| 88 |
SkCanvas bitmapCanvas(bitmap); |
| 89 |
SkPaint p; |
| 90 |
p.setColor(SK_ColorWHITE); |
| 91 |
SkASSERT(shapeColor != SK_ColorWHITE); |
| 92 |
bitmapCanvas.drawRect(SkRect::MakeWH(SkIntToScalar(7), SkIntToScalar(7)), p); |
| 93 |
} |
| 94 |
|
| 95 |
SkRect targetRect(SkRect::MakeWH(SkIntToScalar(kTestRectSize), SkIntToScalar(kTestRectSize))); |
| 96 |
SkPaint layerPaint; |
| 97 |
layerPaint.setColor(SkColorSetARGB(129, 0, 0, 0)); |
| 98 |
canvas->saveLayer(&targetRect, &layerPaint); |
| 99 |
SkPaint drawPaint; |
| 100 |
installDetector(&drawPaint); |
| 101 |
canvas->drawImage(bitmap.asImage(), 0, 0, SkSamplingOptions(), &drawPaint); |
| 102 |
canvas->restore(); |
| 103 |
} |
| 104 |
|
| 105 |
// Draws an pattern that can be optimized by alpha folding outer savelayer alpha value to |
| 106 |
// inner savelayer. We know that alpha folding happens to inner savelayer, so add detector there. |
| 107 |
static void draw_svg_opacity_and_filter_layer_sequence(SkCanvas* canvas, SkColor shapeColor, |
| 108 |
InstallDetectorFunc installDetector) { |
| 109 |
|
| 110 |
SkRect targetRect(SkRect::MakeWH(SkIntToScalar(kTestRectSize), SkIntToScalar(kTestRectSize))); |
| 111 |
sk_sp<SkPicture> shape; |
| 112 |
{ |
| 113 |
SkPictureRecorder recorder; |
| 114 |
SkCanvas* recordCanvas = recorder.beginRecording(SkIntToScalar(kTestRectSize + 2), |
| 115 |
SkIntToScalar(kTestRectSize + 2)); |
| 116 |
SkPaint shapePaint; |
| 117 |
shapePaint.setColor(shapeColor); |
| 118 |
recordCanvas->drawRect(targetRect, shapePaint); |
| 119 |
shape = recorder.finishRecordingAsPicture(); |
| 120 |
} |
| 121 |
|
| 122 |
SkPaint layerPaint; |
| 123 |
layerPaint.setColor(SkColorSetARGB(130, 0, 0, 0)); |
| 124 |
canvas->saveLayer(&targetRect, &layerPaint); |
| 125 |
canvas->save(); |
| 126 |
canvas->clipRect(targetRect); |
| 127 |
SkPaint drawPaint; |
| 128 |
drawPaint.setImageFilter(SkImageFilters::Picture(shape)); |
| 129 |
installDetector(&drawPaint); |
| 130 |
canvas->saveLayer(&targetRect, &drawPaint); |
| 131 |
canvas->restore(); |
| 132 |
canvas->restore(); |
| 133 |
canvas->restore(); |
| 134 |
} |
| 135 |
|
| 136 |
// Draws two columns of rectangles. The test is correct when: |
| 137 |
// - Left and right columns always identical |
| 138 |
// - First 3 rows are green, with a white dent in the middle row |
| 139 |
// - Next 6 rows are green, with a grey dent in the middle row |
| 140 |
// (the grey dent is from the color filter removing everything but the "good" green, see below) |
| 141 |
// - Last 6 rows are grey |
| 142 |
DEF_SIMPLE_GM(recordopts, canvas, (kTestRectSize+1)*2, (kTestRectSize+1)*15) { |
| 143 |
auto flushGanesh = [&] { |
| 144 |
#if defined(SK_GANESH) |
| 145 |
if (auto direct = GrAsDirectContext(canvas->recordingContext())) { |
| 146 |
direct->flushAndSubmit(); |
| 147 |
} |
| 148 |
#endif |
| 149 |
}; |
| 150 |
|
| 151 |
canvas->clear(SK_ColorTRANSPARENT); |
| 152 |
|
| 153 |
typedef void (*TestVariantSequence)(SkCanvas*, SkColor, InstallDetectorFunc); |
| 154 |
TestVariantSequence funcs[] = { |
| 155 |
draw_save_layer_draw_rect_restore_sequence, |
| 156 |
draw_save_layer_draw_bitmap_restore_sequence, |
| 157 |
draw_svg_opacity_and_filter_layer_sequence, |
| 158 |
}; |
| 159 |
|
| 160 |
// Draw layer-related sequences that can be optimized by folding the opacity layer alpha to |
| 161 |
// the inner draw operation. This tries to trigger the optimization, and relies on gm diffs |
| 162 |
// to keep the color value correct over time. |
| 163 |
|
| 164 |
// Draws two green rects side by side: one is without the optimization, the other is with |
| 165 |
// the optimization applied. |
| 166 |
|
| 167 |
SkColor shapeColor = SkColorSetARGB(255, 0, 255, 0); |
| 168 |
for (size_t k = 0; k < std::size(funcs); ++k) { |
| 169 |
canvas->save(); |
| 170 |
|
| 171 |
TestVariantSequence drawTestSequence = funcs[k]; |
| 172 |
drawTestSequence(canvas, shapeColor, no_detector_install); |
| 173 |
flushGanesh(); |
| 174 |
canvas->translate(SkIntToScalar(kTestRectSize) + SkIntToScalar(1), SkIntToScalar(0)); |
| 175 |
{ |
| 176 |
SkPictureRecorder recorder; |
| 177 |
drawTestSequence(recorder.beginRecording(SkIntToScalar(kTestRectSize), |
| 178 |
SkIntToScalar(kTestRectSize)), |
| 179 |
shapeColor, no_detector_install); |
| 180 |
recorder.finishRecordingAsPicture()->playback(canvas); |
| 181 |
flushGanesh(); |
| 182 |
} |
| 183 |
canvas->restore(); |
| 184 |
canvas->translate(SkIntToScalar(0), SkIntToScalar(kTestRectSize) + SkIntToScalar(1)); |
| 185 |
} |
| 186 |
|
| 187 |
// Draw the same layer related sequences, but manipulate the sequences so that the result is |
| 188 |
// incorrect if the alpha is folded or folded incorrectly. These test the observable state |
| 189 |
// throughout the pixel pipeline, and thus may turn off the optimizations (this is why we |
| 190 |
// trigger the optimizations above). |
| 191 |
|
| 192 |
// Draws two green rects side by side: one is without the optimization, the other is with |
| 193 |
// the possibility that optimization is applied. |
| 194 |
// At the end, draws the same patterns in translucent black. This tests that the detectors |
| 195 |
// work, eg. that if the value the detector sees is wrong, the resulting image shows this. |
| 196 |
SkColor shapeColors[] = { |
| 197 |
SkColorSetARGB(255, 0, kDetectorGreenValue, 0), |
| 198 |
SkColorSetARGB(255, 0, (kDetectorGreenValue + 1), 0) // This tests that detectors work. |
| 199 |
}; |
| 200 |
|
| 201 |
InstallDetectorFunc detectorInstallFuncs[] = { |
| 202 |
install_detector_image_filter, |
| 203 |
install_detector_color_filter |
| 204 |
}; |
| 205 |
|
| 206 |
for (size_t i = 0; i < std::size(shapeColors); ++i) { |
| 207 |
shapeColor = shapeColors[i]; |
| 208 |
for (size_t j = 0; j < std::size(detectorInstallFuncs); ++j) { |
| 209 |
InstallDetectorFunc detectorInstallFunc = detectorInstallFuncs[j]; |
| 210 |
for (size_t k = 0; k < std::size(funcs); ++k) { |
| 211 |
TestVariantSequence drawTestSequence = funcs[k]; |
| 212 |
canvas->save(); |
| 213 |
drawTestSequence(canvas, shapeColor, detectorInstallFunc); |
| 214 |
flushGanesh(); |
| 215 |
canvas->translate(SkIntToScalar(kTestRectSize + 1), SkIntToScalar(0)); |
| 216 |
{ |
| 217 |
SkPictureRecorder recorder; |
| 218 |
drawTestSequence(recorder.beginRecording(SkIntToScalar(kTestRectSize), |
| 219 |
SkIntToScalar(kTestRectSize)), |
| 220 |
shapeColor, detectorInstallFunc); |
| 221 |
recorder.finishRecordingAsPicture()->playback(canvas); |
| 222 |
flushGanesh(); |
| 223 |
} |
| 224 |
|
| 225 |
canvas->restore(); |
| 226 |
canvas->translate(SkIntToScalar(0), SkIntToScalar(kTestRectSize + 1)); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
} |