blob: a59c8eedac200902d5a8abe224a3a1c8d4f9035a [file] [log] [blame]
robertphillipsb6c65e92016-02-04 10:52:42 -08001/*
2* Copyright 2016 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
Florin Malitaab244f02017-05-03 19:16:58 +00008#include "SkBitmap.h"
robertphillipsb6c65e92016-02-04 10:52:42 -08009#include "SkCanvas.h"
10#include "SkSpecialImage.h"
11#include "SkSpecialSurface.h"
12#include "Test.h"
13
Brian Salomonc7fe0f72018-05-11 10:14:21 -040014#include "GrCaps.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080015#include "GrContext.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040016#include "GrContextPriv.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080017#include "SkGr.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080018
19class TestingSpecialSurfaceAccess {
20public:
21 static const SkIRect& Subset(const SkSpecialSurface* surf) {
22 return surf->subset();
23 }
robertphillipsb6c65e92016-02-04 10:52:42 -080024};
25
26// Both 'kSmallerSize' and 'kFullSize' need to be a non-power-of-2 to exercise
27// the gpu's loose fit behavior
28static const int kSmallerSize = 10;
29static const int kPad = 5;
30static const int kFullSize = kSmallerSize + 2 * kPad;
31
32// Exercise the public API of SkSpecialSurface (e.g., getCanvas, newImageSnapshot)
robertphillips37bd7c32016-03-17 14:31:39 -070033static void test_surface(const sk_sp<SkSpecialSurface>& surf,
34 skiatest::Reporter* reporter,
35 int offset) {
robertphillipsb6c65e92016-02-04 10:52:42 -080036
robertphillips37bd7c32016-03-17 14:31:39 -070037 const SkIRect surfSubset = TestingSpecialSurfaceAccess::Subset(surf.get());
robertphillipsb6c65e92016-02-04 10:52:42 -080038 REPORTER_ASSERT(reporter, offset == surfSubset.fLeft);
39 REPORTER_ASSERT(reporter, offset == surfSubset.fTop);
40 REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.width());
41 REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.height());
42
43 SkCanvas* canvas = surf->getCanvas();
44 SkASSERT_RELEASE(canvas);
45
46 canvas->clear(SK_ColorRED);
47
robertphillips37bd7c32016-03-17 14:31:39 -070048 sk_sp<SkSpecialImage> img(surf->makeImageSnapshot());
robertphillipsb6c65e92016-02-04 10:52:42 -080049 REPORTER_ASSERT(reporter, img);
50
robertphillips3e302272016-04-20 11:48:36 -070051 const SkIRect imgSubset = img->subset();
robertphillipsb6c65e92016-02-04 10:52:42 -080052 REPORTER_ASSERT(reporter, surfSubset == imgSubset);
53
54 // the canvas was invalidated by the newImageSnapshot call
55 REPORTER_ASSERT(reporter, !surf->getCanvas());
56}
57
58DEF_TEST(SpecialSurface_Raster, reporter) {
59
60 SkImageInfo info = SkImageInfo::MakeN32(kSmallerSize, kSmallerSize, kOpaque_SkAlphaType);
robertphillips3e302272016-04-20 11:48:36 -070061 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRaster(info));
robertphillipsb6c65e92016-02-04 10:52:42 -080062
63 test_surface(surf, reporter, 0);
64}
65
66DEF_TEST(SpecialSurface_Raster2, reporter) {
67
68 SkBitmap bm;
69 bm.allocN32Pixels(kFullSize, kFullSize, true);
70
71 const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
72
robertphillips3e302272016-04-20 11:48:36 -070073 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeFromBitmap(subset, bm));
robertphillipsb6c65e92016-02-04 10:52:42 -080074
75 test_surface(surf, reporter, kPad);
76
77 // TODO: check that the clear didn't escape the active region
78}
79
egdanielab527a52016-06-28 08:07:26 -070080DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1, reporter, ctxInfo) {
Brian Osman10fc6fd2018-03-02 11:01:10 -050081 for (auto config : { kRGBA_8888_GrPixelConfig, kRGBA_1010102_GrPixelConfig }) {
Greg Daniel4065d452018-11-16 15:43:41 -050082 const GrCaps* caps = ctxInfo.grContext()->contextPriv().caps();
83 if (!caps->isConfigRenderable(config)) {
Brian Osman10fc6fd2018-03-02 11:01:10 -050084 continue;
85 }
Greg Daniel4065d452018-11-16 15:43:41 -050086 GrSRGBEncoded srgbEncoded = GrSRGBEncoded::kNo;
87 GrColorType colorType = GrPixelConfigToColorTypeAndEncoding(config, &srgbEncoded);
88 const GrBackendFormat format =
89 caps->getBackendFormatFromGrColorType(colorType, srgbEncoded);
Brian Osman10fc6fd2018-03-02 11:01:10 -050090 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRenderTarget(ctxInfo.grContext(),
Greg Daniel4065d452018-11-16 15:43:41 -050091 format,
Brian Osman10fc6fd2018-03-02 11:01:10 -050092 kSmallerSize, kSmallerSize,
93 config, nullptr));
94 test_surface(surf, reporter, 0);
95 }
robertphillipsb6c65e92016-02-04 10:52:42 -080096}