1 /*
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
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "src/core/SkSpecialImage.h"
11 #include "src/core/SkSpecialSurface.h"
12 #include "tests/Test.h"
13
14 #include "include/gpu/GrContext.h"
15 #include "src/gpu/GrCaps.h"
16 #include "src/gpu/GrContextPriv.h"
17 #include "src/gpu/SkGr.h"
18
19 class TestingSpecialSurfaceAccess {
20 public:
Subset(const SkSpecialSurface * surf)21 static const SkIRect& Subset(const SkSpecialSurface* surf) {
22 return surf->subset();
23 }
24 };
25
26 // Both 'kSmallerSize' and 'kFullSize' need to be a non-power-of-2 to exercise
27 // the gpu's loose fit behavior
28 static const int kSmallerSize = 10;
29 static const int kPad = 5;
30 static const int kFullSize = kSmallerSize + 2 * kPad;
31
32 // Exercise the public API of SkSpecialSurface (e.g., getCanvas, newImageSnapshot)
test_surface(const sk_sp<SkSpecialSurface> & surf,skiatest::Reporter * reporter,int offset)33 static void test_surface(const sk_sp<SkSpecialSurface>& surf,
34 skiatest::Reporter* reporter,
35 int offset) {
36
37 const SkIRect surfSubset = TestingSpecialSurfaceAccess::Subset(surf.get());
38 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
48 sk_sp<SkSpecialImage> img(surf->makeImageSnapshot());
49 REPORTER_ASSERT(reporter, img);
50
51 const SkIRect imgSubset = img->subset();
52 REPORTER_ASSERT(reporter, surfSubset == imgSubset);
53
54 // the canvas was invalidated by the newImageSnapshot call
55 REPORTER_ASSERT(reporter, !surf->getCanvas());
56 }
57
DEF_TEST(SpecialSurface_Raster,reporter)58 DEF_TEST(SpecialSurface_Raster, reporter) {
59
60 SkImageInfo info = SkImageInfo::MakeN32(kSmallerSize, kSmallerSize, kOpaque_SkAlphaType);
61 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRaster(info));
62
63 test_surface(surf, reporter, 0);
64 }
65
DEF_TEST(SpecialSurface_Raster2,reporter)66 DEF_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
73 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeFromBitmap(subset, bm));
74
75 test_surface(surf, reporter, kPad);
76
77 // TODO: check that the clear didn't escape the active region
78 }
79
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1,reporter,ctxInfo)80 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1, reporter, ctxInfo) {
81 for (auto colorType : {GrColorType::kRGBA_8888, GrColorType::kRGBA_1010102}) {
82 if (!ctxInfo.grContext()->colorTypeSupportedAsSurface(
83 GrColorTypeToSkColorType(colorType))) {
84 continue;
85 }
86 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRenderTarget(
87 ctxInfo.grContext(), kSmallerSize, kSmallerSize, colorType, nullptr));
88 test_surface(surf, reporter, 0);
89 }
90 }
91