blob: b1cbf680822939d0b42179256ee28a5ebb29f073 [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)
36static void test_surface(SkSpecialSurface* surf, skiatest::Reporter* reporter, int offset) {
37
38 const SkIRect surfSubset = TestingSpecialSurfaceAccess::Subset(surf);
39 REPORTER_ASSERT(reporter, offset == surfSubset.fLeft);
40 REPORTER_ASSERT(reporter, offset == surfSubset.fTop);
41 REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.width());
42 REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.height());
43
44 SkCanvas* canvas = surf->getCanvas();
45 SkASSERT_RELEASE(canvas);
46
47 canvas->clear(SK_ColorRED);
48
49 SkAutoTUnref<SkSpecialImage> img(surf->newImageSnapshot());
50 REPORTER_ASSERT(reporter, img);
51
52 const SkIRect imgSubset = TestingSpecialSurfaceAccess::Subset(img);
53 REPORTER_ASSERT(reporter, surfSubset == imgSubset);
54
55 // the canvas was invalidated by the newImageSnapshot call
56 REPORTER_ASSERT(reporter, !surf->getCanvas());
57}
58
59DEF_TEST(SpecialSurface_Raster, reporter) {
60
61 SkImageInfo info = SkImageInfo::MakeN32(kSmallerSize, kSmallerSize, kOpaque_SkAlphaType);
62 SkAutoTUnref<SkSpecialSurface> surf(SkSpecialSurface::NewRaster(info));
63
64 test_surface(surf, reporter, 0);
65}
66
67DEF_TEST(SpecialSurface_Raster2, reporter) {
68
69 SkBitmap bm;
70 bm.allocN32Pixels(kFullSize, kFullSize, true);
71
72 const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
73
74 SkAutoTUnref<SkSpecialSurface> surf(SkSpecialSurface::NewFromBitmap(subset, bm));
75
76 test_surface(surf, reporter, kPad);
77
78 // TODO: check that the clear didn't escape the active region
79}
80
81#if SK_SUPPORT_GPU
82
83DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1, reporter, context) {
84 GrSurfaceDesc desc;
85 desc.fConfig = kSkia8888_GrPixelConfig;
86 desc.fFlags = kRenderTarget_GrSurfaceFlag;
87 desc.fWidth = kSmallerSize;
88 desc.fHeight = kSmallerSize;
89
90 SkAutoTUnref<SkSpecialSurface> surf(SkSpecialSurface::NewRenderTarget(context, desc));
91
92 test_surface(surf, reporter, 0);
93}
94
95// test the more flexible factory
96DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu2, reporter, context) {
97 GrSurfaceDesc desc;
98 desc.fConfig = kSkia8888_GrPixelConfig;
99 desc.fFlags = kRenderTarget_GrSurfaceFlag;
100 desc.fWidth = kFullSize;
101 desc.fHeight = kFullSize;
102
103 SkAutoTUnref<GrTexture> temp(context->textureProvider()->createApproxTexture(desc));
104 SkASSERT_RELEASE(temp);
105
106 const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
107
108 SkAutoTUnref<SkSpecialSurface> surf(SkSpecialSurface::NewFromTexture(subset, temp));
109
110 test_surface(surf, reporter, kPad);
111
112 // TODO: check that the clear didn't escape the active region
113}
114
115#endif