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)]; |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 22 | SkWriteBuffer wb(storage, sizeof(storage)); |
| 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 | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 51 | static void check_isopaque(skiatest::Reporter* reporter, SkSurface* surface, bool expectedOpaque) { |
| 52 | SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
| 53 | REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque); |
| 54 | } |
| 55 | |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 56 | DEF_TEST(ImageIsOpaqueTest, reporter) { |
| 57 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
| 58 | SkAutoTUnref<SkSurface> surfaceTransparent(SkSurface::NewRaster(infoTransparent)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 59 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 60 | |
| 61 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
| 62 | SkAutoTUnref<SkSurface> surfaceOpaque(SkSurface::NewRaster(infoOpaque)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 63 | check_isopaque(reporter, surfaceOpaque, true); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | #if SK_SUPPORT_GPU |
| 67 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 68 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, context) { |
| 69 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
| 70 | SkAutoTUnref<SkSurface> surfaceTransparent( |
| 71 | SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, infoTransparent)); |
| 72 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 73 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 74 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
| 75 | SkAutoTUnref<SkSurface> surfaceOpaque( |
| 76 | SkSurface::NewRenderTarget(context,SkSurface::kNo_Budgeted, 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 |