OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #if SK_SUPPORT_GPU | 8 #if SK_SUPPORT_GPU |
9 | 9 |
10 #include "Test.h" | 10 #include "Test.h" |
11 | 11 |
| 12 #include "GrContextFactory.h" |
| 13 #include "GrLayerCache.h" |
12 #include "GrRecordReplaceDraw.h" | 14 #include "GrRecordReplaceDraw.h" |
13 #include "RecordTestUtils.h" | 15 #include "RecordTestUtils.h" |
14 #include "SkBBHFactory.h" | 16 #include "SkBBHFactory.h" |
15 #include "SkPictureRecorder.h" | 17 #include "SkPictureRecorder.h" |
16 #include "SkRecordDraw.h" | 18 #include "SkRecordDraw.h" |
17 #include "SkRecorder.h" | 19 #include "SkRecorder.h" |
18 #include "SkUtils.h" | 20 #include "SkUtils.h" |
19 | 21 |
20 static const int kWidth = 100; | 22 static const int kWidth = 100; |
21 static const int kHeight = 100; | 23 static const int kHeight = 100; |
(...skipping 18 matching lines...) Expand all Loading... |
40 | 42 |
41 canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHe
ight)), SkPaint()); | 43 canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHe
ight)), SkPaint()); |
42 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHe
ight))); | 44 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHe
ight))); |
43 | 45 |
44 pic.reset(recorder.endRecording()); | 46 pic.reset(recorder.endRecording()); |
45 } | 47 } |
46 | 48 |
47 SkRecord rerecord; | 49 SkRecord rerecord; |
48 SkRecorder canvas(&rerecord, kWidth, kHeight); | 50 SkRecorder canvas(&rerecord, kWidth, kHeight); |
49 | 51 |
50 GrReplacements replacements; | |
51 JustOneDraw callback; | 52 JustOneDraw callback; |
52 GrRecordReplaceDraw(pic, &canvas, &replacements, SkMatrix::I(), &callback); | 53 GrRecordReplaceDraw(pic, &canvas, NULL, SkMatrix::I(), &callback); |
53 | 54 |
54 REPORTER_ASSERT(r, 3 == rerecord.count()); | 55 REPORTER_ASSERT(r, 3 == rerecord.count()); |
55 assert_type<SkRecords::Save>(r, rerecord, 0); | 56 assert_type<SkRecords::Save>(r, rerecord, 0); |
56 assert_type<SkRecords::DrawRect>(r, rerecord, 1); | 57 assert_type<SkRecords::DrawRect>(r, rerecord, 1); |
57 assert_type<SkRecords::Restore>(r, rerecord, 2); | 58 assert_type<SkRecords::Restore>(r, rerecord, 2); |
58 } | 59 } |
59 | 60 |
60 // Make sure GrRecordReplaceDraw balances unbalanced saves | 61 // Make sure GrRecordReplaceDraw balances unbalanced saves |
61 DEF_TEST(RecordReplaceDraw_Unbalanced, r) { | 62 DEF_TEST(RecordReplaceDraw_Unbalanced, r) { |
62 SkAutoTUnref<const SkPicture> pic; | 63 SkAutoTUnref<const SkPicture> pic; |
63 | 64 |
64 { | 65 { |
65 SkPictureRecorder recorder; | 66 SkPictureRecorder recorder; |
66 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntT
oScalar(kHeight)); | 67 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntT
oScalar(kHeight)); |
67 | 68 |
68 // We won't balance this, but GrRecordReplaceDraw will for us. | 69 // We won't balance this, but GrRecordReplaceDraw will for us. |
69 canvas->save(); | 70 canvas->save(); |
70 | 71 |
71 pic.reset(recorder.endRecording()); | 72 pic.reset(recorder.endRecording()); |
72 } | 73 } |
73 | 74 |
74 SkRecord rerecord; | 75 SkRecord rerecord; |
75 SkRecorder canvas(&rerecord, kWidth, kHeight); | 76 SkRecorder canvas(&rerecord, kWidth, kHeight); |
76 | 77 |
77 GrReplacements replacements; | 78 GrRecordReplaceDraw(pic, &canvas, NULL, SkMatrix::I(), NULL/*callback*/); |
78 GrRecordReplaceDraw(pic, &canvas, &replacements, SkMatrix::I(), NULL/*callba
ck*/); | |
79 | 79 |
80 REPORTER_ASSERT(r, 4 == rerecord.count()); | 80 REPORTER_ASSERT(r, 4 == rerecord.count()); |
81 assert_type<SkRecords::Save>(r, rerecord, 0); | 81 assert_type<SkRecords::Save>(r, rerecord, 0); |
82 assert_type<SkRecords::Save>(r, rerecord, 1); | 82 assert_type<SkRecords::Save>(r, rerecord, 1); |
83 assert_type<SkRecords::Restore>(r, rerecord, 2); | 83 assert_type<SkRecords::Restore>(r, rerecord, 2); |
84 assert_type<SkRecords::Restore>(r, rerecord, 3); | 84 assert_type<SkRecords::Restore>(r, rerecord, 3); |
85 } | 85 } |
86 | 86 |
87 static SkImage* make_image(SkColor color) { | |
88 const SkPMColor pmcolor = SkPreMultiplyColor(color); | |
89 const SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); | |
90 const size_t rowBytes = info.minRowBytes(); | |
91 const size_t size = rowBytes * info.height(); | |
92 | |
93 SkAutoMalloc addr(size); | |
94 sk_memset32((SkPMColor*)addr.get(), pmcolor, SkToInt(size >> 2)); | |
95 | |
96 return SkImage::NewRasterCopy(info, addr.get(), rowBytes); | |
97 } | |
98 | |
99 // Test out the layer replacement functionality with and w/o a BBH | 87 // Test out the layer replacement functionality with and w/o a BBH |
100 void test_replacements(skiatest::Reporter* r, bool useBBH) { | 88 void test_replacements(skiatest::Reporter* r, GrContext* context, bool useBBH) { |
101 SkAutoTUnref<const SkPicture> pic; | 89 SkAutoTUnref<const SkPicture> pic; |
102 | 90 |
103 { | 91 { |
104 SkRTreeFactory bbhFactory; | 92 SkRTreeFactory bbhFactory; |
105 SkPictureRecorder recorder; | 93 SkPictureRecorder recorder; |
106 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntT
oScalar(kHeight), | 94 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntT
oScalar(kHeight), |
107 useBBH ? &bbhFactory : NULL); | 95 useBBH ? &bbhFactory : NULL); |
108 | 96 |
109 SkAutoTDelete<SkPaint> paint(SkNEW(SkPaint)); | 97 SkAutoTDelete<SkPaint> paint(SkNEW(SkPaint)); |
110 canvas->saveLayer(NULL, paint); | 98 canvas->saveLayer(NULL, paint); |
111 canvas->clear(SK_ColorRED); | 99 canvas->clear(SK_ColorRED); |
112 canvas->restore(); | 100 canvas->restore(); |
113 canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth / 2), SkIntToScalar
(kHeight / 2)), | 101 canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth / 2), SkIntToScalar
(kHeight / 2)), |
114 SkPaint()); | 102 SkPaint()); |
115 pic.reset(recorder.endRecording()); | 103 pic.reset(recorder.endRecording()); |
116 } | 104 } |
117 | 105 |
118 int key[1] = { 0 }; | 106 unsigned key[1] = { 0 }; |
119 | 107 |
120 GrReplacements replacements; | 108 GrLayerCache* layerCache = context->getLayerCache(); |
121 GrReplacements::ReplacementInfo* ri = replacements.newReplacement(pic->uniqu
eID(), | 109 GrCachedLayer* layer = layerCache->findLayerOrCreate(pic->uniqueID(), 0, 2, |
122 SkMatrix::
I(), key, 1); | 110 SkIRect::MakeWH(kWidth,
kHeight), |
123 ri->fStop = 2; | 111 SkMatrix::I(), key, 1,
SkNEW(SkPaint)); |
124 ri->fPos.set(0, 0); | 112 |
125 ri->fImage = make_image(SK_ColorRED); | 113 GrSurfaceDesc desc; |
126 ri->fPaint = SkNEW(SkPaint); | 114 desc.fConfig = kSkia8888_GrPixelConfig; |
127 ri->fSrcRect = SkIRect::MakeWH(kWidth, kHeight); | 115 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 116 desc.fWidth = kWidth; |
| 117 desc.fHeight = kHeight; |
| 118 desc.fSampleCnt = 0; |
| 119 |
| 120 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0
)); |
| 121 layer->setTexture(texture, SkIRect::MakeWH(kWidth, kHeight)); |
128 | 122 |
129 SkAutoTUnref<SkBBoxHierarchy> bbh; | 123 SkAutoTUnref<SkBBoxHierarchy> bbh; |
130 | 124 |
131 SkRecord rerecord; | 125 SkRecord rerecord; |
132 SkRecorder canvas(&rerecord, kWidth, kHeight); | 126 SkRecorder canvas(&rerecord, kWidth, kHeight); |
133 GrRecordReplaceDraw(pic, &canvas, &replacements, SkMatrix::I(), NULL/*callba
ck*/); | 127 GrRecordReplaceDraw(pic, &canvas, layerCache, SkMatrix::I(), NULL/*callback*
/); |
134 | 128 |
135 REPORTER_ASSERT(r, 7 == rerecord.count()); | 129 REPORTER_ASSERT(r, 7 == rerecord.count()); |
136 assert_type<SkRecords::Save>(r, rerecord, 0); | 130 assert_type<SkRecords::Save>(r, rerecord, 0); |
137 assert_type<SkRecords::Save>(r, rerecord, 1); | 131 assert_type<SkRecords::Save>(r, rerecord, 1); |
138 assert_type<SkRecords::SetMatrix>(r, rerecord, 2); | 132 assert_type<SkRecords::SetMatrix>(r, rerecord, 2); |
139 assert_type<SkRecords::DrawImageRect>(r, rerecord, 3); | 133 assert_type<SkRecords::DrawBitmapRectToRect>(r, rerecord, 3); |
140 assert_type<SkRecords::Restore>(r, rerecord, 4); | 134 assert_type<SkRecords::Restore>(r, rerecord, 4); |
141 assert_type<SkRecords::DrawRect>(r, rerecord, 5); | 135 assert_type<SkRecords::DrawRect>(r, rerecord, 5); |
142 assert_type<SkRecords::Restore>(r, rerecord, 6); | 136 assert_type<SkRecords::Restore>(r, rerecord, 6); |
143 } | 137 } |
144 | 138 |
145 DEF_TEST(RecordReplaceDraw_Replace, r) { test_replacements(r, false); } | 139 DEF_GPUTEST(RecordReplaceDraw, r, factory) { |
146 DEF_TEST(RecordReplaceDraw_ReplaceWithBBH, r) { test_replacements(r, true); } | 140 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 141 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
LContextType>(type); |
| 142 if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 143 continue; |
| 144 } |
| 145 GrContext* context = factory->get(glType); |
| 146 if (NULL == context) { |
| 147 continue; |
| 148 } |
| 149 |
| 150 test_replacements(r, context, true); |
| 151 test_replacements(r, context, false); |
| 152 } |
| 153 } |
147 | 154 |
148 #endif | 155 #endif |
OLD | NEW |