Add sk_sp to SkSurface_Gpu and SkGpuDevice
authorrobertphillips <robertphillips@google.com>
Fri, 29 Apr 2016 13:46:36 +0000 (06:46 -0700)
committerCommit bot <commit-bot@chromium.org>
Fri, 29 Apr 2016 13:46:36 +0000 (06:46 -0700)
Split off of https://codereview.chromium.org/1930013002/ ((Mostly) Retract GrRenderTarget from SkGpuDevice)

GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1925803004

Review-Url: https://codereview.chromium.org/1925803004

src/core/SkSpecialSurface.cpp
src/gpu/SkGpuDevice.cpp
src/gpu/SkGpuDevice.h
src/image/SkSurface_Gpu.cpp
src/image/SkSurface_Gpu.h

index eeefb2a..335e206 100644 (file)
@@ -121,9 +121,9 @@ public:
 
         SkASSERT(fTexture->asRenderTarget());
 
-        sk_sp<SkGpuDevice> device(SkGpuDevice::Create(fTexture->asRenderTarget(), width, height,
-                                                      props,
-                                                      SkGpuDevice::kUninit_InitContents));
+        sk_sp<SkGpuDevice> device(SkGpuDevice::Make(sk_ref_sp(fTexture->asRenderTarget()),
+                                                    width, height, props,
+                                                    SkGpuDevice::kUninit_InitContents));
         if (!device) {
             return;
         }
index 0e6eb92..98da472 100644 (file)
@@ -127,13 +127,15 @@ bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
     return true;
 }
 
-SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props,
-                                 InitContents init) {
-    return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, init);
+sk_sp<SkGpuDevice> SkGpuDevice::Make(sk_sp<GrRenderTarget> rt, const SkSurfaceProps* props,
+                                     InitContents init) {
+    const int width = rt->width();
+    const int height = rt->height();
+    return SkGpuDevice::Make(std::move(rt), width, height, props, init);
 }
 
-SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
-                                 const SkSurfaceProps* props, InitContents init) {
+sk_sp<SkGpuDevice> SkGpuDevice::Make(sk_sp<GrRenderTarget> rt, int width, int height,
+                                     const SkSurfaceProps* props, InitContents init) {
     if (!rt || rt->wasDestroyed()) {
         return nullptr;
     }
@@ -141,23 +143,23 @@ SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
     if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
         return nullptr;
     }
-    return new SkGpuDevice(rt, width, height, props, flags);
+    return sk_sp<SkGpuDevice>(new SkGpuDevice(rt.get(), width, height, props, flags));
 }
 
-SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkBudgeted budgeted,
-                                 const SkImageInfo& info, int sampleCount,
-                                 const SkSurfaceProps* props, InitContents init) {
+sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
+                                     const SkImageInfo& info, int sampleCount,
+                                     const SkSurfaceProps* props, InitContents init) {
     unsigned flags;
     if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
         return nullptr;
     }
 
     SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
-    if (nullptr == rt) {
+    if (!rt) {
         return nullptr;
     }
 
-    return new SkGpuDevice(rt, info.width(), info.height(), props, flags);
+    return sk_sp<SkGpuDevice>(new SkGpuDevice(rt, info.width(), info.height(), props, flags));
 }
 
 SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
@@ -1756,8 +1758,9 @@ SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint
 
     if (texture) {
         SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
-        return SkGpuDevice::Create(
-            texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
+        return SkGpuDevice::Make(sk_ref_sp(texture->asRenderTarget()),
+                                 cinfo.fInfo.width(), cinfo.fInfo.height(),
+                                 &props, init).release();
     } else {
         SkErrorInternals::SetError( kInternalError_SkError,
                                     "---- failed to create gpu device texture [%d %d]\n",
index 8420449..1e02567 100644 (file)
@@ -37,22 +37,24 @@ public:
     /**
      * Creates an SkGpuDevice from a GrRenderTarget.
      */
-    static SkGpuDevice* Create(GrRenderTarget* target, const SkSurfaceProps*, InitContents);
+    static sk_sp<SkGpuDevice> Make(sk_sp<GrRenderTarget> target, 
+                                   const SkSurfaceProps*,
+                                   InitContents);
 
     /**
      * Creates an SkGpuDevice from a GrRenderTarget whose texture width/height is
      * different than its actual width/height (e.g., approx-match scratch texture).
      */
-    static SkGpuDevice* Create(GrRenderTarget* target, int width, int height,
-                               const SkSurfaceProps*, InitContents);
+    static sk_sp<SkGpuDevice> Make(sk_sp<GrRenderTarget> target, int width, int height,
+                                   const SkSurfaceProps*, InitContents);
 
     /**
      * New device that will create an offscreen renderTarget based on the ImageInfo and
      * sampleCount. The Budgeted param controls whether the device's backing store counts against
      * the resource cache budget. On failure, returns nullptr.
      */
-    static SkGpuDevice* Create(GrContext*, SkBudgeted, const SkImageInfo&,
-                               int sampleCount, const SkSurfaceProps*, InitContents);
+    static sk_sp<SkGpuDevice> Make(GrContext*, SkBudgeted, const SkImageInfo&,
+                                   int sampleCount, const SkSurfaceProps*, InitContents);
 
     ~SkGpuDevice() override {}
 
index fa09911..110d614 100644 (file)
 
 #if SK_SUPPORT_GPU
 
-SkSurface_Gpu::SkSurface_Gpu(SkGpuDevice* device)
+SkSurface_Gpu::SkSurface_Gpu(sk_sp<SkGpuDevice> device)
     : INHERITED(device->width(), device->height(), &device->surfaceProps())
-    , fDevice(SkRef(device)) {
+    , fDevice(std::move(device)) {
 }
 
 SkSurface_Gpu::~SkSurface_Gpu() {
-    fDevice->unref();
 }
 
 static GrRenderTarget* prepare_rt_for_external_access(SkSurface_Gpu* surface,
@@ -65,7 +64,7 @@ SkCanvas* SkSurface_Gpu::onNewCanvas() {
     SkCanvas::InitFlags flags = SkCanvas::kDefault_InitFlags;
     flags = static_cast<SkCanvas::InitFlags>(flags | SkCanvas::kConservativeRasterClip_InitFlag);
 
-    return new SkCanvas(fDevice, flags);
+    return new SkCanvas(fDevice.get(), flags);
 }
 
 sk_sp<SkSurface> SkSurface_Gpu::onNewSurface(const SkImageInfo& info) {
@@ -136,23 +135,23 @@ void SkSurface_Gpu::onPrepareForExternalIO() {
 
 sk_sp<SkSurface> SkSurface::MakeRenderTargetDirect(GrRenderTarget* target,
                                                    const SkSurfaceProps* props) {
-    SkAutoTUnref<SkGpuDevice> device(
-        SkGpuDevice::Create(target, props, SkGpuDevice::kUninit_InitContents));
+    sk_sp<SkGpuDevice> device(
+        SkGpuDevice::Make(sk_ref_sp(target), props, SkGpuDevice::kUninit_InitContents));
     if (!device) {
         return nullptr;
     }
-    return sk_make_sp<SkSurface_Gpu>(device);
+    return sk_make_sp<SkSurface_Gpu>(std::move(device));
 }
 
 sk_sp<SkSurface> SkSurface::MakeRenderTarget(GrContext* ctx, SkBudgeted budgeted,
                                              const SkImageInfo& info, int sampleCount,
                                              const SkSurfaceProps* props) {
-    SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(
+    sk_sp<SkGpuDevice> device(SkGpuDevice::Make(
             ctx, budgeted, info, sampleCount, props, SkGpuDevice::kClear_InitContents));
     if (!device) {
         return nullptr;
     }
-    return sk_make_sp<SkSurface_Gpu>(device);
+    return sk_make_sp<SkSurface_Gpu>(std::move(device));
 }
 
 sk_sp<SkSurface> SkSurface::MakeFromBackendTexture(GrContext* context,
@@ -169,49 +168,48 @@ sk_sp<SkSurface> SkSurface::MakeFromBackendTexture(GrContext* context,
     if (!surface) {
         return nullptr;
     }
-    SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(surface->asRenderTarget(), props,
-                                                         SkGpuDevice::kUninit_InitContents));
+    sk_sp<SkGpuDevice> device(SkGpuDevice::Make(sk_ref_sp(surface->asRenderTarget()), props,
+                                                SkGpuDevice::kUninit_InitContents));
     if (!device) {
         return nullptr;
     }
-    return sk_make_sp<SkSurface_Gpu>(device);
+    return sk_make_sp<SkSurface_Gpu>(std::move(device));
 }
 
 sk_sp<SkSurface> SkSurface::MakeFromBackendRenderTarget(GrContext* context,
                                                         const GrBackendRenderTargetDesc& desc,
                                                         const SkSurfaceProps* props) {
-    if (nullptr == context) {
+    if (!context) {
         return nullptr;
     }
-    SkAutoTUnref<GrRenderTarget> rt(context->textureProvider()->wrapBackendRenderTarget(desc));
+    sk_sp<GrRenderTarget> rt(context->textureProvider()->wrapBackendRenderTarget(desc));
     if (!rt) {
         return nullptr;
     }
-    SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(rt, props,
-                                                         SkGpuDevice::kUninit_InitContents));
+    sk_sp<SkGpuDevice> device(SkGpuDevice::Make(std::move(rt), props,
+                                                SkGpuDevice::kUninit_InitContents));
     if (!device) {
         return nullptr;
     }
-    return sk_make_sp<SkSurface_Gpu>(device);
+    return sk_make_sp<SkSurface_Gpu>(std::move(device));
 }
 
 sk_sp<SkSurface> SkSurface::MakeFromBackendTextureAsRenderTarget(GrContext* context,
                                                                  const GrBackendTextureDesc& desc,
                                                                  const SkSurfaceProps* props) {
-    if (nullptr == context) {
+    if (!context) {
         return nullptr;
     }
-    SkAutoTUnref<GrRenderTarget> rt(
-            context->resourceProvider()->wrapBackendTextureAsRenderTarget(desc));
+    sk_sp<GrRenderTarget> rt(context->resourceProvider()->wrapBackendTextureAsRenderTarget(desc));
     if (!rt) {
         return nullptr;
     }
-    SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(rt, props,
-                                                         SkGpuDevice::kUninit_InitContents));
+    sk_sp<SkGpuDevice> device(SkGpuDevice::Make(std::move(rt), props,
+                                                SkGpuDevice::kUninit_InitContents));
     if (!device) {
         return nullptr;
     }
-    return sk_make_sp<SkSurface_Gpu>(device);
+    return sk_make_sp<SkSurface_Gpu>(std::move(device));
 }
 
 #endif
index 1e76f26..d0f1959 100644 (file)
@@ -16,7 +16,7 @@ class SkGpuDevice;
 
 class SkSurface_Gpu : public SkSurface_Base {
 public:
-    SkSurface_Gpu(SkGpuDevice*);
+    SkSurface_Gpu(sk_sp<SkGpuDevice>);
     virtual ~SkSurface_Gpu();
 
     GrBackendObject onGetTextureHandle(BackendHandleAccess) override;
@@ -28,10 +28,10 @@ public:
     void onDiscard() override;
     void onPrepareForExternalIO() override;
 
-    SkGpuDevice* getDevice() { return fDevice; }
+    SkGpuDevice* getDevice() { return fDevice.get(); }
 
 private:
-    SkGpuDevice* fDevice;
+    sk_sp<SkGpuDevice> fDevice;
 
     typedef SkSurface_Base INHERITED;
 };