OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 <functional> | 8 #include <functional> |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkData.h" | 10 #include "SkData.h" |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_Sk
AlphaType, | 316 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_Sk
AlphaType, |
317 nullptr)); | 317 nullptr)); |
318 test_backend_handle_access_copy_on_write(reporter, surface, acce
ssMode, | 318 test_backend_handle_access_copy_on_write(reporter, surface, acce
ssMode, |
319 handle_access_func); | 319 handle_access_func); |
320 } | 320 } |
321 } | 321 } |
322 } | 322 } |
323 } | 323 } |
324 #endif | 324 #endif |
325 | 325 |
| 326 static bool same_image(SkImage* a, SkImage* b, |
| 327 std::function<intptr_t(SkImage*)> getImageBackingStore) { |
| 328 return getImageBackingStore(a) == getImageBackingStore(b); |
| 329 } |
| 330 |
| 331 static bool same_image_surf(SkImage* a, SkSurface* b, |
| 332 std::function<intptr_t(SkImage*)> getImageBackingSto
re, |
| 333 std::function<intptr_t(SkSurface*)> getSurfaceBackin
gStore) { |
| 334 return getImageBackingStore(a) == getSurfaceBackingStore(b); |
| 335 } |
| 336 |
| 337 static void test_unique_image_snap(skiatest::Reporter* reporter, SkSurface* surf
ace, |
| 338 bool surfaceIsDirect, |
| 339 std::function<intptr_t(SkImage*)> imageBackin
gStore, |
| 340 std::function<intptr_t(SkSurface*)> surfaceBa
ckingStore) { |
| 341 std::function<intptr_t(SkImage*)> ibs = imageBackingStore; |
| 342 std::function<intptr_t(SkSurface*)> sbs = surfaceBackingStore; |
| 343 static const SkSurface::Budgeted kB = SkSurface::kNo_Budgeted; |
| 344 { |
| 345 SkAutoTUnref<SkImage> image(surface->newImageSnapshot(kB, SkSurface::kYe
s_ForceUnique)); |
| 346 REPORTER_ASSERT(reporter, !same_image_surf(image, surface, ibs, sbs)); |
| 347 REPORTER_ASSERT(reporter, image->unique()); |
| 348 } |
| 349 { |
| 350 SkAutoTUnref<SkImage> image1(surface->newImageSnapshot(kB, SkSurface::kY
es_ForceUnique)); |
| 351 REPORTER_ASSERT(reporter, !same_image_surf(image1, surface, ibs, sbs)); |
| 352 REPORTER_ASSERT(reporter, image1->unique()); |
| 353 SkAutoTUnref<SkImage> image2(surface->newImageSnapshot(kB, SkSurface::kY
es_ForceUnique)); |
| 354 REPORTER_ASSERT(reporter, !same_image_surf(image2, surface, ibs, sbs)); |
| 355 REPORTER_ASSERT(reporter, !same_image(image1, image2, ibs)); |
| 356 REPORTER_ASSERT(reporter, image2->unique()); |
| 357 } |
| 358 { |
| 359 SkAutoTUnref<SkImage> image1(surface->newImageSnapshot(kB, SkSurface::kN
o_ForceUnique)); |
| 360 SkAutoTUnref<SkImage> image2(surface->newImageSnapshot(kB, SkSurface::kY
es_ForceUnique)); |
| 361 SkAutoTUnref<SkImage> image3(surface->newImageSnapshot(kB, SkSurface::kN
o_ForceUnique)); |
| 362 SkAutoTUnref<SkImage> image4(surface->newImageSnapshot(kB, SkSurface::kY
es_ForceUnique)); |
| 363 // Image 1 and 3 ought to be the same (or we're missing an optimization)
. |
| 364 REPORTER_ASSERT(reporter, same_image(image1, image3, ibs)); |
| 365 // If the surface is not direct then images 1 and 3 should alias the sur
face's |
| 366 // store. |
| 367 REPORTER_ASSERT(reporter, !surfaceIsDirect == same_image_surf(image1, su
rface, ibs, sbs)); |
| 368 // Image 2 should not be shared with any other image. |
| 369 REPORTER_ASSERT(reporter, !same_image(image1, image2, ibs) && |
| 370 !same_image(image3, image2, ibs) && |
| 371 !same_image(image4, image2, ibs)); |
| 372 REPORTER_ASSERT(reporter, image2->unique()); |
| 373 REPORTER_ASSERT(reporter, !same_image_surf(image2, surface, ibs, sbs)); |
| 374 // Image 4 should not be shared with any other image. |
| 375 REPORTER_ASSERT(reporter, !same_image(image1, image4, ibs) && |
| 376 !same_image(image3, image4, ibs)); |
| 377 REPORTER_ASSERT(reporter, !same_image_surf(image4, surface, ibs, sbs)); |
| 378 REPORTER_ASSERT(reporter, image4->unique()); |
| 379 } |
| 380 } |
| 381 |
| 382 DEF_TEST(UniqueImageSnapshot, reporter) { |
| 383 auto getImageBackingStore = [reporter](SkImage* image) { |
| 384 SkPixmap pm; |
| 385 bool success = image->peekPixels(&pm); |
| 386 REPORTER_ASSERT(reporter, success); |
| 387 return reinterpret_cast<intptr_t>(pm.addr()); |
| 388 }; |
| 389 auto getSufaceBackingStore = [reporter](SkSurface* surface) { |
| 390 SkImageInfo info; |
| 391 size_t rowBytes; |
| 392 const void* pixels = surface->getCanvas()->peekPixels(&info, &rowBytes); |
| 393 REPORTER_ASSERT(reporter, pixels); |
| 394 return reinterpret_cast<intptr_t>(pixels); |
| 395 }; |
| 396 |
| 397 SkAutoTUnref<SkSurface> surface(create_surface()); |
| 398 test_unique_image_snap(reporter, surface, false, getImageBackingStore, getSu
faceBackingStore); |
| 399 surface.reset(create_direct_surface()); |
| 400 test_unique_image_snap(reporter, surface, true, getImageBackingStore, getSuf
aceBackingStore); |
| 401 } |
| 402 |
| 403 #if SK_SUPPORT_GPU |
| 404 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UniqueImageSnapshot_Gpu, reporter, context) { |
| 405 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface
}) { |
| 406 SkAutoTUnref<SkSurface> surface(surface_func(context, kOpaque_SkAlphaTyp
e, nullptr)); |
| 407 |
| 408 auto imageBackingStore = [reporter](SkImage* image) { |
| 409 GrTexture* texture = as_IB(image)->peekTexture(); |
| 410 if (!texture) { |
| 411 ERRORF(reporter, "Not texture backed."); |
| 412 return static_cast<intptr_t>(0); |
| 413 } |
| 414 return static_cast<intptr_t>(texture->getUniqueID()); |
| 415 }; |
| 416 |
| 417 auto surfaceBackingStore = [reporter](SkSurface* surface) { |
| 418 GrRenderTarget* rt = |
| 419 surface->getCanvas()->internal_private_accessTopLayerRenderTarge
t(); |
| 420 if (!rt) { |
| 421 ERRORF(reporter, "Not render target backed."); |
| 422 return static_cast<intptr_t>(0); |
| 423 } |
| 424 return static_cast<intptr_t>(rt->getUniqueID()); |
| 425 }; |
| 426 |
| 427 test_unique_image_snap(reporter, surface, false, imageBackingStore, surf
aceBackingStore); |
| 428 |
| 429 // Test again with a "direct" render target; |
| 430 GrBackendObject textureObject = context->getGpu()->createTestingOnlyBack
endTexture(nullptr, |
| 431 10, 10, kRGBA_8888_GrPixelConfig); |
| 432 GrBackendTextureDesc desc; |
| 433 desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 434 desc.fWidth = 10; |
| 435 desc.fHeight = 10; |
| 436 desc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 437 desc.fTextureHandle = textureObject; |
| 438 GrTexture* texture = context->textureProvider()->wrapBackendTexture(desc
); |
| 439 { |
| 440 SkAutoTUnref<SkSurface> surface( |
| 441 SkSurface::NewRenderTargetDirect(texture->asRenderTarget())); |
| 442 // We should be able to pass true here, but disallowing copy on writ
e for direct GPU |
| 443 // surfaces is not yet implemented. |
| 444 test_unique_image_snap(reporter, surface, false, imageBackingStore, |
| 445 surfaceBackingStore); |
| 446 } |
| 447 texture->unref(); |
| 448 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject); |
| 449 } |
| 450 } |
| 451 #endif |
| 452 |
326 #if SK_SUPPORT_GPU | 453 #if SK_SUPPORT_GPU |
327 // May we (soon) eliminate the need to keep testing this, by hiding the bloody d
evice! | 454 // May we (soon) eliminate the need to keep testing this, by hiding the bloody d
evice! |
328 static uint32_t get_legacy_gen_id(SkSurface* surface) { | 455 static uint32_t get_legacy_gen_id(SkSurface* surface) { |
329 SkBaseDevice* device = | 456 SkBaseDevice* device = |
330 surface->getCanvas()->getDevice_just_for_deprecated_compatibility_te
sting(); | 457 surface->getCanvas()->getDevice_just_for_deprecated_compatibility_te
sting(); |
331 return device->accessBitmap(false).getGenerationID(); | 458 return device->accessBitmap(false).getGenerationID(); |
332 } | 459 } |
333 /* | 460 /* |
334 * Test legacy behavor of bumping the surface's device's bitmap's genID when we
access its | 461 * Test legacy behavor of bumping the surface's device's bitmap's genID when we
access its |
335 * texture handle for writing. | 462 * texture handle for writing. |
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
779 desc.fHeight = kHeight; | 906 desc.fHeight = kHeight; |
780 desc.fFlags = kRenderTarget_GrBackendTextureFlag; | 907 desc.fFlags = kRenderTarget_GrBackendTextureFlag; |
781 desc.fTextureHandle = textureObject; | 908 desc.fTextureHandle = textureObject; |
782 | 909 |
783 SkSurface* surface = SkSurface::NewFromBackendTexture(context, desc, nul
lptr); | 910 SkSurface* surface = SkSurface::NewFromBackendTexture(context, desc, nul
lptr); |
784 test_surface_clear(reporter, surface, grSurfaceGetter, 0xABABABAB); | 911 test_surface_clear(reporter, surface, grSurfaceGetter, 0xABABABAB); |
785 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject); | 912 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject); |
786 } | 913 } |
787 } | 914 } |
788 #endif | 915 #endif |
OLD | NEW |