• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "include/core/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkSurface.h"
17 #include "include/core/SkSurfaceCharacterization.h"
18 #include "include/core/SkTypes.h"
19 #include "include/gpu/GpuTypes.h"
20 #include "include/gpu/GrBackendSurface.h"
21 #include "include/gpu/GrDirectContext.h"
22 #include "include/gpu/GrTypes.h"
23 #include "tests/CtsEnforcement.h"
24 #include "tests/Test.h"
25 
26 class GrRecordingContext;
27 struct GrContextOptions;
28 
DEF_GANESH_TEST(GrDDLImage_MakeSubset,reporter,options,CtsEnforcement::kApiLevel_T)29 DEF_GANESH_TEST(GrDDLImage_MakeSubset, reporter, options, CtsEnforcement::kApiLevel_T) {
30     sk_gpu_test::GrContextFactory factory(options);
31     for (int ct = 0; ct < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++ct) {
32         auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(ct);
33         auto dContext = factory.get(contextType);
34         if (!dContext) {
35             continue;
36         }
37         SkIRect subsetBounds = SkIRect::MakeLTRB(4,4,8,8);
38         SkImageInfo ii = SkImageInfo::Make(16, 16, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
39 
40         // Raster image:
41         SkBitmap bm;
42         bm.setInfo(ii);
43         bm.allocPixels();
44         bm.eraseColor(SK_ColorBLACK);
45         bm.setImmutable();
46         auto rasterImg = bm.asImage();
47         REPORTER_ASSERT(reporter, rasterImg->isValid(static_cast<GrRecordingContext*>(nullptr)));
48 
49         // raster + context:
50         auto subImg1 = rasterImg->makeSubset(subsetBounds, dContext);
51         REPORTER_ASSERT(reporter, subImg1->isValid(dContext));
52 
53         // raster + no context:
54         auto subImg2 = rasterImg->makeSubset(subsetBounds);
55         REPORTER_ASSERT(reporter, subImg2->isValid(static_cast<GrRecordingContext*>(nullptr)));
56 
57         // Texture image:
58         auto surf = SkSurface::MakeRenderTarget(dContext, skgpu::Budgeted::kNo, ii);
59         SkSurfaceCharacterization sc;
60         REPORTER_ASSERT(reporter, surf->characterize(&sc));
61         GrBackendTexture tex = dContext->createBackendTexture(ii.width(),
62                                                               ii.height(),
63                                                               ii.colorType(),
64                                                               GrMipmapped(sc.isMipMapped()),
65                                                               GrRenderable::kYes);
66         auto gpuImage = SkImage::MakeFromTexture(dContext, tex, kTopLeft_GrSurfaceOrigin,
67                                                  ii.colorType(), ii.alphaType(),
68                                                  ii.refColorSpace());
69         REPORTER_ASSERT(reporter, gpuImage->isValid(dContext));
70 
71         // gpu image + context:
72         auto subImg5 = gpuImage->makeSubset(subsetBounds, dContext);
73         REPORTER_ASSERT(reporter, subImg5->isValid(dContext));
74 
75         // gpu image + nullptr:
76         REPORTER_ASSERT(reporter, !gpuImage->makeSubset(subsetBounds));
77 
78         dContext->flush();
79         dContext->submit(true);
80         dContext->deleteBackendTexture(tex);
81     }
82 }
83