Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: src/core/SkSpecialImage.cpp

Issue 2167813002: have MakeFromImage fission the SkImage into a raster or a texture (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "SkSpecialImage.h" 8 #include "SkSpecialImage.h"
8 9 #include "SkBitmap.h"
9 #if SK_SUPPORT_GPU 10 #include "SkImage.h"
10 #include "GrTexture.h"
11 #include "GrTextureParams.h"
12 #include "SkGr.h"
13 #endif
14
15 #include "SkBitmapCache.h" 11 #include "SkBitmapCache.h"
16 #include "SkCanvas.h" 12 #include "SkCanvas.h"
17 #include "SkImage_Base.h" 13 #include "SkImage_Base.h"
18 #include "SkSpecialSurface.h" 14 #include "SkSpecialSurface.h"
19 #include "SkSurfacePriv.h" 15 #include "SkSurfacePriv.h"
16 #include "SkPixelRef.h"
17
18 #if SK_SUPPORT_GPU
19 #include "GrContext.h"
20 #include "GrTexture.h"
21 #include "GrTextureParams.h"
22 #include "SkGr.h"
robertphillips 2016/07/20 21:20:58 Do we still need SkGrPixelRef.h ?
23 #include "SkGrPixelRef.h"
24 #include "SkGrPriv.h"
25 #endif
20 26
21 // Currently the raster imagefilters can only handle certain imageinfos. Call th is to know if 27 // Currently the raster imagefilters can only handle certain imageinfos. Call th is to know if
22 // a given info is supported. 28 // a given info is supported.
23 static bool valid_for_imagefilters(const SkImageInfo& info) { 29 static bool valid_for_imagefilters(const SkImageInfo& info) {
24 // no support for other swizzles/depths yet 30 // no support for other swizzles/depths yet
25 return info.colorType() == kN32_SkColorType; 31 return info.colorType() == kN32_SkColorType;
26 } 32 }
27 33
28 /////////////////////////////////////////////////////////////////////////////// 34 ///////////////////////////////////////////////////////////////////////////////
29 class SkSpecialImage_Base : public SkSpecialImage { 35 class SkSpecialImage_Base : public SkSpecialImage {
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 } 159 }
154 160
155 sk_sp<SkSpecialImage> SkSpecialImage::makeSubset(const SkIRect& subset) const { 161 sk_sp<SkSpecialImage> SkSpecialImage::makeSubset(const SkIRect& subset) const {
156 return as_SIB(this)->onMakeSubset(subset); 162 return as_SIB(this)->onMakeSubset(subset);
157 } 163 }
158 164
159 sk_sp<SkImage> SkSpecialImage::makeTightSubset(const SkIRect& subset) const { 165 sk_sp<SkImage> SkSpecialImage::makeTightSubset(const SkIRect& subset) const {
160 return as_SIB(this)->onMakeTightSubset(subset); 166 return as_SIB(this)->onMakeTightSubset(subset);
161 } 167 }
162 168
163 #if SK_SUPPORT_GPU
164 #include "SkGr.h"
165 #include "SkGrPixelRef.h"
166 #endif
167
168 ///////////////////////////////////////////////////////////////////////////////
169 #include "SkImage.h"
170 #if SK_SUPPORT_GPU
171 #include "GrContext.h"
172 #include "SkGrPriv.h"
173 #endif
174
175 class SkSpecialImage_Image : public SkSpecialImage_Base {
176 public:
177 SkSpecialImage_Image(const SkIRect& subset,
178 sk_sp<SkImage> image,
179 const SkSurfaceProps* props)
180 : INHERITED(subset, image->uniqueID(), props)
181 , fImage(image) {
182 }
183
184 ~SkSpecialImage_Image() override { }
185
186 bool isOpaque() const override { return fImage->isOpaque(); }
187
188 size_t getSize() const override {
189 #if SK_SUPPORT_GPU
190 if (GrTexture* texture = as_IB(fImage.get())->peekTexture()) {
191 return texture->gpuMemorySize();
192 } else
193 #endif
194 {
195 SkPixmap pm;
196 if (fImage->peekPixels(&pm)) {
197 return pm.height() * pm.rowBytes();
198 }
199 }
200 return 0;
201 }
202
203 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override {
204 SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset ().height());
205
206 canvas->drawImageRect(fImage.get(), this->subset(),
207 dst, paint, SkCanvas::kStrict_SrcRectConstraint);
208 }
209
210 bool onGetROPixels(SkBitmap* bm) const override {
211 return as_IB(fImage)->getROPixels(bm);
212 }
213
214 GrTexture* onPeekTexture() const override { return as_IB(fImage)->peekTextur e(); }
215
216 #if SK_SUPPORT_GPU
217 sk_sp<GrTexture> onAsTextureRef(GrContext* context) const override {
218 return sk_sp<GrTexture>(as_IB(fImage)->asTextureRef(context,
219 GrTextureParams::Cla mpNoFilter(),
220 SkSourceGammaTreatme nt::kRespect));
221 }
222 #endif
223
224 bool getBitmapDeprecated(SkBitmap* result) const override {
225 #if SK_SUPPORT_GPU
226 if (GrTexture* texture = as_IB(fImage.get())->peekTexture()) {
227 const SkImageInfo info = GrMakeInfoFromTexture(texture,
228 fImage->width(), fIma ge->height(),
229 fImage->isOpaque());
230 if (!result->setInfo(info)) {
231 return false;
232 }
233
234 result->setPixelRef(new SkGrPixelRef(info, texture))->unref();
235 return true;
236 }
237 #endif
238
239 return as_IB(fImage.get())->asBitmapForImageFilters(result);
240 }
241
242 sk_sp<SkSpecialSurface> onMakeSurface(const SkImageInfo& info) const overrid e {
243 #if SK_SUPPORT_GPU
244 GrTexture* texture = as_IB(fImage.get())->peekTexture();
245 if (texture) {
246 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *texture->get Context()->caps());
247
248 return SkSpecialSurface::MakeRenderTarget(texture->getContext(),
249 info.width(),
250 info.height(),
251 config);
252 }
253 #endif
254 return SkSpecialSurface::MakeRaster(info, nullptr);
255 }
256
257 sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override {
258 sk_sp<SkImage> subsetImg(fImage->makeSubset(subset));
259 if (!subsetImg) {
260 return nullptr;
261 }
262
263 return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(subset.width(), sub set.height()),
264 subsetImg,
265 &this->props());
266 }
267
268 sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override {
269 return fImage->makeSubset(subset);
270 }
271
272 sk_sp<SkSurface> onMakeTightSurface(const SkImageInfo& info) const override {
273 #if SK_SUPPORT_GPU
274 GrTexture* texture = as_IB(fImage.get())->peekTexture();
275 if (texture) {
276 return SkSurface::MakeRenderTarget(texture->getContext(), SkBudgeted ::kYes, info);
277 }
278 #endif
279 return SkSurface::MakeRaster(info, nullptr);
280 }
281
282 private:
283 sk_sp<SkImage> fImage;
284
285 typedef SkSpecialImage_Base INHERITED;
286 };
287
288 #ifdef SK_DEBUG 169 #ifdef SK_DEBUG
289 static bool rect_fits(const SkIRect& rect, int width, int height) { 170 static bool rect_fits(const SkIRect& rect, int width, int height) {
290 if (0 == width && 0 == height) { 171 if (0 == width && 0 == height) {
291 SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == r ect.fBottom); 172 SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == r ect.fBottom);
292 return true; 173 return true;
293 } 174 }
294 175
295 return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight && 176 return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight &&
296 rect.fRight >= 0 && rect.fRight <= width && 177 rect.fRight >= 0 && rect.fRight <= width &&
297 rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom && 178 rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom &&
298 rect.fBottom >= 0 && rect.fBottom <= height; 179 rect.fBottom >= 0 && rect.fBottom <= height;
299 } 180 }
300 #endif 181 #endif
301 182
302 sk_sp<SkSpecialImage> SkSpecialImage::MakeFromImage(const SkIRect& subset, 183 sk_sp<SkSpecialImage> SkSpecialImage::MakeFromImage(const SkIRect& subset,
303 sk_sp<SkImage> image, 184 sk_sp<SkImage> image,
304 const SkSurfaceProps* props) { 185 const SkSurfaceProps* props) {
305 SkASSERT(rect_fits(subset, image->width(), image->height())); 186 SkASSERT(rect_fits(subset, image->width(), image->height()));
306 187
307 if (valid_for_imagefilters(as_IB(image.get())->onImageInfo())) { 188 if (GrTexture* texture = as_IB(image)->peekTexture()) {
308 return sk_make_sp<SkSpecialImage_Image>(subset, image, props); 189 return MakeFromGpu(subset, image->uniqueID(), sk_ref_sp(texture), props) ;
309 } else { 190 } else {
310 return nullptr; 191 SkBitmap bm;
192 if (as_IB(image)->getROPixels(&bm)) {
193 return MakeFromRaster(subset, bm, props);
194 }
311 } 195 }
196 return nullptr;
312 } 197 }
313 198
314 /////////////////////////////////////////////////////////////////////////////// 199 ///////////////////////////////////////////////////////////////////////////////
315 #include "SkBitmap.h"
316 #include "SkImageInfo.h"
317 #include "SkPixelRef.h"
318 200
319 class SkSpecialImage_Raster : public SkSpecialImage_Base { 201 class SkSpecialImage_Raster : public SkSpecialImage_Base {
320 public: 202 public:
321 SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm, const SkSur faceProps* props) 203 SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm, const SkSur faceProps* props)
322 : INHERITED(subset, bm.getGenerationID(), props) 204 : INHERITED(subset, bm.getGenerationID(), props)
323 , fBitmap(bm) { 205 , fBitmap(bm) {
324 if (bm.pixelRef() && bm.pixelRef()->isPreLocked()) { 206 if (bm.pixelRef() && bm.pixelRef()->isPreLocked()) {
325 // we only preemptively lock if there is no chance of triggering som ething expensive 207 // we only preemptively lock if there is no chance of triggering som ething expensive
326 // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already. 208 // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already.
327 fBitmap.lockPixels(); 209 fBitmap.lockPixels();
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 sk_sp<SkSpecialImage> SkSpecialImage::MakeFromGpu(const SkIRect& subset, 444 sk_sp<SkSpecialImage> SkSpecialImage::MakeFromGpu(const SkIRect& subset,
563 uint32_t uniqueID, 445 uint32_t uniqueID,
564 sk_sp<GrTexture> tex, 446 sk_sp<GrTexture> tex,
565 const SkSurfaceProps* props, 447 const SkSurfaceProps* props,
566 SkAlphaType at) { 448 SkAlphaType at) {
567 SkASSERT(rect_fits(subset, tex->width(), tex->height())); 449 SkASSERT(rect_fits(subset, tex->width(), tex->height()));
568 return sk_make_sp<SkSpecialImage_Gpu>(subset, uniqueID, std::move(tex), at, props); 450 return sk_make_sp<SkSpecialImage_Gpu>(subset, uniqueID, std::move(tex), at, props);
569 } 451 }
570 452
571 #endif 453 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698