blob: cd4f0ae19b6376dcfc4d911f07bdfa5e1f6af1f0 [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 }
23
24 static const SkIRect& Subset(const SkSpecialImage* img) {
25 return img->subset();
26 }
27};
28
29// Both 'kSmallerSize' and 'kFullSize' need to be a non-power-of-2 to exercise
30// the gpu's loose fit behavior
31static const int kSmallerSize = 10;
32static const int kPad = 5;
33static const int kFullSize = kSmallerSize + 2 * kPad;
34
35// Exercise the public API of SkSpecialSurface (e.g., getCanvas, newImageSnapshot)
robertphillips37bd7c32016-03-17 14:31:39 -070036static void test_surface(const sk_sp<SkSpecialSurface>& surf,
37 skiatest::Reporter* reporter,
38 int offset) {
robertphillipsb6c65e92016-02-04 10:52:42 -080039
robertphillips37bd7c32016-03-17 14:31:39 -070040 const SkIRect surfSubset = TestingSpecialSurfaceAccess::Subset(surf.get());
robertphillipsb6c65e92016-02-04 10:52:42 -080041 REPORTER_ASSERT(reporter, offset == surfSubset.fLeft);
42 REPORTER_ASSERT(reporter, offset == surfSubset.fTop);
43 REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.width());
44 REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.height());
45
46 SkCanvas* canvas = surf->getCanvas();
47 SkASSERT_RELEASE(canvas);
48
49 canvas->clear(SK_ColorRED);
50
robertphillips37bd7c32016-03-17 14:31:39 -070051 sk_sp<SkSpecialImage> img(surf->makeImageSnapshot());
robertphillipsb6c65e92016-02-04 10:52:42 -080052 REPORTER_ASSERT(reporter, img);
53
robertphillips37bd7c32016-03-17 14:31:39 -070054 const SkIRect imgSubset = TestingSpecialSurfaceAccess::Subset(img.get());
robertphillipsb6c65e92016-02-04 10:52:42 -080055 REPORTER_ASSERT(reporter, surfSubset == imgSubset);
56
57 // the canvas was invalidated by the newImageSnapshot call
58 REPORTER_ASSERT(reporter, !surf->getCanvas());
59}
60
61DEF_TEST(SpecialSurface_Raster, reporter) {
62
63 SkImageInfo info = SkImageInfo::MakeN32(kSmallerSize, kSmallerSize, kOpaque_SkAlphaType);
robertphillips37bd7c32016-03-17 14:31:39 -070064 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRaster(nullptr, info));
robertphillipsb6c65e92016-02-04 10:52:42 -080065
66 test_surface(surf, reporter, 0);
67}
68
69DEF_TEST(SpecialSurface_Raster2, reporter) {
70
71 SkBitmap bm;
72 bm.allocN32Pixels(kFullSize, kFullSize, true);
73
74 const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
75
robertphillips37bd7c32016-03-17 14:31:39 -070076 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeFromBitmap(nullptr, subset, bm));
robertphillipsb6c65e92016-02-04 10:52:42 -080077
78 test_surface(surf, reporter, kPad);
79
80 // TODO: check that the clear didn't escape the active region
81}
82
83#if SK_SUPPORT_GPU
84
85DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1, reporter, context) {
86 GrSurfaceDesc desc;
87 desc.fConfig = kSkia8888_GrPixelConfig;
88 desc.fFlags = kRenderTarget_GrSurfaceFlag;
89 desc.fWidth = kSmallerSize;
90 desc.fHeight = kSmallerSize;
91
robertphillips37bd7c32016-03-17 14:31:39 -070092 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRenderTarget(nullptr, context, desc));
robertphillipsb6c65e92016-02-04 10:52:42 -080093
94 test_surface(surf, reporter, 0);
95}
96
97// test the more flexible factory
98DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu2, reporter, context) {
99 GrSurfaceDesc desc;
100 desc.fConfig = kSkia8888_GrPixelConfig;
101 desc.fFlags = kRenderTarget_GrSurfaceFlag;
102 desc.fWidth = kFullSize;
103 desc.fHeight = kFullSize;
104
105 SkAutoTUnref<GrTexture> temp(context->textureProvider()->createApproxTexture(desc));
106 SkASSERT_RELEASE(temp);
107
108 const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
109
robertphillips37bd7c32016-03-17 14:31:39 -0700110 sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeFromTexture(nullptr, subset, temp));
robertphillipsb6c65e92016-02-04 10:52:42 -0800111
112 test_surface(surf, reporter, kPad);
113
114 // TODO: check that the clear didn't escape the active region
115}
116
117#endif