| 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 |
#ifndef SurfaceContext_DEFINED |
| 9 |
#define SurfaceContext_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkImage.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkRefCnt.h" |
| 14 |
#include "include/core/SkSize.h" |
| 15 |
#include "include/private/base/SkDebug.h" |
| 16 |
#include "src/gpu/Swizzle.h" |
| 17 |
#include "src/gpu/ganesh/GrColorInfo.h" |
| 18 |
#include "src/gpu/ganesh/GrGpuBuffer.h" |
| 19 |
#include "src/gpu/ganesh/GrImageInfo.h" |
| 20 |
#include "src/gpu/ganesh/GrPixmap.h" |
| 21 |
#include "src/gpu/ganesh/GrRenderTargetProxy.h" // IWYU pragma: keep |
| 22 |
#include "src/gpu/ganesh/GrRenderTask.h" // IWYU pragma: keep |
| 23 |
#include "src/gpu/ganesh/GrSamplerState.h" |
| 24 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 25 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 26 |
#include "src/gpu/ganesh/GrTextureProxy.h" // IWYU pragma: keep |
| 27 |
|
| 28 |
#include <cstddef> |
| 29 |
#include <functional> |
| 30 |
#include <memory> |
| 31 |
#include <utility> |
| 32 |
|
| 33 |
class GrCaps; |
| 34 |
class GrDirectContext; |
| 35 |
class GrDrawingManager; |
| 36 |
class GrRecordingContext; |
| 37 |
class GrRecordingContextPriv; |
| 38 |
class SkColorSpace; |
| 39 |
enum GrSurfaceOrigin : int; |
| 40 |
enum SkColorType : int; |
| 41 |
enum SkYUVColorSpace : int; |
| 42 |
enum class GrColorType; |
| 43 |
struct SkIPoint; |
| 44 |
struct SkImageInfo; |
| 45 |
|
| 46 |
namespace skgpu { |
| 47 |
class SingleOwner; |
| 48 |
enum class Mipmapped : bool; |
| 49 |
} |
| 50 |
|
| 51 |
namespace skgpu::ganesh { |
| 52 |
|
| 53 |
class SurfaceFillContext; |
| 54 |
|
| 55 |
class SurfaceContext { |
| 56 |
public: |
| 57 |
// If it is known that the GrSurfaceProxy is not renderable, you can directly call the ctor |
| 58 |
// here to make a SurfaceContext on the stack. |
| 59 |
SurfaceContext(GrRecordingContext*, GrSurfaceProxyView readView, const GrColorInfo&); |
| 60 |
|
| 61 |
virtual ~SurfaceContext() = default; |
| 62 |
|
| 63 |
GrRecordingContext* recordingContext() const { return fContext; } |
| 64 |
|
| 65 |
const GrColorInfo& colorInfo() const { return fColorInfo; } |
| 66 |
GrImageInfo imageInfo() const { return {fColorInfo, fReadView.proxy()->dimensions()}; } |
| 67 |
|
| 68 |
GrSurfaceOrigin origin() const { return fReadView.origin(); } |
| 69 |
skgpu::Swizzle readSwizzle() const { return fReadView.swizzle(); } |
| 70 |
// TODO: See if it makes sense for this to return a const& instead and require the callers to |
| 71 |
// make a copy (which refs the proxy) if needed. |
| 72 |
GrSurfaceProxyView readSurfaceView() { return fReadView; } |
| 73 |
|
| 74 |
SkISize dimensions() const { return fReadView.dimensions(); } |
| 75 |
int width() const { return fReadView.proxy()->width(); } |
| 76 |
int height() const { return fReadView.proxy()->height(); } |
| 77 |
|
| 78 |
skgpu::Mipmapped mipmapped() const { return fReadView.mipmapped(); } |
| 79 |
|
| 80 |
const GrCaps* caps() const; |
| 81 |
|
| 82 |
/** |
| 83 |
* Reads a rectangle of pixels from the surface context. |
| 84 |
* @param dContext The direct context to use |
| 85 |
* @param dst destination pixels for the read |
| 86 |
* @param srcPt offset w/in the surface context from which to read |
| 87 |
* is a GrDirectContext and fail otherwise. |
| 88 |
*/ |
| 89 |
bool readPixels(GrDirectContext* dContext, GrPixmap dst, SkIPoint srcPt); |
| 90 |
|
| 91 |
using ReadPixelsCallback = SkImage::ReadPixelsCallback; |
| 92 |
using ReadPixelsContext = SkImage::ReadPixelsContext; |
| 93 |
using RescaleGamma = SkImage::RescaleGamma; |
| 94 |
using RescaleMode = SkImage::RescaleMode; |
| 95 |
|
| 96 |
// GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixels. |
| 97 |
void asyncRescaleAndReadPixels(GrDirectContext*, |
| 98 |
const SkImageInfo& info, |
| 99 |
const SkIRect& srcRect, |
| 100 |
RescaleGamma rescaleGamma, |
| 101 |
RescaleMode, |
| 102 |
ReadPixelsCallback callback, |
| 103 |
ReadPixelsContext callbackContext); |
| 104 |
|
| 105 |
// GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixelsYUV420. |
| 106 |
void asyncRescaleAndReadPixelsYUV420(GrDirectContext*, |
| 107 |
SkYUVColorSpace yuvColorSpace, |
| 108 |
bool readAlpha, |
| 109 |
sk_sp<SkColorSpace> dstColorSpace, |
| 110 |
const SkIRect& srcRect, |
| 111 |
SkISize dstSize, |
| 112 |
RescaleGamma rescaleGamma, |
| 113 |
RescaleMode, |
| 114 |
ReadPixelsCallback callback, |
| 115 |
ReadPixelsContext context); |
| 116 |
|
| 117 |
/** |
| 118 |
* Writes a rectangle of pixels from src into the surfaceDrawContext at the specified position. |
| 119 |
* @param dContext The direct context to use |
| 120 |
* @param src source for the write |
| 121 |
* @param dstPt offset w/in the surface context at which to write |
| 122 |
*/ |
| 123 |
bool writePixels(GrDirectContext* dContext, |
| 124 |
GrCPixmap src, |
| 125 |
SkIPoint dstPt); |
| 126 |
|
| 127 |
/** |
| 128 |
* Fully populates either the base level or all MIP levels of the GrSurface with pixel data. |
| 129 |
* @param dContext The direct context to use |
| 130 |
* @param src Array of pixmaps |
| 131 |
* @param numLevels Number of pixmaps in src. To succeed this must be 1 or the total |
| 132 |
* number of MIP levels. |
| 133 |
*/ |
| 134 |
bool writePixels(GrDirectContext* dContext, |
| 135 |
const GrCPixmap src[], |
| 136 |
int numLevels); |
| 137 |
|
| 138 |
GrSurfaceProxy* asSurfaceProxy() { return fReadView.proxy(); } |
| 139 |
const GrSurfaceProxy* asSurfaceProxy() const { return fReadView.proxy(); } |
| 140 |
sk_sp<GrSurfaceProxy> asSurfaceProxyRef() { return fReadView.refProxy(); } |
| 141 |
|
| 142 |
GrTextureProxy* asTextureProxy() { return fReadView.asTextureProxy(); } |
| 143 |
const GrTextureProxy* asTextureProxy() const { return fReadView.asTextureProxy(); } |
| 144 |
sk_sp<GrTextureProxy> asTextureProxyRef() { return fReadView.asTextureProxyRef(); } |
| 145 |
|
| 146 |
GrRenderTargetProxy* asRenderTargetProxy() { return fReadView.asRenderTargetProxy(); } |
| 147 |
const GrRenderTargetProxy* asRenderTargetProxy() const { |
| 148 |
return fReadView.asRenderTargetProxy(); |
| 149 |
} |
| 150 |
sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() { |
| 151 |
return fReadView.asRenderTargetProxyRef(); |
| 152 |
} |
| 153 |
|
| 154 |
virtual SurfaceFillContext* asFillContext() { return nullptr; } |
| 155 |
|
| 156 |
/** |
| 157 |
* Rescales the contents of srcRect. The gamma in which the rescaling occurs is controlled by |
| 158 |
* RescaleGamma. It is always in the original gamut. The result is converted to the color type |
| 159 |
* and color space of info after rescaling. Note: this currently requires that the info have a |
| 160 |
* different size than srcRect. Though, it could be relaxed to allow non-scaling color |
| 161 |
* conversions. |
| 162 |
*/ |
| 163 |
std::unique_ptr<SurfaceFillContext> rescale(const GrImageInfo& info, |
| 164 |
GrSurfaceOrigin, |
| 165 |
SkIRect srcRect, |
| 166 |
SkImage::RescaleGamma, |
| 167 |
SkImage::RescaleMode); |
| 168 |
|
| 169 |
/** |
| 170 |
* Like the above but allows the caller ot specify a destination fill context and |
| 171 |
* rect within that context. The dst rect must be contained by the dst or this will fail. |
| 172 |
*/ |
| 173 |
bool rescaleInto(SurfaceFillContext* dst, |
| 174 |
SkIRect dstRect, |
| 175 |
SkIRect srcRect, |
| 176 |
SkImage::RescaleGamma, |
| 177 |
SkImage::RescaleMode); |
| 178 |
|
| 179 |
#if defined(GPU_TEST_UTILS) |
| 180 |
bool testCopy(sk_sp<GrSurfaceProxy> src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
| 181 |
return this->copy(std::move(src), srcRect, dstPoint) != nullptr; |
| 182 |
} |
| 183 |
|
| 184 |
bool testCopy(sk_sp<GrSurfaceProxy> src) { |
| 185 |
auto rect = SkIRect::MakeSize(src->dimensions()); |
| 186 |
return this->copy(std::move(src), rect, {0, 0}) != nullptr; |
| 187 |
} |
| 188 |
#endif |
| 189 |
|
| 190 |
protected: |
| 191 |
GrDrawingManager* drawingManager(); |
| 192 |
const GrDrawingManager* drawingManager() const; |
| 193 |
|
| 194 |
SkDEBUGCODE(void validate() const;) |
| 195 |
|
| 196 |
SkDEBUGCODE(skgpu::SingleOwner* singleOwner() const;) |
| 197 |
|
| 198 |
GrRecordingContext* fContext; |
| 199 |
|
| 200 |
GrSurfaceProxyView fReadView; |
| 201 |
|
| 202 |
// Inserts a transfer, part of the implementation of asyncReadPixels and |
| 203 |
// asyncRescaleAndReadPixelsYUV420(). |
| 204 |
struct PixelTransferResult { |
| 205 |
using ConversionFn = void(void* dst, const void* mappedBuffer); |
| 206 |
// If null then the transfer could not be performed. Otherwise this buffer will contain |
| 207 |
// the pixel data when the transfer is complete. |
| 208 |
sk_sp<GrGpuBuffer> fTransferBuffer; |
| 209 |
// RowBytes for transfer buffer data |
| 210 |
size_t fRowBytes; |
| 211 |
// If this is null then the transfer buffer will contain the data in the requested |
| 212 |
// color type. Otherwise, when the transfer is done this must be called to convert |
| 213 |
// from the transfer buffer's color type to the requested color type. |
| 214 |
std::function<ConversionFn> fPixelConverter; |
| 215 |
}; |
| 216 |
PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect); |
| 217 |
|
| 218 |
// The async read step of asyncRescaleAndReadPixels() |
| 219 |
void asyncReadPixels(GrDirectContext*, |
| 220 |
const SkIRect& srcRect, |
| 221 |
SkColorType, |
| 222 |
ReadPixelsCallback, |
| 223 |
ReadPixelsContext); |
| 224 |
|
| 225 |
private: |
| 226 |
friend class ::GrRecordingContextPriv; // for validate |
| 227 |
friend class ::GrSurfaceProxy; // for copy |
| 228 |
|
| 229 |
SkDEBUGCODE(virtual void onValidate() const {}) |
| 230 |
|
| 231 |
/** |
| 232 |
* Copy 'src' into the proxy backing this context. This call will not do any draw fallback. |
| 233 |
* Currently only writePixels and replaceRenderTarget call this directly. All other copies |
| 234 |
* should go through GrSurfaceProxy::Copy. |
| 235 |
* @param src src of pixels |
| 236 |
* @param dstPoint the origin of the 'srcRect' in the destination coordinate space |
| 237 |
* @return a task (that may be skippable by calling canSkip) if successful and |
| 238 |
* null otherwise. |
| 239 |
* |
| 240 |
* Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted. |
| 241 |
* Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent. |
| 242 |
* The end result is only valid src pixels and dst pixels will be touched but the copied |
| 243 |
* regions will not be shifted. The 'src' must have the same origin as the backing proxy |
| 244 |
* of fSurfaceContext. |
| 245 |
*/ |
| 246 |
sk_sp<GrRenderTask> copy(sk_sp<GrSurfaceProxy> src, SkIRect srcRect, SkIPoint dstPoint); |
| 247 |
|
| 248 |
/** |
| 249 |
* Copy and scale 'src' into the proxy backing this context. This call will not do any draw |
| 250 |
* fallback. Currently only rescaleInto() calls this directly, which handles drawing fallback |
| 251 |
* automatically. |
| 252 |
* |
| 253 |
* @param src src of pixels |
| 254 |
* @param srcRect the subset of src that is copied to this proxy |
| 255 |
* @param dstRect the subset of dst that receives the copied data, possibly with different |
| 256 |
* dimensions than 'srcRect'. |
| 257 |
* @param filterMode the filter mode to apply when scaling src |
| 258 |
* @return a task (that may be skippable by calling canSkip) if successful and |
| 259 |
* null otherwise. |
| 260 |
* |
| 261 |
* Note: Unlike copy(rect,point), 'srcRect' and 'dstRect' are not adjusted to fit within the |
| 262 |
* surfaces. If they are not contained, then nullptr is returned. The 'src' must have the same |
| 263 |
* origin as the backing proxy of this context. |
| 264 |
*/ |
| 265 |
sk_sp<GrRenderTask> copyScaled(sk_sp<GrSurfaceProxy> src, SkIRect srcRect, SkIRect dstRect, |
| 266 |
GrSamplerState::Filter filterMode); |
| 267 |
|
| 268 |
bool internalWritePixels(GrDirectContext* dContext, |
| 269 |
const GrCPixmap src[], |
| 270 |
int numLevels, |
| 271 |
SkIPoint); |
| 272 |
|
| 273 |
GrColorInfo fColorInfo; |
| 274 |
|
| 275 |
using INHERITED = SkRefCnt; |
| 276 |
}; |
| 277 |
|
| 278 |
} // namespace skgpu::ganesh |
| 279 |
|
| 280 |
#endif // SurfaceContext_DEFINED |