blob: 44faf3db882c1420f15fc0179fdb409b6b43bf12 [file] [log] [blame]
Adlai Holler872a32c2020-07-10 14:33:22 -04001/*
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
8#include "include/core/SkImage.h"
9#include "include/core/SkSurface.h"
10#include "include/core/SkSurfaceCharacterization.h"
11#include "tests/Test.h"
12
13DEF_GPUTEST(GrDDLImage_MakeSubset, reporter, options) {
14 sk_gpu_test::GrContextFactory factory(options);
15 for (int ct = 0; ct < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++ct) {
16 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(ct);
Adlai Holler14dc7912020-08-11 15:48:49 +000017 auto dContext = factory.get(contextType);
18 if (!dContext) {
Adlai Holler872a32c2020-07-10 14:33:22 -040019 continue;
20 }
21 SkIRect subsetBounds = SkIRect::MakeLTRB(4,4,8,8);
22 SkImageInfo ii = SkImageInfo::Make(16, 16, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
23
24 // Raster image:
25 SkBitmap bm;
26 bm.setInfo(ii);
27 bm.allocPixels();
28 bm.eraseColor(SK_ColorBLACK);
29 bm.setImmutable();
30 auto rasterImg = SkImage::MakeFromBitmap(bm);
31 REPORTER_ASSERT(reporter, rasterImg->isValid(static_cast<GrRecordingContext*>(nullptr)));
32
33 // raster + context:
Adlai Holler14dc7912020-08-11 15:48:49 +000034 auto subImg1 = rasterImg->makeSubset(subsetBounds, dContext);
35 REPORTER_ASSERT(reporter, subImg1->isValid(dContext));
Adlai Holler872a32c2020-07-10 14:33:22 -040036
37 // raster + no context:
38 auto subImg2 = rasterImg->makeSubset(subsetBounds, nullptr);
39 REPORTER_ASSERT(reporter, subImg2->isValid(static_cast<GrRecordingContext*>(nullptr)));
40
41 // Texture image:
Adlai Holler14dc7912020-08-11 15:48:49 +000042 auto surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, ii);
Adlai Holler872a32c2020-07-10 14:33:22 -040043 SkSurfaceCharacterization sc;
44 REPORTER_ASSERT(reporter, surf->characterize(&sc));
Greg Daniel398ecf12020-08-26 16:33:54 -040045 GrBackendTexture tex =
46 dContext->createBackendTexture(ii.width(), ii.height(), ii.colorType(),
47 GrMipmapped(sc.isMipMapped()), GrRenderable::kYes);
Adlai Holler14dc7912020-08-11 15:48:49 +000048 auto gpuImage = SkImage::MakeFromTexture(dContext, tex, kTopLeft_GrSurfaceOrigin,
Adlai Holler872a32c2020-07-10 14:33:22 -040049 ii.colorType(), ii.alphaType(),
50 ii.refColorSpace());
Adlai Holler14dc7912020-08-11 15:48:49 +000051 REPORTER_ASSERT(reporter, gpuImage->isValid(dContext));
Adlai Holler872a32c2020-07-10 14:33:22 -040052
53 // gpu image + context:
Adlai Holler14dc7912020-08-11 15:48:49 +000054 auto subImg5 = gpuImage->makeSubset(subsetBounds, dContext);
55 REPORTER_ASSERT(reporter, subImg5->isValid(dContext));
Adlai Holler872a32c2020-07-10 14:33:22 -040056
57 // gpu image + nullptr:
58 REPORTER_ASSERT(reporter, !gpuImage->makeSubset(subsetBounds, nullptr));
59
Adlai Holler14dc7912020-08-11 15:48:49 +000060 dContext->flush();
61 dContext->submit(true);
62 dContext->deleteBackendTexture(tex);
Adlai Holler872a32c2020-07-10 14:33:22 -040063 }
64}