xref: /aosp_15_r20/external/skia/src/image/SkSurface_Null.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #include "include/core/SkCapabilities.h"
9 #include "include/core/SkImage.h"
10 #include "include/core/SkImageInfo.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkScalar.h"
13 #include "include/core/SkSurface.h"
14 #include "include/utils/SkNoDrawCanvas.h"
15 #include "src/image/SkSurface_Base.h"
16 
17 class SkCanvas;
18 class SkPaint;
19 class SkPixmap;
20 struct SkIRect;
21 struct SkSamplingOptions;
22 
23 class SkNullSurface : public SkSurface_Base {
24 public:
SkNullSurface(int width,int height)25     SkNullSurface(int width, int height) : SkSurface_Base(width, height, nullptr) {}
26 
27     // From SkSurface_Base.h
type() const28     SkSurface_Base::Type type() const override { return SkSurface_Base::Type::kNull; }
29 
30 protected:
onNewCanvas()31     SkCanvas* onNewCanvas() override {
32         return new SkNoDrawCanvas(this->width(), this->height());
33     }
onNewSurface(const SkImageInfo & info)34     sk_sp<SkSurface> onNewSurface(const SkImageInfo& info) override {
35         return SkSurfaces::Null(info.width(), info.height());
36     }
onNewImageSnapshot(const SkIRect * subsetOrNull)37     sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subsetOrNull) override { return nullptr; }
onWritePixels(const SkPixmap &,int x,int y)38     void onWritePixels(const SkPixmap&, int x, int y) override {}
onDraw(SkCanvas *,SkScalar,SkScalar,const SkSamplingOptions &,const SkPaint *)39     void onDraw(SkCanvas*, SkScalar, SkScalar, const SkSamplingOptions&, const SkPaint*) override {}
onCopyOnWrite(ContentChangeMode)40     bool onCopyOnWrite(ContentChangeMode) override { return true; }
onCapabilities()41     sk_sp<const SkCapabilities> onCapabilities() override {
42         // Not really, but we have to return *something*
43         return SkCapabilities::RasterBackend();
44     }
imageInfo() const45     SkImageInfo imageInfo() const override {
46         return SkImageInfo::MakeUnknown(this->width(), this->height());
47     }
48 };
49 
50 namespace SkSurfaces {
51 
Null(int width,int height)52 sk_sp<SkSurface> Null(int width, int height) {
53     if (width < 1 || height < 1) {
54         return nullptr;
55     }
56     return sk_sp<SkSurface>(new SkNullSurface(width, height));
57 }
58 
59 }
60