Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSurface_Base.cpp
Go to the documentation of this file.
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
9
13#include "include/core/SkColorSpace.h" // IWYU pragma: keep
17#include "include/core/SkRect.h"
20#include "include/core/SkSize.h"
22
23#include <atomic>
24#include <cstdint>
25#include <memory>
26
28class SkPaint;
29class SkSurfaceProps;
30namespace skgpu { namespace graphite { class Recorder; } }
31
34
37
39 // in case the canvas outsurvives us, we null the callback
40 if (fCachedCanvas) {
41 fCachedCanvas->setSurfaceBase(nullptr);
42 }
43}
44
46
48
50 const SkSamplingOptions& sampling, const SkPaint* paint) {
51 auto image = this->makeImageSnapshot();
52 if (image) {
53 canvas->drawImage(image.get(), x, y, sampling, paint);
54 }
55}
56
58 SkIRect origSrcRect,
59 SkSurface::RescaleGamma rescaleGamma,
60 RescaleMode rescaleMode,
63 SkBitmap src;
64 SkPixmap peek;
65 SkIRect srcRect;
66 if (this->peekPixels(&peek)) {
67 src.installPixels(peek);
68 srcRect = origSrcRect;
69 } else {
70 src.setInfo(this->imageInfo().makeDimensions(origSrcRect.size()));
71 src.allocPixels();
72 if (!this->readPixels(src, origSrcRect.x(), origSrcRect.y())) {
73 callback(context, nullptr);
74 return;
75 }
76 srcRect = SkIRect::MakeSize(src.dimensions());
77 }
78 return SkRescaleAndReadPixels(src, info, srcRect, rescaleGamma, rescaleMode, callback,
79 context);
80}
81
83 SkYUVColorSpace yuvColorSpace, bool readAlpha, sk_sp<SkColorSpace> dstColorSpace,
84 SkIRect srcRect, SkISize dstSize, RescaleGamma rescaleGamma, RescaleMode,
86 // TODO: Call non-YUV asyncRescaleAndReadPixels and then make our callback convert to YUV and
87 // call client's callback.
88 callback(context, nullptr);
89}
90
91bool SkSurface_Base::outstandingImageSnapshot() const {
92 return fCachedImage && !fCachedImage->unique();
93}
94
95bool SkSurface_Base::aboutToDraw(ContentChangeMode mode) {
96 this->dirtyGenerationID();
97
98 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
99
100 if (fCachedImage) {
101 // the surface may need to fork its backend, if its sharing it with
102 // the cached image. Note: we only call if there is an outstanding owner
103 // on the image (besides us).
104 bool unique = fCachedImage->unique();
105 if (!unique) {
106 if (!this->onCopyOnWrite(mode)) {
107 return false;
108 }
109 }
110
111 // regardless of copy-on-write, we must drop our cached image now, so
112 // that the next request will get our new contents.
113 fCachedImage.reset();
114
115 if (unique) {
116 // Our content isn't held by any image now, so we can consider that content mutable.
117 // Raster surfaces need to be told it's safe to consider its pixels mutable again.
118 // We make this call after the ->unref() so the subclass can assert there are no images.
120 }
121 } else if (kDiscard_ContentChangeMode == mode) {
122 this->onDiscard();
123 }
124 return true;
125}
126
128 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
129 static std::atomic<uint32_t> nextID{1};
130 return nextID.fetch_add(1, std::memory_order_relaxed);
131}
132
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
#define SkASSERT(cond)
Definition SkAssert.h:116
SkYUVColorSpace
Definition SkImageInfo.h:68
void SkRescaleAndReadPixels(SkBitmap bmp, const SkImageInfo &resultInfo, const SkIRect &srcRect, SkImage::RescaleGamma rescaleGamma, SkImage::RescaleMode rescaleMode, SkImage::ReadPixelsCallback callback, SkImage::ReadPixelsContext context)
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
static sk_sp< const SkCapabilities > RasterBackend()
RescaleMode
Definition SkImage.h:587
RescaleGamma
Definition SkImage.h:585
bool unique() const
Definition SkRefCnt.h:50
virtual GrRecordingContext * onGetRecordingContext() const
virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace, bool readAlpha, sk_sp< SkColorSpace > dstColorSpace, SkIRect srcRect, SkISize dstSize, RescaleGamma, RescaleMode, ReadPixelsCallback, ReadPixelsContext)
virtual skgpu::graphite::Recorder * onGetRecorder() const
virtual sk_sp< const SkCapabilities > onCapabilities()
uint32_t newGenerationID()
~SkSurface_Base() override
SkSurface_Base(int width, int height, const SkSurfaceProps *)
virtual bool onCopyOnWrite(ContentChangeMode)=0
virtual void onDiscard()
virtual void onRestoreBackingMutability()
virtual void onDraw(SkCanvas *, SkScalar x, SkScalar y, const SkSamplingOptions &, const SkPaint *)
virtual void onAsyncRescaleAndReadPixels(const SkImageInfo &, const SkIRect srcRect, RescaleGamma, RescaleMode, ReadPixelsCallback, ReadPixelsContext)
void dirtyGenerationID()
Definition SkSurface.h:646
@ kDiscard_ContentChangeMode
discards surface on change
Definition SkSurface.h:204
void(ReadPixelsContext, std::unique_ptr< const AsyncReadResult >) ReadPixelsCallback
Definition SkSurface.h:469
virtual SkImageInfo imageInfo() const
Definition SkSurface.h:188
bool peekPixels(SkPixmap *pixmap)
sk_sp< SkImage > makeImageSnapshot()
Definition SkSurface.cpp:90
bool readPixels(const SkPixmap &dst, int srcX, int srcY)
void * ReadPixelsContext
Definition SkSurface.h:464
T * get() const
Definition SkRefCnt.h:303
void reset(T *ptr=nullptr)
Definition SkRefCnt.h:310
const Paint & paint
sk_sp< SkImage > image
Definition examples.cpp:29
float SkScalar
Definition extension.cpp:12
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
double y
double x
int32_t height
int32_t width
constexpr int32_t x() const
Definition SkRect.h:141
constexpr int32_t y() const
Definition SkRect.h:148
constexpr SkISize size() const
Definition SkRect.h:172
static constexpr SkIRect MakeSize(const SkISize &size)
Definition SkRect.h:66