OLD | NEW |
(Empty) | |
| 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: |
| 16 SkSpecialSurface_Base(const SkIRect& subset, const SkSurfaceProps* props) |
| 17 : INHERITED(subset, props) |
| 18 , fCanvas(nullptr) { |
| 19 } |
| 20 |
| 21 virtual ~SkSpecialSurface_Base() { } |
| 22 |
| 23 // reset is called after an SkSpecialImage has been snapped |
| 24 void reset() { fCanvas.reset(); } |
| 25 |
| 26 // This can return nullptr if reset has already been called or something whe
n wrong in the ctor |
| 27 SkCanvas* onGetCanvas() { return fCanvas; } |
| 28 |
| 29 virtual SkSpecialImage* onNewImageSnapshot() = 0; |
| 30 |
| 31 protected: |
| 32 SkAutoTUnref<SkCanvas> fCanvas; // initialized by derived classes in ctors |
| 33 |
| 34 private: |
| 35 typedef SkSpecialSurface INHERITED; |
| 36 }; |
| 37 |
| 38 /////////////////////////////////////////////////////////////////////////////// |
| 39 static SkSpecialSurface_Base* as_SB(SkSpecialSurface* surface) { |
| 40 return static_cast<SkSpecialSurface_Base*>(surface); |
| 41 } |
| 42 |
| 43 SkSpecialSurface::SkSpecialSurface(const SkIRect& subset, const SkSurfaceProps*
props) |
| 44 : fProps(SkSurfacePropsCopyOrDefault(props)) |
| 45 , fSubset(subset) { |
| 46 SkASSERT(fSubset.width() > 0); |
| 47 SkASSERT(fSubset.height() > 0); |
| 48 } |
| 49 |
| 50 SkCanvas* SkSpecialSurface::getCanvas() { |
| 51 return as_SB(this)->onGetCanvas(); |
| 52 } |
| 53 |
| 54 SkSpecialImage* SkSpecialSurface::newImageSnapshot() { |
| 55 SkSpecialImage* image = as_SB(this)->onNewImageSnapshot(); |
| 56 as_SB(this)->reset(); |
| 57 return SkSafeRef(image); // the caller will call unref() to balance this |
| 58 } |
| 59 |
| 60 /////////////////////////////////////////////////////////////////////////////// |
| 61 #include "SkMallocPixelRef.h" |
| 62 |
| 63 class SkSpecialSurface_Raster : public SkSpecialSurface_Base { |
| 64 public: |
| 65 SkSpecialSurface_Raster(SkPixelRef* pr, const SkIRect& subset, const SkSurfa
ceProps* props) |
| 66 : INHERITED(subset, props) { |
| 67 const SkImageInfo& info = pr->info(); |
| 68 |
| 69 fBitmap.setInfo(info, info.minRowBytes()); |
| 70 fBitmap.setPixelRef(pr); |
| 71 |
| 72 fCanvas.reset(new SkCanvas(fBitmap)); |
| 73 } |
| 74 |
| 75 ~SkSpecialSurface_Raster() override { } |
| 76 |
| 77 SkSpecialImage* onNewImageSnapshot() override { |
| 78 return SkSpecialImage::NewFromRaster(this->subset(), fBitmap); |
| 79 } |
| 80 |
| 81 private: |
| 82 SkBitmap fBitmap; |
| 83 |
| 84 typedef SkSpecialSurface_Base INHERITED; |
| 85 }; |
| 86 |
| 87 SkSpecialSurface* SkSpecialSurface::NewFromBitmap(const SkIRect& subset, SkBitma
p& bm, |
| 88 const SkSurfaceProps* props) { |
| 89 return new SkSpecialSurface_Raster(bm.pixelRef(), subset, props); |
| 90 } |
| 91 |
| 92 SkSpecialSurface* SkSpecialSurface::NewRaster(const SkImageInfo& info, |
| 93 const SkSurfaceProps* props) { |
| 94 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, 0, nullptr)); |
| 95 if (nullptr == pr.get()) { |
| 96 return nullptr; |
| 97 } |
| 98 |
| 99 const SkIRect subset = SkIRect::MakeWH(pr->info().width(), pr->info().height
()); |
| 100 |
| 101 return new SkSpecialSurface_Raster(pr, subset, props); |
| 102 } |
| 103 |
| 104 #if SK_SUPPORT_GPU |
| 105 /////////////////////////////////////////////////////////////////////////////// |
| 106 #include "GrContext.h" |
| 107 #include "SkGpuDevice.h" |
| 108 |
| 109 class SkSpecialSurface_Gpu : public SkSpecialSurface_Base { |
| 110 public: |
| 111 SkSpecialSurface_Gpu(GrTexture* texture, const SkIRect& subset, const SkSurf
aceProps* props) |
| 112 : INHERITED(subset, props) |
| 113 , fTexture(texture) { |
| 114 |
| 115 SkASSERT(fTexture->asRenderTarget()); |
| 116 |
| 117 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(fTexture->asRenderT
arget(), props, |
| 118 SkGpuDevice::kUnini
t_InitContents)); |
| 119 if (!device) { |
| 120 return; |
| 121 } |
| 122 |
| 123 fCanvas.reset(new SkCanvas(device)); |
| 124 } |
| 125 |
| 126 ~SkSpecialSurface_Gpu() override { } |
| 127 |
| 128 SkSpecialImage* onNewImageSnapshot() override { |
| 129 return SkSpecialImage::NewFromGpu(this->subset(), fTexture); |
| 130 } |
| 131 |
| 132 private: |
| 133 SkAutoTUnref<GrTexture> fTexture; |
| 134 |
| 135 typedef SkSpecialSurface_Base INHERITED; |
| 136 }; |
| 137 |
| 138 SkSpecialSurface* SkSpecialSurface::NewFromTexture(const SkIRect& subset, GrText
ure* texture, |
| 139 const SkSurfaceProps* props)
{ |
| 140 if (!texture->asRenderTarget()) { |
| 141 return nullptr; |
| 142 } |
| 143 |
| 144 return new SkSpecialSurface_Gpu(texture, subset, props); |
| 145 } |
| 146 |
| 147 SkSpecialSurface* SkSpecialSurface::NewRenderTarget(GrContext* context, |
| 148 const GrSurfaceDesc& desc, |
| 149 const SkSurfaceProps* props)
{ |
| 150 if (!context || !SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag)) { |
| 151 return nullptr; |
| 152 } |
| 153 |
| 154 GrTexture* temp = context->textureProvider()->createApproxTexture(desc); |
| 155 if (!temp) { |
| 156 return nullptr; |
| 157 } |
| 158 |
| 159 const SkIRect subset = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
| 160 |
| 161 return new SkSpecialSurface_Gpu(temp, subset, props); |
| 162 } |
| 163 |
| 164 #else |
| 165 |
| 166 SkSpecialSurface* SkSpecialSurface::NewFromTexture(const SkIRect& subset, GrText
ure*, |
| 167 const SkSurfaceProps*) { |
| 168 return nullptr; |
| 169 } |
| 170 |
| 171 SkSpecialSurface* SkSpecialSurface::NewRenderTarget(GrContext* context, |
| 172 const GrSurfaceDesc& desc, |
| 173 const SkSurfaceProps* props)
{ |
| 174 return nullptr; |
| 175 } |
| 176 |
| 177 #endif |
OLD | NEW |