OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file | 5 * found in the LICENSE file |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
9 #include "SkSpecialImage.h" | 9 #include "SkSpecialImage.h" |
10 #include "SkSpecialSurface.h" | 10 #include "SkSpecialSurface.h" |
11 #include "SkSurfacePriv.h" | 11 #include "SkSurfacePriv.h" |
12 | 12 |
13 /////////////////////////////////////////////////////////////////////////////// | 13 /////////////////////////////////////////////////////////////////////////////// |
14 class SkSpecialSurface_Base : public SkSpecialSurface { | 14 class SkSpecialSurface_Base : public SkSpecialSurface { |
15 public: | 15 public: |
16 SkSpecialSurface_Base(SkImageFilter::Proxy* proxy, | 16 SkSpecialSurface_Base(SkImageFilter::Proxy* proxy, |
17 const SkIRect& subset, const SkSurfaceProps* props) | 17 const SkIRect& subset, const SkSurfaceProps* props) |
18 : INHERITED(proxy, subset, props) | 18 : INHERITED(proxy, subset, props) |
19 , fCanvas(nullptr) { | 19 , fCanvas(nullptr) { |
20 } | 20 } |
21 | 21 |
22 virtual ~SkSpecialSurface_Base() { } | 22 virtual ~SkSpecialSurface_Base() { } |
23 | 23 |
24 // reset is called after an SkSpecialImage has been snapped | 24 // reset is called after an SkSpecialImage has been snapped |
25 void reset() { fCanvas.reset(); } | 25 void reset() { fCanvas.reset(); } |
26 | 26 |
27 // This can return nullptr if reset has already been called or something whe
n wrong in the ctor | 27 // This can return nullptr if reset has already been called or something whe
n wrong in the ctor |
28 SkCanvas* onGetCanvas() { return fCanvas; } | 28 SkCanvas* onGetCanvas() { return fCanvas; } |
29 | 29 |
30 virtual SkSpecialImage* onNewImageSnapshot() = 0; | 30 virtual sk_sp<SkSpecialImage> onMakeImageSnapshot() = 0; |
31 | 31 |
32 protected: | 32 protected: |
33 SkAutoTUnref<SkCanvas> fCanvas; // initialized by derived classes in ctors | 33 SkAutoTUnref<SkCanvas> fCanvas; // initialized by derived classes in ctors |
34 | 34 |
35 private: | 35 private: |
36 typedef SkSpecialSurface INHERITED; | 36 typedef SkSpecialSurface INHERITED; |
37 }; | 37 }; |
38 | 38 |
39 /////////////////////////////////////////////////////////////////////////////// | 39 /////////////////////////////////////////////////////////////////////////////// |
40 static SkSpecialSurface_Base* as_SB(SkSpecialSurface* surface) { | 40 static SkSpecialSurface_Base* as_SB(SkSpecialSurface* surface) { |
41 return static_cast<SkSpecialSurface_Base*>(surface); | 41 return static_cast<SkSpecialSurface_Base*>(surface); |
42 } | 42 } |
43 | 43 |
44 SkSpecialSurface::SkSpecialSurface(SkImageFilter::Proxy* proxy, | 44 SkSpecialSurface::SkSpecialSurface(SkImageFilter::Proxy* proxy, |
45 const SkIRect& subset, | 45 const SkIRect& subset, |
46 const SkSurfaceProps* props) | 46 const SkSurfaceProps* props) |
47 : fProps(SkSurfacePropsCopyOrDefault(props)) | 47 : fProps(SkSurfacePropsCopyOrDefault(props)) |
48 , fSubset(subset) | 48 , fSubset(subset) |
49 , fProxy(proxy) { | 49 , fProxy(proxy) { |
50 SkASSERT(fSubset.width() > 0); | 50 SkASSERT(fSubset.width() > 0); |
51 SkASSERT(fSubset.height() > 0); | 51 SkASSERT(fSubset.height() > 0); |
52 } | 52 } |
53 | 53 |
54 SkCanvas* SkSpecialSurface::getCanvas() { | 54 SkCanvas* SkSpecialSurface::getCanvas() { |
55 return as_SB(this)->onGetCanvas(); | 55 return as_SB(this)->onGetCanvas(); |
56 } | 56 } |
57 | 57 |
58 SkSpecialImage* SkSpecialSurface::newImageSnapshot() { | 58 sk_sp<SkSpecialImage> SkSpecialSurface::makeImageSnapshot() { |
59 SkSpecialImage* image = as_SB(this)->onNewImageSnapshot(); | 59 sk_sp<SkSpecialImage> image(as_SB(this)->onMakeImageSnapshot()); |
60 as_SB(this)->reset(); | 60 as_SB(this)->reset(); |
61 return image; // the caller gets the creation ref | 61 return image; // the caller gets the creation ref |
62 } | 62 } |
63 | 63 |
64 /////////////////////////////////////////////////////////////////////////////// | 64 /////////////////////////////////////////////////////////////////////////////// |
65 #include "SkMallocPixelRef.h" | 65 #include "SkMallocPixelRef.h" |
66 | 66 |
67 class SkSpecialSurface_Raster : public SkSpecialSurface_Base { | 67 class SkSpecialSurface_Raster : public SkSpecialSurface_Base { |
68 public: | 68 public: |
69 SkSpecialSurface_Raster(SkImageFilter::Proxy* proxy, | 69 SkSpecialSurface_Raster(SkImageFilter::Proxy* proxy, |
70 SkPixelRef* pr, | 70 SkPixelRef* pr, |
71 const SkIRect& subset, | 71 const SkIRect& subset, |
72 const SkSurfaceProps* props) | 72 const SkSurfaceProps* props) |
73 : INHERITED(proxy, subset, props) { | 73 : INHERITED(proxy, subset, props) { |
74 const SkImageInfo& info = pr->info(); | 74 const SkImageInfo& info = pr->info(); |
75 | 75 |
76 fBitmap.setInfo(info, info.minRowBytes()); | 76 fBitmap.setInfo(info, info.minRowBytes()); |
77 fBitmap.setPixelRef(pr); | 77 fBitmap.setPixelRef(pr); |
78 | 78 |
79 fCanvas.reset(new SkCanvas(fBitmap)); | 79 fCanvas.reset(new SkCanvas(fBitmap)); |
80 } | 80 } |
81 | 81 |
82 ~SkSpecialSurface_Raster() override { } | 82 ~SkSpecialSurface_Raster() override { } |
83 | 83 |
84 SkSpecialImage* onNewImageSnapshot() override { | 84 sk_sp<SkSpecialImage> onMakeImageSnapshot() override { |
85 return SkSpecialImage::NewFromRaster(this->proxy(), this->subset(), fBit
map); | 85 return SkSpecialImage::MakeFromRaster(this->proxy(), this->subset(), fBi
tmap); |
86 } | 86 } |
87 | 87 |
88 private: | 88 private: |
89 SkBitmap fBitmap; | 89 SkBitmap fBitmap; |
90 | 90 |
91 typedef SkSpecialSurface_Base INHERITED; | 91 typedef SkSpecialSurface_Base INHERITED; |
92 }; | 92 }; |
93 | 93 |
94 SkSpecialSurface* SkSpecialSurface::NewFromBitmap(SkImageFilter::Proxy* proxy, | 94 sk_sp<SkSpecialSurface> SkSpecialSurface::MakeFromBitmap(SkImageFilter::Proxy* p
roxy, |
95 const SkIRect& subset, SkBitma
p& bm, | 95 const SkIRect& subset,
SkBitmap& bm, |
96 const SkSurfaceProps* props) { | 96 const SkSurfaceProps* p
rops) { |
97 return new SkSpecialSurface_Raster(proxy, bm.pixelRef(), subset, props); | 97 return sk_make_sp<SkSpecialSurface_Raster>(proxy, bm.pixelRef(), subset, pro
ps); |
98 } | 98 } |
99 | 99 |
100 SkSpecialSurface* SkSpecialSurface::NewRaster(SkImageFilter::Proxy* proxy, | 100 sk_sp<SkSpecialSurface> SkSpecialSurface::MakeRaster(SkImageFilter::Proxy* proxy
, |
101 const SkImageInfo& info, | 101 const SkImageInfo& info, |
102 const SkSurfaceProps* props) { | 102 const SkSurfaceProps* props
) { |
103 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, 0, nullptr)); | 103 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, 0, nullptr)); |
104 if (nullptr == pr.get()) { | 104 if (nullptr == pr.get()) { |
105 return nullptr; | 105 return nullptr; |
106 } | 106 } |
107 | 107 |
108 const SkIRect subset = SkIRect::MakeWH(pr->info().width(), pr->info().height
()); | 108 const SkIRect subset = SkIRect::MakeWH(pr->info().width(), pr->info().height
()); |
109 | 109 |
110 return new SkSpecialSurface_Raster(proxy, pr, subset, props); | 110 return sk_make_sp<SkSpecialSurface_Raster>(proxy, pr, subset, props); |
111 } | 111 } |
112 | 112 |
113 #if SK_SUPPORT_GPU | 113 #if SK_SUPPORT_GPU |
114 /////////////////////////////////////////////////////////////////////////////// | 114 /////////////////////////////////////////////////////////////////////////////// |
115 #include "GrContext.h" | 115 #include "GrContext.h" |
116 #include "SkGpuDevice.h" | 116 #include "SkGpuDevice.h" |
117 | 117 |
118 class SkSpecialSurface_Gpu : public SkSpecialSurface_Base { | 118 class SkSpecialSurface_Gpu : public SkSpecialSurface_Base { |
119 public: | 119 public: |
120 SkSpecialSurface_Gpu(SkImageFilter::Proxy* proxy, | 120 SkSpecialSurface_Gpu(SkImageFilter::Proxy* proxy, |
121 GrTexture* texture, | 121 GrTexture* texture, |
122 const SkIRect& subset, | 122 const SkIRect& subset, |
123 const SkSurfaceProps* props) | 123 const SkSurfaceProps* props) |
124 : INHERITED(proxy, subset, props) | 124 : INHERITED(proxy, subset, props) |
125 , fTexture(SkRef(texture)) { | 125 , fTexture(SkRef(texture)) { |
126 | 126 |
127 SkASSERT(fTexture->asRenderTarget()); | 127 SkASSERT(fTexture->asRenderTarget()); |
128 | 128 |
129 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(fTexture->asRenderT
arget(), props, | 129 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(fTexture->asRenderT
arget(), props, |
130 SkGpuDevice::kUnini
t_InitContents)); | 130 SkGpuDevice::kUnini
t_InitContents)); |
131 if (!device) { | 131 if (!device) { |
132 return; | 132 return; |
133 } | 133 } |
134 | 134 |
135 fCanvas.reset(new SkCanvas(device)); | 135 fCanvas.reset(new SkCanvas(device)); |
136 } | 136 } |
137 | 137 |
138 ~SkSpecialSurface_Gpu() override { } | 138 ~SkSpecialSurface_Gpu() override { } |
139 | 139 |
140 SkSpecialImage* onNewImageSnapshot() override { | 140 sk_sp<SkSpecialImage> onMakeImageSnapshot() override { |
141 return SkSpecialImage::NewFromGpu(this->proxy(), this->subset(), | 141 return SkSpecialImage::MakeFromGpu(this->proxy(), this->subset(), |
142 kNeedNewImageUniqueID_SpecialImage, fT
exture); | 142 kNeedNewImageUniqueID_SpecialImage, f
Texture); |
143 } | 143 } |
144 | 144 |
145 private: | 145 private: |
146 SkAutoTUnref<GrTexture> fTexture; | 146 SkAutoTUnref<GrTexture> fTexture; |
147 | 147 |
148 typedef SkSpecialSurface_Base INHERITED; | 148 typedef SkSpecialSurface_Base INHERITED; |
149 }; | 149 }; |
150 | 150 |
151 SkSpecialSurface* SkSpecialSurface::NewFromTexture(SkImageFilter::Proxy* proxy, | 151 sk_sp<SkSpecialSurface> SkSpecialSurface::MakeFromTexture(SkImageFilter::Proxy*
proxy, |
152 const SkIRect& subset, | 152 const SkIRect& subset, |
153 GrTexture* texture, | 153 GrTexture* texture, |
154 const SkSurfaceProps* props)
{ | 154 const SkSurfaceProps*
props) { |
155 if (!texture->asRenderTarget()) { | 155 if (!texture->asRenderTarget()) { |
156 return nullptr; | 156 return nullptr; |
157 } | 157 } |
158 | 158 |
159 return new SkSpecialSurface_Gpu(proxy, texture, subset, props); | 159 return sk_make_sp<SkSpecialSurface_Gpu>(proxy, texture, subset, props); |
160 } | 160 } |
161 | 161 |
162 SkSpecialSurface* SkSpecialSurface::NewRenderTarget(SkImageFilter::Proxy* proxy, | 162 sk_sp<SkSpecialSurface> SkSpecialSurface::MakeRenderTarget(SkImageFilter::Proxy*
proxy, |
163 GrContext* context, | 163 GrContext* context, |
164 const GrSurfaceDesc& desc, | 164 const GrSurfaceDesc&
desc, |
165 const SkSurfaceProps* props)
{ | 165 const SkSurfaceProps*
props) { |
166 if (!context || !SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag)) { | 166 if (!context || !SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag)) { |
167 return nullptr; | 167 return nullptr; |
168 } | 168 } |
169 | 169 |
170 SkAutoTUnref<GrTexture> temp(context->textureProvider()->createApproxTexture
(desc)); | 170 SkAutoTUnref<GrTexture> temp(context->textureProvider()->createApproxTexture
(desc)); |
171 if (!temp) { | 171 if (!temp) { |
172 return nullptr; | 172 return nullptr; |
173 } | 173 } |
174 | 174 |
175 const SkIRect subset = SkIRect::MakeWH(desc.fWidth, desc.fHeight); | 175 const SkIRect subset = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
176 | 176 |
177 return new SkSpecialSurface_Gpu(proxy, temp, subset, props); | 177 return sk_make_sp<SkSpecialSurface_Gpu>(proxy, temp, subset, props); |
178 } | 178 } |
179 | 179 |
180 #else | 180 #else |
181 | 181 |
182 SkSpecialSurface* SkSpecialSurface::NewFromTexture(SkImageFilter::Proxy* proxy, | 182 sk_sp<SkSpecialSurface> SkSpecialSurface::MakeFromTexture(SkImageFilter::Proxy*
proxy, |
183 const SkIRect& subset, | 183 const SkIRect& subset, |
184 GrTexture*, | 184 GrTexture*, |
185 const SkSurfaceProps*) { | 185 const SkSurfaceProps*)
{ |
186 return nullptr; | 186 return nullptr; |
187 } | 187 } |
188 | 188 |
189 SkSpecialSurface* SkSpecialSurface::NewRenderTarget(SkImageFilter::Proxy* proxy, | 189 sk_sp<SkSpecialSurface> SkSpecialSurface::MakeRenderTarget(SkImageFilter::Proxy*
proxy, |
190 GrContext* context, | 190 GrContext* context, |
191 const GrSurfaceDesc& desc, | 191 const GrSurfaceDesc&
desc, |
192 const SkSurfaceProps* props)
{ | 192 const SkSurfaceProps*
props) { |
193 return nullptr; | 193 return nullptr; |
194 } | 194 } |
195 | 195 |
196 #endif | 196 #endif |
OLD | NEW |