OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 #include "SkAutoPixmapStorage.h" | 8 #include "SkAutoPixmapStorage.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 p.setAntiAlias(false); | 42 p.setAntiAlias(false); |
43 | 43 |
44 temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad), | 44 temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad), |
45 SkIntToScalar(kSmallerSize), SkIntToScalar(kS
mallerSize)), | 45 SkIntToScalar(kSmallerSize), SkIntToScalar(kS
mallerSize)), |
46 p); | 46 p); |
47 | 47 |
48 return bm; | 48 return bm; |
49 } | 49 } |
50 | 50 |
51 // Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels &
draw) | 51 // Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels &
draw) |
52 static void test_image(SkSpecialImage* img, skiatest::Reporter* reporter, | 52 static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* rep
orter, |
53 bool peekPixelsSucceeds, bool peekTextureSucceeds, | 53 bool peekPixelsSucceeds, bool peekTextureSucceeds, |
54 int offset, int size) { | 54 int offset, int size) { |
55 const SkIRect subset = TestingSpecialImageAccess::Subset(img); | 55 const SkIRect subset = TestingSpecialImageAccess::Subset(img.get()); |
56 REPORTER_ASSERT(reporter, offset == subset.left()); | 56 REPORTER_ASSERT(reporter, offset == subset.left()); |
57 REPORTER_ASSERT(reporter, offset == subset.top()); | 57 REPORTER_ASSERT(reporter, offset == subset.top()); |
58 REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); | 58 REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); |
59 REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); | 59 REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); |
60 | 60 |
61 //-------------- | 61 //-------------- |
62 REPORTER_ASSERT(reporter, peekTextureSucceeds == !!TestingSpecialImageAccess
::PeekTexture(img)); | 62 REPORTER_ASSERT(reporter, peekTextureSucceeds == |
| 63 !!TestingSpecialImageAccess::PeekTexture
(img.get())); |
63 | 64 |
64 //-------------- | 65 //-------------- |
65 SkPixmap pixmap; | 66 SkPixmap pixmap; |
66 REPORTER_ASSERT(reporter, peekPixelsSucceeds == | 67 REPORTER_ASSERT(reporter, peekPixelsSucceeds == |
67 !!TestingSpecialImageAccess::PeekPixels(img, &pixm
ap)); | 68 !!TestingSpecialImageAccess::PeekPixels(img.get(),
&pixmap)); |
68 if (peekPixelsSucceeds) { | 69 if (peekPixelsSucceeds) { |
69 REPORTER_ASSERT(reporter, size == pixmap.width()); | 70 REPORTER_ASSERT(reporter, size == pixmap.width()); |
70 REPORTER_ASSERT(reporter, size == pixmap.height()); | 71 REPORTER_ASSERT(reporter, size == pixmap.height()); |
71 } | 72 } |
72 | 73 |
73 //-------------- | 74 //-------------- |
74 SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlph
aType); | 75 SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlph
aType); |
75 | 76 |
76 SkAutoTUnref<SkSpecialSurface> surf(img->newSurface(info)); | 77 sk_sp<SkSpecialSurface> surf(img->makeSurface(info)); |
77 | 78 |
78 SkCanvas* canvas = surf->getCanvas(); | 79 SkCanvas* canvas = surf->getCanvas(); |
79 | 80 |
80 canvas->clear(SK_ColorBLUE); | 81 canvas->clear(SK_ColorBLUE); |
81 img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr); | 82 img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr); |
82 | 83 |
83 SkBitmap bm; | 84 SkBitmap bm; |
84 bm.allocN32Pixels(kFullSize, kFullSize, true); | 85 bm.allocN32Pixels(kFullSize, kFullSize, true); |
85 | 86 |
86 bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0
, 0); | 87 bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0
, 0); |
87 SkASSERT_RELEASE(result); | 88 SkASSERT_RELEASE(result); |
88 | 89 |
89 // Only the center (red) portion should've been drawn into the canvas | 90 // Only the center (red) portion should've been drawn into the canvas |
90 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1)); | 91 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1)); |
91 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad)); | 92 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad)); |
92 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1, | 93 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1, |
93 kSmallerSize+kPad-1)); | 94 kSmallerSize+kPad-1)); |
94 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad, | 95 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad, |
95 kSmallerSize+kPad)); | 96 kSmallerSize+kPad)); |
96 } | 97 } |
97 | 98 |
98 DEF_TEST(SpecialImage_Raster, reporter) { | 99 DEF_TEST(SpecialImage_Raster, reporter) { |
99 SkBitmap bm = create_bm(); | 100 SkBitmap bm = create_bm(); |
100 | 101 |
101 SkAutoTUnref<SkSpecialImage> fullSImage(SkSpecialImage::NewFromRaster( | 102 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster( |
102 nullptr, | 103 nullptr, |
103 SkIRect::MakeWH(kFul
lSize, kFullSize), | 104 SkIRect::MakeWH(kFul
lSize, kFullSize), |
104 bm)); | 105 bm)); |
105 | 106 |
106 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 107 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
107 | 108 |
108 { | 109 { |
109 SkAutoTUnref<SkSpecialImage> subSImg1(SkSpecialImage::NewFromRaster(null
ptr, subset, bm)); | 110 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(nullptr, s
ubset, bm)); |
110 test_image(subSImg1, reporter, true, false, kPad, kFullSize); | 111 test_image(subSImg1, reporter, true, false, kPad, kFullSize); |
111 } | 112 } |
112 | 113 |
113 { | 114 { |
114 SkAutoTUnref<SkSpecialImage> subSImg2(fullSImage->extractSubset(subset))
; | 115 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); |
115 test_image(subSImg2, reporter, true, false, 0, kSmallerSize); | 116 test_image(subSImg2, reporter, true, false, 0, kSmallerSize); |
116 } | 117 } |
117 } | 118 } |
118 | 119 |
119 DEF_TEST(SpecialImage_Image, reporter) { | 120 DEF_TEST(SpecialImage_Image, reporter) { |
120 SkBitmap bm = create_bm(); | 121 SkBitmap bm = create_bm(); |
121 | 122 |
122 sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm)); | 123 sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm)); |
123 | 124 |
124 SkAutoTUnref<SkSpecialImage> fullSImage(SkSpecialImage::NewFromImage( | 125 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage( |
125 nullptr, | 126 nullptr, |
126 SkIRect::MakeWH(kFul
lSize, kFullSize), | 127 SkIRect::MakeWH(kFul
lSize, kFullSize), |
127 fullImage.get())); | 128 fullImage)); |
128 | 129 |
129 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 130 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
130 | 131 |
131 { | 132 { |
132 SkAutoTUnref<SkSpecialImage> subSImg1(SkSpecialImage::NewFromImage(nullp
tr, | 133 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(nullptr, su
bset, |
133 subse
t, | 134 fullImage))
; |
134 fullI
mage.get())); | |
135 test_image(subSImg1, reporter, true, false, kPad, kFullSize); | 135 test_image(subSImg1, reporter, true, false, kPad, kFullSize); |
136 } | 136 } |
137 | 137 |
138 { | 138 { |
139 SkAutoTUnref<SkSpecialImage> subSImg2(fullSImage->extractSubset(subset))
; | 139 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); |
140 test_image(subSImg2, reporter, true, false, 0, kSmallerSize); | 140 test_image(subSImg2, reporter, true, false, 0, kSmallerSize); |
141 } | 141 } |
142 } | 142 } |
143 | 143 |
144 DEF_TEST(SpecialImage_Pixmap, reporter) { | 144 DEF_TEST(SpecialImage_Pixmap, reporter) { |
145 SkAutoPixmapStorage pixmap; | 145 SkAutoPixmapStorage pixmap; |
146 | 146 |
147 const SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_
SkAlphaType); | 147 const SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_
SkAlphaType); |
148 pixmap.alloc(info); | 148 pixmap.alloc(info); |
149 pixmap.erase(SK_ColorGREEN); | 149 pixmap.erase(SK_ColorGREEN); |
150 | 150 |
151 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 151 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
152 | 152 |
153 pixmap.erase(SK_ColorRED, subset); | 153 pixmap.erase(SK_ColorRED, subset); |
154 | 154 |
155 { | 155 { |
156 // The SkAutoPixmapStorage keeps hold of the memory | 156 // The SkAutoPixmapStorage keeps hold of the memory |
157 SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromPixmap(nullptr,
subset, pixmap, | 157 sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap(nullptr, subset
, pixmap, |
158 nullptr,
nullptr)); | 158 nullptr, nullpt
r)); |
159 test_image(img, reporter, true, false, kPad, kFullSize); | 159 test_image(img, reporter, true, false, kPad, kFullSize); |
160 } | 160 } |
161 | 161 |
162 { | 162 { |
163 // The image takes ownership of the memory | 163 // The image takes ownership of the memory |
164 SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromPixmap( | 164 sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap( |
165 nullptr, subset, pixmap, | 165 nullptr, subset, pixmap, |
166 [] (void* addr, void*) -> void {
sk_free(addr); }, | 166 [] (void* addr, void*) -> void { |
| 167 sk_free(addr); |
| 168 }, |
167 nullptr)); | 169 nullptr)); |
168 pixmap.release(); | 170 pixmap.release(); |
169 test_image(img, reporter, true, false, kPad, kFullSize); | 171 test_image(img, reporter, true, false, kPad, kFullSize); |
170 } | 172 } |
171 } | 173 } |
172 | 174 |
173 | 175 |
174 #if SK_SUPPORT_GPU | 176 #if SK_SUPPORT_GPU |
175 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) { | 177 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) { |
176 SkBitmap bm = create_bm(); | 178 SkBitmap bm = create_bm(); |
177 | 179 |
178 GrSurfaceDesc desc; | 180 GrSurfaceDesc desc; |
179 desc.fConfig = kSkia8888_GrPixelConfig; | 181 desc.fConfig = kSkia8888_GrPixelConfig; |
180 desc.fFlags = kNone_GrSurfaceFlags; | 182 desc.fFlags = kNone_GrSurfaceFlags; |
181 desc.fWidth = kFullSize; | 183 desc.fWidth = kFullSize; |
182 desc.fHeight = kFullSize; | 184 desc.fHeight = kFullSize; |
183 | 185 |
184 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(de
sc, SkBudgeted::kNo, | 186 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(de
sc, SkBudgeted::kNo, |
185 bm
.getPixels(), 0)); | 187 bm
.getPixels(), 0)); |
186 if (!texture) { | 188 if (!texture) { |
187 return; | 189 return; |
188 } | 190 } |
189 | 191 |
190 SkAutoTUnref<SkSpecialImage> fullSImg(SkSpecialImage::NewFromGpu( | 192 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu( |
191 nullptr, | 193 nullptr, |
192 SkIRect::MakeWH(kFul
lSize, kFullSize), | 194 SkIRect::MakeWH(kFul
lSize, kFullSize), |
193 kNeedNewImageUniqueI
D_SpecialImage, | 195 kNeedNewImageUniqueI
D_SpecialImage, |
194 texture)); | 196 texture)); |
195 | 197 |
196 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 198 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
197 | 199 |
198 { | 200 { |
199 SkAutoTUnref<SkSpecialImage> subSImg1(SkSpecialImage::NewFromGpu( | 201 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu( |
200 nullptr, subset, | 202 nullptr, subset, |
201 kNeedNewImageUniq
ueID_SpecialImage, | 203 kNeedNewImageUniq
ueID_SpecialImage, |
202 texture)); | 204 texture)); |
203 test_image(subSImg1, reporter, false, true, kPad, kFullSize); | 205 test_image(subSImg1, reporter, false, true, kPad, kFullSize); |
204 } | 206 } |
205 | 207 |
206 { | 208 { |
207 SkAutoTUnref<SkSpecialImage> subSImg2(fullSImg->extractSubset(subset)); | 209 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset)); |
208 test_image(subSImg2, reporter, false, true, kPad, kFullSize); | 210 test_image(subSImg2, reporter, false, true, kPad, kFullSize); |
209 } | 211 } |
210 } | 212 } |
211 | 213 |
212 #endif | 214 #endif |
OLD | NEW |