LibreOffice Module vcl (master) 1
skia/salbmp.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#ifndef INCLUDED_VCL_INC_SKIA_SALBMP_H
21#define INCLUDED_VCL_INC_SKIA_SALBMP_H
22
23#include <salbmp.hxx>
24#include <vcl/bitmap.hxx>
25
26#include <boost/shared_ptr.hpp>
27
28#include <skia/utils.hxx>
29
30#include <SkImage.h>
31
33{
34public:
36 SkiaSalBitmap(const sk_sp<SkImage>& image);
37 virtual ~SkiaSalBitmap() override;
38
39 // SalBitmap methods
40 virtual bool Create(const Size& rSize, vcl::PixelFormat ePixelFormat,
41 const BitmapPalette& rPal) override;
42 virtual bool Create(const SalBitmap& rSalBmp) override;
43 virtual bool Create(const SalBitmap& rSalBmp, SalGraphics* pGraphics) override;
44 virtual bool Create(const SalBitmap& rSalBmp, vcl::PixelFormat eNewPixelFormat) override;
45 virtual bool Create(const css::uno::Reference<css::rendering::XBitmapCanvas>& rBitmapCanvas,
46 Size& rSize, bool bMask = false) override;
47
48 virtual void Destroy() final override;
49
50 virtual Size GetSize() const override;
51 virtual sal_uInt16 GetBitCount() const override;
52
53 virtual BitmapBuffer* AcquireBuffer(BitmapAccessMode nMode) override;
54 virtual void ReleaseBuffer(BitmapBuffer* pBuffer, BitmapAccessMode nMode) override;
55
56 virtual bool GetSystemData(BitmapSystemData& rData) override;
57
58 virtual bool ScalingSupported() const override;
59 virtual bool Scale(const double& rScaleX, const double& rScaleY,
60 BmpScaleFlag nScaleFlag) override;
61 virtual bool Replace(const Color& rSearchColor, const Color& rReplaceColor,
62 sal_uInt8 nTol) override;
63 virtual bool InterpretAs8Bit() override;
64 virtual bool ConvertToGreyscale() override;
65 virtual bool Erase(const Color& color) override;
66 virtual bool AlphaBlendWith(const SalBitmap& rSalBmp) override;
67
68 const BitmapPalette& Palette() const { return mPalette; }
69
70 // True if GetSkShader() should be preferred to GetSkImage() (or the Alpha variants).
71 bool PreferSkShader() const;
72
73 // Direct image means direct access to the stored SkImage, without checking
74 // if its size is up to date. This should be used only in special cases with care.
76 // Returns the contents as SkImage (possibly GPU-backed).
77 const sk_sp<SkImage>& GetSkImage(DirectImage direct = DirectImage::No) const;
78 sk_sp<SkShader> GetSkShader(const SkSamplingOptions& samplingOptions,
79 DirectImage direct = DirectImage::No) const;
80 // Returns the contents as alpha SkImage (possibly GPU-backed)
81 const sk_sp<SkImage>& GetAlphaSkImage(DirectImage direct = DirectImage::No) const;
82 sk_sp<SkShader> GetAlphaSkShader(const SkSamplingOptions& samplingOptions,
83 DirectImage direct = DirectImage::No) const;
84
85 // Key for caching/hashing.
86 OString GetImageKey(DirectImage direct = DirectImage::No) const;
88
89 // Returns true if it is known that this bitmap can be ignored if it's to be used
90 // as an alpha bitmap. An optimization, not guaranteed to return true for all such cases.
92 // Alpha type best suitable for the content.
93 SkAlphaType alphaType() const;
94
95 // Tries to create direct GetAlphaSkImage() from direct GetSkImage().
97
98 // Dump contents to a file for debugging.
99 void dump(const char* file) const;
100
101 // These are to be used only by unittests.
102 bool unittestHasBuffer() const { return mBuffer.get(); }
103 bool unittestHasImage() const { return mImage.get(); }
104 bool unittestHasAlphaImage() const { return mAlphaImage.get(); }
105 bool unittestHasEraseColor() const { return mEraseColorSet; }
106 bool unittestHasPendingScale() const { return mSize != mPixelsSize; }
107 const sal_uInt8* unittestGetBuffer() const { return mBuffer.get(); }
108 const SkImage* unittestGetImage() const { return mImage.get(); }
109 const SkImage* unittestGetAlphaImage() const { return mAlphaImage.get(); }
110 void unittestResetToImage() { ResetToSkImage(GetSkImage()); }
111
112private:
113 // This should be called whenever the contents have (possibly) changed.
114 // It may reset some cached data such as the checksum.
116 // Reset the state to pixel data (resets cached images allocated in GetSkImage()/GetAlphaSkImage()).
118 // Sets the data only as SkImage (will be converted as needed).
119 void ResetToSkImage(sk_sp<SkImage> image);
120 // Resets all data (buffer and images).
122 // Call to ensure mBuffer has data (will convert from mImage if necessary).
124 void EnsureBitmapData() const { return const_cast<SkiaSalBitmap*>(this)->EnsureBitmapData(); }
125 // Like EnsureBitmapData(), but will also make any shared data unique.
126 // Call before changing the data.
128 // Allocate mBuffer (with uninitialized contents).
130 // Should be called whenever mPixelsSize or mBitCount is set/changed.
132 // Resets information about pending scaling. To be called when mBuffer is resized or created.
134 // Sets bitmap to be erased on demand.
136 // Sets pixels to the erase color.
138 // Try to find out if the content is completely black. Used for optimizations,
139 // not guaranteed to always return true for such bitmaps.
140 bool IsAllBlack() const;
141 void ReleaseBuffer(BitmapBuffer* pBuffer, BitmapAccessMode nMode, bool dontChangeToErase);
142 SkBitmap GetAsSkBitmap() const;
143 bool ConserveMemory() const;
144 void verify() const
145#ifdef DBG_UTIL
146 ;
147#else
148 {
149 }
150#endif
151
152 template <typename charT, typename traits>
153 friend std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
154 const SkiaSalBitmap* bitmap)
155 {
156 if (bitmap == nullptr)
157 return stream << "(null)";
158 // p - has (non-trivial) palette
159 // I/i - has SkImage (on GPU/CPU),
160 // A/a - has alpha SkImage (on GPU/CPU)
161 // E - has erase color
162 // B - has pixel buffer
163 // (wxh) - has pending scaling (after each item)
164 stream << static_cast<const void*>(bitmap) << " " << bitmap->GetSize() << "x"
165 << bitmap->GetBitCount();
166 if (bitmap->GetBitCount() <= 8 && !bitmap->Palette().IsGreyPalette8Bit())
167 stream << "p";
168 stream << "/";
169 if (bitmap->mImage)
170 {
171 stream << (bitmap->mImage->isTextureBacked() ? "I" : "i");
172 if (SkiaHelper::imageSize(bitmap->mImage) != bitmap->mSize)
173 stream << "(" << SkiaHelper::imageSize(bitmap->mImage) << ")";
174 }
175 if (bitmap->mAlphaImage)
176 {
177 stream << (bitmap->mAlphaImage->isTextureBacked() ? "A" : "a");
178 if (SkiaHelper::imageSize(bitmap->mAlphaImage) != bitmap->mSize)
179 stream << "(" << SkiaHelper::imageSize(bitmap->mAlphaImage) << ")";
180 }
181 if (bitmap->mEraseColorSet)
182 stream << "E" << bitmap->mEraseColor;
183 if (bitmap->mBuffer)
184 {
185 stream << "B";
186 if (bitmap->mSize != bitmap->mPixelsSize)
187 stream << "(" << bitmap->mPixelsSize << ")";
188 }
189 return stream;
190 }
191
193 int mBitCount = 0; // bpp
195 // The contents of the bitmap may be stored in several different ways:
196 // As mBuffer buffer, which normally stores pixels in the given format.
197 // As SkImage, as cached GPU-backed data, but sometimes also a result of some operation.
198 // There is no "master" storage that the other would be derived from. The usual
199 // mode of operation is that mBuffer holds the data, mImage is created
200 // on demand as GPU-backed cached data by calling GetSkImage(), and the cached mImage
201 // is reset by ResetCachedImage(). But sometimes only mImage will be set and in that case
202 // mBuffer must be filled from it on demand if necessary by EnsureBitmapData().
203 boost::shared_ptr<sal_uInt8[]> mBuffer;
204 int mScanlineSize; // size of one row in mBuffer (based on mPixelsSize)
205 sk_sp<SkImage> mImage; // possibly GPU-backed
206 sk_sp<SkImage> mAlphaImage; // cached contents as alpha image, possibly GPU-backed
207 // Actual scaling triggered by scale() is done on-demand. This is the size of the pixel
208 // data in mBuffer, if it differs from mSize, then there is a scaling operation pending.
210 BmpScaleFlag mScaleQuality = BmpScaleFlag::BestQuality; // quality for on-demand scaling
211 // Erase() is delayed, just sets these two instead of filling the buffer.
212 bool mEraseColorSet = false;
214 int mAnyAccessCount = 0; // number of any kind of AcquireAccess() that have not been released
215#ifdef DBG_UTIL
216 int mWriteAccessCount = 0; // number of write AcquireAccess() that have not been released
217#endif
218};
219
220#endif // INCLUDED_VCL_INC_SKIA_SALBMP_H
221
222/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
BitmapAccessMode
bool IsGreyPalette8Bit() const
Returns true if the palette is 8-bit grey palette.
const SkImage * unittestGetImage() const
boost::shared_ptr< sal_uInt8[]> mBuffer
void ResetToSkImage(sk_sp< SkImage > image)
virtual sal_uInt16 GetBitCount() const override
void unittestResetToImage()
const SkImage * unittestGetAlphaImage() const
const sk_sp< SkImage > & GetSkImage(DirectImage direct=DirectImage::No) const
bool PreferSkShader() const
bool ComputeScanlineSize()
void ResetToBuffer()
virtual ~SkiaSalBitmap() override
bool unittestHasBuffer() const
virtual Size GetSize() const override
sk_sp< SkImage > mImage
bool unittestHasAlphaImage() const
bool IsAllBlack() const
const sk_sp< SkImage > & GetAlphaSkImage(DirectImage direct=DirectImage::No) const
virtual bool Create(const css::uno::Reference< css::rendering::XBitmapCanvas > &rBitmapCanvas, Size &rSize, bool bMask=false) override
SkAlphaType alphaType() const
SkiaSalBitmap(const sk_sp< SkImage > &image)
void PerformErase()
sk_sp< SkShader > GetAlphaSkShader(const SkSamplingOptions &samplingOptions, DirectImage direct=DirectImage::No) const
virtual bool Create(const SalBitmap &rSalBmp) override
friend std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &stream, const SkiaSalBitmap *bitmap)
const BitmapPalette & Palette() const
Definition: skia/salbmp.hxx:68
SkBitmap GetAsSkBitmap() const
void dump(const char *file) const
void ResetPendingScaling()
void verify() const
void DataChanged()
void EnsureBitmapData()
bool unittestHasEraseColor() const
sk_sp< SkShader > GetSkShader(const SkSamplingOptions &samplingOptions, DirectImage direct=DirectImage::No) const
const sal_uInt8 * unittestGetBuffer() const
OString GetImageKey(DirectImage direct=DirectImage::No) const
bool IsFullyOpaqueAsAlpha() const
BitmapPalette mPalette
sk_sp< SkImage > mAlphaImage
bool unittestHasImage() const
void TryDirectConvertToAlphaNoScaling()
void EnsureBitmapUniqueData()
void ResetAllData()
void EraseInternal(const Color &color)
void CreateBitmapData()
virtual bool Create(const Size &rSize, vcl::PixelFormat ePixelFormat, const BitmapPalette &rPal) override
OString GetAlphaImageKey(DirectImage direct=DirectImage::No) const
virtual bool Create(const SalBitmap &rSalBmp, SalGraphics *pGraphics) override
virtual bool Create(const SalBitmap &rSalBmp, vcl::PixelFormat eNewPixelFormat) override
void ReleaseBuffer(BitmapBuffer *pBuffer, BitmapAccessMode nMode, bool dontChangeToErase)
virtual void Destroy() final override
void EnsureBitmapData() const
bool ConserveMemory() const
bool unittestHasPendingScale() const
#define VCL_PLUGIN_PUBLIC
Definition: dllapi.h:40
Reference< XOutputStream > stream
Size imageSize(const sk_sp< SkImage > &image)
Definition: utils.hxx:76
PixelFormat
Pixel format of the bitmap in bits per pixel.
Definition: BitmapTypes.hxx:20
unsigned char sal_uInt8