piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "SkTypes.h" |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 9 | #include "Test.h" |
| 10 | |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 11 | #if SK_SUPPORT_GPU |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 12 | #include "GrContext.h" |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 13 | #endif |
| 14 | #include "SkImage.h" |
| 15 | #include "SkSurface.h" |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 16 | #include "SkReadBuffer.h" |
| 17 | #include "SkWriteBuffer.h" |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 18 | |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 19 | static void test_flatten(skiatest::Reporter* reporter, const SkImageInfo& info) { |
reed | 7534cf1 | 2014-12-15 06:41:02 -0800 | [diff] [blame] | 20 | // just need a safe amount of storage, but ensure that it is 4-byte aligned. |
| 21 | int32_t storage[(sizeof(SkImageInfo)*2) / sizeof(int32_t)]; |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 22 | SkBinaryWriteBuffer wb(storage, sizeof(storage)); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 23 | info.flatten(wb); |
| 24 | SkASSERT(wb.bytesWritten() < sizeof(storage)); |
| 25 | |
| 26 | SkReadBuffer rb(storage, wb.bytesWritten()); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 27 | |
| 28 | // pick a noisy byte pattern, so we ensure that unflatten sets all of our fields |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame^] | 29 | SkImageInfo info2 = SkImageInfo::Make(0xB8, 0xB8, (SkColorType) 0xB8, (SkAlphaType) 0xB8); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 30 | |
| 31 | info2.unflatten(rb); |
| 32 | REPORTER_ASSERT(reporter, rb.offset() == wb.bytesWritten()); |
msarett | ded0f26 | 2016-06-16 05:33:31 -0700 | [diff] [blame] | 33 | |
| 34 | // FIXME (msarett): |
| 35 | // Support flatten/unflatten of SkColorSpace objects. |
| 36 | REPORTER_ASSERT(reporter, info.makeColorSpace(nullptr) == info2.makeColorSpace(nullptr)); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | DEF_TEST(ImageInfo_flattening, reporter) { |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame^] | 40 | sk_sp<SkColorSpace> spaces[] = { |
| 41 | nullptr, |
| 42 | SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named), |
| 43 | SkColorSpace::NewNamed(SkColorSpace::kAdobeRGB_Named), |
| 44 | }; |
| 45 | |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 46 | for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) { |
| 47 | for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) { |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame^] | 48 | for (auto& cs : spaces) { |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 49 | SkImageInfo info = SkImageInfo::Make(100, 200, |
| 50 | static_cast<SkColorType>(ct), |
| 51 | static_cast<SkAlphaType>(at), |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame^] | 52 | cs); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 53 | test_flatten(reporter, info); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 58 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 59 | static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, |
| 60 | bool expectedOpaque) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 61 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 62 | REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque); |
| 63 | } |
| 64 | |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 65 | DEF_TEST(ImageIsOpaqueTest, reporter) { |
| 66 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 67 | auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 68 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 69 | |
| 70 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 71 | auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 72 | check_isopaque(reporter, surfaceOpaque, true); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | #if SK_SUPPORT_GPU |
| 76 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 77 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 78 | GrContext* context = ctxInfo.grContext(); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 79 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 80 | auto surfaceTransparent(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, infoTransparent)); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 81 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 82 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 83 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 84 | auto surfaceOpaque(SkSurface::MakeRenderTarget(context,SkBudgeted::kNo, infoOpaque)); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 85 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 86 | check_isopaque(reporter, surfaceOpaque, true); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | #endif |