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()); |
| 27 | SkImageInfo info2; |
| 28 | |
| 29 | // pick a noisy byte pattern, so we ensure that unflatten sets all of our fields |
| 30 | memset(&info2, 0xB8, sizeof(info2)); |
| 31 | |
| 32 | info2.unflatten(rb); |
| 33 | REPORTER_ASSERT(reporter, rb.offset() == wb.bytesWritten()); |
| 34 | REPORTER_ASSERT(reporter, info == info2); |
| 35 | } |
| 36 | |
| 37 | DEF_TEST(ImageInfo_flattening, reporter) { |
| 38 | for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) { |
| 39 | for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) { |
| 40 | for (int pt = 0; pt <= kLastEnum_SkColorProfileType; ++pt) { |
| 41 | SkImageInfo info = SkImageInfo::Make(100, 200, |
| 42 | static_cast<SkColorType>(ct), |
| 43 | static_cast<SkAlphaType>(at), |
| 44 | static_cast<SkColorProfileType>(pt)); |
| 45 | test_flatten(reporter, info); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 50 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 51 | static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, |
| 52 | bool expectedOpaque) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 53 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 54 | REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque); |
| 55 | } |
| 56 | |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 57 | DEF_TEST(ImageIsOpaqueTest, reporter) { |
| 58 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 59 | auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 60 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 61 | |
| 62 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 63 | auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 64 | check_isopaque(reporter, surfaceOpaque, true); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | #if SK_SUPPORT_GPU |
| 68 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 69 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 70 | GrContext* context = ctxInfo.grContext(); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 71 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 72 | auto surfaceTransparent(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, infoTransparent)); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 73 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 74 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 75 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 76 | auto surfaceOpaque(SkSurface::MakeRenderTarget(context,SkBudgeted::kNo, infoOpaque)); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 77 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 78 | check_isopaque(reporter, surfaceOpaque, true); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | #endif |