1/* 2 * Copyright 2012 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#ifndef SkSurface_Base_DEFINED 9#define SkSurface_Base_DEFINED 10 11#include "SkCanvas.h" 12#include "SkImagePriv.h" 13#include "SkSurface.h" 14#include "SkSurfacePriv.h" 15 16class SkSurface_Base : public SkSurface { 17public: 18 SkSurface_Base(int width, int height, const SkSurfaceProps*); 19 SkSurface_Base(const SkImageInfo&, const SkSurfaceProps*); 20 virtual ~SkSurface_Base(); 21 22 virtual GrBackendObject onGetTextureHandle(BackendHandleAccess) { 23 return 0; 24 } 25 26 virtual bool onGetRenderTargetHandle(GrBackendObject*, BackendHandleAccess) { 27 return false; 28 } 29 30 /** 31 * Allocate a canvas that will draw into this surface. We will cache this 32 * canvas, to return the same object to the caller multiple times. We 33 * take ownership, and will call unref() on the canvas when we go out of 34 * scope. 35 */ 36 virtual SkCanvas* onNewCanvas() = 0; 37 38 virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&) = 0; 39 40 /** 41 * Allocate an SkImage that represents the current contents of the surface. 42 * This needs to be able to outlive the surface itself (if need be), and 43 * must faithfully represent the current contents, even if the surface 44 * is changed after this called (e.g. it is drawn to via its canvas). 45 */ 46 virtual sk_sp<SkImage> onNewImageSnapshot() = 0; 47 48 /** 49 * Default implementation: 50 * 51 * image = this->newImageSnapshot(); 52 * if (image) { 53 * image->draw(canvas, ...); 54 * image->unref(); 55 * } 56 */ 57 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*); 58 59 /** 60 * Called as a performance hint when the Surface is allowed to make it's contents 61 * undefined. 62 */ 63 virtual void onDiscard() {} 64 65 /** 66 * If the surface is about to change, we call this so that our subclass 67 * can optionally fork their backend (copy-on-write) in case it was 68 * being shared with the cachedImage. 69 */ 70 virtual void onCopyOnWrite(ContentChangeMode) = 0; 71 72 /** 73 * Signal the surface to remind its backing store that it's mutable again. 74 * Called only when we _didn't_ copy-on-write; we assume the copies start mutable. 75 */ 76 virtual void onRestoreBackingMutability() {} 77 78 /** 79 * Issue any pending surface IO to the current backend 3D API and resolve any surface MSAA. 80 * Inserts the requested number of semaphores for the gpu to signal when work is complete on the 81 * gpu and inits the array of GrBackendSemaphores with the signaled semaphores. 82 */ 83 virtual bool onFlush(int numSemaphores, GrBackendSemaphore* signalSemaphores) { 84 return false; 85 } 86 87 /** 88 * Caused the current backend 3D API to wait on the passed in semaphores before executing new 89 * commands on the gpu. Any previously submitting commands will not be blocked by these 90 * semaphores. 91 */ 92 virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) { 93 return false; 94 } 95 96 inline SkCanvas* getCachedCanvas(); 97 inline sk_sp<SkImage> refCachedImage(); 98 99 bool hasCachedImage() const { return fCachedImage != nullptr; } 100 101 // called by SkSurface to compute a new genID 102 uint32_t newGenerationID(); 103 104private: 105 std::unique_ptr<SkCanvas> fCachedCanvas; 106 sk_sp<SkImage> fCachedImage; 107 108 void aboutToDraw(ContentChangeMode mode); 109 110 // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw 111 // would trigger a copy-on-write. 112 bool outstandingImageSnapshot() const; 113 114 friend class SkCanvas; 115 friend class SkSurface; 116 117 typedef SkSurface INHERITED; 118}; 119 120SkCanvas* SkSurface_Base::getCachedCanvas() { 121 if (nullptr == fCachedCanvas) { 122 fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas()); 123 if (fCachedCanvas) { 124 fCachedCanvas->setSurfaceBase(this); 125 } 126 } 127 return fCachedCanvas.get(); 128} 129 130sk_sp<SkImage> SkSurface_Base::refCachedImage() { 131 if (fCachedImage) { 132 return fCachedImage; 133 } 134 135 fCachedImage = this->onNewImageSnapshot(); 136 137 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this); 138 return fCachedImage; 139} 140 141#endif 142