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 
25 #include <SkImage.h>
26 
27 #include <boost/shared_ptr.hpp>
28 
29 #include <vcl/bitmap.hxx>
30 
32 {
33 public:
34  SkiaSalBitmap();
35  SkiaSalBitmap(const sk_sp<SkImage>& image);
36  virtual ~SkiaSalBitmap() override;
37 
38  // SalBitmap methods
39  virtual bool Create(const Size& rSize, sal_uInt16 nBitCount,
40  const BitmapPalette& rPal) override;
41  virtual bool Create(const SalBitmap& rSalBmp) override;
42  virtual bool Create(const SalBitmap& rSalBmp, SalGraphics* pGraphics) override;
43  virtual bool Create(const SalBitmap& rSalBmp, sal_uInt16 nNewBitCount) override;
44  virtual bool Create(const css::uno::Reference<css::rendering::XBitmapCanvas>& rBitmapCanvas,
45  Size& rSize, bool bMask = false) override;
46 
47  virtual void Destroy() final override;
48 
49  virtual Size GetSize() const override;
50  virtual sal_uInt16 GetBitCount() const override;
51 
52  virtual BitmapBuffer* AcquireBuffer(BitmapAccessMode nMode) override;
53  virtual void ReleaseBuffer(BitmapBuffer* pBuffer, BitmapAccessMode nMode) override;
54 
55  virtual bool GetSystemData(BitmapSystemData& rData) override;
56 
57  virtual bool ScalingSupported() const override;
58  virtual bool Scale(const double& rScaleX, const double& rScaleY,
59  BmpScaleFlag nScaleFlag) override;
60  virtual bool Replace(const Color& rSearchColor, const Color& rReplaceColor,
61  sal_uInt8 nTol) override;
62  virtual bool InterpretAs8Bit() override;
63  virtual bool ConvertToGreyscale() override;
64  virtual bool Erase(const Color& color) override;
65  virtual bool AlphaBlendWith(const SalBitmap& rSalBmp) override;
66 
67  const BitmapPalette& Palette() const { return mPalette; }
68 
69  // True if GetSkShader() should be preferred to GetSkImage() (or the Alpha variants).
70  bool PreferSkShader() const;
71 
72  // Returns the contents as SkImage (possibly GPU-backed).
73  const sk_sp<SkImage>& GetSkImage() const;
74  sk_sp<SkShader> GetSkShader(const SkSamplingOptions& samplingOptions) const;
75 
76  // Returns the contents as alpha SkImage (possibly GPU-backed)
77  const sk_sp<SkImage>& GetAlphaSkImage() const;
78  sk_sp<SkShader> GetAlphaSkShader(const SkSamplingOptions& samplingOptions) const;
79 
80  // Key for caching/hashing.
81  OString GetImageKey() const;
82  OString GetAlphaImageKey() const;
83 
84  // Returns true if it is known that this bitmap can be ignored if it's to be used
85  // as an alpha bitmap. An optimization, not guaranteed to return true for all such cases.
86  bool IsFullyOpaqueAsAlpha() const;
87  // Alpha type best suitable for the content.
88  SkAlphaType alphaType() const;
89 
90 #ifdef DBG_UTIL
91  void dump(const char* file) const;
92 #endif
93 
94  // These are to be used only by unittests.
95  bool unittestHasBuffer() const { return mBuffer.get(); }
96  bool unittestHasImage() const { return mImage.get(); }
97  bool unittestHasAlphaImage() const { return mAlphaImage.get(); }
98  bool unittestHasEraseColor() const { return mEraseColorSet; }
99  const sal_uInt8* unittestGetBuffer() const { return mBuffer.get(); }
100  const SkImage* unittestGetImage() const { return mImage.get(); }
101  const SkImage* unittestGetAlphaImage() const { return mAlphaImage.get(); }
102 
103 private:
104  // Reset the state to pixel data (resets cached images allocated in GetSkImage()/GetAlphaSkImage()).
105  void ResetToBuffer();
106  // Sets the data only as SkImage (will be converted as needed).
107  void ResetToSkImage(sk_sp<SkImage> image);
108  // Resets all data (buffer and images).
109  void ResetAllData();
110  // Call to ensure mBuffer has data (will convert from mImage if necessary).
111  void EnsureBitmapData();
112  void EnsureBitmapData() const { return const_cast<SkiaSalBitmap*>(this)->EnsureBitmapData(); }
113  // Like EnsureBitmapData(), but will also make any shared data unique.
114  // Call before changing the data.
115  void EnsureBitmapUniqueData();
116  // Allocate mBuffer (with uninitialized contents).
117  void CreateBitmapData();
118  // Should be called whenever mPixelsSize or mBitCount is set/changed.
119  bool ComputeScanlineSize();
120  // Resets information about pending scaling. To be called when mBuffer is resized or created.
121  void ResetPendingScaling();
122  // Sets bitmap to be erased on demand.
123  void EraseInternal(const Color& color);
124  // Sets pixels to the erase color.
125  void PerformErase();
126  // Try to find out if the content is completely black. Used for optimizations,
127  // not guaranteed to always return true for such bitmaps.
128  bool IsAllBlack() const;
129  void ReleaseBuffer(BitmapBuffer* pBuffer, BitmapAccessMode nMode, bool dontChangeToErase);
130  SkBitmap GetAsSkBitmap() const;
131  bool ConserveMemory() const;
132  void verify() const
133 #ifdef DBG_UTIL
134  ;
135 #else
136  {
137  }
138 #endif
139 
140  template <typename charT, typename traits>
141  friend inline std::basic_ostream<charT, traits>&
142  operator<<(std::basic_ostream<charT, traits>& stream, const SkiaSalBitmap* bitmap)
143  {
144  if (bitmap == nullptr)
145  return stream << "(null)";
146  // p - has (non-trivial) palette
147  // I/i - has SkImage (on GPU/CPU),
148  // A/a - has alpha SkImage (on GPU/CPU)
149  // E - has erase color
150  stream << static_cast<const void*>(bitmap) << " " << bitmap->GetSize() << "x"
151  << bitmap->GetBitCount();
152  if (bitmap->GetBitCount() <= 8 && !bitmap->Palette().IsGreyPalette8Bit())
153  stream << "p";
154  stream << "/";
155  if (bitmap->mImage)
156  stream << (bitmap->mImage->isTextureBacked() ? "I" : "i");
157  if (bitmap->mAlphaImage)
158  stream << (bitmap->mAlphaImage->isTextureBacked() ? "A" : "a");
159  if (bitmap->mEraseColorSet)
160  stream << "E" << bitmap->mEraseColor;
161  return stream;
162  }
163 
165  int mBitCount = 0; // bpp
167  // The contents of the bitmap may be stored in several different ways:
168  // As mBuffer buffer, which normally stores pixels in the given format.
169  // As SkImage, as cached GPU-backed data, but sometimes also a result of some operation.
170  // There is no "master" storage that the other would be derived from. The usual
171  // mode of operation is that mBuffer holds the data, mImage is created
172  // on demand as GPU-backed cached data by calling GetSkImage(), and the cached mImage
173  // is reset by ResetCachedImage(). But sometimes only mImage will be set and in that case
174  // mBuffer must be filled from it on demand if necessary by EnsureBitmapData().
175  boost::shared_ptr<sal_uInt8[]> mBuffer;
176  int mScanlineSize; // size of one row in mBuffer (based on mPixelsSize)
177  sk_sp<SkImage> mImage; // possibly GPU-backed
178  sk_sp<SkImage> mAlphaImage; // cached contents as alpha image, possibly GPU-backed
179  // Actual scaling triggered by scale() is done on-demand. This is the size of the pixel
180  // data in mBuffer, if it differs from mSize, then there is a scaling operation pending.
182  BmpScaleFlag mScaleQuality = BmpScaleFlag::BestQuality; // quality for on-demand scaling
183  // Erase() is delayed, just sets these two instead of filling the buffer.
184  bool mEraseColorSet = false;
186  int mAnyAccessCount = 0; // number of any kind of AcquireAccess() that have not been released
187 #ifdef DBG_UTIL
188  int mWriteAccessCount = 0; // number of write AcquireAccess() that have not been released
189 #endif
190 };
191 
192 #endif // INCLUDED_VCL_INC_SKIA_SALBMP_H
193 
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void EnsureBitmapData() const
virtual bool AlphaBlendWith(const SalBitmap &)
Definition: salbmp.hxx:94
bool unittestHasAlphaImage() const
Definition: skia/salbmp.hxx:97
const ContentProperties & rData
bool unittestHasBuffer() const
Definition: skia/salbmp.hxx:95
BitmapPalette mPalette
const BitmapPalette & Palette() const
Definition: skia/salbmp.hxx:67
virtual bool ScalingSupported() const =0
virtual Size GetSize() const =0
virtual bool GetSystemData(BitmapSystemData &rData)=0
virtual sal_uInt16 GetBitCount() const =0
virtual bool ConvertToGreyscale()
Definition: salbmp.hxx:80
virtual bool Create(const Size &rSize, sal_uInt16 nBitCount, const BitmapPalette &rPal)=0
bool unittestHasEraseColor() const
Definition: skia/salbmp.hxx:98
virtual bool InterpretAs8Bit()
Definition: salbmp.hxx:84
sk_sp< SkImage > mAlphaImage
sk_sp< SkImage > mImage
virtual void Destroy()=0
virtual Size GetSize() const override
virtual bool Replace(const Color &rSearchColor, const Color &rReplaceColor, sal_uInt8 nTol)=0
BitmapAccessMode
BmpScaleFlag
Definition: bitmap.hxx:54
const sal_uInt8 * unittestGetBuffer() const
Definition: skia/salbmp.hxx:99
VCL_DLLPUBLIC void dump(const SkBitmap &bitmap, const char *file)
virtual bool Erase(const Color &)
Definition: salbmp.hxx:89
short nBitCount
Definition: ipict.cxx:80
Reference< XOutputStream > stream
#define VCL_PLUGIN_PUBLIC
Definition: dllapi.h:40
const SkImage * unittestGetImage() const
unsigned char sal_uInt8
virtual bool Scale(const double &rScaleX, const double &rScaleY, BmpScaleFlag nScaleFlag)=0
const SkImage * unittestGetAlphaImage() const
bool unittestHasImage() const
Definition: skia/salbmp.hxx:96
virtual void ReleaseBuffer(BitmapBuffer *pBuffer, BitmapAccessMode nMode)=0
boost::shared_ptr< sal_uInt8[]> mBuffer
virtual BitmapBuffer * AcquireBuffer(BitmapAccessMode nMode)=0