• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 "include/core/SkBitmap.h"
9 #include "include/core/SkImage.h"
10 #include "include/core/SkImageInfo.h"
11 #include "include/core/SkRect.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkTypes.h"
14 #include "include/gpu/GrDirectContext.h"
15 #include "include/gpu/GrTypes.h"
16 #include "src/core/SkDevice.h"
17 #include "src/core/SkSpecialImage.h"
18 #include "src/gpu/GrDirectContextPriv.h"
19 #include "tests/Test.h"
20 #include "tools/gpu/GrContextFactory.h"
21 
22 class SkColorSpace;
23 
24 class DeviceTestingAccess {
25 public:
MakeSpecial(SkBaseDevice * dev,const SkBitmap & bm)26     static sk_sp<SkSpecialImage> MakeSpecial(SkBaseDevice* dev, const SkBitmap& bm) {
27         return dev->makeSpecial(bm);
28     }
29 
MakeSpecial(SkBaseDevice * dev,SkImage * img)30     static sk_sp<SkSpecialImage> MakeSpecial(SkBaseDevice* dev, SkImage* img) {
31         return dev->makeSpecial(img);
32     }
33 
SnapSpecial(SkBaseDevice * dev)34     static sk_sp<SkSpecialImage> SnapSpecial(SkBaseDevice* dev) {
35         return dev->snapSpecial();
36     }
37 };
38 
39 // TODO: re-enable this when Raster methods are implemented
40 #if 0
41 DEF_TEST(SpecialImage_BitmapDevice, reporter) {
42     static const int kWidth = 100;
43     static const int kHeight = 90;
44 
45     SkImageInfo ii = SkImageInfo::MakeN32Premul(2*kWidth, 2*kHeight);
46 
47     sk_sp<SkBaseDevice> bmDev(SkBitmapDevice::Create(ii));
48 
49     SkBitmap bm;
50     bm.tryAllocN32Pixels(kWidth, kHeight);
51 
52     // Create a raster-backed special image from a raster-backed SkBitmap
53     sk_sp<SkSpecialImage> special = DeviceTestingAccess::MakeSpecial(bmDev.get(), bm);
54     SkASSERT(!special->isTextureBacked());
55     SkASSERT(kWidth == special->width());
56     SkASSERT(kHeight == special->height());
57     SkASSERT(bm.getGenerationID() == special->uniqueID());
58     SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
59 
60     // Create a raster-backed special image from a raster-backed SkImage
61     sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
62     special = DeviceTestingAccess::MakeSpecial(bmDev.get(), image.get());
63     SkASSERT(!special->isTextureBacked());
64     SkASSERT(kWidth == special->width());
65     SkASSERT(kHeight == special->height());
66     SkASSERT(bm.getGenerationID() == special->uniqueID());
67     SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
68 
69     // Snap the device as a raster-backed special image
70     special = DeviceTestingAccess::SnapSpecial(bmDev.get());
71     SkASSERT(!special->isTextureBacked());
72     SkASSERT(2*kWidth == special->width());
73     SkASSERT(2*kHeight == special->height());
74     SkASSERT(SkIRect::MakeWH(2*kWidth, 2*kHeight) == special->subset());
75 }
76 #endif
77 
78 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_GPUDevice,reporter,ctxInfo)79 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_GPUDevice, reporter, ctxInfo) {
80     auto dContext = ctxInfo.directContext();
81 
82     static const int kWidth = 100;
83     static const int kHeight = 90;
84 
85     SkImageInfo ii = SkImageInfo::MakeN32Premul(2*kWidth, 2*kHeight);
86 
87     auto device = dContext->priv().createDevice(SkBudgeted::kNo, ii, SkBackingFit::kExact,
88                                                 1, GrMipmapped::kNo, GrProtected::kNo,
89                                                 kBottomLeft_GrSurfaceOrigin, SkSurfaceProps(),
90                                                 skgpu::BaseDevice::InitContents::kClear);
91 
92     SkBitmap bm;
93     SkAssertResult(bm.tryAllocN32Pixels(kWidth, kHeight));
94 
95     // Create a gpu-backed special image from a raster-backed SkBitmap
96     sk_sp<SkSpecialImage> special = DeviceTestingAccess::MakeSpecial(device.get(), bm);
97     SkASSERT(special->isTextureBacked());
98     SkASSERT(kWidth == special->width());
99     SkASSERT(kHeight == special->height());
100     SkASSERT(bm.getGenerationID() == special->uniqueID());
101     SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
102 
103     // Create a gpu-backed special image from a raster-backed SkImage
104     sk_sp<SkImage> image(bm.asImage());
105     special = DeviceTestingAccess::MakeSpecial(device.get(), image.get());
106     SkASSERT(special->isTextureBacked());
107     SkASSERT(kWidth == special->width());
108     SkASSERT(kHeight == special->height());
109     // TODO: Hmmm, this is a bit unexpected
110     SkASSERT(image->uniqueID() != special->uniqueID());
111     SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
112 
113     // Create a gpu-backed special image from a gpu-backed SkImage
114     image = image->makeTextureImage(dContext);
115     special = DeviceTestingAccess::MakeSpecial(device.get(), image.get());
116     SkASSERT(special->isTextureBacked());
117     SkASSERT(kWidth == special->width());
118     SkASSERT(kHeight == special->height());
119     SkASSERT(image->uniqueID() == special->uniqueID());
120     SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
121 
122     // Snap the device as a gpu-backed special image
123     special = DeviceTestingAccess::SnapSpecial(device.get());
124     SkASSERT(special->isTextureBacked());
125     SkASSERT(2*kWidth == special->width());
126     SkASSERT(2*kHeight == special->height());
127     SkASSERT(SkIRect::MakeWH(2*kWidth, 2*kHeight) == special->subset());
128 }
129