2012-06-22 21:01:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 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"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkImageFilter.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPoint3.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/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2019-07-11 20:32:53 +00:00
|
|
|
#include "tools/timer/TimeUtils.h"
|
2012-06-22 21:01:23 +00:00
|
|
|
|
2021-01-21 23:43:58 +00:00
|
|
|
#define WIDTH 660
|
Implement approx-match support in image filter saveLayer() offscreen.
Currently, the GPU-side image filter implementation creates
exact-match textures for the offscreen backing stores for
saveLayer(). This is because several filters have GPU
implementations which depend on the texture coordinates
being 0..1.
The fix is three-fold:
1) Store the actual requested size in the SkGpuDevice, so
that when wrapping it in an SkBitmap for passing to
filterImage(), we can give it the original size.
2) Fix the filters (SkMagnifierImageFilter,
SkLightingImageFilter, SkMatrixConvolutionImageFilter,
SkMatrixImageFilter) whose GPU implementation depends on
0..1 texture coordinates.
3) Remove the exception for GPU-side image filters in
SkCanvas::internalSaveLayer().
For the lighting filters, there were two bugs which were
cancelling each other out: the sobel filter matrix was
being computed upside down, but then we'd negate the
resulting normal. This worked fine in the exact-match case,
but in the approx-match case we'd sample garbage along
the edge pixels. Also, we never implemented the edge pixels
according to spec in the GPU case. It requires a
different fragment shader for each edge of the nine-patch,
which meant we couldn't use asFragmentProcessor(), and had
to implement the drawing via a filterImageGPU() override.
In order to avoid polluting the public API, I inserted a
new base class, SkLightingImageFilterInternal above
Sk[Diffuse|Specular]LightingImageFilter to handle the
implementation.
For the SkMatrixConvolutionImageFilter, it seems the
GLSL clamp() function occasionally returns values outside
the clamped range, resulting in access of garbage
texels even in GL_NEAREST. The fix here is to clamp to a
rect inset by half a texel. There was also a bug in
the unpremultiply step when fConvolveAlpha is false.
For SkMatrixImageFilter, the fix was to make the generic
draw path be more careful about when to use texture domain.
If the bitmap already has a texture, use texture domain
if the srcRect is smaller than the entire texture (not
the entire bitmap).
N.B.: this change will cause some minor pixel diffs in the
GPU results of the following GMs (and possibly more):
matriximagefilter, matrixconvolution, imagefiltersscaled,
lighting, imagemagnifier, filterfastbounds,
complexclip_aa_Layer_invert, complexclip_aa_layer,
complexclip_bw_layer_invert, complexclip_bw_layer.
BUG=skia:3532
Committed: https://skia.googlesource.com/skia/+/b97dafefe63ea0a1bbce8e8b209f4920983fb8b9
Committed: https://skia.googlesource.com/skia/+/f5f8518fe0bbd2703e4ffc1b11ad7b4312ff7641
Committed: https://skia.googlesource.com/skia/+/46112cf2a7c7307f1c9eebb5f881cbda15aa460c
Review URL: https://codereview.chromium.org/1034733002
2015-04-02 11:54:56 +00:00
|
|
|
#define HEIGHT 660
|
2012-06-22 21:01:23 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class ImageLightingGM : public GM {
|
|
|
|
public:
|
2016-04-08 20:35:14 +00:00
|
|
|
ImageLightingGM()
|
|
|
|
: fAzimuth(SkIntToScalar(kStartAzimuth)) {
|
2012-06-22 21:01:23 +00:00
|
|
|
this->setBGColor(0xFF000000);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-06-22 21:01:23 +00:00
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-06-22 21:01:23 +00:00
|
|
|
return SkString("lighting");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-02-13 17:14:46 +00:00
|
|
|
return SkISize::Make(WIDTH, HEIGHT);
|
2012-06-22 21:01:23 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 16:27:50 +00:00
|
|
|
void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
|
|
|
|
canvas->save();
|
2013-08-27 21:37:01 +00:00
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
2021-01-25 00:49:21 +00:00
|
|
|
canvas->clipIRect(fBitmap.bounds());
|
|
|
|
canvas->drawImage(fBitmap.asImage(), 0, 0, SkSamplingOptions(), &paint);
|
2013-01-14 16:27:50 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2019-03-20 16:12:10 +00:00
|
|
|
fBitmap = ToolUtils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
|
2015-09-03 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2018-08-16 14:17:03 +00:00
|
|
|
canvas->clear(0xFF101010);
|
2012-10-29 19:47:06 +00:00
|
|
|
SkPaint checkPaint;
|
2018-08-16 14:17:03 +00:00
|
|
|
checkPaint.setColor(0xFF202020);
|
2012-10-29 19:47:06 +00:00
|
|
|
for (int y = 0; y < HEIGHT; y += 16) {
|
|
|
|
for (int x = 0; x < WIDTH; x += 16) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
2019-04-02 14:59:28 +00:00
|
|
|
SkScalar sinAzimuth = SkScalarSin(SkDegreesToRadians(fAzimuth)),
|
|
|
|
cosAzimuth = SkScalarCos(SkDegreesToRadians(fAzimuth));
|
2016-04-08 20:35:14 +00:00
|
|
|
|
|
|
|
SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0);
|
|
|
|
SkPoint3 spotLocation = SkPoint3::Make(spotTarget.fX + 70.7214f * cosAzimuth,
|
|
|
|
spotTarget.fY + 70.7214f * sinAzimuth,
|
|
|
|
spotTarget.fZ + SkIntToScalar(20));
|
2021-01-21 23:43:58 +00:00
|
|
|
SkScalar spotExponent1 = SK_Scalar1;
|
|
|
|
SkScalar spotExponent10 = SkIntToScalar(10);
|
|
|
|
SkScalar cutoffAngleSmall = SkIntToScalar(15);
|
|
|
|
SkScalar cutoffAngleNone = SkIntToScalar(180);
|
2016-04-08 20:35:14 +00:00
|
|
|
|
|
|
|
SkPoint3 pointLocation = SkPoint3::Make(spotTarget.fX + 50 * cosAzimuth,
|
|
|
|
spotTarget.fY + 50 * sinAzimuth,
|
|
|
|
SkIntToScalar(10));
|
2012-06-22 21:01:23 +00:00
|
|
|
SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
|
2016-04-08 20:35:14 +00:00
|
|
|
|
2017-02-14 15:59:19 +00:00
|
|
|
SkPoint3 distantDirection = SkPoint3::Make(cosAzimuth * SkScalarCos(elevationRad),
|
|
|
|
sinAzimuth * SkScalarCos(elevationRad),
|
2015-07-13 20:16:44 +00:00
|
|
|
SkScalarSin(elevationRad));
|
2012-06-22 21:01:23 +00:00
|
|
|
SkScalar kd = SkIntToScalar(2);
|
|
|
|
SkScalar ks = SkIntToScalar(1);
|
|
|
|
SkScalar shininess = SkIntToScalar(8);
|
|
|
|
SkScalar surfaceScale = SkIntToScalar(1);
|
2021-01-21 23:43:58 +00:00
|
|
|
SkScalar surfaceScaleSmall = 0.1f;
|
|
|
|
SkColor greenYellow = SkColorSetARGB(255, 173, 255, 47);
|
2012-06-22 21:01:23 +00:00
|
|
|
SkPaint paint;
|
2013-07-26 00:10:07 +00:00
|
|
|
|
2019-08-02 19:21:23 +00:00
|
|
|
SkIRect cropRect = SkIRect::MakeXYWH(20, 10, 60, 65);
|
|
|
|
SkIRect fullSizeCropRect = SkIRect::MakeXYWH(0, 0, 100, 100);
|
|
|
|
sk_sp<SkImageFilter> noopCropped(SkImageFilters::Offset(0, 0, nullptr, &cropRect));
|
2013-07-26 00:10:07 +00:00
|
|
|
|
|
|
|
int y = 0;
|
Implement approx-match support in image filter saveLayer() offscreen.
Currently, the GPU-side image filter implementation creates
exact-match textures for the offscreen backing stores for
saveLayer(). This is because several filters have GPU
implementations which depend on the texture coordinates
being 0..1.
The fix is three-fold:
1) Store the actual requested size in the SkGpuDevice, so
that when wrapping it in an SkBitmap for passing to
filterImage(), we can give it the original size.
2) Fix the filters (SkMagnifierImageFilter,
SkLightingImageFilter, SkMatrixConvolutionImageFilter,
SkMatrixImageFilter) whose GPU implementation depends on
0..1 texture coordinates.
3) Remove the exception for GPU-side image filters in
SkCanvas::internalSaveLayer().
For the lighting filters, there were two bugs which were
cancelling each other out: the sobel filter matrix was
being computed upside down, but then we'd negate the
resulting normal. This worked fine in the exact-match case,
but in the approx-match case we'd sample garbage along
the edge pixels. Also, we never implemented the edge pixels
according to spec in the GPU case. It requires a
different fragment shader for each edge of the nine-patch,
which meant we couldn't use asFragmentProcessor(), and had
to implement the drawing via a filterImageGPU() override.
In order to avoid polluting the public API, I inserted a
new base class, SkLightingImageFilterInternal above
Sk[Diffuse|Specular]LightingImageFilter to handle the
implementation.
For the SkMatrixConvolutionImageFilter, it seems the
GLSL clamp() function occasionally returns values outside
the clamped range, resulting in access of garbage
texels even in GL_NEAREST. The fix here is to clamp to a
rect inset by half a texel. There was also a bug in
the unpremultiply step when fConvolveAlpha is false.
For SkMatrixImageFilter, the fix was to make the generic
draw path be more careful about when to use texture domain.
If the bitmap already has a texture, use texture domain
if the srcRect is smaller than the entire texture (not
the entire bitmap).
N.B.: this change will cause some minor pixel diffs in the
GPU results of the following GMs (and possibly more):
matriximagefilter, matrixconvolution, imagefiltersscaled,
lighting, imagemagnifier, filterfastbounds,
complexclip_aa_Layer_invert, complexclip_aa_layer,
complexclip_bw_layer_invert, complexclip_bw_layer.
BUG=skia:3532
Committed: https://skia.googlesource.com/skia/+/b97dafefe63ea0a1bbce8e8b209f4920983fb8b9
Committed: https://skia.googlesource.com/skia/+/f5f8518fe0bbd2703e4ffc1b11ad7b4312ff7641
Committed: https://skia.googlesource.com/skia/+/46112cf2a7c7307f1c9eebb5f881cbda15aa460c
Review URL: https://codereview.chromium.org/1034733002
2015-04-02 11:54:56 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2019-08-02 19:21:23 +00:00
|
|
|
const SkIRect* cr = (i == 1) ? &cropRect : (i == 2) ? &fullSizeCropRect : nullptr;
|
2016-04-08 23:28:09 +00:00
|
|
|
sk_sp<SkImageFilter> input = (i == 2) ? noopCropped : nullptr;
|
2021-01-21 23:43:58 +00:00
|
|
|
// Basic point, distant and spot lights with diffuse lighting
|
2019-08-02 19:21:23 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::PointLitDiffuse(
|
2021-01-21 23:43:58 +00:00
|
|
|
pointLocation, SK_ColorWHITE, surfaceScale, kd, input, cr));
|
2016-04-08 23:28:09 +00:00
|
|
|
drawClippedBitmap(canvas, paint, 0, y);
|
|
|
|
|
2019-08-02 19:21:23 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::DistantLitDiffuse(
|
2021-01-21 23:43:58 +00:00
|
|
|
distantDirection, SK_ColorWHITE, surfaceScale, kd, input, cr));
|
2013-07-26 00:10:07 +00:00
|
|
|
drawClippedBitmap(canvas, paint, 110, y);
|
2013-08-27 21:37:01 +00:00
|
|
|
|
2019-08-02 19:21:23 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::SpotLitDiffuse(
|
2021-01-21 23:43:58 +00:00
|
|
|
spotLocation, spotTarget, spotExponent1, cutoffAngleSmall, SK_ColorWHITE,
|
|
|
|
surfaceScale, kd, input, cr));
|
2013-07-26 00:10:07 +00:00
|
|
|
drawClippedBitmap(canvas, paint, 220, y);
|
|
|
|
|
2021-01-21 23:43:58 +00:00
|
|
|
// Spot light with no angle cutoff
|
|
|
|
paint.setImageFilter(SkImageFilters::SpotLitDiffuse(
|
|
|
|
spotLocation, spotTarget, spotExponent10, cutoffAngleNone, SK_ColorWHITE,
|
|
|
|
surfaceScale, kd, input, cr));
|
|
|
|
drawClippedBitmap(canvas, paint, 330, y);
|
|
|
|
|
|
|
|
// Spot light with falloff exponent
|
|
|
|
paint.setImageFilter(SkImageFilters::SpotLitDiffuse(
|
|
|
|
spotLocation, spotTarget, spotExponent1, cutoffAngleNone, SK_ColorWHITE,
|
|
|
|
surfaceScaleSmall, kd, input, cr));
|
|
|
|
drawClippedBitmap(canvas, paint, 440, y);
|
|
|
|
|
|
|
|
// Large constant to show oversaturation
|
|
|
|
paint.setImageFilter(SkImageFilters::DistantLitDiffuse(
|
|
|
|
distantDirection, greenYellow, surfaceScale, 4.f * kd, input, cr));
|
|
|
|
drawClippedBitmap(canvas, paint, 550, y);
|
|
|
|
|
2013-07-26 00:10:07 +00:00
|
|
|
y += 110;
|
2013-08-27 21:37:01 +00:00
|
|
|
|
2021-01-21 23:43:58 +00:00
|
|
|
// Basic point, distant and spot lights with specular lighting
|
2019-08-02 19:21:23 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::PointLitSpecular(
|
2021-01-21 23:43:58 +00:00
|
|
|
pointLocation, SK_ColorWHITE, surfaceScale, ks, shininess, input, cr));
|
2016-04-08 23:28:09 +00:00
|
|
|
drawClippedBitmap(canvas, paint, 0, y);
|
|
|
|
|
2019-08-02 19:21:23 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::DistantLitSpecular(
|
2021-01-21 23:43:58 +00:00
|
|
|
distantDirection, SK_ColorWHITE, surfaceScale, ks, shininess, input, cr));
|
2013-07-26 00:10:07 +00:00
|
|
|
drawClippedBitmap(canvas, paint, 110, y);
|
2013-08-27 21:37:01 +00:00
|
|
|
|
2019-08-02 19:21:23 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::SpotLitSpecular(
|
2021-01-21 23:43:58 +00:00
|
|
|
spotLocation, spotTarget, spotExponent1, cutoffAngleSmall, SK_ColorWHITE,
|
|
|
|
surfaceScale, ks, shininess, input, cr));
|
2013-07-26 00:10:07 +00:00
|
|
|
drawClippedBitmap(canvas, paint, 220, y);
|
2013-08-27 21:37:01 +00:00
|
|
|
|
2021-01-21 23:43:58 +00:00
|
|
|
// Spot light with no angle cutoff
|
|
|
|
paint.setImageFilter(SkImageFilters::SpotLitSpecular(
|
|
|
|
spotLocation, spotTarget, spotExponent10, cutoffAngleNone, SK_ColorWHITE,
|
|
|
|
surfaceScale, ks, shininess, input, cr));
|
|
|
|
drawClippedBitmap(canvas, paint, 330, y);
|
|
|
|
|
|
|
|
// Spot light with falloff exponent
|
|
|
|
paint.setImageFilter(SkImageFilters::SpotLitSpecular(
|
|
|
|
spotLocation, spotTarget, spotExponent1, cutoffAngleNone, SK_ColorWHITE,
|
|
|
|
surfaceScaleSmall, ks, shininess, input, cr));
|
|
|
|
drawClippedBitmap(canvas, paint, 440, y);
|
|
|
|
|
|
|
|
// Large constant to show oversaturation
|
|
|
|
paint.setImageFilter(SkImageFilters::DistantLitSpecular(
|
|
|
|
distantDirection, greenYellow, surfaceScale, 4.f * ks, shininess, input, cr));
|
|
|
|
drawClippedBitmap(canvas, paint, 550, y);
|
|
|
|
|
2013-07-26 00:10:07 +00:00
|
|
|
y += 110;
|
|
|
|
}
|
2012-06-22 21:01:23 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kDesiredDurationSecs = 15.0f;
|
2016-04-08 20:35:14 +00:00
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
fAzimuth = kStartAzimuth + TimeUtils::Scaled(1e-9 * nanos, 360.0f/kDesiredDurationSecs, 360.0f);
|
2016-04-08 20:35:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-22 21:01:23 +00:00
|
|
|
private:
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kStartAzimuth = 225;
|
2016-04-08 20:35:14 +00:00
|
|
|
|
2012-06-22 21:01:23 +00:00
|
|
|
SkBitmap fBitmap;
|
2016-04-08 20:35:14 +00:00
|
|
|
SkScalar fAzimuth;
|
2015-09-03 20:32:33 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2012-06-22 21:01:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new ImageLightingGM;)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|