blob: 3c6de0a1a1cfc158b6d824cc075530a19949368d [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
14#if SK_SUPPORT_GPU
Brian Salomonc7fe0f72018-05-11 10:14:21 -040015#include "GrCaps.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080016#include "GrContext.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040017#include "GrContextPriv.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080018#include "SkGr.h"
19#endif
20
21class TestingSpecialSurfaceAccess {
22public:
23 static const SkIRect& Subset(const SkSpecialSurface* surf) {
24 return surf->subset();
25 }
robertphillipsb6c65e92016-02-04 10:52:42 -080026};
27
28// Both 'kSmallerSize' and 'kFullSize' need to be a non-power-of-2 to exercise
29// the gpu's loose fit behavior
30static const int kSmallerSize = 10;
31static const int kPad = 5;
32static const int kFullSize = kSmallerSize + 2 * kPad;
33
34// Exercise the public API of SkSpecialSurface (e.g., getCanvas, newImageSnapshot)
robertphillips37bd7c32016-03-17 14:31:39 -070035static void test_surface(const sk_sp<SkSpecialSurface>& surf,
36 skiatest::Reporter* reporter,
37 int offset) {
robertphillipsb6c65e92016-02-04 10:52:42 -080038
robertphillips37bd7c32016-03-17 14:31:39 -070039 const SkIRect surfSubset = TestingSpecialSurfaceAccess::Subset(surf.get());
robertphillipsb6c65e92016-02-04 10:52:42 -080040 REPORTER_ASSERT(reporter, offset == surfSubset.fLeft);
41 REPORTER_ASSERT(reporter, offset == surfSubset.fTop);
42 REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.width());
43 REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.height());
44
45 SkCanvas* canvas = surf->getCanvas();
46 SkASSERT_RELEASE(canvas);
47
48 canvas->clear(SK_ColorRED);
49
robertphillips37bd7c32016-03-17 14:31:39 -070050 sk_sp<SkSpecialImage> img(surf->makeImageSnapshot());
robertphillipsb6c65e92016-02-04 10:52:42 -080051 REPORTER_ASSERT(reporter, img);
52
robertphillips3e302272016-04-20 11:48:36 -070053 const SkIRect imgSubset = img->subset();
robertphillipsb6c65e92016-02-04 10:52:42 -080054 REPORTER_ASSERT(reporter, surfSubset == imgSubset);
55
56 // the canvas was invalidated by the newImageSnapshot call
57 REPORTER_ASSERT(reporter, !surf->getCanvas());
58}
59
60DEF_TEST(SpecialSurface_Raster, reporter) {
61
62 SkImageInfo info = SkImageInfo::MakeN32(kSmallerSize, kSmallerSize, kOpaque_SkAlphaType);
robertphillips3e302272016-04-20 11:48:36 -070063 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRaster(info));
robertphillipsb6c65e92016-02-04 10:52:42 -080064
65 test_surface(surf, reporter, 0);
66}
67
68DEF_TEST(SpecialSurface_Raster2, reporter) {
69
70 SkBitmap bm;
71 bm.allocN32Pixels(kFullSize, kFullSize, true);
72
73 const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
74
robertphillips3e302272016-04-20 11:48:36 -070075 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeFromBitmap(subset, bm));
robertphillipsb6c65e92016-02-04 10:52:42 -080076
77 test_surface(surf, reporter, kPad);
78
79 // TODO: check that the clear didn't escape the active region
80}
81
82#if SK_SUPPORT_GPU
83
egdanielab527a52016-06-28 08:07:26 -070084DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1, reporter, ctxInfo) {
Brian Osman10fc6fd2018-03-02 11:01:10 -050085 for (auto config : { kRGBA_8888_GrPixelConfig, kRGBA_1010102_GrPixelConfig }) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -040086 if (!ctxInfo.grContext()->contextPriv().caps()->isConfigRenderable(config)) {
Brian Osman10fc6fd2018-03-02 11:01:10 -050087 continue;
88 }
89 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRenderTarget(ctxInfo.grContext(),
90 kSmallerSize, kSmallerSize,
91 config, nullptr));
92 test_surface(surf, reporter, 0);
93 }
robertphillipsb6c65e92016-02-04 10:52:42 -080094}
95
robertphillipsb6c65e92016-02-04 10:52:42 -080096#endif