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& activeRect, const SkSurfaceProps* props
) |
| 17 : INHERITED(activeRect, 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& activeRect, const SkSurfacePro
ps* props) |
| 44 : fProps(SkSurfacePropsCopyOrDefault(props)) |
| 45 , fActiveRect(activeRect) { |
| 46 SkASSERT(fActiveRect.width() > 0); |
| 47 SkASSERT(fActiveRect.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 SkSurfaceProps* props) |
| 66 : INHERITED(SkIRect::MakeWH(pr->info().width(), pr->info().height()), pr
ops) { |
| 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::NewRaster(this->activeRect(), fBitmap); |
| 79 } |
| 80 |
| 81 private: |
| 82 SkBitmap fBitmap; |
| 83 |
| 84 typedef SkSpecialSurface_Base INHERITED; |
| 85 }; |
| 86 |
| 87 SkSpecialSurface* SkSpecialSurface::NewRaster(const SkImageInfo& info, |
| 88 const SkSurfaceProps* props) { |
| 89 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, 0, nullptr)); |
| 90 if (nullptr == pr.get()) { |
| 91 return nullptr; |
| 92 } |
| 93 |
| 94 return new SkSpecialSurface_Raster(pr, props); |
| 95 } |
| 96 |
| 97 #if SK_SUPPORT_GPU |
| 98 /////////////////////////////////////////////////////////////////////////////// |
| 99 #include "GrContext.h" |
| 100 #include "SkGpuDevice.h" |
| 101 |
| 102 class SkSpecialSurface_Gpu : public SkSpecialSurface_Base { |
| 103 public: |
| 104 SkSpecialSurface_Gpu(GrTexture* texture, const SkIRect& activeRect, const Sk
SurfaceProps* props) |
| 105 : INHERITED(activeRect, props) |
| 106 , fTexture(texture) { |
| 107 |
| 108 SkASSERT(fTexture->asRenderTarget()); |
| 109 |
| 110 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(fTexture->asRenderT
arget(), props, |
| 111 SkGpuDevice::kUnini
t_InitContents)); |
| 112 if (!device) { |
| 113 return; |
| 114 } |
| 115 |
| 116 fCanvas.reset(new SkCanvas(device)); |
| 117 } |
| 118 |
| 119 ~SkSpecialSurface_Gpu() override { } |
| 120 |
| 121 SkSpecialImage* onNewImageSnapshot() override { |
| 122 return SkSpecialImage::NewGpu(this->activeRect(), fTexture); |
| 123 } |
| 124 |
| 125 private: |
| 126 SkAutoTUnref<GrTexture> fTexture; |
| 127 |
| 128 typedef SkSpecialSurface_Base INHERITED; |
| 129 }; |
| 130 |
| 131 SkSpecialSurface* SkSpecialSurface::NewGpu(const SkIRect& activeRect, GrTexture*
texture, |
| 132 const SkSurfaceProps* props) { |
| 133 if (!texture->asRenderTarget()) { |
| 134 return nullptr; |
| 135 } |
| 136 |
| 137 return new SkSpecialSurface_Gpu(texture, activeRect, props); |
| 138 } |
| 139 |
| 140 SkSpecialSurface* SkSpecialSurface::NewGpu(GrContext* context, |
| 141 const GrSurfaceDesc& desc, |
| 142 const SkSurfaceProps* props) { |
| 143 if (!context || !SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag)) { |
| 144 return nullptr; |
| 145 } |
| 146 |
| 147 GrTexture* temp = context->textureProvider()->createApproxTexture(desc); |
| 148 if (!temp) { |
| 149 return nullptr; |
| 150 } |
| 151 |
| 152 const SkIRect activeRect = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
| 153 |
| 154 return new SkSpecialSurface_Gpu(temp, activeRect, props); |
| 155 } |
| 156 |
| 157 #else |
| 158 |
| 159 SkSpecialSurface* SkSpecialSurface::NewGpu(GrTexture*, const SkIRect& activeRect
) { |
| 160 return nullptr; |
| 161 } |
| 162 |
| 163 SkSpecialSurface* SkSpecialSurface::NewGpu(GrContext* context, |
| 164 const GrSurfaceDesc& desc, |
| 165 const SkSurfaceProps* props) { |
| 166 return nullptr; |
| 167 } |
| 168 |
| 169 #endif |
OLD | NEW |