blob: 3a068594cdaa0c3e9b036cb1576b40a968e0f1f6 [file] [log] [blame]
Jonathan Backer7f90dfb662017-12-18 16:52:041// Copyright (c) 2017 The Chromium 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
5#include "gpu/command_buffer/service/raster_decoder.h"
6
Jonathan Backer7471b2782018-01-25 18:14:197#include <stdint.h>
8
Jonathan Backer0cd1c4322018-04-17 16:57:109#include <algorithm>
10#include <memory>
Jonathan Backer7471b2782018-01-25 18:14:1911#include <string>
Antoine Labourb9f903b2019-02-04 18:06:5712#include <utility>
Jonathan Backer6307cee92018-04-25 19:53:4313#include <vector>
Jonathan Backer7471b2782018-01-25 18:14:1914
Khushal996e9912018-07-13 08:31:0015#include "base/atomic_sequence_num.h"
Sebastien Marchand6d0558fd2019-01-25 16:49:3716#include "base/bind.h"
Peng Huangcb2042092018-12-04 13:42:3417#include "base/containers/flat_map.h"
Christopher Camerond5698f682018-12-11 00:59:2018#include "base/debug/crash_logging.h"
Jonathan Backer7f90dfb662017-12-18 16:52:0419#include "base/logging.h"
Jonathan Backer7471b2782018-01-25 18:14:1920#include "base/memory/ref_counted.h"
21#include "base/memory/weak_ptr.h"
Avi Drissman6fc2408c2018-12-25 21:29:0822#include "base/stl_util.h"
Jonathan Backere19973e82018-04-18 20:08:0923#include "base/strings/stringprintf.h"
Jonathan Backer7f90dfb662017-12-18 16:52:0424#include "base/trace_event/trace_event.h"
Jonathan Backer10821a82018-04-04 23:56:0325#include "build/build_config.h"
Khushal33205a72018-11-08 10:12:2926#include "cc/paint/paint_cache.h"
Jonathan Backer0cd1c4322018-04-17 16:57:1027#include "cc/paint/paint_op_buffer.h"
28#include "cc/paint/transfer_cache_entry.h"
Jonathan Backera07c4a9e2018-03-21 15:13:1629#include "components/viz/common/resources/resource_format_utils.h"
Jonathan Backer7f90dfb662017-12-18 16:52:0430#include "gpu/command_buffer/common/capabilities.h"
Jonathan Backer7471b2782018-01-25 18:14:1931#include "gpu/command_buffer/common/command_buffer_id.h"
32#include "gpu/command_buffer/common/constants.h"
Jonathan Backer7f90dfb662017-12-18 16:52:0433#include "gpu/command_buffer/common/context_result.h"
Jonathan Backer7471b2782018-01-25 18:14:1934#include "gpu/command_buffer/common/debug_marker_manager.h"
Jonathan Backera07c4a9e2018-03-21 15:13:1635#include "gpu/command_buffer/common/mailbox.h"
Jonathan Backer7471b2782018-01-25 18:14:1936#include "gpu/command_buffer/common/raster_cmd_format.h"
37#include "gpu/command_buffer/common/raster_cmd_ids.h"
Jonathan Backer7f90dfb662017-12-18 16:52:0438#include "gpu/command_buffer/common/sync_token.h"
Jonathan Backer4f9ee5fb2018-04-25 14:03:3839#include "gpu/command_buffer/service/command_buffer_service.h"
Jonathan Backer7471b2782018-01-25 18:14:1940#include "gpu/command_buffer/service/context_state.h"
Jonathan Backer1d807a42018-01-08 20:45:5441#include "gpu/command_buffer/service/decoder_client.h"
Jonathan Backer7471b2782018-01-25 18:14:1942#include "gpu/command_buffer/service/error_state.h"
43#include "gpu/command_buffer/service/feature_info.h"
Jonathan Backer10821a82018-04-04 23:56:0344#include "gpu/command_buffer/service/gl_stream_texture_image.h"
Jonathan Backerc26060e2018-03-29 15:06:2945#include "gpu/command_buffer/service/gl_utils.h"
Jonathan Backer10821a82018-04-04 23:56:0346#include "gpu/command_buffer/service/gles2_cmd_copy_tex_image.h"
47#include "gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.h"
Jonathan Backere26739c2018-05-15 13:27:0748#include "gpu/command_buffer/service/gpu_tracer.h"
Jonathan Backer7471b2782018-01-25 18:14:1949#include "gpu/command_buffer/service/logger.h"
Jonathan Backera07c4a9e2018-03-21 15:13:1650#include "gpu/command_buffer/service/mailbox_manager.h"
Jonathan Backer016bd97e2018-03-14 15:26:3951#include "gpu/command_buffer/service/query_manager.h"
Jonathan Backer9267bf1ec2018-03-06 19:56:5052#include "gpu/command_buffer/service/raster_cmd_validation.h"
Khushala8d50642018-05-03 01:29:0653#include "gpu/command_buffer/service/service_font_manager.h"
Jonathan Backer0cd1c4322018-04-17 16:57:1054#include "gpu/command_buffer/service/service_transfer_cache.h"
Antoine Labour10dddca12019-02-19 19:09:2655#include "gpu/command_buffer/service/service_utils.h"
Peng Huang8294a5a7f2019-01-23 18:56:2656#include "gpu/command_buffer/service/shared_context_state.h"
Eric Karld69119cb2018-10-31 22:19:3757#include "gpu/command_buffer/service/shared_image_factory.h"
Eric Karl3750cfa2018-10-25 16:54:1758#include "gpu/command_buffer/service/shared_image_representation.h"
Jonathan Backera1f3d7c2018-10-16 14:46:3259#include "gpu/command_buffer/service/skia_utils.h"
Jonathan Backera1f3d7c2018-10-16 14:46:3260#include "gpu/command_buffer/service/wrapped_sk_image.h"
Peng Huang3c13b502019-05-08 01:40:0061#include "gpu/vulkan/buildflags.h"
Jonathan Backere19973e82018-04-18 20:08:0962#include "third_party/skia/include/core/SkCanvas.h"
Peng Huang050dee52018-09-05 15:54:0863#include "third_party/skia/include/core/SkDeferredDisplayListRecorder.h"
Peng Huanga8b0e422019-07-16 17:48:5764#include "third_party/skia/include/core/SkPromiseImageTexture.h"
Jonathan Backere19973e82018-04-18 20:08:0965#include "third_party/skia/include/core/SkSurface.h"
66#include "third_party/skia/include/core/SkSurfaceProps.h"
67#include "third_party/skia/include/core/SkTypeface.h"
Peng Huang5f9118b02019-04-24 20:18:2168#include "third_party/skia/include/gpu/GrBackendSemaphore.h"
Jonathan Backere19973e82018-04-18 20:08:0969#include "third_party/skia/include/gpu/GrBackendSurface.h"
Jonathan Backer0cd1c4322018-04-17 16:57:1070#include "third_party/skia/include/gpu/GrContext.h"
Jonathan Backer4fb9f5acb2018-05-10 22:19:3371#include "third_party/skia/include/gpu/GrTypes.h"
Jonathan Backer76dfd502018-05-03 18:34:3172#include "ui/gfx/buffer_format_util.h"
Peng Huanga8b0e422019-07-16 17:48:5773#include "ui/gfx/skia_util.h"
Jonathan Backer7f90dfb662017-12-18 16:52:0474#include "ui/gl/gl_context.h"
Jonathan Backere19973e82018-04-18 20:08:0975#include "ui/gl/gl_gl_api_implementation.h"
Jonathan Backer7f90dfb662017-12-18 16:52:0476#include "ui/gl/gl_surface.h"
Jonathan Backera4568da12018-01-31 16:25:0477#include "ui/gl/gl_version_info.h"
Jonathan Backer7f90dfb662017-12-18 16:52:0478
Peng Huang3c13b502019-05-08 01:40:0079#if BUILDFLAG(ENABLE_VULKAN)
80#include "components/viz/common/gpu/vulkan_context_provider.h"
81#include "gpu/vulkan/vulkan_device_queue.h"
82#endif
83
Jonathan Backer7f90dfb662017-12-18 16:52:0484// Local versions of the SET_GL_ERROR macros
85#define LOCAL_SET_GL_ERROR(error, function_name, msg) \
Peng Huang66a7a3762018-12-07 20:05:3886 ERRORSTATE_SET_GL_ERROR(error_state_.get(), error, function_name, msg)
87#define LOCAL_SET_GL_ERROR_INVALID_ENUM(function_name, value, label) \
88 ERRORSTATE_SET_GL_ERROR_INVALID_ENUM(error_state_.get(), function_name, \
Jonathan Backer9267bf1ec2018-03-06 19:56:5089 static_cast<uint32_t>(value), label)
Peng Huangb4ed1852018-12-05 03:35:2990#define LOCAL_COPY_REAL_GL_ERRORS_TO_WRAPPER(function_name) \
Peng Huang66a7a3762018-12-07 20:05:3891 ERRORSTATE_COPY_REAL_GL_ERRORS_TO_WRAPPER(error_state_.get(), function_name)
Jonathan Backer7f90dfb662017-12-18 16:52:0492#define LOCAL_PEEK_GL_ERROR(function_name) \
Peng Huang66a7a3762018-12-07 20:05:3893 ERRORSTATE_PEEK_GL_ERROR(error_state_.get(), function_name)
Jonathan Backer7f90dfb662017-12-18 16:52:0494#define LOCAL_CLEAR_REAL_GL_ERRORS(function_name) \
Peng Huang66a7a3762018-12-07 20:05:3895 ERRORSTATE_CLEAR_REAL_GL_ERRORS(error_state_.get(), function_name)
Jonathan Backer7f90dfb662017-12-18 16:52:0496#define LOCAL_PERFORMANCE_WARNING(msg) \
97 PerformanceWarning(__FILE__, __LINE__, msg)
98#define LOCAL_RENDER_WARNING(msg) RenderWarning(__FILE__, __LINE__, msg)
99
Jonathan Backer7f90dfb662017-12-18 16:52:04100namespace gpu {
101namespace raster {
102
Jonathan Backera07c4a9e2018-03-21 15:13:16103namespace {
104
Khushal996e9912018-07-13 08:31:00105base::AtomicSequenceNumber g_raster_decoder_id;
106
Jonathan Backer1994ff2e2018-03-22 21:24:33107// This class prevents any GL errors that occur when it is in scope from
108// being reported to the client.
109class ScopedGLErrorSuppressor {
110 public:
Daniel Bratellaabc8d68e2018-07-18 19:20:26111 ScopedGLErrorSuppressor(const char* function_name,
Peng Huangc6a76072018-11-27 23:17:31112 gles2::ErrorState* error_state)
113 : function_name_(function_name), error_state_(error_state) {
114 ERRORSTATE_COPY_REAL_GL_ERRORS_TO_WRAPPER(error_state_, function_name_);
115 }
116 ~ScopedGLErrorSuppressor() {
117 ERRORSTATE_CLEAR_REAL_GL_ERRORS(error_state_, function_name_);
118 }
Jonathan Backer1994ff2e2018-03-22 21:24:33119
120 private:
121 const char* function_name_;
Daniel Bratellaabc8d68e2018-07-18 19:20:26122 gles2::ErrorState* error_state_;
Jonathan Backer1994ff2e2018-03-22 21:24:33123 DISALLOW_COPY_AND_ASSIGN(ScopedGLErrorSuppressor);
124};
125
Jonathan Backer1994ff2e2018-03-22 21:24:33126// Temporarily changes a decoder's bound texture and restore it when this
127// object goes out of scope. Also temporarily switches to using active texture
128// unit zero in case the client has changed that to something invalid.
129class ScopedTextureBinder {
130 public:
Daniel Bratellaabc8d68e2018-07-18 19:20:26131 ScopedTextureBinder(gles2::ContextState* state,
Peng Huange89c2a052018-12-04 11:35:11132 GLenum target,
133 GLuint texture,
Antoine Labour2c9d3fb2019-01-19 08:43:43134 GrContext* gr_context)
135 : state_(state), target_(target) {
Peng Huange89c2a052018-12-04 11:35:11136 auto* api = state->api();
137 api->glActiveTextureFn(GL_TEXTURE0);
138 api->glBindTextureFn(target_, texture);
139 if (gr_context)
140 gr_context->resetContext(kTextureBinding_GrGLBackendState);
141 }
142
Antoine Labour2c9d3fb2019-01-19 08:43:43143 ~ScopedTextureBinder() { state_->api()->glBindTextureFn(target_, 0); }
Jonathan Backer1994ff2e2018-03-22 21:24:33144
145 private:
Daniel Bratellaabc8d68e2018-07-18 19:20:26146 gles2::ContextState* state_;
Jonathan Backer1994ff2e2018-03-22 21:24:33147 GLenum target_;
Jonathan Backer1994ff2e2018-03-22 21:24:33148
149 DISALLOW_COPY_AND_ASSIGN(ScopedTextureBinder);
150};
151
Peng Huangc6a76072018-11-27 23:17:31152// Temporarily changes a decoder's PIXEL_UNPACK_BUFFER to 0 and set pixel
153// unpack params to default, and restore them when this object goes out of
154// scope.
Jonathan Backerc26060e2018-03-29 15:06:29155class ScopedPixelUnpackState {
156 public:
Peng Huange89c2a052018-12-04 11:35:11157 explicit ScopedPixelUnpackState(gles2::ContextState* state,
158 GrContext* gr_context,
159 const gles2::FeatureInfo* feature_info) {
160 DCHECK(state);
161 auto* api = state->api();
162 api->glPixelStoreiFn(GL_UNPACK_ALIGNMENT, 4);
163 if (feature_info->gl_version_info().is_es3 ||
164 feature_info->gl_version_info().is_desktop_core_profile ||
165 feature_info->feature_flags().ext_pixel_buffer_object)
166 api->glBindBufferFn(GL_PIXEL_UNPACK_BUFFER, 0);
167
168 if (feature_info->gl_version_info().is_es3 ||
169 feature_info->gl_version_info().is_desktop_core_profile ||
170 feature_info->feature_flags().ext_unpack_subimage)
171 api->glPixelStoreiFn(GL_UNPACK_ROW_LENGTH, 0);
172 if (gr_context) {
173 gr_context->resetContext(kMisc_GrGLBackendState |
174 kPixelStore_GrGLBackendState);
175 }
Peng Huangc6a76072018-11-27 23:17:31176 }
Peng Huange89c2a052018-12-04 11:35:11177 ~ScopedPixelUnpackState() = default;
Jonathan Backerc26060e2018-03-29 15:06:29178
179 private:
Jonathan Backerc26060e2018-03-29 15:06:29180 DISALLOW_COPY_AND_ASSIGN(ScopedPixelUnpackState);
181};
182
Peng Huangc6a76072018-11-27 23:17:31183// Commands that are whitelisted as OK to occur between BeginRasterCHROMIUM
184// and EndRasterCHROMIUM. They do not invalidate GrContext state tracking.
Jonathan Backer2231a002018-05-14 19:55:22185bool AllowedBetweenBeginEndRaster(CommandId command) {
186 switch (command) {
187 case kCreateTransferCacheEntryINTERNAL:
188 case kDeleteTransferCacheEntryINTERNAL:
189 case kEndRasterCHROMIUM:
190 case kFinish:
191 case kFlush:
192 case kGetError:
193 case kRasterCHROMIUM:
194 case kUnlockTransferCacheEntryINTERNAL:
195 return true;
196 default:
197 return false;
198 }
199}
200
Jonathan Backera07c4a9e2018-03-21 15:13:16201} // namespace
202
Jonathan Backer5ac08a72018-06-27 16:15:35203// RasterDecoderImpl uses two separate state trackers (gpu::gles2::ContextState
204// and GrContext) that cache the current GL driver state. Each class sees a
205// fraction of the GL calls issued and can easily become inconsistent with GL
206// state. We guard against that by resetting. But resetting is expensive, so we
Peng Huange89c2a052018-12-04 11:35:11207// avoid it as much as possible.
Jonathan Backer4f9ee5fb2018-04-25 14:03:38208class RasterDecoderImpl final : public RasterDecoder,
Khushala8d50642018-05-03 01:29:06209 public gles2::ErrorStateClient,
210 public ServiceFontManager::Client {
Jonathan Backer7471b2782018-01-25 18:14:19211 public:
Peng Huang20361dad12019-01-23 14:48:10212 RasterDecoderImpl(DecoderClient* client,
213 CommandBufferServiceBase* command_buffer_service,
214 gles2::Outputter* outputter,
Antoine Labour10dddca12019-02-19 19:09:26215 const GpuFeatureInfo& gpu_feature_info,
216 const GpuPreferences& gpu_preferences,
217 MemoryTracker* memory_tracker,
218 SharedImageManager* shared_image_manager,
Malay Keshava154dcb2020-01-18 00:17:55219 scoped_refptr<SharedContextState> shared_context_state,
220 bool is_privileged);
Jonathan Backer7471b2782018-01-25 18:14:19221 ~RasterDecoderImpl() override;
222
Daniel Bratellaabc8d68e2018-07-18 19:20:26223 gles2::GLES2Util* GetGLES2Util() override { return &util_; }
Jonathan Backera4568da12018-01-31 16:25:04224
Jonathan Backer7471b2782018-01-25 18:14:19225 // DecoderContext implementation.
226 base::WeakPtr<DecoderContext> AsWeakPtr() override;
Jonathan Backer0cd1c4322018-04-17 16:57:10227 ContextResult Initialize(
Jonathan Backer7471b2782018-01-25 18:14:19228 const scoped_refptr<gl::GLSurface>& surface,
229 const scoped_refptr<gl::GLContext>& context,
230 bool offscreen,
231 const gles2::DisallowedFeatures& disallowed_features,
232 const ContextCreationAttribs& attrib_helper) override;
Jonathan Backer7471b2782018-01-25 18:14:19233 void Destroy(bool have_context) override;
234 bool MakeCurrent() override;
235 gl::GLContext* GetGLContext() override;
liberato@chromium.org00c8a6c2018-06-05 02:22:41236 gl::GLSurface* GetGLSurface() override;
Daniel Bratellaabc8d68e2018-07-18 19:20:26237 const gles2::FeatureInfo* GetFeatureInfo() const override {
Antoine Labour10dddca12019-02-19 19:09:26238 return feature_info();
Jonathan Backer34008d882018-03-27 18:57:55239 }
Jonathan Backer7471b2782018-01-25 18:14:19240 Capabilities GetCapabilities() override;
Peng Huangc6a76072018-11-27 23:17:31241 const gles2::ContextState* GetContextState() override;
Peng Huang66a7a3762018-12-07 20:05:38242
243 // TODO(penghuang): Remove unused context state related methods.
Jonathan Backer34008d882018-03-27 18:57:55244 void RestoreGlobalState() const override;
245 void ClearAllAttributes() const override;
246 void RestoreAllAttributes() const override;
Jonathan Backer7471b2782018-01-25 18:14:19247 void RestoreState(const gles2::ContextState* prev_state) override;
248 void RestoreActiveTexture() const override;
249 void RestoreAllTextureUnitAndSamplerBindings(
250 const gles2::ContextState* prev_state) const override;
251 void RestoreActiveTextureUnitBinding(unsigned int target) const override;
252 void RestoreBufferBinding(unsigned int target) override;
253 void RestoreBufferBindings() const override;
254 void RestoreFramebufferBindings() const override;
255 void RestoreRenderbufferBindings() override;
256 void RestoreProgramBindings() const override;
Antoine Labour9ddf6ac2019-01-17 01:59:39257 void RestoreTextureState(unsigned service_id) override;
Jonathan Backer7471b2782018-01-25 18:14:19258 void RestoreTextureUnitBindings(unsigned unit) const override;
259 void RestoreVertexAttribArray(unsigned index) override;
260 void RestoreAllExternalTextureBindingsIfNeeded() override;
Jonathan Backer016bd97e2018-03-14 15:26:39261 QueryManager* GetQueryManager() override;
Peng Huang66a7a3762018-12-07 20:05:38262
Geoff Lang85287e12018-05-22 17:26:40263 void SetQueryCallback(unsigned int query_client_id,
264 base::OnceClosure callback) override;
Jonathan Backer7471b2782018-01-25 18:14:19265 gles2::GpuFenceManager* GetGpuFenceManager() override;
266 bool HasPendingQueries() const override;
267 void ProcessPendingQueries(bool did_finish) override;
268 bool HasMoreIdleWork() const override;
269 void PerformIdleWork() override;
270 bool HasPollingWork() const override;
271 void PerformPollingWork() override;
272 TextureBase* GetTextureBase(uint32_t client_id) override;
Julien Isorcefd6c15f2018-03-15 16:51:17273 void SetLevelInfo(uint32_t client_id,
274 int level,
275 unsigned internal_format,
276 unsigned width,
277 unsigned height,
278 unsigned depth,
279 unsigned format,
280 unsigned type,
281 const gfx::Rect& cleared_rect) override;
Jonathan Backer7471b2782018-01-25 18:14:19282 bool WasContextLost() const override;
283 bool WasContextLostByRobustnessExtension() const override;
284 void MarkContextLost(error::ContextLostReason reason) override;
285 bool CheckResetStatus() override;
286 void BeginDecoding() override;
287 void EndDecoding() override;
288 const char* GetCommandName(unsigned int command_id) const;
289 error::Error DoCommands(unsigned int num_commands,
290 const volatile void* buffer,
291 int num_entries,
292 int* entries_processed) override;
293 base::StringPiece GetLogPrefix() override;
294 void BindImage(uint32_t client_texture_id,
295 uint32_t texture_target,
296 gl::GLImage* image,
297 bool can_bind_to_sampler) override;
298 gles2::ContextGroup* GetContextGroup() override;
299 gles2::ErrorState* GetErrorState() override;
Daniel Bratellaabc8d68e2018-07-18 19:20:26300 std::unique_ptr<gles2::AbstractTexture> CreateAbstractTexture(
301 GLenum target,
302 GLenum internal_format,
303 GLsizei width,
304 GLsizei height,
305 GLsizei depth,
306 GLint border,
307 GLenum format,
308 GLenum type) override;
Jonathan Backerc26060e2018-03-29 15:06:29309 bool IsCompressedTextureFormat(unsigned format) override;
310 bool ClearLevel(gles2::Texture* texture,
311 unsigned target,
312 int level,
313 unsigned format,
314 unsigned type,
315 int xoffset,
316 int yoffset,
317 int width,
318 int height) override;
319 bool ClearCompressedTextureLevel(gles2::Texture* texture,
320 unsigned target,
321 int level,
322 unsigned format,
323 int width,
324 int height) override;
325 bool ClearLevel3D(gles2::Texture* texture,
326 unsigned target,
327 int level,
328 unsigned format,
329 unsigned type,
330 int width,
331 int height,
332 int depth) override {
333 NOTIMPLEMENTED();
334 return false;
335 }
Andres Calderon Jaramillob870ca62019-01-29 16:33:35336 int GetRasterDecoderId() const override;
Khushal996e9912018-07-13 08:31:00337 int DecoderIdForTest() override;
Jonathan Backer0cd1c4322018-04-17 16:57:10338 ServiceTransferCache* GetTransferCacheForTest() override;
Khushal15b6abb2018-06-28 00:16:25339 void SetUpForRasterCHROMIUMForTest() override;
Jonathan Backerc7b82972018-11-21 19:08:48340 void SetOOMErrorForTest() override;
Christopher Cameron386e18582019-01-11 20:17:34341 void DisableFlushWorkaroundForTest() override;
Jonathan Backer7471b2782018-01-25 18:14:19342
343 // ErrorClientState implementation.
344 void OnContextLostError() override;
345 void OnOutOfMemoryError() override;
346
Daniel Bratellaabc8d68e2018-07-18 19:20:26347 gles2::Logger* GetLogger() override;
Jonathan Backera4568da12018-01-31 16:25:04348
349 void SetIgnoreCachedStateForTest(bool ignore) override;
Daniel Bratellaabc8d68e2018-07-18 19:20:26350 gles2::ImageManager* GetImageManagerForTest() override;
Jonathan Backer0cd1c4322018-04-17 16:57:10351
Jonathan Backer10821a82018-04-04 23:56:03352 void SetCopyTextureResourceManagerForTest(
Daniel Bratellaabc8d68e2018-07-18 19:20:26353 gles2::CopyTextureCHROMIUMResourceManager* copy_texture_resource_manager)
Jonathan Backer10821a82018-04-04 23:56:03354 override;
Jonathan Backera4568da12018-01-31 16:25:04355
Khushala8d50642018-05-03 01:29:06356 // ServiceFontManager::Client implementation.
357 scoped_refptr<Buffer> GetShmBuffer(uint32_t shm_id) override;
Khushal39641b92019-06-03 21:32:54358 void ReportProgress() override;
Khushala8d50642018-05-03 01:29:06359
Jonathan Backer7471b2782018-01-25 18:14:19360 private:
Peng Huang66a7a3762018-12-07 20:05:38361 gles2::ContextState* state() const {
Antoine Labour10dddca12019-02-19 19:09:26362 if (use_passthrough_) {
Jonathan Backer628d73f82019-01-17 17:34:18363 NOTREACHED();
364 return nullptr;
365 }
Peng Huang20361dad12019-01-23 14:48:10366 return shared_context_state_->context_state();
Peng Huang66a7a3762018-12-07 20:05:38367 }
Jonathan Backer628d73f82019-01-17 17:34:18368 gl::GLApi* api() const { return api_; }
Peng Huang20361dad12019-01-23 14:48:10369 GrContext* gr_context() const { return shared_context_state_->gr_context(); }
Khushal996e9912018-07-13 08:31:00370 ServiceTransferCache* transfer_cache() {
Peng Huang20361dad12019-01-23 14:48:10371 return shared_context_state_->transfer_cache();
Khushal996e9912018-07-13 08:31:00372 }
Jonathan Backer7471b2782018-01-25 18:14:19373
Antoine Labour10dddca12019-02-19 19:09:26374 const gles2::FeatureInfo* feature_info() const {
375 return shared_context_state_->feature_info();
376 }
377
Daniel Bratellaabc8d68e2018-07-18 19:20:26378 const gles2::FeatureInfo::FeatureFlags& features() const {
Antoine Labour10dddca12019-02-19 19:09:26379 return feature_info()->feature_flags();
Jonathan Backer7471b2782018-01-25 18:14:19380 }
381
Jonathan Backer1994ff2e2018-03-22 21:24:33382 const GpuDriverBugWorkarounds& workarounds() const {
Antoine Labour10dddca12019-02-19 19:09:26383 return feature_info()->workarounds();
Jonathan Backer1994ff2e2018-03-22 21:24:33384 }
385
Christopher Cameronbeb5f272019-02-04 22:49:13386 void FlushToWorkAroundMacCrashes() {
387#if defined(OS_MACOSX)
Christopher Cameronf787d0b2019-05-15 23:52:18388 if (!shared_context_state_->GrContextIsGL())
389 return;
Christopher Cameronbeb5f272019-02-04 22:49:13390 // This function does aggressive flushes to work around crashes in the
391 // macOS OpenGL driver.
392 // https://crbug.com/906453
393 if (!flush_workaround_disabled_for_test_) {
394 if (gr_context())
395 gr_context()->flush();
396 api()->glFlushFn();
Khushald15c5912019-06-14 17:51:04397
398 // Flushes can be expensive, yield to allow interruption after each flush.
399 ExitCommandProcessingEarly();
Christopher Cameronbeb5f272019-02-04 22:49:13400 }
401#endif
402 }
403
Jonathan Backera4568da12018-01-31 16:25:04404 const gl::GLVersionInfo& gl_version_info() {
Antoine Labour10dddca12019-02-19 19:09:26405 return feature_info()->gl_version_info();
Jonathan Backera4568da12018-01-31 16:25:04406 }
407
Jonathan Backer7471b2782018-01-25 18:14:19408 // Set remaining commands to process to 0 to force DoCommands to return
409 // and allow context preemption and GPU watchdog checks in
410 // CommandExecutor().
Corentin Wallez0f412f02019-04-03 22:42:38411 void ExitCommandProcessingEarly() override;
Jonathan Backer7471b2782018-01-25 18:14:19412
413 template <bool DebugImpl>
414 error::Error DoCommandsImpl(unsigned int num_commands,
415 const volatile void* buffer,
416 int num_entries,
417 int* entries_processed);
418
Jonathan Backer016bd97e2018-03-14 15:26:39419 bool GenQueriesEXTHelper(GLsizei n, const GLuint* client_ids);
420 void DeleteQueriesEXTHelper(GLsizei n, const volatile GLuint* client_ids);
Jonathan Backera4568da12018-01-31 16:25:04421 void DoFinish();
422 void DoFlush();
423 void DoGetIntegerv(GLenum pname, GLint* params, GLsizei params_size);
Jonathan Backere26739c2018-05-15 13:27:07424 void DoTraceEndCHROMIUM();
Jonathan Backer10821a82018-04-04 23:56:03425 bool InitializeCopyTexImageBlitter();
426 bool InitializeCopyTextureCHROMIUM();
Antoine Labour9ddf6ac2019-01-17 01:59:39427 void DoCopySubTextureINTERNAL(GLint xoffset,
Antoine Labouraeb4bd70e2019-01-16 02:33:28428 GLint yoffset,
429 GLint x,
430 GLint y,
431 GLsizei width,
Antoine Labour9ddf6ac2019-01-17 01:59:39432 GLsizei height,
Nathan Zabriskief1095d7b2019-12-21 00:33:57433 GLboolean unpack_flip_y,
434 GLboolean unpack_premultiply_alpha,
Antoine Labour9ddf6ac2019-01-17 01:59:39435 const volatile GLbyte* mailboxes);
Peng Huanga8b0e422019-07-16 17:48:57436 void DoCopySubTextureINTERNALGLPassthrough(GLint xoffset,
437 GLint yoffset,
438 GLint x,
439 GLint y,
440 GLsizei width,
441 GLsizei height,
Nathan Zabriskief1095d7b2019-12-21 00:33:57442 GLboolean unpack_flip_y,
443 GLboolean unpack_premultiply_alpha,
Peng Huanga8b0e422019-07-16 17:48:57444 const Mailbox& source_mailbox,
445 const Mailbox& dest_mailbox);
446 void DoCopySubTextureINTERNALGL(GLint xoffset,
447 GLint yoffset,
448 GLint x,
449 GLint y,
450 GLsizei width,
451 GLsizei height,
Nathan Zabriskief1095d7b2019-12-21 00:33:57452 GLboolean unpack_flip_y,
453 GLboolean unpack_premultiply_alpha,
Peng Huanga8b0e422019-07-16 17:48:57454 const Mailbox& source_mailbox,
455 const Mailbox& dest_mailbox);
456 void DoCopySubTextureINTERNALSkia(GLint xoffset,
457 GLint yoffset,
458 GLint x,
459 GLint y,
460 GLsizei width,
461 GLsizei height,
Nathan Zabriskief1095d7b2019-12-21 00:33:57462 GLboolean unpack_flip_y,
463 GLboolean unpack_premultiply_alpha,
Peng Huanga8b0e422019-07-16 17:48:57464 const Mailbox& source_mailbox,
465 const Mailbox& dest_mailbox);
Jonathan Backer7471b2782018-01-25 18:14:19466 void DoLoseContextCHROMIUM(GLenum current, GLenum other) { NOTIMPLEMENTED(); }
Jonathan Backer7e7492522018-07-20 00:23:55467 void DoBeginRasterCHROMIUM(GLuint sk_color,
Jonathan Backer7471b2782018-01-25 18:14:19468 GLuint msaa_sample_count,
469 GLboolean can_use_lcd_text,
Jonathan Backer7e7492522018-07-20 00:23:55470 const volatile GLbyte* key);
Khushala8d50642018-05-03 01:29:06471 void DoRasterCHROMIUM(GLuint raster_shm_id,
472 GLuint raster_shm_offset,
Antoine Labour423fe592019-01-10 22:13:09473 GLuint raster_shm_size,
Khushala8d50642018-05-03 01:29:06474 GLuint font_shm_id,
475 GLuint font_shm_offset,
Antoine Labour423fe592019-01-10 22:13:09476 GLuint font_shm_size);
Jonathan Backere19973e82018-04-18 20:08:09477 void DoEndRasterCHROMIUM();
Jonathan Backer7471b2782018-01-25 18:14:19478 void DoCreateTransferCacheEntryINTERNAL(GLuint entry_type,
479 GLuint entry_id,
480 GLuint handle_shm_id,
481 GLuint handle_shm_offset,
482 GLuint data_shm_id,
483 GLuint data_shm_offset,
Jonathan Backer0cd1c4322018-04-17 16:57:10484 GLuint data_size);
485 void DoUnlockTransferCacheEntryINTERNAL(GLuint entry_type, GLuint entry_id);
486 void DoDeleteTransferCacheEntryINTERNAL(GLuint entry_type, GLuint entry_id);
Jonathan Backer10821a82018-04-04 23:56:03487 void RestoreStateForAttrib(GLuint attrib, bool restore_array_binding);
Khushal33205a72018-11-08 10:12:29488 void DeletePaintCacheTextBlobsINTERNALHelper(
489 GLsizei n,
490 const volatile GLuint* paint_cache_ids);
491 void DeletePaintCachePathsINTERNALHelper(
492 GLsizei n,
493 const volatile GLuint* paint_cache_ids);
494 void DoClearPaintCacheINTERNAL();
Jonathan Backer7471b2782018-01-25 18:14:19495
Jonathan Backera4568da12018-01-31 16:25:04496#if defined(NDEBUG)
497 void LogClientServiceMapping(const char* /* function_name */,
498 GLuint /* client_id */,
499 GLuint /* service_id */) {}
500 template <typename T>
501 void LogClientServiceForInfo(T* /* info */,
502 GLuint /* client_id */,
503 const char* /* function_name */) {}
504#else
505 void LogClientServiceMapping(const char* function_name,
506 GLuint client_id,
507 GLuint service_id) {
Antoine Labour10dddca12019-02-19 19:09:26508 if (gpu_preferences_.enable_gpu_service_logging_gpu) {
Jonathan Backera4568da12018-01-31 16:25:04509 VLOG(1) << "[" << logger_.GetLogPrefix() << "] " << function_name
510 << ": client_id = " << client_id
511 << ", service_id = " << service_id;
512 }
513 }
514 template <typename T>
515 void LogClientServiceForInfo(T* info,
516 GLuint client_id,
517 const char* function_name) {
518 if (info) {
519 LogClientServiceMapping(function_name, client_id, info->service_id());
520 }
521 }
522#endif
523
Jonathan Backer7471b2782018-01-25 18:14:19524// Generate a member function prototype for each command in an automated and
525// typesafe way.
526#define RASTER_CMD_OP(name) \
527 Error Handle##name(uint32_t immediate_data_size, const volatile void* data);
528
529 RASTER_COMMAND_LIST(RASTER_CMD_OP)
530#undef RASTER_CMD_OP
531
532 typedef error::Error (RasterDecoderImpl::*CmdHandler)(
533 uint32_t immediate_data_size,
534 const volatile void* data);
535
536 // A struct to hold info about each command.
537 struct CommandInfo {
538 CmdHandler cmd_handler;
539 uint8_t arg_flags; // How to handle the arguments for this command
540 uint8_t cmd_flags; // How to handle this command
541 uint16_t arg_count; // How many arguments are expected for this command.
542 };
543
544 // A table of CommandInfo for all the commands.
545 static const CommandInfo command_info[kNumCommands - kFirstRasterCommand];
546
Khushal996e9912018-07-13 08:31:00547 const int raster_decoder_id_;
548
Jonathan Backer7471b2782018-01-25 18:14:19549 // Number of commands remaining to be processed in DoCommands().
Jonathan Backer0cd1c4322018-04-17 16:57:10550 int commands_to_process_ = 0;
551
Antoine Labour10dddca12019-02-19 19:09:26552 bool supports_gpu_raster_ = false;
Jonathan Backer0cd1c4322018-04-17 16:57:10553 bool supports_oop_raster_ = false;
Antoine Labour10dddca12019-02-19 19:09:26554 bool use_passthrough_ = false;
Peng Huang050dee52018-09-05 15:54:08555 bool use_ddl_ = false;
Jonathan Backer7471b2782018-01-25 18:14:19556
Jonathan Backer4f9ee5fb2018-04-25 14:03:38557 bool reset_by_robustness_extension_ = false;
558
Jonathan Backer7471b2782018-01-25 18:14:19559 // The current decoder error communicates the decoder error through command
560 // processing functions that do not return the error value. Should be set
561 // only if not returning an error.
Jonathan Backer0cd1c4322018-04-17 16:57:10562 error::Error current_decoder_error_ = error::kNoError;
Jonathan Backer7471b2782018-01-25 18:14:19563
Antoine Labour10dddca12019-02-19 19:09:26564 GpuPreferences gpu_preferences_;
565
Jonathan Backer7471b2782018-01-25 18:14:19566 gles2::DebugMarkerManager debug_marker_manager_;
567 gles2::Logger logger_;
Peng Huangb4ed1852018-12-05 03:35:29568 std::unique_ptr<gles2::ErrorState> error_state_;
Peng Huang66a7a3762018-12-07 20:05:38569 bool context_lost_ = false;
Jonathan Backer7471b2782018-01-25 18:14:19570
Peng Huang20361dad12019-01-23 14:48:10571 scoped_refptr<SharedContextState> shared_context_state_;
Jonathan Backer9267bf1ec2018-03-06 19:56:50572 std::unique_ptr<Validators> validators_;
Jonathan Backer7471b2782018-01-25 18:14:19573
Antoine Labour10dddca12019-02-19 19:09:26574 SharedImageRepresentationFactory shared_image_representation_factory_;
Jonathan Backer016bd97e2018-03-14 15:26:39575 std::unique_ptr<QueryManager> query_manager_;
576
Daniel Bratellaabc8d68e2018-07-18 19:20:26577 gles2::GLES2Util util_;
Jonathan Backera4568da12018-01-31 16:25:04578
Antoine Labour10dddca12019-02-19 19:09:26579 // An optional behaviour to lose the context when OOM.
Jonathan Backer4f9ee5fb2018-04-25 14:03:38580 bool lose_context_when_out_of_memory_ = false;
581
Daniel Bratellaabc8d68e2018-07-18 19:20:26582 std::unique_ptr<gles2::CopyTexImageResourceManager> copy_tex_image_blit_;
583 std::unique_ptr<gles2::CopyTextureCHROMIUMResourceManager>
584 copy_texture_chromium_;
Jonathan Backer10821a82018-04-04 23:56:03585
Daniel Bratellaabc8d68e2018-07-18 19:20:26586 std::unique_ptr<gles2::GPUTracer> gpu_tracer_;
Jonathan Backere26739c2018-05-15 13:27:07587 const unsigned char* gpu_decoder_category_;
588 static constexpr int gpu_trace_level_ = 2;
589 bool gpu_trace_commands_ = false;
590 bool gpu_debug_commands_ = false;
591
Jonathan Backer0cd1c4322018-04-17 16:57:10592 // Raster helpers.
Khushal3d0b8902018-09-18 03:03:34593 scoped_refptr<ServiceFontManager> font_manager_;
Eric Karl3750cfa2018-10-25 16:54:17594 std::unique_ptr<SharedImageRepresentationSkia> shared_image_;
Eric Karl707c1b92019-12-10 06:50:08595 std::unique_ptr<SharedImageRepresentationSkia::ScopedWriteAccess>
Peng Huange9b41cd2019-08-12 19:39:47596 scoped_shared_image_write_;
597 SkSurface* sk_surface_ = nullptr;
598 sk_sp<SkSurface> sk_surface_for_testing_;
Peng Huang5f9118b02019-04-24 20:18:21599 std::vector<GrBackendSemaphore> end_semaphores_;
Khushal33205a72018-11-08 10:12:29600 std::unique_ptr<cc::ServicePaintCache> paint_cache_;
Peng Huang050dee52018-09-05 15:54:08601
602 std::unique_ptr<SkDeferredDisplayListRecorder> recorder_;
Mike Reed421f75d2019-02-25 20:30:03603 SkCanvas* raster_canvas_ = nullptr; // ptr into recorder_ or sk_surface_
Khushal22204a42018-05-17 23:06:21604 std::vector<SkDiscardableHandleId> locked_handles_;
Jonathan Backer4fb9f5acb2018-05-10 22:19:33605
Khushalf9750702018-06-09 00:42:13606 // Tracing helpers.
607 int raster_chromium_id_ = 0;
608
Christopher Cameron386e18582019-01-11 20:17:34609 // Workaround for https://crbug.com/906453
610 bool flush_workaround_disabled_for_test_ = false;
611
Antoine Labour9ddf6ac2019-01-17 01:59:39612 bool in_copy_sub_texture_ = false;
613 bool reset_texture_state_ = false;
614
Malay Keshava154dcb2020-01-18 00:17:55615 bool is_privileged_ = false;
616
Jonathan Backer628d73f82019-01-17 17:34:18617 gl::GLApi* api_ = nullptr;
618
Jeremy Roman28926e92019-07-15 15:42:16619 base::WeakPtrFactory<DecoderContext> weak_ptr_factory_{this};
Jonathan Backer7471b2782018-01-25 18:14:19620
621 DISALLOW_COPY_AND_ASSIGN(RasterDecoderImpl);
Jonathan Backer7f90dfb662017-12-18 16:52:04622};
623
Jonathan Backer7471b2782018-01-25 18:14:19624constexpr RasterDecoderImpl::CommandInfo RasterDecoderImpl::command_info[] = {
Peng Huangc6a76072018-11-27 23:17:31625#define RASTER_CMD_OP(name) \
626 { \
627 &RasterDecoderImpl::Handle##name, \
628 cmds::name::kArgFlags, \
629 cmds::name::cmd_flags, \
630 sizeof(cmds::name) / sizeof(CommandBufferEntry) - 1, \
Jonathan Backer7471b2782018-01-25 18:14:19631 }, /* NOLINT */
632 RASTER_COMMAND_LIST(RASTER_CMD_OP)
633#undef RASTER_CMD_OP
634};
635
636// static
637RasterDecoder* RasterDecoder::Create(
638 DecoderClient* client,
639 CommandBufferServiceBase* command_buffer_service,
Daniel Bratellaabc8d68e2018-07-18 19:20:26640 gles2::Outputter* outputter,
Antoine Labour10dddca12019-02-19 19:09:26641 const GpuFeatureInfo& gpu_feature_info,
642 const GpuPreferences& gpu_preferences,
643 MemoryTracker* memory_tracker,
644 SharedImageManager* shared_image_manager,
Malay Keshava154dcb2020-01-18 00:17:55645 scoped_refptr<SharedContextState> shared_context_state,
646 bool is_privileged) {
Antoine Labour10dddca12019-02-19 19:09:26647 return new RasterDecoderImpl(client, command_buffer_service, outputter,
648 gpu_feature_info, gpu_preferences,
649 memory_tracker, shared_image_manager,
Malay Keshava154dcb2020-01-18 00:17:55650 std::move(shared_context_state), is_privileged);
Jonathan Backer7471b2782018-01-25 18:14:19651}
652
Corentin Wallez0f412f02019-04-03 22:42:38653RasterDecoder::RasterDecoder(DecoderClient* client,
654 CommandBufferServiceBase* command_buffer_service,
Jonathan Backere26739c2018-05-15 13:27:07655 gles2::Outputter* outputter)
Corentin Wallez0f412f02019-04-03 22:42:38656 : CommonDecoder(client, command_buffer_service), outputter_(outputter) {}
Jonathan Backer7471b2782018-01-25 18:14:19657
658RasterDecoder::~RasterDecoder() {}
659
Jonathan Backera4568da12018-01-31 16:25:04660bool RasterDecoder::initialized() const {
661 return initialized_;
662}
663
664TextureBase* RasterDecoder::GetTextureBase(uint32_t client_id) {
665 return nullptr;
666}
667
Julien Isorcefd6c15f2018-03-15 16:51:17668void RasterDecoder::SetLevelInfo(uint32_t client_id,
669 int level,
670 unsigned internal_format,
671 unsigned width,
672 unsigned height,
673 unsigned depth,
674 unsigned format,
675 unsigned type,
676 const gfx::Rect& cleared_rect) {}
677
Jonathan Backera4568da12018-01-31 16:25:04678void RasterDecoder::BeginDecoding() {}
679
680void RasterDecoder::EndDecoding() {}
681
Jonathan Backer0cd1c4322018-04-17 16:57:10682void RasterDecoder::SetLogCommands(bool log_commands) {
683 log_commands_ = log_commands;
684}
685
Jonathan Backere26739c2018-05-15 13:27:07686gles2::Outputter* RasterDecoder::outputter() const {
687 return outputter_;
688}
689
Jonathan Backera4568da12018-01-31 16:25:04690base::StringPiece RasterDecoder::GetLogPrefix() {
691 return GetLogger()->GetLogPrefix();
692}
693
Jonathan Backer7471b2782018-01-25 18:14:19694RasterDecoderImpl::RasterDecoderImpl(
695 DecoderClient* client,
696 CommandBufferServiceBase* command_buffer_service,
Daniel Bratellaabc8d68e2018-07-18 19:20:26697 gles2::Outputter* outputter,
Antoine Labour10dddca12019-02-19 19:09:26698 const GpuFeatureInfo& gpu_feature_info,
699 const GpuPreferences& gpu_preferences,
700 MemoryTracker* memory_tracker,
701 SharedImageManager* shared_image_manager,
Malay Keshava154dcb2020-01-18 00:17:55702 scoped_refptr<SharedContextState> shared_context_state,
703 bool is_privileged)
Corentin Wallez0f412f02019-04-03 22:42:38704 : RasterDecoder(client, command_buffer_service, outputter),
Khushal996e9912018-07-13 08:31:00705 raster_decoder_id_(g_raster_decoder_id.GetNext() + 1),
Antoine Labour10dddca12019-02-19 19:09:26706 supports_gpu_raster_(
707 gpu_feature_info.status_values[GPU_FEATURE_TYPE_GPU_RASTERIZATION] ==
708 kGpuFeatureStatusEnabled),
709 use_passthrough_(gles2::PassthroughCommandDecoderSupported() &&
710 gpu_preferences.use_passthrough_cmd_decoder),
Antoine Labour10dddca12019-02-19 19:09:26711 gpu_preferences_(gpu_preferences),
Peng Huang61eccfe42018-11-15 22:10:13712 logger_(&debug_marker_manager_,
713 base::BindRepeating(&DecoderClient::OnConsoleMessage,
Corentin Wallez0f412f02019-04-03 22:42:38714 base::Unretained(client),
James Darpinian104af6f2018-12-06 03:24:55715 0),
Antoine Labour10dddca12019-02-19 19:09:26716 gpu_preferences_.disable_gl_error_limit),
Peng Huangb4ed1852018-12-05 03:35:29717 error_state_(gles2::ErrorState::Create(this, &logger_)),
Peng Huang20361dad12019-01-23 14:48:10718 shared_context_state_(std::move(shared_context_state)),
Jonathan Backer9267bf1ec2018-03-06 19:56:50719 validators_(new Validators),
Antoine Labour10dddca12019-02-19 19:09:26720 shared_image_representation_factory_(shared_image_manager,
721 memory_tracker),
Jonathan Backere26739c2018-05-15 13:27:07722 gpu_decoder_category_(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
Miguel Casas510fbfcb2018-11-13 23:40:57723 TRACE_DISABLED_BY_DEFAULT("gpu.decoder"))),
Malay Keshava154dcb2020-01-18 00:17:55724 font_manager_(base::MakeRefCounted<ServiceFontManager>(this)),
725 is_privileged_(is_privileged) {
Peng Huang20361dad12019-01-23 14:48:10726 DCHECK(shared_context_state_);
Khushalb2c140b2018-07-09 20:21:16727}
Jonathan Backer7f90dfb662017-12-18 16:52:04728
Andres Calderon Jaramillo56d9f1ea2019-05-24 19:57:34729RasterDecoderImpl::~RasterDecoderImpl() = default;
Jonathan Backer7f90dfb662017-12-18 16:52:04730
Jonathan Backer7471b2782018-01-25 18:14:19731base::WeakPtr<DecoderContext> RasterDecoderImpl::AsWeakPtr() {
Jonathan Backer7f90dfb662017-12-18 16:52:04732 return weak_ptr_factory_.GetWeakPtr();
733}
734
Jonathan Backer0cd1c4322018-04-17 16:57:10735ContextResult RasterDecoderImpl::Initialize(
Jonathan Backer7f90dfb662017-12-18 16:52:04736 const scoped_refptr<gl::GLSurface>& surface,
737 const scoped_refptr<gl::GLContext>& context,
738 bool offscreen,
Daniel Bratellaabc8d68e2018-07-18 19:20:26739 const gles2::DisallowedFeatures& disallowed_features,
Antoine Labourfeab2392017-12-21 20:28:39740 const ContextCreationAttribs& attrib_helper) {
Jonathan Backer7471b2782018-01-25 18:14:19741 TRACE_EVENT0("gpu", "RasterDecoderImpl::Initialize");
Jonathan Backer192586792019-12-18 16:36:33742 DCHECK(shared_context_state_->IsCurrent(nullptr));
Jonathan Backer7f90dfb662017-12-18 16:52:04743
Jonathan Backer628d73f82019-01-17 17:34:18744 api_ = gl::g_current_gl_context;
745
Jonathan Backera4568da12018-01-31 16:25:04746 set_initialized();
Jonathan Backer7f90dfb662017-12-18 16:52:04747
Jonathan Backer7f90dfb662017-12-18 16:52:04748 if (!offscreen) {
Jonathan Backer0cd1c4322018-04-17 16:57:10749 return ContextResult::kFatalFailure;
Jonathan Backer7f90dfb662017-12-18 16:52:04750 }
751
Antoine Labour10dddca12019-02-19 19:09:26752 if (gpu_preferences_.enable_gpu_debugging)
Jonathan Backer7471b2782018-01-25 18:14:19753 set_debug(true);
Jonathan Backer7f90dfb662017-12-18 16:52:04754
Antoine Labour10dddca12019-02-19 19:09:26755 if (gpu_preferences_.enable_gpu_command_logging)
Jonathan Backer0cd1c4322018-04-17 16:57:10756 SetLogCommands(true);
Jonathan Backer7f90dfb662017-12-18 16:52:04757
Peng Huang20361dad12019-01-23 14:48:10758 DCHECK_EQ(surface.get(), shared_context_state_->surface());
759 DCHECK_EQ(context.get(), shared_context_state_->context());
Jonathan Backer7f90dfb662017-12-18 16:52:04760
Jonathan Backere26739c2018-05-15 13:27:07761 // Create GPU Tracer for timing values.
Daniel Bratellaabc8d68e2018-07-18 19:20:26762 gpu_tracer_.reset(new gles2::GPUTracer(this));
Jonathan Backere26739c2018-05-15 13:27:07763
Jonathan Backer4f9ee5fb2018-04-25 14:03:38764 // Save the loseContextWhenOutOfMemory context creation attribute.
765 lose_context_when_out_of_memory_ =
766 attrib_helper.lose_context_when_out_of_memory;
767
Jonathan Backer7f90dfb662017-12-18 16:52:04768 CHECK_GL_ERROR();
769
Peng Huang66a7a3762018-12-07 20:05:38770 query_manager_ = std::make_unique<QueryManager>();
Jonathan Backer016bd97e2018-03-14 15:26:39771
Jonathan Backer0cd1c4322018-04-17 16:57:10772 if (attrib_helper.enable_oop_rasterization) {
773 if (!features().chromium_raster_transport) {
774 LOG(ERROR) << "ContextResult::kFatalFailure: "
775 "chromium_raster_transport not present";
Jonathan Backer15dbca32018-06-04 20:39:33776 Destroy(true);
Jonathan Backer0cd1c4322018-04-17 16:57:10777 return ContextResult::kFatalFailure;
778 }
779
Peng Huang20361dad12019-01-23 14:48:10780 supports_oop_raster_ = !!shared_context_state_->gr_context();
Khushal33205a72018-11-08 10:12:29781 if (supports_oop_raster_)
782 paint_cache_ = std::make_unique<cc::ServicePaintCache>();
Antoine Labour10dddca12019-02-19 19:09:26783 use_ddl_ = gpu_preferences_.enable_oop_rasterization_ddl;
Khushal996e9912018-07-13 08:31:00784 }
Jonathan Backer0cd1c4322018-04-17 16:57:10785
786 return ContextResult::kSuccess;
Jonathan Backer1d807a42018-01-08 20:45:54787}
Jonathan Backer7f90dfb662017-12-18 16:52:04788
Jonathan Backera4568da12018-01-31 16:25:04789void RasterDecoderImpl::Destroy(bool have_context) {
Jonathan Backer10821a82018-04-04 23:56:03790 if (!initialized())
791 return;
792
Peng Huang20361dad12019-01-23 14:48:10793 DCHECK(!have_context || shared_context_state_->context()->IsCurrent(nullptr));
Jonathan Backer10821a82018-04-04 23:56:03794
795 if (have_context) {
Andres Calderon Jaramillo56d9f1ea2019-05-24 19:57:34796 if (supports_oop_raster_) {
797 transfer_cache()->DeleteAllEntriesForDecoder(raster_decoder_id_);
798 }
799
Jonathan Backer10821a82018-04-04 23:56:03800 if (copy_tex_image_blit_.get()) {
801 copy_tex_image_blit_->Destroy();
Jonathan Backer36c8a522018-06-27 20:45:25802 copy_tex_image_blit_.reset();
Jonathan Backer10821a82018-04-04 23:56:03803 }
804
805 if (copy_texture_chromium_.get()) {
806 copy_texture_chromium_->Destroy();
Jonathan Backer36c8a522018-06-27 20:45:25807 copy_texture_chromium_.reset();
Jonathan Backer10821a82018-04-04 23:56:03808 }
Khushalb2c140b2018-07-09 20:21:16809
810 // Make sure we flush any pending skia work on this context.
811 if (sk_surface_) {
Peng Huang5f9118b02019-04-24 20:18:21812 GrFlushInfo flush_info = {
813 .fFlags = kNone_GrFlushFlags,
814 .fNumSemaphores = end_semaphores_.size(),
815 .fSignalSemaphores = end_semaphores_.data(),
816 };
Sergey Ulanovc528c0d2019-05-08 21:06:06817 AddVulkanCleanupTaskForSkiaFlush(
Eric Karl32d9d2e2019-05-03 17:09:47818 shared_context_state_->vk_context_provider(), &flush_info);
Peng Huang5f9118b02019-04-24 20:18:21819 auto result = sk_surface_->flush(
820 SkSurface::BackendSurfaceAccess::kPresent, flush_info);
821 DCHECK(result == GrSemaphoresSubmitted::kYes || end_semaphores_.empty());
822 end_semaphores_.clear();
Peng Huange9b41cd2019-08-12 19:39:47823 sk_surface_ = nullptr;
Peng Huang5f9118b02019-04-24 20:18:21824 if (shared_image_) {
Peng Huange9b41cd2019-08-12 19:39:47825 scoped_shared_image_write_.reset();
Peng Huang5f9118b02019-04-24 20:18:21826 shared_image_.reset();
827 } else {
Peng Huange9b41cd2019-08-12 19:39:47828 sk_surface_for_testing_.reset();
Peng Huang5f9118b02019-04-24 20:18:21829 }
Khushalb2c140b2018-07-09 20:21:16830 }
831 if (gr_context()) {
832 gr_context()->flush();
833 }
Jonathan Backer10821a82018-04-04 23:56:03834 }
835
Jonathan Backer10821a82018-04-04 23:56:03836 copy_tex_image_blit_.reset();
837 copy_texture_chromium_.reset();
838
Jonathan Backer016bd97e2018-03-14 15:26:39839 if (query_manager_.get()) {
840 query_manager_->Destroy(have_context);
841 query_manager_.reset();
842 }
843
Khushal3d0b8902018-09-18 03:03:34844 font_manager_->Destroy();
845 font_manager_.reset();
Jonathan Backera4568da12018-01-31 16:25:04846}
Jonathan Backer7f90dfb662017-12-18 16:52:04847
Jonathan Backer7f90dfb662017-12-18 16:52:04848// Make this decoder's GL context current.
Jonathan Backer7471b2782018-01-25 18:14:19849bool RasterDecoderImpl::MakeCurrent() {
Christopher Camerondd7812d2019-04-23 03:12:21850 if (!shared_context_state_->GrContextIsGL())
Jonathan Backer7621ec92018-12-11 00:02:44851 return true;
852
Peng Huang66a7a3762018-12-07 20:05:38853 if (context_lost_) {
Jonathan Backer7471b2782018-01-25 18:14:19854 LOG(ERROR) << " RasterDecoderImpl: Trying to make lost context current.";
Jonathan Backer7f90dfb662017-12-18 16:52:04855 return false;
856 }
857
Peng Huang20361dad12019-01-23 14:48:10858 if (shared_context_state_->context_lost() ||
859 !shared_context_state_->MakeCurrent(nullptr)) {
Jonathan Backer7471b2782018-01-25 18:14:19860 LOG(ERROR) << " RasterDecoderImpl: Context lost during MakeCurrent.";
Jonathan Backer7f90dfb662017-12-18 16:52:04861 MarkContextLost(error::kMakeCurrentFailed);
Jonathan Backer7f90dfb662017-12-18 16:52:04862 return false;
863 }
Peng Huang66a7a3762018-12-07 20:05:38864
Jonathan Backer34008d882018-03-27 18:57:55865 DCHECK_EQ(api(), gl::g_current_gl_context);
866
Jonathan Backer4f9ee5fb2018-04-25 14:03:38867 if (CheckResetStatus()) {
868 LOG(ERROR)
869 << " RasterDecoderImpl: Context reset detected after MakeCurrent.";
Jonathan Backer4f9ee5fb2018-04-25 14:03:38870 return false;
871 }
872
Jonathan Backer34008d882018-03-27 18:57:55873 // Rebind textures if the service ids may have changed.
874 RestoreAllExternalTextureBindingsIfNeeded();
875
Jonathan Backer7f90dfb662017-12-18 16:52:04876 return true;
877}
878
Jonathan Backer7471b2782018-01-25 18:14:19879gl::GLContext* RasterDecoderImpl::GetGLContext() {
Jonathan Backer192586792019-12-18 16:36:33880 return shared_context_state_->context();
Jonathan Backer7f90dfb662017-12-18 16:52:04881}
882
liberato@chromium.org00c8a6c2018-06-05 02:22:41883gl::GLSurface* RasterDecoderImpl::GetGLSurface() {
Peng Huang20361dad12019-01-23 14:48:10884 return shared_context_state_->surface();
liberato@chromium.org00c8a6c2018-06-05 02:22:41885}
886
Jonathan Backer7471b2782018-01-25 18:14:19887Capabilities RasterDecoderImpl::GetCapabilities() {
Adrienne Walker693f34d2019-07-29 22:07:04888 // TODO(enne): reconcile this with gles2_cmd_decoder's capability settings.
Jonathan Backer0cd1c4322018-04-17 16:57:10889 Capabilities caps;
Antoine Labour10dddca12019-02-19 19:09:26890 caps.gpu_rasterization = supports_gpu_raster_;
Jonathan Backereba6af42018-05-03 22:47:43891 caps.supports_oop_raster = supports_oop_raster_;
Antoine Labour0f9bee72018-11-06 07:35:43892 caps.gpu_memory_buffer_formats =
Antoine Labour10dddca12019-02-19 19:09:26893 feature_info()->feature_flags().gpu_memory_buffer_formats;
Victor Miuracabdfac2018-01-05 02:18:18894 caps.texture_target_exception_list =
Antoine Labour10dddca12019-02-19 19:09:26895 gpu_preferences_.texture_target_exception_list;
Jonathan Backera07c4a9e2018-03-21 15:13:16896 caps.texture_format_bgra8888 =
Antoine Labour10dddca12019-02-19 19:09:26897 feature_info()->feature_flags().ext_texture_format_bgra8888;
Jonathan Backer1994ff2e2018-03-22 21:24:33898 caps.texture_storage_image =
Antoine Labour10dddca12019-02-19 19:09:26899 feature_info()->feature_flags().chromium_texture_storage_image;
900 caps.texture_storage = feature_info()->feature_flags().ext_texture_storage;
Peng Huang3c13b502019-05-08 01:40:00901 // TODO(piman): have a consistent limit in shared image backings.
902 // https://crbug.com/960588
903 if (shared_context_state_->GrContextIsGL()) {
904 api()->glGetIntegervFn(GL_MAX_TEXTURE_SIZE, &caps.max_texture_size);
905 } else if (shared_context_state_->GrContextIsVulkan()) {
906#if BUILDFLAG(ENABLE_VULKAN)
907 caps.max_texture_size = shared_context_state_->vk_context_provider()
908 ->GetDeviceQueue()
909 ->vk_physical_device_properties()
910 .limits.maxImageDimension2D;
911#else
912 NOTREACHED();
913#endif
914 } else {
915 NOTIMPLEMENTED();
916 }
Antoine Labour10dddca12019-02-19 19:09:26917 if (feature_info()->workarounds().max_texture_size) {
918 caps.max_texture_size = std::min(
919 caps.max_texture_size, feature_info()->workarounds().max_texture_size);
James Darpinian3c422032019-04-06 00:20:16920 caps.max_cube_map_texture_size =
921 std::min(caps.max_cube_map_texture_size,
922 feature_info()->workarounds().max_texture_size);
923 }
924 if (feature_info()->workarounds().max_3d_array_texture_size) {
925 caps.max_3d_texture_size =
926 std::min(caps.max_3d_texture_size,
927 feature_info()->workarounds().max_3d_array_texture_size);
928 caps.max_array_texture_layers =
929 std::min(caps.max_array_texture_layers,
930 feature_info()->workarounds().max_3d_array_texture_size);
Antoine Labour10dddca12019-02-19 19:09:26931 }
932 caps.sync_query = feature_info()->feature_flags().chromium_sync_query;
Adrienne Walker693f34d2019-07-29 22:07:04933 caps.msaa_is_slow = feature_info()->workarounds().msaa_is_slow;
934 caps.avoid_stencil_buffers =
935 feature_info()->workarounds().avoid_stencil_buffers;
Khushal315f2fd2018-05-29 03:20:39936
Khushalb2c140b2018-07-09 20:21:16937 if (gr_context()) {
Khushal315f2fd2018-05-29 03:20:39938 caps.context_supports_distance_field_text =
Khushalb2c140b2018-07-09 20:21:16939 gr_context()->supportsDistanceFieldText();
Khushalf9750702018-06-09 00:42:13940 }
Jonathan Backer7f90dfb662017-12-18 16:52:04941 return caps;
942}
943
Peng Huangc6a76072018-11-27 23:17:31944const gles2::ContextState* RasterDecoderImpl::GetContextState() {
Peng Huang66a7a3762018-12-07 20:05:38945 NOTREACHED();
946 return nullptr;
Peng Huangc6a76072018-11-27 23:17:31947}
948
Jonathan Backer34008d882018-03-27 18:57:55949void RasterDecoderImpl::RestoreGlobalState() const {
Peng Huangd7aaf782019-01-09 22:03:08950 // We mark the context state is dirty instead of restoring global
951 // state, and the global state will be restored by the next context.
Peng Huang20361dad12019-01-23 14:48:10952 shared_context_state_->set_need_context_state_reset(true);
953 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer34008d882018-03-27 18:57:55954}
955
Peng Huangc6a76072018-11-27 23:17:31956void RasterDecoderImpl::ClearAllAttributes() const {}
Jonathan Backer34008d882018-03-27 18:57:55957
958void RasterDecoderImpl::RestoreAllAttributes() const {
Peng Huang20361dad12019-01-23 14:48:10959 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer34008d882018-03-27 18:57:55960}
961
Daniel Bratellaabc8d68e2018-07-18 19:20:26962void RasterDecoderImpl::RestoreState(const gles2::ContextState* prev_state) {
Peng Huang20361dad12019-01-23 14:48:10963 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer7f90dfb662017-12-18 16:52:04964}
965
Jonathan Backer7471b2782018-01-25 18:14:19966void RasterDecoderImpl::RestoreActiveTexture() const {
Peng Huang20361dad12019-01-23 14:48:10967 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer7f90dfb662017-12-18 16:52:04968}
969
Jonathan Backer7471b2782018-01-25 18:14:19970void RasterDecoderImpl::RestoreAllTextureUnitAndSamplerBindings(
Daniel Bratellaabc8d68e2018-07-18 19:20:26971 const gles2::ContextState* prev_state) const {
Peng Huang20361dad12019-01-23 14:48:10972 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer7f90dfb662017-12-18 16:52:04973}
974
Jonathan Backer7471b2782018-01-25 18:14:19975void RasterDecoderImpl::RestoreActiveTextureUnitBinding(
976 unsigned int target) const {
Peng Huang20361dad12019-01-23 14:48:10977 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer7f90dfb662017-12-18 16:52:04978}
979
Jonathan Backer7471b2782018-01-25 18:14:19980void RasterDecoderImpl::RestoreBufferBinding(unsigned int target) {
Peng Huang20361dad12019-01-23 14:48:10981 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer7f90dfb662017-12-18 16:52:04982}
983
Jonathan Backer7471b2782018-01-25 18:14:19984void RasterDecoderImpl::RestoreBufferBindings() const {
Peng Huang20361dad12019-01-23 14:48:10985 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer7f90dfb662017-12-18 16:52:04986}
987
Jonathan Backer7471b2782018-01-25 18:14:19988void RasterDecoderImpl::RestoreFramebufferBindings() const {
Peng Huang20361dad12019-01-23 14:48:10989 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer7f90dfb662017-12-18 16:52:04990}
991
Jonathan Backer7471b2782018-01-25 18:14:19992void RasterDecoderImpl::RestoreRenderbufferBindings() {
Peng Huang20361dad12019-01-23 14:48:10993 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer7f90dfb662017-12-18 16:52:04994}
995
Jonathan Backer7471b2782018-01-25 18:14:19996void RasterDecoderImpl::RestoreProgramBindings() const {
Peng Huang20361dad12019-01-23 14:48:10997 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer7f90dfb662017-12-18 16:52:04998}
999
Antoine Labour9ddf6ac2019-01-17 01:59:391000void RasterDecoderImpl::RestoreTextureState(unsigned service_id) {
1001 DCHECK(in_copy_sub_texture_);
1002 reset_texture_state_ = true;
Jonathan Backer7f90dfb662017-12-18 16:52:041003}
1004
Jonathan Backer7471b2782018-01-25 18:14:191005void RasterDecoderImpl::RestoreTextureUnitBindings(unsigned unit) const {
Peng Huang20361dad12019-01-23 14:48:101006 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer7f90dfb662017-12-18 16:52:041007}
1008
Jonathan Backer7471b2782018-01-25 18:14:191009void RasterDecoderImpl::RestoreVertexAttribArray(unsigned index) {
Peng Huang20361dad12019-01-23 14:48:101010 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer7f90dfb662017-12-18 16:52:041011}
1012
Jonathan Backer7471b2782018-01-25 18:14:191013void RasterDecoderImpl::RestoreAllExternalTextureBindingsIfNeeded() {
Peng Huang20361dad12019-01-23 14:48:101014 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer7f90dfb662017-12-18 16:52:041015}
1016
Jonathan Backer7471b2782018-01-25 18:14:191017QueryManager* RasterDecoderImpl::GetQueryManager() {
Jonathan Backer016bd97e2018-03-14 15:26:391018 return query_manager_.get();
Jonathan Backer7f90dfb662017-12-18 16:52:041019}
1020
Geoff Lang85287e12018-05-22 17:26:401021void RasterDecoderImpl::SetQueryCallback(unsigned int query_client_id,
1022 base::OnceClosure callback) {
1023 QueryManager::Query* query = query_manager_->GetQuery(query_client_id);
1024 if (query) {
1025 query->AddCallback(std::move(callback));
1026 } else {
1027 VLOG(1) << "RasterDecoderImpl::SetQueryCallback: No query with ID "
1028 << query_client_id << ". Running the callback immediately.";
1029 std::move(callback).Run();
1030 }
1031}
1032
Daniel Bratellaabc8d68e2018-07-18 19:20:261033gles2::GpuFenceManager* RasterDecoderImpl::GetGpuFenceManager() {
Jonathan Backer7f90dfb662017-12-18 16:52:041034 NOTIMPLEMENTED();
1035 return nullptr;
1036}
1037
Jonathan Backer7471b2782018-01-25 18:14:191038bool RasterDecoderImpl::HasPendingQueries() const {
Peng Huang66a7a3762018-12-07 20:05:381039 return query_manager_ && query_manager_->HavePendingQueries();
Jonathan Backer7f90dfb662017-12-18 16:52:041040}
1041
Jonathan Backer7471b2782018-01-25 18:14:191042void RasterDecoderImpl::ProcessPendingQueries(bool did_finish) {
Peng Huang66a7a3762018-12-07 20:05:381043 if (query_manager_)
1044 query_manager_->ProcessPendingQueries(did_finish);
Jonathan Backer7f90dfb662017-12-18 16:52:041045}
1046
Jonathan Backer7471b2782018-01-25 18:14:191047bool RasterDecoderImpl::HasMoreIdleWork() const {
Jonathan Backere26739c2018-05-15 13:27:071048 return gpu_tracer_->HasTracesToProcess();
Jonathan Backer7f90dfb662017-12-18 16:52:041049}
1050
Jonathan Backer7471b2782018-01-25 18:14:191051void RasterDecoderImpl::PerformIdleWork() {
Jonathan Backere26739c2018-05-15 13:27:071052 gpu_tracer_->ProcessTraces();
Jonathan Backer7f90dfb662017-12-18 16:52:041053}
1054
Jonathan Backer7471b2782018-01-25 18:14:191055bool RasterDecoderImpl::HasPollingWork() const {
Jonathan Backer7f90dfb662017-12-18 16:52:041056 return false;
1057}
1058
Jonathan Backerc2b0efa2018-03-21 16:58:351059void RasterDecoderImpl::PerformPollingWork() {}
Jonathan Backer7f90dfb662017-12-18 16:52:041060
Jonathan Backer7471b2782018-01-25 18:14:191061TextureBase* RasterDecoderImpl::GetTextureBase(uint32_t client_id) {
Jonathan Backer7f90dfb662017-12-18 16:52:041062 NOTIMPLEMENTED();
1063 return nullptr;
1064}
1065
Julien Isorcefd6c15f2018-03-15 16:51:171066void RasterDecoderImpl::SetLevelInfo(uint32_t client_id,
1067 int level,
1068 unsigned internal_format,
1069 unsigned width,
1070 unsigned height,
1071 unsigned depth,
1072 unsigned format,
1073 unsigned type,
1074 const gfx::Rect& cleared_rect) {
1075 NOTIMPLEMENTED();
1076}
1077
Jonathan Backer7471b2782018-01-25 18:14:191078bool RasterDecoderImpl::WasContextLost() const {
Peng Huang66a7a3762018-12-07 20:05:381079 return context_lost_;
Jonathan Backer7f90dfb662017-12-18 16:52:041080}
1081
Jonathan Backer7471b2782018-01-25 18:14:191082bool RasterDecoderImpl::WasContextLostByRobustnessExtension() const {
Jonathan Backer4f9ee5fb2018-04-25 14:03:381083 return WasContextLost() && reset_by_robustness_extension_;
Jonathan Backer7f90dfb662017-12-18 16:52:041084}
1085
Jonathan Backer7471b2782018-01-25 18:14:191086void RasterDecoderImpl::MarkContextLost(error::ContextLostReason reason) {
Jonathan Backer4f9ee5fb2018-04-25 14:03:381087 // Only lose the context once.
1088 if (WasContextLost())
1089 return;
1090
1091 // Don't make GL calls in here, the context might not be current.
Peng Huang66a7a3762018-12-07 20:05:381092 context_lost_ = true;
Jonathan Backer4f9ee5fb2018-04-25 14:03:381093 command_buffer_service()->SetContextLostReason(reason);
1094 current_decoder_error_ = error::kLostContext;
Jonathan Backer7f90dfb662017-12-18 16:52:041095}
1096
Jonathan Backer7471b2782018-01-25 18:14:191097bool RasterDecoderImpl::CheckResetStatus() {
Jonathan Backer4f9ee5fb2018-04-25 14:03:381098 DCHECK(!WasContextLost());
Peng Huang20361dad12019-01-23 14:48:101099 DCHECK(shared_context_state_->context()->IsCurrent(nullptr));
Jonathan Backer4f9ee5fb2018-04-25 14:03:381100
Antoine Labourdce1ba12019-06-22 02:05:591101 // If the reason for the call was a GL error, we can try to determine the
1102 // reset status more accurately.
1103 GLenum driver_status =
1104 shared_context_state_->context()->CheckStickyGraphicsResetStatus();
1105 if (driver_status == GL_NO_ERROR)
1106 return false;
1107
1108 LOG(ERROR) << "RasterDecoder context lost via ARB/EXT_robustness. Reset "
1109 "status = "
1110 << gles2::GLES2Util::GetStringEnum(driver_status);
1111
1112 switch (driver_status) {
1113 case GL_GUILTY_CONTEXT_RESET_ARB:
1114 MarkContextLost(error::kGuilty);
1115 break;
1116 case GL_INNOCENT_CONTEXT_RESET_ARB:
1117 MarkContextLost(error::kInnocent);
1118 break;
1119 case GL_UNKNOWN_CONTEXT_RESET_ARB:
1120 MarkContextLost(error::kUnknown);
1121 break;
1122 default:
1123 NOTREACHED();
Jonathan Backer4f9ee5fb2018-04-25 14:03:381124 return false;
Jonathan Backer4f9ee5fb2018-04-25 14:03:381125 }
Antoine Labourdce1ba12019-06-22 02:05:591126 reset_by_robustness_extension_ = true;
1127 return true;
Jonathan Backer7f90dfb662017-12-18 16:52:041128}
1129
Daniel Bratellaabc8d68e2018-07-18 19:20:261130gles2::Logger* RasterDecoderImpl::GetLogger() {
Jonathan Backera4568da12018-01-31 16:25:041131 return &logger_;
1132}
1133
1134void RasterDecoderImpl::SetIgnoreCachedStateForTest(bool ignore) {
Antoine Labour10dddca12019-02-19 19:09:261135 if (use_passthrough_)
Jonathan Backer628d73f82019-01-17 17:34:181136 return;
Peng Huang66a7a3762018-12-07 20:05:381137 state()->SetIgnoreCachedStateForTest(ignore);
Jonathan Backera4568da12018-01-31 16:25:041138}
1139
Daniel Bratellaabc8d68e2018-07-18 19:20:261140gles2::ImageManager* RasterDecoderImpl::GetImageManagerForTest() {
Antoine Labour10dddca12019-02-19 19:09:261141 NOTREACHED();
1142 return nullptr;
Jonathan Backerac62dde2018-03-27 21:26:091143}
1144
Jonathan Backer10821a82018-04-04 23:56:031145void RasterDecoderImpl::SetCopyTextureResourceManagerForTest(
Daniel Bratellaabc8d68e2018-07-18 19:20:261146 gles2::CopyTextureCHROMIUMResourceManager* copy_texture_resource_manager) {
Jonathan Backer10821a82018-04-04 23:56:031147 copy_texture_chromium_.reset(copy_texture_resource_manager);
1148}
1149
Jonathan Backer7471b2782018-01-25 18:14:191150void RasterDecoderImpl::BeginDecoding() {
Jonathan Backere26739c2018-05-15 13:27:071151 gpu_tracer_->BeginDecoding();
1152 gpu_trace_commands_ = gpu_tracer_->IsTracing() && *gpu_decoder_category_;
1153 gpu_debug_commands_ = log_commands() || debug() || gpu_trace_commands_;
Khushalcd8fbb772018-10-16 22:46:141154 query_manager_->BeginProcessingCommands();
Jonathan Backer7f90dfb662017-12-18 16:52:041155}
1156
Jonathan Backere26739c2018-05-15 13:27:071157void RasterDecoderImpl::EndDecoding() {
1158 gpu_tracer_->EndDecoding();
Khushalcd8fbb772018-10-16 22:46:141159 query_manager_->EndProcessingCommands();
Jonathan Backere26739c2018-05-15 13:27:071160}
Jonathan Backer7f90dfb662017-12-18 16:52:041161
Jonathan Backer7471b2782018-01-25 18:14:191162const char* RasterDecoderImpl::GetCommandName(unsigned int command_id) const {
1163 if (command_id >= kFirstRasterCommand && command_id < kNumCommands) {
Jonathan Backera4568da12018-01-31 16:25:041164 return raster::GetCommandName(static_cast<CommandId>(command_id));
Jonathan Backer7f90dfb662017-12-18 16:52:041165 }
1166 return GetCommonCommandName(static_cast<cmd::CommandId>(command_id));
1167}
1168
Jonathan Backer1d807a42018-01-08 20:45:541169template <bool DebugImpl>
Jonathan Backer7471b2782018-01-25 18:14:191170error::Error RasterDecoderImpl::DoCommandsImpl(unsigned int num_commands,
1171 const volatile void* buffer,
1172 int num_entries,
1173 int* entries_processed) {
Jonathan Backer7f90dfb662017-12-18 16:52:041174 DCHECK(entries_processed);
1175 commands_to_process_ = num_commands;
1176 error::Error result = error::kNoError;
1177 const volatile CommandBufferEntry* cmd_data =
1178 static_cast<const volatile CommandBufferEntry*>(buffer);
1179 int process_pos = 0;
Jonathan Backer2231a002018-05-14 19:55:221180 CommandId command = static_cast<CommandId>(0);
Jonathan Backer7f90dfb662017-12-18 16:52:041181
Jonathan Backer7f90dfb662017-12-18 16:52:041182 while (process_pos < num_entries && result == error::kNoError &&
1183 commands_to_process_--) {
1184 const unsigned int size = cmd_data->value_header.size;
Jonathan Backer2231a002018-05-14 19:55:221185 command = static_cast<CommandId>(cmd_data->value_header.command);
Jonathan Backer7f90dfb662017-12-18 16:52:041186
1187 if (size == 0) {
1188 result = error::kInvalidSize;
1189 break;
1190 }
1191
1192 if (static_cast<int>(size) + process_pos > num_entries) {
1193 result = error::kOutOfBounds;
1194 break;
1195 }
1196
Jonathan Backer1d807a42018-01-08 20:45:541197 if (DebugImpl && log_commands()) {
1198 LOG(ERROR) << "[" << logger_.GetLogPrefix() << "]"
1199 << "cmd: " << GetCommandName(command);
1200 }
1201
Jonathan Backer7f90dfb662017-12-18 16:52:041202 const unsigned int arg_count = size - 1;
Jonathan Backer7471b2782018-01-25 18:14:191203 unsigned int command_index = command - kFirstRasterCommand;
Avi Drissman6fc2408c2018-12-25 21:29:081204 if (command_index < base::size(command_info)) {
Jonathan Backer7f90dfb662017-12-18 16:52:041205 const CommandInfo& info = command_info[command_index];
Jonathan Backer2231a002018-05-14 19:55:221206 if (sk_surface_) {
1207 if (!AllowedBetweenBeginEndRaster(command)) {
1208 LOCAL_SET_GL_ERROR(
1209 GL_INVALID_OPERATION, GetCommandName(command),
1210 "Unexpected command between BeginRasterCHROMIUM and "
1211 "EndRasterCHROMIUM");
1212 process_pos += size;
1213 cmd_data += size;
1214 continue;
1215 }
1216 }
Jonathan Backer7f90dfb662017-12-18 16:52:041217 unsigned int info_arg_count = static_cast<unsigned int>(info.arg_count);
1218 if ((info.arg_flags == cmd::kFixed && arg_count == info_arg_count) ||
1219 (info.arg_flags == cmd::kAtLeastN && arg_count >= info_arg_count)) {
Jonathan Backere26739c2018-05-15 13:27:071220 bool doing_gpu_trace = false;
1221 if (DebugImpl && gpu_trace_commands_) {
1222 if (CMD_FLAG_GET_TRACE_LEVEL(info.cmd_flags) <= gpu_trace_level_) {
1223 doing_gpu_trace = true;
Miguel Casas510fbfcb2018-11-13 23:40:571224 gpu_tracer_->Begin(TRACE_DISABLED_BY_DEFAULT("gpu.decoder"),
Daniel Bratellaabc8d68e2018-07-18 19:20:261225 GetCommandName(command), gles2::kTraceDecoder);
Jonathan Backere26739c2018-05-15 13:27:071226 }
1227 }
1228
Jonathan Backer7f90dfb662017-12-18 16:52:041229 uint32_t immediate_data_size = (arg_count - info_arg_count) *
1230 sizeof(CommandBufferEntry); // NOLINT
Jonathan Backer7471b2782018-01-25 18:14:191231 result = (this->*info.cmd_handler)(immediate_data_size, cmd_data);
Jonathan Backere26739c2018-05-15 13:27:071232
1233 if (DebugImpl && doing_gpu_trace)
Daniel Bratellaabc8d68e2018-07-18 19:20:261234 gpu_tracer_->End(gles2::kTraceDecoder);
Jonathan Backere26739c2018-05-15 13:27:071235
Jonathan Backer7471b2782018-01-25 18:14:191236 if (DebugImpl && debug() && !WasContextLost()) {
1237 GLenum error;
1238 while ((error = api()->glGetErrorFn()) != GL_NO_ERROR) {
1239 LOG(ERROR) << "[" << logger_.GetLogPrefix() << "] "
Daniel Bratellaabc8d68e2018-07-18 19:20:261240 << "GL ERROR: " << gles2::GLES2Util::GetStringEnum(error)
Jonathan Backer7471b2782018-01-25 18:14:191241 << " : " << GetCommandName(command);
1242 LOCAL_SET_GL_ERROR(error, "DoCommand", "GL error from driver");
Jonathan Backer1d807a42018-01-08 20:45:541243 }
Jonathan Backer7f90dfb662017-12-18 16:52:041244 }
1245 } else {
1246 result = error::kInvalidArguments;
1247 }
1248 } else {
1249 result = DoCommonCommand(command, arg_count, cmd_data);
1250 }
1251
1252 if (result == error::kNoError &&
1253 current_decoder_error_ != error::kNoError) {
1254 result = current_decoder_error_;
1255 current_decoder_error_ = error::kNoError;
1256 }
1257
1258 if (result != error::kDeferCommandUntilLater) {
1259 process_pos += size;
1260 cmd_data += size;
1261 }
Christopher Cameron02e3149a2019-01-25 19:52:231262
Christopher Cameronbeb5f272019-02-04 22:49:131263 // Workaround for https://crbug.com/906453: Flush after every command that
1264 // is not between a BeginRaster and EndRaster.
1265 if (!sk_surface_)
1266 FlushToWorkAroundMacCrashes();
Jonathan Backer7f90dfb662017-12-18 16:52:041267 }
1268
1269 *entries_processed = process_pos;
1270
1271 if (error::IsError(result)) {
1272 LOG(ERROR) << "Error: " << result << " for Command "
1273 << GetCommandName(command);
1274 }
1275
Khushal7324ec42018-07-10 20:01:451276 if (supports_oop_raster_)
Corentin Wallez0f412f02019-04-03 22:42:381277 client()->ScheduleGrContextCleanup();
Khushal7324ec42018-07-10 20:01:451278
Jonathan Backer7f90dfb662017-12-18 16:52:041279 return result;
1280}
1281
Jonathan Backer7471b2782018-01-25 18:14:191282error::Error RasterDecoderImpl::DoCommands(unsigned int num_commands,
1283 const volatile void* buffer,
1284 int num_entries,
1285 int* entries_processed) {
Jonathan Backer1d807a42018-01-08 20:45:541286 if (gpu_debug_commands_) {
1287 return DoCommandsImpl<true>(num_commands, buffer, num_entries,
1288 entries_processed);
1289 } else {
1290 return DoCommandsImpl<false>(num_commands, buffer, num_entries,
1291 entries_processed);
1292 }
Jonathan Backer7f90dfb662017-12-18 16:52:041293}
1294
Corentin Wallez0f412f02019-04-03 22:42:381295void RasterDecoderImpl::ExitCommandProcessingEarly() {
1296 commands_to_process_ = 0;
1297}
1298
Jonathan Backer7471b2782018-01-25 18:14:191299base::StringPiece RasterDecoderImpl::GetLogPrefix() {
Jonathan Backer1d807a42018-01-08 20:45:541300 return logger_.GetLogPrefix();
Jonathan Backer7f90dfb662017-12-18 16:52:041301}
1302
Jonathan Backer7471b2782018-01-25 18:14:191303void RasterDecoderImpl::BindImage(uint32_t client_texture_id,
1304 uint32_t texture_target,
1305 gl::GLImage* image,
1306 bool can_bind_to_sampler) {
Jonathan Backer7f90dfb662017-12-18 16:52:041307 NOTIMPLEMENTED();
1308}
1309
Jonathan Backer7471b2782018-01-25 18:14:191310gles2::ContextGroup* RasterDecoderImpl::GetContextGroup() {
Antoine Labour914f3af2019-02-14 20:24:131311 return nullptr;
Jonathan Backer1d807a42018-01-08 20:45:541312}
1313
Jonathan Backer7471b2782018-01-25 18:14:191314gles2::ErrorState* RasterDecoderImpl::GetErrorState() {
Peng Huangb4ed1852018-12-05 03:35:291315 return error_state_.get();
Jonathan Backer1d807a42018-01-08 20:45:541316}
1317
Daniel Bratellaabc8d68e2018-07-18 19:20:261318std::unique_ptr<gles2::AbstractTexture>
1319RasterDecoderImpl::CreateAbstractTexture(GLenum target,
1320 GLenum internal_format,
1321 GLsizei width,
1322 GLsizei height,
1323 GLsizei depth,
1324 GLint border,
1325 GLenum format,
1326 GLenum type) {
liberato@chromium.org00c8a6c2018-06-05 02:22:411327 return nullptr;
1328}
1329
Jonathan Backerc26060e2018-03-29 15:06:291330bool RasterDecoderImpl::IsCompressedTextureFormat(unsigned format) {
Antoine Labour10dddca12019-02-19 19:09:261331 return feature_info()->validators()->compressed_texture_format.IsValid(
1332 format);
Jonathan Backerc26060e2018-03-29 15:06:291333}
1334
1335bool RasterDecoderImpl::ClearLevel(gles2::Texture* texture,
1336 unsigned target,
1337 int level,
1338 unsigned format,
1339 unsigned type,
1340 int xoffset,
1341 int yoffset,
1342 int width,
1343 int height) {
1344 DCHECK(target != GL_TEXTURE_3D && target != GL_TEXTURE_2D_ARRAY &&
1345 target != GL_TEXTURE_EXTERNAL_OES);
Daniel Bratellaabc8d68e2018-07-18 19:20:261346 uint32_t channels = gles2::GLES2Util::GetChannelsForFormat(format);
1347 if (channels & gles2::GLES2Util::kDepth) {
Jonathan Backerc26060e2018-03-29 15:06:291348 DCHECK(false) << "depth not supported";
1349 return false;
1350 }
1351
Jonathan Backer0cd1c4322018-04-17 16:57:101352 static constexpr uint32_t kMaxZeroSize = 1024 * 1024 * 4;
Jonathan Backerc26060e2018-03-29 15:06:291353
1354 uint32_t size;
1355 uint32_t padded_row_size;
Peng Huange89c2a052018-12-04 11:35:111356 constexpr GLint unpack_alignment = 4;
Daniel Bratellaabc8d68e2018-07-18 19:20:261357 if (!gles2::GLES2Util::ComputeImageDataSizes(width, height, 1, format, type,
Peng Huange89c2a052018-12-04 11:35:111358 unpack_alignment, &size, nullptr,
1359 &padded_row_size)) {
Jonathan Backerc26060e2018-03-29 15:06:291360 return false;
1361 }
1362
1363 TRACE_EVENT1("gpu", "RasterDecoderImpl::ClearLevel", "size", size);
1364
1365 int tile_height;
1366
1367 if (size > kMaxZeroSize) {
1368 if (kMaxZeroSize < padded_row_size) {
1369 // That'd be an awfully large texture.
1370 return false;
1371 }
1372 // We should never have a large total size with a zero row size.
1373 DCHECK_GT(padded_row_size, 0U);
1374 tile_height = kMaxZeroSize / padded_row_size;
Daniel Bratellaabc8d68e2018-07-18 19:20:261375 if (!gles2::GLES2Util::ComputeImageDataSizes(width, tile_height, 1, format,
Peng Huange89c2a052018-12-04 11:35:111376 type, unpack_alignment, &size,
1377 nullptr, nullptr)) {
Jonathan Backerc26060e2018-03-29 15:06:291378 return false;
1379 }
1380 } else {
1381 tile_height = height;
1382 }
1383
Jonathan Backerc26060e2018-03-29 15:06:291384 {
Peng Huang66a7a3762018-12-07 20:05:381385 ScopedTextureBinder binder(state(), texture->target(),
Antoine Labour2c9d3fb2019-01-19 08:43:431386 texture->service_id(), gr_context());
Peng Huange89c2a052018-12-04 11:35:111387 base::Optional<ScopedPixelUnpackState> pixel_unpack_state;
Peng Huang20361dad12019-01-23 14:48:101388 if (shared_context_state_->need_context_state_reset()) {
Antoine Labour10dddca12019-02-19 19:09:261389 pixel_unpack_state.emplace(state(), gr_context(), feature_info());
Peng Huange89c2a052018-12-04 11:35:111390 }
Jonathan Backerc26060e2018-03-29 15:06:291391 // Add extra scope to destroy zero and the object it owns right
1392 // after its usage.
1393 // Assumes the size has already been checked.
1394 std::unique_ptr<char[]> zero(new char[size]);
1395 memset(zero.get(), 0, size);
Jonathan Backerc26060e2018-03-29 15:06:291396 GLint y = 0;
1397 while (y < height) {
1398 GLint h = y + tile_height > height ? height - y : tile_height;
1399 api()->glTexSubImage2DFn(
1400 target, level, xoffset, yoffset + y, width, h,
Antoine Labour10dddca12019-02-19 19:09:261401 gles2::TextureManager::AdjustTexFormat(feature_info(), format), type,
1402 zero.get());
Jonathan Backerc26060e2018-03-29 15:06:291403 y += tile_height;
1404 }
1405 }
Jonathan Backerc26060e2018-03-29 15:06:291406 DCHECK(glGetError() == GL_NO_ERROR);
1407 return true;
1408}
1409
1410bool RasterDecoderImpl::ClearCompressedTextureLevel(gles2::Texture* texture,
1411 unsigned target,
1412 int level,
1413 unsigned format,
1414 int width,
1415 int height) {
Peng Huange89c2a052018-12-04 11:35:111416 NOTREACHED();
1417 return false;
Jonathan Backerc26060e2018-03-29 15:06:291418}
1419
Andres Calderon Jaramillob870ca62019-01-29 16:33:351420int RasterDecoderImpl::GetRasterDecoderId() const {
1421 return raster_decoder_id_;
1422}
1423
Khushal996e9912018-07-13 08:31:001424int RasterDecoderImpl::DecoderIdForTest() {
1425 return raster_decoder_id_;
1426}
1427
Jonathan Backer0cd1c4322018-04-17 16:57:101428ServiceTransferCache* RasterDecoderImpl::GetTransferCacheForTest() {
Peng Huang20361dad12019-01-23 14:48:101429 return shared_context_state_->transfer_cache();
Jonathan Backer0cd1c4322018-04-17 16:57:101430}
1431
Khushal15b6abb2018-06-28 00:16:251432void RasterDecoderImpl::SetUpForRasterCHROMIUMForTest() {
1433 // Some tests use mock GL which doesn't work with skia. Just use a bitmap
1434 // backed surface for OOP raster commands.
Mike Reed421f75d2019-02-25 20:30:031435 auto info = SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType,
1436 SkColorSpace::MakeSRGB());
Peng Huange9b41cd2019-08-12 19:39:471437 sk_surface_for_testing_ = SkSurface::MakeRaster(info);
1438 sk_surface_ = sk_surface_for_testing_.get();
Mike Reed421f75d2019-02-25 20:30:031439 raster_canvas_ = sk_surface_->getCanvas();
Khushal15b6abb2018-06-28 00:16:251440}
1441
Jonathan Backerc7b82972018-11-21 19:08:481442void RasterDecoderImpl::SetOOMErrorForTest() {
1443 LOCAL_SET_GL_ERROR(GL_OUT_OF_MEMORY, "SetOOMErrorForTest",
1444 "synthetic out of memory");
1445}
1446
Christopher Cameron386e18582019-01-11 20:17:341447void RasterDecoderImpl::DisableFlushWorkaroundForTest() {
1448 flush_workaround_disabled_for_test_ = true;
1449}
1450
Jonathan Backer7471b2782018-01-25 18:14:191451void RasterDecoderImpl::OnContextLostError() {
Jonathan Backer4f9ee5fb2018-04-25 14:03:381452 if (!WasContextLost()) {
1453 // Need to lose current context before broadcasting!
1454 CheckResetStatus();
Jonathan Backer4f9ee5fb2018-04-25 14:03:381455 reset_by_robustness_extension_ = true;
1456 }
Jonathan Backer7f90dfb662017-12-18 16:52:041457}
1458
Jonathan Backer7471b2782018-01-25 18:14:191459void RasterDecoderImpl::OnOutOfMemoryError() {
Jonathan Backer4f9ee5fb2018-04-25 14:03:381460 if (lose_context_when_out_of_memory_ && !WasContextLost()) {
Antoine Labour10dddca12019-02-19 19:09:261461 if (!CheckResetStatus()) {
Jonathan Backer4f9ee5fb2018-04-25 14:03:381462 MarkContextLost(error::kOutOfMemory);
1463 }
Jonathan Backer4f9ee5fb2018-04-25 14:03:381464 }
Jonathan Backer7f90dfb662017-12-18 16:52:041465}
1466
Jonathan Backer7471b2782018-01-25 18:14:191467error::Error RasterDecoderImpl::HandleBeginQueryEXT(
1468 uint32_t immediate_data_size,
1469 const volatile void* cmd_data) {
Jonathan Backer016bd97e2018-03-14 15:26:391470 const volatile raster::cmds::BeginQueryEXT& c =
1471 *static_cast<const volatile raster::cmds::BeginQueryEXT*>(cmd_data);
1472 GLenum target = static_cast<GLenum>(c.target);
1473 GLuint client_id = static_cast<GLuint>(c.id);
1474 int32_t sync_shm_id = static_cast<int32_t>(c.sync_data_shm_id);
1475 uint32_t sync_shm_offset = static_cast<uint32_t>(c.sync_data_shm_offset);
1476
1477 switch (target) {
1478 case GL_COMMANDS_ISSUED_CHROMIUM:
Jonathan Backer80b270292018-12-13 19:13:551479 break;
Jonathan Backer016bd97e2018-03-14 15:26:391480 case GL_COMMANDS_COMPLETED_CHROMIUM:
Jonathan Backer80b270292018-12-13 19:13:551481 if (!features().chromium_sync_query) {
1482 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glBeginQueryEXT",
1483 "not enabled for commands completed queries");
1484 return error::kNoError;
1485 }
Jonathan Backer016bd97e2018-03-14 15:26:391486 break;
1487 default:
1488 LOCAL_SET_GL_ERROR(GL_INVALID_ENUM, "glBeginQueryEXT",
1489 "unknown query target");
1490 return error::kNoError;
1491 }
1492
1493 if (query_manager_->GetActiveQuery(target)) {
1494 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glBeginQueryEXT",
1495 "query already in progress");
1496 return error::kNoError;
1497 }
1498
1499 if (client_id == 0) {
1500 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glBeginQueryEXT", "id is 0");
1501 return error::kNoError;
1502 }
1503
Jonathan Backer0cd1c4322018-04-17 16:57:101504 scoped_refptr<Buffer> buffer = GetSharedMemoryBuffer(sync_shm_id);
Jonathan Backer016bd97e2018-03-14 15:26:391505 if (!buffer)
1506 return error::kInvalidArguments;
1507 QuerySync* sync = static_cast<QuerySync*>(
1508 buffer->GetDataAddress(sync_shm_offset, sizeof(QuerySync)));
1509 if (!sync)
1510 return error::kOutOfBounds;
1511
1512 QueryManager::Query* query = query_manager_->GetQuery(client_id);
1513 if (!query) {
1514 if (!query_manager_->IsValidQuery(client_id)) {
1515 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glBeginQueryEXT",
1516 "id not made by glGenQueriesEXT");
1517 return error::kNoError;
1518 }
1519
1520 query =
1521 query_manager_->CreateQuery(target, client_id, std::move(buffer), sync);
1522 } else {
1523 if (query->target() != target) {
1524 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glBeginQueryEXT",
1525 "target does not match");
1526 return error::kNoError;
1527 } else if (query->sync() != sync) {
1528 DLOG(ERROR) << "Shared memory used by query not the same as before";
1529 return error::kInvalidArguments;
1530 }
1531 }
1532
1533 query_manager_->BeginQuery(query);
Jonathan Backer7471b2782018-01-25 18:14:191534 return error::kNoError;
1535}
1536
1537error::Error RasterDecoderImpl::HandleEndQueryEXT(
1538 uint32_t immediate_data_size,
1539 const volatile void* cmd_data) {
Jonathan Backer016bd97e2018-03-14 15:26:391540 const volatile raster::cmds::EndQueryEXT& c =
1541 *static_cast<const volatile raster::cmds::EndQueryEXT*>(cmd_data);
1542 GLenum target = static_cast<GLenum>(c.target);
1543 uint32_t submit_count = static_cast<GLuint>(c.submit_count);
1544
1545 QueryManager::Query* query = query_manager_->GetActiveQuery(target);
1546 if (!query) {
1547 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glEndQueryEXT",
1548 "No active query");
1549 return error::kNoError;
1550 }
1551
1552 query_manager_->EndQuery(query, submit_count);
Jonathan Backer7471b2782018-01-25 18:14:191553 return error::kNoError;
1554}
1555
Andres Calderon Jaramillo21e98152019-08-22 01:38:041556error::Error RasterDecoderImpl::HandleQueryCounterEXT(
1557 uint32_t immediate_data_size,
1558 const volatile void* cmd_data) {
1559 const volatile raster::cmds::QueryCounterEXT& c =
1560 *static_cast<const volatile raster::cmds::QueryCounterEXT*>(cmd_data);
1561 GLenum target = static_cast<GLenum>(c.target);
1562 GLuint client_id = static_cast<GLuint>(c.id);
1563 int32_t sync_shm_id = static_cast<int32_t>(c.sync_data_shm_id);
1564 uint32_t sync_shm_offset = static_cast<uint32_t>(c.sync_data_shm_offset);
1565 uint32_t submit_count = static_cast<GLuint>(c.submit_count);
1566
1567 if (target != GL_COMMANDS_ISSUED_TIMESTAMP_CHROMIUM) {
1568 LOCAL_SET_GL_ERROR(GL_INVALID_ENUM, "glQueryCounterEXT",
1569 "unknown query target");
1570 return error::kNoError;
1571 }
1572
1573 scoped_refptr<Buffer> buffer = GetSharedMemoryBuffer(sync_shm_id);
1574 if (!buffer)
1575 return error::kInvalidArguments;
1576 QuerySync* sync = static_cast<QuerySync*>(
1577 buffer->GetDataAddress(sync_shm_offset, sizeof(QuerySync)));
1578 if (!sync)
1579 return error::kOutOfBounds;
1580
1581 QueryManager::Query* query = query_manager_->GetQuery(client_id);
1582 if (!query) {
1583 if (!query_manager_->IsValidQuery(client_id)) {
1584 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glQueryCounterEXT",
1585 "id not made by glGenQueriesEXT");
1586 return error::kNoError;
1587 }
1588 query =
1589 query_manager_->CreateQuery(target, client_id, std::move(buffer), sync);
1590 } else {
1591 if (query->target() != target) {
1592 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glQueryCounterEXT",
1593 "target does not match");
1594 return error::kNoError;
1595 } else if (query->sync() != sync) {
1596 DLOG(ERROR) << "Shared memory used by query not the same as before";
1597 return error::kInvalidArguments;
1598 }
1599 }
1600 query_manager_->QueryCounter(query, submit_count);
1601
1602 return error::kNoError;
1603}
1604
Jonathan Backera4568da12018-01-31 16:25:041605void RasterDecoderImpl::DoFinish() {
Christopher Camerondd7812d2019-04-23 03:12:211606 if (shared_context_state_->GrContextIsGL())
Jonathan Backer7621ec92018-12-11 00:02:441607 api()->glFinishFn();
Jonathan Backera4568da12018-01-31 16:25:041608 ProcessPendingQueries(true);
1609}
1610
1611void RasterDecoderImpl::DoFlush() {
Christopher Camerondd7812d2019-04-23 03:12:211612 if (shared_context_state_->GrContextIsGL())
Jonathan Backer7621ec92018-12-11 00:02:441613 api()->glFlushFn();
Jonathan Backera4568da12018-01-31 16:25:041614 ProcessPendingQueries(false);
1615}
1616
Jonathan Backer016bd97e2018-03-14 15:26:391617bool RasterDecoderImpl::GenQueriesEXTHelper(GLsizei n,
1618 const GLuint* client_ids) {
1619 for (GLsizei ii = 0; ii < n; ++ii) {
1620 if (query_manager_->IsValidQuery(client_ids[ii])) {
1621 return false;
1622 }
1623 }
1624 query_manager_->GenQueries(n, client_ids);
1625 return true;
1626}
1627
1628void RasterDecoderImpl::DeleteQueriesEXTHelper(
1629 GLsizei n,
1630 const volatile GLuint* client_ids) {
1631 for (GLsizei ii = 0; ii < n; ++ii) {
1632 GLuint client_id = client_ids[ii];
1633 query_manager_->RemoveQuery(client_id);
1634 }
1635}
1636
Jonathan Backere26739c2018-05-15 13:27:071637error::Error RasterDecoderImpl::HandleTraceBeginCHROMIUM(
1638 uint32_t immediate_data_size,
1639 const volatile void* cmd_data) {
1640 const volatile gles2::cmds::TraceBeginCHROMIUM& c =
1641 *static_cast<const volatile gles2::cmds::TraceBeginCHROMIUM*>(cmd_data);
1642 Bucket* category_bucket = GetBucket(c.category_bucket_id);
1643 Bucket* name_bucket = GetBucket(c.name_bucket_id);
Jonathan Backerff2ac0f2018-07-18 15:53:471644 static constexpr size_t kMaxStrLen = 256;
1645 if (!category_bucket || category_bucket->size() == 0 ||
1646 category_bucket->size() > kMaxStrLen || !name_bucket ||
1647 name_bucket->size() == 0 || name_bucket->size() > kMaxStrLen) {
Jonathan Backere26739c2018-05-15 13:27:071648 return error::kInvalidArguments;
1649 }
1650
1651 std::string category_name;
1652 std::string trace_name;
1653 if (!category_bucket->GetAsString(&category_name) ||
1654 !name_bucket->GetAsString(&trace_name)) {
1655 return error::kInvalidArguments;
1656 }
1657
1658 debug_marker_manager_.PushGroup(trace_name);
Daniel Bratellaabc8d68e2018-07-18 19:20:261659 if (!gpu_tracer_->Begin(category_name, trace_name, gles2::kTraceCHROMIUM)) {
Jonathan Backere26739c2018-05-15 13:27:071660 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glTraceBeginCHROMIUM",
1661 "unable to create begin trace");
1662 return error::kNoError;
1663 }
1664 return error::kNoError;
1665}
1666
1667void RasterDecoderImpl::DoTraceEndCHROMIUM() {
1668 debug_marker_manager_.PopGroup();
Daniel Bratellaabc8d68e2018-07-18 19:20:261669 if (!gpu_tracer_->End(gles2::kTraceCHROMIUM)) {
Jonathan Backere26739c2018-05-15 13:27:071670 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glTraceEndCHROMIUM",
1671 "no trace begin found");
1672 return;
1673 }
1674}
1675
Khushal49836ab2018-07-25 02:08:451676error::Error RasterDecoderImpl::HandleSetActiveURLCHROMIUM(
1677 uint32_t immediate_data_size,
1678 const volatile void* cmd_data) {
1679 const volatile cmds::SetActiveURLCHROMIUM& c =
1680 *static_cast<const volatile cmds::SetActiveURLCHROMIUM*>(cmd_data);
1681 Bucket* url_bucket = GetBucket(c.url_bucket_id);
1682 static constexpr size_t kMaxStrLen = 1024;
1683 if (!url_bucket || url_bucket->size() == 0 ||
Khushalda1fb8332018-10-26 19:26:221684 url_bucket->size() > kMaxStrLen) {
Khushal49836ab2018-07-25 02:08:451685 return error::kInvalidArguments;
1686 }
1687
Khushalda1fb8332018-10-26 19:26:221688 size_t size = url_bucket->size();
Khushal49836ab2018-07-25 02:08:451689 const char* url_str = url_bucket->GetDataAs<const char*>(0, size);
1690 if (!url_str)
1691 return error::kInvalidArguments;
1692
1693 GURL url(base::StringPiece(url_str, size));
Corentin Wallez0f412f02019-04-03 22:42:381694 client()->SetActiveURL(std::move(url));
Khushal49836ab2018-07-25 02:08:451695 return error::kNoError;
1696}
1697
Jonathan Backer10821a82018-04-04 23:56:031698bool RasterDecoderImpl::InitializeCopyTexImageBlitter() {
1699 if (!copy_tex_image_blit_.get()) {
1700 LOCAL_COPY_REAL_GL_ERRORS_TO_WRAPPER("glCopySubTexture");
1701 copy_tex_image_blit_.reset(
Antoine Labour10dddca12019-02-19 19:09:261702 new gles2::CopyTexImageResourceManager(feature_info()));
Jonathan Backer10821a82018-04-04 23:56:031703 copy_tex_image_blit_->Initialize(this);
1704 if (LOCAL_PEEK_GL_ERROR("glCopySubTexture") != GL_NO_ERROR)
1705 return false;
1706 }
1707 return true;
1708}
1709
1710bool RasterDecoderImpl::InitializeCopyTextureCHROMIUM() {
1711 // Defer initializing the CopyTextureCHROMIUMResourceManager until it is
1712 // needed because it takes 10s of milliseconds to initialize.
1713 if (!copy_texture_chromium_.get()) {
1714 LOCAL_COPY_REAL_GL_ERRORS_TO_WRAPPER("glCopySubTexture");
Daniel Bratellaabc8d68e2018-07-18 19:20:261715 copy_texture_chromium_.reset(
1716 gles2::CopyTextureCHROMIUMResourceManager::Create());
Jonathan Backer10821a82018-04-04 23:56:031717 copy_texture_chromium_->Initialize(this, features());
1718 if (LOCAL_PEEK_GL_ERROR("glCopySubTexture") != GL_NO_ERROR)
1719 return false;
1720
1721 // On the desktop core profile this also needs emulation of
1722 // CopyTex{Sub}Image2D for luminance, alpha, and luminance_alpha
1723 // textures.
Daniel Bratellaabc8d68e2018-07-18 19:20:261724 if (gles2::CopyTexImageResourceManager::CopyTexImageRequiresBlit(
Antoine Labour10dddca12019-02-19 19:09:261725 feature_info(), GL_LUMINANCE)) {
Jonathan Backer10821a82018-04-04 23:56:031726 if (!InitializeCopyTexImageBlitter())
1727 return false;
1728 }
1729 }
1730 return true;
1731}
1732
Antoine Labour9ddf6ac2019-01-17 01:59:391733void RasterDecoderImpl::DoCopySubTextureINTERNAL(
1734 GLint xoffset,
1735 GLint yoffset,
1736 GLint x,
1737 GLint y,
1738 GLsizei width,
1739 GLsizei height,
Nathan Zabriskief1095d7b2019-12-21 00:33:571740 GLboolean unpack_flip_y,
1741 GLboolean unpack_premultiply_alpha,
Antoine Labour9ddf6ac2019-01-17 01:59:391742 const volatile GLbyte* mailboxes) {
1743 Mailbox source_mailbox = Mailbox::FromVolatile(
1744 reinterpret_cast<const volatile Mailbox*>(mailboxes)[0]);
1745 DLOG_IF(ERROR, !source_mailbox.Verify())
1746 << "CopySubTexture was passed an invalid mailbox";
1747 Mailbox dest_mailbox = Mailbox::FromVolatile(
1748 reinterpret_cast<const volatile Mailbox*>(mailboxes)[1]);
1749 DLOG_IF(ERROR, !dest_mailbox.Verify())
1750 << "CopySubTexture was passed an invalid mailbox";
1751
Antoine Labourb9f903b2019-02-04 18:06:571752 if (source_mailbox == dest_mailbox) {
1753 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glCopySubTexture",
1754 "source and destination mailboxes are the same");
1755 return;
1756 }
1757
Peng Huanga8b0e422019-07-16 17:48:571758 if (!shared_context_state_->GrContextIsGL()) {
1759 // Use Skia to copy texture if raster's gr_context() is not using GL.
1760 DoCopySubTextureINTERNALSkia(xoffset, yoffset, x, y, width, height,
Nathan Zabriskief1095d7b2019-12-21 00:33:571761 unpack_flip_y, unpack_premultiply_alpha,
Peng Huanga8b0e422019-07-16 17:48:571762 source_mailbox, dest_mailbox);
1763 } else if (use_passthrough_) {
Nathan Zabriskief1095d7b2019-12-21 00:33:571764 DoCopySubTextureINTERNALGLPassthrough(
1765 xoffset, yoffset, x, y, width, height, unpack_flip_y,
1766 unpack_premultiply_alpha, source_mailbox, dest_mailbox);
Peng Huanga8b0e422019-07-16 17:48:571767 } else {
1768 DoCopySubTextureINTERNALGL(xoffset, yoffset, x, y, width, height,
Nathan Zabriskief1095d7b2019-12-21 00:33:571769 unpack_flip_y, unpack_premultiply_alpha,
Peng Huanga8b0e422019-07-16 17:48:571770 source_mailbox, dest_mailbox);
1771 }
1772}
Antoine Labourb9f903b2019-02-04 18:06:571773
Peng Huanga8b0e422019-07-16 17:48:571774void RasterDecoderImpl::DoCopySubTextureINTERNALGLPassthrough(
1775 GLint xoffset,
1776 GLint yoffset,
1777 GLint x,
1778 GLint y,
1779 GLsizei width,
1780 GLsizei height,
Nathan Zabriskief1095d7b2019-12-21 00:33:571781 GLboolean unpack_flip_y,
1782 GLboolean unpack_premultiply_alpha,
Peng Huanga8b0e422019-07-16 17:48:571783 const Mailbox& source_mailbox,
1784 const Mailbox& dest_mailbox) {
1785 DCHECK(source_mailbox != dest_mailbox);
1786 DCHECK(use_passthrough_);
Jonathan Backer628d73f82019-01-17 17:34:181787
Peng Huanga8b0e422019-07-16 17:48:571788 std::unique_ptr<SharedImageRepresentationGLTexturePassthrough>
1789 source_shared_image =
1790 shared_image_representation_factory_.ProduceGLTexturePassthrough(
1791 source_mailbox);
1792 std::unique_ptr<SharedImageRepresentationGLTexturePassthrough>
1793 dest_shared_image =
1794 shared_image_representation_factory_.ProduceGLTexturePassthrough(
1795 dest_mailbox);
1796 if (!source_shared_image || !dest_shared_image) {
1797 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture", "unknown mailbox");
Jonathan Backer628d73f82019-01-17 17:34:181798 return;
1799 }
1800
Eric Karl707c1b92019-12-10 06:50:081801 std::unique_ptr<SharedImageRepresentationGLTexturePassthrough::ScopedAccess>
1802 source_access = source_shared_image->BeginScopedAccess(
Eric Karl14bfb992020-01-03 01:32:211803 GL_SHARED_IMAGE_ACCESS_MODE_READ_CHROMIUM,
1804 SharedImageRepresentation::AllowUnclearedAccess::kNo);
Eric Karl707c1b92019-12-10 06:50:081805 if (!source_access) {
Peng Huanga8b0e422019-07-16 17:48:571806 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture",
1807 "unable to access source for read");
1808 return;
1809 }
1810
Eric Karl14bfb992020-01-03 01:32:211811 // Allow uncleared access, as we manually handle clear tracking.
Eric Karl707c1b92019-12-10 06:50:081812 std::unique_ptr<SharedImageRepresentationGLTexturePassthrough::ScopedAccess>
1813 dest_access = dest_shared_image->BeginScopedAccess(
Eric Karl14bfb992020-01-03 01:32:211814 GL_SHARED_IMAGE_ACCESS_MODE_READWRITE_CHROMIUM,
1815 SharedImageRepresentation::AllowUnclearedAccess::kYes);
Eric Karl707c1b92019-12-10 06:50:081816 if (!dest_access) {
Peng Huanga8b0e422019-07-16 17:48:571817 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture",
1818 "unable to access destination for write");
1819 return;
1820 }
1821
Eric Karlb25e1b62019-12-17 23:07:491822 gfx::Rect new_cleared_rect;
1823 gfx::Rect old_cleared_rect = dest_shared_image->ClearedRect();
1824 gfx::Rect dest_rect(xoffset, yoffset, width, height);
1825 if (gles2::TextureManager::CombineAdjacentRects(old_cleared_rect, dest_rect,
1826 &new_cleared_rect)) {
1827 DCHECK(old_cleared_rect.IsEmpty() ||
1828 new_cleared_rect.Contains(old_cleared_rect));
1829 } else {
1830 // No users of RasterDecoder leverage this functionality. Clearing uncleared
1831 // regions could be added here if needed.
1832 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture",
1833 "Cannot clear non-combineable rects.");
1834 return;
1835 }
1836
Peng Huanga8b0e422019-07-16 17:48:571837 gles2::TexturePassthrough* source_texture =
1838 source_shared_image->GetTexturePassthrough().get();
1839 gles2::TexturePassthrough* dest_texture =
1840 dest_shared_image->GetTexturePassthrough().get();
1841 DCHECK(!source_texture->is_bind_pending());
1842 DCHECK_NE(source_texture->service_id(), dest_texture->service_id());
1843
1844 api()->glCopySubTextureCHROMIUMFn(
1845 source_texture->service_id(), /*source_level=*/0, dest_texture->target(),
1846 dest_texture->service_id(),
Nathan Zabriskief1095d7b2019-12-21 00:33:571847 /*dest_level=*/0, xoffset, yoffset, x, y, width, height, unpack_flip_y,
1848 unpack_premultiply_alpha,
Peng Huanga8b0e422019-07-16 17:48:571849 /*unpack_unmultiply_alpha=*/false);
1850 LOCAL_COPY_REAL_GL_ERRORS_TO_WRAPPER("glCopySubTexture");
Eric Karlb25e1b62019-12-17 23:07:491851
1852 if (!dest_shared_image->IsCleared()) {
1853 dest_shared_image->SetClearedRect(new_cleared_rect);
1854 }
Peng Huanga8b0e422019-07-16 17:48:571855}
1856
1857void RasterDecoderImpl::DoCopySubTextureINTERNALGL(
1858 GLint xoffset,
1859 GLint yoffset,
1860 GLint x,
1861 GLint y,
1862 GLsizei width,
1863 GLsizei height,
Nathan Zabriskief1095d7b2019-12-21 00:33:571864 GLboolean unpack_flip_y,
1865 GLboolean unpack_premultiply_alpha,
Peng Huanga8b0e422019-07-16 17:48:571866 const Mailbox& source_mailbox,
1867 const Mailbox& dest_mailbox) {
1868 DCHECK(source_mailbox != dest_mailbox);
1869 DCHECK(shared_context_state_->GrContextIsGL());
1870
Antoine Labourb9f903b2019-02-04 18:06:571871 std::unique_ptr<SharedImageRepresentationGLTexture> source_shared_image =
Antoine Labour10dddca12019-02-19 19:09:261872 shared_image_representation_factory_.ProduceGLTexture(source_mailbox);
Antoine Labourb9f903b2019-02-04 18:06:571873 std::unique_ptr<SharedImageRepresentationGLTexture> dest_shared_image =
Antoine Labour10dddca12019-02-19 19:09:261874 shared_image_representation_factory_.ProduceGLTexture(dest_mailbox);
Antoine Labourb9f903b2019-02-04 18:06:571875 if (!source_shared_image || !dest_shared_image) {
Antoine Labour9ddf6ac2019-01-17 01:59:391876 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture", "unknown mailbox");
Jonathan Backer10821a82018-04-04 23:56:031877 return;
1878 }
Antoine Labourb9f903b2019-02-04 18:06:571879
Eric Karl707c1b92019-12-10 06:50:081880 std::unique_ptr<SharedImageRepresentationGLTexture::ScopedAccess>
1881 source_access = source_shared_image->BeginScopedAccess(
Eric Karl14bfb992020-01-03 01:32:211882 GL_SHARED_IMAGE_ACCESS_MODE_READ_CHROMIUM,
1883 SharedImageRepresentation::AllowUnclearedAccess::kNo);
Eric Karl707c1b92019-12-10 06:50:081884 if (!source_access) {
Antoine Labourb9f903b2019-02-04 18:06:571885 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture",
1886 "unable to access source for read");
Jonathan Backer10821a82018-04-04 23:56:031887 return;
1888 }
Antoine Labourb9f903b2019-02-04 18:06:571889
1890 gles2::Texture* source_texture = source_shared_image->GetTexture();
Antoine Labouraeb4bd70e2019-01-16 02:33:281891 GLenum source_target = source_texture->target();
Antoine Labourb9f903b2019-02-04 18:06:571892 DCHECK(source_target);
1893 GLint source_level = 0;
1894 gfx::Size source_size = source_shared_image->size();
1895 gfx::Rect source_rect(x, y, width, height);
1896 if (!gfx::Rect(source_size).Contains(source_rect)) {
1897 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture",
1898 "source texture bad dimensions.");
Antoine Laboura2da0802019-01-22 21:06:591899 return;
1900 }
1901
Eric Karl14bfb992020-01-03 01:32:211902 // Allow uncleared access, as we manually handle clear tracking.
Eric Karl707c1b92019-12-10 06:50:081903 std::unique_ptr<SharedImageRepresentationGLTexture::ScopedAccess>
1904 dest_access = dest_shared_image->BeginScopedAccess(
Eric Karl14bfb992020-01-03 01:32:211905 GL_SHARED_IMAGE_ACCESS_MODE_READWRITE_CHROMIUM,
1906 SharedImageRepresentation::AllowUnclearedAccess::kYes);
Eric Karl707c1b92019-12-10 06:50:081907 if (!dest_access) {
Antoine Labourb9f903b2019-02-04 18:06:571908 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture",
1909 "unable to access destination for write");
1910 return;
Findit1f74b7f2019-01-23 01:32:411911 }
Antoine Laboura2da0802019-01-22 21:06:591912
Antoine Labourb9f903b2019-02-04 18:06:571913 gles2::Texture* dest_texture = dest_shared_image->GetTexture();
1914 GLenum dest_target = dest_texture->target();
1915 DCHECK(dest_target);
1916 GLint dest_level = 0;
1917 gfx::Size dest_size = dest_shared_image->size();
1918 gfx::Rect dest_rect(xoffset, yoffset, width, height);
1919 if (!gfx::Rect(dest_size).Contains(dest_rect)) {
1920 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture",
1921 "destination texture bad dimensions.");
1922 return;
1923 }
1924
1925 DCHECK_NE(source_texture->service_id(), dest_texture->service_id());
1926
Jonathan Backer10821a82018-04-04 23:56:031927 GLenum source_type = 0;
1928 GLenum source_internal_format = 0;
Antoine Labourb9f903b2019-02-04 18:06:571929 source_texture->GetLevelType(source_target, source_level, &source_type,
Jonathan Backer10821a82018-04-04 23:56:031930 &source_internal_format);
1931
1932 GLenum dest_type = 0;
1933 GLenum dest_internal_format = 0;
1934 bool dest_level_defined = dest_texture->GetLevelType(
Antoine Labourb9f903b2019-02-04 18:06:571935 dest_target, dest_level, &dest_type, &dest_internal_format);
1936 DCHECK(dest_level_defined);
1937
1938 // TODO(piman): Do we need this check? It might always be true by
1939 // construction.
Jonathan Backer4cb165c82018-07-03 20:25:331940 std::string output_error_msg;
1941 if (!ValidateCopyTextureCHROMIUMInternalFormats(
1942 GetFeatureInfo(), source_internal_format, dest_internal_format,
1943 &output_error_msg)) {
1944 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glCopySubTexture",
1945 output_error_msg.c_str());
1946 return;
1947 }
Jonathan Backer10821a82018-04-04 23:56:031948
Jonathan Backer10821a82018-04-04 23:56:031949 // Clear the source texture if necessary.
Antoine Labour10dddca12019-02-19 19:09:261950 if (!gles2::TextureManager::ClearTextureLevel(this, source_texture,
1951 source_target, 0 /* level */)) {
Jonathan Backer10821a82018-04-04 23:56:031952 LOCAL_SET_GL_ERROR(GL_OUT_OF_MEMORY, "glCopySubTexture",
1953 "source texture dimensions too big");
1954 return;
1955 }
1956
Antoine Labourb9f903b2019-02-04 18:06:571957 gfx::Rect new_cleared_rect;
1958 gfx::Rect old_cleared_rect =
1959 dest_texture->GetLevelClearedRect(dest_target, dest_level);
1960 if (gles2::TextureManager::CombineAdjacentRects(
1961 dest_texture->GetLevelClearedRect(dest_target, dest_level), dest_rect,
1962 &new_cleared_rect)) {
1963 DCHECK(old_cleared_rect.IsEmpty() ||
1964 new_cleared_rect.Contains(old_cleared_rect));
1965 } else {
Eric Karlb25e1b62019-12-17 23:07:491966 // No users of RasterDecoder leverage this functionality. Clearing uncleared
1967 // regions could be added here if needed.
1968 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture",
1969 "Cannot clear non-combineable rects.");
1970 return;
Antoine Labourb9f903b2019-02-04 18:06:571971 }
1972
1973 ScopedTextureBinder binder(state(), dest_target, dest_texture->service_id(),
1974 gr_context());
1975
1976 gles2::Texture::ImageState image_state;
Dave Tapuskab8a73b32019-09-10 13:38:391977 if (gl::GLImage* image =
1978 source_texture->GetLevelImage(source_target, 0, &image_state)) {
Antoine Labourb9f903b2019-02-04 18:06:571979 base::Optional<ScopedPixelUnpackState> pixel_unpack_state;
1980 if (image->GetType() == gl::GLImage::Type::MEMORY &&
1981 shared_context_state_->need_context_state_reset()) {
1982 // If the image is in shared memory, we may need upload the pixel data
1983 // with SubTexImage2D, so we need reset pixel unpack state if gl context
1984 // state has been touched by skia.
Antoine Labour10dddca12019-02-19 19:09:261985 pixel_unpack_state.emplace(state(), gr_context(), feature_info());
Antoine Labourb9f903b2019-02-04 18:06:571986 }
1987
1988 // Try to copy by uploading to the destination texture.
1989 if (dest_internal_format == source_internal_format) {
1990 if (image->CopyTexSubImage(dest_target, gfx::Point(xoffset, yoffset),
1991 gfx::Rect(x, y, width, height))) {
1992 dest_texture->SetLevelClearedRect(dest_target, dest_level,
1993 new_cleared_rect);
Jonathan Backer10821a82018-04-04 23:56:031994 return;
1995 }
1996 }
Jonathan Backer10821a82018-04-04 23:56:031997
Antoine Labourb9f903b2019-02-04 18:06:571998 // Otherwise, update the source if needed.
1999 if (image_state == gles2::Texture::UNBOUND) {
2000 ScopedGLErrorSuppressor suppressor(
2001 "RasterDecoderImpl::DoCopySubTextureINTERNAL", error_state_.get());
2002 api()->glBindTextureFn(source_target, source_texture->service_id());
2003 if (image->ShouldBindOrCopy() == gl::GLImage::BIND) {
2004 bool rv = image->BindTexImage(source_target);
2005 DCHECK(rv) << "BindTexImage() failed";
2006 image_state = gles2::Texture::BOUND;
2007 } else {
2008 bool rv = image->CopyTexImage(source_target);
2009 DCHECK(rv) << "CopyTexImage() failed";
2010 image_state = gles2::Texture::COPIED;
2011 }
2012 source_texture->SetLevelImageState(source_target, 0, image_state);
Jonathan Backer10821a82018-04-04 23:56:032013 }
2014 }
2015
2016 if (!InitializeCopyTextureCHROMIUM())
2017 return;
2018
Jonathan Backer10821a82018-04-04 23:56:032019 // GL_TEXTURE_EXTERNAL_OES texture requires apply a transform matrix
2020 // before presenting.
2021 if (source_target == GL_TEXTURE_EXTERNAL_OES) {
Daniel Bratellaabc8d68e2018-07-18 19:20:262022 if (gles2::GLStreamTextureImage* image =
Jonathan Backer10821a82018-04-04 23:56:032023 source_texture->GetLevelStreamTextureImage(GL_TEXTURE_EXTERNAL_OES,
2024 source_level)) {
2025 GLfloat transform_matrix[16];
2026 image->GetTextureMatrix(transform_matrix);
Jonathan Backer76dfd502018-05-03 18:34:312027
Jonathan Backer10821a82018-04-04 23:56:032028 copy_texture_chromium_->DoCopySubTextureWithTransform(
2029 this, source_target, source_texture->service_id(), source_level,
2030 source_internal_format, dest_target, dest_texture->service_id(),
2031 dest_level, dest_internal_format, xoffset, yoffset, x, y, width,
Antoine Labourb9f903b2019-02-04 18:06:572032 height, dest_size.width(), dest_size.height(), source_size.width(),
Nathan Zabriskief1095d7b2019-12-21 00:33:572033 source_size.height(), unpack_flip_y, unpack_premultiply_alpha,
Jonathan Backer10821a82018-04-04 23:56:032034 false /* unpack_unmultiply_alpha */, false /* dither */,
2035 transform_matrix, copy_tex_image_blit_.get());
Antoine Labourb9f903b2019-02-04 18:06:572036 dest_texture->SetLevelClearedRect(dest_target, dest_level,
2037 new_cleared_rect);
Jonathan Backer10821a82018-04-04 23:56:032038 return;
2039 }
2040 }
2041
Daniel Bratellaabc8d68e2018-07-18 19:20:262042 gles2::CopyTextureMethod method = GetCopyTextureCHROMIUMMethod(
Jonathan Backer10821a82018-04-04 23:56:032043 GetFeatureInfo(), source_target, source_level, source_internal_format,
Nathan Zabriskief1095d7b2019-12-21 00:33:572044 source_type, dest_target, dest_level, dest_internal_format, unpack_flip_y,
2045 unpack_premultiply_alpha, false /* unpack_unmultiply_alpha */,
2046 false /* dither */);
Jonathan Backer10821a82018-04-04 23:56:032047#if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY)
2048 // glDrawArrays is faster than glCopyTexSubImage2D on IA Mesa driver,
2049 // although opposite in Android.
2050 // TODO(dshwang): After Mesa fixes this issue, remove this hack.
2051 // https://bugs.freedesktop.org/show_bug.cgi?id=98478,
2052 // https://crbug.com/535198.
Daniel Bratellaabc8d68e2018-07-18 19:20:262053 if (gles2::Texture::ColorRenderable(GetFeatureInfo(), dest_internal_format,
2054 dest_texture->IsImmutable()) &&
2055 method == gles2::CopyTextureMethod::DIRECT_COPY) {
2056 method = gles2::CopyTextureMethod::DIRECT_DRAW;
Jonathan Backer10821a82018-04-04 23:56:032057 }
2058#endif
2059
Antoine Labour9ddf6ac2019-01-17 01:59:392060 in_copy_sub_texture_ = true;
Jonathan Backer10821a82018-04-04 23:56:032061 copy_texture_chromium_->DoCopySubTexture(
2062 this, source_target, source_texture->service_id(), source_level,
2063 source_internal_format, dest_target, dest_texture->service_id(),
2064 dest_level, dest_internal_format, xoffset, yoffset, x, y, width, height,
Antoine Labourb9f903b2019-02-04 18:06:572065 dest_size.width(), dest_size.height(), source_size.width(),
Nathan Zabriskief1095d7b2019-12-21 00:33:572066 source_size.height(), unpack_flip_y, unpack_premultiply_alpha,
2067 false /* unpack_unmultiply_alpha */, false /* dither */, method,
2068 copy_tex_image_blit_.get());
Antoine Labourb9f903b2019-02-04 18:06:572069 dest_texture->SetLevelClearedRect(dest_target, dest_level, new_cleared_rect);
Antoine Labour9ddf6ac2019-01-17 01:59:392070 in_copy_sub_texture_ = false;
2071 if (reset_texture_state_) {
2072 reset_texture_state_ = false;
2073 for (auto* texture : {source_texture, dest_texture}) {
2074 GLenum target = texture->target();
2075 api()->glBindTextureFn(target, texture->service_id());
2076 api()->glTexParameteriFn(target, GL_TEXTURE_WRAP_S, texture->wrap_s());
2077 api()->glTexParameteriFn(target, GL_TEXTURE_WRAP_T, texture->wrap_t());
2078 api()->glTexParameteriFn(target, GL_TEXTURE_MIN_FILTER,
2079 texture->min_filter());
2080 api()->glTexParameteriFn(target, GL_TEXTURE_MAG_FILTER,
2081 texture->mag_filter());
2082 }
Peng Huang20361dad12019-01-23 14:48:102083 shared_context_state_->PessimisticallyResetGrContext();
Antoine Labour9ddf6ac2019-01-17 01:59:392084 }
Jonathan Backer10821a82018-04-04 23:56:032085}
2086
Peng Huanga8b0e422019-07-16 17:48:572087void RasterDecoderImpl::DoCopySubTextureINTERNALSkia(
2088 GLint xoffset,
2089 GLint yoffset,
2090 GLint x,
2091 GLint y,
2092 GLsizei width,
2093 GLsizei height,
Nathan Zabriskief1095d7b2019-12-21 00:33:572094 GLboolean unpack_flip_y,
2095 GLboolean unpack_premultiply_alpha,
Peng Huanga8b0e422019-07-16 17:48:572096 const Mailbox& source_mailbox,
2097 const Mailbox& dest_mailbox) {
2098 DCHECK(source_mailbox != dest_mailbox);
2099
2100 // Use Skia to copy texture if raster's gr_context() is not using GL.
2101 auto source_shared_image = shared_image_representation_factory_.ProduceSkia(
2102 source_mailbox, shared_context_state_);
2103 auto dest_shared_image = shared_image_representation_factory_.ProduceSkia(
2104 dest_mailbox, shared_context_state_);
2105 if (!source_shared_image || !dest_shared_image) {
2106 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture", "unknown mailbox");
2107 return;
2108 }
2109
2110 gfx::Size source_size = source_shared_image->size();
2111 gfx::Rect source_rect(x, y, width, height);
2112 if (!gfx::Rect(source_size).Contains(source_rect)) {
2113 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture",
2114 "source texture bad dimensions.");
2115 return;
2116 }
2117
2118 gfx::Size dest_size = dest_shared_image->size();
2119 gfx::Rect dest_rect(xoffset, yoffset, width, height);
2120 if (!gfx::Rect(dest_size).Contains(dest_rect)) {
2121 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture",
2122 "destination texture bad dimensions.");
2123 return;
2124 }
2125
2126 std::vector<GrBackendSemaphore> begin_semaphores;
2127 std::vector<GrBackendSemaphore> end_semaphores;
2128
Eric Karl14bfb992020-01-03 01:32:212129 // Allow uncleared access, as we manually handle clear tracking.
Eric Karl707c1b92019-12-10 06:50:082130 std::unique_ptr<SharedImageRepresentationSkia::ScopedWriteAccess>
2131 dest_scoped_access = dest_shared_image->BeginScopedWriteAccess(
Eric Karl14bfb992020-01-03 01:32:212132 &begin_semaphores, &end_semaphores,
2133 SharedImageRepresentation::AllowUnclearedAccess::kYes);
Eric Karl707c1b92019-12-10 06:50:082134 if (!dest_scoped_access) {
Peng Huanga8b0e422019-07-16 17:48:572135 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture",
2136 "Dest shared image is not writable");
2137 return;
2138 }
2139
Eric Karlb25e1b62019-12-17 23:07:492140 gfx::Rect new_cleared_rect;
2141 gfx::Rect old_cleared_rect = dest_shared_image->ClearedRect();
2142 if (gles2::TextureManager::CombineAdjacentRects(old_cleared_rect, dest_rect,
2143 &new_cleared_rect)) {
2144 DCHECK(old_cleared_rect.IsEmpty() ||
2145 new_cleared_rect.Contains(old_cleared_rect));
2146 } else {
2147 // No users of RasterDecoder leverage this functionality. Clearing uncleared
2148 // regions could be added here if needed.
2149 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture",
2150 "Cannot clear non-combineable rects.");
2151 return;
2152 }
2153
Peng Huanga8b0e422019-07-16 17:48:572154 // With OneCopyRasterBufferProvider, source_shared_image->BeginReadAccess()
2155 // will copy pixels from SHM GMB to the texture in |source_shared_image|,
2156 // and then use drawImageRect() to draw that texure to the target
2157 // |dest_shared_image|. We can save one copy by drawing the SHM GMB to the
2158 // target |dest_shared_image| directly.
2159 // TODO(penghuang): get rid of the one extra copy. https://crbug.com/984045
Eric Karl707c1b92019-12-10 06:50:082160 std::unique_ptr<SharedImageRepresentationSkia::ScopedReadAccess>
2161 source_scoped_access = source_shared_image->BeginScopedReadAccess(
2162 &begin_semaphores, &end_semaphores);
Peng Huanga8b0e422019-07-16 17:48:572163
2164 if (!begin_semaphores.empty()) {
Eric Karl707c1b92019-12-10 06:50:082165 bool result = dest_scoped_access->surface()->wait(begin_semaphores.size(),
2166 begin_semaphores.data());
Peng Huanga8b0e422019-07-16 17:48:572167 DCHECK(result);
2168 }
2169
Eric Karl707c1b92019-12-10 06:50:082170 if (!source_scoped_access) {
Peng Huanga8b0e422019-07-16 17:48:572171 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCopySubTexture",
2172 "Source shared image is not accessable");
2173 } else {
2174 auto color_type = viz::ResourceFormatToClosestSkColorType(
2175 true /* gpu_compositing */, source_shared_image->format());
Nathan Zabriskief1095d7b2019-12-21 00:33:572176
2177 // TODO(http://crbug.com/1034086): We should initialize alpha_type and
2178 // origin using metadata stored with the shared image.
2179 SkAlphaType alpha_type = kPremul_SkAlphaType;
2180 if (unpack_premultiply_alpha)
2181 alpha_type = kUnpremul_SkAlphaType;
Peng Huanga8b0e422019-07-16 17:48:572182 auto source_image = SkImage::MakeFromTexture(
2183 shared_context_state_->gr_context(),
Eric Karl707c1b92019-12-10 06:50:082184 source_scoped_access->promise_image_texture()->backendTexture(),
Nathan Zabriskief1095d7b2019-12-21 00:33:572185 kTopLeft_GrSurfaceOrigin, color_type, alpha_type,
Peng Huanga8b0e422019-07-16 17:48:572186 nullptr /* colorSpace */);
2187
Eric Karl707c1b92019-12-10 06:50:082188 auto* canvas = dest_scoped_access->surface()->getCanvas();
Peng Huanga8b0e422019-07-16 17:48:572189 SkPaint paint;
Nathan Zabriskief1095d7b2019-12-21 00:33:572190 if (unpack_flip_y) {
2191 canvas->scale(1, -1);
2192 canvas->translate(0, -height);
2193 }
Peng Huanga8b0e422019-07-16 17:48:572194 paint.setBlendMode(SkBlendMode::kSrc);
2195 canvas->drawImageRect(source_image, gfx::RectToSkRect(source_rect),
2196 gfx::RectToSkRect(dest_rect), &paint);
2197 }
2198
2199 // Always flush the surface even if source_scoped_access.success() is false,
2200 // so the begin_semaphores can be released, and end_semaphores can be
2201 // signalled.
2202 GrFlushInfo flush_info = {
2203 .fFlags = kNone_GrFlushFlags,
2204 .fNumSemaphores = end_semaphores.size(),
2205 .fSignalSemaphores = end_semaphores.data(),
2206 };
2207 gpu::AddVulkanCleanupTaskForSkiaFlush(
2208 shared_context_state_->vk_context_provider(), &flush_info);
Eric Karl707c1b92019-12-10 06:50:082209 dest_scoped_access->surface()->flush(
Peng Huanga8b0e422019-07-16 17:48:572210 SkSurface::BackendSurfaceAccess::kNoAccess, flush_info);
Eric Karlb25e1b62019-12-17 23:07:492211
2212 if (!dest_shared_image->IsCleared()) {
2213 dest_shared_image->SetClearedRect(new_cleared_rect);
2214 }
Peng Huanga8b0e422019-07-16 17:48:572215}
2216
Jonathan Backere19973e82018-04-18 20:08:092217namespace {
2218
2219// Helper to read client data from transfer cache.
Jonathan Backer4f9ee5fb2018-04-25 14:03:382220class TransferCacheDeserializeHelperImpl final
Jonathan Backere19973e82018-04-18 20:08:092221 : public cc::TransferCacheDeserializeHelper {
2222 public:
2223 explicit TransferCacheDeserializeHelperImpl(
Khushal996e9912018-07-13 08:31:002224 int raster_decoder_id,
Jonathan Backere19973e82018-04-18 20:08:092225 ServiceTransferCache* transfer_cache)
Khushal996e9912018-07-13 08:31:002226 : raster_decoder_id_(raster_decoder_id), transfer_cache_(transfer_cache) {
Jonathan Backere19973e82018-04-18 20:08:092227 DCHECK(transfer_cache_);
2228 }
Jonathan Backer4f9ee5fb2018-04-25 14:03:382229 ~TransferCacheDeserializeHelperImpl() override = default;
Jonathan Backere19973e82018-04-18 20:08:092230
Adrienne Walker90b79a22018-05-08 21:40:422231 void CreateLocalEntry(
2232 uint32_t id,
2233 std::unique_ptr<cc::ServiceTransferCacheEntry> entry) override {
Khushal996e9912018-07-13 08:31:002234 auto type = entry->Type();
2235 transfer_cache_->CreateLocalEntry(
2236 ServiceTransferCache::EntryKey(raster_decoder_id_, type, id),
2237 std::move(entry));
Adrienne Walker90b79a22018-05-08 21:40:422238 }
2239
Jonathan Backere19973e82018-04-18 20:08:092240 private:
2241 cc::ServiceTransferCacheEntry* GetEntryInternal(
2242 cc::TransferCacheEntryType entry_type,
Jonathan Backer4f9ee5fb2018-04-25 14:03:382243 uint32_t entry_id) override {
Khushal996e9912018-07-13 08:31:002244 return transfer_cache_->GetEntry(ServiceTransferCache::EntryKey(
2245 raster_decoder_id_, entry_type, entry_id));
Jonathan Backere19973e82018-04-18 20:08:092246 }
Khushal996e9912018-07-13 08:31:002247
2248 const int raster_decoder_id_;
Jonathan Backere19973e82018-04-18 20:08:092249 ServiceTransferCache* const transfer_cache_;
2250
2251 DISALLOW_COPY_AND_ASSIGN(TransferCacheDeserializeHelperImpl);
2252};
2253
2254} // namespace
2255
Khushal33205a72018-11-08 10:12:292256void RasterDecoderImpl::DeletePaintCacheTextBlobsINTERNALHelper(
2257 GLsizei n,
2258 const volatile GLuint* paint_cache_ids) {
2259 if (!supports_oop_raster_) {
2260 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION,
2261 "glDeletePaintCacheEntriesINTERNAL",
2262 "No chromium raster support");
2263 return;
2264 }
2265
Khushalcb653fb2018-11-15 08:56:282266 paint_cache_->Purge(cc::PaintCacheDataType::kTextBlob, n, paint_cache_ids);
Khushal33205a72018-11-08 10:12:292267}
2268
2269void RasterDecoderImpl::DeletePaintCachePathsINTERNALHelper(
2270 GLsizei n,
2271 const volatile GLuint* paint_cache_ids) {
2272 if (!supports_oop_raster_) {
2273 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION,
2274 "glDeletePaintCacheEntriesINTERNAL",
2275 "No chromium raster support");
2276 return;
2277 }
2278
Khushalcb653fb2018-11-15 08:56:282279 paint_cache_->Purge(cc::PaintCacheDataType::kPath, n, paint_cache_ids);
Khushal33205a72018-11-08 10:12:292280}
2281
2282void RasterDecoderImpl::DoClearPaintCacheINTERNAL() {
2283 if (!supports_oop_raster_) {
2284 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glClearPaintCacheINTERNAL",
2285 "No chromium raster support");
2286 return;
2287 }
2288
2289 paint_cache_->PurgeAll();
2290}
2291
Jonathan Backere19973e82018-04-18 20:08:092292void RasterDecoderImpl::DoBeginRasterCHROMIUM(
Jonathan Backere19973e82018-04-18 20:08:092293 GLuint sk_color,
2294 GLuint msaa_sample_count,
2295 GLboolean can_use_lcd_text,
Jonathan Backer7e7492522018-07-20 00:23:552296 const volatile GLbyte* key) {
Christopher Cameronbeb5f272019-02-04 22:49:132297 // Workaround for https://crbug.com/906453: Flush before BeginRaster (the
2298 // commands between BeginRaster and EndRaster will not flush).
2299 FlushToWorkAroundMacCrashes();
2300
Adrienne Walker2e2270c2019-07-22 20:44:102301 if (!gr_context() || !supports_oop_raster_) {
Jonathan Backere19973e82018-04-18 20:08:092302 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glBeginRasterCHROMIUM",
2303 "chromium_raster_transport not enabled via attribs");
2304 return;
2305 }
2306 if (sk_surface_) {
2307 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glBeginRasterCHROMIUM",
2308 "BeginRasterCHROMIUM without EndRasterCHROMIUM");
2309 return;
2310 }
2311
Jonathan Backer7e7492522018-07-20 00:23:552312 Mailbox mailbox =
2313 Mailbox::FromVolatile(*reinterpret_cast<const volatile Mailbox*>(key));
2314 DLOG_IF(ERROR, !mailbox.Verify()) << "BeginRasterCHROMIUM was "
2315 "passed a mailbox that was not "
2316 "generated by ProduceTextureCHROMIUM.";
Eric Karl3750cfa2018-10-25 16:54:172317
2318 DCHECK(!shared_image_);
Chris Blumeff2de6422019-04-02 01:48:492319 shared_image_ = shared_image_representation_factory_.ProduceSkia(
2320 mailbox, shared_context_state_.get());
Eric Karl3750cfa2018-10-25 16:54:172321 if (!shared_image_) {
Jonathan Backer7e7492522018-07-20 00:23:552322 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glBeginRasterCHROMIUM",
2323 "passed invalid mailbox.");
2324 return;
2325 }
2326
Khushal22204a42018-05-17 23:06:212327 DCHECK(locked_handles_.empty());
Jonathan Backere19973e82018-04-18 20:08:092328 DCHECK(!raster_canvas_);
Peng Huang20361dad12019-01-23 14:48:102329 shared_context_state_->set_need_context_state_reset(true);
Jonathan Backere19973e82018-04-18 20:08:092330
Jonathan Backere19973e82018-04-18 20:08:092331 // Use unknown pixel geometry to disable LCD text.
Jonathan Backera1f3d7c2018-10-16 14:46:322332 uint32_t flags = 0;
Jonathan Backere19973e82018-04-18 20:08:092333 SkSurfaceProps surface_props(flags, kUnknown_SkPixelGeometry);
2334 if (can_use_lcd_text) {
2335 // LegacyFontHost will get LCD text and skia figures out what type to use.
2336 surface_props =
2337 SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
2338 }
2339
Jonathan Backerd95c8032018-12-13 16:51:292340 SkColorType sk_color_type = viz::ResourceFormatToClosestSkColorType(
2341 /*gpu_compositing=*/true, shared_image_->format());
Jonathan Backere19973e82018-04-18 20:08:092342 // If we can't match requested MSAA samples, don't use MSAA.
2343 int final_msaa_count = std::max(static_cast<int>(msaa_sample_count), 0);
2344 if (final_msaa_count >
Khushalb2c140b2018-07-09 20:21:162345 gr_context()->maxSurfaceSampleCountForColorType(sk_color_type))
Jonathan Backere19973e82018-04-18 20:08:092346 final_msaa_count = 0;
Jonathan Backere19973e82018-04-18 20:08:092347
Peng Huang5f9118b02019-04-24 20:18:212348 std::vector<GrBackendSemaphore> begin_semaphores;
2349 DCHECK(end_semaphores_.empty());
Peng Huange9b41cd2019-08-12 19:39:472350 DCHECK(!scoped_shared_image_write_);
Eric Karl14bfb992020-01-03 01:32:212351 // Allow uncleared access, as raster specifically handles uncleared images by
2352 // clearing them before writing.
Eric Karl707c1b92019-12-10 06:50:082353 scoped_shared_image_write_ = shared_image_->BeginScopedWriteAccess(
Eric Karl14bfb992020-01-03 01:32:212354 final_msaa_count, surface_props, &begin_semaphores, &end_semaphores_,
2355 SharedImageRepresentation::AllowUnclearedAccess::kYes);
Eric Karl707c1b92019-12-10 06:50:082356 if (!scoped_shared_image_write_) {
2357 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glBeginRasterCHROMIUM",
2358 "failed to create surface");
2359 shared_image_.reset();
2360 return;
2361 }
2362
Peng Huange9b41cd2019-08-12 19:39:472363 sk_surface_ = scoped_shared_image_write_->surface();
Peng Huang5f9118b02019-04-24 20:18:212364
2365 if (!begin_semaphores.empty()) {
2366 bool result =
2367 sk_surface_->wait(begin_semaphores.size(), begin_semaphores.data());
2368 DCHECK(result);
2369 }
2370
Peng Huang050dee52018-09-05 15:54:082371 if (use_ddl_) {
2372 SkSurfaceCharacterization characterization;
2373 bool result = sk_surface_->characterize(&characterization);
2374 DCHECK(result) << "Failed to characterize raster SkSurface.";
2375 recorder_ =
2376 std::make_unique<SkDeferredDisplayListRecorder>(characterization);
Mike Reed421f75d2019-02-25 20:30:032377 raster_canvas_ = recorder_->getCanvas();
Peng Huang050dee52018-09-05 15:54:082378 } else {
Mike Reed421f75d2019-02-25 20:30:032379 raster_canvas_ = sk_surface_->getCanvas();
Peng Huang050dee52018-09-05 15:54:082380 }
2381
Jonathan Backere19973e82018-04-18 20:08:092382 // All or nothing clearing, as no way to validate the client's input on what
2383 // is the "used" part of the texture.
Jonathan Backere19973e82018-04-18 20:08:092384 // TODO(enne): This doesn't handle the case where the background color
2385 // changes and so any extra pixels outside the raster area that get
2386 // sampled may be incorrect.
Eric Karl3750cfa2018-10-25 16:54:172387 if (shared_image_->IsCleared())
2388 return;
Jonathan Backera1f3d7c2018-10-16 14:46:322389
Eric Karl3750cfa2018-10-25 16:54:172390 raster_canvas_->drawColor(sk_color);
2391 shared_image_->SetCleared();
Jonathan Backere19973e82018-04-18 20:08:092392}
2393
Khushala8d50642018-05-03 01:29:062394scoped_refptr<Buffer> RasterDecoderImpl::GetShmBuffer(uint32_t shm_id) {
2395 return GetSharedMemoryBuffer(shm_id);
2396}
2397
Khushal39641b92019-06-03 21:32:542398void RasterDecoderImpl::ReportProgress() {
2399 if (shared_context_state_->progress_reporter())
2400 shared_context_state_->progress_reporter()->ReportProgress();
2401}
2402
Khushala8d50642018-05-03 01:29:062403void RasterDecoderImpl::DoRasterCHROMIUM(GLuint raster_shm_id,
2404 GLuint raster_shm_offset,
Antoine Labour423fe592019-01-10 22:13:092405 GLuint raster_shm_size,
Khushala8d50642018-05-03 01:29:062406 GLuint font_shm_id,
2407 GLuint font_shm_offset,
Antoine Labour423fe592019-01-10 22:13:092408 GLuint font_shm_size) {
Khushalf9750702018-06-09 00:42:132409 TRACE_EVENT1("gpu", "RasterDecoderImpl::DoRasterCHROMIUM", "raster_id",
2410 ++raster_chromium_id_);
Khushala8d50642018-05-03 01:29:062411
Jonathan Backere19973e82018-04-18 20:08:092412 if (!sk_surface_) {
2413 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glRasterCHROMIUM",
2414 "RasterCHROMIUM without BeginRasterCHROMIUM");
2415 return;
2416 }
Khushal996e9912018-07-13 08:31:002417 DCHECK(transfer_cache());
Peng Huang20361dad12019-01-23 14:48:102418 shared_context_state_->set_need_context_state_reset(true);
Jonathan Backere19973e82018-04-18 20:08:092419
Khushala8d50642018-05-03 01:29:062420 if (font_shm_size > 0) {
2421 // Deserialize fonts before raster.
2422 volatile char* font_buffer_memory =
2423 GetSharedMemoryAs<char*>(font_shm_id, font_shm_offset, font_shm_size);
2424 if (!font_buffer_memory) {
2425 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glRasterCHROMIUM",
2426 "Can not read font buffer.");
2427 return;
2428 }
2429
Khushaldb6ea3c2018-05-31 00:53:092430 std::vector<SkDiscardableHandleId> new_locked_handles;
Khushal3d0b8902018-09-18 03:03:342431 if (!font_manager_->Deserialize(font_buffer_memory, font_shm_size,
2432 &new_locked_handles)) {
Khushala8d50642018-05-03 01:29:062433 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glRasterCHROMIUM",
2434 "Invalid font buffer.");
2435 return;
2436 }
Khushaldb6ea3c2018-05-31 00:53:092437 locked_handles_.insert(locked_handles_.end(), new_locked_handles.begin(),
2438 new_locked_handles.end());
Khushala8d50642018-05-03 01:29:062439 }
2440
2441 char* paint_buffer_memory = GetSharedMemoryAs<char*>(
2442 raster_shm_id, raster_shm_offset, raster_shm_size);
2443 if (!paint_buffer_memory) {
2444 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glRasterCHROMIUM",
2445 "Can not read paint buffer.");
2446 return;
2447 }
2448
Jonathan Backere19973e82018-04-18 20:08:092449 alignas(
2450 cc::PaintOpBuffer::PaintOpAlign) char data[sizeof(cc::LargestPaintOp)];
Jonathan Backere19973e82018-04-18 20:08:092451
Khushal48cbd2a12018-09-18 20:02:512452 cc::PlaybackParams playback_params(nullptr, SkMatrix::I());
Khushal996e9912018-07-13 08:31:002453 TransferCacheDeserializeHelperImpl impl(raster_decoder_id_, transfer_cache());
Adrienne Walker5a4952e2019-01-07 21:16:292454 cc::PaintOp::DeserializeOptions options(
2455 &impl, paint_cache_.get(), font_manager_->strike_client(),
Malay Keshava154dcb2020-01-18 00:17:552456 shared_context_state_->scratch_deserialization_buffer(), is_privileged_);
Adrienne Walkere62484a2018-11-27 18:41:302457 options.crash_dump_on_failure = true;
Jonathan Backere19973e82018-04-18 20:08:092458
Khushala8d50642018-05-03 01:29:062459 size_t paint_buffer_size = raster_shm_size;
Adrienne Walkerd8a56462019-03-16 03:11:472460 gl::ScopedProgressReporter report_progress(
Khushal61977dfc2019-02-22 02:24:292461 shared_context_state_->progress_reporter());
Khushala4156862018-06-08 22:46:022462 while (paint_buffer_size > 0) {
Jonathan Backere19973e82018-04-18 20:08:092463 size_t skip = 0;
2464 cc::PaintOp* deserialized_op = cc::PaintOp::Deserialize(
Khushala8d50642018-05-03 01:29:062465 paint_buffer_memory, paint_buffer_size, &data[0],
2466 sizeof(cc::LargestPaintOp), &skip, options);
Jonathan Backere19973e82018-04-18 20:08:092467 if (!deserialized_op) {
Adrienne Walkere62484a2018-11-27 18:41:302468 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glRasterCHROMIUM",
2469 "RasterCHROMIUM: serialization failure");
Jonathan Backere19973e82018-04-18 20:08:092470 return;
2471 }
2472
Mike Reed421f75d2019-02-25 20:30:032473 deserialized_op->Raster(raster_canvas_, playback_params);
Christopher Camerond5698f682018-12-11 00:59:202474 deserialized_op->DestroyThis();
2475
Christopher Camerond5698f682018-12-11 00:59:202476 paint_buffer_size -= skip;
2477 paint_buffer_memory += skip;
Jonathan Backere19973e82018-04-18 20:08:092478 }
Jonathan Backere19973e82018-04-18 20:08:092479}
2480
2481void RasterDecoderImpl::DoEndRasterCHROMIUM() {
Peng Huang050dee52018-09-05 15:54:082482 TRACE_EVENT0("gpu", "RasterDecoderImpl::DoEndRasterCHROMIUM");
Jonathan Backere19973e82018-04-18 20:08:092483 if (!sk_surface_) {
Jonathan Backera1f3d7c2018-10-16 14:46:322484 LOCAL_SET_GL_ERROR(GL_INVALID_OPERATION, "glEndRasterCHROMIUM",
Jonathan Backere19973e82018-04-18 20:08:092485 "EndRasterCHROMIUM without BeginRasterCHROMIUM");
2486 return;
2487 }
2488
Peng Huang20361dad12019-01-23 14:48:102489 shared_context_state_->set_need_context_state_reset(true);
Jonathan Backer4fb9f5acb2018-05-10 22:19:332490
Mike Reed421f75d2019-02-25 20:30:032491 raster_canvas_ = nullptr;
Peng Huang050dee52018-09-05 15:54:082492
2493 if (use_ddl_) {
Peng Huangd9608432019-07-29 15:56:362494 TRACE_EVENT0("gpu",
2495 "RasterDecoderImpl::DoEndRasterCHROMIUM::DetachAndDrawDDL");
Peng Huang050dee52018-09-05 15:54:082496 auto ddl = recorder_->detach();
2497 recorder_ = nullptr;
2498 sk_surface_->draw(ddl.get());
2499 }
Khushal61977dfc2019-02-22 02:24:292500
2501 {
Peng Huangd9608432019-07-29 15:56:362502 TRACE_EVENT0("gpu", "RasterDecoderImpl::DoEndRasterCHROMIUM::Flush");
Khushal61977dfc2019-02-22 02:24:292503 // This is a slow operation since skia will execute the GPU work for the
2504 // complete tile. Make sure the progress reporter is notified to avoid
2505 // hangs.
Adrienne Walkerd8a56462019-03-16 03:11:472506 gl::ScopedProgressReporter report_progress(
Khushal61977dfc2019-02-22 02:24:292507 shared_context_state_->progress_reporter());
Peng Huang5f9118b02019-04-24 20:18:212508 GrFlushInfo flush_info = {
2509 .fFlags = kNone_GrFlushFlags,
2510 .fNumSemaphores = end_semaphores_.size(),
2511 .fSignalSemaphores = end_semaphores_.data(),
2512 };
Sergey Ulanovc528c0d2019-05-08 21:06:062513 AddVulkanCleanupTaskForSkiaFlush(
Eric Karl32d9d2e2019-05-03 17:09:472514 shared_context_state_->vk_context_provider(), &flush_info);
Peng Huang72545ea2019-07-24 20:42:362515 auto result = sk_surface_->flush(SkSurface::BackendSurfaceAccess::kPresent,
2516 flush_info);
2517 DCHECK(result == GrSemaphoresSubmitted::kYes || end_semaphores_.empty());
Peng Huang5f9118b02019-04-24 20:18:212518 end_semaphores_.clear();
Khushal61977dfc2019-02-22 02:24:292519 }
2520
Kramer Geddcac3532019-12-12 23:33:102521 shared_context_state_->UpdateSkiaOwnedMemorySize();
Peng Huange9b41cd2019-08-12 19:39:472522 sk_surface_ = nullptr;
Eric Karl3750cfa2018-10-25 16:54:172523 if (!shared_image_) {
2524 // Test only path for SetUpForRasterCHROMIUMForTest.
Peng Huange9b41cd2019-08-12 19:39:472525 sk_surface_for_testing_.reset();
Eric Karl3750cfa2018-10-25 16:54:172526 } else {
Peng Huange9b41cd2019-08-12 19:39:472527 scoped_shared_image_write_.reset();
Eric Karl3750cfa2018-10-25 16:54:172528 shared_image_.reset();
2529 }
Khushal22204a42018-05-17 23:06:212530
2531 // Unlock all font handles. This needs to be deferred until
Greg Daniel18362e9d2019-04-15 19:54:502532 // SkSurface::flush since that flushes batched Gr operations
Peng Huangc6a76072018-11-27 23:17:312533 // in skia that access the glyph data.
2534 // TODO(khushalsagar): We just unlocked a bunch of handles, do we need to
2535 // give a call to skia to attempt to purge any unlocked handles?
Khushal3d0b8902018-09-18 03:03:342536 if (!font_manager_->Unlock(locked_handles_)) {
Khushal22204a42018-05-17 23:06:212537 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glRasterCHROMIUM",
2538 "Invalid font discardable handle.");
2539 }
2540 locked_handles_.clear();
Khushal15b6abb2018-06-28 00:16:252541
2542 // We just flushed a tile's worth of GPU work from the SkSurface in
Khushal Sagarb3f53292019-06-05 04:24:232543 // flush above. Yield to the Scheduler to allow pre-emption before
2544 // processing more commands.
2545 ExitCommandProcessingEarly();
Jonathan Backere19973e82018-04-18 20:08:092546}
2547
Jonathan Backer0cd1c4322018-04-17 16:57:102548void RasterDecoderImpl::DoCreateTransferCacheEntryINTERNAL(
2549 GLuint raw_entry_type,
2550 GLuint entry_id,
2551 GLuint handle_shm_id,
2552 GLuint handle_shm_offset,
2553 GLuint data_shm_id,
2554 GLuint data_shm_offset,
2555 GLuint data_size) {
2556 if (!supports_oop_raster_) {
2557 LOCAL_SET_GL_ERROR(
2558 GL_INVALID_VALUE, "glCreateTransferCacheEntryINTERNAL",
2559 "Attempt to use OOP transfer cache on a context without OOP raster.");
2560 return;
2561 }
Khushalb2c140b2018-07-09 20:21:162562 DCHECK(gr_context());
Khushal996e9912018-07-13 08:31:002563 DCHECK(transfer_cache());
Jonathan Backer0cd1c4322018-04-17 16:57:102564
2565 // Validate the type we are about to create.
2566 cc::TransferCacheEntryType entry_type;
2567 if (!cc::ServiceTransferCacheEntry::SafeConvertToType(raw_entry_type,
2568 &entry_type)) {
Peng Huangc6a76072018-11-27 23:17:312569 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCreateTransferCacheEntryINTERNAL",
2570 "Attempt to use OOP transfer cache with an invalid "
2571 "cache entry type.");
Jonathan Backer0cd1c4322018-04-17 16:57:102572 return;
2573 }
2574
2575 uint8_t* data_memory =
2576 GetSharedMemoryAs<uint8_t*>(data_shm_id, data_shm_offset, data_size);
2577 if (!data_memory) {
2578 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCreateTransferCacheEntryINTERNAL",
2579 "Can not read transfer cache entry data.");
2580 return;
2581 }
2582
2583 scoped_refptr<Buffer> handle_buffer = GetSharedMemoryBuffer(handle_shm_id);
2584 if (!DiscardableHandleBase::ValidateParameters(handle_buffer.get(),
2585 handle_shm_offset)) {
2586 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCreateTransferCacheEntryINTERNAL",
2587 "Invalid shm for discardable handle.");
2588 return;
2589 }
2590 ServiceDiscardableHandle handle(std::move(handle_buffer), handle_shm_offset,
2591 handle_shm_id);
2592
Peng Huangc6a76072018-11-27 23:17:312593 // If the entry is going to use skia during deserialization, make sure we
2594 // mark the context state dirty.
Khushal6a090ff2018-11-17 04:12:232595 GrContext* context_for_entry =
2596 cc::ServiceTransferCacheEntry::UsesGrContext(entry_type) ? gr_context()
2597 : nullptr;
2598 if (context_for_entry)
Peng Huang20361dad12019-01-23 14:48:102599 shared_context_state_->set_need_context_state_reset(true);
Khushal6a090ff2018-11-17 04:12:232600
Khushal996e9912018-07-13 08:31:002601 if (!transfer_cache()->CreateLockedEntry(
2602 ServiceTransferCache::EntryKey(raster_decoder_id_, entry_type,
2603 entry_id),
Khushal6a090ff2018-11-17 04:12:232604 handle, context_for_entry, base::make_span(data_memory, data_size))) {
Jonathan Backer0cd1c4322018-04-17 16:57:102605 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glCreateTransferCacheEntryINTERNAL",
2606 "Failure to deserialize transfer cache entry.");
2607 return;
2608 }
Khushal Sagarb3f53292019-06-05 04:24:232609
2610 // The only entry using the GrContext are image transfer cache entries for
2611 // image uploads. Since this tends to a slow operation, yield to allow the
2612 // decoder to be pre-empted.
2613 if (context_for_entry)
2614 ExitCommandProcessingEarly();
Jonathan Backer0cd1c4322018-04-17 16:57:102615}
2616
2617void RasterDecoderImpl::DoUnlockTransferCacheEntryINTERNAL(
2618 GLuint raw_entry_type,
2619 GLuint entry_id) {
2620 if (!supports_oop_raster_) {
2621 LOCAL_SET_GL_ERROR(
2622 GL_INVALID_VALUE, "glUnlockTransferCacheEntryINTERNAL",
2623 "Attempt to use OOP transfer cache on a context without OOP raster.");
2624 return;
2625 }
Khushal996e9912018-07-13 08:31:002626 DCHECK(transfer_cache());
Jonathan Backer0cd1c4322018-04-17 16:57:102627 cc::TransferCacheEntryType entry_type;
2628 if (!cc::ServiceTransferCacheEntry::SafeConvertToType(raw_entry_type,
2629 &entry_type)) {
Peng Huangc6a76072018-11-27 23:17:312630 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glUnlockTransferCacheEntryINTERNAL",
2631 "Attempt to use OOP transfer cache with an invalid "
2632 "cache entry type.");
Jonathan Backer0cd1c4322018-04-17 16:57:102633 return;
2634 }
2635
Khushal996e9912018-07-13 08:31:002636 if (!transfer_cache()->UnlockEntry(ServiceTransferCache::EntryKey(
2637 raster_decoder_id_, entry_type, entry_id))) {
Jonathan Backer0cd1c4322018-04-17 16:57:102638 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glUnlockTransferCacheEntryINTERNAL",
2639 "Attempt to unlock an invalid ID");
2640 }
2641}
2642
2643void RasterDecoderImpl::DoDeleteTransferCacheEntryINTERNAL(
2644 GLuint raw_entry_type,
2645 GLuint entry_id) {
2646 if (!supports_oop_raster_) {
2647 LOCAL_SET_GL_ERROR(
2648 GL_INVALID_VALUE, "glDeleteTransferCacheEntryINTERNAL",
2649 "Attempt to use OOP transfer cache on a context without OOP raster.");
2650 return;
2651 }
Khushal996e9912018-07-13 08:31:002652 DCHECK(transfer_cache());
Jonathan Backer0cd1c4322018-04-17 16:57:102653 cc::TransferCacheEntryType entry_type;
2654 if (!cc::ServiceTransferCacheEntry::SafeConvertToType(raw_entry_type,
2655 &entry_type)) {
Peng Huangc6a76072018-11-27 23:17:312656 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glDeleteTransferCacheEntryINTERNAL",
2657 "Attempt to use OOP transfer cache with an invalid "
2658 "cache entry type.");
Jonathan Backer0cd1c4322018-04-17 16:57:102659 return;
2660 }
2661
Khushal996e9912018-07-13 08:31:002662 if (!transfer_cache()->DeleteEntry(ServiceTransferCache::EntryKey(
2663 raster_decoder_id_, entry_type, entry_id))) {
Jonathan Backer0cd1c4322018-04-17 16:57:102664 LOCAL_SET_GL_ERROR(GL_INVALID_VALUE, "glDeleteTransferCacheEntryINTERNAL",
2665 "Attempt to delete an invalid ID");
2666 }
2667}
2668
Jonathan Backer10821a82018-04-04 23:56:032669void RasterDecoderImpl::RestoreStateForAttrib(GLuint attrib_index,
2670 bool restore_array_binding) {
Peng Huang20361dad12019-01-23 14:48:102671 shared_context_state_->PessimisticallyResetGrContext();
Jonathan Backer10821a82018-04-04 23:56:032672}
2673
Jonathan Backer7471b2782018-01-25 18:14:192674// Include the auto-generated part of this file. We split this because it means
2675// we can easily edit the non-auto generated parts right here in this file
2676// instead of having to edit some template or the code generator.
2677#include "base/macros.h"
Jonathan Backera4568da12018-01-31 16:25:042678#include "gpu/command_buffer/service/raster_decoder_autogen.h"
Jonathan Backer7471b2782018-01-25 18:14:192679
Jonathan Backer7f90dfb662017-12-18 16:52:042680} // namespace raster
2681} // namespace gpu