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/. */
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
),
23 mChangeMutex("SourceSurfaceSkia::mChangeMutex"),
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
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.
60 DrawTargetWillChange();
62 sk_sp
<SkImage
> image
= mImage
;
66 static sk_sp
<SkData
> MakeSkData(void* aData
, int32_t aHeight
, size_t aStride
) {
67 CheckedInt
<size_t> size
= aStride
;
70 void* mem
= sk_malloc_flags(size
.value(), 0);
73 memcpy(mem
, aData
, size
.value());
75 return SkData::MakeFromMalloc(mem
, size
.value());
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
);
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
);
99 SkImageInfo info
= MakeSkiaImageInfo(aSize
, aFormat
);
100 mImage
= SkImage::MakeRasterData(info
, data
, aStride
);
111 bool SourceSurfaceSkia::InitFromImage(const sk_sp
<SkImage
>& aImage
,
112 SurfaceFormat aFormat
,
113 DrawTargetSkia
* aOwner
) {
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
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.
128 if (aImage
->peekPixels(&pixmap
)) {
130 aFormat
!= SurfaceFormat::UNKNOWN
132 : SkiaColorTypeToGfxFormat(pixmap
.colorType(), pixmap
.alphaType());
133 mStride
= pixmap
.rowBytes();
134 } else if (aFormat
!= SurfaceFormat::UNKNOWN
) {
136 SkImageInfo info
= MakeSkiaImageInfo(mSize
, mFormat
);
137 mStride
= SkAlign4(info
.minRowBytes());
145 mDrawTarget
= aOwner
;
151 uint8_t* SourceSurfaceSkia::GetData() {
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
{
165 aMappedSurface
->mData
= GetData();
166 aMappedSurface
->mStride
= Stride();
167 mIsMapped
= !!aMappedSurface
->mData
;
168 bool isMapped
= mIsMapped
;
170 mChangeMutex
.Unlock();
172 // Static analysis will warn due to a conditional Unlock
173 PUSH_IGNORE_THREAD_SAFETY
178 void SourceSurfaceSkia::Unmap() NO_THREAD_SAFETY_ANALYSIS
{
179 mChangeMutex
.AssertCurrentThreadOwns();
180 MOZ_ASSERT(mIsMapped
);
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.
194 if (mImage
->peekPixels(&pixmap
)) {
195 mImage
= ReadSkImage(mImage
, pixmap
.info(), pixmap
.rowBytes());
197 gfxCriticalError() << "Failed copying Skia raster snapshot";
203 } // namespace mozilla::gfx