tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SkImage_Raster.h (4215B)


      1 /*
      2 * Copyright 2023 Google LLC
      3 *
      4 * Use of this source code is governed by a BSD-style license that can be
      5 * found in the LICENSE file.
      6 */
      7 
      8 #ifndef SkImage_Raster_DEFINED
      9 #define SkImage_Raster_DEFINED
     10 
     11 #include "include/core/SkBitmap.h"
     12 #include "include/core/SkImage.h"
     13 #include "include/core/SkPixelRef.h"
     14 #include "include/core/SkRecorder.h"
     15 #include "include/core/SkRefCnt.h"
     16 #include "include/core/SkTypes.h"
     17 #include "include/private/base/SkTo.h"
     18 #include "src/core/SkImagePriv.h"
     19 #include "src/core/SkMipmap.h"
     20 #include "src/image/SkImage_Base.h"
     21 
     22 #include <cstddef>
     23 #include <cstdint>
     24 #include <utility>
     25 
     26 class GrDirectContext;
     27 class SkColorSpace;
     28 class SkData;
     29 class SkPixmap;
     30 class SkSurface;
     31 enum SkColorType : int;
     32 struct SkIRect;
     33 struct SkImageInfo;
     34 
     35 class SkImage_Raster : public SkImage_Base {
     36 public:
     37    SkImage_Raster(const SkImageInfo&, sk_sp<SkData>, size_t rb,
     38                   uint32_t id = kNeedNewImageUniqueID);
     39    SkImage_Raster(const SkBitmap& bm, bool bitmapMayBeMutable = false);
     40    ~SkImage_Raster() override;
     41 
     42    // From SkImage.h
     43    bool isValid(SkRecorder* recorder) const override {
     44        if (!recorder) {
     45            return false;
     46        }
     47        if (!recorder->cpuRecorder()) {
     48            return false;
     49        }
     50        return true;
     51    }
     52    sk_sp<SkImage> makeColorTypeAndColorSpace(SkRecorder*,
     53                                              SkColorType targetColorType,
     54                                              sk_sp<SkColorSpace> targetColorSpace,
     55                                              RequiredProperties) const override;
     56 
     57    // From SkImage_Base.h
     58    bool onReadPixels(GrDirectContext*, const SkImageInfo&, void*, size_t, int srcX, int srcY,
     59                      CachingHint) const override;
     60    bool onPeekPixels(SkPixmap*) const override;
     61    const SkBitmap* onPeekBitmap() const override { return &fBitmap; }
     62 
     63    bool getROPixels(GrDirectContext*, SkBitmap*, CachingHint) const override;
     64 
     65    sk_sp<SkImage> onMakeSubset(SkRecorder*, const SkIRect&, RequiredProperties) const override;
     66 
     67    sk_sp<SkSurface> onMakeSurface(SkRecorder*, const SkImageInfo&) const final;
     68 
     69    SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
     70 
     71    bool onAsLegacyBitmap(GrDirectContext*, SkBitmap*) const override;
     72 
     73    sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const override;
     74 
     75    void notifyAddedToRasterCache() const override {
     76        // We explicitly DON'T want to call INHERITED::notifyAddedToRasterCache. That ties the
     77        // lifetime of derived/cached resources to the image. In this case, we only want cached
     78        // data (eg mips) tied to the lifetime of the underlying pixelRef.
     79        SkASSERT(fBitmap.pixelRef());
     80        fBitmap.pixelRef()->notifyAddedToCache();
     81    }
     82 
     83    bool onHasMipmaps() const override { return SkToBool(fBitmap.fMips); }
     84    bool onIsProtected() const override { return false; }
     85 
     86    SkMipmap* onPeekMips() const override { return fBitmap.fMips.get(); }
     87 
     88    sk_sp<SkImage> onMakeWithMipmaps(sk_sp<SkMipmap> mips) const override {
     89        // It's dangerous to have two SkBitmaps that share a SkPixelRef but have different SkMipmaps
     90        // since various caches key on SkPixelRef's generation ID. Also, SkPixelRefs that back
     91        // SkSurfaces are marked "temporarily immutable" and making an image that uses the same
     92        // SkPixelRef can interact badly with SkSurface/SkImage copy-on-write. So we just always
     93        // make a copy with a new ID.
     94        static auto constexpr kCopyMode = SkCopyPixelsMode::kAlways_SkCopyPixelsMode;
     95        sk_sp<SkImage> img = SkMakeImageFromRasterBitmap(fBitmap, kCopyMode);
     96        auto imgRaster = static_cast<SkImage_Raster*>(img.get());
     97        if (mips) {
     98            imgRaster->fBitmap.fMips = std::move(mips);
     99        } else {
    100            imgRaster->fBitmap.fMips.reset(SkMipmap::Build(fBitmap.pixmap(), nullptr));
    101        }
    102        return img;
    103    }
    104 
    105    SkImage_Base::Type type() const override { return SkImage_Base::Type::kRaster; }
    106 
    107    SkBitmap bitmap() const { return fBitmap; }
    108 private:
    109    SkBitmap fBitmap;
    110 };
    111 
    112 sk_sp<SkImage> MakeRasterCopyPriv(const SkPixmap& pmap, uint32_t id);
    113 
    114 #endif // SkImage_Raster_DEFINED