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/SkAlphaType.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkSurfaceProps.h"
16 #include "include/core/SkTypes.h"
17 #include "include/gpu/GrDirectContext.h"
18 #include "include/gpu/GrTypes.h"
19 #include "src/core/SkSpecialImage.h"
20 #include "src/core/SkSpecialSurface.h"
21 #include "tests/CtsEnforcement.h"
22 #include "tests/Test.h"
23 
24 #include <initializer_list>
25 struct GrContextOptions;
26 
27 static const int kSurfaceSize = 10;
28 
29 // Exercise the public API of SkSpecialSurface (e.g., getCanvas, newImageSnapshot)
test_surface(const sk_sp<SkSpecialSurface> & surf,skiatest::Reporter * reporter,int offset)30 static void test_surface(const sk_sp<SkSpecialSurface>& surf,
31                          skiatest::Reporter* reporter,
32                          int offset) {
33 
34     const SkIRect surfSubset = surf->subset();
35     REPORTER_ASSERT(reporter, offset == surfSubset.fLeft);
36     REPORTER_ASSERT(reporter, offset == surfSubset.fTop);
37     REPORTER_ASSERT(reporter, kSurfaceSize == surfSubset.width());
38     REPORTER_ASSERT(reporter, kSurfaceSize == surfSubset.height());
39 
40     SkCanvas* canvas = surf->getCanvas();
41     SkASSERT_RELEASE(canvas);
42 
43     canvas->clear(SK_ColorRED);
44 
45     sk_sp<SkSpecialImage> img(surf->makeImageSnapshot());
46     REPORTER_ASSERT(reporter, img);
47 
48     const SkIRect imgSubset = img->subset();
49     REPORTER_ASSERT(reporter, surfSubset == imgSubset);
50 
51     // the canvas was invalidated by the newImageSnapshot call
52     REPORTER_ASSERT(reporter, !surf->getCanvas());
53 }
54 
DEF_TEST(SpecialSurface_Raster,reporter)55 DEF_TEST(SpecialSurface_Raster, reporter) {
56 
57     SkImageInfo info = SkImageInfo::MakeN32(kSurfaceSize, kSurfaceSize, kOpaque_SkAlphaType);
58     sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRaster(info, SkSurfaceProps()));
59 
60     test_surface(surf, reporter, 0);
61 }
62 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)63 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1,
64                                        reporter,
65                                        ctxInfo,
66                                        CtsEnforcement::kApiLevel_T) {
67     auto dContext = ctxInfo.directContext();
68 
69     for (auto colorType : { kRGBA_8888_SkColorType, kRGBA_1010102_SkColorType }) {
70         if (!dContext->colorTypeSupportedAsSurface(colorType)) {
71             continue;
72         }
73 
74         SkImageInfo ii = SkImageInfo::Make({ kSurfaceSize, kSurfaceSize }, colorType,
75                                            kPremul_SkAlphaType);
76 
77         auto surf(SkSpecialSurface::MakeRenderTarget(dContext, ii, SkSurfaceProps(),
78                                                      kTopLeft_GrSurfaceOrigin));
79         test_surface(surf, reporter, 0);
80     }
81 }
82 
83 #if SK_GRAPHITE
84 
85 #include "include/gpu/graphite/Context.h"
86 #include "include/gpu/graphite/TextureInfo.h"
87 #include "src/gpu/graphite/Caps.h"
88 #include "src/gpu/graphite/ContextPriv.h"
89 
DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Graphite,reporter,context)90 DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Graphite, reporter, context) {
91     using namespace skgpu::graphite;
92 
93     auto caps = context->priv().caps();
94     auto recorder = context->makeRecorder();
95 
96     for (auto colorType : { kRGBA_8888_SkColorType, kRGBA_1010102_SkColorType }) {
97         TextureInfo info = caps->getDefaultSampledTextureInfo(colorType,
98                                                               skgpu::Mipmapped::kNo,
99                                                               skgpu::Protected::kNo,
100                                                               Renderable::kYes);
101         if (!info.isValid()) {
102             continue;
103         }
104 
105         SkImageInfo ii = SkImageInfo::Make({ kSurfaceSize, kSurfaceSize }, colorType,
106                                            kPremul_SkAlphaType);
107 
108         auto surf(SkSpecialSurface::MakeGraphite(recorder.get(), ii, SkSurfaceProps()));
109         test_surface(surf, reporter, 0);
110     }
111 }
112 
113 #endif // SK_GRAPHITE
114