• 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/SkCanvas.h"
9 #include "src/core/SkSpecialImage.h"
10 #include "src/core/SkSpecialSurface.h"
11 #include "src/core/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 "include/core/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::MakeAllocate(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 "include/gpu/GrBackendSurface.h"
121 #include "include/private/GrRecordingContext.h"
122 #include "src/gpu/GrRecordingContextPriv.h"
123 #include "src/gpu/SkGpuDevice.h"
124 
125 class SkSpecialSurface_Gpu : public SkSpecialSurface_Base {
126 public:
SkSpecialSurface_Gpu(GrRecordingContext * context,sk_sp<GrRenderTargetContext> renderTargetContext,int width,int height,const SkIRect & subset)127     SkSpecialSurface_Gpu(GrRecordingContext* context,
128                          sk_sp<GrRenderTargetContext> renderTargetContext,
129                          int width, int height, const SkIRect& subset)
130         : INHERITED(subset, &renderTargetContext->surfaceProps())
131         , fRenderTargetContext(std::move(renderTargetContext)) {
132 
133         // CONTEXT TODO: remove this use of 'backdoor' to create an SkGpuDevice
134         sk_sp<SkBaseDevice> device(SkGpuDevice::Make(context->priv().backdoor(),
135                                                      fRenderTargetContext, width, height,
136                                                      SkGpuDevice::kUninit_InitContents));
137         if (!device) {
138             return;
139         }
140 
141         fCanvas.reset(new SkCanvas(device));
142         fCanvas->clipRect(SkRect::Make(subset));
143 #ifdef SK_IS_BOT
144         fCanvas->clear(SK_ColorRED);  // catch any imageFilter sloppiness
145 #endif
146     }
147 
~SkSpecialSurface_Gpu()148     ~SkSpecialSurface_Gpu() override { }
149 
onMakeImageSnapshot()150     sk_sp<SkSpecialImage> onMakeImageSnapshot() override {
151         if (!fRenderTargetContext->asTextureProxy()) {
152             return nullptr;
153         }
154         sk_sp<SkSpecialImage> tmp(SkSpecialImage::MakeDeferredFromGpu(
155                 fCanvas->getGrContext(),
156                 this->subset(),
157                 kNeedNewImageUniqueID_SpecialImage,
158                 fRenderTargetContext->asTextureProxyRef(),
159                 fRenderTargetContext->colorSpaceInfo().refColorSpace(),
160                 &this->props()));
161         fRenderTargetContext = nullptr;
162         return tmp;
163     }
164 
165 private:
166     sk_sp<GrRenderTargetContext> fRenderTargetContext;
167 
168     typedef SkSpecialSurface_Base INHERITED;
169 };
170 
MakeRenderTarget(GrRecordingContext * context,int width,int height,GrColorType colorType,sk_sp<SkColorSpace> colorSpace,const SkSurfaceProps * props)171 sk_sp<SkSpecialSurface> SkSpecialSurface::MakeRenderTarget(GrRecordingContext* context,
172                                                            int width, int height,
173                                                            GrColorType colorType,
174                                                            sk_sp<SkColorSpace> colorSpace,
175                                                            const SkSurfaceProps* props) {
176     if (!context) {
177         return nullptr;
178     }
179     sk_sp<GrRenderTargetContext> renderTargetContext(
180             context->priv().makeDeferredRenderTargetContext(
181                     SkBackingFit::kApprox, width, height, colorType, std::move(colorSpace), 1,
182                     GrMipMapped::kNo, kBottomLeft_GrSurfaceOrigin, props));
183     if (!renderTargetContext) {
184         return nullptr;
185     }
186 
187     const SkIRect subset = SkIRect::MakeWH(width, height);
188 
189     return sk_make_sp<SkSpecialSurface_Gpu>(context, std::move(renderTargetContext),
190                                             width, height, subset);
191 }
192 
193 #endif
194