skia2/tests/PinnedImageTest.cpp

136 lines
4.2 KiB
C++
Raw Normal View History

/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
// This is a GPU-backend specific test.
#include "tests/Test.h"
using namespace sk_gpu_test;
#include "tools/gpu/GrContextFactory.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkSurface.h"
#include "include/gpu/GrDirectContext.h"
#include "src/core/SkImagePriv.h"
static bool surface_is_expected_color(SkSurface* surf, const SkImageInfo& ii, SkColor color) {
SkBitmap bm;
bm.allocPixels(ii);
surf->readPixels(bm, 0, 0);
for (int y = 0; y < bm.height(); ++y) {
for (int x = 0; x < bm.width(); ++x) {
if (bm.getColor(x, y) != color) {
return false;
}
}
}
return true;
}
static void basic_test(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
SkBitmap bm;
bm.allocPixels(ii);
SkCanvas bmCanvas(bm);
bmCanvas.clear(SK_ColorRED);
// We start off with the raster image being all red.
sk_sp<SkImage> img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, ii);
SkCanvas* canvas = gpuSurface->getCanvas();
Revert "Reland "Use SkImage_Raster's unique ID to cache textures."" This reverts commit 814c6db4c04de0c757816e1d5b2aff4e1ccada54. Reason for revert: Pending investigatoin of what's going on with Chrome issue 1211203. Original change's description: > Reland "Use SkImage_Raster's unique ID to cache textures." > > This is a reland of 8005007e9879f2778189e6505d70c3b2ce83452c > > Original change's description: > > Use SkImage_Raster's unique ID to cache textures. > > > > SkImages can share SkBitmaps and have different unique IDs/mipmap > > status. Currently we cache the texture version using the bitmap's > > unique ID. Instead use the image's ID so different images produce > > different textures (e.g. mipped and nonmipped). > > > > Bug: skia:11983 > > Change-Id: Ic37564186f675277e5a9de1bcf36b40a19c3a3de > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/407356 > > Commit-Queue: Brian Salomon <bsalomon@google.com> > > Reviewed-by: Robert Phillips <robertphillips@google.com> > > Bug: skia:11983 > Change-Id: I63e9d15ffdf6b6769c9b0b97d9aa30f353e1a3a3 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/409376 > Reviewed-by: Robert Phillips <robertphillips@google.com> > Commit-Queue: Brian Salomon <bsalomon@google.com> # Not skipping CQ checks because original CL landed > 1 day ago. Bug: skia:11983 Change-Id: I4fb2875c2a62d409c3f6ddd41344e83f4e68a375 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/411240 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-05-21 14:57:03 +00:00
// w/o pinning - the gpu draw always reflects the current state of the underlying bitmap
{
canvas->drawImage(img, 0, 0);
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorRED));
bmCanvas.clear(SK_ColorGREEN);
canvas->drawImage(img, 0, 0);
Revert "Reland "Use SkImage_Raster's unique ID to cache textures."" This reverts commit 814c6db4c04de0c757816e1d5b2aff4e1ccada54. Reason for revert: Pending investigatoin of what's going on with Chrome issue 1211203. Original change's description: > Reland "Use SkImage_Raster's unique ID to cache textures." > > This is a reland of 8005007e9879f2778189e6505d70c3b2ce83452c > > Original change's description: > > Use SkImage_Raster's unique ID to cache textures. > > > > SkImages can share SkBitmaps and have different unique IDs/mipmap > > status. Currently we cache the texture version using the bitmap's > > unique ID. Instead use the image's ID so different images produce > > different textures (e.g. mipped and nonmipped). > > > > Bug: skia:11983 > > Change-Id: Ic37564186f675277e5a9de1bcf36b40a19c3a3de > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/407356 > > Commit-Queue: Brian Salomon <bsalomon@google.com> > > Reviewed-by: Robert Phillips <robertphillips@google.com> > > Bug: skia:11983 > Change-Id: I63e9d15ffdf6b6769c9b0b97d9aa30f353e1a3a3 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/409376 > Reviewed-by: Robert Phillips <robertphillips@google.com> > Commit-Queue: Brian Salomon <bsalomon@google.com> # Not skipping CQ checks because original CL landed > 1 day ago. Bug: skia:11983 Change-Id: I4fb2875c2a62d409c3f6ddd41344e83f4e68a375 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/411240 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-05-21 14:57:03 +00:00
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
}
// w/ pinning - the gpu draw is stuck at the pinned state
{
SkImage_pinAsTexture(img.get(), rContext); // pin at blue
canvas->drawImage(img, 0, 0);
Revert "Reland "Use SkImage_Raster's unique ID to cache textures."" This reverts commit 814c6db4c04de0c757816e1d5b2aff4e1ccada54. Reason for revert: Pending investigatoin of what's going on with Chrome issue 1211203. Original change's description: > Reland "Use SkImage_Raster's unique ID to cache textures." > > This is a reland of 8005007e9879f2778189e6505d70c3b2ce83452c > > Original change's description: > > Use SkImage_Raster's unique ID to cache textures. > > > > SkImages can share SkBitmaps and have different unique IDs/mipmap > > status. Currently we cache the texture version using the bitmap's > > unique ID. Instead use the image's ID so different images produce > > different textures (e.g. mipped and nonmipped). > > > > Bug: skia:11983 > > Change-Id: Ic37564186f675277e5a9de1bcf36b40a19c3a3de > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/407356 > > Commit-Queue: Brian Salomon <bsalomon@google.com> > > Reviewed-by: Robert Phillips <robertphillips@google.com> > > Bug: skia:11983 > Change-Id: I63e9d15ffdf6b6769c9b0b97d9aa30f353e1a3a3 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/409376 > Reviewed-by: Robert Phillips <robertphillips@google.com> > Commit-Queue: Brian Salomon <bsalomon@google.com> # Not skipping CQ checks because original CL landed > 1 day ago. Bug: skia:11983 Change-Id: I4fb2875c2a62d409c3f6ddd41344e83f4e68a375 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/411240 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-05-21 14:57:03 +00:00
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
Revert "Reland "Use SkImage_Raster's unique ID to cache textures."" This reverts commit 814c6db4c04de0c757816e1d5b2aff4e1ccada54. Reason for revert: Pending investigatoin of what's going on with Chrome issue 1211203. Original change's description: > Reland "Use SkImage_Raster's unique ID to cache textures." > > This is a reland of 8005007e9879f2778189e6505d70c3b2ce83452c > > Original change's description: > > Use SkImage_Raster's unique ID to cache textures. > > > > SkImages can share SkBitmaps and have different unique IDs/mipmap > > status. Currently we cache the texture version using the bitmap's > > unique ID. Instead use the image's ID so different images produce > > different textures (e.g. mipped and nonmipped). > > > > Bug: skia:11983 > > Change-Id: Ic37564186f675277e5a9de1bcf36b40a19c3a3de > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/407356 > > Commit-Queue: Brian Salomon <bsalomon@google.com> > > Reviewed-by: Robert Phillips <robertphillips@google.com> > > Bug: skia:11983 > Change-Id: I63e9d15ffdf6b6769c9b0b97d9aa30f353e1a3a3 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/409376 > Reviewed-by: Robert Phillips <robertphillips@google.com> > Commit-Queue: Brian Salomon <bsalomon@google.com> # Not skipping CQ checks because original CL landed > 1 day ago. Bug: skia:11983 Change-Id: I4fb2875c2a62d409c3f6ddd41344e83f4e68a375 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/411240 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-05-21 14:57:03 +00:00
bmCanvas.clear(SK_ColorBLUE);
canvas->drawImage(img, 0, 0);
Revert "Reland "Use SkImage_Raster's unique ID to cache textures."" This reverts commit 814c6db4c04de0c757816e1d5b2aff4e1ccada54. Reason for revert: Pending investigatoin of what's going on with Chrome issue 1211203. Original change's description: > Reland "Use SkImage_Raster's unique ID to cache textures." > > This is a reland of 8005007e9879f2778189e6505d70c3b2ce83452c > > Original change's description: > > Use SkImage_Raster's unique ID to cache textures. > > > > SkImages can share SkBitmaps and have different unique IDs/mipmap > > status. Currently we cache the texture version using the bitmap's > > unique ID. Instead use the image's ID so different images produce > > different textures (e.g. mipped and nonmipped). > > > > Bug: skia:11983 > > Change-Id: Ic37564186f675277e5a9de1bcf36b40a19c3a3de > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/407356 > > Commit-Queue: Brian Salomon <bsalomon@google.com> > > Reviewed-by: Robert Phillips <robertphillips@google.com> > > Bug: skia:11983 > Change-Id: I63e9d15ffdf6b6769c9b0b97d9aa30f353e1a3a3 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/409376 > Reviewed-by: Robert Phillips <robertphillips@google.com> > Commit-Queue: Brian Salomon <bsalomon@google.com> # Not skipping CQ checks because original CL landed > 1 day ago. Bug: skia:11983 Change-Id: I4fb2875c2a62d409c3f6ddd41344e83f4e68a375 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/411240 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-05-21 14:57:03 +00:00
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
SkImage_unpinAsTexture(img.get(), rContext);
}
Revert "Reland "Use SkImage_Raster's unique ID to cache textures."" This reverts commit 814c6db4c04de0c757816e1d5b2aff4e1ccada54. Reason for revert: Pending investigatoin of what's going on with Chrome issue 1211203. Original change's description: > Reland "Use SkImage_Raster's unique ID to cache textures." > > This is a reland of 8005007e9879f2778189e6505d70c3b2ce83452c > > Original change's description: > > Use SkImage_Raster's unique ID to cache textures. > > > > SkImages can share SkBitmaps and have different unique IDs/mipmap > > status. Currently we cache the texture version using the bitmap's > > unique ID. Instead use the image's ID so different images produce > > different textures (e.g. mipped and nonmipped). > > > > Bug: skia:11983 > > Change-Id: Ic37564186f675277e5a9de1bcf36b40a19c3a3de > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/407356 > > Commit-Queue: Brian Salomon <bsalomon@google.com> > > Reviewed-by: Robert Phillips <robertphillips@google.com> > > Bug: skia:11983 > Change-Id: I63e9d15ffdf6b6769c9b0b97d9aa30f353e1a3a3 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/409376 > Reviewed-by: Robert Phillips <robertphillips@google.com> > Commit-Queue: Brian Salomon <bsalomon@google.com> # Not skipping CQ checks because original CL landed > 1 day ago. Bug: skia:11983 Change-Id: I4fb2875c2a62d409c3f6ddd41344e83f4e68a375 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/411240 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-05-21 14:57:03 +00:00
// once unpinned local changes will be picked up
{
canvas->drawImage(img, 0, 0);
Revert "Reland "Use SkImage_Raster's unique ID to cache textures."" This reverts commit 814c6db4c04de0c757816e1d5b2aff4e1ccada54. Reason for revert: Pending investigatoin of what's going on with Chrome issue 1211203. Original change's description: > Reland "Use SkImage_Raster's unique ID to cache textures." > > This is a reland of 8005007e9879f2778189e6505d70c3b2ce83452c > > Original change's description: > > Use SkImage_Raster's unique ID to cache textures. > > > > SkImages can share SkBitmaps and have different unique IDs/mipmap > > status. Currently we cache the texture version using the bitmap's > > unique ID. Instead use the image's ID so different images produce > > different textures (e.g. mipped and nonmipped). > > > > Bug: skia:11983 > > Change-Id: Ic37564186f675277e5a9de1bcf36b40a19c3a3de > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/407356 > > Commit-Queue: Brian Salomon <bsalomon@google.com> > > Reviewed-by: Robert Phillips <robertphillips@google.com> > > Bug: skia:11983 > Change-Id: I63e9d15ffdf6b6769c9b0b97d9aa30f353e1a3a3 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/409376 > Reviewed-by: Robert Phillips <robertphillips@google.com> > Commit-Queue: Brian Salomon <bsalomon@google.com> # Not skipping CQ checks because original CL landed > 1 day ago. Bug: skia:11983 Change-Id: I4fb2875c2a62d409c3f6ddd41344e83f4e68a375 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/411240 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-05-21 14:57:03 +00:00
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorBLUE));
}
}
// Deleting the context while there are still pinned images shouldn't result in a crash.
static void cleanup_test(skiatest::Reporter* reporter) {
const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
SkBitmap bm;
bm.allocPixels(ii);
SkCanvas bmCanvas(bm);
bmCanvas.clear(SK_ColorRED);
Reland "Remove SkImage_Base::refPinnedImage(), use refView() instead." This reverts commit 864465765b89691778f109b470f4ed5ca5b5f0e0. Reason for revert: fix: check tiling before call refView() Original change's description: > Revert "Remove SkImage_Base::refPinnedImage(), use refView() instead." > > This reverts commit 9f899ac9edb5ff682a78dab8d9b7b451630dce72. > > Reason for revert: broke ios bot? > > Original change's description: > > Remove SkImage_Base::refPinnedImage(), use refView() instead. > > > > Also use refView() for lazy/raster images as well rather than creating > > a GrTextureProducer class outside the image. > > > > Bug: skia:11208 > > > > Change-Id: Iee628c337bc1b4cfcccd78eaba98589757bb55ab > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/360980 > > Reviewed-by: Robert Phillips <robertphillips@google.com> > > Commit-Queue: Brian Salomon <bsalomon@google.com> > > TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com > > Change-Id: Iab4851f5cefdca853e16c030ec67238c55c69aa2 > Cq-Include-Trybots: luci.skia.skia.primary:Test-iOS-Clang-iPhone6-GPU-PowerVRGX6450-arm64-Release-All-Metal > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/361838 > Commit-Queue: Brian Salomon <bsalomon@google.com> > Reviewed-by: Brian Salomon <bsalomon@google.com> TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com # Not skipping CQ checks because this is a reland. Change-Id: Ib0405bd99a3875a40f6f75b9e785e073ba48605d Cq-Include-Trybots: luci.skia.skia.primary:Canary-Android Reviewed-on: https://skia-review.googlesource.com/c/skia/+/362036 Reviewed-by: Brian Salomon <bsalomon@google.com> Reviewed-by: Robert Phillips <robertphillips@google.com> Reviewed-by: Greg Daniel <egdaniel@google.com>
2021-02-01 18:14:37 +00:00
GrMockOptions options;
sk_sp<GrDirectContext> mockContext = GrDirectContext::MakeMock(&options);
for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
{
sk_sp<SkImage> img;
GrDirectContext* dContext = nullptr;
{
GrContextFactory testFactory;
ContextInfo info = testFactory.getContextInfo(ctxType);
dContext = info.directContext();
if (!dContext) {
continue;
}
img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
if (!SkImage_pinAsTexture(img.get(), dContext)) {
continue;
}
Reland "Remove SkImage_Base::refPinnedImage(), use refView() instead." This reverts commit 864465765b89691778f109b470f4ed5ca5b5f0e0. Reason for revert: fix: check tiling before call refView() Original change's description: > Revert "Remove SkImage_Base::refPinnedImage(), use refView() instead." > > This reverts commit 9f899ac9edb5ff682a78dab8d9b7b451630dce72. > > Reason for revert: broke ios bot? > > Original change's description: > > Remove SkImage_Base::refPinnedImage(), use refView() instead. > > > > Also use refView() for lazy/raster images as well rather than creating > > a GrTextureProducer class outside the image. > > > > Bug: skia:11208 > > > > Change-Id: Iee628c337bc1b4cfcccd78eaba98589757bb55ab > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/360980 > > Reviewed-by: Robert Phillips <robertphillips@google.com> > > Commit-Queue: Brian Salomon <bsalomon@google.com> > > TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com > > Change-Id: Iab4851f5cefdca853e16c030ec67238c55c69aa2 > Cq-Include-Trybots: luci.skia.skia.primary:Test-iOS-Clang-iPhone6-GPU-PowerVRGX6450-arm64-Release-All-Metal > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/361838 > Commit-Queue: Brian Salomon <bsalomon@google.com> > Reviewed-by: Brian Salomon <bsalomon@google.com> TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com # Not skipping CQ checks because this is a reland. Change-Id: Ib0405bd99a3875a40f6f75b9e785e073ba48605d Cq-Include-Trybots: luci.skia.skia.primary:Canary-Android Reviewed-on: https://skia-review.googlesource.com/c/skia/+/362036 Reviewed-by: Brian Salomon <bsalomon@google.com> Reviewed-by: Robert Phillips <robertphillips@google.com> Reviewed-by: Greg Daniel <egdaniel@google.com>
2021-02-01 18:14:37 +00:00
// Pinning on a second context should be blocked.
REPORTER_ASSERT(reporter, !SkImage_pinAsTexture(img.get(), mockContext.get()));
}
// The context used to pin the image is gone at this point!
// "context" isn't technically used in this call but it can't be null!
// We don't really want to support this use case but it currently happens.
SkImage_unpinAsTexture(img.get(), dContext);
}
}
}
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PinnedImageTest, reporter, ctxInfo) {
basic_test(reporter, ctxInfo.directContext());
cleanup_test(reporter);
}