blob: 7169b5e8e2204149d4c90de0facb49a5ba1b70ca [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
8#include "SkCanvas.h"
9#include "SkSpecialImage.h"
10#include "SkSpecialSurface.h"
11#include "Test.h"
12
13#if SK_SUPPORT_GPU
14#include "GrContext.h"
15#include "SkGr.h"
16#endif
17
18class TestingSpecialSurfaceAccess {
19public:
20 static const SkIRect& Subset(const SkSpecialSurface* surf) {
21 return surf->subset();
22 }
robertphillipsb6c65e92016-02-04 10:52:42 -080023};
24
25// Both 'kSmallerSize' and 'kFullSize' need to be a non-power-of-2 to exercise
26// the gpu's loose fit behavior
27static const int kSmallerSize = 10;
28static const int kPad = 5;
29static const int kFullSize = kSmallerSize + 2 * kPad;
30
31// Exercise the public API of SkSpecialSurface (e.g., getCanvas, newImageSnapshot)
robertphillips37bd7c32016-03-17 14:31:39 -070032static void test_surface(const sk_sp<SkSpecialSurface>& surf,
33 skiatest::Reporter* reporter,
34 int offset) {
robertphillipsb6c65e92016-02-04 10:52:42 -080035
robertphillips37bd7c32016-03-17 14:31:39 -070036 const SkIRect surfSubset = TestingSpecialSurfaceAccess::Subset(surf.get());
robertphillipsb6c65e92016-02-04 10:52:42 -080037 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
robertphillips37bd7c32016-03-17 14:31:39 -070047 sk_sp<SkSpecialImage> img(surf->makeImageSnapshot());
robertphillipsb6c65e92016-02-04 10:52:42 -080048 REPORTER_ASSERT(reporter, img);
49
robertphillips3e302272016-04-20 11:48:36 -070050 const SkIRect imgSubset = img->subset();
robertphillipsb6c65e92016-02-04 10:52:42 -080051 REPORTER_ASSERT(reporter, surfSubset == imgSubset);
52
53 // the canvas was invalidated by the newImageSnapshot call
54 REPORTER_ASSERT(reporter, !surf->getCanvas());
55}
56
57DEF_TEST(SpecialSurface_Raster, reporter) {
58
59 SkImageInfo info = SkImageInfo::MakeN32(kSmallerSize, kSmallerSize, kOpaque_SkAlphaType);
robertphillips3e302272016-04-20 11:48:36 -070060 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRaster(info));
robertphillipsb6c65e92016-02-04 10:52:42 -080061
62 test_surface(surf, reporter, 0);
63}
64
65DEF_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
robertphillips3e302272016-04-20 11:48:36 -070072 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeFromBitmap(subset, bm));
robertphillipsb6c65e92016-02-04 10:52:42 -080073
74 test_surface(surf, reporter, kPad);
75
76 // TODO: check that the clear didn't escape the active region
77}
78
79#if SK_SUPPORT_GPU
80
bsalomon758586c2016-04-06 14:02:39 -070081DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SpecialSurface_Gpu1, reporter, ctxInfo) {
robertphillipsb6c65e92016-02-04 10:52:42 -080082 GrSurfaceDesc desc;
83 desc.fConfig = kSkia8888_GrPixelConfig;
84 desc.fFlags = kRenderTarget_GrSurfaceFlag;
85 desc.fWidth = kSmallerSize;
86 desc.fHeight = kSmallerSize;
87
robertphillips3e302272016-04-20 11:48:36 -070088 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRenderTarget(ctxInfo.fGrContext, desc));
robertphillipsb6c65e92016-02-04 10:52:42 -080089
90 test_surface(surf, reporter, 0);
91}
92
93// test the more flexible factory
bsalomon758586c2016-04-06 14:02:39 -070094DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SpecialSurface_Gpu2, reporter, ctxInfo) {
robertphillipsb6c65e92016-02-04 10:52:42 -080095 GrSurfaceDesc desc;
96 desc.fConfig = kSkia8888_GrPixelConfig;
97 desc.fFlags = kRenderTarget_GrSurfaceFlag;
98 desc.fWidth = kFullSize;
99 desc.fHeight = kFullSize;
100
bsalomonf2f1c172016-04-05 12:59:06 -0700101 SkAutoTUnref<GrTexture> temp(ctxInfo.fGrContext->textureProvider()->createApproxTexture(desc));
robertphillipsb6c65e92016-02-04 10:52:42 -0800102 SkASSERT_RELEASE(temp);
103
104 const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
105
robertphillips3e302272016-04-20 11:48:36 -0700106 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeFromTexture(subset, temp));
robertphillipsb6c65e92016-02-04 10:52:42 -0800107
108 test_surface(surf, reporter, kPad);
109
110 // TODO: check that the clear didn't escape the active region
111}
112
113#endif