1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2020 Google LLC
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/core/SkBitmap.h"
9cb93a386Sopenharmony_ci#include "include/core/SkImage.h"
10cb93a386Sopenharmony_ci#include "include/core/SkSurface.h"
11cb93a386Sopenharmony_ci#include "include/core/SkSurfaceCharacterization.h"
12cb93a386Sopenharmony_ci#include "tests/Test.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ciDEF_GPUTEST(GrDDLImage_MakeSubset, reporter, options) {
15cb93a386Sopenharmony_ci    sk_gpu_test::GrContextFactory factory(options);
16cb93a386Sopenharmony_ci    for (int ct = 0; ct < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++ct) {
17cb93a386Sopenharmony_ci        auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(ct);
18cb93a386Sopenharmony_ci        auto dContext = factory.get(contextType);
19cb93a386Sopenharmony_ci        if (!dContext) {
20cb93a386Sopenharmony_ci            continue;
21cb93a386Sopenharmony_ci        }
22cb93a386Sopenharmony_ci        SkIRect subsetBounds = SkIRect::MakeLTRB(4,4,8,8);
23cb93a386Sopenharmony_ci        SkImageInfo ii = SkImageInfo::Make(16, 16, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_ci        // Raster image:
26cb93a386Sopenharmony_ci        SkBitmap bm;
27cb93a386Sopenharmony_ci        bm.setInfo(ii);
28cb93a386Sopenharmony_ci        bm.allocPixels();
29cb93a386Sopenharmony_ci        bm.eraseColor(SK_ColorBLACK);
30cb93a386Sopenharmony_ci        bm.setImmutable();
31cb93a386Sopenharmony_ci        auto rasterImg = bm.asImage();
32cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, rasterImg->isValid(static_cast<GrRecordingContext*>(nullptr)));
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_ci        // raster + context:
35cb93a386Sopenharmony_ci        auto subImg1 = rasterImg->makeSubset(subsetBounds, dContext);
36cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, subImg1->isValid(dContext));
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ci        // raster + no context:
39cb93a386Sopenharmony_ci        auto subImg2 = rasterImg->makeSubset(subsetBounds, nullptr);
40cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, subImg2->isValid(static_cast<GrRecordingContext*>(nullptr)));
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci        // Texture image:
43cb93a386Sopenharmony_ci        auto surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, ii);
44cb93a386Sopenharmony_ci        SkSurfaceCharacterization sc;
45cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, surf->characterize(&sc));
46cb93a386Sopenharmony_ci        GrBackendTexture tex =
47cb93a386Sopenharmony_ci                dContext->createBackendTexture(ii.width(), ii.height(), ii.colorType(),
48cb93a386Sopenharmony_ci                                               GrMipmapped(sc.isMipMapped()), GrRenderable::kYes);
49cb93a386Sopenharmony_ci        auto gpuImage = SkImage::MakeFromTexture(dContext, tex, kTopLeft_GrSurfaceOrigin,
50cb93a386Sopenharmony_ci                                                 ii.colorType(), ii.alphaType(),
51cb93a386Sopenharmony_ci                                                 ii.refColorSpace());
52cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, gpuImage->isValid(dContext));
53cb93a386Sopenharmony_ci
54cb93a386Sopenharmony_ci        // gpu image + context:
55cb93a386Sopenharmony_ci        auto subImg5 = gpuImage->makeSubset(subsetBounds, dContext);
56cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, subImg5->isValid(dContext));
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci        // gpu image + nullptr:
59cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, !gpuImage->makeSubset(subsetBounds, nullptr));
60cb93a386Sopenharmony_ci
61cb93a386Sopenharmony_ci        dContext->flush();
62cb93a386Sopenharmony_ci        dContext->submit(true);
63cb93a386Sopenharmony_ci        dContext->deleteBackendTexture(tex);
64cb93a386Sopenharmony_ci    }
65cb93a386Sopenharmony_ci}
66