Bug 1758688 [wpt PR 33067] - [FedCM] Make revoke a non-static method, a=testonly
[gecko.git] / gfx / 2d / SourceSurfaceSkia.cpp
blob93e72636a884682b72ef7c3aaaa022d8b8563cf5
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "Logging.h"
8 #include "SourceSurfaceSkia.h"
9 #include "HelpersSkia.h"
10 #include "DrawTargetSkia.h"
11 #include "DataSurfaceHelpers.h"
12 #include "skia/include/core/SkData.h"
13 #include "skia/include/core/SkImage.h"
14 #include "skia/include/core/SkSurface.h"
15 #include "mozilla/CheckedInt.h"
17 namespace mozilla::gfx {
19 SourceSurfaceSkia::SourceSurfaceSkia()
20 : mFormat(SurfaceFormat::UNKNOWN),
21 mStride(0),
22 mDrawTarget(nullptr),
23 mChangeMutex("SourceSurfaceSkia::mChangeMutex"),
24 mIsMapped(false) {}
26 SourceSurfaceSkia::~SourceSurfaceSkia() {
27 // if mIsMapped is true then mChangeMutex will be locked
28 // which will cause problems during destruction.
29 MOZ_RELEASE_ASSERT(!mIsMapped);
32 IntSize SourceSurfaceSkia::GetSize() const { return mSize; }
34 SurfaceFormat SourceSurfaceSkia::GetFormat() const { return mFormat; }
36 // This is only ever called by the DT destructor, which can only ever happen
37 // from one place at a time. Therefore it doesn't need to hold the ChangeMutex
38 // as mSurface is never read to directly and is just there to keep the object
39 // alive, which itself is refcounted in a thread-safe manner.
40 void SourceSurfaceSkia::GiveSurface(SkSurface* aSurface) {
41 mSurface.reset(aSurface);
42 mDrawTarget = nullptr;
45 sk_sp<SkImage> SourceSurfaceSkia::GetImage(Maybe<MutexAutoLock>* aLock) {
46 // If we were provided a lock object, we can let the caller access
47 // a shared SkImage and we know it won't go away while the lock is held.
48 // Otherwise we need to call DrawTargetWillChange to ensure we have our
49 // own SkImage.
50 if (aLock) {
51 MOZ_ASSERT(aLock->isNothing());
52 aLock->emplace(mChangeMutex);
54 // Now that we are locked, we can check mDrawTarget. If it's null, then
55 // we're not shared and we can unlock eagerly.
56 if (!mDrawTarget) {
57 aLock->reset();
59 } else {
60 DrawTargetWillChange();
62 sk_sp<SkImage> image = mImage;
63 return image;
66 static sk_sp<SkData> MakeSkData(void* aData, int32_t aHeight, size_t aStride) {
67 CheckedInt<size_t> size = aStride;
68 size *= aHeight;
69 if (size.isValid()) {
70 void* mem = sk_malloc_flags(size.value(), 0);
71 if (mem) {
72 if (aData) {
73 memcpy(mem, aData, size.value());
75 return SkData::MakeFromMalloc(mem, size.value());
78 return nullptr;
81 static sk_sp<SkImage> ReadSkImage(const sk_sp<SkImage>& aImage,
82 const SkImageInfo& aInfo, size_t aStride) {
83 if (sk_sp<SkData> data = MakeSkData(nullptr, aInfo.height(), aStride)) {
84 if (aImage->readPixels(aInfo, data->writable_data(), aStride, 0, 0,
85 SkImage::kDisallow_CachingHint)) {
86 return SkImage::MakeRasterData(aInfo, data, aStride);
89 return nullptr;
92 bool SourceSurfaceSkia::InitFromData(unsigned char* aData, const IntSize& aSize,
93 int32_t aStride, SurfaceFormat aFormat) {
94 sk_sp<SkData> data = MakeSkData(aData, aSize.height, aStride);
95 if (!data) {
96 return false;
99 SkImageInfo info = MakeSkiaImageInfo(aSize, aFormat);
100 mImage = SkImage::MakeRasterData(info, data, aStride);
101 if (!mImage) {
102 return false;
105 mSize = aSize;
106 mFormat = aFormat;
107 mStride = aStride;
108 return true;
111 bool SourceSurfaceSkia::InitFromImage(const sk_sp<SkImage>& aImage,
112 SurfaceFormat aFormat,
113 DrawTargetSkia* aOwner) {
114 if (!aImage) {
115 return false;
118 mSize = IntSize(aImage->width(), aImage->height());
120 // For the raster image case, we want to use the format and stride
121 // information that the underlying raster image is using, which is
122 // reliable.
123 // For the GPU case (for which peekPixels is false), we can't easily
124 // figure this information out. It is better to report the originally
125 // intended format and stride that we will convert to if this GPU
126 // image is ever read back into a raster image.
127 SkPixmap pixmap;
128 if (aImage->peekPixels(&pixmap)) {
129 mFormat =
130 aFormat != SurfaceFormat::UNKNOWN
131 ? aFormat
132 : SkiaColorTypeToGfxFormat(pixmap.colorType(), pixmap.alphaType());
133 mStride = pixmap.rowBytes();
134 } else if (aFormat != SurfaceFormat::UNKNOWN) {
135 mFormat = aFormat;
136 SkImageInfo info = MakeSkiaImageInfo(mSize, mFormat);
137 mStride = SkAlign4(info.minRowBytes());
138 } else {
139 return false;
142 mImage = aImage;
144 if (aOwner) {
145 mDrawTarget = aOwner;
148 return true;
151 uint8_t* SourceSurfaceSkia::GetData() {
152 if (!mImage) {
153 return nullptr;
155 SkPixmap pixmap;
156 if (!mImage->peekPixels(&pixmap)) {
157 gfxCriticalError() << "Failed accessing pixels for Skia raster image";
159 return reinterpret_cast<uint8_t*>(pixmap.writable_addr());
162 bool SourceSurfaceSkia::Map(MapType, MappedSurface* aMappedSurface)
163 NO_THREAD_SAFETY_ANALYSIS {
164 mChangeMutex.Lock();
165 aMappedSurface->mData = GetData();
166 aMappedSurface->mStride = Stride();
167 mIsMapped = !!aMappedSurface->mData;
168 bool isMapped = mIsMapped;
169 if (!mIsMapped) {
170 mChangeMutex.Unlock();
172 // Static analysis will warn due to a conditional Unlock
173 PUSH_IGNORE_THREAD_SAFETY
174 return isMapped;
175 POP_THREAD_SAFETY
178 void SourceSurfaceSkia::Unmap() NO_THREAD_SAFETY_ANALYSIS {
179 mChangeMutex.AssertCurrentThreadOwns();
180 MOZ_ASSERT(mIsMapped);
181 mIsMapped = false;
182 mChangeMutex.Unlock();
185 void SourceSurfaceSkia::DrawTargetWillChange() {
186 MutexAutoLock lock(mChangeMutex);
187 if (mDrawTarget.exchange(nullptr)) {
188 // Raster snapshots do not use Skia's internal copy-on-write mechanism,
189 // so we need to do an explicit copy here.
190 // GPU snapshots, for which peekPixels is false, will already be dealt
191 // with automatically via the internal copy-on-write mechanism, so we
192 // don't need to do anything for them here.
193 SkPixmap pixmap;
194 if (mImage->peekPixels(&pixmap)) {
195 mImage = ReadSkImage(mImage, pixmap.info(), pixmap.rowBytes());
196 if (!mImage) {
197 gfxCriticalError() << "Failed copying Skia raster snapshot";
203 } // namespace mozilla::gfx