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