| 1 |
/* |
| 2 |
* Copyright 2023 Google LLC |
| 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 "src/image/SkSurface_Base.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkCapabilities.h" |
| 13 |
#include "include/core/SkColorSpace.h" // IWYU pragma: keep |
| 14 |
#include "include/core/SkImage.h" |
| 15 |
#include "include/core/SkImageInfo.h" |
| 16 |
#include "include/core/SkPixmap.h" |
| 17 |
#include "include/core/SkRect.h" |
| 18 |
#include "include/core/SkRefCnt.h" |
| 19 |
#include "include/core/SkScalar.h" |
| 20 |
#include "include/core/SkSize.h" |
| 21 |
#include "include/core/SkSurface.h" |
| 22 |
#include "src/capture/SkCaptureCanvas.h" |
| 23 |
#include "src/image/SkRescaleAndReadPixels.h" |
| 24 |
|
| 25 |
#include <atomic> |
| 26 |
#include <cstdint> |
| 27 |
|
| 28 |
class GrRecordingContext; |
| 29 |
class SkPaint; |
| 30 |
class SkSurfaceProps; |
| 31 |
namespace skgpu { namespace graphite { class Recorder; } } |
| 32 |
|
| 33 |
SkSurface_Base::SkSurface_Base(int width, int height, const SkSurfaceProps* props) |
| 34 |
: SkSurface(width, height, props) {} |
| 35 |
|
| 36 |
SkSurface_Base::SkSurface_Base(const SkImageInfo& info, const SkSurfaceProps* props) |
| 37 |
: SkSurface(info, props) {} |
| 38 |
|
| 39 |
SkSurface_Base::~SkSurface_Base() { |
| 40 |
// in case the canvas outsurvives us, we null the callback |
| 41 |
if (fCachedCanvas) { |
| 42 |
fCachedCanvas->setSurfaceBase(nullptr); |
| 43 |
fCachedCanvas->onSurfaceDelete(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
GrRecordingContext* SkSurface_Base::onGetRecordingContext() const { return nullptr; } |
| 48 |
|
| 49 |
skgpu::graphite::Recorder* SkSurface_Base::onGetRecorder() const { return nullptr; } |
| 50 |
SkRecorder* SkSurface_Base::onGetBaseRecorder() const { return nullptr; } |
| 51 |
|
| 52 |
void SkSurface_Base::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 53 |
const SkSamplingOptions& sampling, const SkPaint* paint) { |
| 54 |
auto image = this->makeTemporaryImage(); |
| 55 |
if (image) { |
| 56 |
canvas->drawImage(image.get(), x, y, sampling, paint); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
void SkSurface_Base::onAsyncRescaleAndReadPixels(const SkImageInfo& info, |
| 61 |
SkIRect origSrcRect, |
| 62 |
SkSurface::RescaleGamma rescaleGamma, |
| 63 |
RescaleMode rescaleMode, |
| 64 |
SkSurface::ReadPixelsCallback callback, |
| 65 |
SkSurface::ReadPixelsContext context) { |
| 66 |
SkBitmap src; |
| 67 |
SkPixmap peek; |
| 68 |
SkIRect srcRect; |
| 69 |
if (this->peekPixels(&peek)) { |
| 70 |
src.installPixels(peek); |
| 71 |
srcRect = origSrcRect; |
| 72 |
} else { |
| 73 |
src.setInfo(this->imageInfo().makeDimensions(origSrcRect.size())); |
| 74 |
src.allocPixels(); |
| 75 |
if (!this->readPixels(src, origSrcRect.x(), origSrcRect.y())) { |
| 76 |
callback(context, nullptr); |
| 77 |
return; |
| 78 |
} |
| 79 |
srcRect = SkIRect::MakeSize(src.dimensions()); |
| 80 |
} |
| 81 |
return SkRescaleAndReadPixels(src, info, srcRect, rescaleGamma, rescaleMode, callback, |
| 82 |
context); |
| 83 |
} |
| 84 |
|
| 85 |
void SkSurface_Base::onAsyncRescaleAndReadPixelsYUV420( |
| 86 |
SkYUVColorSpace yuvColorSpace, bool readAlpha, sk_sp<SkColorSpace> dstColorSpace, |
| 87 |
SkIRect srcRect, SkISize dstSize, RescaleGamma rescaleGamma, RescaleMode, |
| 88 |
ReadPixelsCallback callback, ReadPixelsContext context) { |
| 89 |
// TODO: Call non-YUV asyncRescaleAndReadPixels and then make our callback convert to YUV and |
| 90 |
// call client's callback. |
| 91 |
callback(context, nullptr); |
| 92 |
} |
| 93 |
|
| 94 |
bool SkSurface_Base::outstandingImageSnapshot() const { |
| 95 |
return fCachedImage && !fCachedImage->unique(); |
| 96 |
} |
| 97 |
|
| 98 |
bool SkSurface_Base::aboutToDraw(ContentChangeMode mode) { |
| 99 |
this->dirtyGenerationID(); |
| 100 |
|
| 101 |
SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this); |
| 102 |
|
| 103 |
if (fCachedImage) { |
| 104 |
// the surface may need to fork its backend, if its sharing it with |
| 105 |
// the cached image. Note: we only call if there is an outstanding owner |
| 106 |
// on the image (besides us). |
| 107 |
bool unique = fCachedImage->unique(); |
| 108 |
if (!unique) { |
| 109 |
if (!this->onCopyOnWrite(mode)) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// regardless of copy-on-write, we must drop our cached image now, so |
| 115 |
// that the next request will get our new contents. |
| 116 |
fCachedImage.reset(); |
| 117 |
|
| 118 |
if (unique) { |
| 119 |
// Our content isn't held by any image now, so we can consider that content mutable. |
| 120 |
// Raster surfaces need to be told it's safe to consider its pixels mutable again. |
| 121 |
// We make this call after the ->unref() so the subclass can assert there are no images. |
| 122 |
this->onRestoreBackingMutability(); |
| 123 |
} |
| 124 |
} else if (kDiscard_ContentChangeMode == mode) { |
| 125 |
this->onDiscard(); |
| 126 |
} |
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
uint32_t SkSurface_Base::newGenerationID() { |
| 131 |
SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this); |
| 132 |
static std::atomic<uint32_t> nextID{1}; |
| 133 |
return nextID.fetch_add(1, std::memory_order_relaxed); |
| 134 |
} |
| 135 |
|
| 136 |
sk_sp<const SkCapabilities> SkSurface_Base::onCapabilities() { |
| 137 |
return SkCapabilities::RasterBackend(); |
| 138 |
} |
| 139 |
|
| 140 |
void SkSurface_Base::createCaptureBreakpoint() { |
| 141 |
if (this->baseRecorder()) { |
| 142 |
this->baseRecorder()->createCaptureBreakpoint(this); |
| 143 |
} |
| 144 |
} |