2015-07-16 14:01:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2017-04-03 21:49:05 +00:00
|
|
|
#include "SkBitmap.h"
|
2018-03-09 19:41:36 +00:00
|
|
|
#include "SkBlendMode.h"
|
2017-04-03 21:49:05 +00:00
|
|
|
#include "SkCanvas.h"
|
2014-02-25 21:31:03 +00:00
|
|
|
#include "SkColor.h"
|
2018-03-09 19:41:36 +00:00
|
|
|
#include "SkColorSpace.h"
|
|
|
|
#include "SkImageInfo.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPoint.h"
|
|
|
|
#include "SkRect.h"
|
|
|
|
#include "SkRefCnt.h"
|
2017-04-03 21:49:05 +00:00
|
|
|
#include "SkSurface.h"
|
2018-03-09 19:41:36 +00:00
|
|
|
#include "SkTypes.h"
|
2017-04-03 21:49:05 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
2018-03-09 19:41:36 +00:00
|
|
|
#include "GrBackendSurface.h"
|
2017-04-03 21:49:05 +00:00
|
|
|
#include "GrContext.h"
|
2018-03-09 19:41:36 +00:00
|
|
|
#include "GrContextFactory.h"
|
2017-04-03 21:49:05 +00:00
|
|
|
#include "GrContextPriv.h"
|
|
|
|
#include "GrResourceProvider.h"
|
|
|
|
#include "GrTexture.h"
|
2018-03-09 19:41:36 +00:00
|
|
|
#include "GrTypes.h"
|
2015-11-17 16:39:01 +00:00
|
|
|
|
2018-03-09 19:41:36 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2015-11-17 16:39:01 +00:00
|
|
|
struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; };
|
|
|
|
|
|
|
|
static bool acceptable(const Results& r) {
|
|
|
|
#if 0
|
|
|
|
SkDebugf("%d diffs, %d at 0x00, %d at 0xff, %d off by 1, all out of 65536\n",
|
|
|
|
r.diffs, r.diffs_0x00, r.diffs_0xff, r.diffs_by_1);
|
|
|
|
#endif
|
|
|
|
return r.diffs_by_1 == r.diffs // never off by more than 1
|
|
|
|
&& r.diffs_0x00 == 0 // transparent must stay transparent
|
|
|
|
&& r.diffs_0xff == 0; // opaque must stay opaque
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Fn>
|
|
|
|
static Results test(Fn&& multiply) {
|
|
|
|
Results r = { 0,0,0,0 };
|
|
|
|
for (int x = 0; x < 256; x++) {
|
|
|
|
for (int y = 0; y < 256; y++) {
|
|
|
|
int p = multiply(x, y),
|
|
|
|
ideal = (x*y+127)/255;
|
|
|
|
if (p != ideal) {
|
|
|
|
r.diffs++;
|
|
|
|
if (x == 0x00 || y == 0x00) { r.diffs_0x00++; }
|
|
|
|
if (x == 0xff || y == 0xff) { r.diffs_0xff++; }
|
|
|
|
if (SkTAbs(ideal - p) == 1) { r.diffs_by_1++; }
|
2014-02-25 21:31:03 +00:00
|
|
|
}
|
2015-11-17 16:39:01 +00:00
|
|
|
}}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(Blend_byte_multiply, r) {
|
|
|
|
// These are all temptingly close but fundamentally broken.
|
|
|
|
int (*broken[])(int, int) = {
|
|
|
|
[](int x, int y) { return (x*y)>>8; },
|
|
|
|
[](int x, int y) { return (x*y+128)>>8; },
|
|
|
|
[](int x, int y) { y += y>>7; return (x*y)>>8; },
|
|
|
|
};
|
|
|
|
for (auto multiply : broken) { REPORTER_ASSERT(r, !acceptable(test(multiply))); }
|
|
|
|
|
|
|
|
// These are fine to use, but not perfect.
|
|
|
|
int (*fine[])(int, int) = {
|
|
|
|
[](int x, int y) { return (x*y+x)>>8; },
|
|
|
|
[](int x, int y) { return (x*y+y)>>8; },
|
|
|
|
[](int x, int y) { return (x*y+255)>>8; },
|
|
|
|
[](int x, int y) { y += y>>7; return (x*y+128)>>8; },
|
|
|
|
};
|
|
|
|
for (auto multiply : fine) { REPORTER_ASSERT(r, acceptable(test(multiply))); }
|
|
|
|
|
|
|
|
// These are pefect.
|
|
|
|
int (*perfect[])(int, int) = {
|
|
|
|
[](int x, int y) { return (x*y+127)/255; }, // Duh.
|
|
|
|
[](int x, int y) { int p = (x*y+128); return (p+(p>>8))>>8; },
|
|
|
|
[](int x, int y) { return ((x*y+128)*257)>>16; },
|
|
|
|
};
|
|
|
|
for (auto multiply : perfect) { REPORTER_ASSERT(r, test(multiply).diffs == 0); }
|
2014-02-25 21:31:03 +00:00
|
|
|
}
|
2017-04-03 21:49:05 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
|
2017-12-19 18:15:02 +00:00
|
|
|
GrContext* context, int sampleCnt, int width, int height, SkColorType colorType,
|
|
|
|
GrPixelConfig config, GrSurfaceOrigin origin,
|
2017-04-03 21:49:05 +00:00
|
|
|
sk_sp<GrTexture>* backingSurface) {
|
|
|
|
GrSurfaceDesc backingDesc;
|
2017-04-07 14:08:53 +00:00
|
|
|
backingDesc.fFlags = kRenderTarget_GrSurfaceFlag;
|
2017-04-06 21:17:15 +00:00
|
|
|
backingDesc.fWidth = width;
|
2017-04-07 14:08:53 +00:00
|
|
|
backingDesc.fHeight = height;
|
2017-04-03 21:49:05 +00:00
|
|
|
backingDesc.fConfig = config;
|
2017-04-07 14:08:53 +00:00
|
|
|
backingDesc.fSampleCnt = sampleCnt;
|
2017-04-03 21:49:05 +00:00
|
|
|
|
2019-02-04 18:26:26 +00:00
|
|
|
auto resourceProvider = context->priv().resourceProvider();
|
2018-01-16 20:07:54 +00:00
|
|
|
|
|
|
|
*backingSurface = resourceProvider->createTexture(backingDesc, SkBudgeted::kNo);
|
2017-04-07 14:08:53 +00:00
|
|
|
if (!(*backingSurface)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-04-03 21:49:05 +00:00
|
|
|
|
2017-12-13 20:00:45 +00:00
|
|
|
GrBackendTexture backendTex = (*backingSurface)->getBackendTexture();
|
|
|
|
|
2017-04-03 21:49:05 +00:00
|
|
|
sk_sp<SkSurface> surface =
|
Revert "Revert "Plumb GrBackendTexture throughout skia.""
This reverts commit 7fa5c31c2c9af834bee66d5fcf476e250076c8d6.
Reason for revert: Relanding this change now that other fixes have landed.
Original change's description:
> Revert "Plumb GrBackendTexture throughout skia."
>
> This reverts commit 7da62b9059f3c1d31624a0e4da96ee5f908f9c12.
>
> Reason for revert: fix android roll
>
> Original change's description:
> > Plumb GrBackendTexture throughout skia.
> >
> > Bug: skia:
> > Change-Id: I1bae6768ee7229818a83ba608035a1f7867e6875
> > Reviewed-on: https://skia-review.googlesource.com/13645
> > Commit-Queue: Greg Daniel <egdaniel@google.com>
> > Reviewed-by: Robert Phillips <robertphillips@google.com>
> >
>
> TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com,brianosman@google.com,reviews@skia.org,stani@google.com
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
>
> Change-Id: I5cb8763cc837c83ebc6d10366fe2dd3efe35fb89
> Reviewed-on: https://skia-review.googlesource.com/13773
> Reviewed-by: Stan Iliev <stani@google.com>
> Commit-Queue: Stan Iliev <stani@google.com>
>
TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com,reviews@skia.org,brianosman@google.com,stani@google.com
# Not skipping CQ checks because original CL landed > 1 day ago.
Change-Id: I92bc074e4fe37fa5c83186afadc472c03802e8f2
Reviewed-on: https://skia-review.googlesource.com/13975
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
2017-04-20 16:41:55 +00:00
|
|
|
SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex, origin,
|
2017-12-19 18:15:02 +00:00
|
|
|
sampleCnt, colorType, nullptr, nullptr);
|
Revert "Revert "Plumb GrBackendTexture throughout skia.""
This reverts commit 7fa5c31c2c9af834bee66d5fcf476e250076c8d6.
Reason for revert: Relanding this change now that other fixes have landed.
Original change's description:
> Revert "Plumb GrBackendTexture throughout skia."
>
> This reverts commit 7da62b9059f3c1d31624a0e4da96ee5f908f9c12.
>
> Reason for revert: fix android roll
>
> Original change's description:
> > Plumb GrBackendTexture throughout skia.
> >
> > Bug: skia:
> > Change-Id: I1bae6768ee7229818a83ba608035a1f7867e6875
> > Reviewed-on: https://skia-review.googlesource.com/13645
> > Commit-Queue: Greg Daniel <egdaniel@google.com>
> > Reviewed-by: Robert Phillips <robertphillips@google.com>
> >
>
> TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com,brianosman@google.com,reviews@skia.org,stani@google.com
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
>
> Change-Id: I5cb8763cc837c83ebc6d10366fe2dd3efe35fb89
> Reviewed-on: https://skia-review.googlesource.com/13773
> Reviewed-by: Stan Iliev <stani@google.com>
> Commit-Queue: Stan Iliev <stani@google.com>
>
TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com,reviews@skia.org,brianosman@google.com,stani@google.com
# Not skipping CQ checks because original CL landed > 1 day ago.
Change-Id: I92bc074e4fe37fa5c83186afadc472c03802e8f2
Reviewed-on: https://skia-review.googlesource.com/13975
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
2017-04-20 16:41:55 +00:00
|
|
|
|
2017-04-03 21:49:05 +00:00
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests blending to a surface with no texture available.
|
|
|
|
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture, reporter, ctxInfo) {
|
|
|
|
GrContext* context = ctxInfo.grContext();
|
|
|
|
const int kWidth = 10;
|
|
|
|
const int kHeight = 10;
|
|
|
|
const GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig;
|
|
|
|
const SkColorType kColorType = kRGBA_8888_SkColorType;
|
|
|
|
|
|
|
|
// Build our test cases:
|
|
|
|
struct RectAndSamplePoint {
|
|
|
|
SkRect rect;
|
|
|
|
SkIPoint outPoint;
|
|
|
|
SkIPoint inPoint;
|
|
|
|
} allRectsAndPoints[3] = {
|
|
|
|
{SkRect::MakeXYWH(0, 0, 5, 5), SkIPoint::Make(7, 7), SkIPoint::Make(2, 2)},
|
2017-04-04 20:42:10 +00:00
|
|
|
{SkRect::MakeXYWH(2, 2, 5, 5), SkIPoint::Make(1, 1), SkIPoint::Make(4, 4)},
|
2017-04-03 21:49:05 +00:00
|
|
|
{SkRect::MakeXYWH(5, 5, 5, 5), SkIPoint::Make(2, 2), SkIPoint::Make(7, 7)},
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestCase {
|
2017-04-07 14:08:53 +00:00
|
|
|
RectAndSamplePoint fRectAndPoints;
|
|
|
|
SkRect fClip;
|
|
|
|
int fSampleCnt;
|
|
|
|
GrSurfaceOrigin fOrigin;
|
2017-04-03 21:49:05 +00:00
|
|
|
};
|
|
|
|
std::vector<TestCase> testCases;
|
|
|
|
|
2017-04-07 14:08:53 +00:00
|
|
|
for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
|
2018-02-03 01:32:49 +00:00
|
|
|
for (int sampleCnt : {1, 4}) {
|
2017-04-07 14:08:53 +00:00
|
|
|
for (auto rectAndPoints : allRectsAndPoints) {
|
|
|
|
for (auto clip : {SkRect::MakeXYWH(0, 0, 10, 10), SkRect::MakeXYWH(1, 1, 8, 8)}) {
|
|
|
|
testCases.push_back({rectAndPoints, clip, sampleCnt, origin});
|
|
|
|
}
|
2017-04-04 20:42:10 +00:00
|
|
|
}
|
2017-04-03 21:49:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run each test case:
|
|
|
|
for (auto testCase : testCases) {
|
2017-04-07 14:08:53 +00:00
|
|
|
int sampleCnt = testCase.fSampleCnt;
|
|
|
|
SkRect paintRect = testCase.fRectAndPoints.rect;
|
|
|
|
SkIPoint outPoint = testCase.fRectAndPoints.outPoint;
|
|
|
|
SkIPoint inPoint = testCase.fRectAndPoints.inPoint;
|
|
|
|
GrSurfaceOrigin origin = testCase.fOrigin;
|
2017-04-03 21:49:05 +00:00
|
|
|
|
|
|
|
sk_sp<GrTexture> backingSurface;
|
|
|
|
// BGRA forces a framebuffer blit on ES2.
|
|
|
|
sk_sp<SkSurface> surface = create_gpu_surface_backend_texture_as_render_target(
|
2017-12-19 18:15:02 +00:00
|
|
|
context, sampleCnt, kWidth, kHeight, kColorType, kConfig, origin, &backingSurface);
|
2017-04-03 21:49:05 +00:00
|
|
|
|
2018-02-03 01:32:49 +00:00
|
|
|
if (!surface && sampleCnt > 1) {
|
2017-04-03 21:49:05 +00:00
|
|
|
// Some platforms don't support MSAA.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, !!surface);
|
|
|
|
|
|
|
|
// Fill our canvas with 0xFFFF80
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
2017-04-07 14:08:53 +00:00
|
|
|
canvas->clipRect(testCase.fClip, false);
|
2017-04-03 21:49:05 +00:00
|
|
|
SkPaint black_paint;
|
|
|
|
black_paint.setColor(SkColorSetRGB(0xFF, 0xFF, 0x80));
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(0, 0, kWidth, kHeight), black_paint);
|
|
|
|
|
|
|
|
// Blend 2x2 pixels at 5,5 with 0x80FFFF. Use multiply blend mode as this will trigger
|
|
|
|
// a copy of the destination.
|
|
|
|
SkPaint white_paint;
|
|
|
|
white_paint.setColor(SkColorSetRGB(0x80, 0xFF, 0xFF));
|
|
|
|
white_paint.setBlendMode(SkBlendMode::kMultiply);
|
|
|
|
canvas->drawRect(paintRect, white_paint);
|
|
|
|
|
|
|
|
// Read the result into a bitmap.
|
|
|
|
SkBitmap bitmap;
|
|
|
|
REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(
|
|
|
|
kWidth, kHeight, kColorType, kPremul_SkAlphaType)));
|
|
|
|
REPORTER_ASSERT(
|
|
|
|
reporter,
|
|
|
|
surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0));
|
|
|
|
|
|
|
|
// Check the in/out pixels.
|
|
|
|
REPORTER_ASSERT(reporter, bitmap.getColor(outPoint.x(), outPoint.y()) ==
|
|
|
|
SkColorSetRGB(0xFF, 0xFF, 0x80));
|
|
|
|
REPORTER_ASSERT(reporter, bitmap.getColor(inPoint.x(), inPoint.y()) ==
|
|
|
|
SkColorSetRGB(0x80, 0xFF, 0x80));
|
|
|
|
|
|
|
|
// Clean up - surface depends on backingSurface and must be released first.
|
|
|
|
surface.reset();
|
|
|
|
backingSurface.reset();
|
|
|
|
}
|
|
|
|
}
|