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 |
| 12 | #include "GrContextFactory.h" |
| 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 | |
| 68 | DEF_GPUTEST(ImageIsOpaqueTest_GPU, reporter, factory) { |
| 69 | for (int i = 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { |
| 70 | GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; |
| 71 | |
| 72 | if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | GrContext* context = factory->get(glCtxType); |
| 77 | |
| 78 | if (NULL == context) { |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 83 | SkAutoTUnref<SkSurface> surfaceTransparent( |
| 84 | SkSurface::NewRenderTarget(context,SkSurface::kNo_Budgeted, infoTransparent)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 85 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 86 | |
| 87 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 88 | SkAutoTUnref<SkSurface> surfaceOpaque( |
| 89 | SkSurface::NewRenderTarget(context,SkSurface::kNo_Budgeted, infoOpaque)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 90 | #if 0 |
| 91 | // this is failing right now : TODO fix me |
| 92 | check_isopaque(reporter, surfaceOpaque, true); |
| 93 | #endif |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | #endif |