1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file
*/
#include "SkCanvas.h"
#include "SkSpecialImage.h"
#include "SkSpecialSurface.h"
#include "SkSurfacePriv.h"
///////////////////////////////////////////////////////////////////////////////
class SkSpecialSurface_Base : public SkSpecialSurface {
public:
SkSpecialSurface_Base(const SkIRect& subset, const SkSurfaceProps* props)
: INHERITED(subset, props)
, fCanvas(nullptr) {
}
virtual ~SkSpecialSurface_Base() { }
// reset is called after an SkSpecialImage has been snapped
void reset() { fCanvas.reset(); }
// This can return nullptr if reset has already been called or something when wrong in the ctor
SkCanvas* onGetCanvas() { return fCanvas.get(); }
virtual sk_sp<SkSpecialImage> onMakeImageSnapshot() = 0;
protected:
std::unique_ptr<SkCanvas> fCanvas; // initialized by derived classes in ctors
private:
typedef SkSpecialSurface INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
static SkSpecialSurface_Base* as_SB(SkSpecialSurface* surface) {
return static_cast<SkSpecialSurface_Base*>(surface);
}
SkSpecialSurface::SkSpecialSurface(const SkIRect& subset,
const SkSurfaceProps* props)
: fProps(SkSurfacePropsCopyOrDefault(props).flags(), kUnknown_SkPixelGeometry)
, fSubset(subset) {
SkASSERT(fSubset.width() > 0);
SkASSERT(fSubset.height() > 0);
}
SkCanvas* SkSpecialSurface::getCanvas() {
return as_SB(this)->onGetCanvas();
}
sk_sp<SkSpecialImage> SkSpecialSurface::makeImageSnapshot() {
sk_sp<SkSpecialImage> image(as_SB(this)->onMakeImageSnapshot());
as_SB(this)->reset();
return image; // the caller gets the creation ref
}
///////////////////////////////////////////////////////////////////////////////
#include "SkMallocPixelRef.h"
class SkSpecialSurface_Raster : public SkSpecialSurface_Base {
public:
SkSpecialSurface_Raster(const SkImageInfo& info,
sk_sp<SkPixelRef> pr,
const SkIRect& subset,
const SkSurfaceProps* props)
: INHERITED(subset, props) {
SkASSERT(info.width() == pr->width() && info.height() == pr->height());
fBitmap.setInfo(info, info.minRowBytes());
fBitmap.setPixelRef(std::move(pr), 0, 0);
fCanvas.reset(new SkCanvas(fBitmap, this->props()));
fCanvas->clipRect(SkRect::Make(subset));
#ifdef SK_IS_BOT
fCanvas->clear(SK_ColorRED); // catch any imageFilter sloppiness
#endif
}
~SkSpecialSurface_Raster() override { }
sk_sp<SkSpecialImage> onMakeImageSnapshot() override {
return SkSpecialImage::MakeFromRaster(this->subset(), fBitmap, &this->props());
}
private:
SkBitmap fBitmap;
typedef SkSpecialSurface_Base INHERITED;
};
sk_sp<SkSpecialSurface> SkSpecialSurface::MakeFromBitmap(const SkIRect& subset, SkBitmap& bm,
const SkSurfaceProps* props) {
if (subset.isEmpty() || !SkSurfaceValidateRasterInfo(bm.info(), bm.rowBytes())) {
return nullptr;
}
return sk_make_sp<SkSpecialSurface_Raster>(bm.info(), sk_ref_sp(bm.pixelRef()), subset, props);
}
sk_sp<SkSpecialSurface> SkSpecialSurface::MakeRaster(const SkImageInfo& info,
const SkSurfaceProps* props) {
if (!SkSurfaceValidateRasterInfo(info)) {
return nullptr;
}
sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeZeroed(info, 0, nullptr);
if (!pr) {
return nullptr;
}
const SkIRect subset = SkIRect::MakeWH(info.width(), info.height());
return sk_make_sp<SkSpecialSurface_Raster>(info, std::move(pr), subset, props);
}
#if SK_SUPPORT_GPU
///////////////////////////////////////////////////////////////////////////////
#include "GrContext.h"
#include "SkGpuDevice.h"
class SkSpecialSurface_Gpu : public SkSpecialSurface_Base {
public:
SkSpecialSurface_Gpu(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
int width, int height, const SkIRect& subset)
: INHERITED(subset, &renderTargetContext->surfaceProps())
, fRenderTargetContext(std::move(renderTargetContext)) {
sk_sp<SkBaseDevice> device(SkGpuDevice::Make(context, fRenderTargetContext, width, height,
SkGpuDevice::kUninit_InitContents));
if (!device) {
return;
}
fCanvas.reset(new SkCanvas(device.get()));
fCanvas->clipRect(SkRect::Make(subset));
#ifdef SK_IS_BOT
fCanvas->clear(SK_ColorRED); // catch any imageFilter sloppiness
#endif
}
~SkSpecialSurface_Gpu() override { }
sk_sp<SkSpecialImage> onMakeImageSnapshot() override {
if (!fRenderTargetContext->asTextureProxy()) {
return nullptr;
}
sk_sp<SkSpecialImage> tmp(SkSpecialImage::MakeDeferredFromGpu(
fCanvas->getGrContext(),
this->subset(),
kNeedNewImageUniqueID_SpecialImage,
fRenderTargetContext->asTextureProxyRef(),
fRenderTargetContext->refColorSpace(),
&this->props()));
fRenderTargetContext = nullptr;
return tmp;
}
private:
sk_sp<GrRenderTargetContext> fRenderTargetContext;
typedef SkSpecialSurface_Base INHERITED;
};
sk_sp<SkSpecialSurface> SkSpecialSurface::MakeRenderTarget(GrContext* context,
int width, int height,
GrPixelConfig config,
sk_sp<SkColorSpace> colorSpace) {
if (!context) {
return nullptr;
}
sk_sp<GrRenderTargetContext> renderTargetContext(context->makeDeferredRenderTargetContext(
SkBackingFit::kApprox, width, height, config, std::move(colorSpace)));
if (!renderTargetContext) {
return nullptr;
}
const SkIRect subset = SkIRect::MakeWH(width, height);
return sk_make_sp<SkSpecialSurface_Gpu>(context, std::move(renderTargetContext),
width, height, subset);
}
#endif
|