| 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" |
| 11 #include "SkImage.h" | 11 #include "SkImage.h" |
| 12 #include "SkPixmap.h" | 12 #include "SkPixmap.h" |
| 13 #include "SkSpecialImage.h" | 13 #include "SkSpecialImage.h" |
| 14 #include "SkSpecialSurface.h" | 14 #include "SkSpecialSurface.h" |
| 15 #include "SkSurface.h" | 15 #include "SkSurface.h" |
| 16 #include "Test.h" | 16 #include "Test.h" |
| 17 #include "TestingSpecialImageAccess.h" | |
| 18 | 17 |
| 19 #if SK_SUPPORT_GPU | 18 #if SK_SUPPORT_GPU |
| 20 #include "GrContext.h" | 19 #include "GrContext.h" |
| 21 #endif | 20 #endif |
| 22 | 21 |
| 23 | 22 |
| 24 // This test creates backing resources exactly sized to [kFullSize x kFullSize]. | 23 // This test creates backing resources exactly sized to [kFullSize x kFullSize]. |
| 25 // It then wraps them in an SkSpecialImage with only the center (red) region bei
ng active. | 24 // It then wraps them in an SkSpecialImage with only the center (red) region bei
ng active. |
| 26 // It then draws the SkSpecialImage to a full sized (all blue) canvas and checks
that none | 25 // It then draws the SkSpecialImage to a full sized (all blue) canvas and checks
that none |
| 27 // of the inactive (green) region leaked out. | 26 // of the inactive (green) region leaked out. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 46 SkIntToScalar(kSmallerSize), SkIntToScalar(kS
mallerSize)), | 45 SkIntToScalar(kSmallerSize), SkIntToScalar(kS
mallerSize)), |
| 47 p); | 46 p); |
| 48 | 47 |
| 49 return bm; | 48 return bm; |
| 50 } | 49 } |
| 51 | 50 |
| 52 // 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) |
| 53 static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* rep
orter, | 52 static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* rep
orter, |
| 54 GrContext* context, bool peekTextureSucceeds, | 53 GrContext* context, bool peekTextureSucceeds, |
| 55 int offset, int size) { | 54 int offset, int size) { |
| 56 const SkIRect subset = TestingSpecialImageAccess::Subset(img.get()); | 55 const SkIRect subset = img->subset(); |
| 57 REPORTER_ASSERT(reporter, offset == subset.left()); | 56 REPORTER_ASSERT(reporter, offset == subset.left()); |
| 58 REPORTER_ASSERT(reporter, offset == subset.top()); | 57 REPORTER_ASSERT(reporter, offset == subset.top()); |
| 59 REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); | 58 REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); |
| 60 REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); | 59 REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); |
| 61 | 60 |
| 62 //-------------- | 61 //-------------- |
| 63 // Test that peekTexture reports the correct backing type | 62 // Test that peekTexture reports the correct backing type |
| 64 REPORTER_ASSERT(reporter, peekTextureSucceeds == img->isTextureBacked()); | 63 REPORTER_ASSERT(reporter, peekTextureSucceeds == img->isTextureBacked()); |
| 65 | 64 |
| 66 #if SK_SUPPORT_GPU | 65 #if SK_SUPPORT_GPU |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_Back
endHandleAccess)); | 132 !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_Back
endHandleAccess)); |
| 134 SkPixmap tmpPixmap; | 133 SkPixmap tmpPixmap; |
| 135 REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightSurf->peekPixels
(&tmpPixmap)); | 134 REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightSurf->peekPixels
(&tmpPixmap)); |
| 136 } | 135 } |
| 137 } | 136 } |
| 138 | 137 |
| 139 DEF_TEST(SpecialImage_Raster, reporter) { | 138 DEF_TEST(SpecialImage_Raster, reporter) { |
| 140 SkBitmap bm = create_bm(); | 139 SkBitmap bm = create_bm(); |
| 141 | 140 |
| 142 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster( | 141 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster( |
| 143 nullptr, | |
| 144 SkIRect::MakeWH(kFul
lSize, kFullSize), | 142 SkIRect::MakeWH(kFul
lSize, kFullSize), |
| 145 bm)); | 143 bm)); |
| 146 | 144 |
| 147 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 145 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
| 148 | 146 |
| 149 { | 147 { |
| 150 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(nullptr, s
ubset, bm)); | 148 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm
)); |
| 151 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize); | 149 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize); |
| 152 } | 150 } |
| 153 | 151 |
| 154 { | 152 { |
| 155 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); | 153 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); |
| 156 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize); | 154 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize); |
| 157 } | 155 } |
| 158 } | 156 } |
| 159 | 157 |
| 160 DEF_TEST(SpecialImage_Image, reporter) { | 158 DEF_TEST(SpecialImage_Image, reporter) { |
| 161 SkBitmap bm = create_bm(); | 159 SkBitmap bm = create_bm(); |
| 162 | 160 |
| 163 sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm)); | 161 sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm)); |
| 164 | 162 |
| 165 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage( | 163 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage( |
| 166 nullptr, | |
| 167 SkIRect::MakeWH(kFul
lSize, kFullSize), | 164 SkIRect::MakeWH(kFul
lSize, kFullSize), |
| 168 fullImage)); | 165 fullImage)); |
| 169 | 166 |
| 170 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 167 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
| 171 | 168 |
| 172 { | 169 { |
| 173 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(nullptr, su
bset, | 170 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, ful
lImage)); |
| 174 fullImage))
; | |
| 175 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize); | 171 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize); |
| 176 } | 172 } |
| 177 | 173 |
| 178 { | 174 { |
| 179 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); | 175 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); |
| 180 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize); | 176 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize); |
| 181 } | 177 } |
| 182 } | 178 } |
| 183 | 179 |
| 184 DEF_TEST(SpecialImage_Pixmap, reporter) { | 180 DEF_TEST(SpecialImage_Pixmap, reporter) { |
| 185 SkAutoPixmapStorage pixmap; | 181 SkAutoPixmapStorage pixmap; |
| 186 | 182 |
| 187 const SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_
SkAlphaType); | 183 const SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_
SkAlphaType); |
| 188 pixmap.alloc(info); | 184 pixmap.alloc(info); |
| 189 pixmap.erase(SK_ColorGREEN); | 185 pixmap.erase(SK_ColorGREEN); |
| 190 | 186 |
| 191 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 187 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
| 192 | 188 |
| 193 pixmap.erase(SK_ColorRED, subset); | 189 pixmap.erase(SK_ColorRED, subset); |
| 194 | 190 |
| 195 { | 191 { |
| 196 sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap(nullptr, subset
, pixmap, | 192 sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap(subset, pixmap, |
| 197 nullptr, nullpt
r)); | 193 nullptr, nullpt
r)); |
| 198 test_image(img, reporter, nullptr, false, kPad, kFullSize); | 194 test_image(img, reporter, nullptr, false, kPad, kFullSize); |
| 199 } | 195 } |
| 200 } | 196 } |
| 201 | 197 |
| 202 | 198 |
| 203 #if SK_SUPPORT_GPU | 199 #if SK_SUPPORT_GPU |
| 204 | 200 |
| 205 static void test_texture_backed(skiatest::Reporter* reporter, | 201 static void test_texture_backed(skiatest::Reporter* reporter, |
| 206 const sk_sp<SkSpecialImage>& orig, | 202 const sk_sp<SkSpecialImage>& orig, |
| 207 const sk_sp<SkSpecialImage>& gpuBacked) { | 203 const sk_sp<SkSpecialImage>& gpuBacked) { |
| 208 REPORTER_ASSERT(reporter, gpuBacked); | 204 REPORTER_ASSERT(reporter, gpuBacked); |
| 209 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked()); | 205 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked()); |
| 210 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID()); | 206 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID()); |
| 211 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().widt
h() && | 207 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().widt
h() && |
| 212 gpuBacked->subset().height() == orig->subset().hei
ght()); | 208 gpuBacked->subset().height() == orig->subset().hei
ght()); |
| 213 } | 209 } |
| 214 | 210 |
| 215 // Test out the SkSpecialImage::makeTextureImage entry point | 211 // Test out the SkSpecialImage::makeTextureImage entry point |
| 216 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo)
{ | 212 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo)
{ |
| 217 GrContext* context = ctxInfo.fGrContext; | 213 GrContext* context = ctxInfo.fGrContext; |
| 218 SkBitmap bm = create_bm(); | 214 SkBitmap bm = create_bm(); |
| 219 | 215 |
| 220 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 216 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
| 221 | 217 |
| 222 { | 218 { |
| 223 // raster | 219 // raster |
| 224 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster( | 220 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster( |
| 225 nullptr, | |
| 226 SkIRect:
:MakeWH(kFullSize, | 221 SkIRect:
:MakeWH(kFullSize, |
| 227
kFullSize), | 222
kFullSize), |
| 228 bm)); | 223 bm)); |
| 229 | 224 |
| 230 { | 225 { |
| 231 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(nullp
tr, context)); | 226 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(conte
xt)); |
| 232 test_texture_backed(reporter, rasterImage, fromRaster); | 227 test_texture_backed(reporter, rasterImage, fromRaster); |
| 233 } | 228 } |
| 234 | 229 |
| 235 { | 230 { |
| 236 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset)
); | 231 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset)
); |
| 237 | 232 |
| 238 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage
(nullptr, context)); | 233 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage
(context)); |
| 239 test_texture_backed(reporter, subRasterImage, fromSubRaster); | 234 test_texture_backed(reporter, subRasterImage, fromSubRaster); |
| 240 } | 235 } |
| 241 } | 236 } |
| 242 | 237 |
| 243 { | 238 { |
| 244 // gpu | 239 // gpu |
| 245 GrSurfaceDesc desc; | 240 GrSurfaceDesc desc; |
| 246 desc.fConfig = kSkia8888_GrPixelConfig; | 241 desc.fConfig = kSkia8888_GrPixelConfig; |
| 247 desc.fFlags = kNone_GrSurfaceFlags; | 242 desc.fFlags = kNone_GrSurfaceFlags; |
| 248 desc.fWidth = kFullSize; | 243 desc.fWidth = kFullSize; |
| 249 desc.fHeight = kFullSize; | 244 desc.fHeight = kFullSize; |
| 250 | 245 |
| 251 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTextur
e(desc, | 246 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTextur
e(desc, |
| 252
SkBudgeted::kNo, | 247
SkBudgeted::kNo, |
| 253
bm.getPixels(), | 248
bm.getPixels(), |
| 254
0)); | 249
0)); |
| 255 if (!texture) { | 250 if (!texture) { |
| 256 return; | 251 return; |
| 257 } | 252 } |
| 258 | 253 |
| 259 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu( | 254 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu( |
| 260 nullptr, | |
| 261 SkIRect::MakeWH(
kFullSize, | 255 SkIRect::MakeWH(
kFullSize, |
| 262
kFullSize), | 256
kFullSize), |
| 263 kNeedNewImageUni
queID_SpecialImage, | 257 kNeedNewImageUni
queID_SpecialImage, |
| 264 texture)); | 258 texture)); |
| 265 | 259 |
| 266 { | 260 { |
| 267 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(nullptr, co
ntext)); | 261 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context)); |
| 268 test_texture_backed(reporter, gpuImage, fromGPU); | 262 test_texture_backed(reporter, gpuImage, fromGPU); |
| 269 } | 263 } |
| 270 | 264 |
| 271 { | 265 { |
| 272 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset)); | 266 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset)); |
| 273 | 267 |
| 274 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(nullp
tr, context)); | 268 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(conte
xt)); |
| 275 test_texture_backed(reporter, subGPUImage, fromSubGPU); | 269 test_texture_backed(reporter, subGPUImage, fromSubGPU); |
| 276 } | 270 } |
| 277 } | 271 } |
| 278 } | 272 } |
| 279 | 273 |
| 280 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) { | 274 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) { |
| 281 GrContext* context = ctxInfo.fGrContext; | 275 GrContext* context = ctxInfo.fGrContext; |
| 282 SkBitmap bm = create_bm(); | 276 SkBitmap bm = create_bm(); |
| 283 | 277 |
| 284 GrSurfaceDesc desc; | 278 GrSurfaceDesc desc; |
| 285 desc.fConfig = kSkia8888_GrPixelConfig; | 279 desc.fConfig = kSkia8888_GrPixelConfig; |
| 286 desc.fFlags = kNone_GrSurfaceFlags; | 280 desc.fFlags = kNone_GrSurfaceFlags; |
| 287 desc.fWidth = kFullSize; | 281 desc.fWidth = kFullSize; |
| 288 desc.fHeight = kFullSize; | 282 desc.fHeight = kFullSize; |
| 289 | 283 |
| 290 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(de
sc, | 284 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(de
sc, |
| 291 Sk
Budgeted::kNo, | 285 Sk
Budgeted::kNo, |
| 292 bm
.getPixels(), 0)); | 286 bm
.getPixels(), 0)); |
| 293 if (!texture) { | 287 if (!texture) { |
| 294 return; | 288 return; |
| 295 } | 289 } |
| 296 | 290 |
| 297 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu( | 291 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu( |
| 298 nullptr, | |
| 299 SkIRect::MakeWH(kFul
lSize, kFullSize), | 292 SkIRect::MakeWH(kFul
lSize, kFullSize), |
| 300 kNeedNewImageUniqueI
D_SpecialImage, | 293 kNeedNewImageUniqueI
D_SpecialImage, |
| 301 texture)); | 294 texture)); |
| 302 | 295 |
| 303 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 296 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
| 304 | 297 |
| 305 { | 298 { |
| 306 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu( | 299 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu( |
| 307 nullptr, subset, | 300 subset, |
| 308 kNeedNewImageUniq
ueID_SpecialImage, | 301 kNeedNewImageUniq
ueID_SpecialImage, |
| 309 texture)); | 302 texture)); |
| 310 test_image(subSImg1, reporter, context, true, kPad, kFullSize); | 303 test_image(subSImg1, reporter, context, true, kPad, kFullSize); |
| 311 } | 304 } |
| 312 | 305 |
| 313 { | 306 { |
| 314 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset)); | 307 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset)); |
| 315 test_image(subSImg2, reporter, context, true, kPad, kFullSize); | 308 test_image(subSImg2, reporter, context, true, kPad, kFullSize); |
| 316 } | 309 } |
| 317 } | 310 } |
| 318 | 311 |
| 319 #endif | 312 #endif |
| OLD | NEW |