Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
render_context_skia.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <GLES2/gl2.h>
8#include <GLES2/gl2ext.h>
9
13#include "third_party/skia/include/core/SkSurface.h"
14#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
15#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
16#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
17#include "third_party/skia/include/gpu/ganesh/gl/GrGLBackendSurface.h"
18#include "third_party/skia/include/gpu/ganesh/gl/GrGLDirectContext.h"
19#include "third_party/skia/include/gpu/ganesh/gl/GrGLInterface.h"
20#include "third_party/skia/include/gpu/ganesh/gl/GrGLMakeWebGLInterface.h"
21#include "third_party/skia/include/gpu/ganesh/gl/GrGLTypes.h"
22
24 return false;
25}
26
27namespace {
28class SkiaRenderContext : public Skwasm::RenderContext {
29 public:
30 SkiaRenderContext(int sample_count, int stencil)
31 : sample_count_(sample_count),
32 stencil_(stencil),
33 gr_context_(GrDirectContexts::MakeGL(GrGLInterfaces::MakeWebGL())),
34 fb_info_({0, GL_RGBA8_OES}) {
35 gr_context_->resetContext(kRenderTarget_GrGLBackendState |
36 kMisc_GrGLBackendState);
37 }
38
39 virtual void RenderPicture(
40 const sk_sp<flutter::DisplayList> display_list) override {
41 auto canvas = surface_->getCanvas();
42 canvas->drawColor(SK_ColorTRANSPARENT, SkBlendMode::kSrc);
43 auto dispatcher = flutter::DlSkCanvasDispatcher(canvas);
44 dispatcher.save();
45 dispatcher.drawDisplayList(display_list, 1.0f);
46 dispatcher.restore();
47
48 gr_context_->flush(surface_.get());
49 }
50
53 void* out_pixels) override {
54 // TODO(jacksongardner):
55 // Normally we'd just call `readPixels` on the image. However, this doesn't
56 // actually work in some cases due to a skia bug. Instead, we just draw the
57 // image to our scratch canvas and grab the pixels out directly with
58 // `glReadPixels`. Once the skia bug is fixed, we should switch back to
59 // using `SkImage::readPixels` instead. See
60 // https://g-issues.skia.org/issues/349201915
61 RenderImage(image, format);
62 glReadPixels(0, 0, image->width(), image->height(), GL_RGBA,
63 GL_UNSIGNED_BYTE, out_pixels);
64 return true;
65 }
66
67 virtual void Resize(int width, int height) override {
68 if (width_ != width || height_ != height) {
69 width_ = width;
70 height_ = height;
71 auto target = GrBackendRenderTargets::MakeGL(width, height, sample_count_,
72 stencil_, fb_info_);
73 surface_ = SkSurfaces::WrapBackendRenderTarget(
74 gr_context_.get(), target, kBottomLeft_GrSurfaceOrigin,
75 kRGBA_8888_SkColorType, SkColorSpace::MakeSRGB(), nullptr);
76 }
77 }
78
79 virtual void SetResourceCacheLimit(int bytes) override {
80 gr_context_->setResourceCacheLimit(bytes);
81 }
82
83 private:
84 void RenderImage(flutter::DlImage* image, Skwasm::ImageByteFormat format) {
85 auto canvas = surface_->getCanvas();
86 canvas->drawColor(SK_ColorTRANSPARENT, SkBlendMode::kSrc);
87
88 // We want the pixels from the upper left corner, but glReadPixels gives us
89 // the pixels from the lower left corner. So we have to flip the image when
90 // we are drawing it to get the pixels in the desired order.
91 canvas->save();
92 canvas->scale(1, -1);
93 auto skia_image = image ? image->asSkiaImage() : nullptr;
94 canvas->drawImage(skia_image ? skia_image->skia_image() : nullptr, 0,
95 -height_);
96 canvas->restore();
97
98 gr_context_->flush(surface_.get());
99 }
100
101 sk_sp<GrDirectContext> gr_context_ = nullptr;
102 sk_sp<SkSurface> surface_ = nullptr;
103 GrGLFramebufferInfo fb_info_;
104 GrGLint sample_count_;
105 GrGLint stencil_;
106 int width_ = 0;
107 int height_ = 0;
108};
109} // namespace
110
111std::unique_ptr<Skwasm::RenderContext> Skwasm::RenderContext::Make(
112 int sample_count,
113 int stencil) {
114 return std::make_unique<SkiaRenderContext>(sample_count, stencil);
115}
virtual void RenderPicture(const sk_sp< flutter::DisplayList > display_list)=0
static std::unique_ptr< RenderContext > Make(int sample_count, int stencil)
virtual void Resize(int width, int height)=0
virtual void SetResourceCacheLimit(int bytes)=0
virtual bool RasterizeImage(flutter::DlImage *image, ImageByteFormat format, void *out_pixels)=0
Represents an image whose allocation is (usually) resident on device memory.
Definition dl_image.h:34
Backend implementation of |DlOpReceiver| for |SkCanvas|.
FlutterVulkanImage * image
uint32_t * target
EGLSurface surface_
ImageByteFormat
Definition helpers.h:75
SKWASM_EXPORT bool skwasm_isWimp()
int32_t height
int32_t width
#define SKWASM_EXPORT
Definition export.h:10