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