Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC |
| 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 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 9 | #include "include/core/SkImage.h" |
| 10 | #include "include/core/SkSurface.h" |
| 11 | #include "include/core/SkSurfaceCharacterization.h" |
| 12 | #include "tests/Test.h" |
| 13 | |
| 14 | DEF_GPUTEST(GrDDLImage_MakeSubset, reporter, options) { |
| 15 | sk_gpu_test::GrContextFactory factory(options); |
| 16 | for (int ct = 0; ct < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++ct) { |
| 17 | auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(ct); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 18 | auto dContext = factory.get(contextType); |
| 19 | if (!dContext) { |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 20 | continue; |
| 21 | } |
| 22 | SkIRect subsetBounds = SkIRect::MakeLTRB(4,4,8,8); |
| 23 | SkImageInfo ii = SkImageInfo::Make(16, 16, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 24 | |
| 25 | // Raster image: |
| 26 | SkBitmap bm; |
| 27 | bm.setInfo(ii); |
| 28 | bm.allocPixels(); |
| 29 | bm.eraseColor(SK_ColorBLACK); |
| 30 | bm.setImmutable(); |
Mike Reed | dc607e3 | 2020-12-23 11:50:36 -0500 | [diff] [blame] | 31 | auto rasterImg = bm.asImage(); |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 32 | REPORTER_ASSERT(reporter, rasterImg->isValid(static_cast<GrRecordingContext*>(nullptr))); |
| 33 | |
| 34 | // raster + context: |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 35 | auto subImg1 = rasterImg->makeSubset(subsetBounds, dContext); |
| 36 | REPORTER_ASSERT(reporter, subImg1->isValid(dContext)); |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 37 | |
| 38 | // raster + no context: |
| 39 | auto subImg2 = rasterImg->makeSubset(subsetBounds, nullptr); |
| 40 | REPORTER_ASSERT(reporter, subImg2->isValid(static_cast<GrRecordingContext*>(nullptr))); |
| 41 | |
| 42 | // Texture image: |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 43 | auto surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, ii); |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 44 | SkSurfaceCharacterization sc; |
| 45 | REPORTER_ASSERT(reporter, surf->characterize(&sc)); |
Greg Daniel | 398ecf1 | 2020-08-26 16:33:54 -0400 | [diff] [blame] | 46 | GrBackendTexture tex = |
| 47 | dContext->createBackendTexture(ii.width(), ii.height(), ii.colorType(), |
| 48 | GrMipmapped(sc.isMipMapped()), GrRenderable::kYes); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 49 | auto gpuImage = SkImage::MakeFromTexture(dContext, tex, kTopLeft_GrSurfaceOrigin, |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 50 | ii.colorType(), ii.alphaType(), |
| 51 | ii.refColorSpace()); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 52 | REPORTER_ASSERT(reporter, gpuImage->isValid(dContext)); |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 53 | |
| 54 | // gpu image + context: |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 55 | auto subImg5 = gpuImage->makeSubset(subsetBounds, dContext); |
| 56 | REPORTER_ASSERT(reporter, subImg5->isValid(dContext)); |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 57 | |
| 58 | // gpu image + nullptr: |
| 59 | REPORTER_ASSERT(reporter, !gpuImage->makeSubset(subsetBounds, nullptr)); |
| 60 | |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 61 | dContext->flush(); |
| 62 | dContext->submit(true); |
| 63 | dContext->deleteBackendTexture(tex); |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 64 | } |
| 65 | } |