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