• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/gpu/GrDirectContext.h"
11 #include "src/core/SkSpecialImage.h"
12 #include "src/core/SkSpecialSurface.h"
13 #include "src/gpu/GrCaps.h"
14 #include "src/gpu/GrDirectContextPriv.h"
15 #include "src/gpu/SkGr.h"
16 #include "tests/Test.h"
17 
18 class TestingSpecialSurfaceAccess {
19 public:
Subset(const SkSpecialSurface * surf)20     static const SkIRect& Subset(const SkSpecialSurface* surf) {
21         return surf->subset();
22     }
23 };
24 
25 // Both 'kSmallerSize' and 'kFullSize' need to be a non-power-of-2 to exercise
26 // the gpu's loose fit behavior
27 static const int kSmallerSize = 10;
28 static const int kPad = 5;
29 static const int kFullSize = kSmallerSize + 2 * kPad;
30 
31 // Exercise the public API of SkSpecialSurface (e.g., getCanvas, newImageSnapshot)
test_surface(const sk_sp<SkSpecialSurface> & surf,skiatest::Reporter * reporter,int offset)32 static void test_surface(const sk_sp<SkSpecialSurface>& surf,
33                          skiatest::Reporter* reporter,
34                          int offset) {
35 
36     const SkIRect surfSubset = TestingSpecialSurfaceAccess::Subset(surf.get());
37     REPORTER_ASSERT(reporter, offset == surfSubset.fLeft);
38     REPORTER_ASSERT(reporter, offset == surfSubset.fTop);
39     REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.width());
40     REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.height());
41 
42     SkCanvas* canvas = surf->getCanvas();
43     SkASSERT_RELEASE(canvas);
44 
45     canvas->clear(SK_ColorRED);
46 
47     sk_sp<SkSpecialImage> img(surf->makeImageSnapshot());
48     REPORTER_ASSERT(reporter, img);
49 
50     const SkIRect imgSubset = img->subset();
51     REPORTER_ASSERT(reporter, surfSubset == imgSubset);
52 
53     // the canvas was invalidated by the newImageSnapshot call
54     REPORTER_ASSERT(reporter, !surf->getCanvas());
55 }
56 
DEF_TEST(SpecialSurface_Raster,reporter)57 DEF_TEST(SpecialSurface_Raster, reporter) {
58 
59     SkImageInfo info = SkImageInfo::MakeN32(kSmallerSize, kSmallerSize, kOpaque_SkAlphaType);
60     sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRaster(info, SkSurfaceProps()));
61 
62     test_surface(surf, reporter, 0);
63 }
64 
DEF_TEST(SpecialSurface_Raster2,reporter)65 DEF_TEST(SpecialSurface_Raster2, reporter) {
66 
67     SkBitmap bm;
68     bm.allocN32Pixels(kFullSize, kFullSize, true);
69 
70     const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
71 
72     sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeFromBitmap(subset, bm, SkSurfaceProps()));
73 
74     test_surface(surf, reporter, kPad);
75 
76     // TODO: check that the clear didn't escape the active region
77 }
78 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1,reporter,ctxInfo)79 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1, reporter, ctxInfo) {
80     auto dContext = ctxInfo.directContext();
81 
82     for (auto colorType : { kRGBA_8888_SkColorType, kRGBA_1010102_SkColorType }) {
83         if (!dContext->colorTypeSupportedAsSurface(colorType)) {
84             continue;
85         }
86 
87         SkImageInfo ii = SkImageInfo::Make({ kSmallerSize, kSmallerSize }, colorType,
88                                            kPremul_SkAlphaType);
89 
90         auto surf(SkSpecialSurface::MakeRenderTarget(dContext, ii, SkSurfaceProps()));
91         test_surface(surf, reporter, 0);
92     }
93 }
94